Source Code Cross Referenced for TestHTMLParser.java in  » Testing » jakarta-jmeter » org » apache » jmeter » protocol » http » parser » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » jakarta jmeter » org.apache.jmeter.protocol.http.parser 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.