|
| 1 | +package io.jooby; |
| 2 | + |
| 3 | +import com.typesafe.config.Config; |
| 4 | +import org.junit.jupiter.api.extension.AfterAllCallback; |
| 5 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 6 | +import org.junit.jupiter.api.extension.BeforeAllCallback; |
| 7 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 8 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 9 | +import org.junit.jupiter.api.extension.ParameterContext; |
| 10 | +import org.junit.jupiter.api.extension.ParameterResolutionException; |
| 11 | +import org.junit.jupiter.api.extension.ParameterResolver; |
| 12 | +import org.junit.jupiter.api.extension.TestInstancePostProcessor; |
| 13 | + |
| 14 | +import java.lang.reflect.Field; |
| 15 | +import java.lang.reflect.Method; |
| 16 | +import java.lang.reflect.Parameter; |
| 17 | +import java.util.Optional; |
| 18 | +import java.util.function.Supplier; |
| 19 | + |
| 20 | +/** |
| 21 | + * JUnit extension that control lifecycle of Jooby applications on tests. The extension shouldn't |
| 22 | + * use it directly. Usage is done via {@link JoobyTest} annotation. |
| 23 | + * |
| 24 | + * @author edgar |
| 25 | + * @since 2.0.0 |
| 26 | + */ |
| 27 | +public class JoobyExtension implements BeforeAllCallback, BeforeEachCallback, AfterAllCallback, |
| 28 | + AfterEachCallback, ParameterResolver, TestInstancePostProcessor { |
| 29 | + |
| 30 | + private static final int DEFAULT_PORT = 8911; |
| 31 | + |
| 32 | + static { |
| 33 | + System.setProperty("jooby.useShutdownHook", "false"); |
| 34 | + } |
| 35 | + |
| 36 | + @Override public void beforeAll(ExtensionContext context) throws Exception { |
| 37 | + context.getElement().ifPresent(element -> { |
| 38 | + JoobyTest metadata = element.getAnnotation(JoobyTest.class); |
| 39 | + if (metadata != null) { |
| 40 | + startApp(context, metadata.value(), metadata.environment(), |
| 41 | + port(metadata.port(), DEFAULT_PORT), metadata.executionMode()); |
| 42 | + } |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + private Jooby startApp(ExtensionContext context, Class applicationClass, |
| 47 | + String environment, int port, ExecutionMode executionMode) { |
| 48 | + Jooby app = Jooby.createApp(new String[]{environment}, executionMode, applicationClass); |
| 49 | + ServerOptions serverOptions = app.getServerOptions(); |
| 50 | + if (serverOptions == null) { |
| 51 | + serverOptions = new ServerOptions(); |
| 52 | + app.setServerOptions(serverOptions); |
| 53 | + } |
| 54 | + serverOptions.setPort(port); |
| 55 | + Server server = app.start(); |
| 56 | + ExtensionContext.Store store = getStore(context); |
| 57 | + store.put("server", server); |
| 58 | + store.put("application", app); |
| 59 | + return app; |
| 60 | + } |
| 61 | + |
| 62 | + @Override public void beforeEach(ExtensionContext context) throws Exception { |
| 63 | + context.getElement().ifPresent(element -> { |
| 64 | + JoobyTest metadata = element.getAnnotation(JoobyTest.class); |
| 65 | + if (metadata != null) { |
| 66 | + startApp(context, metadata.value(), metadata.environment(), |
| 67 | + port(metadata.port(), 0), metadata.executionMode()); |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + @Override public void afterAll(ExtensionContext context) throws Exception { |
| 73 | + Server server = getStore(context).get("server", Server.class); |
| 74 | + if (server != null) { |
| 75 | + server.stop(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override public void afterEach(ExtensionContext context) throws Exception { |
| 80 | + Server server = getStore(context).get("server", Server.class); |
| 81 | + if (server != null) { |
| 82 | + server.stop(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override public boolean supportsParameter(ParameterContext parameterContext, |
| 87 | + ExtensionContext context) throws ParameterResolutionException { |
| 88 | + return joobyParameter(context, parameterContext) != null; |
| 89 | + } |
| 90 | + |
| 91 | + @Override public Object resolveParameter(ParameterContext parameterContext, |
| 92 | + ExtensionContext context) throws ParameterResolutionException { |
| 93 | + return joobyParameter(context, parameterContext).get(); |
| 94 | + } |
| 95 | + |
| 96 | + private ExtensionContext.Store getStore(ExtensionContext context) { |
| 97 | + Optional<Method> testMethod = context.getTestMethod(); |
| 98 | + ExtensionContext.Namespace namespace = testMethod.isPresent() |
| 99 | + ? ExtensionContext.Namespace.create(context.getRequiredTestClass(), testMethod.get()) |
| 100 | + : ExtensionContext.Namespace.create(context.getRequiredTestClass()); |
| 101 | + return context.getStore(namespace); |
| 102 | + } |
| 103 | + |
| 104 | + private Supplier<Object> joobyParameter(ExtensionContext context, |
| 105 | + ParameterContext parameterContext) { |
| 106 | + Parameter parameter = parameterContext.getParameter(); |
| 107 | + return injectionPoint(context, parameter.getType(), parameter.getName()); |
| 108 | + } |
| 109 | + |
| 110 | + private Supplier<Object> injectionPoint(ExtensionContext context, |
| 111 | + Class type, String name) { |
| 112 | + if (name.equals("serverPath") && type == String.class) { |
| 113 | + return () -> { |
| 114 | + Jooby app = application(context); |
| 115 | + return "http://localhost:" + app.getServerOptions().getPort() + app.getContextPath(); |
| 116 | + }; |
| 117 | + } |
| 118 | + if (name.equals("serverPort") && type == int.class) { |
| 119 | + return () -> { |
| 120 | + Jooby app = application(context); |
| 121 | + return app.getServerOptions().getPort(); |
| 122 | + }; |
| 123 | + } |
| 124 | + if (Jooby.class.isAssignableFrom(type)) { |
| 125 | + return () -> application(context); |
| 126 | + } |
| 127 | + if (Environment.class.isAssignableFrom(type)) { |
| 128 | + return () -> application(context).getEnvironment(); |
| 129 | + } |
| 130 | + if (Config.class.isAssignableFrom(type)) { |
| 131 | + return () -> application(context).getEnvironment().getConfig(); |
| 132 | + } |
| 133 | + throw new IllegalArgumentException("Unsupported: " + type + " " + name); |
| 134 | + } |
| 135 | + |
| 136 | + private Jooby application(ExtensionContext context) { |
| 137 | + Jooby application = getStore(context).get("application", Jooby.class); |
| 138 | + if (application == null) { |
| 139 | + ExtensionContext parent = context.getParent() |
| 140 | + .orElseThrow(() -> new IllegalStateException("Application not found")); |
| 141 | + application = getStore(parent).get("application", Jooby.class); |
| 142 | + } |
| 143 | + if (application == null) { |
| 144 | + throw new IllegalStateException("Application not found"); |
| 145 | + } |
| 146 | + return application; |
| 147 | + } |
| 148 | + |
| 149 | + private int port(int port, int fallback) { |
| 150 | + return port == -1 ? fallback : port; |
| 151 | + } |
| 152 | + |
| 153 | + @Override public void postProcessTestInstance(Object instance, ExtensionContext context) |
| 154 | + throws Exception { |
| 155 | + for (Field field : instance.getClass().getFields()) { |
| 156 | + Supplier<Object> injectionPoint = injectionPoint(context, field.getType(), field.getName()); |
| 157 | + if (injectionPoint != null) { |
| 158 | + field.set(instance, injectionPoint.get()); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments