AternativeApproach 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;
    26
    27  import java.util.regex.Matcher;
    28  import java.util.regex.Pattern;
    29
    30  import org.slf4j.migrator.line.MultiGroupConversionRule;
    31
    32  import junit.framework.TestCase;
    33
    34  public class AternativeApproach extends TestCase {
    35
    36    /**
    37     * In this test we see that we cans use more simple Pattern to do the
    38     * conversion
    39     *
    40     */
    41    public void test() {
    42      MultiGroupConversionRule cr2 = new MultiGroupConversionRule(Pattern
    43          .compile("(.*)(Log)"));
    44      cr2.addReplacement(2, "LOGGER");
    45
    46      String s = "abcd Log";
    47      Pattern pat = cr2.getPattern();
    48      Matcher m = pat.matcher(s);
    49
    50      assertTrue(m.matches());
    51      String r = cr2.replace(m);
    52      assertEquals("abcd LOGGER", r);
    53
    54      System.out.println(r);
    55    }
    56
    57    /**
    58     * In this test we replace, using the simple Pattern (Log), the full Log
    59     * declaration and instanciation. This is not convenient because we will also
    60     * replace all String containing "Log".
    61     */
    62    public void test2() {
    63      Pattern pat = Pattern.compile("(Log)");
    64      String s = "abcd Log =";
    65      Matcher m = pat.matcher(s);
    66      assertTrue(m.find());
    67      String r = m.replaceAll("Logger");
    68      assertEquals("abcd Logger =", r);
    69
    70      String s1 = "Log l = LogFactory.getLog(MyClass.class);";
    71      m = pat.matcher(s1);
    72      assertTrue(m.find());
    73      r = m.replaceAll("Logger");
    74      assertEquals("Logger l = LoggerFactory.getLogger(MyClass.class);", r);
    75
    76      String s2 = "Logabc ";
    77      m = pat.matcher(s2);
    78      assertTrue(m.find());
    79
    80      String s3 = "abcLog";
    81      m = pat.matcher(s3);
    82      assertTrue(m.find());
    83    }
    84
    85    /**
    86     * In this test we use a simple Pattern to replace the log instanciation
    87     * without influence on Log declaration.
    88     *
    89     */
    90    public void test3() {
    91      Pattern pat = Pattern.compile("LogFactory.getFactory\\(\\).getInstance\\(");
    92      String s = "Log log =  LogFactory.getFactory().getInstance(\"x\");";
    93      Matcher m = pat.matcher(s);
    94      assertTrue(m.find());
    95      String r = m.replaceAll("LoggerFactory.getLogger(");
    96      assertEquals("Log log =  LoggerFactory.getLogger(\"x\");", r);
    97
    98      String nonMatching = "Log log = xxx;";
    99      pat.matcher(nonMatching);
    100     assertFalse(m.find());
    101   }
    102
    103   /**
    104    * In this test we try to replace keyword Log without influence on String
    105    * containg Log We see that we have to use two differents Patterns
    106    */
    107   public void test4() {
    108     Pattern pat = Pattern.compile("(\\sLog\\b)");
    109     String s = "abcd Log =";
    110     Matcher m = pat.matcher(s);
    111     assertTrue(m.find());
    112     String r = m.replaceAll(" Logger");
    113     assertEquals("abcd Logger =", r);
    114
    115     String s2 = "Logabcd ";
    116     m = pat.matcher(s2);
    117     assertFalse(m.find());
    118
    119     String s3 = "abcdLogabcd ";
    120     m = pat.matcher(s3);
    121     assertFalse(m.find());
    122
    123     String s4 = "abcdLog";
    124     m = pat.matcher(s4);
    125     assertFalse(m.find());
    126
    127     String s5 = "Log myLog";
    128     m = pat.matcher(s5);
    129     assertFalse(m.find());
    130
    131     Pattern pat2 = Pattern.compile("^Log\\b");
    132     Matcher m2 = pat2.matcher(s5);
    133     assertTrue(m2.find());
    134     r = m2.replaceAll("Logger");
    135     assertEquals("Logger myLog", r);
    136   }
    137
    138   /**
    139    * In this test we combine two Pattern to achieve the intended conversion
    140    */
    141   public void test5() {
    142     Pattern pat = Pattern.compile("(\\sLog\\b)");
    143     String s = "public Log myLog =LogFactory.getFactory().getInstance(myClass.class);";
    144     Matcher m = pat.matcher(s);
    145     assertTrue(m.find());
    146     String r = m.replaceAll(" Logger");
    147     assertEquals("public Logger myLog =LogFactory.getFactory().getInstance(myClass.class);", r);
    148
    149     Pattern pat2 = Pattern.compile("LogFactory.getFactory\\(\\).getInstance\\(");
    150     m = pat2.matcher(r);
    151     assertTrue(m.find());
    152     r = m.replaceAll("LoggerFactory.getLogger(");
    153     assertEquals("public Logger myLog =LoggerFactory.getLogger(myClass.class);", r);
    154   }
    155 }