[slf4j-dev] svn commit: r890 - in slf4j/trunk/slf4j-converter/src: main/java/org/slf4j/converter test/java/org/slf4j/converter

ceki at slf4j.org ceki at slf4j.org
Mon Sep 3 23:35:44 CEST 2007


Author: ceki
Date: Mon Sep  3 23:35:43 2007
New Revision: 890

Added:
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/MultiGroupConversionRule.java
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/SingleConversionRule.java
      - copied, changed from r889, /slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConversionRule.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/AllTest.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/PackageTest.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/TrivialMatcherTest.java
      - copied, changed from r889, /slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/SimpleMatcherTest.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/TriviialMatcher.java
Removed:
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConversionRule.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/SimpleMatcherTest.java
Modified:
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/JCLMatcher.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/JCLMatcherTest.java

Log:
In pursuit of a better clarity, I've made the following changes to the
converter code.

I've split ConversionRule into an interface and two implementations:
SingleConversionRule and MultiGroupConversionRule. As its name
indicates, SingleConversionRule admits a single group, the entire
matching text, and replaces it with text specified by the developer.

MultiGroupConversion admits multiple groups with a replacement text
specific for each group. I've removed the map associating indexes with
a replacement text. Instead, MultiGroupConversion uses a bounded array
of replacement texts. I believe the result to be somewhat easier to
follow and to understand -- in particular, the replace(Matcher) method
in both implementations.

