|
| 1 | +package graphql.kickstart.tools.boot; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.mock; |
| 4 | + |
| 5 | +import javax.servlet.ServletContext; |
| 6 | +import javax.websocket.server.ServerContainer; |
| 7 | +import org.junit.After; |
| 8 | +import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; |
| 9 | +import org.springframework.boot.test.util.TestPropertyValues; |
| 10 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 11 | +import org.springframework.context.annotation.AnnotationConfigRegistry; |
| 12 | +import org.springframework.context.support.AbstractApplicationContext; |
| 13 | +import org.springframework.mock.web.MockServletContext; |
| 14 | +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Andrew Potter |
| 18 | + */ |
| 19 | +public abstract class AbstractAutoConfigurationTest { |
| 20 | + |
| 21 | + private final Class<? extends AbstractApplicationContext> contextClass; |
| 22 | + private final Class<?> autoConfiguration; |
| 23 | + |
| 24 | + private AbstractApplicationContext context; |
| 25 | + |
| 26 | + protected AbstractAutoConfigurationTest(Class<?> autoConfiguration) { |
| 27 | + this(AnnotationConfigApplicationContext.class, autoConfiguration); |
| 28 | + } |
| 29 | + |
| 30 | + protected AbstractAutoConfigurationTest(Class<? extends AbstractApplicationContext> contextClass, |
| 31 | + Class<?> autoConfiguration) { |
| 32 | + assert AnnotationConfigRegistry.class.isAssignableFrom(contextClass); |
| 33 | + this.contextClass = contextClass; |
| 34 | + this.autoConfiguration = autoConfiguration; |
| 35 | + } |
| 36 | + |
| 37 | + @After |
| 38 | + public void tearDown() { |
| 39 | + if (this.context != null) { |
| 40 | + this.context.close(); |
| 41 | + this.context = null; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + protected void load(Class<?> config, String... environment) { |
| 46 | + try { |
| 47 | + this.context = contextClass.newInstance(); |
| 48 | + } catch (InstantiationException | IllegalAccessException e) { |
| 49 | + throw new RuntimeException("Failed to instantiate testing context", e); |
| 50 | + } |
| 51 | + |
| 52 | + if (environment != null && environment.length > 0) { |
| 53 | + TestPropertyValues.of(environment).applyTo(context); |
| 54 | + } |
| 55 | + |
| 56 | + getRegistry().register(config); |
| 57 | + getRegistry().register(autoConfiguration); |
| 58 | + getRegistry().register(JacksonAutoConfiguration.class); |
| 59 | + |
| 60 | + loadServletContext(); |
| 61 | + getContext().refresh(); |
| 62 | + } |
| 63 | + |
| 64 | + private void loadServletContext() { |
| 65 | + if (context instanceof AnnotationConfigWebApplicationContext) { |
| 66 | + ServerContainer serverContainer = mock(ServerContainer.class); |
| 67 | + ServletContext servletContext = new MockServletContext(); |
| 68 | + servletContext.setAttribute("javax.websocket.server.ServerContainer", serverContainer); |
| 69 | + ((AnnotationConfigWebApplicationContext) context).setServletContext(servletContext); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public AnnotationConfigRegistry getRegistry() { |
| 74 | + return (AnnotationConfigRegistry) context; |
| 75 | + } |
| 76 | + |
| 77 | + public AbstractApplicationContext getContext() { |
| 78 | + return context; |
| 79 | + } |
| 80 | +} |
0 commit comments