01: // Copyright © 2002-2007 Canoo Engineering AG, Switzerland.
02: package com.canoo.webtest.steps.request;
03:
04: import java.util.Collections;
05:
06: import org.apache.log4j.Level;
07: import org.apache.log4j.Logger;
08:
09: import com.canoo.webtest.boundary.UrlBoundary;
10: import com.canoo.webtest.engine.StepExecutionException;
11: import com.canoo.webtest.engine.StepFailedException;
12: import com.canoo.webtest.self.TestBlock;
13: import com.canoo.webtest.self.ThrowAssert;
14: import com.canoo.webtest.steps.BaseStepTestCase;
15: import com.canoo.webtest.steps.Step;
16: import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
17: import com.gargoylesoftware.htmlunit.Page;
18: import com.gargoylesoftware.htmlunit.SubmitMethod;
19: import com.gargoylesoftware.htmlunit.WebResponse;
20: import com.gargoylesoftware.htmlunit.WebResponseData;
21: import com.gargoylesoftware.htmlunit.WebResponseImpl;
22:
23: /**
24: * @author Paul King
25: * @author Marc Guillemot
26: */
27: public class AbstractTargetActionTest extends BaseStepTestCase {
28: private TargetActionStub fTargetStep;
29:
30: protected Step createStep() {
31: return new TargetActionStub();
32: }
33:
34: protected void setUp() throws Exception {
35: super .setUp();
36: fTargetStep = (TargetActionStub) getStep();
37: }
38:
39: public void testUnprotectedCall() throws Exception {
40: executeStep(fTargetStep);
41: }
42:
43: public void testProtectedCalls() throws Exception {
44: final WebResponseData responseData = new WebResponseData(
45: new byte[] {}, 404, "Not Found", Collections.EMPTY_LIST);
46: final WebResponse webResponse = new WebResponseImpl(
47: responseData, UrlBoundary.tryCreateUrl("http://foo"),
48: SubmitMethod.GET, 1);
49: assertProtectionToStepFailed(
50: new FailingHttpStatusCodeException(webResponse),
51: StepFailedException.class);
52: final Logger stepLogger = Logger.getLogger(Step.class);
53: final Level oldLevel = stepLogger.getLevel();
54: //stepLogger.setLevel(Level.OFF); // dk: we produce an error log on purpose. don't show it here.
55: assertProtectionToStepFailed(new RuntimeException("any other"),
56: StepExecutionException.class);
57: stepLogger.setLevel(oldLevel);
58: }
59:
60: private void assertProtectionToStepFailed(final Exception toThrow,
61: final Class expectedException) {
62: ThrowAssert.assertThrows(expectedException, new TestBlock() {
63: public void call() throws Exception {
64: fTargetStep.setException(toThrow);
65: fTargetStep.execute();
66: }
67: });
68: }
69:
70: private static class TargetActionStub extends AbstractTargetAction {
71: private Exception fException;
72:
73: public void setException(final Exception exception) {
74: fException = exception;
75: }
76:
77: protected Page findTarget() throws Exception {
78: if (fException != null) {
79: throw fException;
80: }
81: return null;
82: }
83:
84: protected String getLogMessageForTarget() {
85: return "by stub";
86: }
87: }
88: }
|