|
| 1 | +package com.tngtech.archunit.exampletest; |
| 2 | + |
| 3 | +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Objects; |
| 7 | +import java.util.Optional; |
| 8 | + |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.experimental.categories.Category; |
| 11 | + |
| 12 | +import com.tngtech.archunit.core.domain.JavaClasses; |
| 13 | +import com.tngtech.archunit.core.domain.JavaMethod; |
| 14 | +import com.tngtech.archunit.core.domain.JavaModifier; |
| 15 | +import com.tngtech.archunit.core.importer.ClassFileImporter; |
| 16 | +import com.tngtech.archunit.example.singleton.SingletonClass; |
| 17 | +import com.tngtech.archunit.lang.ArchCondition; |
| 18 | +import com.tngtech.archunit.lang.ConditionEvents; |
| 19 | +import com.tngtech.archunit.lang.SimpleConditionEvent; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Per Lundberg |
| 23 | + */ |
| 24 | +@Category(Example.class) |
| 25 | +public class SingletonTest { |
| 26 | + private static final JavaClasses classes = new ClassFileImporter().importPackagesOf( SingletonClass.class); |
| 27 | + |
| 28 | + @Test |
| 29 | + public void getInstance_is_not_used_from_inside_methods() { |
| 30 | + methods() |
| 31 | + .that().haveName( "getInstance" ) |
| 32 | + .and().areStatic() |
| 33 | + |
| 34 | + // Note: this is a convoluted way to say "no parameters". |
| 35 | + .and().haveRawParameterTypes( new String[0] ) |
| 36 | + |
| 37 | + .should( onlyBeCalledFromWhitelistedOrigins( |
| 38 | + // The class below will trigger a violation unless present as a parameter here. |
| 39 | + "com.tngtech.archunit.example.singleton.SingletonClassWhitelistedInvalidConsumer" |
| 40 | + ) ) |
| 41 | + .because( "" + |
| 42 | + "getInstance() calls should not be spread out through the methods of a class. This makes it hard/impossible " + |
| 43 | + "to override the dependencies for tests, and also means the dependencies are much harder to identify when " + |
| 44 | + "quickly looking at the code. Instead, move all getInstance() calls to the INSTANCE supplier and pass the " + |
| 45 | + "dependency to the constructor that way. If this is impossible for a particular case, add the class name to " + |
| 46 | + "the whitelist in " + getClass().getName() ) |
| 47 | + .check( classes ); |
| 48 | + } |
| 49 | + |
| 50 | + private ArchCondition<JavaMethod> onlyBeCalledFromWhitelistedOrigins( String... whitelistedOrigins ) { |
| 51 | + return new ArchCondition<JavaMethod>( "only be called by whitelisted methods" ) { |
| 52 | + @Override |
| 53 | + public void check( JavaMethod method, ConditionEvents events ) { |
| 54 | + method.getCallsOfSelf().stream() |
| 55 | + // TODO: Add your own exceptions as needed here, if you have particular |
| 56 | + // TODO: use cases where getInstance() calls are permissible. |
| 57 | + // Static getInstance() methods are always allowed to call getInstance. This |
| 58 | + // does not break dependency injection and does not come with significant |
| 59 | + // design flaws. |
| 60 | + .filter( call -> !( Objects.equals( call.getOrigin().getName(), "getInstance" ) && call.getOrigin().getModifiers().contains( JavaModifier.STATIC ) ) ) |
| 61 | + |
| 62 | + // Anything not handled by the exceptions above must be explicitly listed in |
| 63 | + // the whitelistedOrigins parameter. |
| 64 | + .filter( call -> { |
| 65 | + Optional<String> result = Arrays.stream( whitelistedOrigins ) |
| 66 | + .filter( o -> call.getOrigin().getFullName().startsWith( o ) ) |
| 67 | + .findFirst(); |
| 68 | + |
| 69 | + return !result.isPresent(); |
| 70 | + } ) |
| 71 | + .forEach( call -> events.add( SimpleConditionEvent.violated( method, call.getDescription() ) ) ); |
| 72 | + } |
| 73 | + }; |
| 74 | + } |
| 75 | +} |
0 commit comments