1   package org.slf4j.migrator;
2   
3   import java.util.regex.Matcher;
4   import java.util.regex.Pattern;
5   
6   import org.slf4j.migrator.line.MultiGroupConversionRule;
7   
8   import junit.framework.TestCase;
9   
10  public class AternativeApproach extends TestCase {
11  
12    /**
13     * In this test we see that we cans use more simple Pattern to do the
14     * conversion
15     * 
16     */
17    public void test() {
18      MultiGroupConversionRule cr2 = new MultiGroupConversionRule(Pattern
19          .compile("(.*)(Log)"));
20      cr2.addReplacement(2, "LOGGER");
21  
22      String s = "abcd Log";
23      Pattern pat = cr2.getPattern();
24      Matcher m = pat.matcher(s);
25  
26      assertTrue(m.matches());
27      String r = cr2.replace(m);
28      assertEquals("abcd LOGGER", r);
29  
30      System.out.println(r);
31    }
32  
33    /**
34     * In this test we replace, using the simple Pattern (Log), the full Log
35     * declaration and instanciation. This is not convenient because we will also
36     * replace all String containing "Log".
37     */
38    public void test2() {
39      Pattern pat = Pattern.compile("(Log)");
40      String s = "abcd Log =";
41      Matcher m = pat.matcher(s);
42      assertTrue(m.find());
43      String r = m.replaceAll("Logger");
44      assertEquals("abcd Logger =", r);
45  
46      String s1 = "Log l = LogFactory.getLog(MyClass.class);";
47      m = pat.matcher(s1);
48      assertTrue(m.find());
49      r = m.replaceAll("Logger");
50      assertEquals("Logger l = LoggerFactory.getLogger(MyClass.class);", r);
51  
52      String s2 = "Logabc ";
53      m = pat.matcher(s2);
54      assertTrue(m.find());
55  
56      String s3 = "abcLog";
57      m = pat.matcher(s3);
58      assertTrue(m.find());
59    }
60  
61    /**
62     * In this test we use a simple Pattern to replace the log instanciation
63     * without influence on Log declaration.
64     * 
65     */
66    public void test3() {
67      Pattern pat = Pattern.compile("LogFactory.getFactory\\(\\).getInstance\\(");
68      String s = "Log log =  LogFactory.getFactory().getInstance(\"x\");";
69      Matcher m = pat.matcher(s);
70      assertTrue(m.find());
71      String r = m.replaceAll("LoggerFactory.getLogger(");
72      assertEquals("Log log =  LoggerFactory.getLogger(\"x\");", r);
73  
74      String nonMatching = "Log log = xxx;";
75      pat.matcher(nonMatching);
76      assertFalse(m.find());
77    }
78  
79    /**
80     * In this test we try to replace keyword Log without influence on String
81     * containg Log We see that we have to use two differents Patterns
82     */
83    public void test4() {
84      Pattern pat = Pattern.compile("(\\sLog\\b)");
85      String s = "abcd Log =";
86      Matcher m = pat.matcher(s);
87      assertTrue(m.find());
88      String r = m.replaceAll(" Logger");
89      assertEquals("abcd Logger =", r);
90  
91      String s2 = "Logabcd ";
92      m = pat.matcher(s2);
93      assertFalse(m.find());
94  
95      String s3 = "abcdLogabcd ";
96      m = pat.matcher(s3);
97      assertFalse(m.find());
98  
99      String s4 = "abcdLog";
100     m = pat.matcher(s4);
101     assertFalse(m.find());
102 
103     String s5 = "Log myLog";
104     m = pat.matcher(s5);
105     assertFalse(m.find());
106 
107     Pattern pat2 = Pattern.compile("^Log\\b");
108     Matcher m2 = pat2.matcher(s5);
109     assertTrue(m2.find());
110     r = m2.replaceAll("Logger");
111     assertEquals("Logger myLog", r);
112   }
113 
114   /**
115    * In this test we combine two Pattern to achieve the intended conversion
116    */
117   public void test5() {
118     Pattern pat = Pattern.compile("(\\sLog\\b)");
119     String s = "public Log myLog =LogFactory.getFactory().getInstance(myClass.class);";
120     Matcher m = pat.matcher(s);
121     assertTrue(m.find());
122     String r = m.replaceAll(" Logger");
123     assertEquals("public Logger myLog =LogFactory.getFactory().getInstance(myClass.class);", r);
124 
125     Pattern pat2 = Pattern.compile("LogFactory.getFactory\\(\\).getInstance\\(");
126     m = pat2.matcher(r);
127     assertTrue(m.find());
128     r = m.replaceAll("LoggerFactory.getLogger(");
129     assertEquals("public Logger myLog =LoggerFactory.getLogger(myClass.class);", r);
130   }
131 }