Skip to content

Commit

Permalink
Tidy and more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Aug 19, 2024
1 parent f378637 commit 4bab88f
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public AnnotationTypeMatcher(Class<? extends Annotation> annotation, String tech

@Override
public boolean matches(Type type) {
if (type == null) {
throw new IllegalArgumentException("A type must be specified");
}

if (type.getJavaClass() == null) {
throw new IllegalArgumentException("This type matcher requires a BCEL JavaClass");
}

AnnotationEntry[] annotationEntries = type.getJavaClass().getAnnotationEntries();
for (AnnotationEntry annotationEntry : annotationEntries) {
if (annotationType.equals(annotationEntry.getAnnotationType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public ExtendsTypeMatcher(String className, String technology) {

@Override
public boolean matches(Type type) {
if (type == null) {
throw new IllegalArgumentException("A type must be specified");
}

if (type.getJavaClass() == null) {
throw new IllegalArgumentException("This type matcher requires a BCEL JavaClass");
}

JavaClass javaClass = type.getJavaClass();
try {
Set<String> superClasses = Stream.of(javaClass.getSuperClasses()).map(JavaClass::getClassName).collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public ImplementsTypeMatcher(String interfaceName, String technology) {

@Override
public boolean matches(Type type) {
if (type == null) {
throw new IllegalArgumentException("A type must be specified");
}

if (type.getJavaClass() == null) {
throw new IllegalArgumentException("This type matcher requires a BCEL JavaClass");
}

JavaClass javaClass = type.getJavaClass();
Set<String> interfaceNames = Set.of(javaClass.getInterfaceNames());
return interfaceNames.contains(interfaceName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public NameSuffixTypeMatcher(String suffix, String technology) {

@Override
public boolean matches(Type type) {
if (type == null) {
throw new IllegalArgumentException("A type must be specified");
}

return type.getFullyQualifiedName().endsWith(suffix);
}

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 java.util.regex.Pattern;

Expand All @@ -14,30 +15,20 @@ public class RegexTypeMatcher extends AbstractTypeMatcher {
public RegexTypeMatcher(String regex, String technology) {
super(technology);

if (regex == null) {
if (StringUtils.isNullOrEmpty(regex)) {
throw new IllegalArgumentException("A regex must be supplied");
}

this.regex = Pattern.compile(regex);
}

public RegexTypeMatcher(Pattern regex, String technology) {
super(technology);

if (regex == null) {
throw new IllegalArgumentException("A regex must be supplied");
}

this.regex = regex;
}

@Override
public boolean matches(Type type) {
if (type != null && type.getFullyQualifiedName() != null) {
return Pattern.matches(regex.pattern(), type.getFullyQualifiedName());
} else {
return false;
if (type == null) {
throw new IllegalArgumentException("A type must be specified");
}

return Pattern.matches(regex.pattern(), type.getFullyQualifiedName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.structurizr.component.matcher;

import com.structurizr.component.Type;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class NameSuffixTypeMatcherTests {

@Test
void construction_ThrowsAnException_WhenPassedANullSuffix() {
assertThrowsExactly(IllegalArgumentException.class, () -> new NameSuffixTypeMatcher(null, "Technology"));
}

@Test
void construction_ThrowsAnException_WhenPassedAnEmptySuffix() {
assertThrowsExactly(IllegalArgumentException.class, () -> new NameSuffixTypeMatcher("", "Technology"));
assertThrowsExactly(IllegalArgumentException.class, () -> new NameSuffixTypeMatcher(" ", "Technology"));
}

@Test
void matches_ThrowsAnException_WhenPassedNull() {
assertThrowsExactly(IllegalArgumentException.class, () -> new NameSuffixTypeMatcher("Suffix", "Technology").matches(null));
}

@Test
void matches_ReturnsFalse_WhenThereIsNoMatch() {
assertFalse(new NameSuffixTypeMatcher("Component", "Technology").matches(new Type("com.example.SomeClass")));
}

@Test
void matches_ReturnsTrue_WhenThereIsAMatch() {
assertTrue(new NameSuffixTypeMatcher("Component", "Technology").matches(new Type("com.example.SomeComponent")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.structurizr.component.matcher;

import com.structurizr.component.Type;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class RegexSuffixTypeMatcherTests {

@Test
void construction_ThrowsAnException_WhenPassedANullRegex() {
assertThrowsExactly(IllegalArgumentException.class, () -> new RegexTypeMatcher(null, "Technology"));
}

@Test
void construction_ThrowsAnException_WhenPassedAnEmptyRegex() {
assertThrowsExactly(IllegalArgumentException.class, () -> new RegexTypeMatcher("", "Technology"));
assertThrowsExactly(IllegalArgumentException.class, () -> new RegexTypeMatcher(" ", "Technology"));
}

@Test
void matches_ThrowsAnException_WhenPassedNull() {
assertThrowsExactly(IllegalArgumentException.class, () -> new RegexTypeMatcher(".*Controller", "Technology").matches(null));
}

@Test
void matches_ReturnsFalse_WhenThereIsNoMatch() {
assertFalse(new RegexTypeMatcher(".*Controller", "Technology").matches(new Type("com.example.SomeClass")));
}

@Test
void matches_ReturnsTrue_WhenThereIsAMatch() {
assertTrue(new RegexTypeMatcher(".*Controller", "Technology").matches(new Type("com.example.SomeController")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.structurizr.component.naming;

import com.structurizr.component.Type;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DefaultNamingStrategyTests {

@Test
void nameOf() {
assertEquals("Class Name", new DefaultNamingStrategy().nameOf(new Type("com.example.ClassName")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.structurizr.component.naming;

import com.structurizr.component.Type;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class FullyQualifiedNamingStrategyTests {

@Test
void nameOf() {
assertEquals("com.example.ClassName", new FullyQualifiedNamingStrategy().nameOf(new Type("com.example.ClassName")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.structurizr.component.naming;

import com.structurizr.component.Type;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SimpleNamingStrategyTests {

@Test
void nameOf() {
assertEquals("ClassName", new SimpleNamingStrategy().nameOf(new Type("com.example.ClassName")));
}

}

0 comments on commit 4bab88f

Please sign in to comment.