[logback-dev] svn commit: r1846 - in logback/trunk/logback-core/src/test: input/joran/skip java/ch/qos/logback/core/joran java/ch/qos/logback/core/joran/action/ext

noreply.ceki at qos.ch noreply.ceki at qos.ch
Sat Oct 18 21:09:55 CEST 2008


Author: ceki
Date: Sat Oct 18 21:09:55 2008
New Revision: 1846

Modified:
   logback/trunk/logback-core/src/test/input/joran/skip/badEnd2.xml
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/SkippingInInterpreterTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/ext/BadEndAction.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/ext/HelloAction.java

Log:
LBCLASSIC-63  LBCORE-38

Test that Joran does continues processing in presence of errors and that 
it registers status messages

Modified: logback/trunk/logback-core/src/test/input/joran/skip/badEnd2.xml
==============================================================================
--- logback/trunk/logback-core/src/test/input/joran/skip/badEnd2.xml	(original)
+++ logback/trunk/logback-core/src/test/input/joran/skip/badEnd2.xml	Sat Oct 18 21:09:55 2008
@@ -8,7 +8,7 @@
     <badEnd type="ActionException">
       <touch/>
     </badEnd> 
-    <!-- should be skipped -->
+    <!-- should not be skipped -->
     <touch/> 
   </isolate>
 

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/SkippingInInterpreterTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/SkippingInInterpreterTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/SkippingInInterpreterTest.java	Sat Oct 18 21:09:55 2008
@@ -1,7 +1,7 @@
 /**
  * Logback: the generic, reliable, fast and flexible logging framework.
  * 
- * Copyright (C) 1999-2006, QOS.ch
+ * Copyright (C) 1999-2008, QOS.ch
  * 
  * This library is free software, you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the Free
@@ -11,8 +11,10 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 import java.util.HashMap;
+import java.util.List;
 
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
@@ -24,15 +26,18 @@
 import ch.qos.logback.core.joran.action.Action;
 import ch.qos.logback.core.joran.action.NOPAction;
 import ch.qos.logback.core.joran.action.ext.BadBeginAction;
+import ch.qos.logback.core.joran.action.ext.BadEndAction;
 import ch.qos.logback.core.joran.action.ext.HelloAction;
 import ch.qos.logback.core.joran.action.ext.TouchAction;
+import ch.qos.logback.core.joran.spi.ActionException;
 import ch.qos.logback.core.joran.spi.Pattern;
+import ch.qos.logback.core.status.Status;
+import ch.qos.logback.core.status.StatusManager;
 import ch.qos.logback.core.util.Constants;
-import ch.qos.logback.core.util.StatusPrinter;
 
 /**
- * Test the way Interpreter skips elements in case of exceptions thrown by
- * Actions.
+ * Test the way Interpreter skips child elements in case of exceptions thrown by
+ * Actions. It also tests addition of status messages in case of exceptions.
  * 
  * @author Ceki Gulcu
  */
@@ -40,66 +45,67 @@
 
   HashMap<Pattern, Action> rulesMap = new HashMap<Pattern, Action>();
   Context context = new ContextBase();
+  StatusManager sm = context.getStatusManager();
 
   SAXParser createParser() throws Exception {
     SAXParserFactory spf = SAXParserFactory.newInstance();
     return spf.newSAXParser();
   }
 
