KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > test > TestAssertRegexp


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.test;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.tapestry.junit.TapestryTestCase;
19 import org.apache.tapestry.test.assertions.AssertRegexp;
20 import org.apache.tapestry.test.assertions.RegexpMatch;
21
22 /**
23  * Tests for the {@link org.apache.tapestry.test.assertions.AssertRegexp}class.
24  *
25  * @author Howard Lewis Ship
26  * @since 4.0
27  */

28 public class TestAssertRegexp extends TapestryTestCase
29 {
30     public void testSuccess() throws Exception JavaDoc
31     {
32         ScriptedTestSession ss = TestScriptParser.createSession();
33
34         AssertRegexp ar = new AssertRegexp();
35         ar.setRegexp("<title>.*</title>");
36
37         ar.execute(ss);
38     }
39
40     public void testFailure() throws Exception JavaDoc
41     {
42         ScriptedTestSession ss = TestScriptParser.createSession();
43
44         AssertRegexp ar = new AssertRegexp();
45         ar.setRegexp("<body>.*</body>");
46
47         try
48         {
49             ar.execute(ss);
50             unreachable();
51         }
52         catch (ApplicationRuntimeException ex)
53         {
54             assertRegexp(
55                     "Expected regular expression \\(\"<body>.*</body>\", at .*?\\) was not found in the response\\.",
56                     ex.getMessage());
57         }
58     }
59
60     public void testMatchesSuccess() throws Exception JavaDoc
61     {
62         ScriptedTestSession ss = TestScriptParser.createSession();
63
64         AssertRegexp ar = new AssertRegexp();
65         ar.setRegexp("<.*?>");
66
67         addMatch(ar, "<title>");
68         addMatch(ar, "</title>");
69
70         ar.execute(ss);
71     }
72
73     public void testMatchesWrongCount() throws Exception JavaDoc
74     {
75         ScriptedTestSession ss = TestScriptParser.createSession();
76
77         AssertRegexp ar = new AssertRegexp();
78         ar.setRegexp("<.*?>");
79
80         addMatch(ar, "<title>");
81
82         try
83         {
84             ar.execute(ss);
85             unreachable();
86         }
87         catch (ApplicationRuntimeException ex)
88         {
89             assertRegexp(
90                     "Regular expression '<\\.\\*\\?>' \\(at .*?\\) should have generated 1 matches, but generated 2 instead\\.",
91                     ex.getMessage());
92         }
93     }
94
95     public void testMatchesSubgroupSuccess() throws Exception JavaDoc
96     {
97         ScriptedTestSession ss = TestScriptParser.createSession();
98
99         AssertRegexp ar = new AssertRegexp();
100         ar.setRegexp("<(.*?)>");
101         ar.setSubgroup(1);
102
103         addMatch(ar, "title");
104         addMatch(ar, "/title");
105
106         ar.execute(ss);
107     }
108
109     public void testMatchesFailure() throws Exception JavaDoc
110     {
111         ScriptedTestSession ss = TestScriptParser.createSession();
112
113         AssertRegexp ar = new AssertRegexp();
114         ar.setRegexp("<.*?>");
115
116         addMatch(ar, "<little>");
117         addMatch(ar, "</title>");
118
119         try
120         {
121             ar.execute(ss);
122             unreachable();
123         }
124         catch (ApplicationRuntimeException ex)
125         {
126             assertRegexp(
127                     "Expected match was '<little>' \\(at .*?\\), but actual value matched was '<title>'\\.",
128                     ex.getMessage());
129         }
130     }
131
132     private void addMatch(AssertRegexp ar, String JavaDoc matchValue)
133     {
134         RegexpMatch m = new RegexpMatch();
135         m.setExpectedString(matchValue);
136
137         ar.addMatch(m);
138     }
139 }
Popular Tags