[logback-dev] svn commit: r1651 - in logback/trunk: logback-classic/src/test logback-classic/src/test/input/joran logback-classic/src/test/java/ch/qos/logback/classic logback-classic/src/test/java/ch/qos/logback/classic/joran logback-core/src/main/java/ch/qos/logback/core/joran/spi logback-core/src/main/java/ch/qos/logback/core/spi logback-core/src/main/java/ch/qos/logback/core/util logback-core/src/test/java/ch/qos/logback/core/util

noreply.ceki at qos.ch noreply.ceki at qos.ch
Wed Mar 19 21:39:37 CET 2008


Author: ceki
Date: Wed Mar 19 21:39:37 2008
New Revision: 1651

Added:
   logback/trunk/logback-classic/src/test/input/joran/evaluatorFilter.xml
   logback/trunk/logback-classic/src/test/input/joran/levelFilter.xml
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java
      - copied, changed from r1638, /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/BasicJoranTest.java
Removed:
   logback/trunk/logback-classic/src/test/build.xml
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/BasicJoranTest.java
Modified:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/StringListAppender.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/PackageTest.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/Interpreter.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwareImpl.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/PropertySetter.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PropertySetterTest.java

Log:
- added basic test for level and evaluatorFilter
- PropertySetter can now deal with *any* enum type, not just FilterReply

Added: logback/trunk/logback-classic/src/test/input/joran/evaluatorFilter.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/input/joran/evaluatorFilter.xml	Wed Mar 19 21:39:37 2008
@@ -0,0 +1,20 @@
+<configuration>
+
+  <appender name="LIST"
+             class="ch.qos.logback.core.read.ListAppender">
+
+    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
+      <Name>myFilter</Name>
+      <OnMatch>DENY</OnMatch>
+      <Evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
+        <Name>mdcEvaluator</Name>
+        <Expression>"to be ignored".equals(message)</Expression>
+      </Evaluator>
+    </filter>
+  </appender>
+
+  <root>
+    <level value="debug" />
+    <appender-ref ref="LIST" />
+  </root>
+</configuration>
\ No newline at end of file

Added: logback/trunk/logback-classic/src/test/input/joran/levelFilter.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/input/joran/levelFilter.xml	Wed Mar 19 21:39:37 2008
@@ -0,0 +1,16 @@
+<configuration>
+
+  <appender name="LIST"
+            class="ch.qos.logback.core.read.ListAppender">
+
+    <filter class="ch.qos.logback.classic.filter.LevelFilter">
+      <OnMatch>DENY</OnMatch>
+      <level>ERROR</level>
+    </filter>
+  </appender>
+
+  <root>
+    <level value="debug" />
+    <appender-ref ref="LIST" />
+  </root>
+</configuration>
\ No newline at end of file

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/StringListAppender.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/StringListAppender.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/StringListAppender.java	Wed Mar 19 21:39:37 2008
@@ -38,7 +38,6 @@
 
   public void setLayout(Layout<LoggingEvent> layout) {
     this.layout = layout;
-
   }
 
 }

Copied: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java (from r1638, /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/BasicJoranTest.java)
==============================================================================
--- /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/BasicJoranTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java	Wed Mar 19 21:39:37 2008
@@ -12,6 +12,7 @@
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
+import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.Logger;
 import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.StringListAppender;
@@ -22,10 +23,11 @@
 import ch.qos.logback.classic.util.TeztConstants;
 import ch.qos.logback.core.joran.spi.JoranException;
 import ch.qos.logback.core.read.ListAppender;
+import ch.qos.logback.core.util.StatusPrinter;
 
