[logback-dev] svn commit: r754 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/joran/action main/java/ch/qos/logback/core/joran/spi test/java/ch/qos/logback/core/joran/replay test/java/ch/qos/logback/core/joran/spi

noreply.ceki at qos.ch noreply.ceki at qos.ch
Tue Oct 24 15:16:59 CEST 2006


Author: ceki
Date: Tue Oct 24 15:16:58 2006
New Revision: 754

Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedSimplePropertyIA.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/InterpretationContext.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/Pattern.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/SimpleRuleStore.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/replay/FruitConfigurationTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/PatternTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/SimpleStoreTest.java

Log:
- fixed bug where the pattern "a/b/*" incorrectly matched "a/other".
- also fixed bug whereby if there was no object at the top of the stack, NestedComponentIA
  and NestedSimplePropertyIA would barf with an EmptryStackException

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedSimplePropertyIA.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedSimplePropertyIA.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedSimplePropertyIA.java	Tue Oct 24 15:16:58 2006
@@ -45,6 +45,11 @@
     //LogLog.debug("in NestComponentIA.isApplicable <" + pattern + ">");
     String nestedElementTagName = pattern.peekLast();
 
+    // no point in attempting if there is no parent object
+    if(ec.isEmpty()) {
+      return false;
+    }
+    
     Object o = ec.peekObject();
     PropertySetter parentBean = new PropertySetter(o);
 

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/InterpretationContext.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/InterpretationContext.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/InterpretationContext.java	Tue Oct 24 15:16:58 2006
@@ -71,6 +71,10 @@
     return objectStack;
   }
 
+  public boolean isEmpty() {
+    return objectStack.isEmpty();
+  }
+
   public Object peekObject() {
     return objectStack.peek();
   }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/Pattern.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/Pattern.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/Pattern.java	Tue Oct 24 15:16:58 2006
@@ -154,7 +154,8 @@
       String l = (String) this.components.get(i);
       String r = (String) p.components.get(i);
 
