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