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 f98299cdb479edc01849fa4cafea4fecc5b858ad Author: Bertrand Delacretaz <[email protected]> AuthorDate: Wed Jul 16 17:39:11 2014 +0000 HealthCheckTestsProviderTest tests everything now git-svn-id: https://svn.apache.org/repos/asf/sling/whiteboard/bdelacretaz/junit-bridge@1611109 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 6 + .../hc/junitbridge/HealthCheckTestsProvider.java | 8 +- .../tests/HealthCheckTestsProviderTest.java | 146 ++++++++++++++++++++- 3 files changed, 154 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index ef8fe22..33f1002 100644 --- a/pom.xml +++ b/pom.xml @@ -82,6 +82,12 @@ <version>1.9.5</version> <scope>test</scope> </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.5</version> + <scope>test</scope> + </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> 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 f99d2d6..7008aa2 100644 --- a/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java +++ b/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java @@ -78,7 +78,13 @@ public class HealthCheckTestsProvider implements TestsProvider { @Override public Class<?> createTestClass(String testName) throws ClassNotFoundException { // The test name is like "Health Checks(foo,bar)" and we need just 'foo,bar' - final String tagString = testName.substring(0, testName.length() - TEST_NAME_SUFFIX.length()).substring(TEST_NAME_PREFIX.length()); + String tagString = null; + try { + tagString = testName.substring(0, testName.length() - TEST_NAME_SUFFIX.length()).substring(TEST_NAME_PREFIX.length()); + } catch(Exception e) { + throw new RuntimeException("Invalid test name:" + testName); + } + JUnitTestBridge.setThreadContext(new TestBridgeContext(bundleContext, splitTags(tagString))); return JUnitTestBridge.class; } 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 index 2ef9114..07069b0 100644 --- a/src/test/java/org/apache/sling/hc/junitbridge/tests/HealthCheckTestsProviderTest.java +++ b/src/test/java/org/apache/sling/hc/junitbridge/tests/HealthCheckTestsProviderTest.java @@ -1,17 +1,32 @@ package org.apache.sling.hc.junitbridge.tests; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import java.util.ArrayList; import java.util.Dictionary; +import java.util.HashMap; import java.util.Hashtable; import java.util.List; +import java.util.Map; +import java.util.Random; +import org.apache.sling.hc.api.HealthCheck; +import org.apache.sling.hc.api.Result; import org.apache.sling.hc.junitbridge.HealthCheckTestsProvider; +import org.apache.sling.hc.util.FormattingResultLog; import org.apache.sling.junit.TestsProvider; import org.junit.Before; import org.junit.Test; +import org.junit.runner.JUnitCore; import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; import org.osgi.service.component.ComponentContext; /** Test the HealthCheckTestsProvider, which @@ -19,24 +34,109 @@ import org.osgi.service.component.ComponentContext; */ public class HealthCheckTestsProviderTest { private TestsProvider provider; + private long setupTimestamp; + private final Random random = new Random(); + static abstract class HcLogSetter { + abstract FormattingResultLog setLog(FormattingResultLog log); + }; + + private static final Map<String, HcLogSetter> LOG_SETTERS = new HashMap<String, HcLogSetter>(); + + static { + LOG_SETTERS.put("PASS_HC", new HcLogSetter() { + @Override + FormattingResultLog setLog(FormattingResultLog log) { + log.info("pass"); + return log; + } + }); + LOG_SETTERS.put("OK_HC", new HcLogSetter() { + @Override + FormattingResultLog setLog(FormattingResultLog log) { + log.debug("ok"); + return log; + } + }); + LOG_SETTERS.put("FAIL_HC", new HcLogSetter() { + @Override + FormattingResultLog setLog(FormattingResultLog log) { + log.warn("fail"); + return log; + } + }); + LOG_SETTERS.put("BAD_HC", new HcLogSetter() { + @Override + FormattingResultLog setLog(FormattingResultLog log) { + log.warn("bad"); + return log; + } + }); + } + + // Our fake tags represent a number of + // passing (P) or failing (F) fake HCs final String [] TAG_GROUPS = { - "foo,bar", - "wii", - "blue" + "a,b", + "some,tags", + "justOne" }; private static String testName(String tagGroup) { return HealthCheckTestsProvider.TEST_NAME_PREFIX + tagGroup + HealthCheckTestsProvider.TEST_NAME_SUFFIX; } + + /** Return ServiceReferences that point to our test HealthChecks */ + private ServiceReference [] getMockReferences(BundleContext bc, String OSGiFilter) { + + final List<ServiceReference> refs = new ArrayList<ServiceReference>(); + + for(String key : LOG_SETTERS.keySet()) { + if(OSGiFilter.contains(key)) { + final HcLogSetter hls = LOG_SETTERS.get(key); + final ServiceReference ref = Mockito.mock(ServiceReference.class); + Mockito.when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(random.nextLong()); + Mockito.when(ref.getProperty(HealthCheck.NAME)).thenReturn("someTest"); + final HealthCheck hc = new HealthCheck() { + @Override + public Result execute() { + final FormattingResultLog log = new FormattingResultLog(); + return new Result(hls.setLog(log)); + } + }; + Mockito.when(bc.getService(ref)).thenReturn(hc); + + refs.add(ref); + } + } + + return refs.toArray(new ServiceReference[]{}); + } @Before - public void setup() { + public void setup() throws InvalidSyntaxException { + setupTimestamp = System.currentTimeMillis(); final ComponentContext ctx = Mockito.mock(ComponentContext.class); + + // context properties final Dictionary<String, Object> props = new Hashtable<String, Object>(); props.put(HealthCheckTestsProvider.PROP_TAG_GROUPS, TAG_GROUPS); + props.put(Constants.SERVICE_PID, getClass().getName()); Mockito.when(ctx.getProperties()).thenReturn(props); + // bundle context + final BundleContext bc = Mockito.mock(BundleContext.class); + Mockito.when(ctx.getBundleContext()).thenReturn(bc); + + // HealthCheck ServiceReferences mocks + Mockito.when(bc.getServiceReferences(Mockito.anyString(), Mockito.anyString())).thenAnswer( + new Answer<ServiceReference[]> () { + @Override + public ServiceReference[] answer(InvocationOnMock invocation) throws Throwable { + return getMockReferences(bc, (String)invocation.getArguments()[1]); + } + }); + provider = new HealthCheckTestsProvider() { { activate(ctx); @@ -54,4 +154,40 @@ public class HealthCheckTestsProviderTest { } } -} + @Test + public void testServicePid() { + assertEquals(getClass().getName(), provider.getServicePid()); + } + + @Test + public void testLastModified() { + assertTrue(provider.lastModified() >= setupTimestamp); + } + + @Test + public void testNoFailuresHealthCheck() throws ClassNotFoundException { + final Class<?> c = provider.createTestClass(testName("PASS_HC")); + assertNotNull("Expecting non-null test class", c); + final org.junit.runner.Result r = JUnitCore.runClasses(c); + assertEquals(0, r.getFailureCount()); + assertEquals(1, r.getRunCount()); + } + + @Test + public void testFailingHealthCheck() throws ClassNotFoundException { + final Class<?> c = provider.createTestClass(testName("FAIL_HC and BAD_HC")); + assertNotNull("Expecting non-null test class", c); + final org.junit.runner.Result r = JUnitCore.runClasses(c); + assertEquals(2, r.getFailureCount()); + assertEquals(2, r.getRunCount()); + } + + @Test + public void testPassAndFailHealthCheck() throws ClassNotFoundException { + final Class<?> c = provider.createTestClass(testName("FAIL_HC and PASS_HC and OK_HC and BAD_HC")); + assertNotNull("Expecting non-null test class", c); + final org.junit.runner.Result r = JUnitCore.runClasses(c); + assertEquals(2, r.getFailureCount()); + assertEquals(4, r.getRunCount()); + } +} \ No newline at end of file -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
