|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AssertRegexp.java | 100% | 100% | 100% | 100% |
|
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;
|
|
18 |
import java.util.List;
|
|
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 _matches;
|
|
38 |
private String _regexp;
|
|
39 |
private int _subgroup; |
|
40 |
|
|
41 | 11 |
public void addMatch(RegexpMatch m) |
42 |
{ |
|
43 | 11 |
if (_matches == null) |
44 | 5 |
_matches = new ArrayList();
|
45 |
|
|
46 | 11 |
_matches.add(m); |
47 |
} |
|
48 |
|
|
49 | 8 |
public void execute(ScriptedTestSession session) |
50 |
{ |
|
51 | 8 |
if (_matches != null) |
52 |
{ |
|
53 | 5 |
executeMatches(session); |
54 | 2 |
return;
|
55 |
} |
|
56 |
|
|
57 | 3 |
String responseContent = session.getResponse().getOutputString(); |
58 |
|
|
59 | 3 |
if (session.getMatcher().contains(_regexp, responseContent))
|
60 | 1 |
return;
|
61 |
|
|
62 | 2 |
throw new ApplicationRuntimeException( |
63 |
ScriptMessages.expectedRegexpMissing(_regexp, getLocation()), |
|
64 |
getLocation(), |
|
65 |
null);
|
|
66 |
} |
|
67 |
|
|
68 | 5 |
private void executeMatches(ScriptedTestSession session) |
69 |
{ |
|
70 | 5 |
String responseContent = session.getResponse().getOutputString(); |
71 |
|
|
72 | 5 |
String[] matches = session.getMatcher().getMatches(_regexp, responseContent, _subgroup); |
73 |
|
|
74 | 5 |
int expectedCount = _matches.size();
|
75 | 5 |
int count = Math.min(expectedCount, matches.length);
|
76 |
|
|
77 | 5 |
for (int i = 0; i < count; i++) |
78 |
{ |
|
79 | 8 |
RegexpMatch m = (RegexpMatch) _matches.get(i); |
80 |
|
|
81 | 8 |
String expected = m.getExpectedString(); |
82 | 8 |
String actual = matches[i]; |
83 |
|
|
84 | 8 |
if (expected.equals(actual))
|
85 | 7 |
continue;
|
86 |
|
|
87 | 1 |
throw new ApplicationRuntimeException( |
88 |
ScriptMessages.incorrectRegexpMatch(expected, m.getLocation(), actual), |
|
89 |
m.getLocation(), |
|
90 |
null);
|
|
91 |
|
|
92 |
} |
|
93 |
|
|
94 | 4 |
if (matches.length != expectedCount)
|
95 | 2 |
throw new ApplicationRuntimeException( |
96 |
ScriptMessages.incorrectRegexpMatchCount( |
|
97 |
_regexp, |
|
98 |
getLocation(), |
|
99 |
expectedCount, |
|
100 |
matches.length), |
|
101 |
getLocation(), |
|
102 |
null);
|
|
103 |
} |
|
104 |
|
|
105 | 8 |
public void setRegexp(String string) |
106 |
{ |
|
107 | 8 |
_regexp = string; |
108 |
} |
|
109 |
|
|
110 | 3 |
public void setSubgroup(int subgroup) |
111 |
{ |
|
112 | 3 |
_subgroup = subgroup; |
113 |
} |
|
114 |
|
|
115 |
} |
|
116 |
|
|