1 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 28 public class TestAssertRegexp extends TapestryTestCase 29 { 30 public void testSuccess() throws Exception 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 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 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 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 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 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 matchValue) 133 { 134 RegexpMatch m = new RegexpMatch(); 135 m.setExpectedString(matchValue); 136 137 ar.addMatch(m); 138 } 139 } | Popular Tags |