Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test for CollectorClassFileVisitor #74

Merged
merged 3 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.