1 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 name) { 42 super(name); 43 } 44 45 private String parseToObtainDecodedResult(String STRING_TO_DECODE) 46 throws ParserException { 47 StringBuffer decodedContent = new StringBuffer (); 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 { 61 String ENCODED_WORKSHOP_TITLE = 62 "The Testing & Refactoring Workshop"; 63 64 String 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 { 74 String ENCODED_DIVISION_SIGN = 75 "÷ is the division sign."; 76 77 String 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 { 88 String 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 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 { 103 104 String ENCODED_BOGUS_CHARACTER_ENTITY = 105 "The character entity reference &divode; is bogus"; 106 107 String 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 { 117 118 String ENCODED_WITH_NON_BREAKING_SPACE = 119 "Here is string with \u00a0."; 120 121 String 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 |