Skip to content

Commit

Permalink
Added test for CollectorClassFileVisitor (#74)
Browse files Browse the repository at this point in the history
Co-authored-by: César Soto Valero <[email protected]>
  • Loading branch information
ABHAY0O7 and cesarsotovalero authored Apr 8, 2021
1 parent 30afab9 commit 0c4bcae
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ You can configure the `pom.xml` file of your Maven project to use DepClean as pa
```

Or you can run DepClean directly from the command line.
Let's see it in action with the project https://github.com/apache/commons-numbers/tree/master/commons-numbers-examples/examples-jmh:
Let's see it in action with the project [Apache Commons Numbers](https://github.com/apache/commons-numbers/tree/master/commons-numbers-examples/examples-jmh)!

![Demo](https://github.com/castor-software/depclean/blob/master/.img/demo.gif)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public static void accept(URL url, ClassFileVisitor visitor)
if (file.isDirectory()) {
acceptDirectory(file, visitor);
} else if (file.exists()) {
throw new IllegalArgumentException(message + url);
throw new IllegalArgumentException(message + url + " because file is not a directory.");
}
} catch (URISyntaxException exception) {
throw new IllegalArgumentException(message + url, exception);
}
} else {
throw new IllegalArgumentException(message + url);
throw new IllegalArgumentException(message + url + " because url isn't pointing a file.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public DefaultProjectDependencyAnalyzer(boolean isIgnoredTest) {
* @throws ProjectDependencyAnalyzerException if the analysis fails.
* @see <code>ProjectDependencyAnalyzer#analyze(org.apache.invoke.project.MavenProject)</code>
*/
@Override
public ProjectDependencyAnalysis analyze(MavenProject project) throws ProjectDependencyAnalyzerException {
try {
// a map of [dependency] -> [classes]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class DependencyClassFileVisitor implements ClassFileVisitor {
*
* @see org.apache.invoke.shared.dependency.analyzer.ClassFileVisitor#visitClass(java.lang.String.java.io.InputStream)
*/
@Override
public void visitClass(String className, InputStream in) {
try {
ClassReader reader = new ClassReader(in);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package se.kth.depclean.core.analysis;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@Slf4j
class CollectorClassFileVisitorTest {

private static final File classFile = new File("src/test/resources/ClassFileVisitorResources/test.class");
private static final String className = "test";
private static final CollectorClassFileVisitor collector = new CollectorClassFileVisitor();

@Test
@DisplayName("Test that the class is visited and added to the set of visited classes")
void whenClassIsVisited_thenItIsAddedToTheSetOfVisitedClasses() throws FileNotFoundException {
FileInputStream fileInputStream = new FileInputStream(classFile);
try {
collector.visitClass(className, fileInputStream);
} catch (IllegalArgumentException e) {
log.error("Failed to visit the class at: " + classFile.getAbsolutePath());
}
Set<String> visitedClasses = new HashSet<>(collector.getClasses());
Assertions.assertFalse(visitedClasses.isEmpty());
}
}
Binary file not shown.

0 comments on commit 0c4bcae

Please sign in to comment.