/** * Refresh the given application context, if necessary. */ protected void refreshApplicationContext(ApplicationContext context) { if (context instanceof ConfigurableApplicationContext) { ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context; if (!cac.isActive()) { cac.refresh(); } } }
/** * Get the {@linkplain ApplicationContext application context} for this * test context. * <p>The default implementation delegates to the {@link CacheAwareContextLoaderDelegate} * that was supplied when this {@code TestContext} was constructed. * @throws IllegalStateException if the context returned by the context * loader delegate is not <em>active</em> (i.e., has been closed). * @see CacheAwareContextLoaderDelegate#loadContext */ public ApplicationContext getApplicationContext() { ApplicationContext context = this.cacheAwareContextLoaderDelegate.loadContext(this.mergedContextConfiguration); if (context instanceof ConfigurableApplicationContext) { @SuppressWarnings("resource") ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context; Assert.state(cac.isActive(), () -> "The ApplicationContext loaded for [" + this.mergedContextConfiguration + "] is not active. This may be due to one of the following reasons: " + "1) the context was closed programmatically by user code; " + "2) the context was closed during parallel test execution either " + "according to @DirtiesContext semantics or due to automatic eviction " + "from the ContextCache due to a maximum cache size policy."); } return context; }
/** * Refresh the given application context, if necessary. */ protected void refreshApplicationContext(ApplicationContext context) { if (context instanceof ConfigurableApplicationContext) { ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context; if (!cac.isActive()) { cac.refresh(); } } }
@Override public void onApplicationEvent(ContextClosedEvent event) { ConfigurableApplicationContext context = this.childContext.get(); if ((context != null) && (event.getApplicationContext() == context.getParent()) && context.isActive()) { context.close(); } }
logger.info(commonContext.getId() + " isActive: " + commonContext.isActive()); new SpringApplicationBuilder(ConfigServiceApplication.class).parent(commonContext) .sources(RefreshScope.class).run(args); logger.info(configContext.getId() + " isActive: " + configContext.isActive()); new SpringApplicationBuilder(AdminServiceApplication.class).parent(commonContext) .sources(RefreshScope.class).run(args); logger.info(adminContext.getId() + " isActive: " + adminContext.isActive()); new SpringApplicationBuilder(PortalApplication.class).parent(commonContext) .sources(RefreshScope.class).run(args); logger.info(portalContext.getId() + " isActive: " + portalContext.isActive());
/** * Unregister all the jobs and close all the contexts created by this * loader. * * @see JobLoader#clear() */ @Override public void clear() { for (ConfigurableApplicationContext context : contexts.values()) { if (context.isActive()) { context.close(); } } for (String jobName : jobRegistry.getJobNames()) { doUnregister(jobName); } contexts.clear(); contextToJobNames.clear(); }
if (!cac.isActive()) {
private int getExitCodeFromMappedException(ConfigurableApplicationContext context, Throwable exception) { if (context == null || !context.isActive()) { return 0; } ExitCodeGenerators generators = new ExitCodeGenerators(); Collection<ExitCodeExceptionMapper> beans = context .getBeansOfType(ExitCodeExceptionMapper.class).values(); generators.addAll(exception, beans); return generators.getExitCode(); }
private void performAssertions(int expectedContextCreationCount) throws Exception { assertNotNull("context must not be null", this.context); assertTrue("context must be active", this.context.isActive()); assertNotNull("count must not be null", this.count); assertEquals("count: ", expectedContextCreationCount, this.count.intValue()); assertEquals("context creation count: ", expectedContextCreationCount, contextCount.get()); }
if (!cac.isActive()) {
private void runTestAndVerifyHierarchies(Class<? extends FooTestCase> testClass, boolean isFooContextActive, boolean isBarContextActive, boolean isBazContextActive) { JUnitCore jUnitCore = new JUnitCore(); Result result = jUnitCore.run(testClass); assertTrue("all tests passed", result.wasSuccessful()); assertThat(ContextHierarchyDirtiesContextTests.context, notNullValue()); ConfigurableApplicationContext bazContext = (ConfigurableApplicationContext) ContextHierarchyDirtiesContextTests.context; assertEquals("baz", ContextHierarchyDirtiesContextTests.baz); assertThat("bazContext#isActive()", bazContext.isActive(), is(isBazContextActive)); ConfigurableApplicationContext barContext = (ConfigurableApplicationContext) bazContext.getParent(); assertThat(barContext, notNullValue()); assertEquals("bar", ContextHierarchyDirtiesContextTests.bar); assertThat("barContext#isActive()", barContext.isActive(), is(isBarContextActive)); ConfigurableApplicationContext fooContext = (ConfigurableApplicationContext) barContext.getParent(); assertThat(fooContext, notNullValue()); assertEquals("foo", ContextHierarchyDirtiesContextTests.foo); assertThat("fooContext#isActive()", fooContext.isActive(), is(isFooContextActive)); }
@Override public void failed(ConfigurableApplicationContext context, Throwable exception) { ApplicationFailedEvent event = new ApplicationFailedEvent(this.application, this.args, context, exception); if (context != null && context.isActive()) { // Listeners have been registered to the application context so we should // use it at this point if we can context.publishEvent(event); } else { // An inactive context may not have a multicaster so we use our multicaster to // call all of the context's listeners instead if (context instanceof AbstractApplicationContext) { for (ApplicationListener<?> listener : ((AbstractApplicationContext) context) .getApplicationListeners()) { this.initialMulticaster.addApplicationListener(listener); } } this.initialMulticaster.setErrorHandler(new LoggingErrorHandler()); this.initialMulticaster.multicastEvent(event); } }
@Override public void doDispose() { // check we aren't trying to close a context which has never been started, // spring's appContext.isActive() isn't working for this case if (!springContextInitialised.get()) { return; } if (!isReadOnly() && ((ConfigurableApplicationContext) applicationContext).isActive()) { ((ConfigurableApplicationContext) applicationContext).close(); } // release the circular implicit ref to MuleContext applicationContext = null; this.springContextInitialised.set(false); }
private boolean isInactive() { return ( applicationContext instanceof ConfigurableApplicationContext && !((ConfigurableApplicationContext)applicationContext).isActive() ); } }
/** * Closing Citrus and its application context. */ public void close() { if (applicationContext instanceof ConfigurableApplicationContext) { if (((ConfigurableApplicationContext) applicationContext).isActive()) { ((ConfigurableApplicationContext) applicationContext).close(); } } } }
@Override public boolean isActive() { lock.readLock().lock(); try { return configurableApplicationContext.isActive(); } finally { lock.readLock().unlock(); } }
@Override public <T> T getComponent(String componentName, Class<T> requiredType) { if (this.context.isActive() && this.context.containsBean(componentName)) { return context.getBean(componentName, requiredType); } return null; }
/** * Refresh the given application context, if necessary. */ protected void refreshApplicationContext(ApplicationContext context) { if (context instanceof ConfigurableApplicationContext) { ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context; if (!cac.isActive()) { cac.refresh(); } } }
@Override public void stop() { if (context.isActive()) { context.stop(); // Shouldn't need to close() as well? } }
@Override public void init() { synchronized (this) { if (!_applicationContext.isActive()) { _applicationContext.refresh(); _stopWatch = new StopWatch(); } } }