Note the addition of AllTest and PackageTest enabling the lauch of
all test in one click. Admittedly, there are only two test classes 
for the time being.


Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/JCLMatcher.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/JCLMatcher.java	(original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/JCLMatcher.java	Mon Sep  3 23:35:43 2007
@@ -18,66 +18,66 @@
 
   protected void initRules() {
     // matching : import org.apache.commons.logging.LogFactory;
-    ConversionRule cr0 = new ConversionRule(Pattern
-        .compile("import\\s*+org.apache.commons.logging.LogFactory;"));
-    cr0.addReplacement(Constant.INDEX_0, "import org.slf4j.LoggerFactory;");
+    SingleConversionRule cr0 = new SingleConversionRule(Pattern
+        .compile("import\\s*+org.apache.commons.logging.LogFactory;"),
+        "import org.slf4j.LoggerFactory;");
 
     // matching : import org.apache.commons.logging.Log;
-    ConversionRule cr1 = new ConversionRule(Pattern
-        .compile("import\\s*+org.apache.commons.logging.Log;"));
-    cr1.addReplacement(Constant.INDEX_0, "import org.slf4j.Logger;");
+    SingleConversionRule cr1 = new SingleConversionRule(Pattern
+        .compile("import\\s*+org.apache.commons.logging.Log;"), 
+        "import org.slf4j.Logger;");
 
     // matching declaration and instanciation : protected Log myLog =
     // LogFactory.getFactory().getInstance(MyClass.class); //comment or other
     // instruction
-    ConversionRule cr2 = new ConversionRule(
+    MultiGroupConversionRule cr2 = new MultiGroupConversionRule(
         Pattern
             .compile("((\\w*+\\W*+)*)(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
-    cr2.addReplacement(Constant.INDEX_3, "Logger");
-    cr2.addReplacement(Constant.INDEX_2, "");
-    cr2.addReplacement(Constant.INDEX_5, "LoggerFactory.getLogger(");
+    cr2.addReplacement(3, "Logger");
+    cr2.addReplacement(2, "");
+    cr2.addReplacement(5, "LoggerFactory.getLogger(");
 
     // matching declaration and instanciation : protected static Log myLog =
     // LogFactory.getLog(MyClass.class); //comment or other instruction
-    ConversionRule cr3 = new ConversionRule(
+    MultiGroupConversionRule cr3 = new MultiGroupConversionRule(
         Pattern
             .compile("((\\w*+\\W*+)*)(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
-    cr3.addReplacement(Constant.INDEX_3, "Logger");
-    cr3.addReplacement(Constant.INDEX_2, "");
-    cr3.addReplacement(Constant.INDEX_5, "LoggerFactory.getLogger(");
+    cr3.addReplacement(2, "");
+    cr3.addReplacement(3, "Logger");
+    cr3.addReplacement(5, "LoggerFactory.getLogger(");
 
     // matching instanciation without declaration : myLog =
     // LogFactory.getFactory().getInstance(MyClass.class); //comment or other
     // instruction
-    ConversionRule cr4 = new ConversionRule(
+    MultiGroupConversionRule cr4 = new MultiGroupConversionRule(
         Pattern
             .compile("((\\w*+\\W*+)*)(\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
-    cr4.addReplacement(Constant.INDEX_4, "LoggerFactory.getLogger(");
-    cr4.addReplacement(Constant.INDEX_2, "");
+    cr4.addReplacement(4, "LoggerFactory.getLogger(");
+    cr4.addReplacement(2, "");
 
     // matching instanciation without declaration : myLog =
     // LogFactory.getLog(MyClass.class); //comment or other instruction
-    ConversionRule cr5 = new ConversionRule(
+    MultiGroupConversionRule cr5 = new MultiGroupConversionRule(
         Pattern
             .compile("((\\w*+\\W*+)*)(\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
-    cr5.addReplacement(Constant.INDEX_4, "LoggerFactory.getLogger(");
-    cr5.addReplacement(Constant.INDEX_2, "");
+    cr5.addReplacement(4, "LoggerFactory.getLogger(");
+    cr5.addReplacement(2, "");
 
     // matching declaration without instanciation : public static final Log
     // myLog //comment or other instruction
-    ConversionRule cr6 = new ConversionRule(Pattern
+    MultiGroupConversionRule cr6 = new MultiGroupConversionRule(Pattern
         .compile("((\\w*+\\W*+)*)(Log)(\\s*+\\w+\\s*+;)((\\w*+\\W*+)*)"));
-    cr6.addReplacement(Constant.INDEX_3, "Logger");
-    cr6.addReplacement(Constant.INDEX_2, "");
+    cr6.addReplacement(3, "Logger");
+    cr6.addReplacement(2, "");
 
     // matching incomplete instanciation : protected Log log =
-    ConversionRule cr7 = new ConversionRule(Pattern
+    MultiGroupConversionRule cr7 = new MultiGroupConversionRule(Pattern
         .compile("((\\w*+\\W*+)*)(Log)(\\s+\\w+\\s*+=*\\s*+)"));
     cr7.addReplacement(Constant.INDEX_3, "Logger");
     cr7.addReplacement(Constant.INDEX_2, "");
 
     // matching incomlete instanciation : LogFactory.getLog(MyComponent.class);
-    ConversionRule cr8 = new ConversionRule(
+    MultiGroupConversionRule cr8 = new MultiGroupConversionRule(
         Pattern
             .compile("((\\w*+\\W*+)*)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
     cr8.addReplacement(Constant.INDEX_3, "LoggerFactory.getLogger(");
@@ -85,7 +85,7 @@
 
     // matching incomlete instanciation :
     // LogFactory.getFactory().getInstance(MyComponent.class);
-    ConversionRule cr9 = new ConversionRule(
+    MultiGroupConversionRule cr9 = new MultiGroupConversionRule(
         Pattern
             .compile("((\\w*+\\W*+)*)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
     cr9.addReplacement(Constant.INDEX_3, "LoggerFactory.getLogger(");

Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/MultiGroupConversionRule.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/MultiGroupConversionRule.java	Mon Sep  3 23:35:43 2007
@@ -0,0 +1,90 @@
+/* 
+ * Copyright (c) 2004-2007 QOS.ch
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+package org.slf4j.converter;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * This class represents a conversion rule It uses a Pattern and defines for
+ * each capturing group of this Pattern a replacement text
+ * 
+ * @author jean-noelcharpin
+ * 
+ */
+public class MultiGroupConversionRule implements ConversionRule {
+ 
+  // It is extremely unlikely to encounter more than 10 groups in one of 
+  // our conversion reg-expressions
+  final private static int MAX_GROUPS = 10;
+
+  private Pattern pattern;
+  private String[] replacementTable = new String[MAX_GROUPS];
+
+  public MultiGroupConversionRule(Pattern pattern) {
+    this.pattern = pattern;
+  }
+
+  /* (non-Javadoc)
+   * @see org.slf4j.converter.ConversionRule#getPattern()
+   */
+  public Pattern getPattern() {
+    return pattern;
+  }
+
+  public void addReplacement(int groupIndex, String replacement) {
+    if(groupIndex == 0) {
+      throw new IllegalArgumentException("regex groups start at 1, not zero");
+    }
+    replacementTable[groupIndex] = replacement;
+  }
+
+  /* (non-Javadoc)
+   * @see org.slf4j.converter.ConversionRule#getReplacement(java.lang.Integer)
+   */
+  public String getReplacement(int groupIndex) {
+    return  replacementTable[groupIndex];
+  }
+
+  /* (non-Javadoc)
+   * @see org.slf4j.converter.ConversionRule#replace(java.util.regex.Matcher)
+   */
+  public String replace(Matcher matcher) {
+    StringBuffer replacementBuffer = new StringBuffer();
+    String replacementText;
+    
+    for (int group = 1; group <= matcher.groupCount(); group++) {
+      replacementText = getReplacement(group);
+      if (replacementText != null) {
+        System.out.println("replacing group " + group + " : "
+            + matcher.group(group) + " with " + replacementText);
+        replacementBuffer.append(replacementText);
+      } else  {
+        replacementBuffer.append(matcher.group(group));
+      }
+    }
+    return replacementBuffer.toString();
+  }
+}

Copied: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/SingleConversionRule.java (from r889, /slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConversionRule.java)
==============================================================================
--- /slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConversionRule.java	(original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/SingleConversionRule.java	Mon Sep  3 23:35:43 2007
@@ -1,6 +1,29 @@
+/* 
+ * Copyright (c) 2004-2007 QOS.ch
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
 package org.slf4j.converter;
 
-import java.util.HashMap;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -11,48 +34,29 @@
  * @author jean-noelcharpin
  * 
  */
-public class ConversionRule {
+public class SingleConversionRule implements ConversionRule {
 
-  private Pattern pattern;
+  final  private Pattern pattern;
+  final private String replacementText ;
 
-  private HashMap<Integer, String> replacementMap;
-
-  public ConversionRule(Pattern pattern) {
+  public SingleConversionRule(Pattern pattern, String replacementText) {
     this.pattern = pattern;
-    this.replacementMap = new HashMap<Integer, String>();
+    this.replacementText = replacementText;
   }
 
+  /* (non-Javadoc)
+   * @see org.slf4j.converter.ConversionRule#getPattern()
+   */
   public Pattern getPattern() {
     return pattern;
   }
 
-  public void addReplacement(Integer groupIndex, String replacement) {
-    replacementMap.put(groupIndex, replacement);
-  }
 
-  public String getReplacement(Integer groupIndex) {
-    return replacementMap.get(groupIndex);
-  }
 
-  /**
-   * Given replacement rules, replace each capturing group in matcher's pattern
-   * 
-   * @param matcher
-   * @return String
+  /* (non-Javadoc)
+   * @see org.slf4j.converter.ConversionRule#replace(java.util.regex.Matcher)
    */
   public String replace(Matcher matcher) {
-    StringBuffer replacementBuffer = new StringBuffer();
-    String replacementText;
-    for (int group = 0; group <= matcher.groupCount(); group++) {
-      replacementText = getReplacement(group);
-      if (replacementText != null) {
-        System.out.println("replacing group " + group + " : "
-            + matcher.group(group) + " with " + replacementText);
-        replacementBuffer.append(replacementText);
-      } else if (group > 0) {
-        replacementBuffer.append(matcher.group(group));
-      }
-    }
-    return replacementBuffer.toString();
+    return replacementText;
   }
 }

Added: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/AllTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/AllTest.java	Mon Sep  3 23:35:43 2007
@@ -0,0 +1,39 @@
+/* 
+ * Copyright (c) 2004-2007 QOS.ch
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+package org.slf4j.converter;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class AllTest extends TestCase {
+
+  public static Test suite() {
+    TestSuite suite = new TestSuite();
+    suite.addTest(org.slf4j.converter.PackageTest.suite());
+
+    return suite;
+  }
+}

Modified: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/JCLMatcherTest.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/JCLMatcherTest.java	(original)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/JCLMatcherTest.java	Mon Sep  3 23:35:43 2007
@@ -9,13 +9,11 @@
   public void testImportReplacement() {
     JCLMatcher jclMatcher = new JCLMatcher();
     // LogFactory import replacement
-    assertEquals(jclMatcher
-        .getReplacement("import org.apache.commons.logging.LogFactory;"),
-        "import org.slf4j.LoggerFactory;");
+    assertEquals("import org.slf4j.LoggerFactory;", jclMatcher
+        .getReplacement("import org.apache.commons.logging.LogFactory;"));
     // Log import replacement
-    assertEquals(jclMatcher
-        .getReplacement("import org.apache.commons.logging.Log;"),
-        "import org.slf4j.Logger;");
+    assertEquals("import org.slf4j.Logger;", jclMatcher
+        .getReplacement("import org.apache.commons.logging.Log;"));
   }
 
   public void testLogFactoryGetLogReplacement() {

Added: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/PackageTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/PackageTest.java	Mon Sep  3 23:35:43 2007
@@ -0,0 +1,39 @@
+/* 
+ * Copyright (c) 2004-2007 QOS.ch
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+package org.slf4j.converter;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class PackageTest extends TestCase {
+
+  public static Test suite() {
+    TestSuite suite = new TestSuite();
+    suite.addTestSuite(TrivialMatcherTest.class);
+    suite.addTestSuite(JCLMatcherTest.class);
+    return suite;
+  }
+}

Copied: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/TrivialMatcherTest.java (from r889, /slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/SimpleMatcherTest.java)
==============================================================================
--- /slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/SimpleMatcherTest.java	(original)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/TrivialMatcherTest.java	Mon Sep  3 23:35:43 2007
@@ -1,50 +1,26 @@
 package org.slf4j.converter;
 
-import java.util.ArrayList;
-import java.util.regex.Pattern;
 
 import junit.framework.TestCase;
 
-public class SimpleMatcherTest  extends TestCase {
+public class TrivialMatcherTest  extends TestCase {
 
   
   
-  private class SimpleMatcher extends AbstractMatcher{
- 
-    protected void initRules() {
-      //simple rule no capturing group is defined, we use default capturing group which is group zero
-      ConversionRule cr = new ConversionRule(Pattern.compile("import org.slf4j.converter"));
-      //we define an unique replacement text for group 0
-      cr.addReplacement(Constant.INDEX_0, "simple replacement with an unique capturing group");
-      
-      //we define 4 differents capturing groups
-      ConversionRule cr1 = new ConversionRule(Pattern.compile("(first group)( second group)( third group)( 4th group)"));
-      //group zero is ignored during treatment
-      //replacement for the first
-      cr1.addReplacement(Constant.INDEX_1, "1st group");
-      //no replacement for the second group it will remains the same
-      //empty string for the third group it will be deleted
-      cr1.addReplacement(Constant.INDEX_3, "");
-      //no replacement for the third group it will remains the same
-      
-      rules = new ArrayList<ConversionRule>();
-      rules.add(cr);
-      rules.add(cr1);
-    }
-    
-  }
-  
-  
-  
   public void testSimpleReplacement() {
-    SimpleMatcher simpleMatcher = new SimpleMatcher();
-    simpleMatcher.initRules();
+    TrivialMatcher trivialMatcher = new TrivialMatcher();
+    trivialMatcher.initRules();
     
-    assertEquals(simpleMatcher.getReplacement("no replacement for this text, it remains the same!"),"no replacement for this text, it remains the same!");
+    //
+    //assertEquals("no replacement for this text, it remains the same!", 
+    //    simpleMatcher.getReplacement("no replacement for this text, it remains the same!"));
     
-    assertEquals(simpleMatcher.getReplacement("import org.slf4j.converter"),"simple replacement with an unique capturing group");
+    // "import org.slf4j.converter" -- > simple replacement with an unique capturing group
+    assertEquals("simple replacement with an unique capturing group", 
+        trivialMatcher.getReplacement("import org.slf4j.converter"));
     
-    assertEquals(simpleMatcher.getReplacement("first group second group third group 4th group"),"1st group second group 4th group");
+    assertEquals("1st group second group 4th group", 
+        trivialMatcher.getReplacement("first group second group third group 4th group"));
     
   }
   

Added: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/TriviialMatcher.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/TriviialMatcher.java	Mon Sep  3 23:35:43 2007
@@ -0,0 +1,52 @@
+/* 
+ * Copyright (c) 2004-2007 QOS.ch
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+package org.slf4j.converter;
+
+import java.util.ArrayList;
+import java.util.regex.Pattern;
+
+class TrivialMatcher extends AbstractMatcher{
+ 
+  protected void initRules() {
+    //simple rule no capturing group is defined, we use default capturing group which is group zero
+    SingleConversionRule cr = new SingleConversionRule(Pattern.compile("import org.slf4j.converter"), 
+        "simple replacement with an unique capturing group");
+    
+    //we define 4 differents capturing groups
+    MultiGroupConversionRule cr1 = new MultiGroupConversionRule(Pattern.compile("(first group)( second group)( third group)( 4th group)"));
+    //group zero is ignored during treatment
+    //replacement for the first
+    cr1.addReplacement(Constant.INDEX_1, "1st group");
+    //no replacement for the second group it will remains the same
+    //empty string for the third group it will be deleted
+    cr1.addReplacement(Constant.INDEX_3, "");
+    //no replacement for the third group it will remains the same
+    
+    rules = new ArrayList<ConversionRule>();
+    rules.add(cr);
+    rules.add(cr1);
+  }
+  
+}
\ No newline at end of file



More information about the slf4j-dev mailing list