1 package org.columba.mail.parser.text; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.nio.charset.Charset ; 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 testCharset = Charset.forName("iso-8859-1"); 33 34 public void testSubstituteURL1() { 35 String input = "This page http://columba.sourceforge.net is net!"; 36 37 String 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 input = "This page \t(http://columba.sourceforge.net/phpBB2/viewtopic.php?p=239#239) is net!"; 43 44 String 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 input = "This page http://columba.sourceforge.net. is net!"; 53 54 String 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 input = "This page http://test.com/$255. is net!"; 60 61 String 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 input = "http://columbamail.org/jira/browse/CA-117<br>"; 67 68 String 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 input = "<html><body><p><!- this is a text without comments -></p></body></html>"; 77 String 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 input = "<html><body><p><!-- this is a comment -->And some text</p></body></html>"; 84 String 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 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 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 input = "this ; is encoded!"; 102 103 assertEquals("this ; is encoded!", HtmlParser.restoreSpecialCharacters(testCharset, input)); 104 } 105 106 public void testrestoreSpecialCharacters2() { 107 String input = "this ä is encoded!"; 108 109 assertEquals("this \u00e4 is encoded!", HtmlParser.restoreSpecialCharacters(testCharset, input)); 110 } 111 112 public void testrestoreSpecialCharacters3() { 113 String input = "this is ½ encoded ; !"; 114 115 assertEquals("this is \u00bd encoded ; !", HtmlParser.restoreSpecialCharacters(testCharset, input)); 116 } 117 118 public void testrestoreSpecialCharacters4() { 119 String input = "this is<encoded>!"; 120 121 assertEquals("this is<encoded>!", HtmlParser.restoreSpecialCharacters(testCharset, input)); 122 } 123 124 public void testrestoreSpecialCharacters5() { 125 String input = "½ this is  this is ;this is ;this is ;\nthis is ;\nthis is ;"; 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 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 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 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 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 input = "http://www.supercool.com/name=super@cool.com"; 156 157 String firstStep = HtmlParser.substituteURL(input); 159 160 assertEquals("<A HREF=\"" + input +"\">"+ input + "</A>", HtmlParser.substituteEmailAddress(firstStep) ); 161 } 162 } | Popular Tags |