001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.parser.text;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.nio.charset.Charset;
025:
026: import junit.framework.TestCase;
027:
028: import org.columba.core.io.StreamUtils;
029:
030: public class HtmlParserTest extends TestCase {
031:
032: public Charset testCharset = Charset.forName("iso-8859-1");
033:
034: public void testSubstituteURL1() {
035: String input = "This page http://columba.sourceforge.net is net!";
036:
037: String result = HtmlParser.substituteURL(input);
038: assertEquals(
039: "This page <A HREF=\"http://columba.sourceforge.net\">http://columba.sourceforge.net</A> is net!",
040: result);
041: }
042:
043: public void testSubstituteURL3() {
044: String input = "This page \t(http://columba.sourceforge.net/phpBB2/viewtopic.php?p=239#239) is net!";
045:
046: String result = HtmlParser.substituteURL(input);
047:
048: assertEquals(
049: "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!",
050: result);
051: }
052:
053: public void testSubstituteURL4() {
054: String input = "This page http://columba.sourceforge.net. is net!";
055:
056: String result = HtmlParser.substituteURL(input);
057: assertEquals(
058: "This page <A HREF=\"http://columba.sourceforge.net\">http://columba.sourceforge.net</A>. is net!",
059: result);
060: }
061:
062: public void testSubstituteURL5() {
063: String input = "This page http://test.com/$255. is net!";
064:
065: String result = HtmlParser.substituteURL(input);
066: assertEquals(
067: "This page <A HREF=\"http://test.com/$255\">http://test.com/$255</A>. is net!",
068: result);
069: }
070:
071: public void testSubstituteURL6() {
072: String input = "http://columbamail.org/jira/browse/CA-117<br>";
073:
074: String result = HtmlParser.substituteURL(input);
075:
076: assertEquals(
077: "<A HREF=\"http://columbamail.org/jira/browse/CA-117\">http://columbamail.org/jira/browse/CA-117</A><br>",
078: result);
079: }
080:
081: public void testRemoveComments1() {
082: String input = "<html><body><p><!- this is a text without comments -></p></body></html>";
083: String result = HtmlParser.removeComments(input);
084: assertTrue(result
085: .equals("<html><body><p><!- this is a text without comments -></p></body></html>"));
086: }
087:
088: public void testRemoveComments2() {
089: String input = "<html><body><p><!-- this is a comment -->And some text</p></body></html>";
090: String result = HtmlParser.removeComments(input);
091: assertTrue(result
092: .equals("<html><body><p>And some text</p></body></html>"));
093: }
094:
095: public void testRemoveComments3() {
096: String input = "<html><body><p><!-- this is a comment \n"
097: + "\t\twhich is spread over \n"
098: + " multiple lines-->And some text</p> \n\n"
099: + "<h1>A header </h><!-- a little comment --><p>"
100: + "<i>The end</i></p></body></html>";
101: String result = HtmlParser.removeComments(input);
102: assertTrue(result
103: .equals("<html><body><p>And some text</p> \n\n"
104: + "<h1>A header </h><p>"
105: + "<i>The end</i></p></body></html>"));
106: }
107:
108: public void testrestoreSpecialCharacters1() {
109: String input = "this ; is encoded!";
110:
111: assertEquals("this ; is encoded!", HtmlParser
112: .restoreSpecialCharacters(testCharset, input));
113: }
114:
115: public void testrestoreSpecialCharacters2() {
116: String input = "this ä is encoded!";
117:
118: assertEquals("this \u00e4 is encoded!", HtmlParser
119: .restoreSpecialCharacters(testCharset, input));
120: }
121:
122: public void testrestoreSpecialCharacters3() {
123: String input = "this is ½ encoded ; !";
124:
125: assertEquals("this is \u00bd encoded ; !", HtmlParser
126: .restoreSpecialCharacters(testCharset, input));
127: }
128:
129: public void testrestoreSpecialCharacters4() {
130: String input = "this is<encoded>!";
131:
132: assertEquals("this is<encoded>!", HtmlParser
133: .restoreSpecialCharacters(testCharset, input));
134: }
135:
136: public void testrestoreSpecialCharacters5() {
137: String input = "½ this is  this is ;this is ;this is ;\nthis is ;\nthis is ;";
138:
139: assertEquals(
140: "\u00bd this is \u00a0this is ;this is ;this is ;\nthis is ;\nthis is ;",
141: HtmlParser.restoreSpecialCharacters(testCharset, input));
142: }
143:
144: public void testsubstitiuteEmail1() {
145: String input = "test@mail.com";
146:
147: assertEquals(
148: ("<A HREF=\"mailto:" + input + "\">" + input + "</A>")
149: .toLowerCase(), HtmlParser
150: .substituteEmailAddress(input));
151: }
152:
153: public void testsubstitiuteEmail2() {
154: String input = "te+st09@mail.com";
155:
156: assertEquals(
157: ("<A HREF=\"mailto:" + input + "\">" + input + "</A>")
158: .toLowerCase(), HtmlParser
159: .substituteEmailAddress(input));
160: }
161:
162: public void testsubstitiuteEmail3() {
163: String input = "test09_+@mail.com";
164:
165: assertEquals(
166: ("<A HREF=\"mailto:" + input + "\">" + input + "</A>")
167: .toLowerCase(), HtmlParser
168: .substituteEmailAddress(input));
169: }
170:
171: public void testsubstitiuteEmail4() {
172: String input = "test09_+@mail.af.two.three.four.five.com";
173:
174: assertEquals(
175: ("<A HREF=\"mailto:" + input + "\">" + input + "</A>")
176: .toLowerCase(), HtmlParser
177: .substituteEmailAddress(input));
178: }
179:
180: public void testsubstitiuteEmailInUrl() {
181: String input = "http://www.supercool.com/name=super@cool.com";
182:
183: // url decode
184: String firstStep = HtmlParser.substituteURL(input);
185:
186: assertEquals("<A HREF=\"" + input + "\">" + input + "</A>",
187: HtmlParser.substituteEmailAddress(firstStep));
188: }
189: }
|