01: // Copyright © 2002-2007 Canoo Engineering AG, Switzerland.
02: package com.canoo.webtest.steps.store;
03:
04: import java.net.URL;
05:
06: import org.apache.commons.httpclient.Cookie;
07:
08: import com.canoo.webtest.engine.Context;
09: import com.canoo.webtest.self.TestBlock;
10: import com.canoo.webtest.steps.BaseStepTestCase;
11: import com.canoo.webtest.steps.Step;
12:
13: /**
14: * Test class for {@link StoreCookie}.<p>
15: *
16: * @author Denis N. Antonioli
17: * @author Paul King, ASERT
18: * @author Marc Guillemot
19: */
20: public class StoreCookieTest extends BaseStepTestCase {
21: private StoreCookie fStep;
22: private TestBlock fTestBlock;
23:
24: protected Step createStep() {
25: return new StoreCookie();
26: }
27:
28: /**
29: * Adds a cookie for the domain of the currently loaded page in the context
30: * @param context the current context
31: * @param name the cookie name
32: * @param value the cookie value
33: */
34: public static void addCookie(final Context context,
35: final String name, final String value) {
36: final URL url = context.getCurrentResponse().getWebResponse()
37: .getUrl();
38: final Cookie cookie = new Cookie(url.getHost(), name, value,
39: "/", -1, false);
40: context.getWebClient().getWebConnection().getState().addCookie(
41: cookie);
42: }
43:
44: protected void setUp() throws Exception {
45: super .setUp();
46: assertEquals(0, StoreCookie.getCookies(getContext()).length);
47: fStep = (StoreCookie) getStep();
48: fTestBlock = new TestBlock() {
49: public void call() throws Exception {
50: executeStep(fStep);
51: }
52: };
53: }
54:
55: public void testVerifyInsufficientParameters() {
56: fStep.setProperty("someCookieProp");
57: assertStepRejectsNullParam("name", fTestBlock);
58: }
59:
60: public void testVerifyParametersWithoutPreviousPage() {
61: fStep.setProperty("somePropertyName");
62: fStep.setName("someCookieName");
63: assertStepRejectsNullResponse(fStep);
64: }
65:
66: public void testNoCookieSet() {
67: final String name = "SessionID";
68: fStep.setName(name);
69: fStep.setProperty("myProp");
70: fStep.setPropertyType(Step.PROPERTY_TYPE_DYNAMIC);
71: assertFailOnExecute(fStep, "Cookie not defined",
72: "No cookies set!");
73: }
74:
75: public void testCookieNotSet() {
76: final String name = "SessionID";
77: addCookie(getContext(), "Not" + name, "2046");
78: fStep.setName(name);
79: fStep.setProperty("myProp");
80: assertFailOnExecute(fStep, "Cookie not defined", "Cookie \""
81: + name + "\" not set!");
82: }
83:
84: }
|