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 77d4e111853a1370219abd229f1a64b135b200d3 Author: Chetan Mehrotra <[email protected]> AuthorDate: Thu Jan 5 11:29:42 2017 +0000 SLING-6431 - Move RemoteLogDumper to org.apache.sling.testing.rules module Adapted from org.apache.sling.testing.tools.junit.RemoteLogDumper git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1777452 13f79535-47bb-0310-9956-ffa450edef68 --- .../testing/junit/rules/RemoteLogDumperRule.java | 125 +++++++++++++++++++++ 1 file changed, 125 insertions(+) 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 new file mode 100644 index 0000000..23548ee --- /dev/null +++ b/src/main/java/org/apache/sling/testing/junit/rules/RemoteLogDumperRule.java @@ -0,0 +1,125 @@ +/* + * 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.junit.rules; + +import java.io.PrintWriter; +import java.io.StringWriter; + +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.junit.rules.TestWatcher; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +import static org.apache.sling.testing.clients.interceptors.TestDescriptionInterceptor.TEST_CLASS_HEADER; +import static org.apache.sling.testing.clients.interceptors.TestDescriptionInterceptor.TEST_NAME_HEADER; + +/** + * The RemoteLogDumper Rule fetches logs which are generated due to execution of test from the + * 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 + * + * <pre> + * public class LoginTestIT { + * + * @Rule + * public TestRule logDumper = new RemoteLogDumperRule(slingClient); + * + * @Test + * public void remoteLogin() { + * //Make calls to remote server + * assertEquals("testA", name.getMethodName()); + * } + * + * } + * </pre> + */ +public class RemoteLogDumperRule extends TestWatcher { + /** + * Path for the org.apache.sling.junit.impl.servlet.TestLogServlet + */ + static final String SERVLET_PATH = "/system/sling/testlog"; + + private SlingClient slingClient; + + public RemoteLogDumperRule(){ + + } + + public RemoteLogDumperRule(SlingClient slingClient) { + this.slingClient = slingClient; + } + + @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(); + TestDescriptionHolder.removeMethodName(); + } + + @Override + protected void starting(Description description) { + TestDescriptionHolder.setClassName(description.getClassName()); + TestDescriptionHolder.setMethodName(description.getMethodName()); + } + + @Override + protected void failed(Throwable e, Description description) { + final StringWriter sw = new StringWriter(); + final PrintWriter pw = new PrintWriter(sw); + + if (slingClient != null) { + try { + SlingHttpResponse response = slingClient.doGet(SERVLET_PATH, URLParameterBuilder.create() + .add(TEST_CLASS_HEADER, description.getClassName()) + .add(TEST_NAME_HEADER, description.getMethodName()) + .getList(), + 200); + String msg = response.getSlingMessage(); + if (msg != null) { + pw.println(msg); + } + + pw.printf("=============== Logs from server [%s] for [%s]===================%n", + slingClient.getUrl(), description.getMethodName()); + pw.print(response.getContent()); + pw.println("========================================================"); + + System.err.print(sw.toString()); + } catch (Throwable t) { + System.err.printf("Error occurred while fetching test logs from server [%s] %n", slingClient.getUrl()); + t.printStackTrace(System.err); + } + } else { + System.err.println("No SlingClient configured with the rule"); + } + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
