Author: kwin
Date: Thu Feb 23 10:11:18 2017
New Revision: 1784101

URL: http://svn.apache.org/viewvc?rev=1784101&view=rev
Log:
SLING-6551 allow to embed complete class directories in test bundles

Modified:
    
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/ClientSideTeleporter.java
    
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizer.java
    
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java

Modified: 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/ClientSideTeleporter.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/ClientSideTeleporter.java?rev=1784101&r1=1784100&r2=1784101&view=diff
==============================================================================
--- 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/ClientSideTeleporter.java
 (original)
+++ 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/ClientSideTeleporter.java
 Thu Feb 23 10:11:18 2017
@@ -26,6 +26,11 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.MalformedURLException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
 import java.text.SimpleDateFormat;
 import java.util.Collection;
 import java.util.Date;
@@ -69,6 +74,10 @@ public class ClientSideTeleporter extend
     
     private Logger log;
     
+    public ClientSideTeleporter() {
+        initLogger();
+    }
+    
     private InputStream buildTestBundle(Class<?> c, Collection<Class<?>> 
embeddedClasses, String bundleSymbolicName) throws IOException {
         final TinyBundle b = TinyBundles.bundle()
             .set(Constants.BUNDLE_SYMBOLICNAME, bundleSymbolicName)
@@ -81,13 +90,14 @@ public class ClientSideTeleporter extend
         }
         
         // enrich embedded classes by automatically detected dependencies
-        for(Class<?> embeddedClazz : dependencyAnalyzer.getDependencies(log)) {
-            log.info("Embed class '{}' because it is referenced and in the 
allowed package prefixes", embeddedClazz);
-            embeddedClasses.add(embeddedClazz);
+        for(Class<?> clz : dependencyAnalyzer.getDependencies(log)) {
+            log.debug("Embed dependent class '{}' because it is referenced and 
in the allowed package prefixes", clz);
+            b.add(clz);
         }
         
         // Embed specified classes
         for(Class<?> clz : embeddedClasses) {
+            log.info("Embed class '{}'", clz);
             b.add(clz);
         }
         
@@ -98,7 +108,7 @@ public class ClientSideTeleporter extend
                     @Override
                     public void process(String resourcePath, InputStream 
resourceStream) throws IOException {
                         b.add(resourcePath, resourceStream);
-                        log.info("Embedded resource '{}' in test bundle", 
resourcePath);
+                        log.info("Embed resource '{}'", resourcePath);
                     }
                     
                 };
@@ -189,6 +199,7 @@ public class ClientSideTeleporter extend
 
     public void setEnableLogging(boolean enableLogging) {
         this.enableLogging = enableLogging;
+        this.initLogger();
     }
 
     public void setPreventToUninstallBundle(boolean 
preventToUninstallTestBundle) {
@@ -199,11 +210,35 @@ public class ClientSideTeleporter extend
         this.directoryForPersistingTestBundles = 
directoryForPersistingTestBundles;
     }
 
+    /** Embeds every class found in the given directory
+     * 
+     * @throws IOException */
+    public void embedClassesDirectory(File classesDirectory) throws 
IOException, ClassNotFoundException {
+        final Path start = classesDirectory.toPath();
+        Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
+            @Override
+            public FileVisitResult visitFile(Path file, BasicFileAttributes 
attrs)
+                    throws IOException {
+                if (file.getFileName().toString().endsWith(".class")) {
+                    String className = 
start.relativize(file).toString().replace(file.getFileSystem().getSeparator(), 
".");
+                    // strip off extension
+                    className = className.substring(0, className.length() - 6);
+                    try {
+                        Class<?> clazz = 
this.getClass().getClassLoader().loadClass(className);
+                        embedClass(clazz);
+                    } catch (ClassNotFoundException e) {
+                        throw new IOException("Could not load class with name 
'" + className + "'", e);
+                    }
+                }
+                return FileVisitResult.CONTINUE;
+            }
+        });
+    }
+
     private String installTestBundle(TeleporterHttpClient httpClient) throws 
MalformedURLException, IOException {
         final SimpleDateFormat fmt = new SimpleDateFormat("HH-mm-ss");
         final String bundleSymbolicName = getClass().getSimpleName() + "." + 
classUnderTest.getSimpleName() + "." + fmt.format(new Date()) + "." + 
UUID.randomUUID();
-        log.info("Installing bundle '{}' to {}", bundleSymbolicName, baseUrl);
-        
+        log.info("Building bundle '{}'", bundleSymbolicName, baseUrl);
         try (final InputStream bundle = buildTestBundle(classUnderTest, 
embeddedClasses, bundleSymbolicName)) {
             // optionally persist the test bundle
             if (directoryForPersistingTestBundles != null) {
@@ -214,9 +249,11 @@ public class ClientSideTeleporter extend
                     IOUtils.copy(bundle, output);
                 }
                 try (InputStream bundleInput = new BufferedInputStream(new 
FileInputStream(bundleFile))) {
+                    log.info("Installing bundle '{}' to {}", 
bundleSymbolicName, baseUrl);
                     httpClient.installBundle(bundleInput, bundleSymbolicName, 
webConsoleReadyTimeoutSeconds);
                 }
             } else {
+                log.info("Installing bundle '{}' to {}", bundleSymbolicName, 
baseUrl);
                 httpClient.installBundle(bundle, bundleSymbolicName, 
webConsoleReadyTimeoutSeconds);
             }
             httpClient.verifyCorrectBundleState(bundleSymbolicName, 
webConsoleReadyTimeoutSeconds);

Modified: 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizer.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizer.java?rev=1784101&r1=1784100&r2=1784101&view=diff
==============================================================================
--- 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizer.java
 (original)
+++ 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizer.java
 Thu Feb 23 10:11:18 2017
@@ -19,6 +19,7 @@ package org.apache.sling.testing.telepor
 import static org.junit.Assert.fail;
 
 import java.io.File;
+import java.io.IOException;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.junit.rules.TeleporterRule;
@@ -47,6 +48,8 @@ import org.apache.sling.junit.rules.Tele
  *      This takes precedence over the {@code 
ClientSideTeleporter.includeDependencyPrefixes}.</li>
  *      <li>{@code ClientSideTeleporter.embedClasses}, comma-separated list of 
fully qualified class names which should be embedded in the test bundle.
  *      Use this only for classes which are not detected automatically by the 
Maven Dependency Analyzer but still should be embedded in the test bundle</li>
+ *      <li>{@code ClientSideTeleporter.embedClassesDirectories}, 
comma-separated list directories containing classfiles which should be embedded 
in the test bundle.
+ *      Use this only for classes which are not detected automatically by the 
Maven Dependency Analyzer but still should be embedded in the test bundle</li>
  *      <li>{@code ClientSideTeleporter.testReadyTimeoutSeconds}, how long to 
wait for our test to be ready on the server-side in seconds, after installing 
the test bundle. By default {@code 12}.</li>
  *      <li>{@code ClientSideTeleporter.serverUsername}, the username with 
which to send requests to the Sling server. By default {@code admin}.</li>
  *      <li>{@code ClientSideTeleporter.serverPassword}, the password with 
which to send requests to the Sling server. By default {@code admin}.</li>
@@ -61,6 +64,7 @@ public class DefaultPropertyBasedCustomi
     static final String PROPERTY_INCLUDE_DEPENDENCY_PREFIXES = 
"ClientSideTeleporter.includeDependencyPrefixes";
     static final String PROPERTY_EXCLUDE_DEPENDENCY_PREFIXES = 
"ClientSideTeleporter.excludeDependencyPrefixes";
     static final String PROPERTY_EMBED_CLASSES = 
"ClientSideTeleporter.embedClasses";
+    static final String PROPERTY_EMBED_CLASSES_DIRECTORIES = 
"ClientSideTeleporter.embedClassesDirectories";
     static final String PROPERTY_SERVER_PASSWORD = 
"ClientSideTeleporter.serverPassword";
     static final String PROPERTY_SERVER_USERNAME = 
"ClientSideTeleporter.serverUsername";
     static final String PROPERTY_TESTREADY_TIMEOUT_SECONDS = 
"ClientSideTeleporter.testReadyTimeoutSeconds";
@@ -76,6 +80,7 @@ public class DefaultPropertyBasedCustomi
     private final String includeDependencyPrefixes;
     private final String excludeDependencyPrefixes;
     private final String embedClasses;
+    private final String embedClassesDirectories;
     private final String baseUrl;
     private final String testBundleDirectory;
     private final boolean enableLogging;
@@ -88,6 +93,7 @@ public class DefaultPropertyBasedCustomi
         includeDependencyPrefixes = 
System.getProperty(PROPERTY_INCLUDE_DEPENDENCY_PREFIXES);
         excludeDependencyPrefixes = 
System.getProperty(PROPERTY_EXCLUDE_DEPENDENCY_PREFIXES);
         embedClasses = System.getProperty(PROPERTY_EMBED_CLASSES);
+        embedClassesDirectories = 
System.getProperty(PROPERTY_EMBED_CLASSES_DIRECTORIES);
         baseUrl = System.getProperty(PROPERTY_BASE_URL);
         testBundleDirectory = 
System.getProperty(PROPERTY_TESTBUNDLE_DIRECTORY);
         enableLogging = Boolean.getBoolean(PROPERTY_ENABLE_LOGGING);
@@ -122,6 +128,17 @@ public class DefaultPropertyBasedCustomi
                 }
             }
         }
+        if (StringUtils.isNotBlank(embedClassesDirectories)) {
+            for (String embedClassesDirectory : 
embedClassesDirectories.split(LIST_SEPARATOR)) {
+                if (StringUtils.isNotBlank(embedClassesDirectory)) {
+                    try {
+                        cst.embedClassesDirectory(new 
File(embedClassesDirectory));
+                    } catch (ClassNotFoundException | IOException e) {
+                        fail("Could not load class directory '" + 
embedClassesDirectory + "': " + e.getMessage());
+                    }
+                }
+            }
+        }
         if (StringUtils.isNotBlank(embedClasses)) {
             for (String embedClass : embedClasses.split(LIST_SEPARATOR)) {
                 if (StringUtils.isNotBlank(embedClass)) {

Modified: 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java?rev=1784101&r1=1784100&r2=1784101&view=diff
==============================================================================
--- 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java
 (original)
+++ 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java
 Thu Feb 23 10:11:18 2017
@@ -175,7 +175,7 @@ class TeleporterHttpClient {
                                 }
                             }
                         }
-                        throw new IllegalStateException("The test bundle is in 
state " + state +". Most probably this is due to unresolved import-packages: " 
+ reason);
+                        throw new IllegalStateException("The test bundle is in 
state '" + state +"'. Most probably this is due to unresolved import-packages: 
" + reason);
                     }
                 }
             }


Reply via email to