1 16 17 18 package org.apache.xmlrpc; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.util.Hashtable ; 23 24 import junit.framework.Test; 25 import junit.framework.TestCase; 26 import junit.framework.TestSuite; 27 28 34 public class XmlWriterTest 35 extends TestCase 36 { 37 private ByteArrayOutputStream buffer; 38 private XmlWriter writer; 39 40 43 public XmlWriterTest(String testName) 44 { 45 super(testName); 46 } 47 48 51 public static Test suite() 52 { 53 return new TestSuite(XmlWriterTest.class); 54 } 55 56 59 public void setUp() 60 { 61 XmlRpc.setDebug(true); 62 buffer = new ByteArrayOutputStream (); 63 } 64 65 68 public void tearDown() 69 { 70 XmlRpc.setDebug(false); 71 } 72 73 public void testForceAlternateEncoding() 74 throws Exception 75 { 76 writer = new XmlWriter(buffer, null); 77 assertEquals("null should be forced to UTF-8", 78 XmlWriter.UTF8, writer.getEncoding()); 79 80 writer = new XmlWriter(buffer, XmlWriter.ISO8859_1); 81 assertEquals(XmlWriter.ISO8859_1 + " should be forced to " + 82 XmlWriter.UTF8, XmlWriter.UTF8, writer.getEncoding()); 83 84 writer = new XmlWriter(buffer, "ISO8859_15"); 85 assertEquals("ISO8859_15 should be forced to " + XmlWriter.UTF8, 86 XmlWriter.UTF8, writer.getEncoding()); 87 88 writer = new XmlWriter(buffer, "EUC_JP"); 89 assertEquals("EUC_JP should be forced to " + XmlWriter.UTF8, 90 XmlWriter.UTF8, writer.getEncoding()); 91 92 writer = new XmlWriter(buffer, XmlWriter.UTF16); 93 assertEquals(XmlWriter.UTF16 + " should remain " + XmlWriter.UTF16, 94 XmlWriter.UTF16, writer.getEncoding()); 95 } 96 97 public void testProlog() 98 throws IOException 99 { 100 final String EXPECTED_PROLOG = 101 XmlWriter.PROLOG_START + XmlWriter.PROLOG_END; 102 103 writer = new XmlWriter(buffer, XmlWriter.UTF8); 104 writer.write(new char[0], 0, 0); 105 writer.flush(); 106 assertEquals("Unexpected or missing XML prolog when writing char[]", 107 EXPECTED_PROLOG, buffer.toString()); 108 writer.write(' '); 110 writer.flush(); 111 assertEquals("Unexpected or missing XML prolog when writing char", 112 EXPECTED_PROLOG + ' ', buffer.toString()); 113 114 buffer = new ByteArrayOutputStream (); 115 writer = new XmlWriter(buffer, XmlWriter.UTF8); 116 writer.write(""); 117 writer.flush(); 118 assertEquals("Unexpected or missing XML prolog when writing String", 119 EXPECTED_PROLOG, buffer.toString()); 120 writer.write(""); 122 writer.flush(); 123 assertEquals("Unexpected or missing XML prolog when writing String", 124 EXPECTED_PROLOG, buffer.toString()); 125 126 } 127 128 public void testBasicResults() 129 throws Exception 130 { 131 try 132 { 133 writer = new XmlWriter(buffer, XmlWriter.UTF8); 134 135 String foobar = "foobar"; 136 writer.writeObject(foobar); 137 writer.flush(); 138 String postProlog = "<value>" + foobar + "</value>"; 139 assertTrue("Unexpected results from writing of String", 140 buffer.toString().endsWith(postProlog)); 141 142 Integer thirtySeven = new Integer (37); 143 writer.writeObject(thirtySeven); 144 writer.flush(); 145 postProlog += "<value><int>" + thirtySeven + "</int></value>"; 146 assertTrue("Unexpected results from writing of Integer", 147 buffer.toString().endsWith(postProlog)); 148 149 Boolean flag = Boolean.TRUE; 150 writer.writeObject(flag); 151 writer.flush(); 152 postProlog += "<value><boolean>1</boolean></value>"; 153 assertTrue("Unexpected results from writing of Boolean", 154 buffer.toString().endsWith(postProlog)); 155 156 Object [] array = { foobar, thirtySeven }; 157 writer.writeObject(array); 158 writer.flush(); 159 postProlog += "<value><array><data>"; 160 postProlog += "<value>" + foobar + "</value>"; 161 postProlog += "<value><int>" + thirtySeven + "</int></value>"; 162 postProlog += "</data></array></value>"; 163 assertTrue("Unexpected results from writing of Object[]", 164 buffer.toString().endsWith(postProlog)); 165 166 Hashtable map = new Hashtable (); 167 map.put(foobar, thirtySeven); 168 writer.writeObject(map); 169 writer.flush(); 170 postProlog += "<value><struct><member>"; 171 postProlog += "<name>" + foobar + "</name>"; 172 postProlog += "<value><int>" + thirtySeven + "</int></value>"; 173 postProlog += "</member></struct></value>"; 174 assertTrue("Unexpected results from writing of Hashtable", 175 buffer.toString().endsWith(postProlog)); 176 } 177 catch (Exception e) 178 { 179 e.printStackTrace(); 180 fail(e.getMessage()); 181 } 182 } 183 184 public void testWriteCharacterReference() 185 throws Exception 186 { 187 writer = new XmlWriter(buffer, null); 188 writer.hasWrittenProlog = true; 189 writer.writeObject(String.valueOf((char) 0x80)); 190 writer.flush(); 191 String postProlog = "<value>€</value>"; 192 assertTrue("Character reference not created as expected", 193 buffer.toString().endsWith(postProlog)); 194 } 195 } 196 | Popular Tags |