Log4jRuleSetTest xref

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