Skip to content

Instantly share code, notes, and snippets.

View nipafx's full-sized avatar

Nicolai Parlog nipafx

View GitHub Profile
@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;
<?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 / 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'
@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 / 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 / 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 / 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 / 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 / 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 / Factors.java
Last active February 2, 2016 07:11
Computing factors with Java 8 streams - see https://bit.ly/1VF85Fa 17:28
package org.codefx.lab.aliquot;
import com.google.common.collect.ImmutableSetMultimap;
import org.junit.Test;
import java.util.Collection;
import java.util.List;
import java.util.stream.IntStream;
import static java.lang.Math.sqrt;