From 0c4bcae003b9f7024988cc88c0aeb76d856e2bc4 Mon Sep 17 00:00:00 2001 From: ABHAY SINGH <75938293+ABHAY0O7@users.noreply.github.com> Date: Fri, 9 Apr 2021 02:51:18 +0530 Subject: [PATCH] Added test for CollectorClassFileVisitor (#74) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: César Soto Valero --- README.md | 2 +- .../core/analysis/ClassFileVisitorUtils.java | 4 +-- .../DefaultProjectDependencyAnalyzer.java | 1 + .../asm/DependencyClassFileVisitor.java | 1 + .../CollectorClassFileVisitorTest.java | 32 ++++++++++++++++++ .../ClassFileVisitorResources/test.class | Bin 0 -> 393 bytes 6 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 depclean-core/src/test/java/se/kth/depclean/core/analysis/CollectorClassFileVisitorTest.java create mode 100644 depclean-core/src/test/resources/ClassFileVisitorResources/test.class diff --git a/README.md b/README.md index eb4802e7..6565d69e 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/depclean-core/src/main/java/se/kth/depclean/core/analysis/ClassFileVisitorUtils.java b/depclean-core/src/main/java/se/kth/depclean/core/analysis/ClassFileVisitorUtils.java index f4dad7ae..bd717d60 100644 --- a/depclean-core/src/main/java/se/kth/depclean/core/analysis/ClassFileVisitorUtils.java +++ b/depclean-core/src/main/java/se/kth/depclean/core/analysis/ClassFileVisitorUtils.java @@ -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."); } } } diff --git a/depclean-core/src/main/java/se/kth/depclean/core/analysis/DefaultProjectDependencyAnalyzer.java b/depclean-core/src/main/java/se/kth/depclean/core/analysis/DefaultProjectDependencyAnalyzer.java index a562cde5..9d3ef78b 100644 --- a/depclean-core/src/main/java/se/kth/depclean/core/analysis/DefaultProjectDependencyAnalyzer.java +++ b/depclean-core/src/main/java/se/kth/depclean/core/analysis/DefaultProjectDependencyAnalyzer.java @@ -75,6 +75,7 @@ public DefaultProjectDependencyAnalyzer(boolean isIgnoredTest) { * @throws ProjectDependencyAnalyzerException if the analysis fails. * @see ProjectDependencyAnalyzer#analyze(org.apache.invoke.project.MavenProject) */ + @Override public ProjectDependencyAnalysis analyze(MavenProject project) throws ProjectDependencyAnalyzerException { try { // a map of [dependency] -> [classes] diff --git a/depclean-core/src/main/java/se/kth/depclean/core/analysis/asm/DependencyClassFileVisitor.java b/depclean-core/src/main/java/se/kth/depclean/core/analysis/asm/DependencyClassFileVisitor.java index 9a50e50f..c61ca234 100644 --- a/depclean-core/src/main/java/se/kth/depclean/core/analysis/asm/DependencyClassFileVisitor.java +++ b/depclean-core/src/main/java/se/kth/depclean/core/analysis/asm/DependencyClassFileVisitor.java @@ -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); diff --git a/depclean-core/src/test/java/se/kth/depclean/core/analysis/CollectorClassFileVisitorTest.java b/depclean-core/src/test/java/se/kth/depclean/core/analysis/CollectorClassFileVisitorTest.java new file mode 100644 index 00000000..4214211c --- /dev/null +++ b/depclean-core/src/test/java/se/kth/depclean/core/analysis/CollectorClassFileVisitorTest.java @@ -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 visitedClasses = new HashSet<>(collector.getClasses()); + Assertions.assertFalse(visitedClasses.isEmpty()); + } +} diff --git a/depclean-core/src/test/resources/ClassFileVisitorResources/test.class b/depclean-core/src/test/resources/ClassFileVisitorResources/test.class new file mode 100644 index 0000000000000000000000000000000000000000..18c6de868ef13acb03020539cca49059e5010fc9 GIT binary patch literal 393 zcmZ8dO-sW-5PjRmrb(l%_2V3G+Cy`eikE<=qKB5;H^~yWY_gKw2>n?eMDXAb@JETW zZ9$M_mU%O8<~`>7=kp7|0+%6r==<;@9AUsP+3;7MDBe`bQ@#;}WfCfKT8uAiW?JC!sD{5>+PY?8BxYo$5t2{r&=jmqTAGwAEXkQG?MT