KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > tagTests > ObjectCollectionTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Somik Raha
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/ObjectCollectionTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/31 16:42:31 $
10
// $Revision: 1.22 $
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.tagTests;
28
29 import org.htmlparser.Node;
30 import org.htmlparser.PrototypicalNodeFactory;
31 import org.htmlparser.Tag;
32 import org.htmlparser.tags.Div;
33 import org.htmlparser.tags.Span;
34 import org.htmlparser.tags.TableTag;
35 import org.htmlparser.tests.ParserTestCase;
36 import org.htmlparser.util.ParserException;
37 import org.htmlparser.util.ParserUtils;
38
39 public class ObjectCollectionTest extends ParserTestCase {
40
41     static
42     {
43         System.setProperty ("org.htmlparser.tests.tagTests.ObjectCollectionTest", "ObjectCollectionTest");
44     }
45
46     public ObjectCollectionTest(String JavaDoc name) {
47         super(name);
48     }
49
50     private void assertSpanContent(Node[] spans) {
51         assertEquals("number of span objects expected",2,spans.length);
52         assertType("span",Span.class,spans[0]);
53         assertType("span",Span.class,spans[1]);
54         assertStringEquals(
55             "span[0] text",
56             "The Refactoring Challenge",
57             spans[0].toPlainTextString()
58         );
59         assertStringEquals(
60             "span[1] text",
61             "
id: 6",
62             spans[1].toPlainTextString()
63         );
64     }
65
66     public void testSimpleSearch() throws ParserException {
67         createParser(
68             "<SPAN>The Refactoring Challenge</SPAN>" +
69             "<SPAN>&#013;id: 6</SPAN>"
70         );
71         parser.setNodeFactory (new PrototypicalNodeFactory (new Span ()));
72         assertSpanContent(parser.extractAllNodesThatAre(Span.class));
73     }
74
75     public void testOneLevelNesting() throws ParserException {
76         createParser(
77             "<DIV>" +
78             " <SPAN>The Refactoring Challenge</SPAN>" +
79             " <SPAN>&#013;id: 6</SPAN>" +
80             "</DIV>"
81         );
82         parser.setNodeFactory (
83             new PrototypicalNodeFactory (
84                 new Tag[]
85                 {
86                     new Div (),
87                     new Span (),
88                 }));
89         parseAndAssertNodeCount(1);
90         Div div = (Div)node[0];
91         Node[] spans = ParserUtils.findTypeInNode (div, Span.class);
92         assertSpanContent(spans);
93     }
94
95     public void testTwoLevelNesting() throws ParserException {
96         createParser(
97             "<table>" +
98             " <DIV>" +
99             " <SPAN>The Refactoring Challenge</SPAN>" +
100             " <SPAN>&#013;id: 6</SPAN>" +
101             " </DIV>" +
102             "</table>"
103         );
104         parser.setNodeFactory (
105             new PrototypicalNodeFactory (
106                 new Tag[]
107                 {
108                     new Div (),
109                     new Span (),
110                     new TableTag (),
111                 }));
112         parseAndAssertNodeCount(1);
113         TableTag tableTag = (TableTag)node[0];
114         Node[] spans = ParserUtils.findTypeInNode (tableTag, Span.class);
115         assertSpanContent(spans);
116     }
117 }
118
Popular Tags