Skip to content

Commit

Permalink
Tidy and some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Aug 19, 2024
1 parent db3da9b commit f378637
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.apache.bcel.classfile.JavaClass;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

Expand All @@ -11,21 +12,20 @@
*/
public final class Type {

private JavaClass javaClass;
private final JavaClass javaClass;
private final String fullyQualifiedName;
private String description;
private String source;
private final Set<Type> dependencies = new HashSet<>();

private final String fullyQualifiedName;
private final Set<Type> dependencies = new LinkedHashSet<>();

public Type(JavaClass javaClass) {
this(javaClass.getClassName());

this.fullyQualifiedName = javaClass.getClassName();
this.javaClass = javaClass;
}

public Type(String fullyQualifiedName) {
this.fullyQualifiedName = fullyQualifiedName;
this.javaClass = null;
}

public String getFullyQualifiedName() {
Expand Down Expand Up @@ -56,19 +56,6 @@ public void setSource(String source) {
this.source = source;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Type type = (Type) o;
return fullyQualifiedName.equals(type.fullyQualifiedName);
}

@Override
public int hashCode() {
return Objects.hash(fullyQualifiedName);
}

public JavaClass getJavaClass() {
return this.javaClass;
}
Expand All @@ -78,13 +65,26 @@ public void addDependency(Type type) {
}

public Set<Type> getDependencies() {
return new HashSet<>(dependencies);
return new LinkedHashSet<>(dependencies);
}

public boolean isAbstract() {
return javaClass.isAbstract();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Type type = (Type) o;
return fullyQualifiedName.equals(type.fullyQualifiedName);
}

@Override
public int hashCode() {
return Objects.hash(fullyQualifiedName);
}

@Override
public String toString() {
return this.fullyQualifiedName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.structurizr.component;

import org.junit.jupiter.api.Test;

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

public class TypeTests {

private Type type;

@Test
void name() {
type = new Type("com.example.ClassName");
assertEquals("ClassName", type.getName());
}

@Test
void packageName() {
type = new Type("com.example.ClassName");
assertEquals("com.example", type.getPackageName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.structurizr.component.ComponentFinderBuilder;
import com.structurizr.component.ComponentFinderStrategyBuilder;
import com.structurizr.component.matcher.NameSuffixTypeMatcher;
import com.structurizr.component.naming.DefaultNamingStrategy;
import com.structurizr.model.*;

public class Example {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.structurizr.component.supporting;

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

import java.util.Set;

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

public class AllReferencedTypesInSamePackageSupportingTypesStrategyTests {

@Test
void findSupportingTypes() {
Type type = new Type("com.example.a.A");

Type dependency1 = new Type("com.example.a.AImpl");
Type dependency2 = new Type("com.example.util.SomeUtils");
type.addDependency(dependency1);
type.addDependency(dependency2);

type.addDependency(new Type("com.example.util.SomeUtils"));

Set<Type> supportingTypes = new AllReferencedTypesInSamePackageSupportingTypesStrategy().findSupportingTypes(type);
assertEquals(1, supportingTypes.size());
assertTrue(supportingTypes.contains(dependency1));
}

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

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

import java.util.Set;

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

public class AllReferencedTypesSupportingTypesStrategyTests {

@Test
void findSupportingTypes() {
Type type = new Type("com.example.a.A");

Type dependency1 = new Type("com.example.a.AImpl");
Type dependency2 = new Type("com.example.util.SomeUtils");
type.addDependency(dependency1);
type.addDependency(dependency2);

type.addDependency(new Type("com.example.util.SomeUtils"));

Set<Type> supportingTypes = new AllReferencedTypesSupportingTypesStrategy().findSupportingTypes(type);
assertEquals(2, supportingTypes.size());
assertTrue(supportingTypes.contains(dependency1));
assertTrue(supportingTypes.contains(dependency2));
}

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

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

import java.util.Set;

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

public class DefaultSupportingTypesStrategyTests {

@Test
void findSupportingTypes() {
Type type = new Type("com.example.a.A");
type.addDependency(new Type("com.example.a.AImpl"));

Set<Type> supportingTypes = new DefaultSupportingTypesStrategy().findSupportingTypes(type);
assertTrue(supportingTypes.isEmpty());
}

}

0 comments on commit f378637

Please sign in to comment.