Fine tune test coverage
authorBertrand Delacretaz <bdelacretaz@apache.org>
Tue, 30 Oct 2018 14:05:44 +0000 (15:05 +0100)
committerBertrand Delacretaz <bdelacretaz@apache.org>
Tue, 30 Oct 2018 14:05:44 +0000 (15:05 +0100)
src/main/java/org/apache/sling/capabilities/defaultsources/SlingServletsSource.java
src/main/java/org/apache/sling/capabilities/internal/RegexFilter.java
src/test/java/org/apache/sling/capabilities/defaultsources/SlingServletsSourceTest.java
src/test/java/org/apache/sling/capabilities/internal/CapabilitesServletTest.java
src/test/java/org/apache/sling/capabilities/internal/RegexFilterTest.java
src/test/java/org/apache/sling/capabilities/it/CapabilitiesBundleIT.java

index 6f9683e..bdb6e64 100644 (file)
@@ -109,7 +109,7 @@ public class SlingServletsSource implements CapabilitiesSource {
     }
     
     /** Convert input to a single value if it's an array of size one */
-    private static Object maybeConvertToSingleValue(Object input) {
+    static Object maybeConvertToSingleValue(Object input) {
         Object result = input;
         if(input instanceof Object[]) {
             Object[] arr = (Object[])input;
index b0af763..5d4c69c 100644 (file)
@@ -28,7 +28,7 @@ class RegexFilter {
     private final Pattern [] patterns;
     
     RegexFilter(String ... regexp) {
-        if(regexp == null || regexp.length == 0) {
+        if(regexp == null) {
             patterns = new Pattern[0];
         } else {
             patterns = new Pattern[regexp.length];
index 7f43e80..9503603 100644 (file)
@@ -102,4 +102,11 @@ public class SlingServletsSourceTest {
             assertTrue("Expecting id=12 or 41", id.equals("12") || id.equals("41"));
         }
     }
+    
+    @Test
+    public void testMaybeConvertToSingleValue() {
+        final Integer [] single = { 42 };
+        final Object result = SlingServletsSource.maybeConvertToSingleValue(single);
+        assertTrue(result instanceof Integer);
+    }
 }
\ No newline at end of file
index 2bcf16b..1d3546d 100644 (file)
@@ -43,6 +43,7 @@ import org.apache.sling.testing.resourceresolver.MockResource;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceRegistration;
@@ -75,6 +76,12 @@ public class CapabilitesServletTest {
     private final String OK_PATH = "/denied/but/ok";
     private final String VAR_PATH = "/var/something";
 
+    private static final CapabilitiesSource [] SOURCES = {
+        new MockSource("F", 2),
+        new MockSource("G", 43),
+        new MockSource("X", 45)
+    };
+
     @Before
     public void setup() throws IOException {
         
@@ -91,16 +98,12 @@ public class CapabilitesServletTest {
         resourceResolver = MockSling.newResourceResolver(bundleContext);
 
         
-        final CapabilitiesSource [] sources = {
-            new MockSource("F", 2),
-            new MockSource("G", 43),
-            new MockSource("X", 45)
-        };
-        for(CapabilitiesSource src : sources) {
+        for(CapabilitiesSource src : SOURCES) {
             // Not sure why both are needed, but tests fails otherwise
             context.registerService(src);
             servlet.bindSource(src);
         }
+
         context.registerInjectActivateService(servlet);
     }
     
@@ -116,6 +119,19 @@ public class CapabilitesServletTest {
     }
     
     @Test
+    public void testToStringProvidesTestingInfo() {
+        final String expected = SOURCES.length + " " + CapabilitiesSource.class.getSimpleName();
+        assertTrue("Expecting toString to indicate number of sources", servlet.toString().contains(expected));
+    }
+
+    @Test
+    public void testUnbind() {
+        servlet.unbindSource(SOURCES[0]);
+        final String expected = (SOURCES.length - 1) + " " + CapabilitiesSource.class.getSimpleName();
+        assertTrue("Expecting one source to be removed", servlet.toString().contains(expected));
+    }
+
+    @Test
     public void testDeniedPath() throws ServletException, IOException {
         MockSlingHttpServletResponse resp = new MockSlingHttpServletResponse();
         servlet.service(requestFor(DENIED_PATH, true), resp);
index 18ed258..a301668 100644 (file)
@@ -31,6 +31,12 @@ public class RegexFilterTest {
     }
     
     @Test
+    public void testNullPatternList() {
+        RegexFilter f = new RegexFilter((String[])null);
+        assertFalse(f.accept("meu"));
+    }
+
+    @Test
     public void testTwoStrings() {
         RegexFilter f = new RegexFilter("zo", "meu");
         assertFalse(f.accept("gabu"));
index 6541579..610c2cf 100644 (file)
@@ -85,7 +85,9 @@ public class CapabilitiesBundleIT extends CapabilitiesTestSupport {
             final String expected = reg.length + " CapabilitiesSource";
             final ServiceReference[] refs =  bundleContext.getServiceReferences(Servlet.class.getName(), "(sling.servlet.resourceTypes=sling/capabilities)");
             assertTrue("Expecting a single Servlet service", refs != null && refs.length == 1);
-            final String str = bundleContext.getService(refs[0]).toString();
+            final Object svc = bundleContext.getService(refs[0]);
+            assertNotNull("Expecting a service", svc);
+            final String str = svc.toString();
             assertTrue("Expecting " + expected + " but got " + str, str.contains(expected));
             bundleContext.ungetService(refs[0]);
         } finally {