KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > tools > TestStringReplace


1 /**
2  * Copyright (C) 2001-2005 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  *
20  * Authors: S.Chassande-Barrioz.
21  * Created on 16 févr. 2005
22  *
23  */

24 package org.objectweb.speedo.tools;
25
26 import java.util.regex.Pattern JavaDoc;
27
28 import junit.framework.TestCase;
29
30 /**
31  *
32  *
33  * @author S.Chassande-Barrioz
34  */

35 public class TestStringReplace extends TestCase {
36
37     public TestStringReplace() {
38         this("TestStringReplace");
39     }
40     public TestStringReplace(String JavaDoc name) {
41         super(name);
42     }
43     
44     public static void main(String JavaDoc[] args) {
45         junit.textui.TestRunner.run(TestStringReplace.class);
46     }
47
48     public void testReplaceChar() {
49         assertEquals("org/objectweb/speedo/Foo",
50                 StringReplace.replaceChar('.', '/',
51                         "org.objectweb.speedo.Foo"));
52         assertEquals("org\\objectweb\\speedo\\Foo",
53                 StringReplace.replaceChar('.', '\\',
54                         "org.objectweb.speedo.Foo"));
55     }
56
57     public void testToJavaPattern() {
58         String JavaDoc s = "org.objectweb.s?peedo.*.*Foo*";
59         assertEquals("org\\.objectweb\\.s.?peedo\\..*\\..*Foo.*",
60                 StringReplace.toJavaPattern(s));
61     }
62     public void testPatterns() {
63         assertPattern("org", "org", true);
64         assertPattern("org.objectweb.*", "org.objectweb.toto", true);
65         assertPattern("org.objectweb.*", "org.objectwe.toto", false);
66         assertPattern("org.objectweb.*#*", "org.objectweb.Toto#", true);
67         assertPattern("org.objectweb.*#*", "org.objectweb.Toto#f2", true);
68         assertPattern("org.objectweb.*#*", "org.objectwe.Toto#", false);
69         assertPattern("org.objectweb.*#f1", "org.objectweb.Toto#f1", true);
70         assertPattern("org.objectweb.*#f1", "org.objectweb.dgf.sdfsgdf.Toto#f1", true);
71         assertPattern("org.objectweb.*#f1", "org.objectweb.#f1", true);
72         assertPattern("org.objectweb.*#f1", "org.objectweb.Toto#f1", true);
73         assertPattern("org.objectweb.*#f1", "org.objectweb.Toto#f2", false);
74         assertPattern("org.objectweb.*#f?", "org.objectweb.Toto#f2", true);
75         assertPattern("org.objectweb.*#f?", "org.objectweb.Toto#f3", true);
76         
77     }
78     
79     private void assertPattern(String JavaDoc pattern, String JavaDoc exp, boolean match) {
80         assertEquals(match,
81                 Pattern.compile(StringReplace.toJavaPattern(pattern))
82                     .matcher(exp).matches());
83     }
84 }
85
Popular Tags