-  void doTest(String filename) throws Exception {
+  void doTest(String filename, Integer expectedInt, Class exceptionClass)
+      throws Exception {
 
     rulesMap.put(new Pattern("test"), new NOPAction());
     rulesMap.put(new Pattern("test/badBegin"), new BadBeginAction());
     rulesMap.put(new Pattern("test/badBegin/touch"), new TouchAction());
+    rulesMap.put(new Pattern("test/badEnd"), new BadEndAction());
+    rulesMap.put(new Pattern("test/badEnd/touch"), new TouchAction());
+    rulesMap.put(new Pattern("test/hello"), new HelloAction());
+
+    rulesMap.put(new Pattern("test/isolate"), new NOPAction());
+    rulesMap.put(new Pattern("test/isolate/badEnd"), new BadEndAction());
+    rulesMap.put(new Pattern("test/isolate/badEnd/touch"), new TouchAction());
+    rulesMap.put(new Pattern("test/isolate/touch"), new TouchAction());
     rulesMap.put(new Pattern("test/hello"), new HelloAction());
 
     TrivialConfigurator tc = new TrivialConfigurator(rulesMap);
     tc.setContext(context);
-    tc .doConfigure(Constants.TEST_DIR_PREFIX
-            + "input/joran/skip/"+filename);
+    tc.doConfigure(Constants.TEST_DIR_PREFIX + "input/joran/skip/" + filename);
 
-    String str = context.getProperty("hello");
+    String str = context.getProperty(HelloAction.PROPERTY_KEY);
     assertEquals("Hello John Doe.", str);
 
-    Object i = (String) context.getObject(TouchAction.KEY);
-    assertNull(i);
-
-    StatusPrinter.print(context);
+    Integer i = (Integer) context.getObject(TouchAction.KEY);
+    if (expectedInt == null) {
+      assertNull(i);
+    } else {
+      assertEquals(expectedInt, i);
+    }
+
+    // check the existence of an ERROR status
+    List<Status> statusList = sm.getCopyOfStatusList();
+    Status s0 = statusList.get(0);
+    assertEquals(Status.ERROR, s0.getLevel());
+    assertTrue(s0.getThrowable().getClass() == exceptionClass);
   }
 
-  /**
-   * Tests that whenever an action throws a RuntimeException, processing of
-   * child elements is skipped.
-   */
   @Test
   public void testSkippingRuntimeExInBadBegin() throws Exception {
-    doTest("badBegin1.xml");
+    doTest("badBegin1.xml", null, IllegalStateException.class);
   }
 
-  /**
-   * Tests that whenever an action throws a RuntimeException, processing of
-   * child elements is skipped.
-   */
   @Test
   public void testSkippingActionExInBadBegin() throws Exception {
-    doTest("badBegin2.xml");
+    doTest("badBegin2.xml", null, ActionException.class);
   }
 
-  /**
-   * A RuntimeException thrown by the end() method of an action will be caught without
-   * further consequences (as there are no children).
-   */
   @Test
   public void testSkippingRuntimeExInBadEnd() throws Exception {
-    doTest("badEnd1.xml");
+    doTest("badEnd1.xml", new Integer(2), IllegalStateException.class);
   }
 
-  /**
-   * An ActionException thrown by the end() method of an action will be caught without
-   * further consequences (as there are no children).
-   */
   @Test
   public void testSkippingActionExInBadEnd() throws Exception {
-    doTest("badEnd2.xml");
+    doTest("badEnd2.xml", new Integer(2), ActionException.class);
   }
 }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/ext/BadEndAction.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/ext/BadEndAction.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/ext/BadEndAction.java	Sat Oct 18 21:09:55 2008
@@ -22,7 +22,7 @@
 public class BadEndAction extends Action {
 
   static String EXCEPTION_TYPE = "type";
-  static final int RUNTIME_EDXCEPTION = 0;
+  static final int RUNTIME_EXCEPTION = 0;
   static final int ACTION_EXCEPTION = 1;
   
   int type;
@@ -30,7 +30,7 @@
   
   public void begin(InterpretationContext ec, String name, Attributes attributes) {
     String exType = attributes.getValue(EXCEPTION_TYPE);
-    type = RUNTIME_EDXCEPTION;
+    type = RUNTIME_EXCEPTION;
     if("ActionException".equals(exType)) {
       type = ACTION_EXCEPTION;
     }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/ext/HelloAction.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/ext/HelloAction.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/action/ext/HelloAction.java	Sat Oct 18 21:09:55 2008
@@ -19,6 +19,8 @@
 public class HelloAction extends Action {
 
 
+  static final public String PROPERTY_KEY = "name";
+  
   public HelloAction() {
   }
   /**
@@ -27,7 +29,7 @@
    */
   public void begin(InterpretationContext ec, String name, Attributes attributes) {
     String str = "Hello "+attributes.getValue("name")+".";
-    ec.getContext().putProperty("hello", str);
+    ec.getContext().putProperty(PROPERTY_KEY, str);
   }
 
   /**


More information about the logback-dev mailing list