This is an automated email from the ASF dual-hosted git repository. royteeuwen pushed a commit to branch feature/SLING-12582 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-junit-remote.git
commit 5d9629c6b0a80c5f6c3a7953e45c0f7d2bc59d7e Author: Roy Teeuwen <[email protected]> AuthorDate: Wed Dec 18 11:45:34 2024 +0100 SLING-12582: Upgrade to parent 62 --- pom.xml | 36 +++-- .../junit/remote/exported/ExampleRemoteTest.java | 45 +++--- .../sling/junit/remote/exported/package-info.java | 1 - .../remote/httpclient/RemoteTestHttpClient.java | 83 +++++----- .../sling/junit/remote/ide/ExecutionResult.java | 80 ++++----- .../sling/junit/remote/ide/SerializedRenderer.java | 69 ++++---- .../junit/remote/ide/SlingRemoteExecutionRule.java | 179 +++++++++++---------- .../testrunner/RemoteExecutionException.java | 36 +++-- .../junit/remote/testrunner/SlingRemoteTest.java | 37 ++--- .../testrunner/SlingRemoteTestParameters.java | 30 ++-- .../remote/testrunner/SlingRemoteTestRunner.java | 104 ++++++------ .../remote/testrunner/SlingTestsCountChecker.java | 26 +-- .../junit/remote/testrunner/package-info.java | 1 - .../testrunner/RemoteExecutionExceptionTest.java | 70 +++++--- 14 files changed, 418 insertions(+), 379 deletions(-) diff --git a/pom.xml b/pom.xml index 577d2b6..aff70ed 100644 --- a/pom.xml +++ b/pom.xml @@ -7,9 +7,9 @@ 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 @@ -23,7 +23,7 @@ <parent> <groupId>org.apache.sling</groupId> <artifactId>sling-bundle-parent</artifactId> - <version>52</version> + <version>62</version> <relativePath /> </parent> @@ -32,13 +32,17 @@ <name>Apache Sling JUnit Remote Tests Runners</name> <description>Utilities to run server-side JUnit tests remotely</description> - + <scm> <connection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-junit-remote.git</connection> <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-junit-remote.git</developerConnection> <url>https://github.com/apache/sling-org-apache-sling-junit-remote.git</url> </scm> - + + <properties> + <sling.java.version>11</sling.java.version> + </properties> + <dependencies> <dependency> <groupId>org.osgi</groupId> @@ -91,16 +95,16 @@ <scope>provided</scope> </dependency> <dependency> - <groupId>org.apache.sling</groupId> - <artifactId>org.apache.sling.jcr.contentparser</artifactId> - <version>1.2.2</version> - <scope>provided</scope> - <exclusions> - <exclusion> - <groupId>*</groupId> - <artifactId>*</artifactId> - </exclusion> - </exclusions> - </dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.jcr.contentparser</artifactId> + <version>1.2.2</version> + <scope>provided</scope> + <exclusions> + <exclusion> + <groupId>*</groupId> + <artifactId>*</artifactId> + </exclusion> + </exclusions> + </dependency> </dependencies> </project> diff --git a/src/main/java/org/apache/sling/junit/remote/exported/ExampleRemoteTest.java b/src/main/java/org/apache/sling/junit/remote/exported/ExampleRemoteTest.java index 7f160e7..ac9eaaa 100644 --- a/src/main/java/org/apache/sling/junit/remote/exported/ExampleRemoteTest.java +++ b/src/main/java/org/apache/sling/junit/remote/exported/ExampleRemoteTest.java @@ -1,23 +1,23 @@ /* - * 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 + * 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 + * 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. + * 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.junit.remote.exported; -import static org.junit.Assert.fail; - import org.apache.sling.junit.remote.ide.SlingRemoteExecutionRule; import org.apache.sling.testing.tools.http.Request; import org.junit.Rule; @@ -25,9 +25,11 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.junit.Assert.fail; + /** Test that can be run remotely on a Sling instance from an IDE, by * setting the {@link SlingRemoteExecutionRule.SLING_REMOTE_TEST_URL} - * system property in the IDE setup, to the URL of + * system property in the IDE setup, to the URL of * the Sling JUnit servlet (like http://localhost:8080/system/sling/junit) */ public class ExampleRemoteTest { @@ -43,20 +45,19 @@ public class ExampleRemoteTest { log.info("Customizing request {}", r); } }; - + @Test - public void testAlwaysPasses() { - } - + public void testAlwaysPasses() {} + @Test public void testAlwaysFails() { fail("This test always fails"); } - + @Test public void testFailsSometimes() { - if(Math.random() < 0.5) { + if (Math.random() < 0.5) { fail("This test fails sometimes"); } } -} \ No newline at end of file +} diff --git a/src/main/java/org/apache/sling/junit/remote/exported/package-info.java b/src/main/java/org/apache/sling/junit/remote/exported/package-info.java index c393216..db5440d 100644 --- a/src/main/java/org/apache/sling/junit/remote/exported/package-info.java +++ b/src/main/java/org/apache/sling/junit/remote/exported/package-info.java @@ -21,4 +21,3 @@ package org.apache.sling.junit.remote.exported; import org.osgi.annotation.versioning.Version; - diff --git a/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java b/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java index 713feeb..1c381e6 100644 --- a/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java +++ b/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java @@ -1,18 +1,20 @@ /* - * 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 + * 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 + * 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. + * 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.junit.remote.httpclient; @@ -35,10 +37,10 @@ import org.apache.sling.testing.tools.sling.SlingTestBase; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** HTTP client that executes tests remotely */ +/** HTTP client that executes tests remotely */ public class RemoteTestHttpClient { private final Logger log = LoggerFactory.getLogger(getClass()); - + private final String junitServletUrl; private final String username; private final String password; @@ -51,9 +53,9 @@ public class RemoteTestHttpClient { public RemoteTestHttpClient(String junitServletUrl, boolean consumeContent) { this(junitServletUrl, null, null, consumeContent); } - + public RemoteTestHttpClient(String junitServletUrl, String username, String password, boolean consumeContent) { - if(junitServletUrl == null) { + if (junitServletUrl == null) { throw new IllegalArgumentException("JUnit servlet URL is null, cannot run tests"); } this.junitServletUrl = junitServletUrl; @@ -71,71 +73,70 @@ public class RemoteTestHttpClient { this.password = SlingTestBase.ADMIN; } } - + public void setRequestCustomizer(RequestCustomizer c) { requestCustomizer = c; } - + public RequestExecutor runTests(String testClassesSelector, String testMethodSelector, String extension) - throws ClientProtocolException, IOException { + throws ClientProtocolException, IOException { return runTests(testClassesSelector, testMethodSelector, extension, null); } - - public RequestExecutor runTests(String testClassesSelector, String testMethodSelector, String extension, Map<String, String> requestOptions) - throws ClientProtocolException, IOException { + + public RequestExecutor runTests( + String testClassesSelector, String testMethodSelector, String extension, Map<String, String> requestOptions) + throws ClientProtocolException, IOException { final RequestBuilder builder = new RequestBuilder(junitServletUrl); // Optionally let the client to consume the response entity final RequestExecutor executor = new RequestExecutor(new DefaultHttpClient()) { @Override protected void consumeEntity() throws ParseException, IOException { - if(consumeContent) { + if (consumeContent) { super.consumeEntity(); } } }; - + // Build path for POST request to execute the tests - + // Test classes selector subpath = new StringBuilder(); - if(!junitServletUrl.endsWith(SLASH)) { + if (!junitServletUrl.endsWith(SLASH)) { subpath.append(SLASH); } subpath.append(testClassesSelector); - + // Test method selector - if(testMethodSelector != null && testMethodSelector.length() > 0) { + if (testMethodSelector != null && testMethodSelector.length() > 0) { subpath.append("/"); subpath.append(testMethodSelector); } - + // Extension - if(!extension.startsWith(DOT)) { + if (!extension.startsWith(DOT)) { subpath.append(DOT); } subpath.append(extension); // Request options if any final List<NameValuePair> opt = new ArrayList<NameValuePair>(); - if(requestOptions != null) { - for(Map.Entry<String, String> e : requestOptions.entrySet()) { + if (requestOptions != null) { + for (Map.Entry<String, String> e : requestOptions.entrySet()) { opt.add(new BasicNameValuePair(e.getKey(), e.getValue())); } } - - log.info("Executing test remotely, path={} JUnit servlet URL={}", - subpath, junitServletUrl); - final Request r = builder - .buildPostRequest(subpath.toString()) - .withCredentials(username, password) - .withCustomizer(requestCustomizer) - .withEntity(new UrlEncodedFormEntity(opt)); + + log.info("Executing test remotely, path={} JUnit servlet URL={}", subpath, junitServletUrl); + final Request r = builder.buildPostRequest(subpath.toString()) + .withCredentials(username, password) + .withCustomizer(requestCustomizer) + .withEntity(new UrlEncodedFormEntity(opt)); executor.execute(r).assertStatus(200); return executor; } - + /** If called after runTests, returns the path used to * run tests on the remote JUnit servlet */ diff --git a/src/main/java/org/apache/sling/junit/remote/ide/ExecutionResult.java b/src/main/java/org/apache/sling/junit/remote/ide/ExecutionResult.java index 19cf76d..7684d14 100644 --- a/src/main/java/org/apache/sling/junit/remote/ide/ExecutionResult.java +++ b/src/main/java/org/apache/sling/junit/remote/ide/ExecutionResult.java @@ -1,18 +1,20 @@ /* - * 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 + * 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 + * 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. + * 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.junit.remote.ide; @@ -24,30 +26,30 @@ import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class ExecutionResult implements Serializable { - private static final long serialVersionUID = 7935484811381524530L; - private final Throwable throwable; - - public ExecutionResult(Result result) { - if (result.getFailureCount() > 0) { - final List<Throwable> failures = new ArrayList<Throwable>(result.getFailureCount()); - for (Failure f : result.getFailures()) { - failures.add(f.getException()); - } - - // TODO MultipleFailureException is an internal JUnit class - - // we don't have it when running server-side in Sling - // throwable = new MultipleFailureException(failures); - throwable = failures.get(0); - } else { - throwable = null; - } - } - - public Throwable getException() { - return throwable; - } - - public boolean isFailure() { - return throwable != null; - } -} \ No newline at end of file + private static final long serialVersionUID = 7935484811381524530L; + private final Throwable throwable; + + public ExecutionResult(Result result) { + if (result.getFailureCount() > 0) { + final List<Throwable> failures = new ArrayList<Throwable>(result.getFailureCount()); + for (Failure f : result.getFailures()) { + failures.add(f.getException()); + } + + // TODO MultipleFailureException is an internal JUnit class - + // we don't have it when running server-side in Sling + // throwable = new MultipleFailureException(failures); + throwable = failures.get(0); + } else { + throwable = null; + } + } + + public Throwable getException() { + return throwable; + } + + public boolean isFailure() { + return throwable != null; + } +} diff --git a/src/main/java/org/apache/sling/junit/remote/ide/SerializedRenderer.java b/src/main/java/org/apache/sling/junit/remote/ide/SerializedRenderer.java index f5a2570..715ac47 100644 --- a/src/main/java/org/apache/sling/junit/remote/ide/SerializedRenderer.java +++ b/src/main/java/org/apache/sling/junit/remote/ide/SerializedRenderer.java @@ -1,75 +1,74 @@ /* - * 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 + * 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 + * 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. + * 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.junit.remote.ide; +import javax.servlet.http.HttpServletResponse; + import java.io.IOException; import java.io.ObjectOutputStream; import java.io.UnsupportedEncodingException; import java.util.Collection; -import javax.servlet.http.HttpServletResponse; - -import org.osgi.service.component.annotations.Component; - import org.apache.sling.junit.Renderer; import org.apache.sling.junit.RendererFactory; import org.apache.sling.junit.TestSelector; import org.junit.runner.Result; import org.junit.runner.notification.RunListener; +import org.osgi.service.component.annotations.Component; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** Renderer for Sling JUnit server-side testing, which * renders test results in binary form. * Used to send results, and especially Exceptions, as - * is to a remote IDE. + * is to a remote IDE. */ @Component( immediate = false, - service = { Renderer.class, RendererFactory.class } - ) -public class SerializedRenderer extends RunListener implements Renderer,RendererFactory { + service = {Renderer.class, RendererFactory.class}) +public class SerializedRenderer extends RunListener implements Renderer, RendererFactory { public static final String EXTENSION = "serialized"; private ObjectOutputStream outputStream; private final Logger log = LoggerFactory.getLogger(getClass()); /** @inheritDoc */ - public Renderer createRenderer() { + public Renderer createRenderer() { return new SerializedRenderer(); } - + /** @inheritDoc */ public boolean appliesTo(TestSelector s) { return EXTENSION.equals(s.getExtension()); } - + /** @inheritDoc */ public String getExtension() { return EXTENSION; } /** @inheritDoc */ - public void setup(HttpServletResponse response, String pageTitle) - throws IOException, UnsupportedEncodingException { + public void setup(HttpServletResponse response, String pageTitle) throws IOException, UnsupportedEncodingException { response.setContentType("application/x-java-serialized-object"); outputStream = new ObjectOutputStream(response.getOutputStream()); } - + /** @inheritDoc */ public void cleanup() { try { @@ -87,24 +86,20 @@ public class SerializedRenderer extends RunListener implements Renderer,Renderer } /** @inheritDoc */ - public void info(String role, String info) { - } + public void info(String role, String info) {} /** @inheritDoc */ - public void link(String info, String url, String method) { - } + public void link(String info, String url, String method) {} /** @inheritDoc */ - public void list(String role, Collection<String> data) { - } + public void list(String role, Collection<String> data) {} /** @inheritDoc */ - public void title(int level, String title) { - } - - @Override + public void title(int level, String title) {} + + @Override public void testRunFinished(Result result) throws IOException { final ExecutionResult er = new ExecutionResult(result); outputStream.writeObject(er); } -} \ No newline at end of file +} diff --git a/src/main/java/org/apache/sling/junit/remote/ide/SlingRemoteExecutionRule.java b/src/main/java/org/apache/sling/junit/remote/ide/SlingRemoteExecutionRule.java index 590ebd2..dbe4e39 100644 --- a/src/main/java/org/apache/sling/junit/remote/ide/SlingRemoteExecutionRule.java +++ b/src/main/java/org/apache/sling/junit/remote/ide/SlingRemoteExecutionRule.java @@ -1,18 +1,20 @@ /* - * 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 + * 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 + * 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. + * 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.junit.remote.ide; @@ -29,87 +31,88 @@ import org.junit.runners.model.Statement; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** JUnit MethodRule that executes tests remotely if the +/** JUnit MethodRule that executes tests remotely if the * {@link #SLING_REMOTE_TEST_URL} property is set. * Used to execute tests from an IDE and have then run - * on a remote Sling instance. + * on a remote Sling instance. */ public class SlingRemoteExecutionRule implements MethodRule, RequestCustomizer { - private static final Logger log = - LoggerFactory.getLogger(SlingRemoteExecutionRule.class); + private static final Logger log = LoggerFactory.getLogger(SlingRemoteExecutionRule.class); - /** Name of the system property that activates remote test execution */ - public static final String SLING_REMOTE_TEST_URL = "sling.remote.test.url"; - public static final String SLING_REMOTE_TEST_USERNAME = "sling.remote.test.username"; - public static final String SLING_REMOTE_TEST_PASSWORD = "sling.remote.test.password"; - - public Statement apply(final Statement base, final FrameworkMethod method, Object target) { - return new Statement() { - @Override - public void evaluate() throws Throwable { - if (tryRemoteEvaluation(method)) { - return; - } - base.evaluate(); - } - }; - } + /** Name of the system property that activates remote test execution */ + public static final String SLING_REMOTE_TEST_URL = "sling.remote.test.url"; - /** - * Execute test remotely if the corresponding system property is set - * - * @return <code>true</code> if the method was executed remotely and passed. - * If the test was <b>not</b> executed remotely then - * <code>false</code> is returned to indicate that test should be - * executed locally - */ - private boolean tryRemoteEvaluation(FrameworkMethod method) throws Throwable { - String remoteUrl = System.getProperty(SLING_REMOTE_TEST_URL); - String remoteUsername = System.getProperty(SLING_REMOTE_TEST_USERNAME); - String remotePassword = System.getProperty(SLING_REMOTE_TEST_PASSWORD); + public static final String SLING_REMOTE_TEST_USERNAME = "sling.remote.test.username"; + public static final String SLING_REMOTE_TEST_PASSWORD = "sling.remote.test.password"; - if(remoteUrl != null) { - remoteUrl = remoteUrl.trim(); - if(remoteUrl.length() > 0) { - invokeRemote(remoteUrl, remoteUsername, remotePassword, method); - return true; - } - } - return false; - } + public Statement apply(final Statement base, final FrameworkMethod method, Object target) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + if (tryRemoteEvaluation(method)) { + return; + } + base.evaluate(); + } + }; + } - private void invokeRemote(String remoteUrl, String remoteUsername, String remotePassword, FrameworkMethod method) throws Throwable { - final String testClassesSelector = method.getMethod().getDeclaringClass().getName(); - final String methodName = method.getMethod().getName(); - - final RemoteTestHttpClient testHttpClient = new RemoteTestHttpClient(remoteUrl, remoteUsername, remotePassword, false); - testHttpClient.setRequestCustomizer(this); - final RequestExecutor executor = testHttpClient.runTests( - testClassesSelector, methodName, "serialized" - ); - log.debug("Ran test {} method {} at URL {}", - new Object[] { testClassesSelector, methodName, remoteUrl }); - - final HttpEntity entity = executor.getResponse().getEntity(); - if (entity != null) { - try { - final Object o = new ObjectInputStream(entity.getContent()).readObject(); - if( !(o instanceof ExecutionResult) ) { - throw new IllegalStateException("Expected an ExecutionResult, got a " + o.getClass().getName()); - } - final ExecutionResult result = (ExecutionResult)o; - if (result.isFailure()) { - throw result.getException(); - } - } finally { - entity.consumeContent(); - } - } - } - - /** @inheritDoc */ - public void customizeRequest(Request r) { - // Do nothing by default, tests that use this rule can - // customize this method - } -} \ No newline at end of file + /** + * Execute test remotely if the corresponding system property is set + * + * @return <code>true</code> if the method was executed remotely and passed. + * If the test was <b>not</b> executed remotely then + * <code>false</code> is returned to indicate that test should be + * executed locally + */ + private boolean tryRemoteEvaluation(FrameworkMethod method) throws Throwable { + String remoteUrl = System.getProperty(SLING_REMOTE_TEST_URL); + String remoteUsername = System.getProperty(SLING_REMOTE_TEST_USERNAME); + String remotePassword = System.getProperty(SLING_REMOTE_TEST_PASSWORD); + + if (remoteUrl != null) { + remoteUrl = remoteUrl.trim(); + if (remoteUrl.length() > 0) { + invokeRemote(remoteUrl, remoteUsername, remotePassword, method); + return true; + } + } + return false; + } + + private void invokeRemote(String remoteUrl, String remoteUsername, String remotePassword, FrameworkMethod method) + throws Throwable { + final String testClassesSelector = + method.getMethod().getDeclaringClass().getName(); + final String methodName = method.getMethod().getName(); + + final RemoteTestHttpClient testHttpClient = + new RemoteTestHttpClient(remoteUrl, remoteUsername, remotePassword, false); + testHttpClient.setRequestCustomizer(this); + final RequestExecutor executor = testHttpClient.runTests(testClassesSelector, methodName, "serialized"); + log.debug("Ran test {} method {} at URL {}", new Object[] {testClassesSelector, methodName, remoteUrl}); + + final HttpEntity entity = executor.getResponse().getEntity(); + if (entity != null) { + try { + final Object o = new ObjectInputStream(entity.getContent()).readObject(); + if (!(o instanceof ExecutionResult)) { + throw new IllegalStateException( + "Expected an ExecutionResult, got a " + o.getClass().getName()); + } + final ExecutionResult result = (ExecutionResult) o; + if (result.isFailure()) { + throw result.getException(); + } + } finally { + entity.consumeContent(); + } + } + } + + /** @inheritDoc */ + public void customizeRequest(Request r) { + // Do nothing by default, tests that use this rule can + // customize this method + } +} diff --git a/src/main/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionException.java b/src/main/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionException.java index 5c60e9f..36caade 100644 --- a/src/main/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionException.java +++ b/src/main/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionException.java @@ -1,18 +1,20 @@ /* - * 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 + * 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 + * 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. + * 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.junit.remote.testrunner; @@ -35,8 +37,8 @@ public class RemoteExecutionException extends RuntimeException { * Matches on lines in the format {@code at package.class.method(source.java:123)} with 4 (1,2,3,4) or * {@code at package.class.method(Native method)} with 3 different groups (1,2,5) */ - private static final Pattern TRACE_PATTERN = Pattern - .compile("\\s*at\\s+([\\w\\.$_]+)\\.([\\w$_]+)(?:\\((.*\\.java):(\\d+)\\)|(\\(.*\\)))"); + private static final Pattern TRACE_PATTERN = + Pattern.compile("\\s*at\\s+([\\w\\.$_]+)\\.([\\w$_]+)(?:\\((.*\\.java):(\\d+)\\)|(\\(.*\\)))"); /** * Matches on lines in the format {@code Caused by: java.io.IOException: Some message} with 1 group containing the * part after the first colon @@ -69,7 +71,7 @@ public class RemoteExecutionException extends RuntimeException { s.print(trace); } } - + @Override public void printStackTrace(PrintWriter s) { if (trace != null) { @@ -85,7 +87,7 @@ public class RemoteExecutionException extends RuntimeException { /** * Returns all StackTraceElement created from the given String. Also evaluates the cause of an exception by setting {@link #initCause(Throwable)}. * Example format for given trace: - * + * * <pre> * {@code * java.lang.RuntimeException: Wrapper exception @@ -100,7 +102,7 @@ public class RemoteExecutionException extends RuntimeException { * ... 23 more * } * </pre> - * + * * @param trace a serialized stack trace. * @return an array of {@link StackTraceElement}s. * @throws IOException diff --git a/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java b/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java index a2e67ff..060fe3d 100644 --- a/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java +++ b/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java @@ -1,18 +1,20 @@ /* - * 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 + * 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 + * 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. + * 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.junit.remote.testrunner; @@ -20,7 +22,6 @@ import java.io.IOException; import jakarta.json.JsonException; import jakarta.json.JsonObject; - import org.junit.runner.Description; /** Info about a remote tests, as provided by the Sling JUnit servlet */ @@ -29,11 +30,11 @@ class SlingRemoteTest { private final String description; private final String failure; private final String trace; - + public static final String DESCRIPTION = "description"; public static final String FAILURE = "failure"; public static final String TRACE = "trace"; - + SlingRemoteTest(Class<?> testClass, JsonObject json) throws JsonException { this.testClass = testClass; description = json.containsKey(DESCRIPTION) ? json.getString(DESCRIPTION) : null; @@ -44,13 +45,13 @@ class SlingRemoteTest { trace = null; } } - + Description describe() { return Description.createTestDescription(testClass, description); } - + void run() { - if(failure != null && failure.trim().length() > 0) { + if (failure != null && failure.trim().length() > 0) { try { throw new RemoteExecutionException(failure, trace); } catch (NumberFormatException e) { diff --git a/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestParameters.java b/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestParameters.java index 7dd7944..c895f88 100644 --- a/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestParameters.java +++ b/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestParameters.java @@ -1,18 +1,20 @@ /* - * 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 + * 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 + * 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. + * 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.junit.remote.testrunner; @@ -22,12 +24,12 @@ package org.apache.sling.junit.remote.testrunner; public interface SlingRemoteTestParameters { /** Return the URL of the JUnit servlet */ String getJunitServletUrl(); - + /** Return the optional selector for the test classes to run, * for example "org.apache.sling.testing.samples.sampletests.JUnit4Test" */ String getTestClassesSelector(); - + /** Return the optional selector for the test methods to run, * for example "someMethodName" */ diff --git a/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java b/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java index 7c64115..f32b0e8 100644 --- a/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java +++ b/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java @@ -1,18 +1,20 @@ /* - * 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 + * 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 + * 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. + * 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.junit.remote.testrunner; @@ -23,7 +25,6 @@ import java.util.List; import jakarta.json.Json; import jakarta.json.JsonArray; import jakarta.json.JsonObject; - import org.apache.sling.jcr.contentparser.impl.JsonTicksConverter; import org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient; import org.apache.sling.testing.tools.http.RequestCustomizer; @@ -57,24 +58,24 @@ public class SlingRemoteTestRunner extends ParentRunner<SlingRemoteTest> { private final String username; private final String password; private final Class<?> testClass; - + private final List<SlingRemoteTest> children = new LinkedList<SlingRemoteTest>(); - + public SlingRemoteTestRunner(Class<?> testClass) throws InitializationError { super(testClass); this.testClass = testClass; - + Object o = null; try { o = testClass.newInstance(); - if( !(o instanceof SlingRemoteTestParameters)) { - throw new IllegalArgumentException(o.getClass().getName() - + " is not a " + SlingRemoteTestParameters.class.getSimpleName()); + if (!(o instanceof SlingRemoteTestParameters)) { + throw new IllegalArgumentException( + o.getClass().getName() + " is not a " + SlingRemoteTestParameters.class.getSimpleName()); } - } catch(Exception e) { + } catch (Exception e) { throw new InitializationError(e); } - + // Set configured username using "admin" as default credential final String configuredUsername = System.getProperty(SlingTestBase.TEST_SERVER_USERNAME); if (configuredUsername != null && configuredUsername.trim().length() > 0) { @@ -90,51 +91,52 @@ public class SlingRemoteTestRunner extends ParentRunner<SlingRemoteTest> { } else { password = SlingTestBase.ADMIN; } - - testParameters = (SlingRemoteTestParameters)o; + + testParameters = (SlingRemoteTestParameters) o; } - + private void maybeExecuteTests() throws Exception { - if(testHttpClient != null) { + if (testHttpClient != null) { // Tests already ran return; } - - testHttpClient = new RemoteTestHttpClient(testParameters.getJunitServletUrl(), this.username, this.password, true); - // Let the parameters class customize the request if desired - if(testParameters instanceof RequestCustomizer) { - testHttpClient.setRequestCustomizer((RequestCustomizer)testParameters); + testHttpClient = + new RemoteTestHttpClient(testParameters.getJunitServletUrl(), this.username, this.password, true); + + // Let the parameters class customize the request if desired + if (testParameters instanceof RequestCustomizer) { + testHttpClient.setRequestCustomizer((RequestCustomizer) testParameters); } - + // Run tests remotely and get response final RequestExecutor executor = testHttpClient.runTests( - testParameters.getTestClassesSelector(), - testParameters.getTestMethodSelector(), - "json" - ); + testParameters.getTestClassesSelector(), testParameters.getTestMethodSelector(), "json"); executor.assertContentType("application/json"); - final JsonArray json = Json.createReader(new StringReader(JsonTicksConverter.tickToDoubleQuote(executor.getContent()))).readArray(); + final JsonArray json = Json.createReader( + new StringReader(JsonTicksConverter.tickToDoubleQuote(executor.getContent()))) + .readArray(); - // Response contains an array of objects identified by + // Response contains an array of objects identified by // their INFO_TYPE, extract the tests // based on this vlaue - for(int i = 0 ; i < json.size(); i++) { + for (int i = 0; i < json.size(); i++) { final JsonObject obj = json.getJsonObject(i); - if(obj.containsKey("INFO_TYPE") && "test".equals(obj.getString("INFO_TYPE"))) { + if (obj.containsKey("INFO_TYPE") && "test".equals(obj.getString("INFO_TYPE"))) { children.add(new SlingRemoteTest(testClass, obj)); } } - - log.info("Server-side tests executed as {} at {} with path {}", - new Object[]{this.username, testParameters.getJunitServletUrl(), testHttpClient.getTestExecutionPath()}); - + + log.info("Server-side tests executed as {} at {} with path {}", new Object[] { + this.username, testParameters.getJunitServletUrl(), testHttpClient.getTestExecutionPath() + }); + // Optionally check that number of tests is as expected - if(testParameters instanceof SlingTestsCountChecker) { - ((SlingTestsCountChecker)testParameters).checkNumberOfTests(children.size()); + if (testParameters instanceof SlingTestsCountChecker) { + ((SlingTestsCountChecker) testParameters).checkNumberOfTests(children.size()); } } - + @Override protected Description describeChild(SlingRemoteTest t) { return t.describe(); @@ -144,7 +146,7 @@ public class SlingRemoteTestRunner extends ParentRunner<SlingRemoteTest> { protected List<SlingRemoteTest> getChildren() { try { maybeExecuteTests(); - } catch(Exception e) { + } catch (Exception e) { throw new Error(e); } return children; @@ -154,11 +156,11 @@ public class SlingRemoteTestRunner extends ParentRunner<SlingRemoteTest> { protected void runChild(SlingRemoteTest t, RunNotifier notifier) { try { maybeExecuteTests(); - } catch(Exception e) { + } catch (Exception e) { throw new Error(e); } - - EachTestNotifier eachNotifier= new EachTestNotifier(notifier, t.describe()); + + EachTestNotifier eachNotifier = new EachTestNotifier(notifier, t.describe()); eachNotifier.fireTestStarted(); try { log.debug("Running test {}", t.describe()); @@ -184,7 +186,7 @@ public class SlingRemoteTestRunner extends ParentRunner<SlingRemoteTest> { statement = withAfterClasses(statement); return statement; } - + /** * Returns a {@link Statement}: run all non-overridden {@code @After} methods on this class and superclasses after * executing {@code statement}; all After methods are always executed: exceptions thrown by previous steps are diff --git a/src/main/java/org/apache/sling/junit/remote/testrunner/SlingTestsCountChecker.java b/src/main/java/org/apache/sling/junit/remote/testrunner/SlingTestsCountChecker.java index 31bdb37..1961c16 100644 --- a/src/main/java/org/apache/sling/junit/remote/testrunner/SlingTestsCountChecker.java +++ b/src/main/java/org/apache/sling/junit/remote/testrunner/SlingTestsCountChecker.java @@ -1,18 +1,20 @@ /* - * 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 + * 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 + * 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. + * 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.junit.remote.testrunner; diff --git a/src/main/java/org/apache/sling/junit/remote/testrunner/package-info.java b/src/main/java/org/apache/sling/junit/remote/testrunner/package-info.java index ad7c15f..28a2da6 100644 --- a/src/main/java/org/apache/sling/junit/remote/testrunner/package-info.java +++ b/src/main/java/org/apache/sling/junit/remote/testrunner/package-info.java @@ -21,4 +21,3 @@ package org.apache.sling.junit.remote.testrunner; import org.osgi.annotation.versioning.Version; - diff --git a/src/test/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionExceptionTest.java b/src/test/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionExceptionTest.java index e976f39..1dca6c5 100644 --- a/src/test/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionExceptionTest.java +++ b/src/test/java/org/apache/sling/junit/remote/testrunner/RemoteExecutionExceptionTest.java @@ -1,18 +1,20 @@ /* - * 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 + * 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 + * 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. + * 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.junit.remote.testrunner; @@ -24,7 +26,6 @@ import java.util.List; import org.hamcrest.Matchers; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; public class RemoteExecutionExceptionTest { @@ -34,15 +35,22 @@ public class RemoteExecutionExceptionTest { String trace = null; try { throw new IllegalStateException("Some message"); - } catch(Exception e) { + } catch (Exception e) { StringWriter writer = new StringWriter(); e.printStackTrace(new PrintWriter(writer)); trace = writer.toString(); } RemoteExecutionException e = RemoteExecutionException.getExceptionFromTrace(trace); Assert.assertThat(e.getMessage(), Matchers.equalTo("java.lang.IllegalStateException: Some message")); - List<StackTraceElement> stackTraceElements = Arrays.asList(new RemoteExecutionException("some failure", trace).getStackTrace()); - Assert.assertThat(stackTraceElements, Matchers.hasItem(new StackTraceElement("org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest", "testGetStackTraceFromString", "RemoteExecutionExceptionTest.java", 36))); + List<StackTraceElement> stackTraceElements = + Arrays.asList(new RemoteExecutionException("some failure", trace).getStackTrace()); + Assert.assertThat( + stackTraceElements, + Matchers.hasItem(new StackTraceElement( + "org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest", + "testGetStackTraceFromString", + "RemoteExecutionExceptionTest.java", + 37))); // compare original stacktrace with newly generated one from the exception StringWriter writer = new StringWriter(); e.printStackTrace(new PrintWriter(writer)); @@ -56,7 +64,7 @@ public class RemoteExecutionExceptionTest { try { try { throw new IllegalStateException("Some message"); - } catch(Exception e) { + } catch (Exception e) { throw new RuntimeException("Wrapper exception", e); } } catch (Exception e) { @@ -64,18 +72,36 @@ public class RemoteExecutionExceptionTest { e.printStackTrace(new PrintWriter(writer)); trace = writer.toString(); } - + RemoteExecutionException e = RemoteExecutionException.getExceptionFromTrace(trace); Assert.assertThat(e.getMessage(), Matchers.equalTo("java.lang.RuntimeException: Wrapper exception")); List<StackTraceElement> stackTraceElements = Arrays.asList(e.getStackTrace()); - Assert.assertThat(stackTraceElements, Matchers.hasItem(new StackTraceElement("org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest", "testGetStackTraceFromStringWithNestedException", "RemoteExecutionExceptionTest.java", 60))); + Assert.assertThat( + stackTraceElements, + Matchers.hasItem(new StackTraceElement( + "org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest", + "testGetStackTraceFromStringWithNestedException", + "RemoteExecutionExceptionTest.java", + 68))); // no original exception in the stack trace - Assert.assertThat(stackTraceElements, Matchers.not(Matchers.hasItem(new StackTraceElement("org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest", "testGetStackTraceFromStringWithNestedException", "RemoteExecutionExceptionTest.java", 58)))); - + Assert.assertThat( + stackTraceElements, + Matchers.not(Matchers.hasItem(new StackTraceElement( + "org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest", + "testGetStackTraceFromStringWithNestedException", + "RemoteExecutionExceptionTest.java", + 66)))); + // cause must be set Assert.assertNotNull("Cause must be set on the exception", e.getCause()); Assert.assertThat(e.getCause().getMessage(), Matchers.equalTo("java.lang.IllegalStateException: Some message")); stackTraceElements = Arrays.asList(e.getCause().getStackTrace()); - Assert.assertThat(stackTraceElements, Matchers.hasItem(new StackTraceElement("org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest", "testGetStackTraceFromStringWithNestedException", "RemoteExecutionExceptionTest.java", 58))); + Assert.assertThat( + stackTraceElements, + Matchers.hasItem(new StackTraceElement( + "org.apache.sling.junit.remote.testrunner.RemoteExecutionExceptionTest", + "testGetStackTraceFromStringWithNestedException", + "RemoteExecutionExceptionTest.java", + 66))); } }
