001: // Copyright © 2004-2005 ASERT.
002: // Parts Copyright © 2005 Canoo Engineering AG, Switzerland.
003: // Released under the Canoo Webtest license.
004: package com.canoo.webtest.steps.store;
005:
006: import java.net.URL;
007: import java.util.Map;
008:
009: import org.apache.commons.httpclient.Cookie;
010: import org.apache.commons.httpclient.HttpState;
011: import org.apache.log4j.Logger;
012:
013: import com.canoo.webtest.boundary.HttpClientBoundary;
014: import com.canoo.webtest.engine.Context;
015: import com.canoo.webtest.engine.StepFailedException;
016: import com.canoo.webtest.util.MapUtil;
017: import com.gargoylesoftware.htmlunit.WebClient;
018:
019: /**
020: * Stores a cookie value (from the Http Response) into a property.<p>
021: * <p/>
022: * Either ant or dynamic properties are supported. The property can
023: * be checked subsequently with \"verifyProperty\".
024: *
025: * @author Paul King, ASERT
026: * @author Denis N. Antonioli
027: * @author Marc Guillemot
028: * @webtest.step category="Core"
029: * name="storeCookie"
030: * description="Provides the ability to store an <key>HTTP</key> Cookie value for later checking."
031: */
032: public class StoreCookie extends BaseStoreStep {
033: private static final Logger LOG = Logger
034: .getLogger(StoreCookie.class);
035: private String fCookieName;
036: private String fCookieValue; // will hold the value of the found cookie to give it to the report
037:
038: /**
039: * Sets the Name of the cookie of interest.<p>
040: *
041: * @param name The cookie Name
042: * @webtest.parameter required="yes"
043: * description="The name of the cookie of interest.
044: * If the property name is not specified, the cookie name is used as key to store the value found."
045: */
046: public void setName(final String name) {
047: fCookieName = name;
048: }
049:
050: public String getName() {
051: return fCookieName;
052: }
053:
054: public void doExecute() {
055: final Cookie[] cookies = getCookies(getContext());
056: LOG.debug("Found " + cookies.length + " cookie(s)");
057: if (cookies.length == 0) {
058: throw new StepFailedException("No cookies set!", this );
059: }
060: final Cookie cookie = findCookie(cookies);
061: if (cookie == null)
062: throw new StepFailedException("Cookie \"" + fCookieName
063: + "\" not set!", this );
064: else {
065: storeProperty(cookie.getValue(), getName());
066: fCookieValue = cookie.getValue();
067: }
068: }
069:
070: /**
071: * Adds the value of the found cookie
072: */
073: protected void addComputedParameters(final Map map) {
074: MapUtil.putIfNotNull(map, "-> cookie value", fCookieValue);
075: }
076:
077: /**
078: * Search the cookie with the requested property
079: * @param cookies the cookies to search in
080: * @return the first cookie found, <code>null</code> if none is found
081: */
082: Cookie findCookie(final Cookie[] cookies) {
083: for (int i = 0; i < cookies.length; i++) {
084: final Cookie cookie = cookies[i];
085: if (cookie.getName().equals(fCookieName)) {
086: return cookie;
087: }
088: }
089: return null;
090: }
091:
092: /**
093: * Gets the cookies for the current page of the context
094: * @param context the context
095: * @return the cookies
096: */
097: public static Cookie[] getCookies(final Context context) {
098: final URL url = context.getCurrentResponse().getWebResponse()
099: .getUrl();
100: WebClient webClient = context.getWebClient();
101: final HttpState stateForUrl = webClient.getWebConnection()
102: .getState();
103: return HttpClientBoundary.getCookiesFromState(stateForUrl, url);
104: }
105:
106: /**
107: * Verifies the parameters
108: */
109: protected void verifyParameters() {
110: super .verifyParameters();
111: nullParamCheck(fCookieName, "name");
112: nullResponseCheck();
113: }
114: }
|