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: TestParsedHtml.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.test;
009:
010: import com.uwyn.rife.tools.FileUtils;
011: import com.uwyn.rife.tools.StringUtils;
012: import java.util.Collection;
013: import java.util.List;
014: import java.util.Map;
015: import junit.framework.TestCase;
016:
017: public class TestParsedHtml extends TestCase {
018: public TestParsedHtml(String name) {
019: super (name);
020: }
021:
022: public void testTitle() throws Exception {
023: String text = FileUtils.readString(getClass().getClassLoader()
024: .getResource("parsed_html.html"), "UTF-8");
025: ParsedHtml parsed = ParsedHtml.parse(null, text);
026: assertEquals("This is the title", parsed.getTitle());
027: }
028:
029: public void testLinks() throws Exception {
030: String text = FileUtils.readString(getClass().getClassLoader()
031: .getResource("parsed_html.html"), "UTF-8");
032: ParsedHtml parsed = ParsedHtml.parse(null, text);
033: List<MockLink> links = parsed.getLinks();
034: assertEquals(2, links.size());
035:
036: MockLink link1 = links.get(0);
037: assertEquals(0, link1.getParameters().size());
038: assertEquals(0, link1.getParameterNames().size());
039: assertEquals("link1", link1.getText());
040: assertEquals("/link/url/1", link1.getHref());
041: assertEquals("linkid1", link1.getId());
042: assertEquals("linkclass1", link1.getClassName());
043: assertEquals("linktitle1", link1.getTitle());
044: assertEquals("linktarget1", link1.getTarget());
045: assertEquals("linkname1", link1.getName());
046:
047: MockLink link2 = links.get(1);
048: assertEquals(2, link2.getParameters().size());
049: assertEquals(2, link2.getParameterNames().size());
050: assertTrue(link2.hasParameter("param1"));
051: assertTrue(link2.hasParameter("param2"));
052: assertEquals("value1", link2.getParameterValue("param1"));
053: assertEquals("value2", link2.getParameterValue("param2"));
054: assertNull(link2.getParameterValue("unknown"));
055: assertEquals("value1,value3", StringUtils.join(link2
056: .getParameterValues("param1"), ","));
057: assertEquals("value2", StringUtils.join(link2
058: .getParameterValues("param2"), ","));
059: assertNull(link2.getParameterValues("unknown"));
060: assertEquals("", link2.getText());
061: assertEquals(
062: "/link/url/2?param1=value1¶m2=value2¶m1=value3",
063: link2.getHref());
064: assertEquals("linkid2", link2.getId());
065: assertEquals("linkclass2", link2.getClassName());
066: assertEquals("linktitle2", link2.getTitle());
067: assertEquals("linktarget2", link2.getTarget());
068: assertEquals("linkname2", link2.getName());
069:
070: assertSame(link1, parsed.getLinkWithId("linkid1"));
071: assertSame(link1, parsed.getLinkWithName("linkname1"));
072: assertSame(link1, parsed.getLinkWithText("link1"));
073:
074: assertSame(link2, parsed.getLinkWithId("linkid2"));
075: assertSame(link2, parsed.getLinkWithName("linkname2"));
076: assertSame(link2, parsed.getLinkWithText(""));
077: assertSame(link2, parsed.getLinkWithImageAlt("imagealt2"));
078: assertSame(link2, parsed.getLinkWithImageName("imagename2"));
079: }
080:
081: public void testUnknownLinks() throws Exception {
082: String text = FileUtils.readString(getClass().getClassLoader()
083: .getResource("parsed_html.html"), "UTF-8");
084: ParsedHtml parsed = ParsedHtml.parse(null, text);
085:
086: assertNull(parsed.getLinkWithId("unknown"));
087: assertNull(parsed.getLinkWithName("unknown"));
088: assertNull(parsed.getLinkWithText("unknown"));
089: assertNull(parsed.getLinkWithImageAlt("unknown"));
090: assertNull(parsed.getLinkWithImageName("unknown"));
091: }
092:
093: public void testForms() throws Exception {
094: String text = FileUtils.readString(getClass().getClassLoader()
095: .getResource("parsed_html.html"), "UTF-8");
096: ParsedHtml parsed = ParsedHtml.parse(null, text);
097: List<MockForm> forms = parsed.getForms();
098: assertEquals(2, forms.size());
099:
100: MockForm form1 = forms.get(0);
101: assertEquals("/form/url/1", form1.getAction());
102: assertEquals("POST", form1.getMethod());
103: assertEquals("formid1", form1.getId());
104: assertEquals("formclass1", form1.getClassName());
105: assertEquals("formtitle1", form1.getTitle());
106: assertEquals("formname1", form1.getName());
107:
108: MockForm form2 = forms.get(1);
109: assertEquals("/form/url/2", form2.getAction());
110: assertEquals("get", form2.getMethod());
111: assertEquals("formid2", form2.getId());
112: assertEquals("formclass2", form2.getClassName());
113: assertEquals("formtitle2", form2.getTitle());
114: assertEquals("formname2", form2.getName());
115:
116: assertSame(form1, parsed.getFormWithId("formid1"));
117: assertSame(form1, parsed.getFormWithName("formname1"));
118:
119: assertSame(form2, parsed.getFormWithId("formid2"));
120: assertSame(form2, parsed.getFormWithName("formname2"));
121: }
122:
123: public void testFormParameters() throws Exception {
124: String text = FileUtils.readString(getClass().getClassLoader()
125: .getResource("parsed_html.html"), "UTF-8");
126: ParsedHtml parsed = ParsedHtml.parse(null, text);
127: List<MockForm> forms = parsed.getForms();
128: assertEquals(2, forms.size());
129:
130: MockForm form1 = forms.get(0);
131:
132: Map<String, String[]> params = form1.getParameters();
133: assertEquals(16, params.size());
134: assertTrue(params.containsKey("paramhidden1"));
135: assertTrue(params.containsKey("paramhidden2"));
136: assertTrue(params.containsKey("paramtext1"));
137: assertTrue(params.containsKey("paramtext2"));
138: assertTrue(params.containsKey("parampassword1"));
139: assertTrue(params.containsKey("parampassword2"));
140: assertTrue(params.containsKey("paramcheckbox2"));
141: assertTrue(params.containsKey("paramcheckbox3"));
142: assertTrue(params.containsKey("paramcheckbox4"));
143: assertTrue(params.containsKey("paramradio2"));
144: assertTrue(params.containsKey("paramradio3"));
145: assertTrue(params.containsKey("textarea1"));
146: assertTrue(params.containsKey("textarea2"));
147: assertTrue(params.containsKey("select1"));
148: assertTrue(params.containsKey("select2"));
149: assertTrue(params.containsKey("select3"));
150: }
151:
152: public void testFormParameterNames() throws Exception {
153: String text = FileUtils.readString(getClass().getClassLoader()
154: .getResource("parsed_html.html"), "UTF-8");
155: ParsedHtml parsed = ParsedHtml.parse(null, text);
156: List<MockForm> forms = parsed.getForms();
157: assertEquals(2, forms.size());
158:
159: MockForm form1 = forms.get(0);
160:
161: Collection<String> paramvalues = form1.getParameterNames();
162: assertEquals(16, paramvalues.size());
163: assertTrue(paramvalues.contains("paramhidden1"));
164: assertTrue(paramvalues.contains("paramhidden2"));
165: assertTrue(paramvalues.contains("paramtext1"));
166: assertTrue(paramvalues.contains("paramtext2"));
167: assertTrue(paramvalues.contains("parampassword1"));
168: assertTrue(paramvalues.contains("parampassword2"));
169: assertTrue(paramvalues.contains("paramcheckbox2"));
170: assertTrue(paramvalues.contains("paramcheckbox3"));
171: assertTrue(paramvalues.contains("paramcheckbox4"));
172: assertTrue(paramvalues.contains("paramradio2"));
173: assertTrue(paramvalues.contains("paramradio3"));
174: assertTrue(paramvalues.contains("textarea1"));
175: assertTrue(paramvalues.contains("textarea2"));
176: assertTrue(paramvalues.contains("select1"));
177: assertTrue(paramvalues.contains("select2"));
178: assertTrue(paramvalues.contains("select3"));
179: }
180:
181: public void testFormDefaultValues() throws Exception {
182: String text = FileUtils.readString(getClass().getClassLoader()
183: .getResource("parsed_html.html"), "UTF-8");
184: ParsedHtml parsed = ParsedHtml.parse(null, text);
185: List<MockForm> forms = parsed.getForms();
186: assertEquals(2, forms.size());
187:
188: MockForm form1 = forms.get(0);
189:
190: Map<String, String[]> params = form1.getParameters();
191:
192: assertEquals("", StringUtils.join(params.get("paramhidden1"),
193: ","));
194: assertEquals("paramhidden2avalue,paramhidden2bvalue",
195: StringUtils.join(params.get("paramhidden2"), ","));
196: assertEquals("", StringUtils
197: .join(params.get("paramtext1"), ","));
198: assertEquals("paramtext2avalue,paramtext2bvalue", StringUtils
199: .join(params.get("paramtext2"), ","));
200: assertEquals("", StringUtils.join(params.get("parampassword1"),
201: ","));
202: assertEquals("parampassword2avalue,parampassword2bvalue",
203: StringUtils.join(params.get("parampassword2"), ","));
204: assertEquals("on", StringUtils.join(params
205: .get("paramcheckbox2"), ","));
206: assertEquals("paramcheckbox3value", StringUtils.join(params
207: .get("paramcheckbox3"), ","));
208: assertEquals("on,paramcheckbox4value", StringUtils.join(params
209: .get("paramcheckbox4"), ","));
210: assertEquals("on", StringUtils.join(params.get("paramradio2"),
211: ","));
212: assertEquals("paramradio3bvalue", StringUtils.join(params
213: .get("paramradio3"), ","));
214: assertEquals("", StringUtils.join(params.get("textarea1"), ","));
215: assertEquals("paramtextarea2avalue,paramtextarea2bvalue",
216: StringUtils.join(params.get("textarea2"), ","));
217: assertEquals(
218: "select1option1value,select1option3,select1option2",
219: StringUtils.join(params.get("select1"), ","));
220: assertEquals("select2option3", StringUtils.join(params
221: .get("select2"), ","));
222: assertEquals("select3option1", StringUtils.join(params
223: .get("select3"), ","));
224: }
225:
226: public void testFormParameterValue() throws Exception {
227: String text = FileUtils.readString(getClass().getClassLoader()
228: .getResource("parsed_html.html"), "UTF-8");
229: ParsedHtml parsed = ParsedHtml.parse(null, text);
230: List<MockForm> forms = parsed.getForms();
231: assertEquals(2, forms.size());
232:
233: MockForm form1 = forms.get(0);
234:
235: assertEquals("", form1.getParameterValue("paramhidden1"));
236: assertEquals("paramhidden2avalue", form1
237: .getParameterValue("paramhidden2"));
238: assertEquals("", form1.getParameterValue("paramtext1"));
239: assertEquals("paramtext2avalue", form1
240: .getParameterValue("paramtext2"));
241: assertEquals("", form1.getParameterValue("parampassword1"));
242: assertEquals("parampassword2avalue", form1
243: .getParameterValue("parampassword2"));
244: assertEquals("on", form1.getParameterValue("paramcheckbox2"));
245: assertEquals("paramcheckbox3value", form1
246: .getParameterValue("paramcheckbox3"));
247: assertEquals("on", form1.getParameterValue("paramcheckbox4"));
248: assertEquals("on", form1.getParameterValue("paramradio2"));
249: assertEquals("paramradio3bvalue", form1
250: .getParameterValue("paramradio3"));
251: assertEquals("", form1.getParameterValue("textarea1"));
252: assertEquals("paramtextarea2avalue", form1
253: .getParameterValue("textarea2"));
254: assertEquals("select1option1value", form1
255: .getParameterValue("select1"));
256: assertEquals("select2option3", form1
257: .getParameterValue("select2"));
258: assertEquals("select3option1", form1
259: .getParameterValue("select3"));
260: }
261:
262: public void testFormParameterValues() throws Exception {
263: String text = FileUtils.readString(getClass().getClassLoader()
264: .getResource("parsed_html.html"), "UTF-8");
265: ParsedHtml parsed = ParsedHtml.parse(null, text);
266: List<MockForm> forms = parsed.getForms();
267: assertEquals(2, forms.size());
268:
269: MockForm form1 = forms.get(0);
270:
271: assertEquals("", StringUtils.join(form1
272: .getParameterValues("paramhidden1"), ","));
273: assertEquals("paramhidden2avalue,paramhidden2bvalue",
274: StringUtils.join(form1
275: .getParameterValues("paramhidden2"), ","));
276: assertEquals("", StringUtils.join(form1
277: .getParameterValues("paramtext1"), ","));
278: assertEquals("paramtext2avalue,paramtext2bvalue", StringUtils
279: .join(form1.getParameterValues("paramtext2"), ","));
280: assertEquals("", StringUtils.join(form1
281: .getParameterValues("parampassword1"), ","));
282: assertEquals("parampassword2avalue,parampassword2bvalue",
283: StringUtils.join(form1
284: .getParameterValues("parampassword2"), ","));
285: assertEquals("on", StringUtils.join(form1
286: .getParameterValues("paramcheckbox2"), ","));
287: assertEquals("paramcheckbox3value", StringUtils.join(form1
288: .getParameterValues("paramcheckbox3"), ","));
289: assertEquals("on,paramcheckbox4value", StringUtils.join(form1
290: .getParameterValues("paramcheckbox4"), ","));
291: assertEquals("on", StringUtils.join(form1
292: .getParameterValues("paramradio2"), ","));
293: assertEquals("paramradio3bvalue", StringUtils.join(form1
294: .getParameterValues("paramradio3"), ","));
295: assertEquals("", StringUtils.join(form1
296: .getParameterValues("textarea1"), ","));
297: assertEquals("paramtextarea2avalue,paramtextarea2bvalue",
298: StringUtils.join(form1.getParameterValues("textarea2"),
299: ","));
300: assertEquals(
301: "select1option1value,select1option3,select1option2",
302: StringUtils.join(form1.getParameterValues("select1"),
303: ","));
304: assertEquals("select2option3", StringUtils.join(form1
305: .getParameterValues("select2"), ","));
306: assertEquals("select3option1", StringUtils.join(form1
307: .getParameterValues("select3"), ","));
308: }
309:
310: public void testUnknownForms() throws Exception {
311: String text = FileUtils.readString(getClass().getClassLoader()
312: .getResource("parsed_html.html"), "UTF-8");
313: ParsedHtml parsed = ParsedHtml.parse(null, text);
314:
315: assertNull(parsed.getFormWithId("unknown"));
316: assertNull(parsed.getFormWithName("unknown"));
317: }
318: }
|