001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.store;
003:
004: import org.apache.tools.ant.RuntimeConfigurable;
005: import org.apache.tools.ant.UnknownElement;
006:
007: import com.canoo.webtest.ant.WebtestPropertyHelper;
008: import com.canoo.webtest.ant.WebtestTask;
009: import com.canoo.webtest.engine.StepFailedException;
010: import com.canoo.webtest.self.TestBlock;
011: import com.canoo.webtest.self.ThrowAssert;
012: import com.canoo.webtest.steps.BaseStepTestCase;
013: import com.canoo.webtest.steps.Step;
014:
015: /**
016: * @author Carsten Seibert
017: * @author Paul King
018: */
019: public class StoreRegExMatchTest extends BaseStepTestCase {
020: public class StoreRegExMatchStub extends StoreRegExMatch {
021: public StoreRegExMatchStub(String text, String group,
022: String propertyName) {
023: setText(text);
024: setGroup(group);
025: setProperty(propertyName);
026: }
027: }
028:
029: protected Step createStep() {
030: return new StoreRegExMatch();
031: }
032:
033: public void testVerifyParameters() throws Exception {
034: final StoreRegExMatch step = new StoreRegExMatchStub("x", "-1",
035: "X");
036: configureStep(step);
037: assertErrorOnExecute(
038: step,
039: "Should raise StepExecutionException due to invalid parameter",
040: "group parameter with value '-1' must not be negative");
041: step.setGroup("0");
042: step.setProperty(null);
043: checkStepRejectsEmptyParam(step, "property");
044: step.setProperty("myProp");
045: step.setText(null);
046: checkStepRejectsEmptyParam(step,
047: "Regular expression (text attribute)");
048: String documentText = "XXX";
049: step.setText(documentText);
050: getContext().setDefaultResponse(documentText);
051: step.execute();
052: }
053:
054: private void checkStepRejectsEmptyParam(final StoreRegExMatch step,
055: String param) {
056: assertStepRejectsEmptyParam(param, new TestBlock() {
057: public void call() throws Exception {
058: step.execute();
059: }
060: });
061: }
062:
063: public void testGrouping() throws Exception {
064: StoreRegExMatchStub step = new StoreRegExMatchStub(
065: "X(.*)(X(.*)X)((?i:X\\d+))", "0", "myProp");
066: getContext().setDefaultResponse("X11X22Xx33");
067:
068: executeStep(step);
069: assertEquals("Group 0 (whole match)", "X11X22Xx33", step
070: .getWebtestProperty("myProp"));
071:
072: step.setGroup("1");
073: executeStep(step);
074: assertEquals("Group 1", "11", step.getWebtestProperty("myProp"));
075:
076: step.setGroup("2");
077: executeStep(step);
078: assertEquals("Group 2 (outer nested)", "X22X", step
079: .getWebtestProperty("myProp"));
080:
081: step.setGroup("3");
082: executeStep(step);
083: assertEquals("Group 3 (inner nested)", "22", step
084: .getWebtestProperty("myProp"));
085:
086: step.setGroup("4");
087: executeStep(step);
088: assertEquals("Group 4 (last x...)", "x33", step
089: .getWebtestProperty("myProp"));
090: }
091:
092: public void testInvalidGroup() throws Exception {
093: final StoreRegExMatchStub step = new StoreRegExMatchStub(
094: "X(.*)X(.*)X", "3", "myProp");
095: getContext().setDefaultResponse("X11X22X");
096:
097: final Throwable t = ThrowAssert.assertThrows(
098: "referencing an invalid group",
099: StepFailedException.class, new TestBlock() {
100: public void call() throws Exception {
101: executeStep(step);
102: }
103: });
104: assertTrue(t.getMessage().startsWith("Group not found:"));
105: }
106:
107: public void testExceptionIfNoCurrentResponse() throws Exception {
108: final StoreRegExMatchStub step = new StoreRegExMatchStub(
109: "X(.*)X(.*)X", "1", "myProp");
110:
111: assertStepRejectsNullResponse(step);
112: }
113:
114: public void testExceptionIfNoMatch() throws Exception {
115: final StoreRegExMatchStub step = new StoreRegExMatchStub(
116: "X(.*)X(.*)X", "1", "myProp");
117: final Throwable t = ThrowAssert.assertThrows(
118: "no match available", StepFailedException.class,
119: new TestBlock() {
120: public void call() throws Exception {
121: executeStep(step);
122: }
123: });
124: assertTrue(t.getMessage().startsWith(
125: "No match for regular expression"));
126: }
127:
128: public void testSetWebtestProperties() throws Exception {
129: getProject().addTaskDefinition("storeRegExMatch",
130: StoreRegExMatch.class);
131: final UnknownElement task = new UnknownElement(
132: "storeRegExMatch");
133: task.setTaskName("storeRegExMatch");
134:
135: RuntimeConfigurable wrapper = new RuntimeConfigurable(task,
136: task.getTaskName());
137: wrapper.setAttribute("text", "X(#{wildcard})(X(#{wildcard})X)");
138: wrapper.setAttribute("group", "0");
139: wrapper.setAttribute("property", "myProp");
140:
141: configureTask(task);
142: final WebtestTask webtest = getContext().getWebtest();
143: WebtestPropertyHelper
144: .configureWebtestPropertyHelper(getProject());
145: webtest.setDynamicProperty("wildcard", ".*");
146:
147: getContext().setDefaultResponse("X11X22X");
148: task.perform();
149: assertEquals("Group 0 (whole match)", "X11X22X", webtest
150: .getDynamicProperty("myProp"));
151: }
152: }
|