01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.testutil;
04:
05: import junit.framework.TestCase;
06: import java.util.regex.Matcher;
07: import java.util.regex.Pattern;
08:
09: public abstract class AbstractRegex extends TestCase {
10: public static void assertMatches(String regexp, String string) {
11: assertHasRegexp(regexp, string);
12: }
13:
14: public static void assertNotMatches(String regexp, String string) {
15: assertDoesntHaveRegexp(regexp, string);
16: }
17:
18: public static void assertHasRegexp(String regexp, String butgot) {
19: Matcher match = Pattern.compile(regexp,
20: Pattern.MULTILINE | Pattern.DOTALL).matcher(butgot);
21: boolean found = match.find();
22: if (!found) {
23: fail("Expected regexp <" + regexp + "> was not found in\n<"
24: + butgot + ">");
25: }
26: }
27:
28: public static void assertDoesntHaveRegexp(String regexp,
29: String butgot) {
30: Matcher match = Pattern.compile(regexp, Pattern.MULTILINE)
31: .matcher(butgot);
32: boolean found = match.find();
33: if (found) {
34: fail("Unexpected regexp <" + regexp + "> was found in\n<"
35: + butgot + ">");
36: }
37: }
38:
39: public static void assertSubString(String substring, String butgot) {
40: if (butgot.indexOf(substring) == -1) {
41: fail("Expected substring <" + substring
42: + "> was not found in\n<" + butgot + ">");
43: }
44: }
45:
46: public static void assertNotSubString(String substring,
47: String butgot) {
48: if (butgot.indexOf(substring) > -1) {
49: fail("Unexpected substring <" + substring
50: + "> was found in <" + butgot + ">");
51: }
52: }
53:
54: public static String divWithIdAndContent(String id,
55: String expectedDivContent) {
56: return "<div.*?id=\"" + id + "\".*?>" + expectedDivContent
57: + "</div>";
58: }
59: }
|