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