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