Skip to content

Commit

Permalink
More tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Aug 20, 2024
1 parent e8ba1f3 commit 1a003d8
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
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;

/**
* 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;
Expand Down Expand Up @@ -77,8 +76,8 @@ public Set<Type> getDependencies() {
return new LinkedHashSet<>(dependencies);
}

public boolean isAbstract() {
return javaClass.isAbstract();
public boolean isAbstractClass() {
return javaClass.isAbstract() && javaClass.isClass();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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")));
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}));
}

}
Original file line number Diff line number Diff line change
@@ -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")));
}

}
Original file line number Diff line number Diff line change
@@ -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")));
}

}

0 comments on commit 1a003d8

Please sign in to comment.