You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2015/09/18 12:11:45 UTC
svn commit: r1703796 - in /sling/whiteboard/bdelacretaz/test-rules: pom.xml
src/main/java/org/apache/sling/testing/rules/DependencyAnalyzer.java
src/test/java/org/apache/sling/testing/rules/EmbeddedDependenciesTest.java
Author: bdelacretaz
Date: Fri Sep 18 10:11:44 2015
New Revision: 1703796
URL: http://svn.apache.org/viewvc?rev=1703796&view=rev
Log:
SLING-5040 - dependency analyzer added, not used yet
Added:
sling/whiteboard/bdelacretaz/test-rules/src/main/java/org/apache/sling/testing/rules/DependencyAnalyzer.java
Modified:
sling/whiteboard/bdelacretaz/test-rules/pom.xml
sling/whiteboard/bdelacretaz/test-rules/src/test/java/org/apache/sling/testing/rules/EmbeddedDependenciesTest.java
Modified: sling/whiteboard/bdelacretaz/test-rules/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/test-rules/pom.xml?rev=1703796&r1=1703795&r2=1703796&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/test-rules/pom.xml (original)
+++ sling/whiteboard/bdelacretaz/test-rules/pom.xml Fri Sep 18 10:11:44 2015
@@ -44,6 +44,11 @@
<version>2.0.0</version>
</dependency>
<dependency>
+ <groupId>org.apache.maven.shared</groupId>
+ <artifactId>maven-dependency-analyzer</artifactId>
+ <version>1.6</version>
+ </dependency>
+ <dependency>
<!-- TODO remove this once we remove our dummy/sample tests -->
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.junit.core</artifactId>
Added: sling/whiteboard/bdelacretaz/test-rules/src/main/java/org/apache/sling/testing/rules/DependencyAnalyzer.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/test-rules/src/main/java/org/apache/sling/testing/rules/DependencyAnalyzer.java?rev=1703796&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/test-rules/src/main/java/org/apache/sling/testing/rules/DependencyAnalyzer.java (added)
+++ sling/whiteboard/bdelacretaz/test-rules/src/main/java/org/apache/sling/testing/rules/DependencyAnalyzer.java Fri Sep 18 10:11:44 2015
@@ -0,0 +1,126 @@
+/*
+ * 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.
+ */
+package org.apache.sling.testing.rules;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.maven.shared.dependency.analyzer.asm.DependencyClassFileVisitor;
+
+/** Find the class dependencies of a class, recursively,
+ * using the maven-dependency-analyzer and optionally considering only
+ * specific package prefixes to avoid exploring the whole graph.
+ */
+class DependencyAnalyzer {
+ private final Class<?> [] classes;
+ private final Set<String> dependencyNames = new HashSet<String>();
+ private final Set<String> includes = new HashSet<String>();
+ private final Set<String> excludes = new HashSet<String>();
+
+ private DependencyAnalyzer(Class <?> ... classes) {
+ this.classes = classes;
+ }
+
+ static DependencyAnalyzer forClass(Class<?> ... classes) {
+ return new DependencyAnalyzer(classes);
+ }
+
+ DependencyAnalyzer include(String prefix) {
+ includes.add(prefix);
+ return this;
+ }
+
+ DependencyAnalyzer exclude(String prefix) {
+ excludes.add(prefix);
+ return this;
+ }
+
+ Collection<Class<?>> getDependencies() {
+ final Set<Class<?>> result = new HashSet<Class<?>>();
+ for(Class<?> c : classes) {
+ analyze(c);
+ }
+ for(String dep : dependencyNames) {
+ result.add(toClass(dep));
+ }
+ return result;
+ }
+
+ private void analyze(Class<?> c) {
+ final Set<String> deps = new HashSet<String>();
+ final String path = "/" + c.getName().replace('.', '/') + ".class";
+ final InputStream input = getClass().getResourceAsStream(path);
+ if(input == null) {
+ throw new RuntimeException("Class resource not found: " + path);
+ }
+ try {
+ try {
+ final DependencyClassFileVisitor v = new DependencyClassFileVisitor();
+ v.visitClass(c.getName(), input);
+ deps.addAll(v.getDependencies());
+ } finally {
+ input.close();
+ }
+ } catch(IOException ioe) {
+ throw new RuntimeException("IOException while reading " + path);
+ }
+
+ // Keep only accepted dependencies, and recursively analyze them
+ for(String dep : deps) {
+ if(dep.equals(c.getName())) {
+ continue;
+ }
+ if(accept(dep)) {
+ dependencyNames.add(dep);
+ analyze(toClass(dep));
+ }
+ }
+ }
+
+ private boolean accept(String className) {
+ boolean result = false;
+
+ for(String s : includes) {
+ if(className.startsWith(s)) {
+ result = true;
+ break;
+ }
+ }
+
+ // Excludes win over includes
+ if(result) {
+ for(String s : excludes) {
+ if(className.startsWith(s)) {
+ result = false;
+ break;
+ }
+ }
+ }
+ return result;
+ }
+
+ private Class<?> toClass(String className) {
+ try {
+ return getClass().getClassLoader().loadClass(className);
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException("Class not found :" + className, e);
+ }
+ }
+}
\ No newline at end of file
Modified: sling/whiteboard/bdelacretaz/test-rules/src/test/java/org/apache/sling/testing/rules/EmbeddedDependenciesTest.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/test-rules/src/test/java/org/apache/sling/testing/rules/EmbeddedDependenciesTest.java?rev=1703796&r1=1703795&r2=1703796&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/test-rules/src/test/java/org/apache/sling/testing/rules/EmbeddedDependenciesTest.java (original)
+++ sling/whiteboard/bdelacretaz/test-rules/src/test/java/org/apache/sling/testing/rules/EmbeddedDependenciesTest.java Fri Sep 18 10:11:44 2015
@@ -18,6 +18,8 @@ package org.apache.sling.testing.rules;
import static org.junit.Assert.assertEquals;
+import java.io.IOException;
+
import org.junit.Rule;
import org.junit.Test;
@@ -36,7 +38,9 @@ public class EmbeddedDependenciesTest {
;
@Test
- public void testSum() {
+ public void testSum() throws IOException {
+ // TODO - include in rule
+ // final Collection<Class<?>> deps = DependencyAnalyzer.forClass(getClass()).include("org.apache.sling.testing.rules").getDependencies();
assertEquals(84, new SomeUtility().getSum());
}