Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.deltaspike.testcontrol.api.junit;
import junit.framework.Assert;
import org.apache.deltaspike.cdise.api.CdiContainer;
import org.apache.deltaspike.cdise.api.CdiContainerLoader;
import org.apache.deltaspike.cdise.api.ContextControl;
import org.apache.deltaspike.core.api.projectstage.ProjectStage;
import org.apache.deltaspike.core.api.provider.BeanManagerProvider;
import org.apache.deltaspike.core.api.provider.BeanProvider;
import org.apache.deltaspike.core.util.ExceptionUtils;
import org.apache.deltaspike.core.util.ProjectStageProducer;
import org.apache.deltaspike.core.util.ServiceUtils;
import org.apache.deltaspike.testcontrol.api.TestControl;
import org.apache.deltaspike.testcontrol.api.literal.TestControlLiteral;
import org.apache.deltaspike.testcontrol.spi.ExternalContainer;
import org.apache.deltaspike.testcontrol.spi.TestAware;
import org.apache.deltaspike.testcontrol.spi.TestControlValidator;
import org.apache.deltaspike.testcontrol.spi.junit.TestStatementDecoratorFactory;
import org.junit.Test;
import org.junit.internal.runners.statements.FailOnTimeout;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestClass;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Singleton;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A JUnit test runner to start up with a CDI or embedded JavaEE container.
*
*
If the underlying container supports it you can even pass in a property file
* to the bootstrap. You can configure the name of the configuration file via a
* {@link org.apache.deltaspike.core.api.config.ConfigResolver} property named
* cdiTestRunnerConfig. By default the name of the config file is
* cdiTestRunnerConfig.properties.
*/
public class CdiTestRunner extends BlockJUnit4ClassRunner
{
private static final Logger LOGGER = Logger.getLogger(CdiTestRunner.class.getName());
private static final boolean USE_TEST_CLASS_AS_CDI_BEAN;
private static final boolean ALLOW_INJECTION_POINT_MANIPULATION;
private static Set notifierIdentities = new CopyOnWriteArraySet();
static
{
USE_TEST_CLASS_AS_CDI_BEAN = TestBaseConfig.ContainerIntegration.USE_TEST_CLASS_AS_CDI_BEAN;
ALLOW_INJECTION_POINT_MANIPULATION = TestBaseConfig.MockIntegration.ALLOW_MANUAL_INJECTION_POINT_MANIPULATION;
}
private static ThreadLocal automaticScopeHandlingActive = new ThreadLocal();
private static ThreadLocal currentTestRunner = new ThreadLocal();
private List statementDecoratorFactories;
private ContainerAwareTestContext testContext;
public CdiTestRunner(Class> testClass) throws InitializationError
{
super(testClass);
TestControl testControl = testClass.getAnnotation(TestControl.class);
this.testContext = new ContainerAwareTestContext(testControl, null);
//benefits from the fallback-handling in ContainerAwareTestContext
Class extends Handler> logHandlerClass = this.testContext.getLogHandlerClass();
if (!Handler.class.equals(logHandlerClass))
{
try
{
LOGGER.addHandler(logHandlerClass.newInstance());
}
catch (Exception e)
{
throw ExceptionUtils.throwAsRuntimeException(e);
}
}
this.statementDecoratorFactories = ServiceUtils.loadServiceImplementations(TestStatementDecoratorFactory.class);
Collections.sort(this.statementDecoratorFactories, new Comparator()
{
@Override
public int compare(TestStatementDecoratorFactory f1, TestStatementDecoratorFactory f2)
{
return f1.getOrdinal() > f2.getOrdinal() ? 1 : -1;
}
});
}
@Override
public void run(RunNotifier runNotifier)
{
if (!CdiTestSuiteRunner.isContainerStarted()) //not called as a part of a test-suite
{
int identityHashCode = System.identityHashCode(runNotifier);
if (!notifierIdentities.contains(identityHashCode))
{
addLogRunListener(runNotifier, identityHashCode);
}
}
super.run(runNotifier);
}
private static synchronized void addLogRunListener(RunNotifier notifier, int identityHashCode)
{
if (notifierIdentities.contains(identityHashCode))
{
return;
}
notifierIdentities.add(identityHashCode);
notifier.addListener(new CdiTestSuiteRunner.LogRunListener());
}
@Override
protected Statement methodInvoker(FrameworkMethod method, Object test)
{
return new ContainerAwareMethodInvoker(method, test);
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier)
{
currentTestRunner.set(this);
TestControl testControl = method.getAnnotation(TestControl.class);
ContainerAwareTestContext currentTestContext =
new ContainerAwareTestContext(testControl, this.testContext);
currentTestContext.applyBeforeMethodConfig(method.getMethod());
try
{
super.runChild(method, notifier);
}
finally
{
currentTestContext.applyAfterMethodConfig();
}
}
@Override
protected Object createTest() throws Exception
{
BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
Class> type = getTestClass().getJavaClass();
Set> beans = beanManager.getBeans(type);
Object result;
if (!USE_TEST_CLASS_AS_CDI_BEAN || beans == null || beans.isEmpty())
{
result = super.createTest();
BeanProvider.injectFields(result); //fallback to simple injection
}
else
{
Bean