001: package org.slf4j.migrator;
002:
003: import java.util.regex.Matcher;
004: import java.util.regex.Pattern;
005:
006: import org.slf4j.migrator.line.MultiGroupConversionRule;
007:
008: import junit.framework.TestCase;
009:
010: public class AternativeApproach extends TestCase {
011:
012: /**
013: * In this test we see that we cans use more simple Pattern to do the
014: * conversion
015: *
016: */
017: public void test() {
018: MultiGroupConversionRule cr2 = new MultiGroupConversionRule(
019: Pattern.compile("(.*)(Log)"));
020: cr2.addReplacement(2, "LOGGER");
021:
022: String s = "abcd Log";
023: Pattern pat = cr2.getPattern();
024: Matcher m = pat.matcher(s);
025:
026: assertTrue(m.matches());
027: String r = cr2.replace(m);
028: assertEquals("abcd LOGGER", r);
029:
030: System.out.println(r);
031: }
032:
033: /**
034: * In this test we replace, using the simple Pattern (Log), the full Log
035: * declaration and instanciation. This is not convenient because we will also
036: * replace all String containing "Log".
037: */
038: public void test2() {
039: Pattern pat = Pattern.compile("(Log)");
040: String s = "abcd Log =";
041: Matcher m = pat.matcher(s);
042: assertTrue(m.find());
043: String r = m.replaceAll("Logger");
044: assertEquals("abcd Logger =", r);
045:
046: String s1 = "Log l = LogFactory.getLog(MyClass.class);";
047: m = pat.matcher(s1);
048: assertTrue(m.find());
049: r = m.replaceAll("Logger");
050: assertEquals(
051: "Logger l = LoggerFactory.getLogger(MyClass.class);", r);
052:
053: String s2 = "Logabc ";
054: m = pat.matcher(s2);
055: assertTrue(m.find());
056:
057: String s3 = "abcLog";
058: m = pat.matcher(s3);
059: assertTrue(m.find());
060: }
061:
062: /**
063: * In this test we use a simple Pattern to replace the log instanciation
064: * without influence on Log declaration.
065: *
066: */
067: public void test3() {
068: Pattern pat = Pattern
069: .compile("LogFactory.getFactory\\(\\).getInstance\\(");
070: String s = "Log log = LogFactory.getFactory().getInstance(\"x\");";
071: Matcher m = pat.matcher(s);
072: assertTrue(m.find());
073: String r = m.replaceAll("LoggerFactory.getLogger(");
074: assertEquals("Log log = LoggerFactory.getLogger(\"x\");", r);
075:
076: String nonMatching = "Log log = xxx;";
077: pat.matcher(nonMatching);
078: assertFalse(m.find());
079: }
080:
081: /**
082: * In this test we try to replace keyword Log without influence on String
083: * containg Log We see that we have to use two differents Patterns
084: */
085: public void test4() {
086: Pattern pat = Pattern.compile("(\\sLog\\b)");
087: String s = "abcd Log =";
088: Matcher m = pat.matcher(s);
089: assertTrue(m.find());
090: String r = m.replaceAll(" Logger");
091: assertEquals("abcd Logger =", r);
092:
093: String s2 = "Logabcd ";
094: m = pat.matcher(s2);
095: assertFalse(m.find());
096:
097: String s3 = "abcdLogabcd ";
098: m = pat.matcher(s3);
099: assertFalse(m.find());
100:
101: String s4 = "abcdLog";
102: m = pat.matcher(s4);
103: assertFalse(m.find());
104:
105: String s5 = "Log myLog";
106: m = pat.matcher(s5);
107: assertFalse(m.find());
108:
109: Pattern pat2 = Pattern.compile("^Log\\b");
110: Matcher m2 = pat2.matcher(s5);
111: assertTrue(m2.find());
112: r = m2.replaceAll("Logger");
113: assertEquals("Logger myLog", r);
114: }
115:
116: /**
117: * In this test we combine two Pattern to achieve the intended conversion
118: */
119: public void test5() {
120: Pattern pat = Pattern.compile("(\\sLog\\b)");
121: String s = "public Log myLog =LogFactory.getFactory().getInstance(myClass.class);";
122: Matcher m = pat.matcher(s);
123: assertTrue(m.find());
124: String r = m.replaceAll(" Logger");
125: assertEquals(
126: "public Logger myLog =LogFactory.getFactory().getInstance(myClass.class);",
127: r);
128:
129: Pattern pat2 = Pattern
130: .compile("LogFactory.getFactory\\(\\).getInstance\\(");
131: m = pat2.matcher(r);
132: assertTrue(m.find());
133: r = m.replaceAll("LoggerFactory.getLogger(");
134: assertEquals(
135: "public Logger myLog =LoggerFactory.getLogger(myClass.class);",
136: r);
137: }
138: }
|