KickJava   Java API By Example, From Geeks To Geeks.

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


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/DecodingNodeTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/01/02 16:24:56 $
10
// $Revision: 1.19 $
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 DecodingNodeTest extends ParserTestCase {
35
36     static
37     {
38         System.setProperty ("org.htmlparser.tests.nodeDecoratorTests.DecodingNodeTest", "DecodingNodeTest");
39     }
40
41     public DecodingNodeTest(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         StringNodeFactory stringNodeFactory = new StringNodeFactory();
49         stringNodeFactory.setDecode (true);
50         createParser(STRING_TO_DECODE);
51         parser.setNodeFactory(stringNodeFactory);
52         NodeIterator nodes = parser.elements();
53
54         while (nodes.hasMoreNodes())
55             decodedContent.append(nodes.nextNode().toPlainTextString());
56
57         return decodedContent.toString();
58     }
59
60     public void testAmpersand() throws Exception JavaDoc {
61         String JavaDoc ENCODED_WORKSHOP_TITLE =
62             "The Testing & Refactoring Workshop";
63
64         String JavaDoc DECODED_WORKSHOP_TITLE =
65             "The Testing & Refactoring Workshop";
66
67         assertEquals(
68             "ampersand in string",
69             DECODED_WORKSHOP_TITLE,
70             parseToObtainDecodedResult(ENCODED_WORKSHOP_TITLE));
71     }
72
73     public void testNumericReference() throws Exception JavaDoc {
74         String JavaDoc ENCODED_DIVISION_SIGN =
75             "÷ is the division sign.";
76
77         String JavaDoc DECODED_DIVISION_SIGN =
78             "\u00f7 is the division sign.";
79
80         assertEquals(
81             "numeric reference for division sign",
82             DECODED_DIVISION_SIGN,
83             parseToObtainDecodedResult(ENCODED_DIVISION_SIGN));
84     }
85
86
87     public void testReferencesInString () throws Exception JavaDoc {
88         String JavaDoc ENCODED_REFERENCE_IN_STRING =
89             "Thus, the character entity reference ÷ is a more convenient" +
90             " form than ÷ for obtaining the division sign (\u00f7)";
91
92         String JavaDoc DECODED_REFERENCE_IN_STRING =
93             "Thus, the character entity reference \u00f7 is a more convenient" +
94             " form than \u00f7 for obtaining the division sign (\u00f7)";
95
96         assertEquals (
97             "character references within a string",
98             DECODED_REFERENCE_IN_STRING,
99             parseToObtainDecodedResult(ENCODED_REFERENCE_IN_STRING));
100     }
101
102     public void testBogusCharacterEntityReference() throws Exception JavaDoc {
103
104         String JavaDoc ENCODED_BOGUS_CHARACTER_ENTITY =
105             "The character entity reference &divode; is bogus";
106
107         String JavaDoc DECODED_BOGUS_CHARACTER_ENTITY =
108             "The character entity reference &divode; is bogus";
109
110         assertEquals (
111             "bogus character entity reference",
112             DECODED_BOGUS_CHARACTER_ENTITY,
113             parseToObtainDecodedResult(ENCODED_BOGUS_CHARACTER_ENTITY));
114     }
115
116     public void testDecodingNonBreakingSpaceDoesNotOccur() throws Exception JavaDoc {
117
118         String JavaDoc ENCODED_WITH_NON_BREAKING_SPACE =
119             "Here is string with \u00a0.";
120
121         String JavaDoc DECODED_WITH_NON_BREAKING_SPACE =
122             "Here is string with \u00a0.";
123
124         assertEquals (
125             "bogus character entity reference",
126             DECODED_WITH_NON_BREAKING_SPACE,
127             parseToObtainDecodedResult(ENCODED_WITH_NON_BREAKING_SPACE));
128     }
129
130
131
132 }
133
Popular Tags