[slf4j-dev] svn commit: r889 - in slf4j/trunk/slf4j-converter/src: main/java/org/slf4j/converter test/java/org/slf4j/converter
jncharpin at slf4j.org
jncharpin at slf4j.org
Fri Aug 31 16:05:40 CEST 2007
Author: jncharpin
Date: Fri Aug 31 16:05:40 2007
New Revision: 889
Added:
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConversionRule.java
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Log4jMatcher.java
slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/SimpleMatcherTest.java
Removed:
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/PatternWrapper.java
Modified:
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/AbstractMatcher.java
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.java
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converter.java
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:
on going work refactoring
Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/AbstractMatcher.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/AbstractMatcher.java (original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/AbstractMatcher.java Fri Aug 31 16:05:40 2007
@@ -5,85 +5,62 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+/**
+ * This class runs Pattern matching with java.util.regex
+ * using Patterns defined in concretes implementations
+ *
+ * @author jean-noelcharpin
+ *
+ */
public abstract class AbstractMatcher {
- protected ArrayList<PatternWrapper> rules;
-
- protected boolean commentConversion = true;
+ protected ArrayList<ConversionRule> rules;
protected boolean blockComment = false;
public AbstractMatcher() {
}
+ /**
+ * Return matcher implementation depending on the conversion mode
+ * @param conversionType
+ * @return AbstractMatcher implementation
+ */
public static AbstractMatcher getMatcherImpl(int conversionType) {
- if (conversionType == Constant.JCL_TO_SLF4J) {
+ switch(conversionType){
+ case Constant.JCL_TO_SLF4J :
return new JCLMatcher();
+ case Constant.LOG4J_TO_SLF4J :
+ return new Log4jMatcher();
+ default :
+ return null;
}
- return null;
- }
-
- public void setCommentConversion(boolean commentConversion) {
- this.commentConversion = commentConversion;
}
+
/**
- *
+ * Check if the specified text is matching one of the conversion rules
+ * If a rule is resolved, ask for replacement
+ * If no rule can be applied the text is returned without change
* @param text
+ * @return String
*/
- public String replace(String text) {
- if (isTextConvertible(text)) {
- PatternWrapper patternWrapper;
- Pattern pattern;
- Matcher matcher;
- String replacementText;
- Iterator rulesIter = rules.iterator();
- while (rulesIter.hasNext()) {
- patternWrapper = (PatternWrapper) rulesIter.next();
- pattern = patternWrapper.getPattern();
- matcher = pattern.matcher(text);
- if (matcher.matches()) {
- System.out.println("matching " + text);
- StringBuffer replacementBuffer = new StringBuffer();
- for (int group = 0; group <= matcher.groupCount(); group++) {
- replacementText = patternWrapper.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();
- }
+ public String getReplacement(String text) {
+ ConversionRule conversionRule;
+ Pattern pattern;
+ Matcher matcher;
+ Iterator rulesIter = rules.iterator();
+ while (rulesIter.hasNext()) {
+ conversionRule = (ConversionRule) rulesIter.next();
+ pattern = conversionRule.getPattern();
+ matcher = pattern.matcher(text);
+ if (matcher.matches()) {
+ System.out.println("matching " + text);
+ return conversionRule.replace(matcher);
}
}
return text;
}
- /**
- *
- * @param text
- * @return
- */
- private boolean isTextConvertible(String text) {
- boolean isConvertible = true;
- if (text.trim().length() == 0) {
- isConvertible = false;
- } else if (commentConversion) {
- isConvertible = true;
- } else if (blockComment || text.startsWith(Constant.LINE_COMMENT)) {
- isConvertible = false;
- } else if (text.startsWith(Constant.BLOCK_COMMENT_START)) {
- blockComment = true;
- isConvertible = false;
- }
- if (text.endsWith(Constant.BLOCK_COMMENT_END)) {
- blockComment = false;
- }
- return isConvertible;
- }
-
protected abstract void initRules();
}
Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.java (original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Constant.java Fri Aug 31 16:05:40 2007
@@ -3,6 +3,8 @@
public class Constant {
public final static int JCL_TO_SLF4J = 1;
+
+ public final static int LOG4J_TO_SLF4J = 2;
public final static String LINE_COMMENT = "//";
@@ -22,6 +24,6 @@
public final static Integer INDEX_5 = new Integer(5);
- public final static int NB_FILES_MAX = 1000;
+ public final static int NB_FILES_MAX = 1;
}
Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConversionRule.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConversionRule.java Fri Aug 31 16:05:40 2007
@@ -0,0 +1,58 @@
+package org.slf4j.converter;
+
+import java.util.HashMap;
+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 ConversionRule {
+
+ private Pattern pattern;
+
+ private HashMap<Integer, String> replacementMap;
+
+ public ConversionRule(Pattern pattern) {
+ this.pattern = pattern;
+ this.replacementMap = new HashMap<Integer, String>();
+ }
+
+ 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
+ */
+ 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();
+ }
+}
Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converter.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converter.java (original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Converter.java Fri Aug 31 16:05:40 2007
@@ -27,10 +27,11 @@
private int conversionType;
- private boolean commentConversion;
-
/**
+ * Run JCL to SLF4J conversion
+ *
* @param args
+ * source folder directory optional
* @throws IOException
* @throws IOException
*/
@@ -41,7 +42,6 @@
if (args.length > 0) {
converter.source = args[0];
} else {
- // converter.source = new File("").getAbsolutePath();
JFileChooser selector = new JFileChooser();
selector.setDialogTitle("Source folder selector");
selector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
@@ -61,8 +61,11 @@
}
/**
+ * Ask for concrete matcher implementation depending on the conversion mode
+ * Ask for user confirmation to convert the selected source directory if valid
+ * Ask for user confirmation in case of number of files to convert > 1000
*
- * @return
+ * @return true if init operation complete
* @throws IOException
*/
public boolean init() throws IOException {
@@ -70,7 +73,7 @@
if (matcher == null) {
return false;
}
- matcher.setCommentConversion(commentConversion);
+
writer = new Writer();
File fileSource = new File(source);
@@ -89,9 +92,10 @@
selectFiles(fileSource);
- if (javaFiles.size() > 1000) {
+ if (javaFiles.size() > Constant.NB_FILES_MAX) {
System.out.println("THERE IS " + javaFiles.size()
+ " FILES TO CONVERT, DO YOU WANT TO CONTINUE Y / N ?");
+ response = in.readLine();
if (response.equalsIgnoreCase("N")) {
return false;
}
@@ -101,6 +105,7 @@
}
/**
+ * delete a file
*
* @param fdest
*/
@@ -119,6 +124,7 @@
}
/**
+ * copy a file from source to destination
*
* @param fsource
* @param fdest
@@ -145,6 +151,7 @@
}
/**
+ * Select java files to be converted
*
* @param file
* @return
@@ -169,6 +176,7 @@
}
/**
+ * Convert a list of files
*
* @param lstFiles
*/
@@ -181,6 +189,8 @@
}
/**
+ * Convert the specified file Read each line and ask matcher implementation
+ * for conversion Rewrite the line returned by matcher
*
* @param file
*/
@@ -196,7 +206,7 @@
while (!isEmpty) {
line = breader.readLine();
if (line != null) {
- newLine = matcher.replace(line);
+ newLine = matcher.getReplacement(line);
writer.write(newLine);
} else {
isEmpty = true;
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 Fri Aug 31 16:05:40 2007
@@ -3,6 +3,12 @@
import java.util.ArrayList;
import java.util.regex.Pattern;
+/**
+ * This class represents JCL to SLF4J conversion rules
+ *
+ * @author jean-noelcharpin
+ *
+ */
public class JCLMatcher extends AbstractMatcher {
public JCLMatcher() {
@@ -12,92 +18,89 @@
protected void initRules() {
// matching : import org.apache.commons.logging.LogFactory;
- PatternWrapper p0 = new PatternWrapper(Pattern
+ ConversionRule cr0 = new ConversionRule(Pattern
.compile("import\\s*+org.apache.commons.logging.LogFactory;"));
- p0.addReplacement(Constant.INDEX_0, "import org.slf4j.LoggerFactory;");
-
+ cr0.addReplacement(Constant.INDEX_0, "import org.slf4j.LoggerFactory;");
+
// matching : import org.apache.commons.logging.Log;
- PatternWrapper p1 = new PatternWrapper(Pattern
+ ConversionRule cr1 = new ConversionRule(Pattern
.compile("import\\s*+org.apache.commons.logging.Log;"));
- p1.addReplacement(Constant.INDEX_0, "import org.slf4j.Logger;");
-
+ cr1.addReplacement(Constant.INDEX_0, "import org.slf4j.Logger;");
+
// matching declaration and instanciation : protected Log myLog =
// LogFactory.getFactory().getInstance(MyClass.class); //comment or other
// instruction
- PatternWrapper p2 = new PatternWrapper(
+ ConversionRule cr2 = new ConversionRule(
Pattern
- .compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
- p2.addReplacement(Constant.INDEX_3, "Logger");
- p2.addReplacement(Constant.INDEX_2, "");
- p2.addReplacement(Constant.INDEX_5, "LoggerFactory.getLogger(");
-
+ .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(");
+
// matching declaration and instanciation : protected static Log myLog =
// LogFactory.getLog(MyClass.class); //comment or other instruction
- PatternWrapper p3 = new PatternWrapper(
+ ConversionRule cr3 = new ConversionRule(
Pattern
- .compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s+\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
- p3.addReplacement(Constant.INDEX_3, "Logger");
- p3.addReplacement(Constant.INDEX_2, "");
- p3.addReplacement(Constant.INDEX_5, "LoggerFactory.getLogger(");
-
+ .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(");
+
// matching instanciation without declaration : myLog =
// LogFactory.getFactory().getInstance(MyClass.class); //comment or other
// instruction
- PatternWrapper p4 = new PatternWrapper(
+ ConversionRule cr4 = new ConversionRule(
Pattern
- .compile("((\\w*+\\W*+\\.*)*;*+)(\\w+\\s*+=\\s*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
- p4.addReplacement(Constant.INDEX_4, "LoggerFactory.getLogger(");
- p4.addReplacement(Constant.INDEX_2, "");
-
+ .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, "");
+
// matching instanciation without declaration : myLog =
// LogFactory.getLog(MyClass.class); //comment or other instruction
- PatternWrapper p5 = new PatternWrapper(
+ ConversionRule cr5 = new ConversionRule(
Pattern
- .compile("((\\w*+\\W*+\\.*)*;*+)(\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
- p5.addReplacement(Constant.INDEX_4, "LoggerFactory.getLogger(");
- p5.addReplacement(Constant.INDEX_2, "");
-
+ .compile("((\\w*+\\W*+)*)(\\w+\\s*+=\\s*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
+ cr5.addReplacement(Constant.INDEX_4, "LoggerFactory.getLogger(");
+ cr5.addReplacement(Constant.INDEX_2, "");
+
// matching declaration without instanciation : public static final Log
// myLog //comment or other instruction
- PatternWrapper p6 = new PatternWrapper(
- Pattern
- .compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s*+\\w+\\s*+;)((\\w*+\\W*+\\.*)*;*+)"));
- p6.addReplacement(Constant.INDEX_3, "Logger");
- p6.addReplacement(Constant.INDEX_2, "");
-
-
+ ConversionRule cr6 = new ConversionRule(Pattern
+ .compile("((\\w*+\\W*+)*)(Log)(\\s*+\\w+\\s*+;)((\\w*+\\W*+)*)"));
+ cr6.addReplacement(Constant.INDEX_3, "Logger");
+ cr6.addReplacement(Constant.INDEX_2, "");
+
// matching incomplete instanciation : protected Log log =
- PatternWrapper p7 = new PatternWrapper(Pattern
- .compile("((\\w*+\\W*+\\.*)*;*+)(Log)(\\s+\\w+\\s*+=*\\s*+)"));
- p7.addReplacement(Constant.INDEX_3, "Logger");
- p7.addReplacement(Constant.INDEX_2, "");
-
+ ConversionRule cr7 = new ConversionRule(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);
- PatternWrapper p8 = new PatternWrapper(
+ ConversionRule cr8 = new ConversionRule(
Pattern
- .compile("((\\w*+\\W*+\\.*)*;*+)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
- p8.addReplacement(Constant.INDEX_3, "LoggerFactory.getLogger(");
- p8.addReplacement(Constant.INDEX_1, "");
-
+ .compile("((\\w*+\\W*+)*)(LogFactory.getLog\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
+ cr8.addReplacement(Constant.INDEX_3, "LoggerFactory.getLogger(");
+ cr8.addReplacement(Constant.INDEX_1, "");
+
// matching incomlete instanciation :
// LogFactory.getFactory().getInstance(MyComponent.class);
- PatternWrapper p9 = new PatternWrapper(
+ ConversionRule cr9 = new ConversionRule(
Pattern
- .compile("((\\w*+\\W*+\\.*)*;*+)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+\\.*)*;*+)"));
- p9.addReplacement(Constant.INDEX_3, "LoggerFactory.getLogger(");
- p9.addReplacement(Constant.INDEX_1, "");
-
-
- rules = new ArrayList<PatternWrapper>();
- rules.add(p0);
- rules.add(p1);
- rules.add(p2);
- rules.add(p3);
- rules.add(p4);
- rules.add(p5);
- rules.add(p6);
- rules.add(p7);
- rules.add(p8);
- rules.add(p9);
+ .compile("((\\w*+\\W*+)*)(LogFactory.getFactory\\(\\).getInstance\\()(\\w+)(.class\\);)((\\w*+\\W*+)*)"));
+ cr9.addReplacement(Constant.INDEX_3, "LoggerFactory.getLogger(");
+ cr9.addReplacement(Constant.INDEX_1, "");
+
+ rules = new ArrayList<ConversionRule>();
+ rules.add(cr0);
+ rules.add(cr1);
+ rules.add(cr2);
+ rules.add(cr3);
+ rules.add(cr4);
+ rules.add(cr5);
+ rules.add(cr6);
+ rules.add(cr7);
+ rules.add(cr8);
+ rules.add(cr9);
}
}
Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Log4jMatcher.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/Log4jMatcher.java Fri Aug 31 16:05:40 2007
@@ -0,0 +1,11 @@
+package org.slf4j.converter;
+
+public class Log4jMatcher extends AbstractMatcher {
+
+ @Override
+ protected void initRules() {
+ // TODO Auto-generated method stub
+
+ }
+
+}
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 Fri Aug 31 16:05:40 2007
@@ -6,78 +6,132 @@
public class JCLMatcherTest extends TestCase {
- public void testConversion() throws IOException {
-
+ public void testImportReplacement() {
JCLMatcher jclMatcher = new JCLMatcher();
- jclMatcher.setCommentConversion(true);
-
+ // LogFactory import replacement
assertEquals(jclMatcher
- .replace("import org.apache.commons.logging.LogFactory;"),
+ .getReplacement("import org.apache.commons.logging.LogFactory;"),
"import org.slf4j.LoggerFactory;");
- assertEquals(jclMatcher.replace("import org.apache.commons.logging.Log;"),
+ // Log import replacement
+ assertEquals(jclMatcher
+ .getReplacement("import org.apache.commons.logging.Log;"),
"import org.slf4j.Logger;");
+ }
+
+ public void testLogFactoryGetLogReplacement() {
+ JCLMatcher jclMatcher = new JCLMatcher();
+ // Logger declaration and instanciation without modifier
assertEquals(jclMatcher
- .replace("Log l = LogFactory.getLog(MyClass.class);"),
+ .getReplacement("Log l = LogFactory.getLog(MyClass.class);"),
"Logger l = LoggerFactory.getLogger(MyClass.class);");
+ // Logger declaration and instanciation with one modifier
assertEquals(jclMatcher
- .replace("public Log mylog=LogFactory.getLog(MyClass.class);"),
+ .getReplacement("public Log mylog=LogFactory.getLog(MyClass.class);"),
"public Logger mylog=LoggerFactory.getLogger(MyClass.class);");
+ // Logger declaration and instanciation with two modifier
assertEquals(
jclMatcher
- .replace("public static Log mylog1 = LogFactory.getLog(MyClass.class);"),
+ .getReplacement("public static Log mylog1 = LogFactory.getLog(MyClass.class);"),
"public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);");
+ // Logger declaration and instanciation with two modifier and comment at the
+ // end of line
assertEquals(
jclMatcher
- .replace("Log log3=LogFactory.getFactory().getInstance(MyClass.class);"),
- "Logger log3=LoggerFactory.getLogger(MyClass.class);");
+ .getReplacement("public static Log mylog1 = LogFactory.getLog(MyClass.class); //logger instanciation and declaration"),
+ "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class); //logger instanciation and declaration");
+ // Logger instanciation without declaration and comment at the end of line
assertEquals(
jclMatcher
- .replace("Log mylog4 = LogFactory.getFactory().getInstance(MyClass.class);//logger instanciation"),
- "Logger mylog4 = LoggerFactory.getLogger(MyClass.class);//logger instanciation");
- assertEquals(jclMatcher.replace("Log myLog6;//logger declaration"),
- "Logger myLog6;//logger declaration");
- assertEquals(jclMatcher
- .replace("//log7=LogFactory.getFactory().getInstance(MyClass.class);"),
- "//log7=LoggerFactory.getLogger(MyClass.class);");
- assertEquals(jclMatcher
- .replace(" log8 =LogFactory.getFactory().getInstance(MyClass.class);"),
- " log8 =LoggerFactory.getLogger(MyClass.class);");
- assertEquals(jclMatcher
- .replace(" myLog9 = LogFactory.getLog(MyClass.class);"),
- " myLog9 = LoggerFactory.getLogger(MyClass.class);");
- assertEquals(jclMatcher.replace("private Log mylog10;"),
- "private Logger mylog10;");
- assertEquals(jclMatcher.replace("protected final Log myLog11;"),
- "protected final Logger myLog11;");
- assertEquals(jclMatcher.replace("public static final Log myLog12;"),
- "public static final Logger myLog12;");
+ .getReplacement(" myLog = LogFactory.getLog(MyClass.class);//logger instanciation"),
+ " myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation");
+ // commented Logger declaration and instanciation with two modifier
+ assertEquals(
+ jclMatcher
+ .getReplacement("//public static Log mylog1 = LogFactory.getLog(MyClass.class);"),
+ "//public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);");
+ // commented Logger instanciation without declaration
+ assertEquals(
+ jclMatcher
+ .getReplacement("// myLog = LogFactory.getLog(MyClass.class);//logger instanciation"),
+ "// myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation");
+ }
+
+ public void testLogFactoryGetFactoryReplacement() {
+ JCLMatcher jclMatcher = new JCLMatcher();
+ // Logger declaration and instanciation without modifier
assertEquals(
jclMatcher
- .replace("System.out.println(\"Log\") ;System.out.println(\"Log2\"); public static final Log myLog13;"),
- "System.out.println(\"Log\") ;System.out.println(\"Log2\"); public static final Logger myLog13;");
+ .getReplacement("Log l = LogFactory.getFactory().getInstance(MyClass.class);"),
+ "Logger l = LoggerFactory.getLogger(MyClass.class);");
+ // Logger declaration and instanciation with one modifier
+ assertEquals(
+ jclMatcher
+ .getReplacement("public Log mylog=LogFactory.getFactory().getInstance(MyClass.class);"),
+ "public Logger mylog=LoggerFactory.getLogger(MyClass.class);");
+ // Logger declaration and instanciation with modifiers
assertEquals(
jclMatcher
- .replace("public static final Log myLog14;System.out.println(\"Log\");"),
- "public static final Logger myLog14;System.out.println(\"Log\");");
+ .getReplacement("public static Log mylog1 = LogFactory.getFactory().getInstance(MyClass.class);"),
+ "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);");
+ // Logger declaration and instanciation with two modifier and comment at the
+ // end of line
+ assertEquals(
+ jclMatcher
+ .getReplacement("public static Log mylog1 = LogFactory.getFactory().getInstance(MyClass.class); //logger instanciation and declaration"),
+ "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class); //logger instanciation and declaration");
+ // Logger instanciation without declaration and comment at the end of line
assertEquals(
jclMatcher
- .replace("System.out.println(\"\");public static final Log myLog15;System.out.println(\"Log\") ;System.out.println(\"Log2\");"),
- "System.out.println(\"\");public static final Logger myLog15;System.out.println(\"Log\") ;System.out.println(\"Log2\");");
+ .getReplacement(" myLog = LogFactory.getFactory().getInstance(MyClass.class);//logger instanciation"),
+ " myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation");
+ // commented Logger declaration and instanciation with two modifier
assertEquals(
jclMatcher
- .replace("((Pojo)pojo.getPojo()).get(\"pojo\",pojo);public static final Log myLog16;"),
- "((Pojo)pojo.getPojo()).get(\"pojo\",pojo);public static final Logger myLog16;");
- assertEquals(jclMatcher.replace("protected Log log ="),
+ .getReplacement("//public static Log mylog1 = LogFactory.getFactory().getInstance(MyClass.class);"),
+ "//public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);");
+ // commented Logger instanciation without declaration
+ assertEquals(
+ jclMatcher
+ .getReplacement("// myLog = LogFactory.getFactory().getInstance(MyClass.class);//logger instanciation"),
+ "// myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation");
+ }
+
+ public void testLogDeclarationReplacement() throws IOException {
+ JCLMatcher jclMatcher = new JCLMatcher();
+ // simple Logger declaration
+ assertEquals(jclMatcher.getReplacement("Log mylog;"), "Logger mylog;");
+ // Logger declaration with a modifier
+ assertEquals(jclMatcher.getReplacement("private Log mylog;"),
+ "private Logger mylog;");
+ // Logger declaration with modifiers
+ assertEquals(jclMatcher.getReplacement("public static final Log myLog;"),
+ "public static final Logger myLog;");
+ // Logger declaration with modifiers and comment at the end of line
+ assertEquals(jclMatcher
+ .getReplacement("public Log myLog;//logger declaration"),
+ "public Logger myLog;//logger declaration");
+ // commented Logger declaration
+ assertEquals(jclMatcher.getReplacement("//private Log myLog;"),
+ "//private Logger myLog;");
+ }
+
+ public void testMultiLineReplacement() throws IOException {
+ JCLMatcher jclMatcher = new JCLMatcher();
+ // Logger declaration on a line
+ assertEquals(jclMatcher.getReplacement("protected Log log ="),
"protected Logger log =");
+ // Logger instanciation on the next line
assertEquals(jclMatcher
- .replace(" LogFactory.getLog(MyComponent.class);"),
- " LoggerFactory.getLogger(MyComponent.class);");
- assertEquals(jclMatcher.replace("protected Log log "),
+ .getReplacement(" LogFactory.getLog(MyComponent.class);"),
+ " LoggerFactory.getLogger(MyComponent.class);");
+ // Logger declaration on a line
+ assertEquals(jclMatcher.getReplacement("protected Log log "),
"protected Logger log ");
+ // Logger instanciation on the next line
assertEquals(
jclMatcher
- .replace(" = LogFactory.getFactory().getInstance(MyComponent.class);"),
- " = LoggerFactory.getLogger(MyComponent.class);");
+ .getReplacement(" = LogFactory.getFactory().getInstance(MyComponent.class);"),
+ " = LoggerFactory.getLogger(MyComponent.class);");
}
}
Added: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/SimpleMatcherTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/SimpleMatcherTest.java Fri Aug 31 16:05:40 2007
@@ -0,0 +1,52 @@
+package org.slf4j.converter;
+
+import java.util.ArrayList;
+import java.util.regex.Pattern;
+
+import junit.framework.TestCase;
+
+public class SimpleMatcherTest 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();
+
+ assertEquals(simpleMatcher.getReplacement("no replacement for this text, it remains the same!"),"no replacement for this text, it remains the same!");
+
+ assertEquals(simpleMatcher.getReplacement("import org.slf4j.converter"),"simple replacement with an unique capturing group");
+
+ assertEquals(simpleMatcher.getReplacement("first group second group third group 4th group"),"1st group second group 4th group");
+
+ }
+
+
+}
More information about the slf4j-dev
mailing list