aboutsummaryrefslogtreecommitdiff
path: root/slf4j-migrator/src/test/java/org/slf4j/migrator/AternativeApproach.java
blob: 0626b871b415a5b4869b7a0c4c5acf0658859b7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
 * Copyright (c) 2004-2011 QOS.ch
 * All rights reserved.
 *
 * Permission is hereby granted, free  of charge, to any person obtaining
 * a  copy  of this  software  and  associated  documentation files  (the
 * "Software"), to  deal in  the Software without  restriction, including
 * without limitation  the rights to  use, copy, modify,  merge, publish,
 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
 * permit persons to whom the Software  is furnished to do so, subject to
 * the following conditions:
 *
 * The  above  copyright  notice  and  this permission  notice  shall  be
 * included in all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package org.slf4j.migrator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.migrator.line.MultiGroupConversionRule;

import junit.framework.TestCase;

public class AternativeApproach extends TestCase {

    /**
     * In this test we see that we can use more simple Pattern to do the
     * conversion
     * 
     */
    public void test() {
        MultiGroupConversionRule cr2 = new MultiGroupConversionRule(Pattern.compile("(.*)(Log)"));
        cr2.addReplacement(2, "LOGGER");

        String s = "abcd Log";
        Pattern pat = cr2.getPattern();
        Matcher m = pat.matcher(s);

        assertTrue(m.matches());
        String r = cr2.replace(m);
        assertEquals("abcd LOGGER", r);

        System.out.println(r);
    }

    /**
     * In this test we replace, using the simple Pattern (Log), the full Log
     * declaration and instantiation. This is not convenient because we will also
     * replace all String containing "Log".
     */
    public void test2() {
        Pattern pat = Pattern.compile("(Log)");
        String s = "abcd Log =";
        Matcher m = pat.matcher(s);
        assertTrue(m.find());
        String r = m.replaceAll("Logger");
        assertEquals("abcd Logger =", r);

        String s1 = "Log l = LogFactory.getLog(MyClass.class);";
        m = pat.matcher(s1);
        assertTrue(m.find());
        r = m.replaceAll("Logger");
        assertEquals("Logger l = LoggerFactory.getLogger(MyClass.class);", r);

        String s2 = "Logabc ";
        m = pat.matcher(s2);
        assertTrue(m.find());

        String s3 = "abcLog";
        m = pat.matcher(s3);
        assertTrue(m.find());
    }

    /**
     * In this test we use a simple Pattern to replace the log instantiation
     * without influence on Log declaration.
     * 
     */
    public void test3() {
        Pattern pat = Pattern.compile("LogFactory.getFactory\\(\\).getInstance\\(");
        String s = "Log log =  LogFactory.getFactory().getInstance(\"x\");";
        Matcher m = pat.matcher(s);
        assertTrue(m.find());
        String r = m.replaceAll("LoggerFactory.getLogger(");
        assertEquals("Log log =  LoggerFactory.getLogger(\"x\");", r);

        String nonMatching = "Log log = xxx;";
        pat.matcher(nonMatching);
        assertFalse(m.find());
    }

    /**
     * In this test we try to replace keyword Log without influence on String
     * containing Log We see that we have to use two different Patterns
     */
    public void test4() {
        Pattern pat = Pattern.compile("(\\sLog\\b)");
        String s = "abcd Log =";
        Matcher m = pat.matcher(s);
        assertTrue(m.find());
        String r = m.replaceAll(" Logger");
        assertEquals("abcd Logger =", r);

        String s2 = "Logabcd ";
        m = pat.matcher(s2);
        assertFalse(m.find());

        String s3 = "abcdLogabcd ";
        m = pat.matcher(s3);
        assertFalse(m.find());

        String s4 = "abcdLog";
        m = pat.matcher(s4);
        assertFalse(m.find());

        String s5 = "Log myLog";
        m = pat.matcher(s5);
        assertFalse(m.find());

        Pattern pat2 = Pattern.compile("^Log\\b");
        Matcher m2 = pat2.matcher(s5);
        assertTrue(m2.find());
        r = m2.replaceAll("Logger");
        assertEquals("Logger myLog", r);
    }

    /**
     * In this test we combine two Pattern to achieve the intended conversion
     */
    public void test5() {
        Pattern pat = Pattern.compile("(\\sLog\\b)");
        String s = "public Log myLog =LogFactory.getFactory().getInstance(myClass.class);";
        Matcher m = pat.matcher(s);
        assertTrue(m.find());
        String r = m.replaceAll(" Logger");
        assertEquals("public Logger myLog =LogFactory.getFactory().getInstance(myClass.class);", r);

        Pattern pat2 = Pattern.compile("LogFactory.getFactory\\(\\).getInstance\\(");
        m = pat2.matcher(r);
        assertTrue(m.find());
        r = m.replaceAll("LoggerFactory.getLogger(");
        assertEquals("public Logger myLog =LoggerFactory.getLogger(myClass.class);", r);
    }
}