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 f92ac2847612a5453362fe211e1c82ac9e035cef Author: Bertrand Delacretaz <[email protected]> AuthorDate: Tue Jul 15 15:01:44 2014 +0000 Configurable tag groups, and start testing git-svn-id: https://svn.apache.org/repos/asf/sling/whiteboard/bdelacretaz/junit-bridge@1610720 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 13 ++++- .../hc/junitbridge/HealthCheckTestsProvider.java | 28 +++++++---- .../tests/HealthCheckTestsProviderTest.java | 57 ++++++++++++++++++++++ 3 files changed, 88 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 04440b9..ef8fe22 100644 --- a/pom.xml +++ b/pom.xml @@ -71,7 +71,18 @@ <artifactId>junit</artifactId> <scope>provided</scope> </dependency> - <dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>1.9.5</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.6.2</version> diff --git a/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java b/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java index fb5c14b..f99d2d6 100644 --- a/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java +++ b/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java @@ -23,16 +23,20 @@ import java.util.List; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Deactivate; +import org.apache.felix.scr.annotations.Property; import org.apache.felix.scr.annotations.Service; +import org.apache.sling.commons.osgi.PropertiesUtil; import org.apache.sling.junit.TestsProvider; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; import org.osgi.service.component.ComponentContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** Bridge Health Checks into the Sling JUnit server-side test * framework, based on their tags. */ -@Component +@Component(metatype=true) @Service public class HealthCheckTestsProvider implements TestsProvider { @@ -40,20 +44,26 @@ public class HealthCheckTestsProvider implements TestsProvider { private long lastModified; private BundleContext bundleContext; + private final Logger log = LoggerFactory.getLogger(getClass()); + public static final String TEST_NAME_PREFIX = "HealthChecks("; public static final String TEST_NAME_SUFFIX = ")"; - // TODO configurable - private String [] tags = { - "script", - "sling", - "bundles,script", - "bundles,-script" - }; + @Property(cardinality=2147483647, + label="Health Check Tags", + description="Groups of health check tags to execute as JUnit tests. Use the standard Health Checks 'includeThis,-omitThat' syntax") + public static final String PROP_TAG_GROUPS = "health.check.tag.groups"; + + private String [] tagGroups; @Activate protected void activate(ComponentContext ctx) { bundleContext = ctx.getBundleContext(); + tagGroups = PropertiesUtil.toStringArray(ctx.getProperties().get(PROP_TAG_GROUPS)); + if(tagGroups == null) { + tagGroups = new String[]{}; + log.warn("No tag groups configured via {}, Health Checks won't be available as JUnit tests", PROP_TAG_GROUPS); + } servicePid = (String)ctx.getProperties().get(Constants.SERVICE_PID); lastModified = System.currentTimeMillis(); } @@ -89,7 +99,7 @@ public class HealthCheckTestsProvider implements TestsProvider { @Override public List<String> getTestNames() { final List<String> result = new ArrayList<String>(); - for(String t : tags) { + for(String t : tagGroups) { result.add(TEST_NAME_PREFIX + t + TEST_NAME_SUFFIX); } return result; diff --git a/src/test/java/org/apache/sling/hc/junitbridge/tests/HealthCheckTestsProviderTest.java b/src/test/java/org/apache/sling/hc/junitbridge/tests/HealthCheckTestsProviderTest.java new file mode 100644 index 0000000..2ef9114 --- /dev/null +++ b/src/test/java/org/apache/sling/hc/junitbridge/tests/HealthCheckTestsProviderTest.java @@ -0,0 +1,57 @@ +package org.apache.sling.hc.junitbridge.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.List; + +import org.apache.sling.hc.junitbridge.HealthCheckTestsProvider; +import org.apache.sling.junit.TestsProvider; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.osgi.service.component.ComponentContext; + +/** Test the HealthCheckTestsProvider, which + * uses everything else. + */ +public class HealthCheckTestsProviderTest { + private TestsProvider provider; + + final String [] TAG_GROUPS = { + "foo,bar", + "wii", + "blue" + }; + + private static String testName(String tagGroup) { + return HealthCheckTestsProvider.TEST_NAME_PREFIX + tagGroup + HealthCheckTestsProvider.TEST_NAME_SUFFIX; + } + + @Before + public void setup() { + final ComponentContext ctx = Mockito.mock(ComponentContext.class); + final Dictionary<String, Object> props = new Hashtable<String, Object>(); + props.put(HealthCheckTestsProvider.PROP_TAG_GROUPS, TAG_GROUPS); + Mockito.when(ctx.getProperties()).thenReturn(props); + + provider = new HealthCheckTestsProvider() { + { + activate(ctx); + } + }; + } + + @Test + public void testGetTestNames() { + final List<String> names = provider.getTestNames(); + assertEquals(TAG_GROUPS.length, names.size()); + for(String tag : TAG_GROUPS) { + final String expected = testName(tag); + assertTrue("Expecting test names to contain " + expected + ", " + names, names.contains(expected)); + } + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
