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

com.tngtech.archunit.junit.ArchUnitRunner Maven / Gradle / Ivy

Go to download

A Java architecture test library, to specify and assert architecture rules in plain Java - Module 'archunit-junit'

There is a newer version: 0.8.3
Show newest version
/*
 * Copyright 2017 TNG Technology Consulting GmbH
 *
 * 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.tngtech.archunit.junit;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.tngtech.archunit.Internal;
import com.tngtech.archunit.PublicAPI;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.lang.ArchRule;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.ParentRunner;
import org.junit.runners.model.FrameworkField;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;

import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;

/**
 * Evaluates {@link ArchRule ArchRules} against the classes inside of the packages specified via
 * 

* {@link AnalyzeClasses @AnalyzeClasses} on the annotated test class. *

* NOTE: The runner demands {@link AnalyzeClasses @AnalyzeClasses} to be present on the respective test class. *

* Example *

 *    {@literal @}RunWith(ArchUnitRunner.class)
 *    {@literal @}AnalyzeClasses(packages = "com.example")
 *    public class SomeArchTest {
 *        {@literal @}ArchTest
 *        public final ArchRule<JavaClass> some_rule = rule(all(JavaClass.class))
 *                .should("satisfy something special")
 *                .assertedBy(mySpecificCondition);
 *    }
 * 
*/ @PublicAPI(usage = ACCESS) public class ArchUnitRunner extends ParentRunner { private SharedCache cache = new SharedCache(); // NOTE: We want to change this in tests -> no static reference @Internal public ArchUnitRunner(Class testClass) throws InitializationError { super(testClass); } @Override protected List getChildren() { List children = new ArrayList<>(); children.addAll(findArchRuleFields()); children.addAll(findArchRuleMethods()); return children; } private Collection findArchRuleFields() { List result = new ArrayList<>(); for (FrameworkField ruleField : getTestClass().getAnnotatedFields(ArchTest.class)) { result.addAll(findArchRulesIn(ruleField)); } return result; } private Set findArchRulesIn(FrameworkField ruleField) { if (ruleField.getType() == ArchRules.class) { return getArchRules(ruleField).asTestExecutions(); } return Collections.singleton(new ArchRuleExecution(getTestClass().getJavaClass(), ruleField.getField())); } private ArchRules getArchRules(FrameworkField ruleField) { ArchTestExecution.validate(ruleField.getField()); try { return (ArchRules) ruleField.get(null); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } private Collection findArchRuleMethods() { List result = new ArrayList<>(); for (FrameworkMethod testMethod : getTestClass().getAnnotatedMethods(ArchTest.class)) { result.add(new ArchTestMethodExecution(getTestClass().getJavaClass(), testMethod.getMethod())); } return result; } @Override protected Description describeChild(ArchTestExecution child) { return child.describeSelf(); } @Override protected void runChild(ArchTestExecution child, RunNotifier notifier) { if (child.ignore()) { notifier.fireTestIgnored(describeChild(child)); } else { notifier.fireTestStarted(describeChild(child)); JavaClasses classes = cache.get().getClassesToAnalyzeFor(getTestClass().getJavaClass()); child.evaluateOn(classes).notify(notifier); notifier.fireTestFinished(describeChild(child)); } } static class SharedCache { private static final ClassCache cache = new ClassCache(); ClassCache get() { return cache; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy