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 org.slf4j.migrator.line.JCLRuleSet;
28  import org.slf4j.migrator.line.LineConverter;
29  
30  import junit.framework.TestCase;
31  
32  public class JCLRuleSetTest extends TestCase {
33  
34      LineConverter jclConverter = new LineConverter(new JCLRuleSet());
35  
36      public void testImportReplacement() {
37          // LogFactory import replacement
38          assertEquals("import org.slf4j.LoggerFactory;", jclConverter.getOneLineReplacement("import org.apache.commons.logging.LogFactory;"));
39          // Log import replacement
40          assertEquals("import org.slf4j.Logger;", jclConverter.getOneLineReplacement("import org.apache.commons.logging.Log;"));
41      }
42  
43      public void testLogFactoryGetLogReplacement() {
44          // Logger declaration and instanciation without modifier
45          assertEquals("  Logger   l = LoggerFactory.getLogger(MyClass.class);",
46                          jclConverter.getOneLineReplacement("  Log   l = LogFactory.getLog(MyClass.class);"));
47          // Logger declaration and instanciation with one modifier
48          assertEquals("public Logger mylog=LoggerFactory.getLogger(MyClass.class);",
49                          jclConverter.getOneLineReplacement("public Log mylog=LogFactory.getLog(MyClass.class);"));
50          // Logger declaration and instanciation with two modifier
51          assertEquals("public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
52                          jclConverter.getOneLineReplacement("public static Log mylog1 = LogFactory.getLog(MyClass.class);"));
53          // Logger declaration and instanciation with two modifier and comment at the
54          // end of line
55          assertEquals("public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class); //logger instanciation and declaration",
56                          jclConverter.getOneLineReplacement("public static Log mylog1 = LogFactory.getLog(MyClass.class); //logger instanciation and declaration"));
57          // Logger instanciation without declaration and comment at the end of line
58          assertEquals(" myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
59                          jclConverter.getOneLineReplacement(" myLog = LogFactory.getLog(MyClass.class);//logger instanciation"));
60          // commented Logger declaration and instanciation with two modifier
61          assertEquals("//public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
62                          jclConverter.getOneLineReplacement("//public static Log mylog1 = LogFactory.getLog(MyClass.class);"));
63          // commented Logger instanciation without declaration
64          assertEquals("// myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
65                          jclConverter.getOneLineReplacement("// myLog = LogFactory.getLog(MyClass.class);//logger instanciation"));
66      }
67  
68      public void testLogFactoryGetFactoryReplacement() {
69  
70          // Logger declaration and instanciation without modifier
71          assertEquals("Logger l = LoggerFactory.getLogger(MyClass.class);",
72                          jclConverter.getOneLineReplacement("Log l = LogFactory.getFactory().getInstance(MyClass.class);"));
73          // Logger declaration and instanciation with one modifier
74          assertEquals("public Logger mylog=LoggerFactory.getLogger(MyClass.class);",
75                          jclConverter.getOneLineReplacement("public Log mylog=LogFactory.getFactory().getInstance(MyClass.class);"));
76          // Logger declaration and instanciation with modifiers
77          assertEquals("public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
78                          jclConverter.getOneLineReplacement("public static Log mylog1 = LogFactory.getFactory().getInstance(MyClass.class);"));
79          // Logger declaration and instanciation with two modifier and comment at the
80          // end of line
81          assertEquals("public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class); //logger instanciation and declaration",
82                          jclConverter.getOneLineReplacement("public static Log mylog1 = LogFactory.getFactory().getInstance(MyClass.class); //logger instanciation and declaration"));
83          // Logger instanciation without declaration and comment at the end of line
84          assertEquals(" myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
85                          jclConverter.getOneLineReplacement(" myLog = LogFactory.getFactory().getInstance(MyClass.class);//logger instanciation"));
86          // commented Logger declaration and instanciation with two modifier
87          assertEquals("//public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
88                          jclConverter.getOneLineReplacement("//public static Log mylog1 = LogFactory.getFactory().getInstance(MyClass.class);"));
89          // commented Logger instanciation without declaration
90          assertEquals("// myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
91                          jclConverter.getOneLineReplacement("// myLog = LogFactory.getFactory().getInstance(MyClass.class);//logger instanciation"));
92      }
93  
94      public void testLogDeclarationReplacement() {
95  
96          // simple Logger declaration
97          assertEquals("Logger mylog;", jclConverter.getOneLineReplacement("Log mylog;"));
98          // Logger declaration with a modifier
99          assertEquals("private Logger mylog;", jclConverter.getOneLineReplacement("private Log mylog;"));
100 
101         // Logger declaration with modifiers
102         assertEquals("public static final Logger myLog;", jclConverter.getOneLineReplacement("public static final Log myLog;"));
103         // Logger declaration with modifiers and comment at the end of line
104         assertEquals("public Logger myLog;//logger declaration", jclConverter.getOneLineReplacement("public Log myLog;//logger declaration"));
105         // commented Logger declaration
106         assertEquals("//private Logger myLog;", jclConverter.getOneLineReplacement("//private Log myLog;"));
107     }
108 
109     public void testMultiLineReplacement() {
110         // Logger declaration on a line
111         assertEquals("protected Logger log =", jclConverter.getOneLineReplacement("protected Log log ="));
112 
113         // Logger instanciation on the next line
114         assertEquals(" LoggerFactory.getLogger(MyComponent.class);", jclConverter.getOneLineReplacement(" LogFactory.getLog(MyComponent.class);"));
115         // Logger declaration on a line
116         assertEquals("protected Logger log ", jclConverter.getOneLineReplacement("protected Log log "));
117         // Logger instanciation on the next line
118         assertEquals(" = LoggerFactory.getLogger(MyComponent.class);",
119                         jclConverter.getOneLineReplacement(" = LogFactory.getFactory().getInstance(MyComponent.class);"));
120     }
121 }