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