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.line;
26  
27  import java.util.Arrays;
28  
29  import org.junit.Test;
30  import org.slf4j.migrator.line.LineConverter;
31  import org.slf4j.migrator.line.Log4jRuleSet;
32  
33  import static org.junit.Assert.assertEquals;
34  import static org.junit.Assert.assertTrue;
35  
36  public class Log4jRuleSetTest {
37  
38    LineConverter log4jConverter = new LineConverter(new Log4jRuleSet());
39  
40    @Test
41    public void testImportReplacement() {
42      // LogFactory import replacement
43      assertEquals("import org.slf4j.LoggerFactory;", log4jConverter
44          .getOneLineReplacement("import org.apache.log4j.LogManager;"));
45      // Log import replacement
46      assertTrue(Arrays.equals( 
47          new String[] {"import org.slf4j.Logger;", "import org.slf4j.LoggerFactory;" },
48          log4jConverter.getReplacement("import org.apache.log4j.Logger;")));
49    }
50  
51    @Test
52    public void testLogManagerGetLoggerReplacement() {
53      // Logger declaration and instanciation without modifier
54      assertEquals(" Logger l = LoggerFactory.getLogger(MyClass.class);",
55          log4jConverter
56              .getOneLineReplacement(" Logger l = LogManager.getLogger(MyClass.class);"));
57      // Logger declaration and instanciation with one modifier
58      assertEquals(
59          "public Logger mylog=LoggerFactory.getLogger(MyClass.class);",
60          log4jConverter
61              .getOneLineReplacement("public Logger mylog=LogManager.getLogger(MyClass.class);"));
62      // Logger declaration and instanciation with two modifier
63      assertEquals(
64          "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
65          log4jConverter
66              .getOneLineReplacement("public static Logger mylog1 = LogManager.getLogger(MyClass.class);"));
67      // Logger declaration and instanciation with two modifier and comment at the
68      // end of line
69      assertEquals(
70          "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);//logger instanciation and declaration",
71          log4jConverter
72              .getOneLineReplacement("public static Logger mylog1 = LogManager.getLogger(MyClass.class);//logger instanciation and declaration"));
73      // Logger instanciation without declaration and comment at the end of line
74      assertEquals(
75          " myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
76          log4jConverter
77              .getOneLineReplacement(" myLog = LogManager.getLogger(MyClass.class);//logger instanciation"));
78      // commented Logger declaration and instanciation with two modifier
79      assertEquals(
80          "//public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
81          log4jConverter
82              .getOneLineReplacement("//public static Logger mylog1 = LogManager.getLogger(MyClass.class);"));
83      // commented Logger instanciation without declaration
84      assertEquals(
85          "// myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
86          log4jConverter
87              .getOneLineReplacement("// myLog = LogManager.getLogger(MyClass.class);//logger instanciation"));
88    }
89  
90    @Test
91    public void testLoggerGetLoggerReplacement() {
92      // Logger declaration and instanciation without modifier
93      assertEquals("Logger l = LoggerFactory.getLogger(MyClass.class);",
94          log4jConverter
95              .getOneLineReplacement("Logger l = Logger.getLogger(MyClass.class);"));
96      // Logger declaration and instanciation with one modifier
97      assertEquals(
98          "public Logger mylog=LoggerFactory.getLogger(MyClass.class);",
99          log4jConverter
100             .getOneLineReplacement("public Logger mylog=Logger.getLogger(MyClass.class);"));
101     // Logger declaration and instanciation with modifiers
102     assertEquals(
103         "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
104         log4jConverter
105             .getOneLineReplacement("public static Logger mylog1 = Logger.getLogger(MyClass.class);"));
106     // Logger declaration and instanciation with two modifier and comment at the
107     // end of line
108     assertEquals(
109         "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class); // logger instanciation and declaration",
110         log4jConverter
111             .getOneLineReplacement("public static Logger mylog1 = Logger.getLogger(MyClass.class); // logger instanciation and declaration"));
112     // Logger instanciation without declaration and comment at the end of line
113     assertEquals(
114         " myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
115         log4jConverter
116             .getOneLineReplacement(" myLog = Logger.getLogger(MyClass.class);//logger instanciation"));
117     // commented Logger declaration and instanciation with two modifier
118     assertEquals(
119         "//public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
120         log4jConverter
121             .getOneLineReplacement("//public static Logger mylog1 = Logger.getLogger(MyClass.class);"));
122     // commented Logger instanciation without declaration
123     assertEquals(
124         "// myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
125         log4jConverter
126             .getOneLineReplacement("// myLog = Logger.getLogger(MyClass.class);//logger instanciation"));
127   }
128 
129   @Test
130   public void testLogDeclarationReplacement() {
131     // simple Logger declaration
132     assertEquals("Logger mylog;", log4jConverter.getOneLineReplacement("Logger mylog;"));
133     // Logger declaration with a modifier
134     assertEquals("private Logger mylog;", log4jConverter
135         .getOneLineReplacement("private Logger mylog;"));
136 
137     // Logger declaration with modifiers
138     assertEquals("public static final Logger myLog;", log4jConverter
139         .getOneLineReplacement("public static final Logger myLog;"));
140     // Logger declaration with modifiers and comment at the end of line
141     assertEquals("public Logger myLog;//logger declaration", log4jConverter
142         .getOneLineReplacement("public Logger myLog;//logger declaration"));
143     // commented Logger declaration
144     assertEquals("//private Logger myLog;", log4jConverter
145         .getOneLineReplacement("//private Logger myLog;"));
146   }
147 
148   @Test
149   public void testMultiLineReplacement()  {
150    // Logger declaration on a line
151    assertEquals("protected Logger log =", log4jConverter
152    .getOneLineReplacement("protected Logger log ="));
153     
154    // Logger instanciation on the next line
155    assertEquals(" LoggerFactory.getLogger(MyComponent.class);", log4jConverter
156    .getOneLineReplacement(" LogManager.getLogger(MyComponent.class);"));
157    // Logger declaration on a line
158    assertEquals("protected Logger log ", log4jConverter
159    .getOneLineReplacement("protected Logger log "));
160    // Logger instanciation on the next line
161    assertEquals(
162    " = LoggerFactory.getLogger(MyComponent.class);",
163    log4jConverter
164    .getOneLineReplacement(" = LogManager.getLogger(MyComponent.class);"));
165    }
166 
167 
168   @Test
169   public void categoryReplacement()  {
170     // Category declaration on a line
171     assertEquals("protected Logger cat =", log4jConverter
172             .getOneLineReplacement("protected Category cat ="));
173 
174     assertEquals(" LoggerFactory.getLogger(MyComponent.class);", log4jConverter
175             .getOneLineReplacement(" Category.getInstance(MyComponent.class);"));
176   }
177 
178 }