View Javadoc
1   /**
2    * Copyright (c) 2004-2011 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   *
24   */
25  package org.slf4j.migrator;
26  
27  import java.util.regex.Matcher;
28  import java.util.regex.Pattern;
29  
30  import org.slf4j.migrator.line.MultiGroupConversionRule;
31  
32  import junit.framework.TestCase;
33  
34  public class AternativeApproach extends TestCase {
35  
36      /**
37       * In this test we see that we cans use more simple Pattern to do the
38       * conversion
39       * 
40       */
41      public void test() {
42          MultiGroupConversionRule cr2 = new MultiGroupConversionRule(Pattern.compile("(.*)(Log)"));
43          cr2.addReplacement(2, "LOGGER");
44  
45          String s = "abcd Log";
46          Pattern pat = cr2.getPattern();
47          Matcher m = pat.matcher(s);
48  
49          assertTrue(m.matches());
50          String r = cr2.replace(m);
51          assertEquals("abcd LOGGER", r);
52  
53          System.out.println(r);
54      }
55  
56      /**
57       * In this test we replace, using the simple Pattern (Log), the full Log
58       * declaration and instantiation. This is not convenient because we will also
59       * replace all String containing "Log".
60       */
61      public void test2() {
62          Pattern pat = Pattern.compile("(Log)");
63          String s = "abcd Log =";
64          Matcher m = pat.matcher(s);
65          assertTrue(m.find());
66          String r = m.replaceAll("Logger");
67          assertEquals("abcd Logger =", r);
68  
69          String s1 = "Log l = LogFactory.getLog(MyClass.class);";
70          m = pat.matcher(s1);
71          assertTrue(m.find());
72          r = m.replaceAll("Logger");
73          assertEquals("Logger l = LoggerFactory.getLogger(MyClass.class);", r);
74  
75          String s2 = "Logabc ";
76          m = pat.matcher(s2);
77          assertTrue(m.find());
78  
79          String s3 = "abcLog";
80          m = pat.matcher(s3);
81          assertTrue(m.find());
82      }
83  
84      /**
85       * In this test we use a simple Pattern to replace the log instantiation
86       * without influence on Log declaration.
87       * 
88       */
89      public void test3() {
90          Pattern pat = Pattern.compile("LogFactory.getFactory\\(\\).getInstance\\(");
91          String s = "Log log =  LogFactory.getFactory().getInstance(\"x\");";
92          Matcher m = pat.matcher(s);
93          assertTrue(m.find());
94          String r = m.replaceAll("LoggerFactory.getLogger(");
95          assertEquals("Log log =  LoggerFactory.getLogger(\"x\");", r);
96  
97          String nonMatching = "Log log = xxx;";
98          pat.matcher(nonMatching);
99          assertFalse(m.find());
100     }
101 
102     /**
103      * In this test we try to replace keyword Log without influence on String
104      * containing Log We see that we have to use two different Patterns
105      */
106     public void test4() {
107         Pattern pat = Pattern.compile("(\\sLog\\b)");
108         String s = "abcd Log =";
109         Matcher m = pat.matcher(s);
110         assertTrue(m.find());
111         String r = m.replaceAll(" Logger");
112         assertEquals("abcd Logger =", r);
113 
114         String s2 = "Logabcd ";
115         m = pat.matcher(s2);
116         assertFalse(m.find());
117 
118         String s3 = "abcdLogabcd ";
119         m = pat.matcher(s3);
120         assertFalse(m.find());
121 
122         String s4 = "abcdLog";
123         m = pat.matcher(s4);
124         assertFalse(m.find());
125 
126         String s5 = "Log myLog";
127         m = pat.matcher(s5);
128         assertFalse(m.find());
129 
130         Pattern pat2 = Pattern.compile("^Log\\b");
131         Matcher m2 = pat2.matcher(s5);
132         assertTrue(m2.find());
133         r = m2.replaceAll("Logger");
134         assertEquals("Logger myLog", r);
135     }
136 
137     /**
138      * In this test we combine two Pattern to achieve the intended conversion
139      */
140     public void test5() {
141         Pattern pat = Pattern.compile("(\\sLog\\b)");
142         String s = "public Log myLog =LogFactory.getFactory().getInstance(myClass.class);";
143         Matcher m = pat.matcher(s);
144         assertTrue(m.find());
145         String r = m.replaceAll(" Logger");
146         assertEquals("public Logger myLog =LogFactory.getFactory().getInstance(myClass.class);", r);
147 
148         Pattern pat2 = Pattern.compile("LogFactory.getFactory\\(\\).getInstance\\(");
149         m = pat2.matcher(r);
150         assertTrue(m.find());
151         r = m.replaceAll("LoggerFactory.getLogger(");
152         assertEquals("public Logger myLog =LoggerFactory.getLogger(myClass.class);", r);
153     }
154 }