-public class BasicJoranTest extends TestCase {
+public class JoranConfiguratorTest extends TestCase {
 
-  public BasicJoranTest(String name) {
+  public JoranConfiguratorTest(String name) {
     super(name);
   }
   
@@ -118,19 +120,61 @@
     DebugUsersTurboFilter dutf = (DebugUsersTurboFilter)filter;
     assertEquals(2, dutf.getUsers().size());
   }
+
   
-  public void test() {
+  public void testLevelFilter() throws JoranException {
+    JoranConfigurator jc = new JoranConfigurator();
+    LoggerContext loggerContext = new LoggerContext();
+    jc.setContext(loggerContext);
+    jc.doConfigure(TeztConstants.TEST_DIR_PREFIX
+        + "input/joran/levelFilter.xml");
+
+    StatusPrinter.print(loggerContext);
+
+    Logger logger = loggerContext.getLogger(this.getClass().getName());
+    logger.warn("hello");
+    logger.error("to be ignored");
     
+    @SuppressWarnings("unchecked")
+    ListAppender<LoggingEvent> listAppender = (ListAppender) loggerContext
+        .getLogger("root").getAppender("LIST");
+    
+    assertNotNull(listAppender);
+    assertEquals(1, listAppender.list.size());
+    LoggingEvent back = listAppender.list.get(0);
+    assertEquals(Level.WARN, back.getLevel());
+    assertEquals("hello", back.getMessage());
   }
-  // COMMENTED_OUT_
-  public static Test suite() {
-    TestSuite suite = new TestSuite();
-    //suite.addTestSuite(BasicJoranTest.class);
+  
+  public void testEvaluatorFilter() throws JoranException {
+    JoranConfigurator jc = new JoranConfigurator();
+    LoggerContext loggerContext = new LoggerContext();
+    jc.setContext(loggerContext);
+    jc.doConfigure(TeztConstants.TEST_DIR_PREFIX
+        + "input/joran/evaluatorFilter.xml");
+
+    StatusPrinter.print(loggerContext);
+
+    Logger logger = loggerContext.getLogger(this.getClass().getName());
+    logger.warn("hello");
+    logger.error("to be ignored");
     
-    suite.addTest(new BasicJoranTest("testLevel"));
+    @SuppressWarnings("unchecked")
+    ListAppender<LoggingEvent> listAppender = (ListAppender) loggerContext
+        .getLogger("root").getAppender("LIST");
     
-    //suite.addTest(new BasicJoranTest("testSimpleList"));
-
+    assertNotNull(listAppender);
+    assertEquals(1, listAppender.list.size());
+    LoggingEvent back = listAppender.list.get(0);
+    assertEquals(Level.WARN, back.getLevel());
+    assertEquals("hello", back.getMessage());
+  }
+  
+  
+  public static Test suite() {
+    TestSuite suite = new TestSuite();
+    suite.addTestSuite(JoranConfiguratorTest.class);
+    //suite.addTest(new JoranConfiguratorTest("testEvaluatorFilter"));
     return suite;
   } 
 }

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/PackageTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/PackageTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/PackageTest.java	Wed Mar 19 21:39:37 2008
@@ -15,7 +15,7 @@
 
   public static Test suite() {
     TestSuite suite = new TestSuite();
-    suite.addTestSuite(BasicJoranTest.class);
+    suite.addTestSuite(JoranConfiguratorTest.class);
     suite.addTestSuite(EvaluatorJoranTest.class);
     return suite;
   }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/Interpreter.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/Interpreter.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/Interpreter.java	Wed Mar 19 21:39:37 2008
@@ -66,7 +66,7 @@
   final private RuleStore ruleStore;
   final private InterpretationContext ec;
   final private ArrayList<ImplicitAction> implicitActions;
-  final private ContextAwareImpl cai;
+  final private CAI_WithLocatorSupport cai;
   Pattern pattern;
   Locator locator;
   EventPlayer player;
@@ -88,7 +88,7 @@
   Pattern skip = null;
 
   public Interpreter(Context context, RuleStore rs) {
-    this.cai = new ContextAwareImpl(this);
+    this.cai = new CAI_WithLocatorSupport(this);
     this.cai.setContext(context);
     ruleStore = rs; 
     ec = new InterpretationContext(context, this);
@@ -342,3 +342,28 @@
     }
   }
 }
+
+/**
+ * When {@link Interpreter} class is used as the origin of an 
+ * {@link ContextAwareImpl} instance, then XML locator information
+ * is lost. This class preserves locator information (as a string).
+ * 
+ * @author ceki
+ */
+class CAI_WithLocatorSupport extends ContextAwareImpl {
+  
+  CAI_WithLocatorSupport(Interpreter interpreter) {
+    super(interpreter);
+  }
+  
+  @Override
+  protected Object getOrigin() {
+    Interpreter i = (Interpreter) super.getOrigin();
+    Locator locator = i.locator;
+    if(locator != null) {
+      return Interpreter.class.getName()+"@"+locator.getLineNumber()+":"+locator.getColumnNumber();
+    } else {
+      return Interpreter.class.getName()+"@NA:NA";
+    }
+  }
+}

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwareImpl.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwareImpl.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwareImpl.java	Wed Mar 19 21:39:37 2008
@@ -23,7 +23,7 @@
  * 
  * @author Ceki G&uumllc&uuml;
  */
-final public class ContextAwareImpl implements ContextAware {
+public class ContextAwareImpl implements ContextAware {
 
   private int noContextWarning = 0;
   protected Context context;
@@ -33,6 +33,10 @@
     this.origin = origin;
   }
   
+  protected Object getOrigin() {
+    return origin;
+  }
+  
   public void setContext(Context context) {
     if (this.context == null) {
       this.context = context;
@@ -66,27 +70,27 @@
   }
 
   public void addInfo(String msg) {
-    addStatus(new InfoStatus(msg, origin));
+    addStatus(new InfoStatus(msg, getOrigin()));
   }
 
   public void addInfo(String msg, Throwable ex) {
-    addStatus(new InfoStatus(msg, origin, ex));
+    addStatus(new InfoStatus(msg, getOrigin(), ex));
   }
 
   public void addWarn(String msg) {
-    addStatus(new WarnStatus(msg, origin));
+    addStatus(new WarnStatus(msg, getOrigin()));
   }
 
   public void addWarn(String msg, Throwable ex) {
-    addStatus(new WarnStatus(msg, origin, ex));
+    addStatus(new WarnStatus(msg, getOrigin(), ex));
   }
 
   public void addError(String msg) {
-    addStatus(new ErrorStatus(msg, origin));
+    addStatus(new ErrorStatus(msg, getOrigin()));
   }
 
   public void addError(String msg, Throwable ex) {
-    addStatus(new ErrorStatus(msg, origin, ex));
+    addStatus(new ErrorStatus(msg, getOrigin(), ex));
   }
 
 }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/PropertySetter.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/PropertySetter.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/PropertySetter.java	Wed Mar 19 21:39:37 2008
@@ -25,7 +25,7 @@
 import java.lang.reflect.Method;
 
 import ch.qos.logback.core.spi.ContextAwareBase;
-import ch.qos.logback.core.spi.FilterReply;
+
 
 /**
  * General purpose Object property setter. Clients repeatedly invokes
@@ -54,7 +54,10 @@
   private static final int X_NOT_FOUND = 0;
   private static final int X_AS_COMPONENT = 1;
   private static final int X_AS_PROPERTY = 2;
+  
+  private static final Class[] STING_CLASS_PARAMETER = new Class[] {String.class};
 
+  
   protected Object obj;
   protected Class objClass;
   protected PropertyDescriptor[] propertyDescriptors;
@@ -237,7 +240,7 @@
         return X_AS_PROPERTY;
       } else if (FileSize.class.isAssignableFrom(clazz)) {
         return X_AS_PROPERTY;
-      } else if (FilterReply.class.isAssignableFrom(clazz)){
+      } else if (clazz.isEnum()){
         return X_AS_PROPERTY;
       } else {
         return X_AS_COMPONENT;
@@ -367,7 +370,7 @@
     }
   }
 
-  String capitalizeFirstLetter(String name) {
+  private String capitalizeFirstLetter(String name) {
     return name.substring(0, 1).toUpperCase() + name.substring(1);
   }
 
@@ -401,13 +404,23 @@
       return Duration.valueOf(val);
     } else if (FileSize.class.isAssignableFrom(type)) {
       return FileSize.valueOf(val);
-    } else if (FilterReply.class.isAssignableFrom(type)) {
-      return FilterReply.valueOf(v);
+    } else if(type.isEnum()) {
+      return convertEnum(val, type);
     }
 
     return null;
   }
 
+  protected Object convertEnum(String val, Class type) {
+    try {
+      Method m = type.getMethod("valueOf", STING_CLASS_PARAMETER);
+      return m.invoke(null, val);
+    } catch (Exception e) {
+      addError("Failed to convert value ["+val+"] to enum ["+type.getName()+"]", e);
+    }
+    return null;
+  }
+  
   protected Method getMethod(String methodName) {
     if (methodDescriptors == null) {
       introspect();

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PropertySetterTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PropertySetterTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PropertySetterTest.java	Wed Mar 19 21:39:37 2008
@@ -29,7 +29,10 @@
     assertEquals(ContainmentType.AS_COMPONENT_COLLECTION, setter.canContainComponent("Window"));
     assertEquals(ContainmentType.AS_PROPERTY_COLLECTION, setter.canContainComponent("adjective"));
     
+    assertEquals(ContainmentType.AS_SINGLE_PROPERTY, setter.canContainComponent("filterReply"));
+    assertEquals(ContainmentType.AS_SINGLE_PROPERTY, setter.canContainComponent("houseColor"));
     
+    System.out.println();
   }
 
   public void testSetProperty() {
@@ -136,8 +139,7 @@
     House house = new House();
     PropertySetter setter = new PropertySetter(house);
     setter.setProperty("houseColor", "BLUE");
-    //TODO fails for now
-    //assertEquals(HouseColor.BLUE, house.getHouseColor());
+    assertEquals(HouseColor.BLUE, house.getHouseColor());
   }
 }
 



More information about the logback-dev mailing list