1 21 22 package nu.xom.tests; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.IOException ; 27 28 import nu.xom.Attribute; 29 import nu.xom.Builder; 30 import nu.xom.Document; 31 import nu.xom.Element; 32 import nu.xom.ParsingException; 33 import nu.xom.Serializer; 34 35 45 public class RoundTripTest extends XOMTestCase { 46 47 private Builder builder; 48 private Serializer serializer; 49 private ByteArrayOutputStream out; 50 51 public RoundTripTest(String name) { 52 super(name); 53 } 54 55 protected void setUp() { 56 builder = new Builder(); 57 out = new ByteArrayOutputStream (); 58 serializer = new Serializer(out); 59 } 60 61 public void testTabInAttributeValue() 62 throws IOException , ParsingException { 63 Element test = new Element("test"); 64 test.addAttribute(new Attribute("tab", "\t")); 65 Document doc = new Document(test); 66 serializer.write(doc); 67 byte[] input = out.toByteArray(); 68 Document reparsed = builder.build(new ByteArrayInputStream (input)); 69 Element root = reparsed.getRootElement(); 70 Attribute tab = root.getAttribute("tab"); 71 assertEquals( 72 "Round trip did not preserve tab in attribute value", 73 "\t", tab.getValue() 74 ); 75 assertEquals("Unexpected error on round trip", doc, reparsed); 76 } 77 78 public void testCarriageReturnInAttributeValue() 79 throws IOException , ParsingException { 80 Element test = new Element("test"); 81 test.addAttribute(new Attribute("cr", "\r")); 82 Document doc = new Document(test); 83 serializer.write(doc); 84 byte[] input = out.toByteArray(); 85 Document reparsed = builder.build(new ByteArrayInputStream (input)); 86 Element root = reparsed.getRootElement(); 87 Attribute cr = root.getAttribute("cr"); 88 assertEquals( 89 "Round trip did not preserve carriage return in attribute value", 90 "\r", cr.getValue() 91 ); 92 assertEquals("Unexpected error on round trip", doc, reparsed); 93 } 94 95 public void testCarriageReturnInText() 96 throws IOException , ParsingException { 97 Element test = new Element("test"); 98 test.appendChild("\r"); 99 Document doc = new Document(test); 100 serializer.write(doc); 101 byte[] input = out.toByteArray(); 102 Document reparsed = builder.build(new ByteArrayInputStream (input)); 103 Element root = reparsed.getRootElement(); 104 String value = root.getValue(); 105 assertEquals( 106 "Round trip did not preserve carriage return in text", 107 "\r", value 108 ); 109 assertEquals("Unexpected error on round trip", doc, reparsed); 110 } 111 112 public void testLineFeedInAttributeValue() 113 throws IOException , ParsingException { 114 Element test = new Element("test"); 115 test.addAttribute(new Attribute("lf", "\n")); 116 Document doc = new Document(test); 117 serializer.write(doc); 118 byte[] input = out.toByteArray(); 119 Document reparsed = builder.build(new ByteArrayInputStream (input)); 120 Element root = reparsed.getRootElement(); 121 Attribute lf = root.getAttribute("lf"); 122 assertEquals( 123 "Round trip did not preserve carriage return in attribute value", 124 "\n", lf.getValue() 125 ); 126 assertEquals("Unexpected error on round trip", doc, reparsed); 127 } 128 129 public void testSpacesInAttributeValue() 130 throws IOException , ParsingException { 131 Element test = new Element("test"); 132 test.addAttribute(new Attribute("spaces", " ")); 133 Document doc = new Document(test); 134 serializer.write(doc); 135 byte[] input = out.toByteArray(); 136 Document reparsed = builder.build(new ByteArrayInputStream (input)); 137 Element root = reparsed.getRootElement(); 138 Attribute spaces = root.getAttribute("spaces"); 139 assertEquals( 140 "Round trip did not preserve spaces in attribute value", 141 " ", spaces.getValue() 142 ); 143 assertEquals("Unexpected error on round trip", doc, reparsed); 144 } 145 146 } 147 | Popular Tags |