KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > nodeDecoratorTests > EscapeCharacterRemovingNodeTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Joshua Kerievsky
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/nodeDecoratorTests/EscapeCharacterRemovingNodeTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/01/02 16:24:56 $
10
// $Revision: 1.18 $
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.nodeDecoratorTests;
28
29 import org.htmlparser.StringNodeFactory;
30 import org.htmlparser.tests.ParserTestCase;
31 import org.htmlparser.util.NodeIterator;
32 import org.htmlparser.util.ParserException;
33
34 public class EscapeCharacterRemovingNodeTest extends ParserTestCase {
35
36     static
37     {
38         System.setProperty ("org.htmlparser.tests.nodeDecoratorTests.EscapeCharacterRemovingNodeTest", "EscapeCharacterRemovingNodeTest");
39     }
40
41     public EscapeCharacterRemovingNodeTest(String JavaDoc name) {
42         super(name);
43     }
44
45     private String JavaDoc parseToObtainDecodedResult(String JavaDoc STRING_TO_DECODE)
46         throws ParserException {
47         StringBuffer JavaDoc decodedContent = new StringBuffer JavaDoc();
48
49         StringNodeFactory stringNodeFactory = new StringNodeFactory();
50         stringNodeFactory.setRemoveEscapes (true);
51         createParser(STRING_TO_DECODE);
52         parser.setNodeFactory(stringNodeFactory);
53
54         NodeIterator nodes = parser.elements();
55
56         while (nodes.hasMoreNodes())
57             decodedContent.append(nodes.nextNode().toPlainTextString());
58
59         return decodedContent.toString();
60     }
61
62     public void testTab() throws Exception JavaDoc {
63         String JavaDoc ENCODED_WORKSHOP_TITLE =
64             "The Testing & Refactoring Workshop\tCreated by Industrial Logic, Inc.";
65
66         String JavaDoc DECODED_WORKSHOP_TITLE =
67             "The Testing & Refactoring WorkshopCreated by Industrial Logic, Inc.";
68
69         assertEquals(
70             "tab in string",
71             DECODED_WORKSHOP_TITLE,
72             parseToObtainDecodedResult(ENCODED_WORKSHOP_TITLE));
73     }
74
75     public void testCarriageReturn() throws Exception JavaDoc {
76         String JavaDoc ENCODED_WORKSHOP_TITLE =
77             "The Testing & Refactoring Workshop\nCreated by Industrial Logic, Inc.\n";
78
79         String JavaDoc DECODED_WORKSHOP_TITLE =
80             "The Testing & Refactoring WorkshopCreated by Industrial Logic, Inc.";
81
82         assertEquals(
83             "tab in string",
84             DECODED_WORKSHOP_TITLE,
85             parseToObtainDecodedResult(ENCODED_WORKSHOP_TITLE));
86     }
87
88     public void testWithDecodingNodeDecorator() throws Exception JavaDoc {
89         String JavaDoc ENCODED_WORKSHOP_TITLE =
90             "The Testing & Refactoring Workshop\nCreated by Industrial Logic, Inc.\n";
91
92         String JavaDoc DECODED_WORKSHOP_TITLE =
93             "The Testing & Refactoring WorkshopCreated by Industrial Logic, Inc.";
94
95         StringBuffer JavaDoc decodedContent = new StringBuffer JavaDoc();
96
97         StringNodeFactory stringNodeFactory = new StringNodeFactory();
98         stringNodeFactory.setDecode (true);
99         stringNodeFactory.setRemoveEscapes (true);
100
101         createParser(ENCODED_WORKSHOP_TITLE);
102         parser.setNodeFactory(stringNodeFactory);
103         NodeIterator nodes = parser.elements();
104
105         while (nodes.hasMoreNodes())
106             decodedContent.append(nodes.nextNode().toPlainTextString());
107
108         assertEquals(
109             "tab in string",
110             DECODED_WORKSHOP_TITLE,
111             decodedContent.toString());
112
113     }
114 }
115
Popular Tags