Skip to content

Commit

Permalink
Refactoring to be compliant to overall code style. Introduce static f…
Browse files Browse the repository at this point in the history
…actory methods instead of constructors, make fields immutable.

Issue: TNG#78
Signed-off-by: Moritz Bogs <moritz.bogs@tngtech.com>
  • Loading branch information
Moritz Bogs committed Dec 9, 2018
1 parent 9a9400e commit f1e9886
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;

import static com.tngtech.archunit.library.GeneralCodingRules.*;
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS;
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_THROW_GENERIC_EXCEPTIONS;

@Category(Example.class)
public class CompositeArchRulesTest {
private final JavaClasses classes = new ClassFileImporter().importPackages("com.tngtech.archunit.example");

@Test
public void no_classes_should_access_standard_streams_and_throw_generic_exceptions() {
new CompositeArchRule(NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS)
public void no_classes_should_access_standard_streams_or_throw_generic_exceptions() {
CompositeArchRule.of(NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS)
.and(NO_CLASSES_SHOULD_THROW_GENERIC_EXCEPTIONS).check(classes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,33 +149,11 @@ Stream<DynamicTest> CodingRulesTest() {
com.tngtech.archunit.exampletest.junit5.CodingRulesTest.class)

.ofRule("no classes should access standard streams")
.by(callFromMethod(ClassViolatingCodingRules.class, "printToStandardStream")
.getting().field(System.class, "out")
.inLine(12))
.by(callFromMethod(ClassViolatingCodingRules.class, "printToStandardStream")
.getting().field(System.class, "err")
.inLine(13))
.by(callFromMethod(ClassViolatingCodingRules.class, "printToStandardStream")
.toMethod(SomeCustomException.class, "printStackTrace")
.inLine(14))
.by(callFromMethod(ServiceViolatingLayerRules.class, "illegalAccessToController")
.getting().field(System.class, "out")
.inLine(16))
.expectedAccessToStandardStreams()
.times(2)

.ofRule("no classes should throw generic exceptions")
.by(callFromMethod(ClassViolatingCodingRules.class, "throwGenericExceptions")
.toConstructor(Throwable.class)
.inLine(22))
.by(callFromMethod(ClassViolatingCodingRules.class, "throwGenericExceptions")
.toConstructor(Exception.class, String.class)
.inLine(24))
.by(callFromMethod(ClassViolatingCodingRules.class, "throwGenericExceptions")
.toConstructor(RuntimeException.class, String.class, Throwable.class)
.inLine(26))
.by(callFromMethod(ClassViolatingCodingRules.class, "throwGenericExceptions")
.toConstructor(Exception.class, String.class)
.inLine(26))
.expectedThrowOfGenericExceptions()

.ofRule("no classes should use java.util.logging")
.by(callFromStaticInitializer(ClassViolatingCodingRules.class)
Expand All @@ -188,38 +166,14 @@ Stream<DynamicTest> CodingRulesTest() {
@TestFactory
Stream<DynamicTest> CompositeArchRulesTest() {
return ExpectedTestFailures
.forTests(CompositeArchRulesTest.class)

.ofRule("no classes should access standard streams and no classes should throw generic exceptions")
.by(callFromMethod(ClassViolatingCodingRules.class, "printToStandardStream")
.toMethod(SomeCustomException.class, "printStackTrace")
.inLine(14))
.by(callFromMethod(ClassViolatingCodingRules.class, "printToStandardStream")
.getting().field(System.class, "err")
.inLine(13))
.by(callFromMethod(ClassViolatingCodingRules.class, "printToStandardStream")
.getting().field(System.class, "out")
.inLine(12))
.by(callFromMethod(ServiceViolatingLayerRules.class, "illegalAccessToController")
.getting().field(System.class, "out")
.inLine(16))

.by(callFromMethod(ClassViolatingCodingRules.class, "throwGenericExceptions")
.toConstructor(Exception.class, String.class)
.inLine(26))
.by(callFromMethod(ClassViolatingCodingRules.class, "throwGenericExceptions")
.toConstructor(RuntimeException.class, String.class, Throwable.class)
.inLine(26))
.by(callFromMethod(ClassViolatingCodingRules.class, "throwGenericExceptions")
.toConstructor(Throwable.class)
.inLine(22))
.by(callFromMethod(ClassViolatingCodingRules.class, "throwGenericExceptions")
.toConstructor(Exception.class, String.class)
.inLine(24))

.toDynamicTests();
}
.forTests(CompositeArchRulesTest.class)

.ofRule("no classes should access standard streams and no classes should throw generic exceptions")
.expectedThrowOfGenericExceptions()
.expectedAccessToStandardStreams()

.toDynamicTests();
}
@TestFactory
Stream<DynamicTest> ControllerRulesTest() {
return ExpectedTestFailures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.tngtech.archunit.example.ClassViolatingCodingRules;
import com.tngtech.archunit.example.SomeCustomException;
import com.tngtech.archunit.example.service.ServiceViolatingLayerRules;
import com.tngtech.archunit.lang.ArchRule;
import org.junit.jupiter.api.DynamicTest;
import org.junit.platform.runner.JUnitPlatform;
Expand All @@ -22,6 +25,7 @@
import org.junit.runner.notification.RunNotifier;

import static com.google.common.base.Preconditions.checkArgument;
import static com.tngtech.archunit.testutils.ExpectedAccess.callFromMethod;
import static java.lang.System.lineSeparator;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -115,6 +119,39 @@ public ExpectedTestFailures times(int amount) {
return this;
}

public ExpectedTestFailures expectedAccessToStandardStreams() {
return this
.by(callFromMethod(ClassViolatingCodingRules.class, "printToStandardStream")
.getting().field(System.class, "out")
.inLine(12))
.by(callFromMethod(ClassViolatingCodingRules.class, "printToStandardStream")
.getting().field(System.class, "err")
.inLine(13))
.by(callFromMethod(ClassViolatingCodingRules.class, "printToStandardStream")
.toMethod(SomeCustomException.class, "printStackTrace")
.inLine(14))
.by(callFromMethod(ServiceViolatingLayerRules.class, "illegalAccessToController")
.getting().field(System.class, "out")
.inLine(16));
}

public ExpectedTestFailures expectedThrowOfGenericExceptions() {
return this
.by(callFromMethod(ClassViolatingCodingRules.class, "throwGenericExceptions")
.toConstructor(Throwable.class)
.inLine(22))
.by(callFromMethod(ClassViolatingCodingRules.class, "throwGenericExceptions")
.toConstructor(Exception.class, String.class)
.inLine(24))
.by(callFromMethod(ClassViolatingCodingRules.class, "throwGenericExceptions")
.toConstructor(RuntimeException.class, String.class, Throwable.class)
.inLine(26))
.by(callFromMethod(ClassViolatingCodingRules.class, "throwGenericExceptions")
.toConstructor(Exception.class, String.class)
.inLine(26));
}


private abstract static class RunnableTest {
final Class<?> testClass;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,60 @@
/*
* Copyright 2018 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.lang;

import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.tngtech.archunit.PublicAPI;
import com.tngtech.archunit.core.domain.JavaClasses;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CompositeArchRule implements ArchRule {
private List<ArchRule> rules;
private String overriddenDescription;
import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;

public CompositeArchRule(ArchRule rule) {
this.rules = Arrays.asList(rule);
}
public final class CompositeArchRule implements ArchRule {
private final List<ArchRule> rules;
private final String description;

private CompositeArchRule(CompositeArchRule compositeArchRule, ArchRule archRule) {
List<ArchRule> rules = new ArrayList<>(compositeArchRule.rules);
rules.add(archRule);
private CompositeArchRule(List<ArchRule> rules, String description) {
this.rules = rules;
this.description = description;
}

@PublicAPI(usage = ACCESS)
public static CompositeArchRule of(ArchRule rule) {
return new CompositeArchRule(Collections.singletonList(rule), rule.getDescription());
}

@PublicAPI(usage = ACCESS)
public CompositeArchRule and(ArchRule rule) {
return new CompositeArchRule(this, rule);
List<ArchRule> newRules = new ArrayList<>(rules);
newRules.add(rule);
String newDescription = description + " and " + rule.getDescription();
return new CompositeArchRule(newRules, newDescription);
}

@Override
public void check(JavaClasses classes) {
Assertions.check(this, classes);
}


@Override
public ArchRule because(String reason) {
Iterables.getLast(rules).because(reason);
rules.get(rules.size() - 1).because(reason);
return this;
public CompositeArchRule because(String reason) {
return new CompositeArchRule(rules, description + ", because " + reason);
}

@Override
Expand All @@ -48,21 +67,12 @@ public EvaluationResult evaluate(JavaClasses classes) {
}

@Override
public String getDescription() {
if (overriddenDescription != null) {
return overriddenDescription;
}

List<String> descriptions = new ArrayList<>();
for (ArchRule rule : rules) {
descriptions.add(rule.getDescription());
}
return Joiner.on( " and ").join(descriptions);
public ArchRule as(String newDescription) {
return new CompositeArchRule(rules, newDescription);
}

@Override
public ArchRule as(String newDescription) {
overriddenDescription = newDescription;
return this;
public String getDescription() {
return description;
}
}

0 comments on commit f1e9886

Please sign in to comment.