1   package org.slf4j.migrator.line;
2   
3   import org.slf4j.migrator.line.JCLRuleSet;
4   import org.slf4j.migrator.line.LineConverter;
5   
6   import junit.framework.TestCase;
7   
8   public class JCLRuleSetTest extends TestCase {
9   
10    LineConverter jclConverter = new LineConverter(new JCLRuleSet());
11    
12    public void testImportReplacement() {
13      // LogFactory import replacement
14      assertEquals("import org.slf4j.LoggerFactory;", jclConverter
15          .getOneLineReplacement("import org.apache.commons.logging.LogFactory;"));
16      // Log import replacement
17      assertEquals("import org.slf4j.Logger;", jclConverter
18          .getOneLineReplacement("import org.apache.commons.logging.Log;"));
19    }
20  
21    public void testLogFactoryGetLogReplacement() {
22      // Logger declaration and instanciation without modifier
23      assertEquals("  Logger   l = LoggerFactory.getLogger(MyClass.class);",
24          jclConverter
25              .getOneLineReplacement("  Log   l = LogFactory.getLog(MyClass.class);"));
26      // Logger declaration and instanciation with one modifier
27      assertEquals(
28          "public Logger mylog=LoggerFactory.getLogger(MyClass.class);",
29          jclConverter
30              .getOneLineReplacement("public Log mylog=LogFactory.getLog(MyClass.class);"));
31      // Logger declaration and instanciation with two modifier
32      assertEquals(
33          "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
34          jclConverter
35              .getOneLineReplacement("public static Log mylog1 = LogFactory.getLog(MyClass.class);"));
36      // Logger declaration and instanciation with two modifier and comment at the
37      // end of line
38      assertEquals(
39          "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class); //logger instanciation and declaration",
40          jclConverter
41              .getOneLineReplacement("public static Log mylog1 = LogFactory.getLog(MyClass.class); //logger instanciation and declaration"));
42      // Logger instanciation without declaration and comment at the end of line
43      assertEquals(
44          " myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
45          jclConverter
46              .getOneLineReplacement(" myLog = LogFactory.getLog(MyClass.class);//logger instanciation"));
47      // commented Logger declaration and instanciation with two modifier
48      assertEquals(
49          "//public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
50          jclConverter
51              .getOneLineReplacement("//public static Log mylog1 = LogFactory.getLog(MyClass.class);"));
52      // commented Logger instanciation without declaration
53      assertEquals(
54          "// myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
55          jclConverter
56              .getOneLineReplacement("// myLog = LogFactory.getLog(MyClass.class);//logger instanciation"));
57    }
58  
59    public void testLogFactoryGetFactoryReplacement() {
60     
61      // Logger declaration and instanciation without modifier
62      assertEquals(
63          "Logger l = LoggerFactory.getLogger(MyClass.class);",
64          jclConverter
65              .getOneLineReplacement("Log l = LogFactory.getFactory().getInstance(MyClass.class);"));
66      // Logger declaration and instanciation with one modifier
67      assertEquals(
68          "public Logger mylog=LoggerFactory.getLogger(MyClass.class);",
69          jclConverter
70              .getOneLineReplacement("public Log mylog=LogFactory.getFactory().getInstance(MyClass.class);"));
71      // Logger declaration and instanciation with modifiers
72      assertEquals(
73          "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
74          jclConverter
75              .getOneLineReplacement("public static Log mylog1 = LogFactory.getFactory().getInstance(MyClass.class);"));
76      // Logger declaration and instanciation with two modifier and comment at the
77      // end of line
78      assertEquals(
79          "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class); //logger instanciation and declaration",
80          jclConverter
81              .getOneLineReplacement("public static Log mylog1 = LogFactory.getFactory().getInstance(MyClass.class); //logger instanciation and declaration"));
82      // Logger instanciation without declaration and comment at the end of line
83      assertEquals(
84          " myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
85          jclConverter
86              .getOneLineReplacement(" myLog = LogFactory.getFactory().getInstance(MyClass.class);//logger instanciation"));
87      // commented Logger declaration and instanciation with two modifier
88      assertEquals(
89          "//public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
90          jclConverter
91              .getOneLineReplacement("//public static Log mylog1 = LogFactory.getFactory().getInstance(MyClass.class);"));
92      // commented Logger instanciation without declaration
93      assertEquals(
94          "// myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
95          jclConverter
96              .getOneLineReplacement("// myLog = LogFactory.getFactory().getInstance(MyClass.class);//logger instanciation"));
97    }
98  
99    public void testLogDeclarationReplacement() {
100    
101     // simple Logger declaration
102     assertEquals("Logger mylog;", jclConverter.getOneLineReplacement("Log mylog;"));
103     // Logger declaration with a modifier
104     assertEquals("private Logger mylog;", jclConverter
105         .getOneLineReplacement("private Log mylog;"));
106 
107     // Logger declaration with modifiers
108     assertEquals("public static final Logger myLog;", jclConverter
109         .getOneLineReplacement("public static final Log myLog;"));
110     // Logger declaration with modifiers and comment at the end of line
111     assertEquals("public Logger myLog;//logger declaration", jclConverter
112         .getOneLineReplacement("public Log myLog;//logger declaration"));
113     // commented Logger declaration
114     assertEquals("//private Logger myLog;", jclConverter
115         .getOneLineReplacement("//private Log myLog;"));
116   }
117 
118   public void testMultiLineReplacement() {
119     // Logger declaration on a line
120     assertEquals("protected Logger log =", jclConverter
121         .getOneLineReplacement("protected Log log ="));
122 
123     // Logger instanciation on the next line
124     assertEquals(" LoggerFactory.getLogger(MyComponent.class);", jclConverter
125         .getOneLineReplacement(" LogFactory.getLog(MyComponent.class);"));
126     // Logger declaration on a line
127     assertEquals("protected Logger log ", jclConverter
128         .getOneLineReplacement("protected Log log "));
129     // Logger instanciation on the next line
130     assertEquals(
131         " = LoggerFactory.getLogger(MyComponent.class);",
132         jclConverter
133             .getOneLineReplacement(" = LogFactory.getFactory().getInstance(MyComponent.class);"));
134   }
135 }