1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
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 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
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 |
| } |