KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > junit > protocol > http > parser > HtmlParserTester


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/junit/protocol/http/parser/HtmlParserTester.java,v 1.12 2004/02/22 19:20:24 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.junit.protocol.http.parser;
20
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23
24 import org.apache.jmeter.junit.JMeterTestCase;
25 import org.apache.jmeter.protocol.http.modifier.AnchorModifier;
26 import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult;
27 import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
28 import org.apache.jmeter.threads.JMeterContext;
29 import org.apache.jmeter.threads.JMeterContextService;
30
31 /**
32  * Created June 14, 2001
33  * @version $Revision: 1.12 $ Last updated: $Date: 2004/02/22 19:20:24 $
34  */

35 public class HtmlParserTester extends JMeterTestCase
36 {
37     AnchorModifier parser = new AnchorModifier();
38
39     /**
40      * Constructor for the HtmlParserTester object.
41      */

42     public HtmlParserTester(String JavaDoc name)
43     {
44         super(name);
45     }
46     
47     private JMeterContext jmctx = null;
48     
49     public void setUp()
50     {
51         jmctx = JMeterContextService.getContext();
52         parser.setThreadContext(jmctx);
53     }
54
55     /**
56      * A unit test for JUnit.
57      */

58     public void testSimpleParse() throws Exception JavaDoc
59     {
60         HTTPSampler config = makeUrlConfig(".*/index\\.html");
61         HTTPSampler context =
62             makeContext("http://www.apache.org/subdir/previous.html");
63         String JavaDoc responseText =
64             "<html><head><title>Test page</title></head><body>"
65                 + "<a HREF=\"index.html\">Goto index page</a></body></html>";
66         HTTPSampleResult result = new HTTPSampleResult();
67         jmctx.setCurrentSampler(context);
68         jmctx.setCurrentSampler(config);
69         result.setResponseData(responseText.getBytes());
70         result.setSampleLabel(context.toString());
71         result.setSamplerData(context.toString());
72         result.setURL(context.getUrl());
73         jmctx.setPreviousResult(result);
74         parser.process();
75         assertEquals(
76             "http://www.apache.org/subdir/index.html",
77             config.getUrl().toString());
78     }
79
80     public void testSimpleParse2() throws Exception JavaDoc
81     {
82         HTTPSampler config = makeUrlConfig("/index\\.html");
83         HTTPSampler context =
84             makeContext("http://www.apache.org/subdir/previous.html");
85         String JavaDoc responseText =
86             "<html><head><title>Test page</title></head><body>"
87                 + "<a HREF=\"/index.html\">Goto index page</a>"
88                 + "hfdfjiudfjdfjkjfkdjf"
89                 + "<b>bold text</b><a HREF=lowerdir/index.html>lower</a>"
90                 + "</body></html>";
91         HTTPSampleResult result = new HTTPSampleResult();
92         result.setResponseData(responseText.getBytes());
93         result.setSampleLabel(context.toString());
94         result.setURL(context.getUrl());
95         jmctx.setCurrentSampler(context);
96         jmctx.setCurrentSampler(config);
97         jmctx.setPreviousResult(result);
98         parser.process();
99         String JavaDoc newUrl = config.getUrl().toString();
100         assertTrue(
101             "http://www.apache.org/index.html".equals(newUrl)
102                 || "http://www.apache.org/subdir/lowerdir/index.html".equals(
103                     newUrl));
104     }
105
106     public void testSimpleParse3() throws Exception JavaDoc
107     {
108         HTTPSampler config = makeUrlConfig(".*index.*");
109         config.getArguments().addArgument("param1", "value1");
110         HTTPSampler context =
111             makeContext("http://www.apache.org/subdir/previous.html");
112         String JavaDoc responseText =
113             "<html><head><title>Test page</title></head><body>"
114                 + "<a HREF=\"/home/index.html?param1=value1\">"
115                 + "Goto index page</a></body></html>";
116         HTTPSampleResult result = new HTTPSampleResult();
117         result.setResponseData(responseText.getBytes());
118         result.setSampleLabel(context.toString());
119         result.setURL(context.getUrl());
120         jmctx.setCurrentSampler(context);
121         jmctx.setCurrentSampler(config);
122         jmctx.setPreviousResult(result);
123         parser.process();
124         String JavaDoc newUrl = config.getUrl().toString();
125         assertEquals(
126             "http://www.apache.org/home/index.html?param1=value1",
127             newUrl);
128     }
129
130     public void testSimpleParse4() throws Exception JavaDoc
131     {
132         HTTPSampler config = makeUrlConfig("/subdir/index\\..*");
133         HTTPSampler context =
134             makeContext("http://www.apache.org/subdir/previous.html");
135         String JavaDoc responseText =
136             "<html><head><title>Test page</title></head><body>"
137                 + "<A HREF=\"index.html\">Goto index page</A></body></html>";
138         HTTPSampleResult result = new HTTPSampleResult();
139         result.setResponseData(responseText.getBytes());
140         result.setSampleLabel(context.toString());
141         result.setURL(context.getUrl());
142         jmctx.setCurrentSampler(context);
143         jmctx.setCurrentSampler(config);
144         jmctx.setPreviousResult(result);
145         parser.process();
146         String JavaDoc newUrl = config.getUrl().toString();
147         assertEquals("http://www.apache.org/subdir/index.html", newUrl);
148     }
149
150     public void testSimpleParse5() throws Exception JavaDoc
151     {
152         HTTPSampler config = makeUrlConfig("/subdir/index\\.h.*");
153         HTTPSampler context =
154             makeContext("http://www.apache.org/subdir/one/previous.html");
155         String JavaDoc responseText =
156             "<html><head><title>Test page</title></head><body>"
157                 + "<a HREF=\"../index.html\">Goto index page</a></body></html>";
158         HTTPSampleResult result = new HTTPSampleResult();
159         result.setResponseData(responseText.getBytes());
160         result.setSampleLabel(context.toString());
161         result.setURL(context.getUrl());
162         jmctx.setCurrentSampler(context);
163         jmctx.setCurrentSampler(config);
164         jmctx.setPreviousResult(result);
165         parser.process();
166         String JavaDoc newUrl = config.getUrl().toString();
167         assertEquals("http://www.apache.org/subdir/index.html", newUrl);
168     }
169
170     public void testFailSimpleParse1() throws Exception JavaDoc
171     {
172         HTTPSampler config = makeUrlConfig(".*index.*?param2=.+1");
173         HTTPSampler context =
174             makeContext("http://www.apache.org/subdir/previous.html");
175         String JavaDoc responseText =
176             "<html><head><title>Test page</title></head><body>"
177                 + "<a HREF=\"/home/index.html?param1=value1\">"
178                 + "Goto index page</a></body></html>";
179         HTTPSampleResult result = new HTTPSampleResult();
180         String JavaDoc newUrl = config.getUrl().toString();
181         result.setResponseData(responseText.getBytes());
182         result.setSampleLabel(context.toString());
183         result.setURL(context.getUrl());
184         jmctx.setCurrentSampler(context);
185         jmctx.setCurrentSampler(config);
186         jmctx.setPreviousResult(result);
187         parser.process();
188         assertEquals(newUrl, config.getUrl().toString());
189     }
190
191     public void testFailSimpleParse3() throws Exception JavaDoc
192     {
193         HTTPSampler config = makeUrlConfig("/home/index.html");
194         HTTPSampler context =
195             makeContext("http://www.apache.org/subdir/previous.html");
196         String JavaDoc responseText =
197             "<html><head><title>Test page</title></head><body>"
198                 + "<a HREF=\"/home/index.html?param1=value1\">"
199                 + "Goto index page</a></body></html>";
200         HTTPSampleResult result = new HTTPSampleResult();
201         String JavaDoc newUrl = config.getUrl().toString();
202         result.setResponseData(responseText.getBytes());
203         result.setSampleLabel(context.toString());
204         result.setURL(context.getUrl());
205         jmctx.setCurrentSampler(context);
206         jmctx.setCurrentSampler(config);
207         jmctx.setPreviousResult(result);
208         parser.process();
209         assertEquals(newUrl + "?param1=value1", config.getUrl().toString());
210     }
211
212     public void testFailSimpleParse2() throws Exception JavaDoc
213     {
214         HTTPSampler config = makeUrlConfig(".*login\\.html");
215         HTTPSampler context =
216             makeContext("http://www.apache.org/subdir/previous.html");
217         String JavaDoc responseText =
218             "<html><head><title>Test page</title></head><body>"
219                 + "<a HREF=\"/home/index.html?param1=value1\">"
220                 + "Goto index page</a></body></html>";
221         HTTPSampleResult result = new HTTPSampleResult();
222         result.setResponseData(responseText.getBytes());
223         result.setSampleLabel(context.toString());
224         result.setURL(context.getUrl());
225         jmctx.setCurrentSampler(context);
226         jmctx.setPreviousResult(result);
227         parser.process();
228         String JavaDoc newUrl = config.getUrl().toString();
229         assertTrue(
230             !"http://www.apache.org/home/index.html?param1=value1".equals(
231                 newUrl));
232         assertEquals(config.getUrl().toString(), newUrl);
233     }
234
235     /**
236      * A unit test for JUnit.
237      */

238     public void testSimpleFormParse() throws Exception JavaDoc
239     {
240         HTTPSampler config = makeUrlConfig(".*index.html");
241         config.addArgument("test", "g.*");
242         config.setMethod(HTTPSampler.POST);
243         HTTPSampler context =
244             makeContext("http://www.apache.org/subdir/previous.html");
245         String JavaDoc responseText =
246             "<html><head><title>Test page</title></head><body>"
247                 + "<form action=\"index.html\" method=\"POST\">"
248                 + "<input type=\"checkbox\" name=\"test\""
249                 + " value=\"goto\">Goto index page</form></body></html>";
250         HTTPSampleResult result = new HTTPSampleResult();
251         result.setResponseData(responseText.getBytes());
252         result.setSampleLabel(context.toString());
253         result.setURL(context.getUrl());
254         jmctx.setCurrentSampler(context);
255         jmctx.setCurrentSampler(config);
256         jmctx.setPreviousResult(result);
257         parser.process();
258         assertEquals(
259             "http://www.apache.org/subdir/index.html",
260             config.getUrl().toString());
261         assertEquals("test=goto", config.getQueryString());
262     }
263
264     /**
265      * A unit test for JUnit.
266      */

267     public void testBadCharParse() throws Exception JavaDoc
268     {
269         HTTPSampler config = makeUrlConfig(".*index.html");
270         config.addArgument("te$st", "g.*");
271         config.setMethod(HTTPSampler.POST);
272         HTTPSampler context =
273             makeContext("http://www.apache.org/subdir/previous.html");
274         String JavaDoc responseText =
275             "<html><head><title>Test page</title></head><body>"
276                 + "<form action=\"index.html\" method=\"POST\">"
277                 + "<input type=\"checkbox\" name=\"te$st\""
278                 + " value=\"goto\">Goto index page</form></body></html>";
279         HTTPSampleResult result = new HTTPSampleResult();
280         result.setResponseData(responseText.getBytes());
281         result.setSampleLabel(context.toString());
282         result.setURL(context.getUrl());
283         jmctx.setCurrentSampler(context);
284         jmctx.setCurrentSampler(config);
285         jmctx.setPreviousResult(result);
286         parser.process();
287         assertEquals(
288             "http://www.apache.org/subdir/index.html",
289             config.getUrl().toString());
290         assertEquals("te%24st=goto", config.getQueryString());
291     }
292
293     private HTTPSampler makeContext(String JavaDoc url) throws MalformedURLException JavaDoc
294     {
295         URL JavaDoc u = new URL JavaDoc(url);
296         HTTPSampler context = new HTTPSampler();
297         context.setDomain(u.getHost());
298         context.setPath(u.getPath());
299         context.setPort(u.getPort());
300         context.setProtocol(u.getProtocol());
301         context.parseArguments(u.getQuery());
302         return context;
303     }
304
305     private HTTPSampler makeUrlConfig(String JavaDoc path)
306     {
307         HTTPSampler config = new HTTPSampler();
308         config.setDomain("www.apache.org");
309         config.setMethod(HTTPSampler.GET);
310         config.setPath(path);
311         config.setPort(80);
312         config.setProtocol("http");
313         return config;
314     }
315 }
316
Popular Tags