Skip to content

Instantly share code, notes, and snippets.

View nipafx's full-sized avatar

Nicolai Parlog nipafx

View GitHub Profile
@nipafx
nipafx / HelloJUnit5_viaJUnit4.java
Created March 18, 2016 12:57
Running JUnit 5 tests with JUnit 4
package org.codefx.demo.junit5;
import org.junit.gen5.api.Test;
import org.junit.gen5.junit4.runner.JUnit5;
import org.junit.runner.RunWith;
/**
* Typical "Hello World"; runs as a JUnit 4 test.
*/
@RunWith(JUnit5.class)
@nipafx
nipafx / GetURLStreamHandler.java
Last active April 9, 2016 13:00
Accessing Package Scoped Methods In Java 9
package org.codefx.lab;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLStreamHandler;
public class GetURLStreamHandler {
public static void main(String[] args) throws Exception {
printDefaultPortForProtocol("http");
@nipafx
nipafx / Reordering.java
Created May 3, 2016 10:03
Exposing partially constructed instances
public class Reordering {
private static Constructed constructed;
public static void main(String[] args) throws Exception {
new Thread(Reordering::constructForever).start();
Thread.sleep(100);
checkInitializationUntilFails();
}
@nipafx
nipafx / PackagePrivate.java
Created May 11, 2016 21:34
Demonstrating Visibility
package org.codefx.lab.visibility;
class PackagePrivate {
public String publicToUpper(String s) {
return s.toUpperCase();
}
String packageToUpper(String s) {
return s.toUpperCase();
@nipafx
nipafx / MethodReferencesToStaticMethodsDemo.java
Created May 25, 2016 10:49
How cool would it be if `class::foo` would be a method reference to the static method `foo`
public class MethodReferencesToStaticMethodsDemo {
public Optional<String> firstSpecialString(Collection<String> strings) {
return strings.stream()
.filter(MethodReferencesToStaticMethodsDemo::isSpecial)
.findFirst();
}
public Optional<String> anySpecialString(Collection<String> strings) {
return strings.stream()
@nipafx
nipafx / ParallelStream.java
Created June 28, 2016 20:50
Checking in which pool parallel streams are executed
package org.codefx.lab.stream;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ForkJoinPool;
import java.util.function.Consumer;
import java.util.stream.IntStream;
public class ParallelStream {
public static void main(String[] args) throws InterruptedException {
@nipafx
nipafx / build.gradle
Last active August 8, 2018 23:32
Publish project snapshots to Sonatype's snapshot repo with Gradle's maven-publish Plugin
// This is the Gist for a post I wrote about publishing snapshots with Gradle's maven-publish plugin:
// http://blog.codefx.org/tools/snapshots-gradle-maven-publish-plugin
// PROJECT_NAME is defined in settings.gradle
group 'PROJECT_GROUP'
version 'PROJECT_VERSION'
apply plugin: 'java'
apply plugin: 'maven-publish'
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="statistics-unit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test;INIT=CREATE SCHEMA IF NOT EXISTS test\;RUNSCRIPT FROM 'classpath:META-INF/init.sql'" />
@nipafx
nipafx / RandomParameterExtensionTest.java
Created May 21, 2017 12:17
Injecting random data into Jupiter tests
import org.codefx.demo.junit5.extensions.RandomParameterExtensionTest.RandomProvider;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
@nipafx
nipafx / JavaVersionSniffer.java
Last active November 30, 2017 10:06
Get the major Java version on Java 8 and earlier and on Java 9 and later
/*
* RELEASED UNDER CC-0 (https://creativecommons.org/publicdomain/zero/1.0/):
*
* You can copy, modify, distribute and perform the work, even for commercial
* purposes, all without asking permission.
*/
import java.lang.reflect.Method;
public class JavaVersionSniffer {