All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.arquillian.container.spring.embedded.SpringEmbeddedApplicationContextLifeCycleHandler Maven / Gradle / Ivy

The newest version!
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2013, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jboss.arquillian.container.spring.embedded;

import org.jboss.arquillian.core.api.Event;
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.InstanceProducer;
import org.jboss.arquillian.core.api.annotation.ApplicationScoped;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.api.annotation.Observes;
import org.jboss.arquillian.core.spi.ServiceLoader;
import org.jboss.arquillian.spring.integration.context.ApplicationContextDestroyer;
import org.jboss.arquillian.spring.integration.context.RemoteApplicationContextProducer;
import org.jboss.arquillian.spring.integration.context.RemoteTestScopeApplicationContext;
import org.jboss.arquillian.spring.integration.context.TestScopeApplicationContext;
import org.jboss.arquillian.spring.integration.event.ApplicationContextCreatedEvent;
import org.jboss.arquillian.spring.integration.event.ApplicationContextDestroyedEvent;
import org.jboss.arquillian.spring.integration.event.ApplicationContextEvent;
import org.jboss.arquillian.spring.integration.test.annotation.ContextLifeCycle;
import org.jboss.arquillian.spring.integration.test.annotation.ContextLifeCycleMode;
import org.jboss.arquillian.test.spi.TestClass;
import org.jboss.arquillian.test.spi.event.suite.After;
import org.jboss.arquillian.test.spi.event.suite.AfterSuite;
import org.jboss.arquillian.test.spi.event.suite.Before;
import org.jboss.arquillian.test.spi.event.suite.BeforeClass;

import java.util.Collection;

/**
 * 

A embedded container application context life cycle handler.

* *

This class is responsible for instantiating and destroying the application context when run in spring embedded * environment.

* * @author Jakub Narloch */ public class SpringEmbeddedApplicationContextLifeCycleHandler { /** *

Represents the default ApplicationContext life cycle mode.

*/ private static final ContextLifeCycleMode DEFAULT_LIFE_CYCLE_MODE = ContextLifeCycleMode.TEST_CASE; /** *

Represents the instance of {@link ServiceLoader}.

*/ @Inject private Instance serviceLoaderInstance; /** *

{@link RemoteTestScopeApplicationContext} producer.

*/ @Inject @ApplicationScoped private InstanceProducer applicationContextInstance; /** *

Represents the application context event.

*/ @Inject private Event applicationContextEvent; /** *

The before class event handler.

* *

Delegates to the registered {@link org.jboss.arquillian.spring.integration.context.ApplicationContextProducer} * instances in order to create the application context.

* * @param event the before class event */ public void beforeClass(@Observes BeforeClass event) { createTestApplicationContext(event.getTestClass()); } /** *

The before test event handler.

* *

Delegates to the registered {@link org.jboss.arquillian.spring.integration.context.ApplicationContextProducer} * instances in order to create the application context.

* * @param event the before test event */ public void beforeTest(@Observes Before event) { createTestApplicationContext(event.getTestClass()); } /** *

The after test event handler.

* *

Delegates to the registered {@link org.jboss.arquillian.spring.integration.context.ApplicationContextProducer} * instances in order to create the application context.

* * @param event the after test event */ public void afterTest(@Observes After event) { ContextLifeCycleMode mode = getContextLifeCycleMode(event.getTestClass()); if (mode == ContextLifeCycleMode.TEST) { destroyTestApplicationContext(); } } /** *

The before test event handler.

* *

Delegates to the registered {@link org.jboss.arquillian.spring.integration.context.ApplicationContextProducer} * instances in order to create the application context.

* * @param event the after suite event */ public void afterSuite(@Observes AfterSuite event) { destroyTestApplicationContext(); } /** *

Instantiates the application context if needed base on the meta data provided on the test class.

* *

The actual instances creation is being delegated to the registered

* * @param testClass the instance of the test class */ private void createTestApplicationContext(TestClass testClass) { if (getApplicationContext() != null && getApplicationContext().getTestClass().getJavaClass().equals(testClass.getJavaClass())) { return; } // creates the application context instance RemoteTestScopeApplicationContext applicationContext = createApplicationContext(testClass); if (applicationContext != null) { // triggers the application context created event applicationContextEvent.fire(new ApplicationContextCreatedEvent(applicationContext)); setApplicationContext(applicationContext); } } /** * Creates the application context. * * @param testClass the test class * * @return the application context */ private RemoteTestScopeApplicationContext createApplicationContext(TestClass testClass) { ServiceLoader serviceLoader = serviceLoaderInstance.get(); // retrieves the list of all registered application context producers Collection applicationContextProducers = serviceLoader.all(RemoteApplicationContextProducer.class); for (RemoteApplicationContextProducer applicationContextProducer : applicationContextProducers) { if (applicationContextProducer.supports(testClass)) { return applicationContextProducer.createApplicationContext(testClass); } } // the given test is not supported return null; } /** *

Destroys the application context if it exists.

*/ private void destroyTestApplicationContext() { RemoteTestScopeApplicationContext applicationContext = getApplicationContext(); if (applicationContext != null) { destroyApplicationContext(applicationContext); // triggers the application context destroyed event applicationContextEvent.fire(new ApplicationContextDestroyedEvent(applicationContext)); } } /** * Destroys the application context. * * @param applicationContext the application context to be destroyed */ private void destroyApplicationContext(TestScopeApplicationContext applicationContext) { // single service is expected getService(ApplicationContextDestroyer.class).destroyApplicationContext(applicationContext); } /** *

Retrieves the context life cycle mode.

* * @param testClass the test class * * @return the {@link ContextLifeCycleMode} defined for the test */ private ContextLifeCycleMode getContextLifeCycleMode(TestClass testClass) { ContextLifeCycle contextLifeCycle = testClass.getAnnotation(ContextLifeCycle.class); if (contextLifeCycle != null) { return contextLifeCycle.value(); } return DEFAULT_LIFE_CYCLE_MODE; } /** *

Retrieves the application context instance.

* * @return the application context */ private RemoteTestScopeApplicationContext getApplicationContext() { return applicationContextInstance.get(); } /** *

Sets the application context instance.

* * @param applicationContext the application context instance */ private void setApplicationContext(RemoteTestScopeApplicationContext applicationContext) { this.applicationContextInstance.set(applicationContext); } /** * Retrieves the service by it's type.

The implementation uses {@link ServiceLoader} in order to retrieve first * instance of specified kind. Any other registered instance in given scope is being discarded. * * @param clazz the class of the service * @param the service type * * @return the service instance */ private T getService(Class clazz) { Collection collection = serviceLoaderInstance.get().all(clazz); return collection.iterator().next(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy