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 76ebaf4 commit e8ba1f3
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
Expand All @@ -22,11 +22,23 @@ public final class ClassDirectoryTypeProvider implements TypeProvider {
private final File directory;

public ClassDirectoryTypeProvider(File directory) {
if (directory == null) {
throw new IllegalArgumentException("A directory must be supplied");
}

if (!directory.exists()) {
throw new IllegalArgumentException(directory.getAbsolutePath() + " does not exist");
}

if (!directory.isDirectory()) {
throw new IllegalArgumentException(directory.getAbsolutePath() + " is not a directory");
}

this.directory = directory;
}

public Set<Type> getTypes() {
Set<Type> types = new HashSet<>();
Set<Type> types = new LinkedHashSet<>();

Set<File> files = findClassFiles(directory);
for (File file : files) {
Expand All @@ -43,7 +55,7 @@ public Set<Type> getTypes() {
}

private Set<File> findClassFiles(File path) {
Set<File> classFiles = new HashSet<>();
Set<File> classFiles = new LinkedHashSet<>();
if (path.isDirectory()) {
File[] files = path.listFiles();
if (files != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.jar.JarEntry;

Expand All @@ -28,7 +28,7 @@ public ClassJarFileTypeProvider(File file) {
}

public Set<Type> getTypes() {
Set<Type> types = new HashSet<>();
Set<Type> types = new LinkedHashSet<>();
java.util.jar.JarFile jar = null;
try {
jar = new java.util.jar.JarFile(jarFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
Expand All @@ -25,21 +25,37 @@ public final class SourceDirectoryTypeProvider implements TypeProvider {
private static final String JAVA_FILE_EXTENSION = ".java";
private static final int DEFAULT_DESCRIPTION_LENGTH = 60;

private final Set<Type> types = new HashSet<>();
private final File directory;
private final int maximumDescriptionLength;
private final Set<Type> types = new LinkedHashSet<>();

public SourceDirectoryTypeProvider(File path) {
this(path, DEFAULT_DESCRIPTION_LENGTH);
public SourceDirectoryTypeProvider(File directory) {
this(directory, DEFAULT_DESCRIPTION_LENGTH);
}

public SourceDirectoryTypeProvider(File path, int maximumDescriptionLength) {
StaticJavaParser.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_21);
public SourceDirectoryTypeProvider(File directory, int maximumDescriptionLength) {
if (directory == null) {
throw new IllegalArgumentException("A directory must be supplied");
}

if (!directory.exists()) {
throw new IllegalArgumentException(directory.getAbsolutePath() + " does not exist");
}

parse(path, maximumDescriptionLength);
if (!directory.isDirectory()) {
throw new IllegalArgumentException(directory.getAbsolutePath() + " is not a directory");
}

this.directory = directory;
this.maximumDescriptionLength = maximumDescriptionLength;
StaticJavaParser.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_21);
}

@Override
public Set<Type> getTypes() {
return new HashSet<>(types);
parse(directory, maximumDescriptionLength);

return new LinkedHashSet<>(types);
}

private void parse(File path, int maximumDescriptionLength) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.structurizr.component.provider;

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

import java.io.File;
import java.util.Set;

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

public class ClassDirectoryTypeProviderTests {

private static final File classes = new File("build/classes/java/test");

@Test
void construction_ThrowsAnException_WhenPassedANullDirectory() {
assertThrowsExactly(IllegalArgumentException.class, () -> new ClassDirectoryTypeProvider(null));
}

@Test
void construction_ThrowsAnException_WhenPassedAPathThatDoesNotExist() {
assertThrowsExactly(IllegalArgumentException.class, () -> new ClassDirectoryTypeProvider(new File(classes, "com/example")));
}

@Test
void construction_ThrowsAnException_WhenPassedAFile() {
assertThrowsExactly(IllegalArgumentException.class, () -> new ClassDirectoryTypeProvider(new File(classes, "com/structurizr/component/provider/ClassDirectoryTypeProviderTests.class")));
}

@Test
void getTypes() {
TypeProvider typeProvider = new ClassDirectoryTypeProvider(classes);
Set<Type> types = typeProvider.getTypes();

assertTrue(types.size() > 0);
assertNotNull(types.stream().filter(t -> t.getFullyQualifiedName().equals("com.structurizr.component.provider.ClassDirectoryTypeProviderTests")));
}

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

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

import java.io.File;
import java.util.Set;

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

public class SourceDirectoryTypeProviderTests {

private static final File classes = new File("src/main/java");

@Test
void construction_ThrowsAnException_WhenPassedANullDirectory() {
assertThrowsExactly(IllegalArgumentException.class, () -> new SourceDirectoryTypeProvider(null));
}

@Test
void construction_ThrowsAnException_WhenPassedAPathThatDoesNotExist() {
assertThrowsExactly(IllegalArgumentException.class, () -> new SourceDirectoryTypeProvider(new File(classes, "com/example")));
}

@Test
void construction_ThrowsAnException_WhenPassedAFile() {
assertThrowsExactly(IllegalArgumentException.class, () -> new SourceDirectoryTypeProvider(new File(classes, "com/structurizr/component/provider/SourceDirectoryTypeProviderTests.java")));
}

@Test
void getTypes() {
TypeProvider typeProvider = new SourceDirectoryTypeProvider(classes);
Set<Type> types = typeProvider.getTypes();

assertTrue(types.size() > 0);
assertNotNull(types.stream().filter(t -> t.getFullyQualifiedName().equals("com.structurizr.component.provider.SourceDirectoryTypeProviderTests")));
}

}

0 comments on commit e8ba1f3

Please sign in to comment.