/* * Copyright 2008-2011 the original author or authors. * * 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.kaleidofoundry.core.launcher; import org.junit.runner.notification.RunNotifier; import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.model.InitializationError; import org.kaleidofoundry.core.config.NamedConfiguration; import org.kaleidofoundry.core.config.NamedConfigurationInitializer; import org.kaleidofoundry.core.config.NamedConfigurations; import org.kaleidofoundry.core.env.EnvironmentInitializer; import org.kaleidofoundry.core.persistence.UnmanagedEntityManagerFactory; /** * Kaleido junit runner * <p> * Supported Annotations : * <ul> * <li>{@link NamedConfiguration} to load a configuration resource</li> * <li>{@link NamedConfigurations} to load many configuration resources</li> * </ul> * </p> * <p> * Example : * * <pre> * * // allows to use @Inject in your test classes * @RunWith(KaleidoJunit4ClassRunner.class) * // registers if you need, a configuration to load (@NamedConfigurations also exists) * @NamedConfiguration(name = "configuration", uri = "classpath:/config/application.properties") * class MyTest { * * // you inject your JPA EntityManager * @PersistenceUnit * private EntityManagerFactory myEmf; * * @PersistenceContext * private EntityManager myEm; * * // inject kaleido components * @Context * private yourAppConfig; * * @Context * private FileStore writer; * * ... * } * * </pre> * * </p> * * @author jraduget */ public class KaleidoJunit4ClassRunner extends BlockJUnit4ClassRunner { public KaleidoJunit4ClassRunner(final Class<?> klass) throws InitializationError { super(klass); } /* * (non-Javadoc) * @see org.junit.runners.ParentRunner#run(org.junit.runner.notification.RunNotifier) */ @Override public void run(final RunNotifier notifier) { // create and init the configurations initializer NamedConfigurationInitializer configurationInitializer = new NamedConfigurationInitializer(getTestClass().getJavaClass()); configurationInitializer.init(); // load env variables and start kaleido components EnvironmentInitializer environmentInitializer = new EnvironmentInitializer(getTestClass().getJavaClass()); environmentInitializer.init(); environmentInitializer.start(); // run tests super.run(notifier); // cleanup at end // unload current configurations if (configurationInitializer != null) { try { configurationInitializer.shutdown(); } catch (Throwable th) { throw new IllegalStateException("error during the configurations unregister processing", th); } } // unload other resources like configurations / caches ... if (environmentInitializer != null) { environmentInitializer.stop(); } // release entity manager factory is used UnmanagedEntityManagerFactory.closeAll(); } }