1 package org.sapia.util.xml.idefix.test; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.IOException ; 5 import java.util.ArrayList ; 6 import junit.framework.TestCase; 7 import junit.textui.TestRunner; 8 import org.sapia.util.xml.Attribute; 9 import org.sapia.util.xml.idefix.XmlScribe; 10 11 22 public class XmlScribeTest extends TestCase { 23 24 private XmlScribe _theScribe; 25 26 public static void main(String [] args) { 27 TestRunner.run(XmlScribeTest.class); 28 } 29 30 public XmlScribeTest(String aName) { 31 super(aName); 32 _theScribe = new XmlScribe("UTF-8"); 33 } 34 35 public void testInvalidStreamEncoding() throws Exception { 36 try { 37 _theScribe.setStreamEncoding("UTF-256"); 38 _theScribe.composeXmlDeclaration(null, new ByteArrayOutputStream ()); 39 fail("XMLBuffer should not encode using the invalid UTF-256 encoding!"); 40 } catch (IOException ioe) { 41 } finally { 43 _theScribe.setStreamEncoding("UTF-8"); 44 } 45 } 46 47 48 public void testXmlDeclaration_Buffer() throws Exception { 49 StringBuffer aBuffer = new StringBuffer (); 50 _theScribe.composeXmlDeclaration(null, aBuffer); 51 assertEquals("<?xml version=\"1.0\" ?>", aBuffer.toString()); 52 53 aBuffer = new StringBuffer (); 54 _theScribe.composeXmlDeclaration("", aBuffer); 55 assertEquals("<?xml version=\"1.0\" ?>", aBuffer.toString()); 56 57 aBuffer = new StringBuffer (); 58 _theScribe.composeXmlDeclaration("UTF-8", aBuffer); 59 assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>", aBuffer.toString()); 60 } 61 62 63 public void testXmlDeclaration_Streaming() throws Exception { 64 ByteArrayOutputStream aStream = new ByteArrayOutputStream (); 65 _theScribe.composeXmlDeclaration(null, aStream); 66 assertEquals("<?xml version=\"1.0\" ?>", new String (aStream.toByteArray(), "UTF-8")); 67 68 aStream = new ByteArrayOutputStream (); 69 _theScribe.composeXmlDeclaration("", aStream); 70 assertEquals("<?xml version=\"1.0\" ?>", new String (aStream.toByteArray(), "UTF-8")); 71 72 aStream = new ByteArrayOutputStream (); 73 _theScribe.composeXmlDeclaration("UTF-8", aStream); 74 assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>", new String (aStream.toByteArray(), "UTF-8")); 75 } 76 77 78 public void testUnqualifiedEmptyElement_Buffer() throws Exception { 79 StringBuffer aBuffer = new StringBuffer (); 80 _theScribe.composeStartingElement(null, "foo", true, aBuffer); 81 assertEquals("<foo />", aBuffer.toString()); 82 83 ArrayList aList = new ArrayList (1); 84 aList.add(new Attribute("bar", "value")); 85 aBuffer = new StringBuffer (); 86 _theScribe.composeStartingElement(null, "foo", aList, true, aBuffer); 87 assertEquals("<foo bar=\"value\" />", aBuffer.toString()); 88 } 89 90 91 public void testUnqualifiedEmptyElement_Streaming() throws Exception { 92 ByteArrayOutputStream aStream = new ByteArrayOutputStream (); 93 _theScribe.composeStartingElement(null, "foo", true, aStream); 94 assertEquals("<foo />", new String (aStream.toByteArray(), "UTF-8")); 95 96 ArrayList aList = new ArrayList (1); 97 aList.add(new Attribute("bar", "value")); 98 aStream = new ByteArrayOutputStream (); 99 _theScribe.composeStartingElement(null, "foo", aList, true, aStream); 100 assertEquals("<foo bar=\"value\" />", new String (aStream.toByteArray(), "UTF-8")); 101 } 102 103 104 public void testQualifiedEmptyElement_Buffer() throws Exception { 105 StringBuffer aBuffer = new StringBuffer (); 106 _theScribe.composeStartingElement("A", "foo", true, aBuffer); 107 assertEquals("<A:foo />", aBuffer.toString()); 108 109 ArrayList aList = new ArrayList (1); 110 aList.add(new Attribute("bar", "value")); 111 aBuffer = new StringBuffer (); 112 _theScribe.composeStartingElement("B", "foo", aList, true, aBuffer); 113 assertEquals("<B:foo bar=\"value\" />", aBuffer.toString()); 114 } 115 116 117 public void testQualifiedEmptyElement_Streaming() throws Exception { 118 ByteArrayOutputStream aStream = new ByteArrayOutputStream (); 119 _theScribe.composeStartingElement("A", "foo", true, aStream); 120 assertEquals("<A:foo />", new String (aStream.toByteArray(), "UTF-8")); 121 122 ArrayList aList = new ArrayList (1); 123 aList.add(new Attribute("bar", "value")); 124 aStream = new ByteArrayOutputStream (); 125 _theScribe.composeStartingElement("B", "foo", aList, true, aStream); 126 assertEquals("<B:foo bar=\"value\" />", new String (aStream.toByteArray(), "UTF-8")); 127 } 128 129 130 public void testUnqualifiedStartingElement_Buffer() throws Exception { 131 StringBuffer aBuffer = new StringBuffer (); 132 _theScribe.composeStartingElement(null, "foo", false, aBuffer); 133 assertEquals("<foo>", aBuffer.toString()); 134 135 ArrayList aList = new ArrayList (1); 136 aList.add(new Attribute("bar", "value")); 137 aBuffer = new StringBuffer (); 138 _theScribe.composeStartingElement(null, "foo", aList, false, aBuffer); 139 assertEquals("<foo bar=\"value\">", aBuffer.toString()); 140 } 141 142 143 public void testUnqualifiedStartingElement_Streaming() throws Exception { 144 ByteArrayOutputStream aStream = new ByteArrayOutputStream (); 145 _theScribe.composeStartingElement(null, "foo", false, aStream); 146 assertEquals("<foo>", new String (aStream.toByteArray(), "UTF-8")); 147 148 ArrayList aList = new ArrayList (1); 149 aList.add(new Attribute("bar", "value")); 150 aStream = new ByteArrayOutputStream (); 151 _theScribe.composeStartingElement(null, "foo", aList, false, aStream); 152 assertEquals("<foo bar=\"value\">", new String (aStream.toByteArray(), "UTF-8")); 153 } 154 155 156 public void testQualifiedStartingElement_Buffer() throws Exception { 157 StringBuffer aBuffer = new StringBuffer (); 158 _theScribe.composeStartingElement("A", "foo", false, aBuffer); 159 assertEquals("<A:foo>", aBuffer.toString()); 160 161 ArrayList aList = new ArrayList (1); 162 aList.add(new Attribute("bar", "value")); 163 aBuffer = new StringBuffer (); 164 _theScribe.composeStartingElement("B", "foo", aList, false, aBuffer); 165 assertEquals("<B:foo bar=\"value\">", aBuffer.toString()); 166 } 167 168 169 public void testQualifiedStartingElement_Streaming() throws Exception { 170 ByteArrayOutputStream aStream = new ByteArrayOutputStream (); 171 _theScribe.composeStartingElement("A", "foo", false, aStream); 172 assertEquals("<A:foo>", new String (aStream.toByteArray(), "UTF-8")); 173 174 ArrayList aList = new ArrayList (1); 175 aList.add(new Attribute("bar", "value")); 176 aStream = new ByteArrayOutputStream (); 177 _theScribe.composeStartingElement("B", "foo", aList, false, aStream); 178 assertEquals("<B:foo bar=\"value\">", new String (aStream.toByteArray(), "UTF-8")); 179 } 180 181 182 public void testUnqualifiedEndingElement_Buffer() throws Exception { 183 StringBuffer aBuffer = new StringBuffer (); 184 _theScribe.composeEndingElement("", "foo", aBuffer); 185 assertEquals("</foo>", aBuffer.toString()); 186 187 aBuffer = new StringBuffer (); 188 _theScribe.composeEndingElement(null, "foo", aBuffer); 189 assertEquals("</foo>", aBuffer.toString()); 190 } 191 192 193 public void testUnqualifiedEndingElement_Streaming() throws Exception { 194 ByteArrayOutputStream aStream = new ByteArrayOutputStream (); 195 _theScribe.composeEndingElement("", "foo", aStream); 196 assertEquals("</foo>", new String (aStream.toByteArray(), "UTF-8")); 197 198 aStream = new ByteArrayOutputStream (); 199 _theScribe.composeEndingElement(null, "foo", aStream); 200 assertEquals("</foo>", new String (aStream.toByteArray(), "UTF-8")); 201 } 202 203 204 public void testQualifiedEndingElement_Buffer() throws Exception { 205 StringBuffer aBuffer = new StringBuffer (); 206 _theScribe.composeEndingElement("A", "foo", aBuffer); 207 assertEquals("</A:foo>", aBuffer.toString()); 208 } 209 210 211 public void testQualifiedEndingElement_Streaming() throws Exception { 212 ByteArrayOutputStream aStream = new ByteArrayOutputStream (); 213 _theScribe.composeEndingElement("A", "foo", aStream); 214 assertEquals("</A:foo>", new String (aStream.toByteArray(), "UTF-8")); 215 } 216 217 218 public void testQualifiedAttribute_Buffer() throws Exception { 219 ArrayList aList = new ArrayList (1); 220 aList.add(new Attribute("Z", "bar","value")); 221 StringBuffer aBuffer = new StringBuffer (); 222 _theScribe.composeStartingElement("C", "foo", aList, true, aBuffer); 223 assertEquals("<C:foo Z:bar=\"value\" />", aBuffer.toString()); 224 } 225 226 227 public void testQualifiedAttribute_Streaming() throws Exception { 228 ArrayList aList = new ArrayList (1); 229 aList.add(new Attribute("Z", "bar","value")); 230 ByteArrayOutputStream aStream = new ByteArrayOutputStream (); 231 _theScribe.composeStartingElement("C", "foo", aList, true, aStream); 232 assertEquals("<C:foo Z:bar=\"value\" />", new String (aStream.toByteArray(), "UTF-8")); 233 } 234 235 236 public void testXMLReservedCharacters_Buffer() throws Exception { 237 ArrayList aList = new ArrayList (1); 238 aList.add(new Attribute("bar","<\"value\" & \'value\'>")); 239 StringBuffer aBuffer = new StringBuffer (); 240 _theScribe.composeStartingElement("C", "foo", aList, true, aBuffer); 241 assertEquals("<C:foo bar=\"<"value" & 'value'>\" />", aBuffer.toString()); 242 } 243 244 245 public void testXMLReservedCharacters_Streaming() throws Exception { 246 ArrayList aList = new ArrayList (1); 247 aList.add(new Attribute("bar","<\"value\" & \'value\'>")); 248 ByteArrayOutputStream aStream = new ByteArrayOutputStream (); 249 _theScribe.composeStartingElement("C", "foo", aList, true, aStream); 250 assertEquals("<C:foo bar=\"<"value" & 'value'>\" />", new String (aStream.toByteArray(), "UTF-8")); 251 } 252 } | Popular Tags |