Created
May 14, 2020 01:15
-
-
Save mageddo/d72d112e75e8815cbc61c0bc525653bb to your computer and use it in GitHub Desktop.
Save extension state between tests executions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.util.List; | |
import java.util.concurrent.CopyOnWriteArrayList; | |
import java.util.function.Consumer; | |
import org.junit.jupiter.api.extension.AfterAllCallback; | |
import org.junit.jupiter.api.extension.BeforeAllCallback; | |
import org.junit.jupiter.api.extension.ExtendWith; | |
import org.junit.jupiter.api.extension.ExtensionContext; | |
import org.junit.jupiter.api.extension.ExtensionContext.Store; | |
import org.junit.jupiter.api.extension.RegisterExtension; | |
import io.quarkus.test.junit.QuarkusTest; | |
import io.zonky.test.db.postgres.embedded.EmbeddedPostgres; | |
import lombok.SneakyThrows; | |
@ExtendWith(SingleInstancePostgresExtension.class) | |
@QuarkusTest | |
class StockPriceServiceTest { | |
@RegisterExtension | |
public static final SingleInstancePostgresExtension postgres = new SingleInstancePostgresExtension(); | |
} | |
public class SingleInstancePostgresExtension implements BeforeAllCallback, AfterAllCallback { | |
private final List<Consumer<EmbeddedPostgres.Builder>> builderCustomizers = new CopyOnWriteArrayList<>(); | |
public SingleInstancePostgresExtension() { | |
} | |
@Override | |
public void beforeAll(ExtensionContext context) throws Exception { | |
final Store store = context | |
.getRoot() | |
.getStore(ExtensionContext.Namespace.GLOBAL); | |
if (store.get(EmbeddedPostgres.class.getName(), EmbeddedPostgres.class) == null) { | |
setNewInstanceOnContext(store); | |
} | |
} | |
@Override | |
public void afterAll(ExtensionContext context) throws Exception { | |
} | |
private void setNewInstanceOnContext(Store store) throws IOException { | |
this.customize(customizer -> { | |
customizer.setPort(5531); | |
}); | |
final EmbeddedPostgres instance = this.pg(); | |
store.put(EmbeddedPostgres.class.getName(), instance); | |
Runtime.getRuntime() | |
.addShutdownHook(new Thread() { | |
@SneakyThrows | |
public void run() { | |
instance.close(); | |
} | |
}); | |
} | |
public static SingleInstancePostgresExtension singleton() { | |
return new SingleInstancePostgresExtension(); | |
} | |
private EmbeddedPostgres pg() throws IOException { | |
final EmbeddedPostgres.Builder builder = EmbeddedPostgres.builder(); | |
this.builderCustomizers.forEach(c -> c.accept(builder)); | |
return builder.start(); | |
} | |
public SingleInstancePostgresExtension customize(Consumer<EmbeddedPostgres.Builder> customizer) { | |
this.builderCustomizers.add(customizer); | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment