You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2016/05/15 12:43:37 UTC

svn commit: r1743902 - in /sling/trunk/testing/junit: core/src/main/java/org/apache/sling/junit/rules/ teleporter/ teleporter/src/main/java/org/apache/sling/testing/teleporter/client/ teleporter/src/test/java/org/apache/sling/testing/teleporter/client/

Author: kwin
Date: Sun May 15 12:43:36 2016
New Revision: 1743902

URL: http://svn.apache.org/viewvc?rev=1743902&view=rev
Log:
SLING-5677 add default customizer based on system properties

Added:
    sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizer.java   (with props)
    sling/trunk/testing/junit/teleporter/src/test/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizerTest.java   (with props)
Modified:
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/TeleporterRule.java
    sling/trunk/testing/junit/teleporter/pom.xml

Modified: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/TeleporterRule.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/TeleporterRule.java?rev=1743902&r1=1743901&r2=1743902&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/TeleporterRule.java (original)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/TeleporterRule.java Sun May 15 12:43:36 2016
@@ -40,6 +40,8 @@ public abstract class TeleporterRule ext
     /** Class name pattern for Customizers */ 
     public static final String CUSTOMIZER_PATTERN = "org.apache.sling.junit.teleporter.customizers.<NAME>Customizer";
     
+    private static final String DEFAULT_CUSTOMIZER_CLASS = "org.apache.sling.testing.teleporter.client.DefaultPropertyBasedCustomizer";
+    
     /** Customizer is used client-side to setup the server URL and other parameters */
     public static interface Customizer {
         void customize(TeleporterRule t, String options);
@@ -97,21 +99,26 @@ public abstract class TeleporterRule ext
     protected void customize() {
         // As with the client-side rule implementation, instantiate our Customizer
         // dynamically to avoid requiring its class on the server side.
-        if(!isServerSide() && (clientSetupOptions != null) && !clientSetupOptions.isEmpty()) {
-            String customizerClassName = clientSetupOptions;
-            String customizerOptions = "";
-            final int firstColon = clientSetupOptions.indexOf(":");
-            if(firstColon > 0) {
-                customizerClassName = clientSetupOptions.substring(0, firstColon);
-                customizerOptions = clientSetupOptions.substring(firstColon + 1);
-            }
-            // If a short name is used, transform it using our pattern. Simplifies referring
-            // to these customizers in test code, without having to make the customizer
-            // classes accessible to this bundle
-            if(!customizerClassName.contains(".")) {
-                customizerClassName = CUSTOMIZER_PATTERN.replace("<NAME>", customizerClassName);
+        if(!isServerSide()) {
+            if((clientSetupOptions != null) && !clientSetupOptions.isEmpty()) {
+                String customizerClassName = clientSetupOptions;
+                String customizerOptions = "";
+                final int firstColon = clientSetupOptions.indexOf(":");
+                if(firstColon > 0) {
+                    customizerClassName = clientSetupOptions.substring(0, firstColon);
+                    customizerOptions = clientSetupOptions.substring(firstColon + 1);
+                }
+                // If a short name is used, transform it using our pattern.
+                // Simplifies referring to these customizers in test code,
+                // without having to make the customizer classes accessible to this bundle
+                if(!customizerClassName.contains(".")) {
+                    customizerClassName = CUSTOMIZER_PATTERN.replace("<NAME>", customizerClassName);
+                }
+                createInstance(Customizer.class, customizerClassName).customize(this, customizerOptions);
+            } else {
+                // use default customizer which is based on system properties
+                createInstance(Customizer.class, DEFAULT_CUSTOMIZER_CLASS).customize(this, null);
             }
-            createInstance(Customizer.class, customizerClassName).customize(this, customizerOptions);
         }
     }
     

Modified: sling/trunk/testing/junit/teleporter/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/teleporter/pom.xml?rev=1743902&r1=1743901&r2=1743902&view=diff
==============================================================================
--- sling/trunk/testing/junit/teleporter/pom.xml (original)
+++ sling/trunk/testing/junit/teleporter/pom.xml Sun May 15 12:43:36 2016
@@ -51,7 +51,7 @@
     <dependency>
       <groupId>org.apache.sling</groupId>
       <artifactId>org.apache.sling.junit.core</artifactId>
-      <version>1.0.14</version>
+      <version>1.0.17-SNAPSHOT</version>
     </dependency>
     <dependency>
       <groupId>org.apache.sling</groupId>
@@ -76,6 +76,11 @@
       <scope>test</scope>
     </dependency>
     <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+      <version>3.4</version>
+    </dependency>
+    <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
     </dependency>
@@ -90,6 +95,11 @@
       <version>1.57</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-all</artifactId>
+      <version>1.10.19</version>
+    </dependency>
   </dependencies>
   
   <scm>

Added: 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=1743902&view=auto
==============================================================================
--- sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizer.java (added)
+++ sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizer.java Sun May 15 12:43:36 2016
@@ -0,0 +1,116 @@
+/*
+ * 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.teleporter.client;
+
+import static org.junit.Assert.fail;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.junit.rules.TeleporterRule;
+import org.apache.sling.junit.rules.TeleporterRule.Customizer;
+
+/**
+ * This is the default {@link Customizer} which is used in case {@code TeleporterRule.forClass(Class)} was called.
+ * It relies on system properties to configure the important aspects of teleporting.
+ * <br>
+ * It assumes that there is already a running Sling server at the given baseUrl.
+ * To provision such a server the <a href="https://sling.apache.org/documentation/development/slingstart.html#slingstart-maven-plugin">slingstart-maven-plugin</a>
+ * could be used for example.
+ * <br>
+ * The following system properties must be set for this Customizer to work
+ *  <ul>
+ *      <li>{@code ClientSideTeleporter.baseUrl}, the server url on which Sling is already running</li>
+ * </ul>
+ * 
+ * The following system properties may be set to further tweak the behavior:
+ * <ul>
+ *      <li>{@code ClientSideTeleporter.includeDependencyPrefixes}, comma-separated list of package prefixes for classes referenced from the IT. 
+ *      Only the classes having one of the given package prefix are included in the bundle being deployed to the given Sling instance together with the IT class itself. 
+ *      They are only included though in case they are referenced! If this is not set, no referenced classes will be included.</li>
+ *      <li>{@code ClientSideTeleporter.excludeDependencyPrefixes}, comma-separated list of package prefixes for classes referenced from the IT. 
+ *      Classes having one of the given package prefix will not be included in the bundle being deployed to the given Sling instance together with the IT class itself. 
+ *      This takes precedence over the {@code ClientSideTeleporter.includeDependencyPrefixes}.</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>
+ * </ul>
+ */
+public class DefaultPropertyBasedCustomizer implements Customizer {
+    static final String PROPERTY_BASE_URL = "ClientSideTeleporter.baseUrl";
+    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_SERVER_PASSWORD = "ClientSideTeleporter.serverPassword";
+    static final String PROPERTY_SERVER_USERNAME = "ClientSideTeleporter.serverUsername";
+    static final String PROPERTY_TESTREADY_TIMEOUT_SECONDS = "ClientSideTeleporter.testReadyTimeoutSeconds";
+    
+    static final String LIST_SEPARATOR = ",";
+    
+    private final int testReadyTimeout;
+    private final String serverUsername;
+    private final String serverPassword;
+    private final String includeDependencyPrefixes;
+    private final String excludeDependencyPrefixes;
+    private final String embedClasses;
+    private final String baseUrl;
+
+    public DefaultPropertyBasedCustomizer() {
+        testReadyTimeout = Integer.getInteger(PROPERTY_TESTREADY_TIMEOUT_SECONDS, 12);
+        serverUsername = System.getProperty(PROPERTY_SERVER_USERNAME, "admin");
+        serverPassword = System.getProperty(PROPERTY_SERVER_PASSWORD, "admin");
+        includeDependencyPrefixes = System.getProperty(PROPERTY_INCLUDE_DEPENDENCY_PREFIXES);
+        excludeDependencyPrefixes = System.getProperty(PROPERTY_EXCLUDE_DEPENDENCY_PREFIXES);
+        embedClasses = System.getProperty(PROPERTY_EMBED_CLASSES);
+        baseUrl = System.getProperty(PROPERTY_BASE_URL);
+    }
+
+    @Override
+    public void customize(TeleporterRule rule, String options) {
+        final ClientSideTeleporter cst = (ClientSideTeleporter)rule;
+        if (StringUtils.isBlank(baseUrl)) {
+            fail("The mandatory system property " + PROPERTY_BASE_URL + " is not set!");
+        }
+        cst.setBaseUrl(baseUrl);
+        cst.setServerCredentials(serverUsername, serverPassword);
+        cst.setTestReadyTimeoutSeconds(testReadyTimeout);
+        if (StringUtils.isNotBlank(includeDependencyPrefixes)) {
+            for (String includeDependencyPrefix : includeDependencyPrefixes.split(LIST_SEPARATOR)) {
+                if (StringUtils.isNotBlank(includeDependencyPrefix)) {
+                    cst.includeDependencyPrefix(includeDependencyPrefix);
+                }
+            }
+        }
+        if (StringUtils.isNotBlank(excludeDependencyPrefixes)) {
+            for (String excludeDependencyPrefix : excludeDependencyPrefixes.split(LIST_SEPARATOR)) {
+                if (StringUtils.isNotBlank(excludeDependencyPrefix)) {
+                    cst.excludeDependencyPrefix(excludeDependencyPrefix);
+                }
+            }
+        }
+        if (StringUtils.isNotBlank(embedClasses)) {
+            for (String embedClass : embedClasses.split(LIST_SEPARATOR)) {
+                if (StringUtils.isNotBlank(embedClass)) {
+                    try {
+                        Class<?> clazz = this.getClass().getClassLoader().loadClass(embedClass);
+                        cst.embedClass(clazz);
+                    } catch (ClassNotFoundException e) {
+                        fail("Could not load class with name '" + embedClass + "': " + e.getMessage());
+                    }
+                }
+            }
+        }
+    }
+}

Propchange: sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/testing/junit/teleporter/src/test/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizerTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/teleporter/src/test/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizerTest.java?rev=1743902&view=auto
==============================================================================
--- sling/trunk/testing/junit/teleporter/src/test/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizerTest.java (added)
+++ sling/trunk/testing/junit/teleporter/src/test/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizerTest.java Sun May 15 12:43:36 2016
@@ -0,0 +1,88 @@
+/*
+ * 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.teleporter.client;
+
+import java.util.Properties;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class DefaultPropertyBasedCustomizerTest {
+
+    private DefaultPropertyBasedCustomizer customizer;
+    @Mock
+    private ClientSideTeleporter clientSideTeleporter;
+    
+    @Before
+    public void setUp() {
+        System.setProperties(null);
+    }
+
+    @Test(expected=AssertionError.class)
+    public void testBaseUrlNotSet() {
+        customizer = new DefaultPropertyBasedCustomizer();
+        customizer.customize(clientSideTeleporter, null);
+    }
+
+    @Test(expected=AssertionError.class)
+    public void testEmptyBaseUrl() {
+        Properties props = new Properties();
+        props.setProperty(DefaultPropertyBasedCustomizer.PROPERTY_BASE_URL, "   ");
+        System.setProperties(props);
+        customizer = new DefaultPropertyBasedCustomizer();
+        customizer.customize(clientSideTeleporter, null);
+    }
+
+    @Test
+    public void testSettingAllProperties() {
+        Properties props = new Properties();
+        props.setProperty(DefaultPropertyBasedCustomizer.PROPERTY_BASE_URL, "base-url");
+        props.setProperty(DefaultPropertyBasedCustomizer.PROPERTY_INCLUDE_DEPENDENCY_PREFIXES, "include-dependency1,include-dependency2");
+        props.setProperty(DefaultPropertyBasedCustomizer.PROPERTY_EXCLUDE_DEPENDENCY_PREFIXES, "exclude-dependency1,exclude-dependency2");
+        props.setProperty(DefaultPropertyBasedCustomizer.PROPERTY_EMBED_CLASSES, "org.apache.sling.testing.teleporter.client.ClassResourceVisitor,org.apache.sling.testing.teleporter.client.ClientSideTeleporter");
+        props.setProperty(DefaultPropertyBasedCustomizer.PROPERTY_TESTREADY_TIMEOUT_SECONDS, "50");
+        props.setProperty(DefaultPropertyBasedCustomizer.PROPERTY_SERVER_USERNAME, "adminuser");
+        props.setProperty(DefaultPropertyBasedCustomizer.PROPERTY_SERVER_PASSWORD, "adminpassword");
+        System.setProperties(props);
+        customizer = new DefaultPropertyBasedCustomizer();
+        customizer.customize(clientSideTeleporter, null);
+        Mockito.verify(clientSideTeleporter).setBaseUrl("base-url");
+        Mockito.verify(clientSideTeleporter).includeDependencyPrefix("include-dependency1");
+        Mockito.verify(clientSideTeleporter).includeDependencyPrefix("include-dependency2");
+        Mockito.verify(clientSideTeleporter).excludeDependencyPrefix("exclude-dependency1");
+        Mockito.verify(clientSideTeleporter).excludeDependencyPrefix("exclude-dependency2");
+        Mockito.verify(clientSideTeleporter).embedClass(ClassResourceVisitor.class);
+        Mockito.verify(clientSideTeleporter).embedClass(ClientSideTeleporter.class);
+        Mockito.verify(clientSideTeleporter).setTestReadyTimeoutSeconds(50);
+        Mockito.verify(clientSideTeleporter).setServerCredentials("adminuser", "adminpassword");
+    }
+
+    @Test(expected=AssertionError.class)
+    public void testEmbeddingInvalidClass() {
+        Properties props = new Properties();
+        props.setProperty(DefaultPropertyBasedCustomizer.PROPERTY_BASE_URL, "base-url");
+        props.setProperty(DefaultPropertyBasedCustomizer.PROPERTY_EMBED_CLASSES, "org.apache.sling.testing.teleporter.client.InvalidClass");
+        System.setProperties(props);
+        customizer = new DefaultPropertyBasedCustomizer();
+        customizer.customize(clientSideTeleporter, null);
+    }
+}

Propchange: sling/trunk/testing/junit/teleporter/src/test/java/org/apache/sling/testing/teleporter/client/DefaultPropertyBasedCustomizerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain