SLING-3127 - prepare Health Check tools release

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1526476 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..9bbda57
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,102 @@
+<?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>17</version>
+        <relativePath/>
+    </parent>
+
+    <groupId>org.apache.sling</groupId>
+    <artifactId>org.apache.sling.hc.it</artifactId>
+    <packaging>jar</packaging>
+    <version>0.0.2-SNAPSHOT</version>
+
+    <name>Sling Health Check Integration Tests</name>
+    <inceptionYear>2013</inceptionYear>
+
+    <properties>
+        <exam.version>3.0.3</exam.version>
+        <url.version>1.5.2</url.version>
+        <org.ops4j.pax.logging.DefaultServiceLog.level>INFO</org.ops4j.pax.logging.DefaultServiceLog.level>
+        <felix.shell>false</felix.shell>
+        <sling.java.version>6</sling.java.version>
+    </properties>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <configuration>
+                    <systemProperties>
+                        <org.ops4j.pax.logging.DefaultServiceLog.level>${org.ops4j.pax.logging.DefaultServiceLog.level}</org.ops4j.pax.logging.DefaultServiceLog.level>
+                        <sling.hc.core.version>${project.version}</sling.hc.core.version>
+                        <felix.shell>${felix.shell}</felix.shell>
+                    </systemProperties>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.hc.core</artifactId>
+            <version>0.0.2-SNAPSHOT</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <version>1.6.2</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <version>1.6.2</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-container-native</artifactId>
+            <version>${exam.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-junit4</artifactId>
+            <version>${exam.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-link-mvn</artifactId>
+            <version>${exam.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.url</groupId>
+            <artifactId>pax-url-aether</artifactId>
+            <version>${url.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.framework</artifactId>
+            <version>4.2.1</version>
+            <scope>test</scope>
+        </dependency>
+     </dependencies>
+</project>
diff --git a/src/test/java/org/apache/sling/hc/it/core/HealthCheckFilterTest.java b/src/test/java/org/apache/sling/hc/it/core/HealthCheckFilterTest.java
new file mode 100644
index 0000000..4a154f7
--- /dev/null
+++ b/src/test/java/org/apache/sling/hc/it/core/HealthCheckFilterTest.java
@@ -0,0 +1,221 @@
+/*
+ * 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.it.core;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.inject.Inject;
+
+import org.apache.sling.hc.api.HealthCheck;
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.util.HealthCheckFilter;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@RunWith(PaxExam.class)
+public class HealthCheckFilterTest {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private HealthCheckFilter filter;
+
+    @Inject
+    private BundleContext bundleContext;
+
+    private List<TestHealthCheck> testServices = new ArrayList<TestHealthCheck>();
+    private static int instanceCounter = 0;
+
+    class TestHealthCheck implements HealthCheck {
+
+        private final int id;
+        private final ServiceRegistration<?> reg;
+        final String[] tags;
+
+        TestHealthCheck(String... tags) {
+            id = instanceCounter++;
+            this.tags = tags;
+            final Dictionary<String, Object> props = new Hashtable<String, Object>();
+            if (tags != null) {
+                props.put(HealthCheck.TAGS, tags);
+            }
+            props.put(HealthCheck.TAGS, tags);
+            reg = bundleContext.registerService(HealthCheck.class.getName(),
+                    this, props);
+            log.info("Registered {} with {}={}", new Object[] { this,
+                    HealthCheck.TAGS, props.get(HealthCheck.TAGS) });
+        }
+
+        @Override
+        public String toString() {
+            return Arrays.asList(tags).toString();
+        }
+
+        @Override
+        public boolean equals(Object other) {
+            return other instanceof TestHealthCheck
+                    && ((TestHealthCheck) other).id == id;
+        }
+
+        @Override
+        public int hashCode() {
+            return id;
+        }
+
+        @Override
+        public Result execute() {
+            return null;
+        }
+
+        void unregister() {
+            reg.unregister();
+        }
+    }
+
+    @Configuration
+    public Option[] config() {
+        return U.config();
+    }
+
+    @Before
+    public void setup() {
+        testServices.add(new TestHealthCheck("foo"));
+        testServices.add(new TestHealthCheck("bar"));
+        testServices.add(new TestHealthCheck("foo", "bar"));
+        testServices.add(new TestHealthCheck("other", "thing"));
+        testServices.add(new TestHealthCheck());
+        filter = new HealthCheckFilter(bundleContext);
+    }
+
+    @After
+    public void cleanup() {
+        for (TestHealthCheck tc : testServices) {
+            tc.unregister();
+        }
+    }
+
+    /**
+     * @param included
+     *            true or false, in the same order as testServices
+     */
+    private void assertServices(List<HealthCheck> s, boolean... included) {
+        final Iterator<TestHealthCheck> it = testServices.iterator();
+        for (boolean inc : included) {
+            final TestHealthCheck thc = it.next();
+            if (inc) {
+                assertTrue("Expecting list of services to include " + thc,
+                        s.contains(thc));
+            } else {
+                assertFalse("Not expecting list of services to include " + thc,
+                        s.contains(thc));
+            }
+        }
+    }
+
+    @Test
+    public void testSelectorService() {
+        assertNotNull("Expecting HealthCheckSelector service to be provided",
+                filter);
+    }
+
+    @Test
+    public void testAllServices() {
+        final List<HealthCheck> s = filter.getTaggedHealthChecks();
+        assertServices(s, true, true, true, true, true);
+    }
+
+    @Test
+    public void testEmptyTags() {
+        final List<HealthCheck> s = filter.getTaggedHealthChecks("", "", "");
+        assertServices(s, true, true, true, true, true);
+    }
+
+    @Test
+    public void testFooTag() {
+        final List<HealthCheck> s = filter.getTaggedHealthChecks("foo");
+        assertServices(s, true, false, true, false, false);
+    }
+
+    @Test
+    public void testBarTag() {
+        final List<HealthCheck> s = filter.getTaggedHealthChecks("bar");
+        assertServices(s, false, true, true, false, false);
+    }
+
+    @Test
+    public void testFooAndBar() {
+        final List<HealthCheck> s = filter.getTaggedHealthChecks("foo", "bar");
+        assertServices(s, false, false, true, false, false);
+    }
+
+    @Test
+    public void testFooMinusBar() {
+        final List<HealthCheck> s = filter
+                .getTaggedHealthChecks("foo", "-bar");
+        assertServices(s, true, false, false, false, false);
+    }
+
+    @Test
+    public void testWhitespace() {
+        final List<HealthCheck> s = filter.getTaggedHealthChecks(
+                "\t \n\r foo  \t", "", " \t-bar\n", "");
+        assertServices(s, true, false, false, false, false);
+    }
+
+    @Test
+    public void testOther() {
+        final List<HealthCheck> s = filter.getTaggedHealthChecks("other");
+        assertServices(s, false, false, false, true, false);
+    }
+
+    @Test
+    public void testMinusOther() {
+        final List<HealthCheck> s = filter.getTaggedHealthChecks("-other");
+        assertServices(s, true, true, true, false, true);
+    }
+
+    @Test
+    public void testMinusOtherFoo() {
+        final List<HealthCheck> s = filter.getTaggedHealthChecks("-other",
+                "-foo");
+        assertServices(s, false, true, false, false, true);
+    }
+
+    @Test
+    public void testNoResults() {
+        final List<HealthCheck> s = filter.getTaggedHealthChecks("NOT A TAG");
+        assertTrue("Expecting no services", s.isEmpty());
+    }
+}
diff --git a/src/test/java/org/apache/sling/hc/it/core/U.java b/src/test/java/org/apache/sling/hc/it/core/U.java
new file mode 100644
index 0000000..6503add
--- /dev/null
+++ b/src/test/java/org/apache/sling/hc/it/core/U.java
@@ -0,0 +1,69 @@
+/*
+ * 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.it.core;
+
+import static org.ops4j.pax.exam.CoreOptions.junitBundles;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.provision;
+import static org.ops4j.pax.exam.CoreOptions.systemProperty;
+import static org.ops4j.pax.exam.CoreOptions.when;
+
+import org.ops4j.pax.exam.Option;
+
+/** Test utilities */
+public class U {
+    
+    static Option[] config() {
+        final String coreVersion = System.getProperty("sling.hc.core.version");
+        final String localRepo = System.getProperty("maven.repo.local", "");
+        final boolean felixShell = "true".equals(System.getProperty("felix.shell", "false"));
+
+        return options(
+            when(localRepo.length() > 0).useOptions(
+                    systemProperty("org.ops4j.pax.url.mvn.localRepository").value(localRepo)
+            ),                    
+            junitBundles(),
+            when(felixShell).useOptions(
+                    provision(
+                            mavenBundle("org.apache.felix", "org.apache.felix.gogo.shell", "0.10.0"),
+                            mavenBundle("org.apache.felix", "org.apache.felix.gogo.runtime", "0.10.0"),
+                            mavenBundle("org.apache.felix", "org.apache.felix.gogo.command", "0.12.0")
+                    )
+            ),
+            provision(
+                    mavenBundle("org.apache.felix", "org.apache.felix.scr", "1.6.2"),
+                    mavenBundle("org.apache.sling", "org.apache.sling.hc.core", coreVersion),
+                    mavenBundle("org.apache.sling", "org.apache.sling.commons.osgi", "2.2.0"),
+                    mavenBundle("org.apache.sling", "org.apache.sling.jcr.jcr-wrapper", "2.0.0"),
+                    mavenBundle("org.apache.sling", "org.apache.sling.api", "2.4.2"),
+                    mavenBundle("org.apache.sling", "org.apache.sling.jcr.api", "2.1.0"),
+                    mavenBundle("org.apache.sling", "org.apache.sling.engine", "2.2.8"),
+                    mavenBundle("org.apache.sling", "org.apache.sling.auth.core", "1.1.2"),
+                    mavenBundle("org.apache.sling", "org.apache.sling.commons.mime", "2.1.4"),
+                    mavenBundle("org.apache.sling", "org.apache.sling.settings", "1.2.2"),
+                    mavenBundle("org.apache.sling", "org.apache.sling.launchpad.api", "1.1.0"),
+                    mavenBundle("org.apache.sling", "org.apache.sling.scripting.api", "2.1.0"),
+                    mavenBundle("commons-collections", "commons-collections", "3.2.1"),
+                    mavenBundle("commons-io", "commons-io", "1.4"),
+                    mavenBundle("commons-fileupload", "commons-fileupload", "1.2.2"),
+                    mavenBundle("org.mortbay.jetty", "servlet-api-2.5", "6.1.14")
+            )
+        );
+    }
+}