From 1a003d85f02f614222f3119f4e64fe9df954656d Mon Sep 17 00:00:00 2001 From: Simon Brown Date: Tue, 20 Aug 2024 09:59:59 +0100 Subject: [PATCH] More tests. --- .../java/com/structurizr/component/Type.java | 7 ++-- ... => ExcludeAbstractClassesTypeFilter.java} | 4 +-- .../filter/ExcludeTypesByRegexFilter.java | 5 +++ .../filter/IncludeTypesByRegexFilter.java | 5 +++ .../component/matcher/ExtendsTypeMatcher.java | 3 +- .../matcher/ImplementsTypeMatcher.java | 3 +- .../matcher/NameSuffixTypeMatcher.java | 3 +- .../filter/DefaultTypeFilterTests.java | 15 +++++++++ .../ExcludeAbstractClassesFilterTests.java | 31 ++++++++++++++++++ .../ExcludeTypesByRegexFilterTests.java | 32 +++++++++++++++++++ .../IncludeTypesByRegexFilterTests.java | 32 +++++++++++++++++++ 11 files changed, 131 insertions(+), 9 deletions(-) rename structurizr-component/src/main/java/com/structurizr/component/filter/{ExcludeAbstractClassTypeFilter.java => ExcludeAbstractClassesTypeFilter.java} (72%) create mode 100644 structurizr-component/src/test/java/com/structurizr/component/filter/DefaultTypeFilterTests.java create mode 100644 structurizr-component/src/test/java/com/structurizr/component/filter/ExcludeAbstractClassesFilterTests.java create mode 100644 structurizr-component/src/test/java/com/structurizr/component/filter/ExcludeTypesByRegexFilterTests.java create mode 100644 structurizr-component/src/test/java/com/structurizr/component/filter/IncludeTypesByRegexFilterTests.java diff --git a/structurizr-component/src/main/java/com/structurizr/component/Type.java b/structurizr-component/src/main/java/com/structurizr/component/Type.java index 0f0eed87..3c9fafe0 100644 --- a/structurizr-component/src/main/java/com/structurizr/component/Type.java +++ b/structurizr-component/src/main/java/com/structurizr/component/Type.java @@ -3,7 +3,6 @@ import com.structurizr.util.StringUtils; import org.apache.bcel.classfile.JavaClass; -import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Objects; import java.util.Set; @@ -11,7 +10,7 @@ /** * Represents a Java type (e.g. class or interface) - it's a wrapper around a BCEL JavaClass. */ -public final class Type { +public class Type { private final JavaClass javaClass; private final String fullyQualifiedName; @@ -77,8 +76,8 @@ public Set getDependencies() { return new LinkedHashSet<>(dependencies); } - public boolean isAbstract() { - return javaClass.isAbstract(); + public boolean isAbstractClass() { + return javaClass.isAbstract() && javaClass.isClass(); } @Override diff --git a/structurizr-component/src/main/java/com/structurizr/component/filter/ExcludeAbstractClassTypeFilter.java b/structurizr-component/src/main/java/com/structurizr/component/filter/ExcludeAbstractClassesTypeFilter.java similarity index 72% rename from structurizr-component/src/main/java/com/structurizr/component/filter/ExcludeAbstractClassTypeFilter.java rename to structurizr-component/src/main/java/com/structurizr/component/filter/ExcludeAbstractClassesTypeFilter.java index dad267ef..2a2d979c 100644 --- a/structurizr-component/src/main/java/com/structurizr/component/filter/ExcludeAbstractClassTypeFilter.java +++ b/structurizr-component/src/main/java/com/structurizr/component/filter/ExcludeAbstractClassesTypeFilter.java @@ -5,10 +5,10 @@ /** * A type filter that excludes abstract types. */ -public class ExcludeAbstractClassTypeFilter implements TypeFilter { +public class ExcludeAbstractClassesTypeFilter implements TypeFilter { public boolean accept(Type type) { - return !type.isAbstract(); + return !type.isAbstractClass(); } @Override diff --git a/structurizr-component/src/main/java/com/structurizr/component/filter/ExcludeTypesByRegexFilter.java b/structurizr-component/src/main/java/com/structurizr/component/filter/ExcludeTypesByRegexFilter.java index 0f697b6e..52e7d284 100644 --- a/structurizr-component/src/main/java/com/structurizr/component/filter/ExcludeTypesByRegexFilter.java +++ b/structurizr-component/src/main/java/com/structurizr/component/filter/ExcludeTypesByRegexFilter.java @@ -1,6 +1,7 @@ package com.structurizr.component.filter; import com.structurizr.component.Type; +import com.structurizr.util.StringUtils; /** * A type filter that excludes by matching a regex against the fully qualified type name. @@ -10,6 +11,10 @@ public class ExcludeTypesByRegexFilter implements TypeFilter { private final String regex; public ExcludeTypesByRegexFilter(String regex) { + if (StringUtils.isNullOrEmpty(regex)) { + throw new IllegalArgumentException("A regex must be supplied"); + } + this.regex = regex; } diff --git a/structurizr-component/src/main/java/com/structurizr/component/filter/IncludeTypesByRegexFilter.java b/structurizr-component/src/main/java/com/structurizr/component/filter/IncludeTypesByRegexFilter.java index dc76b1f4..3de92d28 100644 --- a/structurizr-component/src/main/java/com/structurizr/component/filter/IncludeTypesByRegexFilter.java +++ b/structurizr-component/src/main/java/com/structurizr/component/filter/IncludeTypesByRegexFilter.java @@ -1,6 +1,7 @@ package com.structurizr.component.filter; import com.structurizr.component.Type; +import com.structurizr.util.StringUtils; /** * A type filter that includes by matching a regex against the fully qualified type name. @@ -10,6 +11,10 @@ public class IncludeTypesByRegexFilter implements TypeFilter { private final String regex; public IncludeTypesByRegexFilter(String regex) { + if (StringUtils.isNullOrEmpty(regex)) { + throw new IllegalArgumentException("A regex must be supplied"); + } + this.regex = regex; } diff --git a/structurizr-component/src/main/java/com/structurizr/component/matcher/ExtendsTypeMatcher.java b/structurizr-component/src/main/java/com/structurizr/component/matcher/ExtendsTypeMatcher.java index 103d18b5..043ca3ce 100644 --- a/structurizr-component/src/main/java/com/structurizr/component/matcher/ExtendsTypeMatcher.java +++ b/structurizr-component/src/main/java/com/structurizr/component/matcher/ExtendsTypeMatcher.java @@ -1,6 +1,7 @@ package com.structurizr.component.matcher; import com.structurizr.component.Type; +import com.structurizr.util.StringUtils; import org.apache.bcel.classfile.JavaClass; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -21,7 +22,7 @@ public class ExtendsTypeMatcher extends AbstractTypeMatcher { public ExtendsTypeMatcher(String className, String technology) { super(technology); - if (className == null || className.trim().length() == 0) { + if (StringUtils.isNullOrEmpty(className)) { throw new IllegalArgumentException("A fully qualified class name must be supplied"); } diff --git a/structurizr-component/src/main/java/com/structurizr/component/matcher/ImplementsTypeMatcher.java b/structurizr-component/src/main/java/com/structurizr/component/matcher/ImplementsTypeMatcher.java index 54aecdcb..1deb3b6f 100644 --- a/structurizr-component/src/main/java/com/structurizr/component/matcher/ImplementsTypeMatcher.java +++ b/structurizr-component/src/main/java/com/structurizr/component/matcher/ImplementsTypeMatcher.java @@ -1,6 +1,7 @@ package com.structurizr.component.matcher; import com.structurizr.component.Type; +import com.structurizr.util.StringUtils; import org.apache.bcel.classfile.JavaClass; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -19,7 +20,7 @@ public class ImplementsTypeMatcher extends AbstractTypeMatcher { public ImplementsTypeMatcher(String interfaceName, String technology) { super(technology); - if (interfaceName == null || interfaceName.trim().length() == 0) { + if (StringUtils.isNullOrEmpty(interfaceName)) { throw new IllegalArgumentException("A fully qualified interface name must be supplied"); } diff --git a/structurizr-component/src/main/java/com/structurizr/component/matcher/NameSuffixTypeMatcher.java b/structurizr-component/src/main/java/com/structurizr/component/matcher/NameSuffixTypeMatcher.java index 01f27091..078a1a51 100644 --- a/structurizr-component/src/main/java/com/structurizr/component/matcher/NameSuffixTypeMatcher.java +++ b/structurizr-component/src/main/java/com/structurizr/component/matcher/NameSuffixTypeMatcher.java @@ -1,6 +1,7 @@ package com.structurizr.component.matcher; import com.structurizr.component.Type; +import com.structurizr.util.StringUtils; /** * Matches types where the name of the type ends with the specified suffix. @@ -12,7 +13,7 @@ public class NameSuffixTypeMatcher extends AbstractTypeMatcher { public NameSuffixTypeMatcher(String suffix, String technology) { super(technology); - if (suffix == null || suffix.trim().length() == 0) { + if (StringUtils.isNullOrEmpty(suffix)) { throw new IllegalArgumentException("A suffix must be supplied"); } diff --git a/structurizr-component/src/test/java/com/structurizr/component/filter/DefaultTypeFilterTests.java b/structurizr-component/src/test/java/com/structurizr/component/filter/DefaultTypeFilterTests.java new file mode 100644 index 00000000..6352fb21 --- /dev/null +++ b/structurizr-component/src/test/java/com/structurizr/component/filter/DefaultTypeFilterTests.java @@ -0,0 +1,15 @@ +package com.structurizr.component.filter; + +import com.structurizr.component.Type; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class DefaultTypeFilterTests { + + @Test + void filter_ReturnsTrue() { + assertTrue(new DefaultTypeFilter().accept(new Type("com.example.Class"))); + } + +} \ No newline at end of file diff --git a/structurizr-component/src/test/java/com/structurizr/component/filter/ExcludeAbstractClassesFilterTests.java b/structurizr-component/src/test/java/com/structurizr/component/filter/ExcludeAbstractClassesFilterTests.java new file mode 100644 index 00000000..1b3b8c22 --- /dev/null +++ b/structurizr-component/src/test/java/com/structurizr/component/filter/ExcludeAbstractClassesFilterTests.java @@ -0,0 +1,31 @@ +package com.structurizr.component.filter; + +import com.structurizr.component.Type; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ExcludeAbstractClassesFilterTests { + + @Test + void filter_ReturnsTrue_WhenTheTypeIsNotAbstract() { + assertTrue(new ExcludeAbstractClassesTypeFilter().accept(new Type("com.example.Class") { + @Override + public boolean isAbstractClass() { + return false; + } + })); + } + + @Test + void filter_ReturnsFalse_WhenTheTypeIsAbstract() { + assertFalse(new ExcludeAbstractClassesTypeFilter().accept(new Type("com.example.Class") { + @Override + public boolean isAbstractClass() { + return true; + } + })); + } + +} \ No newline at end of file diff --git a/structurizr-component/src/test/java/com/structurizr/component/filter/ExcludeTypesByRegexFilterTests.java b/structurizr-component/src/test/java/com/structurizr/component/filter/ExcludeTypesByRegexFilterTests.java new file mode 100644 index 00000000..4a0b459f --- /dev/null +++ b/structurizr-component/src/test/java/com/structurizr/component/filter/ExcludeTypesByRegexFilterTests.java @@ -0,0 +1,32 @@ +package com.structurizr.component.filter; + +import com.structurizr.component.Type; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class ExcludeTypesByRegexFilterTests { + + @Test + void construction_ThrowsAnException_WhenPassedANullSuffix() { + assertThrowsExactly(IllegalArgumentException.class, () -> new ExcludeTypesByRegexFilter(null)); + } + + @Test + void construction_ThrowsAnException_WhenPassedAnEmptySuffix() { + assertThrowsExactly(IllegalArgumentException.class, () -> new ExcludeTypesByRegexFilter("")); + assertThrowsExactly(IllegalArgumentException.class, () -> new ExcludeTypesByRegexFilter(" ")); + } + + + @Test + void filter_ReturnsTrue_WhenTheTypeDoesNotMatchRegex() { + assertTrue(new ExcludeTypesByRegexFilter(".*Utils").accept(new Type("com.example.CustomerComponent"))); + } + + @Test + void filter_ReturnsFalse_WhenTheTypeMatchesRegex() { + assertFalse(new ExcludeTypesByRegexFilter(".*Utils").accept(new Type("com.example.DateUtils"))); + } + +} \ No newline at end of file diff --git a/structurizr-component/src/test/java/com/structurizr/component/filter/IncludeTypesByRegexFilterTests.java b/structurizr-component/src/test/java/com/structurizr/component/filter/IncludeTypesByRegexFilterTests.java new file mode 100644 index 00000000..25b223c5 --- /dev/null +++ b/structurizr-component/src/test/java/com/structurizr/component/filter/IncludeTypesByRegexFilterTests.java @@ -0,0 +1,32 @@ +package com.structurizr.component.filter; + +import com.structurizr.component.Type; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class IncludeTypesByRegexFilterTests { + + @Test + void construction_ThrowsAnException_WhenPassedANullSuffix() { + assertThrowsExactly(IllegalArgumentException.class, () -> new IncludeTypesByRegexFilter(null)); + } + + @Test + void construction_ThrowsAnException_WhenPassedAnEmptySuffix() { + assertThrowsExactly(IllegalArgumentException.class, () -> new IncludeTypesByRegexFilter("")); + assertThrowsExactly(IllegalArgumentException.class, () -> new IncludeTypesByRegexFilter(" ")); + } + + + @Test + void filter_ReturnsFalse_WhenTheTypeDoesNotMatchRegex() { + assertFalse(new IncludeTypesByRegexFilter(".*Component").accept(new Type("com.example.DateUtils"))); + } + + @Test + void filter_ReturnsTrue_WhenTheTypeMatchesRegex() { + assertTrue(new IncludeTypesByRegexFilter(".*Component").accept(new Type("com.example.CustomerComponent"))); + } + +} \ No newline at end of file