001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.parser;
020:
021: import java.io.BufferedReader;
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileReader;
025: import java.net.URL;
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Comparator;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Properties;
032: import java.util.TreeSet;
033: import java.util.Vector;
034:
035: import org.apache.jmeter.junit.JMeterTestCase;
036: import org.apache.jmeter.util.JMeterUtils;
037: import org.apache.jorphan.logging.LoggingManager;
038: import org.apache.log.Logger;
039:
040: import junit.framework.TestSuite;
041:
042: public class TestHTMLParser extends JMeterTestCase {
043: private static final Logger log = LoggingManager
044: .getLoggerForClass();
045:
046: public TestHTMLParser(String arg0) {
047: super (arg0);
048: }
049:
050: private String parserName;
051:
052: private int testNumber = 0;
053:
054: public TestHTMLParser(String name, int test) {
055: super (name);
056: testNumber = test;
057: }
058:
059: public TestHTMLParser(String name, String parser, int test) {
060: super (name);
061: testNumber = test;
062: parserName = parser;
063: }
064:
065: private static class StaticTestClass // Can't instantiate
066: {
067: private StaticTestClass() {
068: };
069: }
070:
071: private class TestClass // Can't instantiate
072: {
073: private TestClass() {
074: };
075: }
076:
077: private static class TestData {
078: private String fileName;
079:
080: private String baseURL;
081:
082: private String expectedSet;
083:
084: private String expectedList;
085:
086: private TestData(String f, String b, String s, String l) {
087: fileName = f;
088: baseURL = b;
089: expectedSet = s;
090: expectedList = l;
091: }
092:
093: // private TestData(String f, String b, String s) {
094: // this(f, b, s, null);
095: // }
096: }
097:
098: // List of parsers to test. Should probably be derived automatically
099: private static final String[] PARSERS = {
100: "org.apache.jmeter.protocol.http.parser.HtmlParserHTMLParser",
101: "org.apache.jmeter.protocol.http.parser.JTidyHTMLParser",
102: "org.apache.jmeter.protocol.http.parser.RegexpHTMLParser" };
103:
104: private static final TestData[] TESTS = new TestData[] {
105: new TestData("testfiles/HTMLParserTestCase.html",
106: "http://localhost/mydir/myfile.html",
107: "testfiles/HTMLParserTestCase.set",
108: "testfiles/HTMLParserTestCase.all"),
109: new TestData(
110: "testfiles/HTMLParserTestCaseWithBaseHRef.html",
111: "http://localhost/mydir/myfile.html",
112: "testfiles/HTMLParserTestCaseBase.set",
113: "testfiles/HTMLParserTestCaseBase.all"),
114: new TestData(
115: "testfiles/HTMLParserTestCaseWithBaseHRef2.html",
116: "http://localhost/mydir/myfile.html",
117: "testfiles/HTMLParserTestCaseBase.set",
118: "testfiles/HTMLParserTestCaseBase.all"),
119: new TestData(
120: "testfiles/HTMLParserTestCaseWithMissingBaseHRef.html",
121: "http://localhost/mydir/images/myfile.html",
122: "testfiles/HTMLParserTestCaseBase.set",
123: "testfiles/HTMLParserTestCaseBase.all"),
124: new TestData("testfiles/HTMLParserTestCase2.html", "http:",
125: "", ""), // Dummy as the file has no entries
126: new TestData("testfiles/HTMLParserTestCase3.html", "http:",
127: "", ""), // Dummy as the file has no entries
128: new TestData(
129: "testfiles/HTMLParserTestCaseWithComments.html",
130: "http://localhost/mydir/myfile.html",
131: "testfiles/HTMLParserTestCaseBase.set",
132: "testfiles/HTMLParserTestCaseBase.all"),
133: new TestData("testfiles/HTMLScript.html",
134: "http://localhost/", "testfiles/HTMLScript.set",
135: "testfiles/HTMLScript.all"),
136: new TestData("testfiles/HTMLParserTestFrames.html",
137: "http://localhost/",
138: "testfiles/HTMLParserTestFrames.all",
139: "testfiles/HTMLParserTestFrames.all"), };
140:
141: public static junit.framework.Test suite() {
142: TestSuite suite = new TestSuite("TestHTMLParser");
143: suite.addTest(new TestHTMLParser("testDefaultParser"));
144: suite.addTest(new TestHTMLParser("testParserDefault"));
145: suite.addTest(new TestHTMLParser("testParserMissing"));
146: suite.addTest(new TestHTMLParser("testNotParser"));
147: suite.addTest(new TestHTMLParser("testNotCreatable"));
148: suite.addTest(new TestHTMLParser("testNotCreatableStatic"));
149: for (int i = 0; i < PARSERS.length; i++) {
150: TestSuite ps = new TestSuite(PARSERS[i]);// Identify subtests
151: ps.addTest(new TestHTMLParser("testParserProperty",
152: PARSERS[i], 0));
153: for (int j = 0; j < TESTS.length; j++) {
154: TestSuite ts = new TestSuite(TESTS[j].fileName);
155: ts.addTest(new TestHTMLParser("testParserSet",
156: PARSERS[i], j));
157: ts.addTest(new TestHTMLParser("testParserList",
158: PARSERS[i], j));
159: ps.addTest(ts);
160: }
161: suite.addTest(ps);
162: }
163: return suite;
164: }
165:
166: // Test if can instantiate parser using property name
167: public void testParserProperty() throws Exception {
168: Properties p = JMeterUtils.getJMeterProperties();
169: if (p == null) {
170: p = JMeterUtils.getProperties("jmeter.properties");
171: }
172: p.setProperty(HTMLParser.PARSER_CLASSNAME, parserName);
173: HTMLParser.getParser();
174: }
175:
176: public void testDefaultParser() throws Exception {
177: HTMLParser.getParser();
178: }
179:
180: public void testParserDefault() throws Exception {
181: HTMLParser.getParser(HTMLParser.DEFAULT_PARSER);
182: }
183:
184: public void testParserMissing() throws Exception {
185: try {
186: HTMLParser.getParser("no.such.parser");
187: fail("Should not have been able to create the parser");
188: } catch (HTMLParseError e) {
189: if (e.getCause() instanceof ClassNotFoundException) {
190: // This is OK
191: } else {
192: throw e;
193: }
194: }
195: }
196:
197: public void testNotParser() throws Exception {
198: try {
199: HTMLParser.getParser("java.lang.String");
200: fail("Should not have been able to create the parser");
201: } catch (HTMLParseError e) {
202: if (e.getCause() instanceof ClassCastException)
203: return;
204: throw e;
205: }
206: }
207:
208: public void testNotCreatable() throws Exception {
209: try {
210: HTMLParser.getParser(TestClass.class.getName());
211: fail("Should not have been able to create the parser");
212: } catch (HTMLParseError e) {
213: if (e.getCause() instanceof InstantiationException)
214: return;
215: throw e;
216: }
217: }
218:
219: public void testNotCreatableStatic() throws Exception {
220: try {
221: HTMLParser.getParser(StaticTestClass.class.getName());
222: fail("Should not have been able to create the parser");
223: } catch (HTMLParseError e) {
224: if (e.getCause() instanceof ClassCastException)
225: return;
226: if (e.getCause() instanceof IllegalAccessException)
227: return;
228: throw e;
229: }
230: }
231:
232: public void testParserSet() throws Exception {
233: HTMLParser p = HTMLParser.getParser(parserName);
234: filetest(p, TESTS[testNumber].fileName,
235: TESTS[testNumber].baseURL,
236: TESTS[testNumber].expectedSet, null, false);
237: }
238:
239: public void testParserList() throws Exception {
240: HTMLParser p = HTMLParser.getParser(parserName);
241: filetest(p, TESTS[testNumber].fileName,
242: TESTS[testNumber].baseURL,
243: TESTS[testNumber].expectedList, new Vector(), true);
244: }
245:
246: private static void filetest(HTMLParser p, String file, String url,
247: String resultFile, Collection c, boolean orderMatters) // Does the order matter?
248: throws Exception {
249: String parserName = p.getClass().getName().substring(
250: "org.apache.jmeter.protocol.http.parser.".length());
251: String fname = file.substring(file.indexOf("/") + 1);
252: log.debug("file " + file);
253: File f = findTestFile(file);
254: byte[] buffer = new byte[(int) f.length()];
255: int len = new FileInputStream(f).read(buffer);
256: assertEquals(len, buffer.length);
257: Iterator result;
258: if (c == null) {
259: result = p.getEmbeddedResourceURLs(buffer, new URL(url));
260: } else {
261: result = p.getEmbeddedResourceURLs(buffer, new URL(url), c);
262: }
263: /*
264: * TODO: Exact ordering is only required for some tests; change the
265: * comparison to do a set compare where necessary.
266: */
267: Iterator expected;
268: if (orderMatters) {
269: expected = getFile(resultFile).iterator();
270: } else {
271: // Convert both to Sets
272: expected = new TreeSet(getFile(resultFile)).iterator();
273: TreeSet temp = new TreeSet(new Comparator() {
274: public int compare(Object o1, Object o2) {
275: return (o1.toString().compareTo(o2.toString()));
276: }
277: });
278: while (result.hasNext()) {
279: temp.add(result.next());
280: }
281: result = temp.iterator();
282: }
283:
284: while (expected.hasNext()) {
285: Object next = expected.next();
286: assertTrue(fname + "::" + parserName
287: + "::Expecting another result " + next, result
288: .hasNext());
289: try {
290: assertEquals(fname + "::" + parserName + "(next)",
291: next, ((URL) result.next()).toString());
292: } catch (ClassCastException e) {
293: fail(fname + "::" + parserName
294: + "::Expected URL, but got " + e.toString());
295: }
296: }
297: assertFalse(fname + "::" + parserName
298: + "::Should have reached the end of the results",
299: result.hasNext());
300: }
301:
302: // Get expected results as a List
303: private static List getFile(String file) throws Exception {
304: ArrayList al = new ArrayList();
305: if (file != null && file.length() > 0) {
306: BufferedReader br = new BufferedReader(new FileReader(
307: findTestFile(file)));
308: String line = br.readLine();
309: while (line != null) {
310: al.add(line);
311: line = br.readLine();
312: }
313: br.close();
314: }
315: return al;
316: }
317: }
|