-      if (l.equals(r) || "*".equals(l) || "*".equals(r)) {
+      //if (l.equals(r) || "*".equals(l) || "*".equals(r)) {
+      if (l.equals(r)) {
         match++;
       } else {
         break;

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/SimpleRuleStore.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/SimpleRuleStore.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/SimpleRuleStore.java	Tue Oct 24 15:16:58 2006
@@ -82,6 +82,7 @@
     } else if ( (actionList = tailMatch(currentPattern)) != null){
         return actionList;
     } else if ((actionList = prefixMatch(currentPattern)) != null) {
+      //System.out.println(currentPattern + " prefixMatches "+actionList);
       return actionList;
     } else {
       return null;
@@ -121,9 +122,11 @@
       String last = p.peekLast();
       if("*".equals(last)) {
         int r = currentPattern.getPrefixMatchLength(p);
-
-        //System.out.println("prefixMatch " +r);
-        if (r > max) {
+        
+        //System.out.println("r = "+ r + ", p= "+p);
+        
+        // to qualify the match length must equal p's size omitting the '*'
+        if ((r == p.size()-1) && (r > max)) {
           //System.out.println("New longest prefixMatch "+p);
           max = r;
           longestMatchingPattern = p;

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/replay/FruitConfigurationTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/replay/FruitConfigurationTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/replay/FruitConfigurationTest.java	Tue Oct 24 15:16:58 2006
@@ -8,6 +8,7 @@
 import junit.framework.TestSuite;
 import ch.qos.logback.core.joran.action.Action;
 import ch.qos.logback.core.joran.action.NOPAction;
+import ch.qos.logback.core.joran.spi.JoranException;
 import ch.qos.logback.core.joran.spi.Pattern;
 import ch.qos.logback.core.util.Constants;
 import ch.qos.logback.core.util.StatusPrinter;
@@ -30,19 +31,24 @@
 
   public List<FruitShell> doFirstPart(String filename) throws Exception {
 
-    HashMap<Pattern, Action> rulesMap = new HashMap<Pattern, Action>();
-    rulesMap.put(new Pattern("group/fruitShell"), new FruitShellAction());
-    rulesMap.put(new Pattern("group/fruitShell/fruit"), new FruitFactoryAction());
-    rulesMap.put(new Pattern("group/fruitShell/fruit/*"), new NOPAction());
-    SimpleConfigurator simpleConfigurator = new SimpleConfigurator(rulesMap);
-
-    simpleConfigurator.setContext(fruitContext);
-
-    simpleConfigurator.doConfigure(Constants.TEST_DIR_PREFIX + "input/joran/" + filename);
-
-    StatusPrinter.print(fruitContext);
-    return fruitContext.getFruitShellList();
-
+    try {
+      HashMap<Pattern, Action> rulesMap = new HashMap<Pattern, Action>();
+      rulesMap.put(new Pattern("group/fruitShell"), new FruitShellAction());
+      rulesMap.put(new Pattern("group/fruitShell/fruit"),
+          new FruitFactoryAction());
+      rulesMap.put(new Pattern("group/fruitShell/fruit/*"), new NOPAction());
+      SimpleConfigurator simpleConfigurator = new SimpleConfigurator(rulesMap);
+
+      simpleConfigurator.setContext(fruitContext);
+
+      simpleConfigurator.doConfigure(Constants.TEST_DIR_PREFIX + "input/joran/"
+          + filename);
+
+      return fruitContext.getFruitShellList();
+    } catch (Exception je) {
+      StatusPrinter.print(fruitContext);
+      throw je;
+    }
   }
 
   public void test1() throws Exception {
@@ -69,7 +75,7 @@
     Fruit fruit0 = fs0.fruitFactory.buildFruit();
     assertTrue(fruit0 instanceof Fruit);
     assertEquals("blue", fruit0.getName());
-    
+
     FruitShell fs1 = fsList.get(1);
     assertNotNull(fs1);
     assertEquals("fs1", fs1.getName());
@@ -78,7 +84,7 @@
     assertEquals("orange", fruit1.getName());
     assertEquals(1.2, ((WeightytFruit) fruit1).getWeight());
   }
-  
+
   public void testWithSubst() throws Exception {
     List<FruitShell> fsList = doFirstPart("fruitWithSubst.xml");
     assertNotNull(fsList);
@@ -90,13 +96,13 @@
     int oldCount = FruitFactory.count;
     Fruit fruit0 = fs0.fruitFactory.buildFruit();
     assertTrue(fruit0 instanceof WeightytFruit);
-    assertEquals("orange-"+oldCount, fruit0.getName());
+    assertEquals("orange-" + oldCount, fruit0.getName());
     assertEquals(1.2, ((WeightytFruit) fruit0).getWeight());
   }
-  
+
   public static Test suite() {
     TestSuite suite = new TestSuite();
-    //suite.addTest(new FruitConfigurationTest("testWithSubst"));
+    // suite.addTest(new FruitConfigurationTest("testWithSubst"));
     suite.addTestSuite(FruitConfigurationTest.class);
     return suite;
   }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/PatternTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/PatternTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/PatternTest.java	Tue Oct 24 15:16:58 2006
@@ -132,7 +132,7 @@
     {
       Pattern p = new Pattern("/a/b");
       Pattern rulePattern = new Pattern("/a/*");
-      assertEquals(2, p.getPrefixMatchLength(rulePattern));
+      assertEquals(1, p.getPrefixMatchLength(rulePattern));
     }
     
     {
@@ -144,7 +144,7 @@
     {
       Pattern p = new Pattern("/a/b");
       Pattern rulePattern = new Pattern("/*");
-      assertEquals(1, p.getPrefixMatchLength(rulePattern));
+      assertEquals(0, p.getPrefixMatchLength(rulePattern));
     }
   }
 

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/SimpleStoreTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/SimpleStoreTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/SimpleStoreTest.java	Tue Oct 24 15:16:58 2006
@@ -72,14 +72,13 @@
     // jp.parse(doc);
   }
 
-  public void test2() throws Exception {
+  public void testTail1() throws Exception {
     SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
     srs.addRule(new Pattern("*/b"), new XAction());
 
     List r = srs.matchActions(new Pattern("a/b"));
     assertNotNull(r);
 
-    // System.out.println(r);
     assertEquals(1, r.size());
 
     if (!(r.get(0) instanceof XAction)) {
@@ -87,7 +86,22 @@
     }
   }
 
-  public void test3() throws Exception {
+  
+  public void testTail2() throws Exception {
+    SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
+    srs.addRule(new Pattern("*/c"), new XAction());
+
+    List r = srs.matchActions(new Pattern("a/b/c"));
+    assertNotNull(r);
+
+    assertEquals(1, r.size());
+
+    if (!(r.get(0) instanceof XAction)) {
+      fail("Wrong type");
+    }
+  }
+  
+  public void testTail3() throws Exception {
     SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
     srs.addRule(new Pattern("*/b"), new XAction());
     srs.addRule(new Pattern("*/a/b"), new YAction());
@@ -103,7 +117,7 @@
     }
   }
 
-  public void test4() throws Exception {
+  public void testTail4() throws Exception {
     SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
     srs.addRule(new Pattern("*/b"), new XAction());
     srs.addRule(new Pattern("*/a/b"), new YAction());
@@ -127,16 +141,55 @@
 
     List r = srs.matchActions(new Pattern("a/b"));
     assertNotNull(r);
+    assertEquals(1, r.size());
+    assertTrue(r.get(0) instanceof YAction);
+  }
+  
+  public void testDeepSuffix() throws Exception {
+    SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
+    srs.addRule(new Pattern("a"), new XAction(1));
+    srs.addRule(new Pattern("a/b/*"), new XAction(2));
+
+    List r = srs.matchActions(new Pattern("a/other"));
+    assertNull(r);
+  }
+
+  public void testPrefixSuffixInteraction1() throws Exception {
+    SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
+    srs.addRule(new Pattern("a"), new ZAction());
+    srs.addRule(new Pattern("a/*"), new YAction());
+    srs.addRule(new Pattern("*/a/b"), new XAction(3));
+
+    List r = srs.matchActions(new Pattern("a/b"));
+    assertNotNull(r);
 
     assertEquals(1, r.size());
+    
+    assertTrue(r.get(0) instanceof XAction);
+    XAction xaction = (XAction) r.get(0);
+    assertEquals(3, xaction.id);
+  }
 
-    if (!(r.get(0) instanceof YAction)) {
-      fail("Wrong type");
-    }
+  public void testPrefixSuffixInteraction2() throws Exception {
+    SimpleRuleStore srs = new SimpleRuleStore(new ContextBase());
+    srs.addRule(new Pattern("testGroup"), new XAction());
+    srs.addRule(new Pattern("testGroup/testShell"), new YAction());
+    srs.addRule(new Pattern("testGroup/testShell/test"), new ZAction());
+    srs.addRule(new Pattern("testGroup/testShell/test/*"), new XAction(9));
+    
+    List r = srs.matchActions(new Pattern("testGroup/testShell/toto"));
+    System.out.println(r);
+    assertNull(r);
   }
   
-
   class XAction extends Action {
+    int id = 0;
+    XAction() {
+    }
+    XAction(int id) {
+      this.id = id;
+    }
+
     public void begin(InterpretationContext ec, String name, Attributes attributes) {
     }
 
@@ -145,6 +198,10 @@
 
     public void finish(InterpretationContext ec) {
     }
+    
+    public String toString() {
+     return "XAction("+id+")";
+    }    
   }
 
   class YAction extends Action {
@@ -168,4 +225,5 @@
     public void finish(InterpretationContext ec) {
     }
   }
+
 }



More information about the logback-dev mailing list