1 19 20 package org.openide.xml; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.CharConversionException ; 25 import java.io.IOException ; 26 import java.io.StringReader ; 27 import java.net.URL ; 28 import java.net.URLClassLoader ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import junit.framework.Test; 31 import org.netbeans.junit.NbTestCase; 32 import org.netbeans.junit.NbTestSuite; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.DocumentType ; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.NodeList ; 37 import org.xml.sax.EntityResolver ; 38 import org.xml.sax.ErrorHandler ; 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.SAXException ; 41 import org.xml.sax.SAXParseException ; 42 import org.xml.sax.XMLReader ; 43 44 public class XMLUtilTest extends NbTestCase { 45 46 public XMLUtilTest(String testName) { 47 super(testName); 48 } 49 50 public void testCreateXMLReader() { 51 52 XMLReader parser = null; 53 54 try { 55 parser = XMLUtil.createXMLReader(); 56 } catch (Exception ex) { 57 ex.printStackTrace(); 58 } 59 60 if (parser == null) fail("Cannot create XML reader"); 62 } 63 64 public void testCreateDocument() { 65 66 Document doc = null; 67 try { 68 doc = XMLUtil.createDocument("root", null, null, null); 69 } catch (Exception ex) { 70 ex.printStackTrace(); 71 } 72 73 if (doc == null) fail("The test case is empty."); 75 } 76 77 public void testWrite() throws Exception { 78 String data = "<foo bar=\"val\"><baz/></foo>"; 79 Document doc = XMLUtil.parse(new InputSource (new StringReader (data)), false, true, null, null); 80 Element el = doc.getDocumentElement(); 82 assertEquals("foo", el.getNodeName()); 83 assertEquals("val", el.getAttribute("bar")); 84 NodeList l = el.getElementsByTagName("*"); 85 assertEquals(1, l.getLength()); 86 Element el2 = (Element )l.item(0); 87 assertEquals("baz", el2.getLocalName()); 88 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 89 XMLUtil.write(doc, baos, "UTF-8"); 90 String data2 = baos.toString("UTF-8"); 91 assertTrue(data2, data2.indexOf("foo") != -1); 93 assertTrue(data2, data2.indexOf("bar") != -1); 94 assertTrue(data2, data2.indexOf("baz") != -1); 95 assertTrue(data2, data2.indexOf("val") != -1); 96 } 97 98 99 public void testDocType() throws Exception { 100 String data = "<!DOCTYPE foo PUBLIC \"The foo DTD\" \"http://nowhere.net/foo.dtd\"><foo><x/><x/></foo>"; 101 Document doc = XMLUtil.parse(new InputSource (new StringReader (data)), true, true, new Handler (), new Resolver ()); 102 DocumentType t = doc.getDoctype(); 103 assertNotNull(t); 104 assertEquals("foo", t.getName()); 105 assertEquals("The foo DTD", t.getPublicId()); 106 assertEquals("http://nowhere.net/foo.dtd", t.getSystemId()); 107 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 108 XMLUtil.write(doc, baos, "UTF-8"); 109 String data2 = baos.toString("UTF-8"); 110 assertTrue(data2, data2.indexOf("foo") != -1); 112 assertTrue(data2, data2.indexOf("x") != -1); 113 assertTrue(data2, data2.indexOf("DOCTYPE") != -1); 114 assertTrue(data2, data2.indexOf("The foo DTD") != -1); 115 assertTrue(data2, data2.indexOf("http://nowhere.net/foo.dtd") != -1); 116 } 117 private static final class Handler implements ErrorHandler { 118 public void error(SAXParseException exception) throws SAXException { 119 throw exception; 120 } 121 public void fatalError(SAXParseException exception) throws SAXException { 122 throw exception; 123 } 124 public void warning(SAXParseException exception) throws SAXException { 125 throw exception; 126 } 127 } 128 private static final class Resolver implements EntityResolver { 129 public InputSource resolveEntity(String publicId, String systemId) throws SAXException , IOException { 130 assertEquals("The foo DTD", publicId); 131 assertEquals("http://nowhere.net/foo.dtd", systemId); 132 String data = "<!ELEMENT foo (x+)><!ELEMENT x EMPTY>"; 133 return new InputSource (new StringReader (data)); 134 } 135 } 136 137 public void testToAttributeValue() throws IOException { 138 String result = null; 139 try { 140 result = XMLUtil.toAttributeValue("\t\r\n &'<\""); 141 } catch (CharConversionException ex) { 142 } 143 144 assertEquals("Basic escape test failed", "\t\r\n &'<"", result); 145 146 try { 147 XMLUtil.toAttributeValue(new String (new byte[] { 0 })); 148 fail("Forbidden character accepted."); 149 } catch (CharConversionException ex) { 150 } 151 152 try { 153 XMLUtil.toAttributeValue(new String (new byte[] { 31 })); 154 fail("Forbidden character accepted."); 155 } catch (CharConversionException ex) { 156 } 157 } 158 159 public void testElementToContent() { 160 String result = null; 161 162 try { 163 result = XMLUtil.toElementContent("]]>\t\r\n &<>"); 164 } catch (CharConversionException ex) { 165 } 166 167 assertEquals("Basic escape test failed", "]]>\t\r\n &<>", result); 168 169 try { 170 XMLUtil.toElementContent(new String (new byte[] { 0 })); 171 fail("Forbidden character accepted."); 172 } catch (CharConversionException ex) { 173 } 174 175 try { 176 XMLUtil.toElementContent(new String (new byte[] { 31 })); 177 fail("Forbidden character accepted."); 178 } catch (CharConversionException ex) { 179 } 180 181 } 182 183 public void testToHex() { 184 185 byte[] data = new byte[] {0, 1, 15, 16, (byte)255}; 186 String s = XMLUtil.toHex(data, 0, data.length); 187 188 if (s.equalsIgnoreCase("00010f10ff") == false) { 190 fail("toHex() =" + s); 191 } 192 } 193 194 public void testFromHex() { 195 196 char[] hex = "00010f10ff".toCharArray(); 197 try { 198 byte[] ret = XMLUtil.fromHex(hex, 0, hex.length); 199 if (ret[0] != 0 || ret[1] != 1 || ret[2] != 15 || ret[3] != 16 || ret[4] != (byte)255) { 200 fail("fromHex()"); 201 } 202 } catch (IOException ex) { 203 fail(ex.getMessage()); 204 } 205 206 } 207 208 212 public void testNamespaces() throws Exception { 213 String data = "<foo xmlns='bar'><baz/></foo>"; 214 Document doc = XMLUtil.parse(new InputSource (new StringReader (data)), false, true, null, null); 215 Element el = doc.getDocumentElement(); 217 assertEquals("foo", el.getNodeName()); 218 assertEquals("foo", el.getTagName()); 219 assertEquals("foo", el.getLocalName()); 220 assertEquals("bar", el.getNamespaceURI()); 221 NodeList l = el.getElementsByTagName("*"); 222 assertEquals(1, l.getLength()); 223 Element el2 = (Element )l.item(0); 224 assertEquals("baz", el2.getLocalName()); 225 assertEquals("bar", el2.getNamespaceURI()); 226 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 227 XMLUtil.write(doc, baos, "UTF-8"); 228 String data2 = baos.toString("UTF-8"); 229 assertTrue(data2, data2.indexOf("foo") != -1); 231 assertTrue(data2, data2.indexOf("bar") != -1); 232 doc = XMLUtil.parse(new InputSource (new ByteArrayInputStream (baos.toByteArray())), false, true, null, null); 233 el = doc.getDocumentElement(); 234 assertEquals("foo", el.getLocalName()); 235 assertEquals("bar", el.getNamespaceURI()); 236 l = el.getElementsByTagName("*"); 237 assertEquals(1, l.getLength()); 238 el2 = (Element )l.item(0); 239 assertEquals("baz", el2.getLocalName()); 240 assertEquals("bar", el2.getNamespaceURI()); 241 doc = XMLUtil.createDocument("foo2", "bar2", null, null); 242 doc.getDocumentElement().appendChild(doc.createElementNS("bar2", "baz2")); 244 baos = new ByteArrayOutputStream (); 245 XMLUtil.write(doc, baos, "UTF-8"); 246 data2 = baos.toString("UTF-8"); 247 assertTrue(data2, data2.indexOf("foo2") != -1); 248 assertTrue("namespace 'bar2' of root element mentioned in output: " + data2, data2.indexOf("bar2") != -1); 249 doc = XMLUtil.parse(new InputSource (new ByteArrayInputStream (baos.toByteArray())), false, true, null, null); 250 el = doc.getDocumentElement(); 251 assertEquals("foo2", el.getLocalName()); 252 assertEquals("bar2", el.getNamespaceURI()); 253 l = el.getElementsByTagName("*"); 254 assertEquals(1, l.getLength()); 255 el2 = (Element )l.item(0); 256 assertEquals("baz2", el2.getLocalName()); 257 assertEquals("bar2", el2.getNamespaceURI()); 258 } 259 260 264 public void testNamespaces2() throws Exception { 265 String data = "<root xmlns='root'/>"; 266 Document doc = XMLUtil.parse(new InputSource (new StringReader (data)), false, true, null, null); 267 doc.getDocumentElement().appendChild(doc.createElementNS("child", "child")); 268 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 269 XMLUtil.write(doc, baos, "UTF-8"); 270 doc = XMLUtil.parse(new InputSource (new ByteArrayInputStream (baos.toByteArray())), false, true, null, null); 272 Element el = doc.getDocumentElement(); 273 assertEquals("root", el.getLocalName()); 274 assertEquals("root", el.getNamespaceURI()); 275 NodeList l = el.getElementsByTagName("*"); 276 assertEquals(1, l.getLength()); 277 el = (Element ) l.item(0); 278 assertEquals("child", el.getLocalName()); 279 assertEquals("Correct namespaces in " + baos.toString(), "child", el.getNamespaceURI()); 280 } 281 282 public void testIndentation() throws Exception { 283 Document doc = XMLUtil.createDocument("root", null, null, null); 284 doc.getDocumentElement().appendChild(doc.createElement("child")); 285 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 286 XMLUtil.write(doc, baos, "UTF-8"); 287 String data = baos.toString().replaceAll("\r\n", "\n"); 288 assertTrue("had reasonable indentation in\n" + data, data.indexOf("<root>\n <child/>\n</root>\n") != -1); 289 } 290 291 292 public void testIndentation2() throws Exception { 293 String doctype = "<!DOCTYPE p PUBLIC \"random DTD\" \"" + XMLUtilTest.class.getResource("random.dtd") + "\">\n"; 298 String data = 299 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 300 doctype + 301 "<!--\n" + 302 "Some license or whatever.\n" + 303 "-->\n" + 304 "<?stylesheet location=\"here\"?>\n" + 305 "<p>\n" + 306 " <t/>\n" + 307 " <c>\n" + 308 " <d>\n" + 309 " <s/>\n" + 310 " </d>\n" + 311 " </c>\n" + 312 "</p>\n"; 313 Document doc = XMLUtil.parse(new InputSource (new StringReader (data)), false, false, null, null); 314 Element d = (Element ) doc.getElementsByTagName("d").item(0); 315 Element c = (Element ) d.getParentNode(); 316 Element d2 = (Element ) DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument().importNode(d, true); 317 c.removeChild(d); 318 c.appendChild(doc.importNode(d2, true)); 319 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 320 XMLUtil.write(doc, baos, "UTF-8"); 321 String data2 = baos.toString().replaceAll("\r\n", "\n"); 322 assertEquals("identity replacement should not mess up indentation in \n" + data2, ignoreSpaceChanges(data, doctype), ignoreSpaceChanges(data2, doctype)); 324 } 325 private static String ignoreSpaceChanges(String text, String fuzzy) { 326 String regexp = "\\Q" + fuzzy.replaceAll("\\s+", "\\\\E\\\\s+\\\\Q") + "\\E"; 334 return text.replaceFirst(regexp, ""); 336 } 337 338 public void testSignificantWhitespace() throws Exception { 339 String data = 340 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 341 "<r>\n" + 342 " <p>This is <em>not</em> a test!</p>\n" + 343 "</r>\n"; 344 Document doc = XMLUtil.parse(new InputSource (new StringReader (data)), false, false, null, null); 345 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 346 XMLUtil.write(doc, baos, "UTF-8"); 347 String data2 = baos.toString().replaceAll("\r\n", "\n"); 348 assertEquals("identity replacement should not mess up significant whitespace", data, data2); 349 } 350 } 351 | Popular Tags |