KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > parser > text > HtmlParserTest


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.parser.text;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.nio.charset.Charset JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.columba.core.io.StreamUtils;
29
30 public class HtmlParserTest extends TestCase {
31
32     public Charset JavaDoc testCharset = Charset.forName("iso-8859-1");
33     
34     public void testSubstituteURL1() {
35         String JavaDoc input = "This page http://columba.sourceforge.net is net!";
36
37         String JavaDoc result = HtmlParser.substituteURL(input);
38         assertEquals("This page <A HREF=\"http://columba.sourceforge.net\">http://columba.sourceforge.net</A> is net!", result);
39     }
40
41     public void testSubstituteURL3() {
42         String JavaDoc input = "This page \t(http://columba.sourceforge.net/phpBB2/viewtopic.php?p=239#239) is net!";
43
44         String JavaDoc result = HtmlParser.substituteURL(input);
45      
46         assertEquals(
47                 "This page \t(<A HREF=\"http://columba.sourceforge.net/phpBB2/viewtopic.php?p=239#239\">http://columba.sourceforge.net/phpBB2/viewtopic.php?p=239#239</A>) is net!",
48                 result);
49     }
50
51     public void testSubstituteURL4() {
52         String JavaDoc input = "This page http://columba.sourceforge.net. is net!";
53
54         String JavaDoc result = HtmlParser.substituteURL(input);
55         assertEquals("This page <A HREF=\"http://columba.sourceforge.net\">http://columba.sourceforge.net</A>. is net!", result);
56     }
57
58     public void testSubstituteURL5() {
59         String JavaDoc input = "This page http://test.com/$255. is net!";
60
61         String JavaDoc result = HtmlParser.substituteURL(input);
62         assertEquals("This page <A HREF=\"http://test.com/$255\">http://test.com/$255</A>. is net!", result);
63     }
64     
65     public void testSubstituteURL6() {
66         String JavaDoc input = "http://columbamail.org/jira/browse/CA-117<br>";
67
68         String JavaDoc result = HtmlParser.substituteURL(input);
69      
70         assertEquals(
71                 "<A HREF=\"http://columbamail.org/jira/browse/CA-117\">http://columbamail.org/jira/browse/CA-117</A><br>",
72                 result);
73     }
74     
75     public void testRemoveComments1() {
76         String JavaDoc input = "<html><body><p><!- this is a text without comments -></p></body></html>";
77         String JavaDoc result = HtmlParser.removeComments(input);
78         assertTrue(result
79                 .equals("<html><body><p><!- this is a text without comments -></p></body></html>"));
80     }
81
82     public void testRemoveComments2() {
83         String JavaDoc input = "<html><body><p><!-- this is a comment -->And some text</p></body></html>";
84         String JavaDoc result = HtmlParser.removeComments(input);
85         assertTrue(result
86                 .equals("<html><body><p>And some text</p></body></html>"));
87     }
88
89     public void testRemoveComments3() {
90         String JavaDoc input = "<html><body><p><!-- this is a comment \n"
91                 + "\t\twhich is spread over \n"
92                 + " multiple lines-->And some text</p> \n\n"
93                 + "<h1>A header </h><!-- a little comment --><p>"
94                 + "<i>The end</i></p></body></html>";
95         String JavaDoc result = HtmlParser.removeComments(input);
96         assertTrue(result.equals("<html><body><p>And some text</p> \n\n"
97                 + "<h1>A header </h><p>" + "<i>The end</i></p></body></html>"));
98     }
99     
100     public void testrestoreSpecialCharacters1() {
101         String JavaDoc input = "this &#59; is encoded!";
102         
103         assertEquals("this ; is encoded!", HtmlParser.restoreSpecialCharacters(testCharset, input));
104     }
105
106     public void testrestoreSpecialCharacters2() {
107         String JavaDoc input = "this &auml; is encoded!";
108         
109         assertEquals("this \u00e4 is encoded!", HtmlParser.restoreSpecialCharacters(testCharset, input));
110     }
111
112     public void testrestoreSpecialCharacters3() {
113         String JavaDoc input = "this is &frac12; encoded &#59; !";
114         
115         assertEquals("this is \u00bd encoded ; !", HtmlParser.restoreSpecialCharacters(testCharset, input));
116     }
117
118     public void testrestoreSpecialCharacters4() {
119         String JavaDoc input = "this is&lt;encoded&gt;!";
120         
121         assertEquals("this is<encoded>!", HtmlParser.restoreSpecialCharacters(testCharset, input));
122     }
123
124     public void testrestoreSpecialCharacters5() {
125         String JavaDoc input = "&frac12; this is &#160;this is &#59;this is &#59;this is &#59;\nthis is &#59;\nthis is &#59;";
126         
127         assertEquals("\u00bd this is \u00a0this is ;this is ;this is ;\nthis is ;\nthis is ;", HtmlParser.restoreSpecialCharacters(testCharset, input));
128     }
129     
130     public void testsubstitiuteEmail1() {
131         String JavaDoc input = "test@mail.com";
132         
133         assertEquals(("<A HREF=\"mailto:" + input +"\">"+ input + "</A>").toLowerCase(), HtmlParser.substituteEmailAddress(input) );
134     }
135
136     public void testsubstitiuteEmail2() {
137         String JavaDoc input = "te+st09@mail.com";
138         
139         assertEquals(("<A HREF=\"mailto:" + input +"\">"+ input + "</A>").toLowerCase(), HtmlParser.substituteEmailAddress(input) );
140     }
141
142     public void testsubstitiuteEmail3() {
143         String JavaDoc input = "test09_+@mail.com";
144         
145         assertEquals(("<A HREF=\"mailto:" + input +"\">"+ input + "</A>").toLowerCase(), HtmlParser.substituteEmailAddress(input) );
146     }
147
148     public void testsubstitiuteEmail4() {
149         String JavaDoc input = "test09_+@mail.af.two.three.four.five.com";
150         
151         assertEquals(("<A HREF=\"mailto:" + input +"\">"+ input + "</A>").toLowerCase(), HtmlParser.substituteEmailAddress(input) );
152     }
153
154     public void testsubstitiuteEmailInUrl() {
155         String JavaDoc input = "http://www.supercool.com/name=super@cool.com";
156         
157         // url decode
158
String JavaDoc firstStep = HtmlParser.substituteURL(input);
159         
160         assertEquals("<A HREF=\"" + input +"\">"+ input + "</A>", HtmlParser.substituteEmailAddress(firstStep) );
161     }
162 }
Popular Tags