001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: MockLink.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.test;
009:
010: import com.uwyn.rife.engine.RequestMethod;
011: import java.util.Collection;
012: import java.util.LinkedHashMap;
013: import java.util.Map;
014: import org.w3c.dom.Node;
015:
016: /**
017: * Corresponds to a link in a HTML document after it has been parsed with
018: * {@link ParsedHtml#parse}.
019: *
020: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
021: * @version $Revision: 3634 $
022: * @since 1.1
023: */
024: public class MockLink {
025: private MockResponse mResponse;
026: private Node mNode;
027: private String mText;
028: private Map<String, String[]> mParameters;
029:
030: MockLink(MockResponse response, Node node) {
031: assert node != null;
032:
033: mResponse = response;
034: mNode = node;
035: mText = mNode.getTextContent();
036:
037: mParameters = MockConversation.extractParameters(getHref());
038: if (null == mParameters) {
039: mParameters = new LinkedHashMap<String, String[]>();
040: }
041: }
042:
043: /**
044: * Retrieves the DOM XML node that this link corresponds to.
045: *
046: * @return the corresponding DOM XML node
047: * @since 1.0
048: */
049: public Node getNode() {
050: return mNode;
051: }
052:
053: /**
054: * Creates a new {@link MockRequest} that contains the parameters this
055: * link.
056: *
057: * @return the created <code>MockRequest</code>
058: * @since 1.0
059: */
060: public MockRequest getRequest() {
061: return new MockRequest().method(RequestMethod.GET);
062: }
063:
064: /**
065: * Click this link with its current parameters and returns the response.
066: *
067: * @return the resulting {@link MockResponse}
068: * @since 1.0
069: */
070: public MockResponse click() {
071: return mResponse.getMockConversation().doRequest(getHref(),
072: getRequest());
073: }
074:
075: private String getAttribute(String attributeName) {
076: return ParsedHtml.getNodeAttribute(mNode, attributeName, null);
077: }
078:
079: /**
080: * Retrieves all the parameters of this link.
081: *
082: * @return a <code>Map</code> of the parameters with the names as the keys
083: * and their value arrays as the values
084: * @see #getParameterNames
085: * @see #hasParameter
086: * @see #getParameterValue
087: * @see #getParameterValues
088: * @since 1.1
089: */
090: public Map<String, String[]> getParameters() {
091: return mParameters;
092: }
093:
094: /**
095: * Retrieves all the parameter names of this link.
096: *
097: * @return a <code>Collection</code> of the parameter names
098: * @see #getParameters
099: * @see #hasParameter
100: * @see #getParameterValue
101: * @see #getParameterValues
102: * @since 1.1
103: */
104: public Collection<String> getParameterNames() {
105: return mParameters.keySet();
106: }
107:
108: /**
109: * Checks whether a named parameter is present in this link.
110: *
111: * @param name the name of the parameter to check
112: * @return <code>true</code> if the parameter is present; or
113: * <p><code>false</code> otherwise
114: * @see #getParameters
115: * @see #getParameterNames
116: * @see #getParameterValue
117: * @see #getParameterValues
118: * @since 1.1
119: */
120: public boolean hasParameter(String name) {
121: return mParameters.containsKey(name);
122: }
123:
124: /**
125: * Retrieves the first value of a parameter in this link.
126: *
127: * @param name the name of the parameter
128: * @return the first value of the parameter; or
129: * <p><code>null</code> if no such parameter could be found
130: * @see #getParameters
131: * @see #getParameterNames
132: * @see #hasParameter
133: * @see #getParameterValues
134: * @since 1.1
135: */
136: public String getParameterValue(String name) {
137: String[] values = getParameterValues(name);
138: if (null == values || 0 == values.length) {
139: return null;
140: }
141:
142: return values[0];
143: }
144:
145: /**
146: * Retrieves the values of a parameter in this link.
147: *
148: * @param name the name of the parameter
149: * @return the values of the parameter; or
150: * <p><code>null</code> if no such parameter could be found
151: * @see #getParameters
152: * @see #getParameterNames
153: * @see #hasParameter
154: * @see #getParameterValue
155: * @since 1.1
156: */
157: public String[] getParameterValues(String name) {
158: return mParameters.get(name);
159: }
160:
161: /**
162: * Retrieves the content of this link's <code>id</code> attribute.
163: *
164: * @return the content of the <code>id</code> attribute; or
165: * <p>null if no such attribute could be found
166: * @since 1.0
167: */
168: public String getId() {
169: return getAttribute("id");
170: }
171:
172: /**
173: * Retrieves the content of this link's <code>class</code> attribute.
174: *
175: * @return the content of the <code>class</code> attribute; or
176: * <p>null if no such attribute could be found
177: * @since 1.0
178: */
179: public String getClassName() {
180: return getAttribute("class");
181: }
182:
183: /**
184: * Retrieves the content of this link's <code>title</code> attribute.
185: *
186: * @return the content of the <code>title</code> attribute; or
187: * <p>null if no such attribute could be found
188: * @since 1.0
189: */
190: public String getTitle() {
191: return getAttribute("title");
192: }
193:
194: /**
195: * Retrieves the content of this link's <code>href</code> attribute.
196: *
197: * @return the content of the <code>href</code> attribute; or
198: * <p>null if no such attribute could be found
199: * @since 1.0
200: */
201: public String getHref() {
202: return getAttribute("href");
203: }
204:
205: /**
206: * Retrieves the content of this link's <code>target</code> attribute.
207: *
208: * @return the content of the <code>target</code> attribute; or
209: * <p>null if no such attribute could be found
210: * @since 1.0
211: */
212: public String getTarget() {
213: return getAttribute("target");
214: }
215:
216: /**
217: * Retrieves the content of this link's <code>name</code> attribute.
218: *
219: * @return the content of the <code>name</code> attribute; or
220: * <p>null if no such attribute could be found
221: * @since 1.0
222: */
223: public String getName() {
224: return getAttribute("name");
225: }
226:
227: /**
228: * Retrieves the text that this links surrounds.
229: *
230: * @return the surrounded text
231: * @since 1.0
232: */
233: public String getText() {
234: return mText;
235: }
236: }
|