001: package org.slf4j.migrator.line;
002:
003: import java.util.Arrays;
004:
005: import org.slf4j.migrator.line.LineConverter;
006: import org.slf4j.migrator.line.Log4jRuleSet;
007:
008: import junit.framework.TestCase;
009:
010: public class Log4jRuleSetTest extends TestCase {
011:
012: LineConverter log4jConverter = new LineConverter(new Log4jRuleSet());
013:
014: public void testImportReplacement() {
015: // LogFactory import replacement
016: assertEquals(
017: "import org.slf4j.LoggerFactory;",
018: log4jConverter
019: .getOneLineReplacement("import org.apache.log4j.LogManager;"));
020: // Log import replacement
021: assertTrue(Arrays.equals(new String[] {
022: "import org.slf4j.Logger;",
023: "import org.slf4j.LoggerFactory;" }, log4jConverter
024: .getReplacement("import org.apache.log4j.Logger;")));
025: }
026:
027: public void testLogManagerGetLoggerReplacement() {
028: // Logger declaration and instanciation without modifier
029: assertEquals(
030: " Logger l = LoggerFactory.getLogger(MyClass.class);",
031: log4jConverter
032: .getOneLineReplacement(" Logger l = LogManager.getLogger(MyClass.class);"));
033: // Logger declaration and instanciation with one modifier
034: assertEquals(
035: "public Logger mylog=LoggerFactory.getLogger(MyClass.class);",
036: log4jConverter
037: .getOneLineReplacement("public Logger mylog=LogManager.getLogger(MyClass.class);"));
038: // Logger declaration and instanciation with two modifier
039: assertEquals(
040: "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
041: log4jConverter
042: .getOneLineReplacement("public static Logger mylog1 = LogManager.getLogger(MyClass.class);"));
043: // Logger declaration and instanciation with two modifier and comment at the
044: // end of line
045: assertEquals(
046: "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);//logger instanciation and declaration",
047: log4jConverter
048: .getOneLineReplacement("public static Logger mylog1 = LogManager.getLogger(MyClass.class);//logger instanciation and declaration"));
049: // Logger instanciation without declaration and comment at the end of line
050: assertEquals(
051: " myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
052: log4jConverter
053: .getOneLineReplacement(" myLog = LogManager.getLogger(MyClass.class);//logger instanciation"));
054: // commented Logger declaration and instanciation with two modifier
055: assertEquals(
056: "//public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
057: log4jConverter
058: .getOneLineReplacement("//public static Logger mylog1 = LogManager.getLogger(MyClass.class);"));
059: // commented Logger instanciation without declaration
060: assertEquals(
061: "// myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
062: log4jConverter
063: .getOneLineReplacement("// myLog = LogManager.getLogger(MyClass.class);//logger instanciation"));
064: }
065:
066: public void testLoggerGetLoggerReplacement() {
067: // Logger declaration and instanciation without modifier
068: assertEquals(
069: "Logger l = LoggerFactory.getLogger(MyClass.class);",
070: log4jConverter
071: .getOneLineReplacement("Logger l = Logger.getLogger(MyClass.class);"));
072: // Logger declaration and instanciation with one modifier
073: assertEquals(
074: "public Logger mylog=LoggerFactory.getLogger(MyClass.class);",
075: log4jConverter
076: .getOneLineReplacement("public Logger mylog=Logger.getLogger(MyClass.class);"));
077: // Logger declaration and instanciation with modifiers
078: assertEquals(
079: "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
080: log4jConverter
081: .getOneLineReplacement("public static Logger mylog1 = Logger.getLogger(MyClass.class);"));
082: // Logger declaration and instanciation with two modifier and comment at the
083: // end of line
084: assertEquals(
085: "public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class); // logger instanciation and declaration",
086: log4jConverter
087: .getOneLineReplacement("public static Logger mylog1 = Logger.getLogger(MyClass.class); // logger instanciation and declaration"));
088: // Logger instanciation without declaration and comment at the end of line
089: assertEquals(
090: " myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
091: log4jConverter
092: .getOneLineReplacement(" myLog = Logger.getLogger(MyClass.class);//logger instanciation"));
093: // commented Logger declaration and instanciation with two modifier
094: assertEquals(
095: "//public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
096: log4jConverter
097: .getOneLineReplacement("//public static Logger mylog1 = Logger.getLogger(MyClass.class);"));
098: // commented Logger instanciation without declaration
099: assertEquals(
100: "// myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
101: log4jConverter
102: .getOneLineReplacement("// myLog = Logger.getLogger(MyClass.class);//logger instanciation"));
103: }
104:
105: public void testLogDeclarationReplacement() {
106: // simple Logger declaration
107: assertEquals("Logger mylog;", log4jConverter
108: .getOneLineReplacement("Logger mylog;"));
109: // Logger declaration with a modifier
110: assertEquals("private Logger mylog;", log4jConverter
111: .getOneLineReplacement("private Logger mylog;"));
112:
113: // Logger declaration with modifiers
114: assertEquals(
115: "public static final Logger myLog;",
116: log4jConverter
117: .getOneLineReplacement("public static final Logger myLog;"));
118: // Logger declaration with modifiers and comment at the end of line
119: assertEquals(
120: "public Logger myLog;//logger declaration",
121: log4jConverter
122: .getOneLineReplacement("public Logger myLog;//logger declaration"));
123: // commented Logger declaration
124: assertEquals("//private Logger myLog;", log4jConverter
125: .getOneLineReplacement("//private Logger myLog;"));
126: }
127:
128: public void testMultiLineReplacement() {
129: // Logger declaration on a line
130: assertEquals("protected Logger log =", log4jConverter
131: .getOneLineReplacement("protected Logger log ="));
132:
133: // Logger instanciation on the next line
134: assertEquals(
135: " LoggerFactory.getLogger(MyComponent.class);",
136: log4jConverter
137: .getOneLineReplacement(" LogManager.getLogger(MyComponent.class);"));
138: // Logger declaration on a line
139: assertEquals("protected Logger log ", log4jConverter
140: .getOneLineReplacement("protected Logger log "));
141: // Logger instanciation on the next line
142: assertEquals(
143: " = LoggerFactory.getLogger(MyComponent.class);",
144: log4jConverter
145: .getOneLineReplacement(" = LogManager.getLogger(MyComponent.class);"));
146: }
147: }
|