Skip to content

Commit

Permalink
More tidy up and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Aug 24, 2024
1 parent 6c8689a commit f6b55e5
Show file tree
Hide file tree
Showing 32 changed files with 404 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
*/
class ComponentFinderStrategy {

private final String technology;
private final TypeMatcher typeMatcher;
private final TypeFilter typeFilter;
private final SupportingTypesStrategy supportingTypesStrategy;
private final NamingStrategy namingStrategy;
private final ComponentVisitor componentVisitor;

ComponentFinderStrategy(TypeMatcher typeMatcher, TypeFilter typeFilter, SupportingTypesStrategy supportingTypesStrategy, NamingStrategy namingStrategy, ComponentVisitor componentVisitor) {
ComponentFinderStrategy(String technology, TypeMatcher typeMatcher, TypeFilter typeFilter, SupportingTypesStrategy supportingTypesStrategy, NamingStrategy namingStrategy, ComponentVisitor componentVisitor) {
this.technology = technology;
this.typeMatcher = typeMatcher;
this.typeFilter = typeFilter;
this.supportingTypesStrategy = supportingTypesStrategy;
Expand All @@ -44,7 +46,7 @@ Set<DiscoveredComponent> findComponents(TypeRepository typeRepository) {
if (typeMatcher.matches(type) && typeFilter.accept(type)) {
DiscoveredComponent component = new DiscoveredComponent(namingStrategy.nameOf(type), type);
component.setDescription(type.getDescription());
component.setTechnology(typeMatcher.getTechnology());
component.setTechnology(this.technology);
component.setComponentFinderStrategy(this);
components.add(component);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
public final class ComponentFinderStrategyBuilder {

private String technology;
private TypeMatcher typeMatcher;
private TypeFilter typeFilter = new DefaultTypeFilter();
private SupportingTypesStrategy supportingTypesStrategy = new DefaultSupportingTypesStrategy();
Expand All @@ -25,6 +26,12 @@ public final class ComponentFinderStrategyBuilder {
public ComponentFinderStrategyBuilder() {
}

public ComponentFinderStrategyBuilder forTechnology(String technology) {
this.technology = technology;

return this;
}

public ComponentFinderStrategyBuilder matchedBy(TypeMatcher typeMatcher) {
this.typeMatcher = typeMatcher;

Expand Down Expand Up @@ -60,7 +67,19 @@ public ComponentFinderStrategy build() {
throw new RuntimeException("A type matcher must be specified");
}

return new ComponentFinderStrategy(typeMatcher, typeFilter, supportingTypesStrategy, namingStrategy, componentVisitor);
return new ComponentFinderStrategy(technology, typeMatcher, typeFilter, supportingTypesStrategy, namingStrategy, componentVisitor);
}

@Override
public String toString() {
return "ComponentFinderStrategyBuilder{" +
"technology='" + technology + '\'' +
", typeMatcher=" + typeMatcher +
", typeFilter=" + typeFilter +
", supportingTypesStrategy=" + supportingTypesStrategy +
", namingStrategy=" + namingStrategy +
", componentVisitor=" + componentVisitor +
'}';
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,19 @@
/**
* Matches types based upon the presence of a type-level annotation.
*/
public class AnnotationTypeMatcher extends AbstractTypeMatcher {
public class AnnotationTypeMatcher implements TypeMatcher {

private final String annotationType;

public AnnotationTypeMatcher(String annotationType, String technology) {
super(technology);

public AnnotationTypeMatcher(String annotationType) {
if (StringUtils.isNullOrEmpty(annotationType)) {
throw new IllegalArgumentException("An annotation type must be supplied");
}

this.annotationType = "L" + annotationType.replace(".", "/") + ";";
}

public AnnotationTypeMatcher(Class<? extends Annotation> annotation, String technology) {
super(technology);

public AnnotationTypeMatcher(Class<? extends Annotation> annotation) {
if (annotation == null) {
throw new IllegalArgumentException("An annotation must be supplied");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
/**
* Matches types where the type extends the specified class.
*/
public class ExtendsTypeMatcher extends AbstractTypeMatcher {
public class ExtendsTypeMatcher implements TypeMatcher {

private static final Log log = LogFactory.getLog(ExtendsTypeMatcher.class);

private final String className;

public ExtendsTypeMatcher(String className, String technology) {
super(technology);

public ExtendsTypeMatcher(String className) {
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
Expand Up @@ -11,15 +11,13 @@
/**
* Matches types where the type implements the specified interface.
*/
public class ImplementsTypeMatcher extends AbstractTypeMatcher {
public class ImplementsTypeMatcher implements TypeMatcher {

private static final Log log = LogFactory.getLog(ImplementsTypeMatcher.class);

private final String interfaceName;

public ImplementsTypeMatcher(String interfaceName, String technology) {
super(technology);

public ImplementsTypeMatcher(String interfaceName) {
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
Expand Up @@ -6,13 +6,11 @@
/**
* Matches types where the name of the type ends with the specified suffix.
*/
public class NameSuffixTypeMatcher extends AbstractTypeMatcher {
public class NameSuffixTypeMatcher implements TypeMatcher {

private final String suffix;

public NameSuffixTypeMatcher(String suffix, String technology) {
super(technology);

public NameSuffixTypeMatcher(String suffix) {
if (StringUtils.isNullOrEmpty(suffix)) {
throw new IllegalArgumentException("A suffix must be supplied");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
/**
* Matches types using a regex against the fully qualified type name.
*/
public class RegexTypeMatcher extends AbstractTypeMatcher {
public class RegexTypeMatcher implements TypeMatcher {

private final Pattern regex;

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

public RegexTypeMatcher(String regex) {
if (StringUtils.isNullOrEmpty(regex)) {
throw new IllegalArgumentException("A regex must be supplied");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ public interface TypeMatcher {

boolean matches(Type type);

String getTechnology();

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public String nameOf(Type type) {
return String.join(" ", parts);
}

@Override
public String toString() {
return "DefaultNamingStrategy{}";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public Set<Type> findSupportingTypes(Type type, TypeRepository typeRepository) {
return Collections.emptySet();
}

@Override
public String toString() {
return "DefaultSupportingTypesStrategy{}";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public class DefaultComponentVisitor implements ComponentVisitor {
public void visit(Component component) {
}

@Override
public String toString() {
return "DefaultComponentVisitor{}";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ public class AnnotationTypeMatcherTests {

@Test
void construction_ThrowsAnException_WhenPassedANullName() {
assertThrowsExactly(IllegalArgumentException.class, () -> new AnnotationTypeMatcher((String)null, "Technology"));
assertThrowsExactly(IllegalArgumentException.class, () -> new AnnotationTypeMatcher((String)null));
}

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

@Test
void construction_ThrowsAnException_WhenPassedANullClass() {
assertThrowsExactly(IllegalArgumentException.class, () -> new AnnotationTypeMatcher((Class<? extends Annotation>) null, "Technology"));
assertThrowsExactly(IllegalArgumentException.class, () -> new AnnotationTypeMatcher((Class<? extends Annotation>) null));
}

@Test
void matches_ThrowsAnException_WhenPassedNull() {
assertThrowsExactly(IllegalArgumentException.class, () -> new AnnotationTypeMatcher("com.example.AnnotationName", "Technology").matches(null));
assertThrowsExactly(IllegalArgumentException.class, () -> new AnnotationTypeMatcher("com.example.AnnotationName").matches(null));
}

@Test
void matches_ReturnsFalse_WhenThereIsNoUnderlyingJavaClass() {
Type type = new Type("com.structurizr.component.matcher.annotationTypeMatcher.CustomerController");

assertFalse(new AnnotationTypeMatcher("com.structurizr.component.matcher.annotationTypeMatcher.Controller", "Technology").matches(type));
assertFalse(new AnnotationTypeMatcher("com.structurizr.component.matcher.annotationTypeMatcher.Controller").matches(type));
}

@Test
Expand All @@ -45,7 +45,7 @@ void matches_ReturnsFalse_WhenThereIsNoMatch() throws Exception {
ClassParser parser = new ClassParser(new File(classes, "com/structurizr/component/matcher/annotationTypeMatcher/CustomerController.class").getAbsolutePath());
Type type = new Type(parser.parse());

assertFalse(new AnnotationTypeMatcher("com.structurizr.component.matcher.annotationTypeMatcher.Repository", "Technology").matches(type));
assertFalse(new AnnotationTypeMatcher("com.structurizr.component.matcher.annotationTypeMatcher.Repository").matches(type));
}

@Test
Expand All @@ -54,7 +54,7 @@ void matches_ReturnsTrue_WhenThereIsAMatch() throws Exception {
ClassParser parser = new ClassParser(new File(classes, "com/structurizr/component/matcher/annotationTypeMatcher/CustomerController.class").getAbsolutePath());
Type type = new Type(parser.parse());

assertTrue(new AnnotationTypeMatcher("com.structurizr.component.matcher.annotationTypeMatcher.Controller", "Technology").matches(type));
assertTrue(new AnnotationTypeMatcher("com.structurizr.component.matcher.annotationTypeMatcher.Controller").matches(type));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ public class ExtendsTypeMatcherTests {

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

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

@Test
void matches_ThrowsAnException_WhenPassedNull() {
assertThrowsExactly(IllegalArgumentException.class, () -> new ExtendsTypeMatcher("com.example.ClassName", "Technology").matches(null));
assertThrowsExactly(IllegalArgumentException.class, () -> new ExtendsTypeMatcher("com.example.ClassName").matches(null));
}

@Test
void matches_ReturnsFalse_WhenThereIsNoUnderlyingJavaClass() {
Type type = new Type("com.structurizr.component.matcher.extendsTypeMatcher.CustomerController");

assertFalse(new ExtendsTypeMatcher("com.structurizr.component.matcher.extendsTypeMatcher.AbstractController", "Technology").matches(type));
assertFalse(new ExtendsTypeMatcher("com.structurizr.component.matcher.extendsTypeMatcher.AbstractController").matches(type));
}

@Test
Expand All @@ -40,7 +40,7 @@ void matches_ReturnsFalse_WhenThereIsNoMatch() throws Exception {
ClassParser parser = new ClassParser(new File(classes, "com/structurizr/component/matcher/extendsTypeMatcher/CustomerController.class").getAbsolutePath());
Type type = new Type(parser.parse());

assertFalse(new ExtendsTypeMatcher("com.structurizr.component.matcher.extendsTypeMatcher.AbstractRepository", "Technology").matches(type));
assertFalse(new ExtendsTypeMatcher("com.structurizr.component.matcher.extendsTypeMatcher.AbstractRepository").matches(type));
}

@Test
Expand All @@ -49,7 +49,7 @@ void matches_ReturnsTrue_WhenThereIsAMatch() throws Exception {
ClassParser parser = new ClassParser(new File(classes, "com/structurizr/component/matcher/extendsTypeMatcher/CustomerController.class").getAbsolutePath());
Type type = new Type(parser.parse());

assertTrue(new ExtendsTypeMatcher("com.structurizr.component.matcher.extendsTypeMatcher.AbstractController", "Technology").matches(type));
assertTrue(new ExtendsTypeMatcher("com.structurizr.component.matcher.extendsTypeMatcher.AbstractController").matches(type));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ public class ImplementsTypeMatcherTests {

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

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

@Test
void matches_ThrowsAnException_WhenPassedNull() {
assertThrowsExactly(IllegalArgumentException.class, () -> new ImplementsTypeMatcher("com.example.InterfaceName", "Technology").matches(null));
assertThrowsExactly(IllegalArgumentException.class, () -> new ImplementsTypeMatcher("com.example.InterfaceName").matches(null));
}

@Test
void matches_ReturnsFalse_WhenThereIsNoUnderlyingJavaClass() {
Type type = new Type("com.structurizr.component.matcher.implementsTypeMatcher.CustomerController");

assertFalse(new ImplementsTypeMatcher("com.structurizr.component.matcher.implementsTypeMatcher.Controller", "Technology").matches(type));
assertFalse(new ImplementsTypeMatcher("com.structurizr.component.matcher.implementsTypeMatcher.Controller").matches(type));
}

@Test
Expand All @@ -39,7 +39,7 @@ void matches_ReturnsFalse_WhenThereIsNoMatch() throws Exception {
ClassParser parser = new ClassParser(new File(classes, "com/structurizr/component/matcher/implementsTypeMatcher/CustomerController.class").getAbsolutePath());
Type type = new Type(parser.parse());

assertFalse(new ImplementsTypeMatcher("com.structurizr.component.matcher.implementsTypeMatcher.Repository", "Technology").matches(type));
assertFalse(new ImplementsTypeMatcher("com.structurizr.component.matcher.implementsTypeMatcher.Repository").matches(type));
}

@Test
Expand All @@ -48,7 +48,7 @@ void matches_ReturnsTrue_WhenThereIsAMatch() throws Exception {
ClassParser parser = new ClassParser(new File(classes, "com/structurizr/component/matcher/implementsTypeMatcher/CustomerController.class").getAbsolutePath());
Type type = new Type(parser.parse());

assertTrue(new ImplementsTypeMatcher("com.structurizr.component.matcher.implementsTypeMatcher.Controller", "Technology").matches(type));
assertTrue(new ImplementsTypeMatcher("com.structurizr.component.matcher.implementsTypeMatcher.Controller").matches(type));
}

}
Loading

0 comments on commit f6b55e5

Please sign in to comment.