sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1610678 - in /sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge: HealthCheckTest.java HealthCheckTestsProvider.java JUnitTestBridge.java TestBridgeContext.java
Date Tue, 15 Jul 2014 13:02:08 GMT
Author: bdelacretaz
Date: Tue Jul 15 13:02:08 2014
New Revision: 1610678

URL: http://svn.apache.org/r1610678
Log:
Use HealthCheckMetadata to provide test name

Modified:
    sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTest.java
    sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTestsProvider.java
    sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/JUnitTestBridge.java
    sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/TestBridgeContext.java

Modified: sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTest.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTest.java?rev=1610678&r1=1610677&r2=1610678&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTest.java
(original)
+++ sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/HealthCheckTest.java
Tue Jul 15 13:02:08 2014
@@ -22,45 +22,55 @@ import junit.framework.TestCase;
 import org.apache.sling.hc.api.HealthCheck;
 import org.apache.sling.hc.api.Result;
 import org.apache.sling.hc.api.ResultLog;
+import org.apache.sling.hc.util.HealthCheckMetadata;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
 
 public class HealthCheckTest extends TestCase {
     private final HealthCheck hc;
-    private final String name;
+    private final HealthCheckMetadata metadata;
+    private final BundleContext bundleContext;
+    private final ServiceReference serviceRef;
         
-    HealthCheckTest(HealthCheck hc) {
+    HealthCheckTest(TestBridgeContext context, ServiceReference ref) {
         super("testHealthCheck");
-        this.hc = hc;
-        
-        // TODO HC name? see webconsole
-        name = hc.getClass().getName();
+        bundleContext = context.getBundleContext();
+        serviceRef = ref;
+        this.hc = (HealthCheck)bundleContext.getService(ref);
+        this.metadata = new HealthCheckMetadata(ref);
     }
     
     @Override
     public String getName() {
-        return name;
+        return metadata.getName();
     }
 
     /** Execute our health check and dump its log
      *  messages > INFO if it fails */
     public void testHealthCheck() {
-        final Result r = hc.execute(); 
-        final StringBuilder failMsg = new StringBuilder();
-        if(!r.isOk()) {
-            failMsg.append(name);
-            failMsg.append("\n");
-            for(ResultLog.Entry log : r) {
-                if(failMsg.length() > 0) {
-                    failMsg.append("\n");
-                }
-                if(log.getStatus().compareTo(Result.Status.INFO) > 0) {
-                    failMsg.append(log.getStatus().toString());
-                    failMsg.append(" - ");
-                    failMsg.append(log.getMessage());
+        try {
+            final Result r = hc.execute();
+            final StringBuilder failMsg = new StringBuilder();
+            if(!r.isOk()) {
+                failMsg.append(metadata.getName());
+                failMsg.append("\n");
+                for(ResultLog.Entry log : r) {
+                    if(log.getStatus().compareTo(Result.Status.INFO) > 0) {
+                        if(failMsg.length() > 0) {
+                            failMsg.append("\n");
+                        }
+                        failMsg.append(log.getStatus().toString());
+                        failMsg.append(" - ");
+                        failMsg.append(log.getMessage());
+                    }
                 }
             }
-        }
-        if(failMsg.length() > 0) {
-            fail("Health Check failed: " + failMsg.toString());
+            if(failMsg.length() > 0) {
+                fail("Health Check failed: " + failMsg.toString());
+            }
+        } finally {
+            // TODO is that ok? service not used anymore after this?
+            bundleContext.ungetService(serviceRef);
         }
     }
 }
\ No newline at end of file

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=1610678&r1=1610677&r2=1610678&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
Tue Jul 15 13:02:08 2014
@@ -22,9 +22,10 @@ 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.Service;
-import org.apache.sling.hc.util.HealthCheckFilter;
 import org.apache.sling.junit.TestsProvider;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.service.component.ComponentContext;
 
@@ -36,22 +37,49 @@ import org.osgi.service.component.Compon
 public class HealthCheckTestsProvider implements TestsProvider {
 
     private String servicePid;
+    private long lastModified;
+    private BundleContext bundleContext;
+    
+    public static final String TEST_NAME_PREFIX = "HealthChecks(";
+    public static final String TEST_NAME_SUFFIX = ")";
 
     // TODO configurable
-    private String [] tags = { "script" };
-    private HealthCheckFilter filter;
+    private String [] tags = { 
+        "script",
+        "sling",
+        "bundles,script",
+        "bundles,-script"
+    };
     
     @Activate
     protected void activate(ComponentContext ctx) {
+        bundleContext = ctx.getBundleContext();
         servicePid = (String)ctx.getProperties().get(Constants.SERVICE_PID);
-        filter = new HealthCheckFilter(ctx.getBundleContext());
+        lastModified = System.currentTimeMillis();
+    }
+    
+    @Deactivate
+    protected void deactivate() {
+        bundleContext = null;
+        servicePid = null;
+        lastModified = -1;
     }
     
     @Override
     public Class<?> createTestClass(String testName) throws ClassNotFoundException
{
-        JUnitTestBridge.setContext(new TestBridgeContext(filter, tags));
+        // 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());

+        JUnitTestBridge.setContext(new TestBridgeContext(bundleContext, splitTags(tagString)));
         return JUnitTestBridge.class;
     }
+    
+    private String [] splitTags(String tags) {
+        final List<String> result = new ArrayList<String>();
+        for(String tag: tags.split(",")) {
+            result.add(tag.trim());
+        }
+        return result.toArray(new String[]{});
+    }
 
     @Override
     public String getServicePid() {
@@ -61,13 +89,14 @@ public class HealthCheckTestsProvider im
     @Override
     public List<String> getTestNames() {
         final List<String> result = new ArrayList<String>();
-        // TODO use a configurable name?
-        result.add("HealthChecks(sling,slow)");
+        for(String t : tags) {
+            result.add(TEST_NAME_PREFIX + t + TEST_NAME_SUFFIX);
+        }
         return result;
     }
 
     @Override
     public long lastModified() {
-        return 0;
+        return lastModified;
     }
 }
\ No newline at end of file

Modified: sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/JUnitTestBridge.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/JUnitTestBridge.java?rev=1610678&r1=1610677&r2=1610678&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/JUnitTestBridge.java
(original)
+++ sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/JUnitTestBridge.java
Tue Jul 15 13:02:08 2014
@@ -20,9 +20,9 @@ package org.apache.sling.hc.junitbridge;
 import static org.junit.Assert.assertNotNull;
 import junit.framework.TestSuite;
 
-import org.apache.sling.hc.api.HealthCheck;
 import org.junit.runner.RunWith;
 import org.junit.runners.AllTests;
+import org.osgi.framework.ServiceReference;
 
 @RunWith(AllTests.class)
 public class JUnitTestBridge {
@@ -36,8 +36,8 @@ public class JUnitTestBridge {
         final TestBridgeContext context = testContext.get();
         assertNotNull("Expecting non-null TestBridgeContext, via ThreadLocal", context);
         TestSuite suite = new TestSuite();
-        for(HealthCheck hc : context.getFilter().getTaggedHealthChecks(context.getTags()))
{
-            suite.addTest(new HealthCheckTest(hc));
+        for(ServiceReference ref : context.getFilter().getTaggedHealthCheckServiceReferences(context.getTags()))
{
+            suite.addTest(new HealthCheckTest(context, ref));
         }
         return suite;
     }

Modified: sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/TestBridgeContext.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/TestBridgeContext.java?rev=1610678&r1=1610677&r2=1610678&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/TestBridgeContext.java
(original)
+++ sling/whiteboard/bdelacretaz/junit-bridge/src/main/java/org/apache/sling/hc/junitbridge/TestBridgeContext.java
Tue Jul 15 13:02:08 2014
@@ -18,14 +18,17 @@
 package org.apache.sling.hc.junitbridge;
 
 import org.apache.sling.hc.util.HealthCheckFilter;
+import org.osgi.framework.BundleContext;
 
 class TestBridgeContext {
     private final String [] tags;
     private final HealthCheckFilter filter;
+    private final BundleContext bundleContext;
         
-    TestBridgeContext(HealthCheckFilter filter, String [] tags) {
+    TestBridgeContext(BundleContext bundleContext, String [] tags) {
+        this.bundleContext = bundleContext;
         this.tags = tags;
-        this.filter = filter;
+        this.filter = new HealthCheckFilter(bundleContext);
     }
     
     String [] getTags() {
@@ -35,4 +38,8 @@ class TestBridgeContext {
     HealthCheckFilter getFilter() {
         return filter;
     }
+    
+    BundleContext getBundleContext() {
+        return bundleContext;
+    }
 }
\ No newline at end of file



Mime
View raw message