You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2013/08/08 12:22:25 UTC
svn commit: r1511673 - in /sling/trunk/testing/tools: ./
src/main/java/org/apache/sling/testing/tools/junit/
Author: bdelacretaz
Date: Thu Aug 8 10:22:25 2013
New Revision: 1511673
URL: http://svn.apache.org/r1511673
Log:
SLING-3000 - Filter that logs test class info, if set in request headers. Contributed by Chetan Mehrotra, thanks!
Added:
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestDescriptionInterceptor.java
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestDescriptionRule.java
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestNameLoggingFilter.java
Modified:
sling/trunk/testing/tools/pom.xml
Modified: sling/trunk/testing/tools/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/pom.xml?rev=1511673&r1=1511672&r2=1511673&view=diff
==============================================================================
--- sling/trunk/testing/tools/pom.xml (original)
+++ sling/trunk/testing/tools/pom.xml Thu Aug 8 10:22:25 2013
@@ -44,7 +44,11 @@
<build>
<plugins>
- <plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-scr-plugin</artifactId>
+ </plugin>
+ <plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
@@ -68,6 +72,11 @@
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.scr.annotations</artifactId>
+ <scope>provided</scope>
+ </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
@@ -94,6 +103,11 @@
<version>2.0.6</version>
</dependency>
<dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.11</version>
@@ -107,7 +121,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
- <version>4.8.2</version>
+ <version>4.10</version>
<scope>compile</scope>
</dependency>
</dependencies>
Added: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestDescriptionInterceptor.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestDescriptionInterceptor.java?rev=1511673&view=auto
==============================================================================
--- sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestDescriptionInterceptor.java (added)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestDescriptionInterceptor.java Thu Aug 8 10:22:25 2013
@@ -0,0 +1,46 @@
+/*
+ * 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.tools.junit;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpRequestInterceptor;
+import org.apache.http.protocol.HttpContext;
+import org.junit.runner.Description;
+
+import java.io.IOException;
+
+/**
+ * HttpClient interceptor that propagates the current test name as part HTTP request headers.
+ * Headers can then be logged, exported as MDC info etc. by {@link TestNameLoggingFilter}.
+ *
+ * Meant to help in correlating the server side logs with the test case being executed.
+ *
+ * @see MDC http://www.slf4j.org/manual.html
+ */
+public class TestDescriptionInterceptor implements HttpRequestInterceptor{
+ public static final String TEST_NAME_HEADER = "sling.test.name";
+ public static final String TEST_CLASS_HEADER = "sling.test.class";
+
+ public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
+ final Description desc = TestDescriptionRule.getCurrentTestDescription();
+ if(desc != null){
+ httpRequest.addHeader(TEST_NAME_HEADER,desc.getMethodName());
+ httpRequest.addHeader(TEST_CLASS_HEADER,desc.getClassName());
+ }
+ }
+}
\ No newline at end of file
Added: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestDescriptionRule.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestDescriptionRule.java?rev=1511673&view=auto
==============================================================================
--- sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestDescriptionRule.java (added)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestDescriptionRule.java Thu Aug 8 10:22:25 2013
@@ -0,0 +1,42 @@
+/*
+ * 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.tools.junit;
+
+import org.junit.rules.TestWatcher;
+import org.junit.runner.Description;
+
+/**
+ * Junit rule which exposes the current executing test's description as a thread local instance
+ */
+public class TestDescriptionRule extends TestWatcher {
+
+ private static final ThreadLocal<Description> currentTestDescription = new ThreadLocal<Description>();
+
+ @Override
+ protected void finished(Description description) {
+ currentTestDescription.set(description);
+ }
+
+ @Override
+ protected void starting(Description description) {
+ currentTestDescription.remove();
+ }
+
+ public static Description getCurrentTestDescription(){
+ return currentTestDescription.get();
+ }
+}
Added: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestNameLoggingFilter.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestNameLoggingFilter.java?rev=1511673&view=auto
==============================================================================
--- sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestNameLoggingFilter.java (added)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/junit/TestNameLoggingFilter.java Thu Aug 8 10:22:25 2013
@@ -0,0 +1,84 @@
+/*
+ * 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.tools.junit;
+
+import static org.apache.sling.testing.tools.junit.TestDescriptionInterceptor.TEST_CLASS_HEADER;
+import static org.apache.sling.testing.tools.junit.TestDescriptionInterceptor.TEST_NAME_HEADER;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
+
+/**
+ * Filter which logs the test class and name being executed, if set in
+ * request headers.
+ */
+@Component
+@Service
+@Property(name = "pattern",value="/*")
+public class TestNameLoggingFilter implements Filter{
+
+ private Logger log = LoggerFactory.getLogger(getClass());
+
+ public void init(FilterConfig filterConfig) throws ServletException {
+ // nothing to init
+ }
+
+ public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
+ throws IOException, ServletException {
+ final HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
+ final String className = httpRequest.getHeader(TEST_CLASS_HEADER);
+
+ if(className == null) {
+ filterChain.doFilter(servletRequest,servletResponse);
+ return;
+ }
+
+ final String testName = httpRequest.getHeader(TEST_NAME_HEADER);
+ try {
+ MDC.put(TEST_NAME_HEADER,testName);
+ MDC.put(TEST_CLASS_HEADER,className);
+
+ log.info("Starting request as part of test ==== {}.{} ====",className,testName);
+
+ filterChain.doFilter(servletRequest,servletResponse);
+
+ } finally {
+ log.info("Finishing request as part of test ==== {}.{} ====",className,testName);
+
+ MDC.remove(TEST_NAME_HEADER);
+ MDC.remove(TEST_CLASS_HEADER);
+ }
+ }
+
+ public void destroy() {
+ // nothing to destroy
+ }
+}