
com.crosstreelabs.junited.core.FeatureRunner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junited.core Show documentation
Show all versions of junited.core Show documentation
Core runners and features.
The newest version!
/*
* 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 com.crosstreelabs.junited.core;
import com.crosstreelabs.junited.core.annotation.Features;
import com.crosstreelabs.junited.core.execution.Unit;
import com.crosstreelabs.junited.core.features.TestCaseFeature;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.internal.AssumptionViolatedException;
import org.junit.internal.runners.model.EachTestNotifier;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runner.notification.StoppedByUserException;
import org.junit.runners.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FeatureRunner extends Runner {
private static final Logger LOGGER = LoggerFactory.getLogger(FeatureRunner.class);
protected final Class> klass;
protected final List features = new ArrayList<>();
protected final Set children = new HashSet<>();
public FeatureRunner(final Class> klass) {
this.klass = klass;
if (!klass.isAnnotationPresent(Features.class)) {
features.add(new TestCaseFeature(klass));
return;
}
Features features = klass.getAnnotation(Features.class);
for (Class extends Feature> feature: features.value()) {
this.features.add(instantiateFeature(feature));
}
if (this.features.isEmpty()) {
this.features.add(new TestCaseFeature(klass));
}
}
@Override
public Description getDescription() {
Description description = Description.createSuiteDescription(klass.getName(),
klass.getAnnotations());
for (Object child : getChildren()) {
description.addChild(describeChild(child));
}
return description;
}
public Description describeChild(final Object child) {
return Description.createTestDescription(klass,
child.toString(), new Annotation[0]);
}
public Set getChildren() {
if (!children.isEmpty()) {
return children;
}
Set children = new HashSet<>();
for (Feature f : features) {
children = f.getChildren(children);
}
this.children.addAll(children);
return this.children;
}
@Override
public void run(final RunNotifier notifier) {
LOGGER.debug("run");
EachTestNotifier testNotifier = new EachTestNotifier(notifier,
getDescription());
try {
Statement statement = classBlock(notifier);
statement.evaluate();
} catch (AssumptionViolatedException e) {
testNotifier.fireTestIgnored();
} catch (StoppedByUserException e) {
throw e;
} catch (Throwable e) {
testNotifier.addFailure(e);
}
}
protected Statement classBlock(final RunNotifier notifier) {
LOGGER.debug("classBlock");
Statement statement = childrenInvoker(notifier);
// statement = withBeforeClasses(statement);
// statement = withAfterClasses(statement);
// statement = withClassRules(statement);
return statement;
}
protected Statement childrenInvoker(final RunNotifier notifier) {
LOGGER.debug("childrenInvoker");
return new Statement() {
@Override
public void evaluate() {
runChildren(notifier);
}
};
}
private void runChildren(final RunNotifier notifier) {
LOGGER.debug("runChildren");
for (Unit child : getChildren()) {
FeatureRunner.this.runChild(child, notifier);
}
}
protected void runChild(Unit child, RunNotifier notifier) {
child.run(notifier);
}
protected Feature instantiateFeature(final Class extends Feature> cls) {
try {
try {
Constructor extends Feature> constructor = cls.getConstructor(Class.class);
return constructor.newInstance(klass);
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
LOGGER.warn("{} while try to find constructor(class) for {}", ex.getClass().getSimpleName(), cls.getCanonicalName());
}
return cls.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy