1 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 34 public class AssertRegexp extends BaseLocatable implements ResponseAssertion 35 { 36 37 private List _matches; 38 private String _regexp; 39 private int _subgroup; 40 41 public void addMatch(RegexpMatch m) 42 { 43 if (_matches == null) 44 _matches = new ArrayList (); 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 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 responseContent = session.getResponse().getOutputString(); 71 72 String [] 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 expected = m.getExpectedString(); 82 String 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 string) 106 { 107 _regexp = string; 108 } 109 110 public void setSubgroup(int subgroup) 111 { 112 _subgroup = subgroup; 113 } 114 115 } 116 | Popular Tags |