01: /**
02: * Copyright (C) 2001-2005 France Telecom R&D
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * 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 fevr. 2005
22: *
23: */package org.objectweb.speedo.tools;
24:
25: import java.util.regex.Pattern;
26:
27: import junit.framework.TestCase;
28:
29: /**
30: *
31: *
32: * @author S.Chassande-Barrioz
33: */
34: public class TestStringReplace extends TestCase {
35:
36: public TestStringReplace() {
37: this ("TestStringReplace");
38: }
39:
40: public TestStringReplace(String name) {
41: super (name);
42: }
43:
44: public static void main(String[] args) {
45: junit.textui.TestRunner.run(TestStringReplace.class);
46: }
47:
48: public void testReplaceChar() {
49: assertEquals("org/objectweb/speedo/Foo", StringReplace
50: .replaceChar('.', '/', "org.objectweb.speedo.Foo"));
51: assertEquals("org\\objectweb\\speedo\\Foo", StringReplace
52: .replaceChar('.', '\\', "org.objectweb.speedo.Foo"));
53: }
54:
55: public void testToJavaPattern() {
56: String s = "org.objectweb.s?peedo.*.*Foo*";
57: assertEquals("org\\.objectweb\\.s.?peedo\\..*\\..*Foo.*",
58: StringReplace.toJavaPattern(s));
59: }
60:
61: public void testPatterns() {
62: assertPattern("org", "org", true);
63: assertPattern("org.objectweb.*", "org.objectweb.toto", true);
64: assertPattern("org.objectweb.*", "org.objectwe.toto", false);
65: assertPattern("org.objectweb.*#*", "org.objectweb.Toto#", true);
66: assertPattern("org.objectweb.*#*", "org.objectweb.Toto#f2",
67: true);
68: assertPattern("org.objectweb.*#*", "org.objectwe.Toto#", false);
69: assertPattern("org.objectweb.*#f1", "org.objectweb.Toto#f1",
70: true);
71: assertPattern("org.objectweb.*#f1",
72: "org.objectweb.dgf.sdfsgdf.Toto#f1", true);
73: assertPattern("org.objectweb.*#f1", "org.objectweb.#f1", true);
74: assertPattern("org.objectweb.*#f1", "org.objectweb.Toto#f1",
75: true);
76: assertPattern("org.objectweb.*#f1", "org.objectweb.Toto#f2",
77: false);
78: assertPattern("org.objectweb.*#f?", "org.objectweb.Toto#f2",
79: true);
80: assertPattern("org.objectweb.*#f?", "org.objectweb.Toto#f3",
81: true);
82:
83: }
84:
85: private void assertPattern(String pattern, String exp, boolean match) {
86: assertEquals(match, Pattern.compile(
87: StringReplace.toJavaPattern(pattern)).matcher(exp)
88: .matches());
89: }
90: }
|