Author: sebb
Date: Sun Nov 13 16:35:21 2005
New Revision: 344014
URL: http://svn.apache.org/viewcvs?rev=344014&view=rev
Log:
Tidy up constants; move test code to separate file
Added:
jakarta/jmeter/branches/rel-2-1/test/src/org/apache/jmeter/protocol/http/parser/
jakarta/jmeter/branches/rel-2-1/test/src/org/apache/jmeter/protocol/http/parser/TestHTMLParser.java
Modified:
jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/parser/HTMLParser.java
Modified:
jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/parser/HTMLParser.java
URL:
http://svn.apache.org/viewcvs/jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/parser/HTMLParser.java?rev=344014&r1=344013&r2=344014&view=diff
==============================================================================
---
jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/parser/HTMLParser.java
(original)
+++
jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/parser/HTMLParser.java
Sun Nov 13 16:35:21 2005
@@ -1,6 +1,5 @@
-// $Header$
/*
- * Copyright 2003-2004 The Apache Software Foundation.
+ * Copyright 2003-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,24 +17,11 @@
package org.apache.jmeter.protocol.http.parser;
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileReader;
import java.net.URL;
-import java.util.ArrayList;
import java.util.Collection;
-import java.util.Comparator;
import java.util.Hashtable;
import java.util.Iterator;
-import java.util.List;
-import java.util.Properties;
-import java.util.TreeSet;
-import java.util.Vector;
-import junit.framework.TestSuite;
-
-import org.apache.jmeter.junit.JMeterTestCase;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
@@ -47,15 +33,18 @@
* @version $Revision$ updated on $Date$
*/
public abstract class HTMLParser {
- /** Used to store the Logger (used for debug and error messages). */
- transient private static Logger log =
LoggingManager.getLoggerForClass();
+
+ private static final Logger log = LoggingManager.getLoggerForClass();
// Cache of parsers - parsers must be re-usable
private static Hashtable parsers = new Hashtable(3);
- private final static String PARSER_CLASSNAME = "htmlParser.className";
+ public final static String PARSER_CLASSNAME = "htmlParser.className";
// $NON-NLS-1$
- private final static String DEFAULT_PARSER =
"org.apache.jmeter.protocol.http.parser.HtmlParserHTMLParser";
+ public final static String DEFAULT_PARSER =
+ "org.apache.jmeter.protocol.http.parser.HtmlParserHTMLParser"; //
$NON-NLS-1$
+
+ private static final String JAVA_UTIL_LINKED_HASH_SET =
"java.util.LinkedHashSet"; // $NON-NLS-1$
/**
* Protected constructor to prevent instantiation except from within
@@ -128,7 +117,7 @@
// N.B. LinkedHashSet is Java 1.4
if (hasLinkedHashSet) {
try {
- col = (Collection)
Class.forName("java.util.LinkedHashSet").newInstance();
+ col = (Collection)
Class.forName(JAVA_UTIL_LINKED_HASH_SET).newInstance();
} catch (Exception e) {
throw new Error("Should not happen:" +
e.toString());
}
@@ -157,7 +146,7 @@
static {
boolean b;
try {
- Class.forName("java.util.LinkedHashSet");
+ Class.forName(JAVA_UTIL_LINKED_HASH_SET);
b = true;
} catch (ClassNotFoundException e) {
b = false;
@@ -218,243 +207,5 @@
*/
protected boolean isReusable() {
return false;
- }
-
- // ////////////////////////// TEST CODE FOLLOWS
- // /////////////////////////////
-
- public static class Test extends JMeterTestCase {
- private String parserName;
-
- private int testNumber = 0;
-
- public Test() {
- super();
- }
-
- public Test(String name) {
- super(name);
- }
-
- public Test(String name, int test) {
- super(name);
- testNumber = test;
- }
-
- public Test(String name, String parser, int test) {
- super(name);
- testNumber = test;
- parserName = parser;
- }
-
- private class TestClass // Can't instantiate
- {
- private TestClass() {
- };
- }
-
- private static class TestData {
- private String fileName;
-
- private String baseURL;
-
- private String expectedSet;
-
- private String expectedList;
-
- private TestData(String f, String b, String s, String
l) {
- fileName = f;
- baseURL = b;
- expectedSet = s;
- expectedList = l;
- }
-
- private TestData(String f, String b, String s) {
- this(f, b, s, null);
- }
- }
-
- // List of parsers to test. Should probably be derived
automatically
- private static final String[] PARSERS = {
"org.apache.jmeter.protocol.http.parser.HtmlParserHTMLParser",
-
"org.apache.jmeter.protocol.http.parser.JTidyHTMLParser",
-
"org.apache.jmeter.protocol.http.parser.RegexpHTMLParser" };
-
- private static final TestData[] TESTS = new TestData[] {
- new
TestData("testfiles/HTMLParserTestCase.html",
"http://localhost/mydir/myfile.html",
-
"testfiles/HTMLParserTestCase.set", "testfiles/HTMLParserTestCase.all"),
- new
TestData("testfiles/HTMLParserTestCaseWithBaseHRef.html",
"http://localhost/mydir/myfile.html",
-
"testfiles/HTMLParserTestCase.set", "testfiles/HTMLParserTestCase.all"),
- new
TestData("testfiles/HTMLParserTestCaseWithMissingBaseHRef.html",
-
"http://localhost/mydir/images/myfile.html", "testfiles/HTMLParserTestCase.set",
-
"testfiles/HTMLParserTestCase.all"),
- new
TestData("testfiles/HTMLParserTestCase2.html", "http:", // Dummy,
-
// as
-
// the
-
// file
-
// has
-
// no
-
//
entries
- "", ""),
- new
TestData("testfiles/HTMLParserTestCase3.html", "http:", // Dummy,
-
// as
-
// the
-
// file
-
// has
-
// no
-
//
entries
- "", ""),
- new
TestData("testfiles/HTMLParserTestCaseWithComments.html",
"http://localhost/mydir/myfile.html",
-
"testfiles/HTMLParserTestCase.set", "testfiles/HTMLParserTestCase.all"),
- new TestData("testfiles/HTMLScript.html",
"http://localhost/", "testfiles/HTMLScript.set",
- "testfiles/HTMLScript.all"),
- new
TestData("testfiles/HTMLParserTestFrames.html", "http://localhost/",
-
"testfiles/HTMLParserTestFrames.all", "testfiles/HTMLParserTestFrames.all"), };
-
- public static junit.framework.Test suite() {
- TestSuite suite = new TestSuite();
- suite.addTest(new Test("testDefaultParser"));
- suite.addTest(new Test("testParserDefault"));
- suite.addTest(new Test("testParserMissing"));
- suite.addTest(new Test("testNotParser"));
- suite.addTest(new Test("testNotCreatable"));
- for (int i = 0; i < PARSERS.length; i++) {
- TestSuite ps = new TestSuite(PARSERS[i]);//
Identify the
-
// subtests
- ps.addTest(new Test("testParserProperty",
PARSERS[i], 0));
- for (int j = 0; j < TESTS.length; j++) {
- TestSuite ts = new
TestSuite(TESTS[j].fileName);
- ts.addTest(new Test("testParserSet",
PARSERS[i], j));
- ts.addTest(new Test("testParserList",
PARSERS[i], j));
- ps.addTest(ts);
- }
- suite.addTest(ps);
- }
- return suite;
- }
-
- // Test if can instantiate parser using property name
- public void testParserProperty() throws Exception {
- Properties p = JMeterUtils.getJMeterProperties();
- if (p == null) {
- p =
JMeterUtils.getProperties("jmeter.properties");
- }
- p.setProperty(PARSER_CLASSNAME, parserName);
- getParser();
- }
-
- public void testDefaultParser() throws Exception {
- getParser();
- }
-
- public void testParserDefault() throws Exception {
- getParser(DEFAULT_PARSER);
- }
-
- public void testParserMissing() throws Exception {
- try {
- getParser("no.such.parser");
- } catch (HTMLParseError e) {
- if (e.getCause() instanceof
ClassNotFoundException) {
- // This is OK
- } else {
- throw e;
- }
- }
- }
-
- public void testNotParser() throws Exception {
- try {
- getParser("java.lang.String");
- } catch (HTMLParseError e) {
- if (e.getCause() instanceof ClassCastException)
- return;
- throw e;
- }
- }
-
- public void testNotCreatable() throws Exception {
- try {
- getParser(TestClass.class.getName());
- } catch (HTMLParseError e) {
- if (e.getCause() instanceof
InstantiationException)
- return;
- throw e;
- }
- }
-
- public void testParserSet() throws Exception {
- HTMLParser p = getParser(parserName);
- filetest(p, TESTS[testNumber].fileName,
TESTS[testNumber].baseURL, TESTS[testNumber].expectedSet, null,
- false);
- }
-
- public void testParserList() throws Exception {
- HTMLParser p = getParser(parserName);
- filetest(p, TESTS[testNumber].fileName,
TESTS[testNumber].baseURL, TESTS[testNumber].expectedList,
- new Vector(), true);
- }
-
- private static void filetest(HTMLParser p, String file, String
url, String resultFile, Collection c,
- boolean orderMatters) // Does the order matter?
- throws Exception {
- String parserName =
p.getClass().getName().substring("org.apache.jmeter.protocol.http.parser".length());
- log.debug("file " + file);
- File f = findTestFile(file);
- byte[] buffer = new byte[(int) f.length()];
- int len = new FileInputStream(f).read(buffer);
- assertEquals(len, buffer.length);
- Iterator result;
- if (c == null) {
- result = p.getEmbeddedResourceURLs(buffer, new
URL(url));
- } else {
- result = p.getEmbeddedResourceURLs(buffer, new
URL(url), c);
- }
- /*
- * TODO: Exact ordering is only required for some
tests; change the
- * comparison to do a set compare where necessary.
- */
- Iterator expected;
- if (orderMatters) {
- expected = getFile(resultFile).iterator();
- } else {
- // Convert both to Sets
- expected = new
TreeSet(getFile(resultFile)).iterator();
- TreeSet temp = new TreeSet(new Comparator() {
- public int compare(Object o1, Object
o2) {
- return
(o1.toString().compareTo(o2.toString()));
- }
- });
- while (result.hasNext()) {
- temp.add(result.next());
- }
- result = temp.iterator();
- }
-
- while (expected.hasNext()) {
- Object next = expected.next();
- assertTrue(parserName + "::Expecting another
result " + next, result.hasNext());
- try {
- assertEquals(parserName + "(" + file +
")", next, ((URL) result.next()).toString());
- } catch (ClassCastException e) {
- fail(parserName + "::Expected URL, but
got " + e.toString());
- }
- }
- assertFalse(parserName + "::Should have reached the end
of the results", result.hasNext());
- }
-
- // Get expected results as a List
- private static List getFile(String file) throws Exception {
- ArrayList al = new ArrayList();
- if (file != null && file.length() > 0) {
- BufferedReader br = new BufferedReader(new
FileReader(findTestFile(file)));
- String line = br.readLine();
- while (line != null) {
- al.add(line);
- line = br.readLine();
- }
- br.close();
- }
- return al;
- }
}
}
Added:
jakarta/jmeter/branches/rel-2-1/test/src/org/apache/jmeter/protocol/http/parser/TestHTMLParser.java
URL:
http://svn.apache.org/viewcvs/jakarta/jmeter/branches/rel-2-1/test/src/org/apache/jmeter/protocol/http/parser/TestHTMLParser.java?rev=344014&view=auto
==============================================================================
---
jakarta/jmeter/branches/rel-2-1/test/src/org/apache/jmeter/protocol/http/parser/TestHTMLParser.java
(added)
+++
jakarta/jmeter/branches/rel-2-1/test/src/org/apache/jmeter/protocol/http/parser/TestHTMLParser.java
Sun Nov 13 16:35:21 2005
@@ -0,0 +1,271 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.jmeter.protocol.http.parser;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileReader;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import java.util.TreeSet;
+import java.util.Vector;
+
+import org.apache.jmeter.junit.JMeterTestCase;
+import org.apache.jmeter.util.JMeterUtils;
+import org.apache.jorphan.logging.LoggingManager;
+import org.apache.log.Logger;
+
+import junit.framework.TestSuite;
+
+public class TestHTMLParser extends JMeterTestCase {
+ private static final Logger log = LoggingManager.getLoggerForClass();
+
+ public TestHTMLParser(String arg0) {
+ super(arg0);
+ }
+ private String parserName;
+
+ private int testNumber = 0;
+
+ public TestHTMLParser(String name, int test) {
+ super(name);
+ testNumber = test;
+ }
+
+ public TestHTMLParser(String name, String parser, int test) {
+ super(name);
+ testNumber = test;
+ parserName = parser;
+ }
+
+ private class TestClass // Can't instantiate
+ {
+ private TestClass() {
+ };
+ }
+
+ private static class TestData {
+ private String fileName;
+
+ private String baseURL;
+
+ private String expectedSet;
+
+ private String expectedList;
+
+ private TestData(String f, String b, String s, String l) {
+ fileName = f;
+ baseURL = b;
+ expectedSet = s;
+ expectedList = l;
+ }
+
+ private TestData(String f, String b, String s) {
+ this(f, b, s, null);
+ }
+ }
+
+ // List of parsers to test. Should probably be derived automatically
+ private static final String[] PARSERS = {
"org.apache.jmeter.protocol.http.parser.HtmlParserHTMLParser",
+ "org.apache.jmeter.protocol.http.parser.JTidyHTMLParser",
+ "org.apache.jmeter.protocol.http.parser.RegexpHTMLParser" };
+
+ private static final TestData[] TESTS = new TestData[] {
+ new TestData("testfiles/HTMLParserTestCase.html",
"http://localhost/mydir/myfile.html",
+ "testfiles/HTMLParserTestCase.set",
"testfiles/HTMLParserTestCase.all"),
+ new TestData("testfiles/HTMLParserTestCaseWithBaseHRef.html",
"http://localhost/mydir/myfile.html",
+ "testfiles/HTMLParserTestCase.set",
"testfiles/HTMLParserTestCase.all"),
+ new
TestData("testfiles/HTMLParserTestCaseWithMissingBaseHRef.html",
+ "http://localhost/mydir/images/myfile.html",
"testfiles/HTMLParserTestCase.set",
+ "testfiles/HTMLParserTestCase.all"),
+ new TestData("testfiles/HTMLParserTestCase2.html", "http:", //
Dummy,
+ //
as
+ //
the
+ //
file
+ //
has
+ //
no
+ //
entries
+ "", ""),
+ new TestData("testfiles/HTMLParserTestCase3.html", "http:", //
Dummy,
+ //
as
+ //
the
+ //
file
+ //
has
+ //
no
+ //
entries
+ "", ""),
+ new TestData("testfiles/HTMLParserTestCaseWithComments.html",
"http://localhost/mydir/myfile.html",
+ "testfiles/HTMLParserTestCase.set",
"testfiles/HTMLParserTestCase.all"),
+ new TestData("testfiles/HTMLScript.html", "http://localhost/",
"testfiles/HTMLScript.set",
+ "testfiles/HTMLScript.all"),
+ new TestData("testfiles/HTMLParserTestFrames.html",
"http://localhost/",
+ "testfiles/HTMLParserTestFrames.all",
"testfiles/HTMLParserTestFrames.all"), };
+
+ public static junit.framework.Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new TestHTMLParser("testDefaultParser"));
+ suite.addTest(new TestHTMLParser("testParserDefault"));
+ suite.addTest(new TestHTMLParser("testParserMissing"));
+ suite.addTest(new TestHTMLParser("testNotParser"));
+ suite.addTest(new TestHTMLParser("testNotCreatable"));
+ for (int i = 0; i < PARSERS.length; i++) {
+ TestSuite ps = new TestSuite(PARSERS[i]);// Identify the
+ // subtests
+ ps.addTest(new TestHTMLParser("testParserProperty",
PARSERS[i], 0));
+ for (int j = 0; j < TESTS.length; j++) {
+ TestSuite ts = new TestSuite(TESTS[j].fileName);
+ ts.addTest(new TestHTMLParser("testParserSet", PARSERS[i],
j));
+ ts.addTest(new TestHTMLParser("testParserList",
PARSERS[i], j));
+ ps.addTest(ts);
+ }
+ suite.addTest(ps);
+ }
+ return suite;
+ }
+
+ // Test if can instantiate parser using property name
+ public void testParserProperty() throws Exception {
+ Properties p = JMeterUtils.getJMeterProperties();
+ if (p == null) {
+ p = JMeterUtils.getProperties("jmeter.properties");
+ }
+ p.setProperty(HTMLParser.PARSER_CLASSNAME, parserName);
+ HTMLParser.getParser();
+ }
+
+ public void testDefaultParser() throws Exception {
+ HTMLParser.getParser();
+ }
+
+ public void testParserDefault() throws Exception {
+ HTMLParser.getParser(HTMLParser.DEFAULT_PARSER);
+ }
+
+ public void testParserMissing() throws Exception {
+ try {
+ HTMLParser.getParser("no.such.parser");
+ } catch (HTMLParseError e) {
+ if (e.getCause() instanceof ClassNotFoundException) {
+ // This is OK
+ } else {
+ throw e;
+ }
+ }
+ }
+
+ public void testNotParser() throws Exception {
+ try {
+ HTMLParser.getParser("java.lang.String");
+ } catch (HTMLParseError e) {
+ if (e.getCause() instanceof ClassCastException)
+ return;
+ throw e;
+ }
+ }
+
+ public void testNotCreatable() throws Exception {
+ try {
+ HTMLParser.getParser(TestClass.class.getName());
+ } catch (HTMLParseError e) {
+ if (e.getCause() instanceof InstantiationException)
+ return;
+ throw e;
+ }
+ }
+
+ public void testParserSet() throws Exception {
+ HTMLParser p = HTMLParser.getParser(parserName);
+ filetest(p, TESTS[testNumber].fileName, TESTS[testNumber].baseURL,
TESTS[testNumber].expectedSet, null,
+ false);
+ }
+
+ public void testParserList() throws Exception {
+ HTMLParser p = HTMLParser.getParser(parserName);
+ filetest(p, TESTS[testNumber].fileName, TESTS[testNumber].baseURL,
TESTS[testNumber].expectedList,
+ new Vector(), true);
+ }
+
+ private static void filetest(HTMLParser p, String file, String url,
String resultFile, Collection c,
+ boolean orderMatters) // Does the order matter?
+ throws Exception {
+ String parserName =
p.getClass().getName().substring("org.apache.jmeter.protocol.http.parser".length());
+ log.debug("file " + file);
+ File f = findTestFile(file);
+ byte[] buffer = new byte[(int) f.length()];
+ int len = new FileInputStream(f).read(buffer);
+ assertEquals(len, buffer.length);
+ Iterator result;
+ if (c == null) {
+ result = p.getEmbeddedResourceURLs(buffer, new URL(url));
+ } else {
+ result = p.getEmbeddedResourceURLs(buffer, new URL(url), c);
+ }
+ /*
+ * TODO: Exact ordering is only required for some tests; change the
+ * comparison to do a set compare where necessary.
+ */
+ Iterator expected;
+ if (orderMatters) {
+ expected = getFile(resultFile).iterator();
+ } else {
+ // Convert both to Sets
+ expected = new TreeSet(getFile(resultFile)).iterator();
+ TreeSet temp = new TreeSet(new Comparator() {
+ public int compare(Object o1, Object o2) {
+ return (o1.toString().compareTo(o2.toString()));
+ }
+ });
+ while (result.hasNext()) {
+ temp.add(result.next());
+ }
+ result = temp.iterator();
+ }
+
+ while (expected.hasNext()) {
+ Object next = expected.next();
+ assertTrue(parserName + "::Expecting another result " + next,
result.hasNext());
+ try {
+ assertEquals(parserName + "(" + file + ")", next, ((URL)
result.next()).toString());
+ } catch (ClassCastException e) {
+ fail(parserName + "::Expected URL, but got " +
e.toString());
+ }
+ }
+ assertFalse(parserName + "::Should have reached the end of the
results", result.hasNext());
+ }
+
+ // Get expected results as a List
+ private static List getFile(String file) throws Exception {
+ ArrayList al = new ArrayList();
+ if (file != null && file.length() > 0) {
+ BufferedReader br = new BufferedReader(new
FileReader(findTestFile(file)));
+ String line = br.readLine();
+ while (line != null) {
+ al.add(line);
+ line = br.readLine();
+ }
+ br.close();
+ }
+ return al;
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]