KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > junit > utils > TestRegexpMatcher


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.junit.utils;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.tapestry.junit.TapestryTestCase;
19 import org.apache.tapestry.util.RegexpMatch;
20 import org.apache.tapestry.util.RegexpMatcher;
21
22 /**
23  * Simple test case for {@link org.apache.tapestry.util.RegexpMatcher}.
24  *
25  * @author Howard Lewis Ship
26  * @since 3.0
27  */

28 public class TestRegexpMatcher extends TapestryTestCase
29 {
30
31     public void testMatch()
32     {
33         RegexpMatcher m = new RegexpMatcher();
34
35         assertTrue(m.matches("[a-z]+", "c"));
36         assertTrue(m.matches("foo|foot", "foo"));
37     }
38
39     public void testNonmatch()
40     {
41         RegexpMatcher m = new RegexpMatcher();
42
43         assertTrue(!m.matches("[0-9]+", "q"));
44         assertTrue(!m.matches("foo|foot", "foot"));
45     }
46
47     public void testBadPattern()
48     {
49         RegexpMatcher m = new RegexpMatcher();
50
51         try
52         {
53             m.matches("[[[", "x");
54
55             unreachable();
56         }
57         catch (ApplicationRuntimeException ex)
58         {
59             checkException(ex, "Unmatched [] in expression");
60         }
61     }
62
63     public void testClear()
64     {
65         RegexpMatcher m = new RegexpMatcher();
66
67         m.clear();
68     }
69
70     public void testContains()
71     {
72         RegexpMatcher m = new RegexpMatcher();
73
74         assertTrue(m.contains("[a-z]+", "c"));
75         assertTrue(m.contains("^(\\d{5}(-\\d{4})?)$", "06514"));
76         assertTrue(m.contains("^(\\d{5}(-\\d{4})?)$", "06514-3149"));
77         assertTrue(m.contains("foo|foot", "12foot12"));
78     }
79
80     public void testNotContains()
81     {
82         RegexpMatcher m = new RegexpMatcher();
83
84         assertTrue(!m.contains("[0-9]+", "q"));
85         assertTrue(!m.contains("^(\\d{5}(-\\d{4})?)$", "0651"));
86         assertTrue(!m.contains("^(\\d{5}(-\\d{4})?)$", "065147"));
87         assertTrue(!m.contains("^(\\d{5}(-\\d{4})?)$", "06514-314"));
88         assertTrue(!m.contains("^(\\d{5}(-\\d{4})?)$", "06514-31497"));
89         assertTrue(!m.contains("^(foo|foot)$", "12foot12"));
90     }
91
92     public void testGetEscapedPatternStrings()
93     {
94         RegexpMatcher m = new RegexpMatcher();
95
96         assertEquals(m.getEscapedPatternString("^\\d$"), "\\^\\\\d\\$");
97     }
98
99     /** @since 4.0 */
100
101     public void testGetMatches()
102     {
103         RegexpMatcher m = new RegexpMatcher();
104
105         String JavaDoc[] matches = m.getMatches("\\d+", "57,232 89 147", 0);
106
107         assertListsEqual(new String JavaDoc[]
108         { "57", "232", "89", "147" }, matches);
109     }
110
111     /** @since 4.0 */
112
113     public void testGetMatchesAsObjects()
114     {
115         RegexpMatcher m = new RegexpMatcher();
116
117         RegexpMatch[] matches = m.getMatches("\\w+(=(\\w+))?", "fred,barney=rubble;wilma=flintstone");
118
119         assertEquals(3, matches.length);
120         
121         assertEquals("fred", matches[0].getInput());
122         assertEquals("fred", matches[0].getGroup(0));
123         
124         assertEquals("barney=rubble", matches[1].getInput());
125         assertEquals("rubble", matches[1].getGroup(2));
126         
127         assertEquals("wilma=flintstone", matches[2].getInput());
128         assertEquals("flintstone", matches[2].getGroup(2));
129     }
130
131     /** @since 4.0 */
132
133     public void testGetMatchesNoMatch()
134     {
135         RegexpMatcher m = new RegexpMatcher();
136
137         String JavaDoc[] matches = m.getMatches("A(B|C)", "aBCAaBA", 0);
138
139         assertEquals(0, matches.length);
140     }
141
142     /** @since 4.0 */
143
144     public void testGetMatchesSubgroup()
145     {
146         RegexpMatcher m = new RegexpMatcher();
147
148         String JavaDoc matches[] = m.getMatches("A(B|C|fred)", "AA AC AB Afred AA AC", 1);
149
150         assertListsEqual(new String JavaDoc[]
151         { "C", "B", "fred", "C" }, matches);
152     }
153
154 }
155
Popular Tags