01: // Copyright © 2004-2005 ASERT. Released under the Canoo Webtest license.
02: package com.canoo.webtest.steps.verify;
03:
04: import com.canoo.webtest.engine.StepFailedException;
05: import com.canoo.webtest.steps.store.StoreCookie;
06: import org.apache.commons.httpclient.Cookie;
07: import org.apache.log4j.Logger;
08:
09: /**
10: * Verifies a cookie value.<p>
11: *
12: * @author Paul King, ASERT
13: * @author Denis N. Antonioli
14: * @webtest.step category="Core"
15: * name="verifyCookie"
16: * description="Provides the ability to check an <key>HTTP</key> Cookie value."
17: */
18: public class VerifyCookie extends AbstractVerifyTextStep {
19: private static final Logger LOG = Logger
20: .getLogger(VerifyCookie.class);
21: private String fName;
22:
23: {
24: setOptionalText(true);
25: }
26:
27: /**
28: * Sets the Name of the cookie of interest
29: *
30: * @param name The new Name name
31: * @webtest.parameter required="yes"
32: * description="The name of the cookie of interest."
33: */
34: public void setName(String name) {
35: fName = name;
36: }
37:
38: public String getName() {
39: return fName;
40: }
41:
42: /**
43: * @param text The new text
44: * @webtest.parameter required="no"
45: * description="The text of the cookie of interest. If omitted just checks for existence of the cookie."
46: */
47: public void setText(String text) {
48: super .setText(text);
49: }
50:
51: public void doExecute() {
52: final Cookie[] cookies = StoreCookie.getCookies(getContext());
53: LOG.debug("Found " + cookies.length + " cookie(s)");
54: if (cookies.length == 0) {
55: throw new StepFailedException("No cookie available!", this );
56: }
57: for (int i = 0; i < cookies.length; i++) {
58: Cookie cookie = cookies[i];
59: if (cookie.getName().equals(getName())) {
60: // just check for existence of cookie if no text given
61: if (getText() == null) {
62: return;
63: }
64: if (verifyText(cookie.getValue())) {
65: return;
66: }
67: throw new StepFailedException("Wrong cookie \""
68: + getName() + "\" value found!", getText(),
69: cookie.getValue(), this );
70: }
71: }
72: throw new StepFailedException("Cookie \"" + getName()
73: + "\" not set!", this );
74: }
75:
76: /**
77: * Verifies the parameters
78: */
79: protected void verifyParameters() {
80: super .verifyParameters();
81: nullParamCheck(getName(), "name");
82: nullResponseCheck();
83: }
84: }
|