This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-rules.git
commit 09e06c1596ca31828507e7e7f8bffff362923d41 Author: Andrei Dulvac <[email protected]> AuthorDate: Fri Jan 6 09:44:50 2017 +0000 SLING-6431 git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1777550 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 4 +-- .../testing/junit/rules/RemoteLogDumperRule.java | 29 +++++++++++++++++----- .../sling/testing/junit/rules/SlingRule.java | 22 ++++++++++++---- .../junit/rules/instance/AbstractInstance.java | 23 ++++++++++++++++- .../testing/junit/rules/instance/Instance.java | 28 +++++++++++++++++++++ 5 files changed, 92 insertions(+), 14 deletions(-) diff --git a/pom.xml b/pom.xml index 6f34639..d6360b6 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ <parent> <groupId>org.apache.sling</groupId> <artifactId>sling</artifactId> - <version>26</version> + <version>28</version> <relativePath /> </parent> @@ -87,7 +87,7 @@ </dependency> <dependency> <groupId>org.osgi</groupId> - <artifactId>org.osgi.core</artifactId> + <artifactId>osgi.core</artifactId> </dependency> <dependency> <groupId>org.apache.felix</groupId> diff --git a/src/main/java/org/apache/sling/testing/junit/rules/RemoteLogDumperRule.java b/src/main/java/org/apache/sling/testing/junit/rules/RemoteLogDumperRule.java index 23548ee..8c70329 100644 --- a/src/main/java/org/apache/sling/testing/junit/rules/RemoteLogDumperRule.java +++ b/src/main/java/org/apache/sling/testing/junit/rules/RemoteLogDumperRule.java @@ -26,6 +26,7 @@ import org.apache.sling.testing.clients.SlingClient; import org.apache.sling.testing.clients.SlingHttpResponse; import org.apache.sling.testing.clients.interceptors.TestDescriptionHolder; import org.apache.sling.testing.clients.util.URLParameterBuilder; +import org.apache.sling.testing.junit.rules.instance.Instance; import org.junit.rules.TestWatcher; import org.junit.runner.Description; import org.junit.runners.model.Statement; @@ -38,12 +39,14 @@ import static org.apache.sling.testing.clients.interceptors.TestDescriptionInter * remote server and dumps them locally upon test failure. This simplifies determining failure * cause by providing all required data locally. This would be specially useful when running test * in CI server where server logs gets cluttered with all other test executions + * Can be constructed either with a {@link SlingClient} or with a {@link Instance} * * <pre> * public class LoginTestIT { * * @Rule * public TestRule logDumper = new RemoteLogDumperRule(slingClient); + * // OR public TestRule logDumper = new RemoteLogDumperRule(myInstance); * * @Test * public void remoteLogin() { @@ -60,25 +63,35 @@ public class RemoteLogDumperRule extends TestWatcher { */ static final String SERVLET_PATH = "/system/sling/testlog"; - private SlingClient slingClient; + private SlingClient slingClient = null; + private Instance instance = null; - public RemoteLogDumperRule(){ + public RemoteLogDumperRule() { + } + public RemoteLogDumperRule (Instance instance) { + this.instance = instance; } public RemoteLogDumperRule(SlingClient slingClient) { this.slingClient = slingClient; } + public RemoteLogDumperRule setSlingClient(SlingClient slingClient) { + this.slingClient = slingClient; + return this; + } + + public RemoteLogDumperRule setInstance(Instance instance) { + this.instance = instance; + return this; + } + @Override public Statement apply(Statement base, Description description) { return super.apply(base, description); } - public void setSlingClient(SlingClient slingClient) { - this.slingClient = slingClient; - } - @Override protected void finished(Description description) { TestDescriptionHolder.removeClassName(); @@ -87,6 +100,10 @@ public class RemoteLogDumperRule extends TestWatcher { @Override protected void starting(Description description) { + // Get the client from an Instance if it was passed in this way + if (null == this.slingClient && null != instance) { + this.slingClient = instance.getAdminClient(); + } TestDescriptionHolder.setClassName(description.getClassName()); TestDescriptionHolder.setMethodName(description.getMethodName()); } diff --git a/src/main/java/org/apache/sling/testing/junit/rules/SlingRule.java b/src/main/java/org/apache/sling/testing/junit/rules/SlingRule.java index 34fd6f8..0a04aaf 100644 --- a/src/main/java/org/apache/sling/testing/junit/rules/SlingRule.java +++ b/src/main/java/org/apache/sling/testing/junit/rules/SlingRule.java @@ -17,6 +17,7 @@ package org.apache.sling.testing.junit.rules; import org.apache.sling.testing.junit.rules.category.FailingTest; +import org.apache.sling.testing.junit.rules.instance.Instance; import org.junit.rules.RuleChain; import org.junit.rules.TestRule; import org.junit.runner.Description; @@ -25,9 +26,12 @@ import org.junit.runners.model.Statement; /** * Sling Rule that wraps all the Rules (at method level) useful when running a Sling Integration Test. * Can be used in any junit test using the @Rule annotation - * I chains: {@link TestTimeout}, {@link TestDescriptionRule}, {@link TestStickyCookieRule}, {@link FilterRule} + * It chains: {@link TestTimeout}, {@link TestDescriptionRule}, {@link TestStickyCookieRule}, {@link FilterRule} */ public class SlingRule implements TestRule { + + private Instance[] instances; + /** Rule to define the max timeout for all the tests */ public final TestTimeout testTimeoutRule = new TestTimeout(); @@ -41,10 +45,18 @@ public class SlingRule implements TestRule { public final TestDescriptionRule testDescriptionRule = new TestDescriptionRule(); /** Main RuleChain describing the order of execution of all the rules */ - protected TestRule ruleChain = RuleChain.outerRule(testTimeoutRule) - .around(testStickySessionRule) - .around(filterRule) - .around(testDescriptionRule); + protected RuleChain ruleChain = RuleChain.outerRule(testTimeoutRule); + + public SlingRule(Instance... instances) { + this.instances = instances; + this.ruleChain = ruleChain.around(testStickySessionRule) + .around(filterRule) + .around(testDescriptionRule); + // Configure all rules that need an instance (where you can get a client from) + for (Instance instance : instances) { + this.ruleChain = ruleChain.around(new RemoteLogDumperRule(instance)); + } + } @Override public Statement apply(Statement base, Description description) { diff --git a/src/main/java/org/apache/sling/testing/junit/rules/instance/AbstractInstance.java b/src/main/java/org/apache/sling/testing/junit/rules/instance/AbstractInstance.java index f04d557..0dfd448 100644 --- a/src/main/java/org/apache/sling/testing/junit/rules/instance/AbstractInstance.java +++ b/src/main/java/org/apache/sling/testing/junit/rules/instance/AbstractInstance.java @@ -19,12 +19,19 @@ package org.apache.sling.testing.junit.rules.instance; import org.apache.sling.testing.clients.SlingClient; import org.apache.sling.testing.clients.instance.InstanceConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.lang.reflect.Constructor; import java.net.URI; public abstract class AbstractInstance implements Instance { + private static final Logger LOG = LoggerFactory.getLogger(AbstractInstance.class); + + /** + * {@inheritDoc} + */ @Override public <T extends SlingClient> T getClient(Class<T> clientClass, String user, String pass) { InstanceConfiguration configuration = getConfiguration(); @@ -48,9 +55,23 @@ public abstract class AbstractInstance implements Instance { return client; } + /** + * {@inheritDoc} + */ @Override public <T extends SlingClient> T getAdminClient(Class<T> clientClass) { - return getClient(clientClass, "admin", "admin"); + return getClient(clientClass, getConfiguration().getAdminUser(), getConfiguration().getAdminPassword()); } + + /** + * {@inheritDoc} + */ + @Override + public SlingClient getAdminClient() { + return getAdminClient(SlingClient.class); + } + + + } diff --git a/src/main/java/org/apache/sling/testing/junit/rules/instance/Instance.java b/src/main/java/org/apache/sling/testing/junit/rules/instance/Instance.java index 7747158..0f92ff2 100644 --- a/src/main/java/org/apache/sling/testing/junit/rules/instance/Instance.java +++ b/src/main/java/org/apache/sling/testing/junit/rules/instance/Instance.java @@ -18,6 +18,7 @@ package org.apache.sling.testing.junit.rules.instance; import org.apache.sling.testing.clients.SlingClient; import org.apache.sling.testing.clients.instance.InstanceConfiguration; +import org.apache.sling.testing.clients.instance.InstanceSetup; import org.junit.rules.TestRule; public interface Instance extends TestRule { @@ -28,8 +29,35 @@ public interface Instance extends TestRule { InstanceConfiguration getConfiguration(); + /** + * Return <strong>a new client</strong> pointing to the instance corresponding to this {{AbstractInstance}} + * + * @param clientClass the class of the returned client + * @param user the username used in the client + * @param pass the password used in the client + * @param <T> the type of the returned client + * @return a new client extending {{SlingClient}} + */ <T extends SlingClient> T getClient(Class<T> clientClass, String user, String pass); + /** + * Return <strong>a new client</strong> pointing to the instance corresponding to this {{AbstractInstance}}, + * with the admin user and password. + * See {@link InstanceSetup#INSTANCE_CONFIG_ADMINUSER} and {@link InstanceSetup#INSTANCE_CONFIG_ADMINPASSWORD} + * + * @return a new {{SlingClient}} + */ + SlingClient getAdminClient(); + + /** + * Return <strong>a new client</strong> pointing to the instance corresponding to this {{AbstractInstance}}, + * with the admin user and password. + * See {@link InstanceSetup#INSTANCE_CONFIG_ADMINUSER} and {@link InstanceSetup#INSTANCE_CONFIG_ADMINPASSWORD} + * + * @param clientClass the class of the returned client + * @param <T> the class of the returned client + * @return a new client extending on {{SlingClient}} + */ <T extends SlingClient> T getAdminClient(Class<T> clientClass); } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
