-
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a couple more supporting types strategies.
- Loading branch information
1 parent
e6a78c7
commit d5e0d20
Showing
11 changed files
with
256 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...com/structurizr/component/supporting/ImplementationWithPrefixSupportingTypesStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.structurizr.component.supporting; | ||
|
||
import com.structurizr.component.Type; | ||
import com.structurizr.component.TypeRepository; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* A strategy that, given an interface, finds the implementation class with the specified prefix. | ||
*/ | ||
public class ImplementationWithPrefixSupportingTypesStrategy implements SupportingTypesStrategy { | ||
|
||
private final String prefix; | ||
|
||
public ImplementationWithPrefixSupportingTypesStrategy(String prefix) { | ||
this.prefix = prefix; | ||
} | ||
|
||
@Override | ||
public Set<Type> findSupportingTypes(Type type, TypeRepository typeRepository) { | ||
if (!type.isInterface()) { | ||
throw new IllegalArgumentException("The type " + type.getFullyQualifiedName() + " is not an interface"); | ||
} | ||
|
||
return typeRepository.getTypes().stream() | ||
.filter(dependency -> dependency.implementsInterface(type) && dependency.getName().equals(prefix + type.getName())) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ImplementationWithPrefixSupportingTypesStrategy{" + | ||
"prefix='" + prefix + '\'' + | ||
'}'; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...com/structurizr/component/supporting/ImplementationWithSuffixSupportingTypesStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.structurizr.component.supporting; | ||
|
||
import com.structurizr.component.Type; | ||
import com.structurizr.component.TypeRepository; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* A strategy that, given an interface, finds the implementation class with the specified suffix. | ||
*/ | ||
public class ImplementationWithSuffixSupportingTypesStrategy implements SupportingTypesStrategy { | ||
|
||
private final String suffix; | ||
|
||
public ImplementationWithSuffixSupportingTypesStrategy(String suffix) { | ||
this.suffix = suffix; | ||
} | ||
|
||
@Override | ||
public Set<Type> findSupportingTypes(Type type, TypeRepository typeRepository) { | ||
if (!type.isInterface()) { | ||
throw new IllegalArgumentException("The type " + type.getFullyQualifiedName() + " is not an interface"); | ||
} | ||
|
||
return typeRepository.getTypes().stream() | ||
.filter(dependency -> dependency.implementsInterface(type) && dependency.getName().equals(type.getName() + suffix)) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ImplementationWithSuffixSupportingTypesStrategy{" + | ||
"suffix='" + suffix + '\'' + | ||
'}'; | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...tructurizr/component/supporting/ImplementationWithPrefixSupportingTypesStrategyTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.structurizr.component.supporting; | ||
|
||
import com.structurizr.component.Type; | ||
import com.structurizr.component.TypeRepository; | ||
import org.apache.bcel.classfile.ClassParser; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class ImplementationWithPrefixSupportingTypesStrategyTests { | ||
|
||
private final File classes = new File("build/classes/java/test"); | ||
|
||
@Test | ||
void findSupportingTypes() throws Exception { | ||
Type interfaceType = new Type(new ClassParser(new File(classes, "com/structurizr/component/supporting/implementation/ExampleRepository.class").getAbsolutePath()).parse()); | ||
Type implementationTypeWithPrefix = new Type(new ClassParser(new File(classes, "com/structurizr/component/supporting/implementation/JdbcExampleRepository.class").getAbsolutePath()).parse()); | ||
Type implementationTypeWithSuffix = new Type(new ClassParser(new File(classes, "com/structurizr/component/supporting/implementation/ExampleRepositoryImpl.class").getAbsolutePath()).parse()); | ||
|
||
TypeRepository typeRepository = new TypeRepository(); | ||
typeRepository.add(interfaceType); | ||
typeRepository.add(implementationTypeWithPrefix); | ||
typeRepository.add(implementationTypeWithSuffix); | ||
|
||
Set<Type> supportingTypes = new ImplementationWithPrefixSupportingTypesStrategy("Jdbc").findSupportingTypes(interfaceType, typeRepository); | ||
assertEquals(1, supportingTypes.size()); | ||
assertTrue(supportingTypes.contains(implementationTypeWithPrefix)); | ||
} | ||
|
||
@Test | ||
void findSupportingTypes_ThrowsAnException_WhenTheTypeIsNotAnInterface() throws Exception { | ||
try { | ||
Type type = new Type(new ClassParser(new File(classes, "com/structurizr/component/supporting/implementation/JdbcExampleRepository.class").getAbsolutePath()).parse()); | ||
new ImplementationWithPrefixSupportingTypesStrategy("Impl").findSupportingTypes(type, null); | ||
fail(); | ||
} catch (Exception e) { | ||
assertEquals("The type com.structurizr.component.supporting.implementation.JdbcExampleRepository is not an interface", e.getMessage()); | ||
} | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...tructurizr/component/supporting/ImplementationWithSuffixSupportingTypesStrategyTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.structurizr.component.supporting; | ||
|
||
import com.structurizr.component.Type; | ||
import com.structurizr.component.TypeRepository; | ||
import org.apache.bcel.classfile.ClassFormatException; | ||
import org.apache.bcel.classfile.ClassParser; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class ImplementationWithSuffixSupportingTypesStrategyTests { | ||
|
||
private final File classes = new File("build/classes/java/test"); | ||
|
||
@Test | ||
void findSupportingTypes() throws Exception { | ||
Type interfaceType = new Type(new ClassParser(new File(classes, "com/structurizr/component/supporting/implementation/ExampleRepository.class").getAbsolutePath()).parse()); | ||
Type implementationTypeWithPrefix = new Type(new ClassParser(new File(classes, "com/structurizr/component/supporting/implementation/JdbcExampleRepository.class").getAbsolutePath()).parse()); | ||
Type implementationTypeWithSuffix = new Type(new ClassParser(new File(classes, "com/structurizr/component/supporting/implementation/ExampleRepositoryImpl.class").getAbsolutePath()).parse()); | ||
|
||
TypeRepository typeRepository = new TypeRepository(); | ||
typeRepository.add(interfaceType); | ||
typeRepository.add(implementationTypeWithPrefix); | ||
typeRepository.add(implementationTypeWithSuffix); | ||
|
||
Set<Type> supportingTypes = new ImplementationWithSuffixSupportingTypesStrategy("Impl").findSupportingTypes(interfaceType, typeRepository); | ||
assertEquals(1, supportingTypes.size()); | ||
assertTrue(supportingTypes.contains(implementationTypeWithSuffix)); | ||
} | ||
|
||
@Test | ||
void findSupportingTypes_ThrowsAnException_WhenTheTypeIsNotAnInterface() throws Exception { | ||
try { | ||
Type type = new Type(new ClassParser(new File(classes, "com/structurizr/component/supporting/implementation/JdbcExampleRepository.class").getAbsolutePath()).parse()); | ||
new ImplementationWithSuffixSupportingTypesStrategy("Impl").findSupportingTypes(type, null); | ||
fail(); | ||
} catch (Exception e) { | ||
assertEquals("The type com.structurizr.component.supporting.implementation.JdbcExampleRepository is not an interface", e.getMessage()); | ||
} | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
.../src/test/java/com/structurizr/component/supporting/implementation/ExampleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.structurizr.component.supporting.implementation; | ||
|
||
public interface ExampleRepository { | ||
} |
4 changes: 4 additions & 0 deletions
4
.../test/java/com/structurizr/component/supporting/implementation/ExampleRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.structurizr.component.supporting.implementation; | ||
|
||
public class ExampleRepositoryImpl implements ExampleRepository { | ||
} |
4 changes: 4 additions & 0 deletions
4
.../test/java/com/structurizr/component/supporting/implementation/JdbcExampleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.structurizr.component.supporting.implementation; | ||
|
||
public class JdbcExampleRepository implements ExampleRepository { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters