KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > test > assertions > AssertRegexp


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.assertions;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.impl.BaseLocatable;
22 import org.apache.tapestry.test.ResponseAssertion;
23 import org.apache.tapestry.test.ScriptMessages;
24 import org.apache.tapestry.test.ScriptedTestSession;
25
26 /**
27  * Assertion used to check for the presence of a regular expression
28  * within the response output. Alternately, it can check for a specific
29  * list of matches of a regular expression within the response.
30  *
31  * @author Howard Lewis Ship
32  * @since 4.0
33  */

34 public class AssertRegexp extends BaseLocatable implements ResponseAssertion
35 {
36
37     private List JavaDoc _matches;
38     private String JavaDoc _regexp;
39     private int _subgroup;
40
41     public void addMatch(RegexpMatch m)
42     {
43         if (_matches == null)
44             _matches = new ArrayList JavaDoc();
45
46         _matches.add(m);
47     }
48
49     public void execute(ScriptedTestSession session)
50     {
51         if (_matches != null)
52         {
53             executeMatches(session);
54             return;
55         }
56
57         String JavaDoc responseContent = session.getResponse().getOutputString();
58
59         if (session.getMatcher().contains(_regexp, responseContent))
60             return;
61
62         throw new ApplicationRuntimeException(
63             ScriptMessages.expectedRegexpMissing(_regexp, getLocation()),
64             getLocation(),
65             null);
66     }
67
68     private void executeMatches(ScriptedTestSession session)
69     {
70         String JavaDoc responseContent = session.getResponse().getOutputString();
71
72         String JavaDoc[] matches = session.getMatcher().getMatches(_regexp, responseContent, _subgroup);
73
74         int expectedCount = _matches.size();
75         int count = Math.min(expectedCount, matches.length);
76
77         for (int i = 0; i < count; i++)
78         {
79             RegexpMatch m = (RegexpMatch) _matches.get(i);
80
81             String JavaDoc expected = m.getExpectedString();
82             String JavaDoc actual = matches[i];
83
84             if (expected.equals(actual))
85                 continue;
86
87             throw new ApplicationRuntimeException(
88                 ScriptMessages.incorrectRegexpMatch(expected, m.getLocation(), actual),
89                 m.getLocation(),
90                 null);
91
92         }
93
94         if (matches.length != expectedCount)
95             throw new ApplicationRuntimeException(
96                 ScriptMessages.incorrectRegexpMatchCount(
97                     _regexp,
98                     getLocation(),
99                     expectedCount,
100                     matches.length),
101                 getLocation(),
102                 null);
103     }
104
105     public void setRegexp(String JavaDoc string)
106     {
107         _regexp = string;
108     }
109
110     public void setSubgroup(int subgroup)
111     {
112         _subgroup = subgroup;
113     }
114
115 }
116
Popular Tags