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.getOneLineReplacement("import org.apache.log4j.LogManager;"));
    44          // Log import replacement
    45          assertTrue(Arrays.equals(new String[] { "import org.slf4j.Logger;", "import org.slf4j.LoggerFactory;" },
    46                          log4jConverter.getReplacement("import org.apache.log4j.Logger;")));
    47      }
    48
    49      @Test
    50      public void testLogManagerGetLoggerReplacement() {
    51          // Logger declaration and instanciation without modifier
    52          assertEquals(" Logger l = LoggerFactory.getLogger(MyClass.class);",
    53                          log4jConverter.getOneLineReplacement(" Logger l = LogManager.getLogger(MyClass.class);"));
    54          // Logger declaration and instanciation with one modifier
    55          assertEquals("public Logger mylog=LoggerFactory.getLogger(MyClass.class);",
    56                          log4jConverter.getOneLineReplacement("public Logger mylog=LogManager.getLogger(MyClass.class);"));
    57          // Logger declaration and instanciation with two modifier
    58          assertEquals("public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
    59                          log4jConverter.getOneLineReplacement("public static Logger mylog1 = LogManager.getLogger(MyClass.class);"));
    60          // Logger declaration and instanciation with two modifier and comment at the
    61          // end of line
    62          assertEquals("public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);//logger instanciation and declaration",
    63                          log4jConverter.getOneLineReplacement("public static Logger mylog1 = LogManager.getLogger(MyClass.class);//logger instanciation and declaration"));
    64          // Logger instanciation without declaration and comment at the end of line
    65          assertEquals(" myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
    66                          log4jConverter.getOneLineReplacement(" myLog = LogManager.getLogger(MyClass.class);//logger instanciation"));
    67          // commented Logger declaration and instanciation with two modifier
    68          assertEquals("//public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
    69                          log4jConverter.getOneLineReplacement("//public static Logger mylog1 = LogManager.getLogger(MyClass.class);"));
    70          // commented Logger instanciation without declaration
    71          assertEquals("// myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
    72                          log4jConverter.getOneLineReplacement("// myLog = LogManager.getLogger(MyClass.class);//logger instanciation"));
    73      }
    74
    75      @Test
    76      public void testLoggerGetLoggerReplacement() {
    77          // Logger declaration and instanciation without modifier
    78          assertEquals("Logger l = LoggerFactory.getLogger(MyClass.class);", log4jConverter.getOneLineReplacement("Logger l = Logger.getLogger(MyClass.class);"));
    79          // Logger declaration and instanciation with one modifier
    80          assertEquals("public Logger mylog=LoggerFactory.getLogger(MyClass.class);",
    81                          log4jConverter.getOneLineReplacement("public Logger mylog=Logger.getLogger(MyClass.class);"));
    82          // Logger declaration and instanciation with modifiers
    83          assertEquals("public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
    84                          log4jConverter.getOneLineReplacement("public static Logger mylog1 = Logger.getLogger(MyClass.class);"));
    85          // Logger declaration and instanciation with two modifier and comment at the
    86          // end of line
    87          assertEquals("public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class); // logger instanciation and declaration",
    88                          log4jConverter.getOneLineReplacement("public static Logger mylog1 = Logger.getLogger(MyClass.class); // logger instanciation and declaration"));
    89          // Logger instanciation without declaration and comment at the end of line
    90          assertEquals(" myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
    91                          log4jConverter.getOneLineReplacement(" myLog = Logger.getLogger(MyClass.class);//logger instanciation"));
    92          // commented Logger declaration and instanciation with two modifier
    93          assertEquals("//public static Logger mylog1 = LoggerFactory.getLogger(MyClass.class);",
    94                          log4jConverter.getOneLineReplacement("//public static Logger mylog1 = Logger.getLogger(MyClass.class);"));
    95          // commented Logger instanciation without declaration
    96          assertEquals("// myLog = LoggerFactory.getLogger(MyClass.class);//logger instanciation",
    97                          log4jConverter.getOneLineReplacement("// myLog = Logger.getLogger(MyClass.class);//logger instanciation"));
    98      }
    99
    100     @Test
    101     public void testLogDeclarationReplacement() {
    102         // simple Logger declaration
    103         assertEquals("Logger mylog;", log4jConverter.getOneLineReplacement("Logger mylog;"));
    104         // Logger declaration with a modifier
    105         assertEquals("private Logger mylog;", log4jConverter.getOneLineReplacement("private Logger mylog;"));
    106
    107         // Logger declaration with modifiers
    108         assertEquals("public static final Logger myLog;", log4jConverter.getOneLineReplacement("public static final Logger myLog;"));
    109         // Logger declaration with modifiers and comment at the end of line
    110         assertEquals("public Logger myLog;//logger declaration", log4jConverter.getOneLineReplacement("public Logger myLog;//logger declaration"));
    111         // commented Logger declaration
    112         assertEquals("//private Logger myLog;", log4jConverter.getOneLineReplacement("//private Logger myLog;"));
    113     }
    114
    115     @Test
    116     public void testMultiLineReplacement() {
    117         // Logger declaration on a line
    118         assertEquals("protected Logger log =", log4jConverter.getOneLineReplacement("protected Logger log ="));
    119
    120         // Logger instanciation on the next line
    121         assertEquals(" LoggerFactory.getLogger(MyComponent.class);", log4jConverter.getOneLineReplacement(" LogManager.getLogger(MyComponent.class);"));
    122         // Logger declaration on a line
    123         assertEquals("protected Logger log ", log4jConverter.getOneLineReplacement("protected Logger log "));
    124         // Logger instanciation on the next line
    125         assertEquals(" = LoggerFactory.getLogger(MyComponent.class);", log4jConverter.getOneLineReplacement(" = LogManager.getLogger(MyComponent.class);"));
    126     }
    127
    128     @Test
    129     public void categoryReplacement() {
    130         // Category declaration on a line
    131         assertEquals("protected Logger cat =", log4jConverter.getOneLineReplacement("protected Category cat ="));
    132
    133         assertEquals(" LoggerFactory.getLogger(MyComponent.class);", log4jConverter.getOneLineReplacement(" Category.getInstance(MyComponent.class);"));
    134     }
    135
    136 }