sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1611109 - in /sling/whiteboard/bdelacretaz/junit-bridge: pom.xml src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java src/test/java/org/apache/sling/hc/junitbridge/tests/HealthCheckTestsProviderTest.java
Date Wed, 16 Jul 2014 17:39:11 GMT
Author: bdelacretaz
Date: Wed Jul 16 17:39:11 2014
New Revision: 1611109

URL: http://svn.apache.org/r1611109
Log:
HealthCheckTestsProviderTest tests everything now

Modified:
    sling/whiteboard/bdelacretaz/junit-bridge/pom.xml
    sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java
    sling/whiteboard/bdelacretaz/junit-bridge/src/test/java/org/apache/sling/hc/junitbridge/tests/HealthCheckTestsProviderTest.java

Modified: sling/whiteboard/bdelacretaz/junit-bridge/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit-bridge/pom.xml?rev=1611109&r1=1611108&r2=1611109&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit-bridge/pom.xml (original)
+++ sling/whiteboard/bdelacretaz/junit-bridge/pom.xml Wed Jul 16 17:39:11 2014
@@ -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>

Modified: sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java?rev=1611109&r1=1611108&r2=1611109&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java
(original)
+++ sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java
Wed Jul 16 17:39:11 2014
@@ -78,7 +78,13 @@ public class HealthCheckTestsProvider im
     @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;
     }

Modified: sling/whiteboard/bdelacretaz/junit-bridge/src/test/java/org/apache/sling/hc/junitbridge/tests/HealthCheckTestsProviderTest.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit-bridge/src/test/java/org/apache/sling/hc/junitbridge/tests/HealthCheckTestsProviderTest.java?rev=1611109&r1=1611108&r2=1611109&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit-bridge/src/test/java/org/apache/sling/hc/junitbridge/tests/HealthCheckTestsProviderTest.java
(original)
+++ sling/whiteboard/bdelacretaz/junit-bridge/src/test/java/org/apache/sling/hc/junitbridge/tests/HealthCheckTestsProviderTest.java
Wed Jul 16 17:39:11 2014
@@ -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.Compon
  */
 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 HealthCheckTestsProviderTes
         }
     }
     
-}
+    @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



Mime
View raw message