This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.junit.healthcheck-1.0.4 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-junit-healthcheck.git
commit 226355ade941f9cd5d52be89939cc2359dab815b Author: Bertrand Delacretaz <bdelacre...@apache.org> AuthorDate: Tue Feb 22 16:56:19 2011 +0000 SLING-1963 - remote tests runner, work in progress git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/junit/remote@1073404 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 70 ++++++++++ .../junit/remote/testrunner/SlingRemoteTest.java | 47 +++++++ .../testrunner/SlingRemoteTestParameters.java | 26 ++++ .../remote/testrunner/SlingRemoteTestRunner.java | 152 +++++++++++++++++++++ 4 files changed, 295 insertions(+) diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..78a3a3a --- /dev/null +++ b/pom.xml @@ -0,0 +1,70 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.sling</groupId> + <artifactId>sling</artifactId> + <version>10</version> + </parent> + + <artifactId>org.apache.sling.junit.remote</artifactId> + <version>0.1.1-SNAPSHOT</version> + <packaging>jar</packaging> + + <name>Apache Sling JUnit Remote Extension</name> + <description>Runs JUnit tests remotely</description> + + <scm> + <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/testing/junit/remote</connection> + <developerConnection> scm:svn:https://svn.apache.org/repos/asf/sling/trunk/testing/junit/remote</developerConnection> + <url>http://svn.apache.org/viewvc/sling/trunk/testing/junit/remote</url> + </scm> + + <dependencies> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.junit.core</artifactId> + <version>0.1.1-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.8.2</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>1.5.11</version> + </dependency> + <dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.commons.testing.http</artifactId> + <version>0.9-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.commons.json</artifactId> + <version>2.0.6</version> + </dependency> + </dependencies> +</project> 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 new file mode 100644 index 0000000..5171474 --- /dev/null +++ b/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTest.java @@ -0,0 +1,47 @@ +/* + * 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.junit.remote.testrunner; + +import org.apache.sling.commons.json.JSONException; +import org.apache.sling.commons.json.JSONObject; +import org.junit.runner.Description; + +/** Info about a remote tests, as provided by the Sling JUnit servlet */ +class SlingRemoteTest { + private final Class<?> testClass; + private final String description; + private final String failure; + + public static final String DESCRIPTION = "description"; + public static final String FAILURE = "failure"; + + SlingRemoteTest(Class<?> testClass, JSONObject json) throws JSONException { + this.testClass = testClass; + description = json.getString(DESCRIPTION); + failure = json.has(FAILURE) ? json.getString(FAILURE) : null; + } + + Description describe() { + return Description.createTestDescription(testClass, description); + } + + void run() { + if(failure != null && failure.trim().length() > 0) { + throw new AssertionError(failure); + } + } +} 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 new file mode 100644 index 0000000..84e32af --- /dev/null +++ b/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestParameters.java @@ -0,0 +1,26 @@ +/* + * 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.junit.remote.testrunner; + +/** Test class "proxies" implement this to indicate where to + * run the tests. + */ +public interface SlingRemoteTestParameters { + int getExpectedNumberOfTests(); + String getServerBaseUrl(); + String getJunitServletPath(); +} 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 new file mode 100644 index 0000000..6d5010b --- /dev/null +++ b/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java @@ -0,0 +1,152 @@ +/* + * 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.junit.remote.testrunner; + +import static org.junit.Assert.assertEquals; + +import java.util.LinkedList; +import java.util.List; + +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.sling.commons.json.JSONArray; +import org.apache.sling.commons.json.JSONObject; +import org.apache.sling.commons.json.JSONTokener; +import org.apache.stanbol.commons.testing.http.Request; +import org.apache.stanbol.commons.testing.http.RequestBuilder; +import org.apache.stanbol.commons.testing.http.RequestExecutor; +import org.junit.internal.AssumptionViolatedException; +import org.junit.internal.runners.model.EachTestNotifier; +import org.junit.runner.Description; +import org.junit.runner.notification.RunNotifier; +import org.junit.runners.ParentRunner; +import org.junit.runners.model.InitializationError; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** JUnit TestRunner that talks to a remote + * Sling JUnit test servlet. Using this test + * runner lets a test class discover tests + * that the JUnit servlet can execute, execute + * them and report results exactly as if the tests + * ran locally. + */ +public class SlingRemoteTestRunner extends ParentRunner<SlingRemoteTest> { + private static final Logger log = LoggerFactory.getLogger(SlingRemoteTestRunner.class); + private final SlingRemoteTestParameters testParameters; + private RequestExecutor executor; + private RequestBuilder builder; + 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()); + } + } catch(Exception e) { + throw new InitializationError(e); + } + + testParameters = (SlingRemoteTestParameters)o; + } + + private void maybeExecuteTests() throws Exception { + if(executor != null) { + return; + } + + // Setup request execution + executor = new RequestExecutor(new DefaultHttpClient()); + if(testParameters.getServerBaseUrl() == null) { + throw new IllegalStateException("Server base URL is null, cannot run tests"); + } + builder = new RequestBuilder(testParameters.getServerBaseUrl()); + + // TODO for now we run tests at the same time as we count them, + // as the junit servlet uses only GET - need to change that to GET and POST + final Request r = builder.buildGetRequest(testParameters.getJunitServletPath() + "/.json"); + + // Get list of tests in JSON format + executor.execute(r) + .assertStatus(200) + .assertContentType("application/json"); + + final JSONArray json = new JSONArray(new JSONTokener((executor.getContent()))); + + // Response contains an array of objects identified by + // their INFO_TYPE, extract the tests + // based on this vlaue + for(int i = 0 ; i < json.length(); i++) { + final JSONObject obj = json.getJSONObject(i); + if("test".equals(obj.getString("INFO_TYPE"))) { + children.add(new SlingRemoteTest(testClass, obj)); + } + } + + log.info("{} server-side tests executed at {}", + children.size(), testParameters.getJunitServletPath()); + + // Check that number of tests is as expected + assertEquals("Expecting " + testParameters.getExpectedNumberOfTests() + " tests", + testParameters.getExpectedNumberOfTests(), + children.size()); + } + + @Override + protected Description describeChild(SlingRemoteTest t) { + return t.describe(); + } + + @Override + protected List<SlingRemoteTest> getChildren() { + try { + maybeExecuteTests(); + } catch(Exception e) { + throw new Error(e); + } + return children; + } + + @Override + protected void runChild(SlingRemoteTest t, RunNotifier notifier) { + try { + maybeExecuteTests(); + } catch(Exception e) { + throw new Error(e); + } + + EachTestNotifier eachNotifier= new EachTestNotifier(notifier, t.describe()); + eachNotifier.fireTestStarted(); + try { + log.debug("Running test {}", t.describe()); + t.run(); + } catch (AssumptionViolatedException e) { + eachNotifier.addFailedAssumption(e); + } catch (Throwable e) { + eachNotifier.addFailure(e); + } finally { + eachNotifier.fireTestFinished(); + } + } +} \ No newline at end of file -- To stop receiving notification emails like this one, please contact "commits@sling.apache.org" <commits@sling.apache.org>.