Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/uml/class-diagram-technical.puml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ note "Klasse" as Class
note "Interface" as Interface
note "Vererbung" as Vererbung
note "Implementierung" as Implementierung
note "gerichtete Assoziation" as Assoziation
note "gerichtete Assoziation\n als Objektvariable" as Assoziation
note "Vererbung und dabei\ngenerischen Typ binden" as Generics
note "gerichtete Abhängigkeit:\n<<use>> benutzt\n<<create>> erzeugt\n<<call>> Aufruf" as Dependency
note "Aggregation" as Aggregation
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/edu/hm/hafner/archunit/ArchitectureRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
* @author Ullrich Hafner
*/
public final class ArchitectureRules {
/** No class should have non-private instance fields. */
public static final ArchRule ONLY_PRIVATE_FIELDS =
fields().that().doNotHaveModifier(JavaModifier.STATIC)
.should().bePrivate().allowEmptyShould(true);

/** Tests should not use fields. Recommendation is to use factory methods for stubs and mocks. */
public static final ArchRule NO_FIELDS_IN_TESTS =
fields().that().areDeclaredInClassesThat().haveSimpleNameEndingWith("Test")
Expand Down
19 changes: 18 additions & 1 deletion src/test/java/edu/hm/hafner/archunit/ArchitectureRulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
class ArchitectureRulesTest {
private static final String BROKEN_CLASS_NAME = ArchitectureRulesViolatedTest.class.getTypeName();

@Test
void shouldVerifyThatFieldsArePrivate() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(
() -> ArchitectureRules.ONLY_PRIVATE_FIELDS.check(importBrokenClass()))
.withMessageContainingAll(BROKEN_CLASS_NAME, "fields that do not have modifier STATIC should be private' was violated");

assertThatNoException().isThrownBy(
() -> ArchitectureRules.ONLY_PRIVATE_FIELDS.check(importPassingClass()));
}

@Test
void shouldUseProtectedForReadResolve() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(
Expand Down Expand Up @@ -100,7 +110,7 @@ void shouldVerifyNoPublicTestMethodsRule() {

private JavaClasses importPassingClass() {
return new ClassFileImporter().importClasses(ArchitectureRulesPassedTest.class,
ArchitectureRulesAlsoPassedTest.class);
ArchitectureRulesAlsoPassedTest.class, ArchitectureRulesPassed.class);
}

private JavaClasses importBrokenClass() {
Expand All @@ -117,6 +127,8 @@ public static class ArchitectureRulesViolatedTest {
@edu.umd.cs.findbugs.annotations.Nullable
private final String noNullable = null;

int nonPrivate;

@Test @Disabled("This test is just there to be used in architecture tests")
public void shouldFail() {
org.junit.jupiter.api.Assertions.assertEquals(1, 1);
Expand Down Expand Up @@ -188,4 +200,9 @@ protected Object readResolve() {
return this;
}
}

@SuppressWarnings("all") // This class is just there to be used in architecture tests
static class ArchitectureRulesPassed {
private int privateField;
}
}
Loading