incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1513408 - in /sling/trunk/contrib/extensions/healthcheck/api/src: main/java/org/apache/sling/hc/api/Result.java test/java/org/apache/sling/hc/api/ResultMergeTest.java
Date Tue, 13 Aug 2013 09:39:13 GMT
Author: bdelacretaz
Date: Tue Aug 13 09:39:13 2013
New Revision: 1513408

URL: http://svn.apache.org/r1513408
Log:
SLING-2987 - support for merging several Result

Added:
    sling/trunk/contrib/extensions/healthcheck/api/src/test/java/org/apache/sling/hc/api/ResultMergeTest.java
Modified:
    sling/trunk/contrib/extensions/healthcheck/api/src/main/java/org/apache/sling/hc/api/Result.java

Modified: sling/trunk/contrib/extensions/healthcheck/api/src/main/java/org/apache/sling/hc/api/Result.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/api/src/main/java/org/apache/sling/hc/api/Result.java?rev=1513408&r1=1513407&r2=1513408&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/api/src/main/java/org/apache/sling/hc/api/Result.java
(original)
+++ sling/trunk/contrib/extensions/healthcheck/api/src/main/java/org/apache/sling/hc/api/Result.java
Tue Aug 13 09:39:13 2013
@@ -50,22 +50,41 @@ public class Result implements Iterable 
         this.logger = logger != null ? logger : CLASS_LOGGER;
     }
     
+    /** Merge a set of Result into this one. This Result's status
+     *  is set to the highest status of all supplied Result, and
+     *  their log entries are added to this. */
+    public void merge(Result ...results) {
+        for(Result r : results) {
+            setStatus(r.getStatus());
+            for(ResultLogEntry e : r) {
+                log(e);
+            }
+        }
+    }
+    
     /** Add an entry to our log. Use the {@ResultLogEntry}.LT_* constants
      *  for well-known entry types.
      *  Adding an entry with a type where {@ResultLogEntry#isInformationalEntryType} returns
      *  false causes our status to be set to WARN, unless it was already set higher.
      */
     public void log(String entryType, String message) {
-        if(logger.isDebugEnabled() && ResultLogEntry.LT_DEBUG.equals(entryType))
{
-            logger.debug(message);
-        } else if(logger.isInfoEnabled() && ResultLogEntry.LT_INFO.equals(entryType))
{
-            logger.info(message);
+        log(new ResultLogEntry(entryType, message));
+    }
+    
+    /** Add an entry to our log - in general it is more convenient to use the {@link #add(String,
String)}
+     *  method - this is useful when merging Result for example.
+     */
+    public void log(ResultLogEntry e) {
+        if(logger.isDebugEnabled() && ResultLogEntry.LT_DEBUG.equals(e.getEntryType()))
{
+            logger.debug(e.getMessage());
+        } else if(logger.isInfoEnabled() && ResultLogEntry.LT_INFO.equals(e.getEntryType()))
{
+            logger.info(e.getMessage());
         } else {
-            logger.warn(message);
+            logger.warn(e.getMessage());
         }
-        logEntries.add(new ResultLogEntry(entryType, message));
-        if(!ResultLogEntry.isInformationalEntryType(entryType) && status.ordinal()
< Status.WARN.ordinal()) {
-            logger.warn("Setting Result status to WARN due to log entry of type {}", entryType);
+        logEntries.add(e);
+        if(!ResultLogEntry.isInformationalEntryType(e.getEntryType()) && status.ordinal()
< Status.WARN.ordinal()) {
+            logger.warn("Setting Result status to WARN due to log entry of type {}", e.getEntryType());
             setStatus(Status.WARN);
         }
     }

Added: sling/trunk/contrib/extensions/healthcheck/api/src/test/java/org/apache/sling/hc/api/ResultMergeTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/api/src/test/java/org/apache/sling/hc/api/ResultMergeTest.java?rev=1513408&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/api/src/test/java/org/apache/sling/hc/api/ResultMergeTest.java
(added)
+++ sling/trunk/contrib/extensions/healthcheck/api/src/test/java/org/apache/sling/hc/api/ResultMergeTest.java
Tue Aug 13 09:39:13 2013
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The SF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.sling.hc.api;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Iterator;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class ResultMergeTest {
+
+    private Result result;
+    
+    @Before
+    public void setup() {
+        result = new Result();
+        result.setStatus(Result.Status.WARN);
+        result.log(ResultLogEntry.LT_DEBUG, "BM1 debug");
+        result.log(ResultLogEntry.LT_INFO, "BM2 info");
+    }
+    
+    private void assertResult(Result.Status status, String ... messages) {
+        assertEquals(status, result.getStatus());
+        final Iterator<ResultLogEntry> it = result.iterator();
+        for(String msg : messages) {
+            assertTrue("Expecting " + msg + " to be present", it.hasNext());
+            assertEquals(msg, it.next().getMessage());
+        }
+    }
+    
+    @Test
+    public void testInitialState() {
+        assertResult(Result.Status.WARN, "BM1 debug", "BM2 info");
+    }
+    
+    @Test
+    public void testMergeNoResults() {
+        result.merge();
+        assertResult(Result.Status.WARN, "BM1 debug", "BM2 info");
+    }
+    
+    @Test
+    public void testMergeSingleResult() {
+        final Result r2 = new Result();
+        r2.log("FOO", "T1");
+        r2.log("FOO", "T2");
+        result.merge(r2);
+        assertResult(Result.Status.WARN, "BM1 debug", "BM2 info", "T1", "T2");
+    }
+    
+    @Test
+    public void testMergeMultipleResults() {
+        final Result [] more = new Result[3];
+        for(int i=0 ; i < more.length; i++) {
+            more[i] = new Result();
+            if(i==1) {
+                more[i].setStatus(Result.Status.CRITICAL);
+            }
+            more[i].log("BAR", "X" + i);
+        }
+        result.merge(more);
+        assertResult(Result.Status.CRITICAL, "BM1 debug", "BM2 info", "X0", "X1", "X2");
+    }
+    
+}



Mime
View raw message