KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > LineNumberAssignedByNodeReaderTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Marc Novakowski
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/LineNumberAssignedByNodeReaderTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/01/02 16:24:55 $
10
// $Revision: 1.31 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.tests;
28
29 import java.util.Arrays JavaDoc;
30
31 import junit.framework.TestSuite;
32
33 import org.htmlparser.PrototypicalNodeFactory;
34 import org.htmlparser.tests.scannersTests.CompositeTagScannerTest.CustomTag;
35 import org.htmlparser.util.ParserException;
36
37 /**
38  * @author Somik Raha
39  *
40  * To change the template for this generated type comment go to
41  * Window>Preferences>Java>Code Generation>Code and Comments
42  */

43 public class LineNumberAssignedByNodeReaderTest extends ParserTestCase {
44
45     static
46     {
47         System.setProperty ("org.htmlparser.tests.LineNumberAssignedByNodeReaderTest", "LineNumberAssignedByNodeReaderTest");
48     }
49
50     public LineNumberAssignedByNodeReaderTest(String JavaDoc name) {
51         super(name);
52     }
53
54     /**
55      * Test to ensure that the <code>Tag</code> being created by the
56      * <code>CompositeTagScanner</code> has the correct startLine and endLine
57      * information in the <code>TagData</code> it is constructed with.
58      * @throws ParserException if there is a problem parsing the test data
59      */

60     public void testLineNumbers1() throws ParserException
61     {
62         testLineNumber("<Custom/>", 1, 0, 0, 0);
63     }
64
65     public void testLineNumbers2() throws ParserException
66     {
67         testLineNumber("<Custom />", 1, 0, 0, 0);
68     }
69
70     public void testLineNumbers3() throws ParserException
71     {
72         testLineNumber("<Custom></Custom>", 1, 0, 0, 0);
73     }
74
75     public void testLineNumbers4() throws ParserException
76     {
77         testLineNumber("<Custom>Content</Custom>", 1, 0, 0, 0);
78     }
79
80     public void testLineNumbers5() throws ParserException
81     {
82         testLineNumber("<Custom>Content<Custom></Custom>", 1, 0, 0, 0);
83     }
84
85     public void testLineNumbers6() throws ParserException
86     {
87         testLineNumber(
88             "<Custom>\n" +
89             " Content\n" +
90             "</Custom>",
91             1, 0, 0, 2
92         );
93     }
94
95     public void testLineNumbers7() throws ParserException
96     {
97         testLineNumber(
98             "Foo\n" +
99             "<Custom>\n" +
100             " Content\n" +
101             "</Custom>",
102             2, 1, 1, 3
103         );
104     }
105
106     public void testLineNumbers8() throws ParserException
107     {
108         testLineNumber(
109             "Foo\n" +
110             "<Custom>\n" +
111             " <Custom>SubContent</Custom>\n" +
112             "</Custom>",
113             2, 1, 1, 3
114         );
115     }
116
117     public void testLineNumbers9() throws ParserException
118     {
119         char[] oneHundredNewLines = new char[100];
120         Arrays.fill(oneHundredNewLines, '\n');
121         testLineNumber(
122             "Foo\n" +
123             new String JavaDoc(oneHundredNewLines) +
124             "<Custom>\n" +
125             " <Custom>SubContent</Custom>\n" +
126             "</Custom>",
127             2, 1, 101, 103
128         );
129     }
130
131     /**
132      * Helper method to ensure that the <code>Tag</code> being created by the
133      * <code>CompositeTagScanner</code> has the correct startLine and endLine
134      * information in the <code>TagData</code> it is constructed with.
135      * @param xml String containing HTML or XML to parse, containing a Custom tag
136      * @param numNodes int number of expected nodes returned by parser
137      * @param useNode int index of the node to test (should be of type CustomTag)
138      * @param startLine int the expected start line number of the tag
139      * @param endLine int the expected end line number of the tag
140      * @throws ParserException if there is an exception during parsing
141      */

142     private void testLineNumber(String JavaDoc xml, int numNodes, int useNode, int expectedStartLine, int expectedEndLine) throws ParserException {
143         createParser(xml);
144         parser.setNodeFactory (new PrototypicalNodeFactory (new CustomTag ()));
145         parseAndAssertNodeCount(numNodes);
146         assertType("custom node",CustomTag.class,node[useNode]);
147         CustomTag tag = (CustomTag)node[useNode];
148         assertEquals("start line", expectedStartLine, tag.getStartingLineNumber ());
149         assertEquals("end line", expectedEndLine, tag.getEndTag ().getEndingLineNumber ());
150     }
151
152     public static TestSuite suite() {
153         TestSuite suite = new TestSuite("Line Number Tests");
154         suite.addTestSuite(LineNumberAssignedByNodeReaderTest.class);
155         return (suite);
156     }
157 }
158
Popular Tags