This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.hc.junit.bridge-1.0.2 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-hc-junit-bridge.git
commit 9c9e4018571e09eea93c1406250d33be02a0d585 Author: Bertrand Delacretaz <[email protected]> AuthorDate: Mon Jul 14 14:47:37 2014 +0000 SLING-3771 - move bridge module from whiteboard to extensions git-svn-id: https://svn.apache.org/repos/asf/sling/whiteboard/bdelacretaz/junit-bridge@1610419 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 99 ++++++++++++++++++++++ .../sling/hc/junitbridge/HealthCheckTest.java | 66 +++++++++++++++ .../hc/junitbridge/HealthCheckTestsProvider.java | 73 ++++++++++++++++ .../sling/hc/junitbridge/JUnitTestBridge.java | 44 ++++++++++ .../sling/hc/junitbridge/TestBridgeContext.java | 38 +++++++++ 5 files changed, 320 insertions(+) diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..04440b9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,99 @@ +<?xml version="1.0" encoding="UTF-8"?> +<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>19</version> + <relativePath>../../../../parent/pom.xml</relativePath> + </parent> + + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.hc.junit.bridge</artifactId> + <packaging>bundle</packaging> + <version>0.9.9-SNAPSHOT</version> + + <name>Apache Sling Health Check JUnit Bridge</name> + <inceptionYear>2013</inceptionYear> + + <description> + Makes Sling Health Checks available as server-side JUnit tests, to + allow them to be used as part of integration tests. + </description> + + <properties> + <sling.java.version>6</sling.java.version> + </properties> + + <scm> + <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/healthcheck/junit-bridge</connection> + <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/healthcheck/junit-bridge</developerConnection> + <url>http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/junit-bridge</url> + </scm> + + <build> + <plugins> + <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> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.compendium</artifactId> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>1.6.2</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <version>1.6.2</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.commons.osgi</artifactId> + <version>2.2.0</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.hc.core</artifactId> + <version>1.1.0</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.junit.core</artifactId> + <version>1.0.6</version> + <scope>provided</scope> + </dependency> + </dependencies> +</project> diff --git a/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTest.java b/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTest.java new file mode 100644 index 0000000..8c5f36b --- /dev/null +++ b/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTest.java @@ -0,0 +1,66 @@ +/* + * 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 SF 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.hc.junitbridge; + +import junit.framework.TestCase; + +import org.apache.sling.hc.api.HealthCheck; +import org.apache.sling.hc.api.Result; +import org.apache.sling.hc.api.ResultLog; + +public class HealthCheckTest extends TestCase { + private final HealthCheck hc; + private final String name; + + HealthCheckTest(HealthCheck hc) { + super("testHealthCheck"); + this.hc = hc; + + // TODO HC name? see webconsole + name = hc.getClass().getName(); + } + + @Override + public String getName() { + return name; + } + + /** Execute our health check and dump its log + * messages > INFO if it fails */ + public void testHealthCheck() { + final Result r = hc.execute(); + final StringBuilder failMsg = new StringBuilder(); + if(!r.isOk()) { + failMsg.append(name); + failMsg.append("\n"); + for(ResultLog.Entry log : r) { + if(failMsg.length() > 0) { + failMsg.append("\n"); + } + if(log.getStatus().compareTo(Result.Status.INFO) > 0) { + failMsg.append(log.getStatus().toString()); + failMsg.append(" - "); + failMsg.append(log.getMessage()); + } + } + } + if(failMsg.length() > 0) { + fail("Health Check failed: " + failMsg.toString()); + } + } +} \ No newline at end of file diff --git a/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java b/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java new file mode 100644 index 0000000..95849c4 --- /dev/null +++ b/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java @@ -0,0 +1,73 @@ +/* + * 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 SF 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.hc.junitbridge; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Service; +import org.apache.sling.hc.util.HealthCheckFilter; +import org.apache.sling.junit.TestsProvider; +import org.osgi.framework.Constants; +import org.osgi.service.component.ComponentContext; + +/** Bridge Health Checks into the Sling JUnit server-side test + * framework, based on their tags. + */ +@Component +@Service +public class HealthCheckTestsProvider implements TestsProvider { + + private String servicePid; + + // TODO configurable + private String [] tags = { "script" }; + private HealthCheckFilter filter; + + @Activate + protected void activate(ComponentContext ctx) { + servicePid = (String)ctx.getProperties().get(Constants.SERVICE_PID); + filter = new HealthCheckFilter(ctx.getBundleContext()); + } + + @Override + public Class<?> createTestClass(String testName) throws ClassNotFoundException { + JUnitTestBridge.setContext(new TestBridgeContext(filter, tags)); + return JUnitTestBridge.class; + } + + @Override + public String getServicePid() { + return servicePid; + } + + @Override + public List<String> getTestNames() { + final List<String> result = new ArrayList<String>(); + // TODO use a configurable name? + result.add("HealthChecks(sling,slow)"); + return result; + } + + @Override + public long lastModified() { + return 0; + } +} \ No newline at end of file diff --git a/src/main/java/org/apache/sling/hc/junitbridge/JUnitTestBridge.java b/src/main/java/org/apache/sling/hc/junitbridge/JUnitTestBridge.java new file mode 100644 index 0000000..51edad7 --- /dev/null +++ b/src/main/java/org/apache/sling/hc/junitbridge/JUnitTestBridge.java @@ -0,0 +1,44 @@ +/* + * 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 SF 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.hc.junitbridge; + +import static org.junit.Assert.assertNotNull; +import junit.framework.TestSuite; + +import org.apache.sling.hc.api.HealthCheck; +import org.junit.runner.RunWith; +import org.junit.runners.AllTests; + +@RunWith(AllTests.class) +public class JUnitTestBridge { + private static ThreadLocal<TestBridgeContext> testContext = new ThreadLocal<TestBridgeContext>(); + + static void setContext(TestBridgeContext c) { + testContext.set(c); + } + + public static junit.framework.Test suite() { + final TestBridgeContext context = testContext.get(); + assertNotNull("Expecting non-null TestBridgeContext, via ThreadLocal", context); + TestSuite suite = new TestSuite(); + for(HealthCheck hc : context.getFilter().getTaggedHealthChecks(context.getTags())) { + suite.addTest(new HealthCheckTest(hc)); + } + return suite; + } +} \ No newline at end of file diff --git a/src/main/java/org/apache/sling/hc/junitbridge/TestBridgeContext.java b/src/main/java/org/apache/sling/hc/junitbridge/TestBridgeContext.java new file mode 100644 index 0000000..4072c50 --- /dev/null +++ b/src/main/java/org/apache/sling/hc/junitbridge/TestBridgeContext.java @@ -0,0 +1,38 @@ +/* + * 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 SF 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.hc.junitbridge; + +import org.apache.sling.hc.util.HealthCheckFilter; + +class TestBridgeContext { + private final String [] tags; + private final HealthCheckFilter filter; + + TestBridgeContext(HealthCheckFilter filter, String [] tags) { + this.tags = tags; + this.filter = filter; + } + + String [] getTags() { + return tags; + } + + HealthCheckFilter getFilter() { + return filter; + } +} \ No newline at end of file -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
