Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 97B53200BF3 for ; Thu, 5 Jan 2017 12:29:44 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 95735160B27; Thu, 5 Jan 2017 11:29:44 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id BA05C160B26 for ; Thu, 5 Jan 2017 12:29:43 +0100 (CET) Received: (qmail 66432 invoked by uid 500); 5 Jan 2017 11:29:43 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 66423 invoked by uid 99); 5 Jan 2017 11:29:42 -0000 Received: from Unknown (HELO svn01-us-west.apache.org) (209.188.14.144) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 05 Jan 2017 11:29:42 +0000 Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 7B7193A03A7 for ; Thu, 5 Jan 2017 11:29:42 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1777452 - /sling/trunk/testing/junit/rules/src/main/java/org/apache/sling/testing/junit/rules/RemoteLogDumperRule.java Date: Thu, 05 Jan 2017 11:29:42 -0000 To: commits@sling.apache.org From: chetanm@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20170105112942.7B7193A03A7@svn01-us-west.apache.org> archived-at: Thu, 05 Jan 2017 11:29:44 -0000 Author: chetanm Date: Thu Jan 5 11:29:42 2017 New Revision: 1777452 URL: http://svn.apache.org/viewvc?rev=1777452&view=rev Log: SLING-6431 - Move RemoteLogDumper to org.apache.sling.testing.rules module Adapted from org.apache.sling.testing.tools.junit.RemoteLogDumper Added: sling/trunk/testing/junit/rules/src/main/java/org/apache/sling/testing/junit/rules/RemoteLogDumperRule.java (with props) Added: sling/trunk/testing/junit/rules/src/main/java/org/apache/sling/testing/junit/rules/RemoteLogDumperRule.java URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/rules/src/main/java/org/apache/sling/testing/junit/rules/RemoteLogDumperRule.java?rev=1777452&view=auto ============================================================================== --- sling/trunk/testing/junit/rules/src/main/java/org/apache/sling/testing/junit/rules/RemoteLogDumperRule.java (added) +++ sling/trunk/testing/junit/rules/src/main/java/org/apache/sling/testing/junit/rules/RemoteLogDumperRule.java Thu Jan 5 11:29:42 2017 @@ -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 + * + *
+ *     public class LoginTestIT {
+ *
+ *     @Rule
+ *     public TestRule logDumper = new RemoteLogDumperRule(slingClient);
+ *
+ *     @Test
+ *     public void remoteLogin() {
+ *          //Make calls to remote server
+ *          assertEquals("testA", name.getMethodName());
+ *     }
+ *
+ *     }
+ * 
+ */ +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"); + } + } +} Propchange: sling/trunk/testing/junit/rules/src/main/java/org/apache/sling/testing/junit/rules/RemoteLogDumperRule.java ------------------------------------------------------------------------------ svn:eol-style = native