You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2022/03/05 14:48:59 UTC

[maven-dependency-analyzer] 02/02: unit tests

This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-dependency-analyzer.git

commit f8925e74758b36de3ae64193cfab4ea29a4e63d3
Author: Charles Honton <ch...@proofpoint.com>
AuthorDate: Tue Feb 22 15:08:37 2022 -0800

    unit tests
---
 .../analyzer/asm/ResultCollectorTest.java          | 56 ++++++++++++++++++++++
 .../dependency/analyzer/testcases/ArrayCases.java  | 55 +++++++++++++++++++++
 2 files changed, 111 insertions(+)

diff --git a/src/test/java/org/apache/maven/shared/dependency/analyzer/asm/ResultCollectorTest.java b/src/test/java/org/apache/maven/shared/dependency/analyzer/asm/ResultCollectorTest.java
new file mode 100644
index 0000000..f61201f
--- /dev/null
+++ b/src/test/java/org/apache/maven/shared/dependency/analyzer/asm/ResultCollectorTest.java
@@ -0,0 +1,56 @@
+package org.apache.maven.shared.dependency.analyzer.asm;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Set;
+
+import org.apache.maven.shared.dependency.analyzer.testcases.ArrayCases;
+import static org.assertj.core.api.Assertions.assertThat;
+import org.junit.Test;
+
+public class ResultCollectorTest
+{
+    Set<String> getDependencies( Class<?> inspectClass )
+        throws IOException
+    {
+        String className = inspectClass.getName();
+        String path = '/' + className.replace( '.', '/' ) + ".class";
+        DependencyClassFileVisitor visitor = new DependencyClassFileVisitor();
+        try ( InputStream is = inspectClass.getResourceAsStream( path ) )
+        {
+            visitor.visitClass( className, is );
+        }
+        return visitor.getDependencies();
+    }
+
+    @Test
+    public void testArrayCases()
+        throws IOException
+    {
+        Set<String> dependencies = getDependencies( ArrayCases.class );
+        assertThat( dependencies ).doesNotContain( "[I" );
+        assertThat( dependencies ).allSatisfy( dependency -> assertThat( dependency ).doesNotStartWith( "[" ) );
+        assertThat( dependencies )
+            .contains( "java.lang.annotation.Annotation" )
+            .contains( "java.lang.reflect.Constructor" );
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/maven/shared/dependency/analyzer/testcases/ArrayCases.java b/src/test/java/org/apache/maven/shared/dependency/analyzer/testcases/ArrayCases.java
new file mode 100644
index 0000000..55df57b
--- /dev/null
+++ b/src/test/java/org/apache/maven/shared/dependency/analyzer/testcases/ArrayCases.java
@@ -0,0 +1,55 @@
+package org.apache.maven.shared.dependency.analyzer.testcases;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
+
+import org.apache.maven.shared.dependency.analyzer.asm.DefaultMethodVisitor;
+
+public class ArrayCases
+{
+    /**
+     * Cause {@link DefaultMethodVisitor#visitMethodInsn(int, String, String, String, boolean)} to be called
+     * with primitive array
+     * @param source
+     * @return
+     */
+    public int[] primitive( int[] source )
+    {
+        // causes
+        return source.clone();
+    }
+
+    public <T> void arrayOfArrayCollectedAsReference( Class<T> cls )
+    {
+        Constructor<?>[] constructors = cls.getConstructors();
+        for ( Constructor<?> constructor : constructors )
+        {
+            for ( Annotation[] parameters : constructor.getParameterAnnotations() )
+            {
+                for ( Annotation annotation : parameters )
+                {
+                    System.out.println("Class: "+cls+", Annotation: "+ annotation );
+                }
+            }
+        }
+    }
+}