1 4 package org.columba.core.xml; 5 6 import java.io.ByteArrayInputStream ; 7 import java.io.ByteArrayOutputStream ; 8 import java.io.IOException ; 9 10 import junit.framework.TestCase; 11 12 13 16 public class XmlIOTest extends TestCase { 17 21 public void testXmlElement() throws IOException { 22 XmlElement expected = new XmlElement("big_name"); 24 expected.addAttribute("anattr", "avalue"); 25 expected.addAttribute("other", "value"); 26 27 XmlElement child1 = new XmlElement("child1"); 28 child1.addAttribute("othername", "nooname"); 29 child1.addAttribute("onemore", "ok"); 30 expected.addElement(child1); 31 expected.addElement(new XmlElement("child2")); 32 33 XmlIO xmlIO = new XmlIO(expected); 34 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 35 xmlIO.write(baos); 36 37 xmlIO = new XmlIO(); 38 assertTrue("Could not parse the written XML", 39 xmlIO.load(new ByteArrayInputStream (baos.toByteArray()))); 40 41 XmlElement actual = xmlIO.getRoot().getElement(0); 42 assertEquals("The original and the written XML element are not equal", 43 expected, actual); 44 } 45 46 49 public void testReadInputStream() { 50 String expected = "<xml attr=\"one\" secAttr=\"two\"><child name=\"other\"/></xml>"; 51 XmlIO xmlIO = new XmlIO(); 52 assertTrue("The XML could not be loaded", 53 xmlIO.load(new ByteArrayInputStream (expected.getBytes()))); 54 55 XmlElement actualXml = xmlIO.getRoot().getElement("xml"); 56 assertEquals("Name isnt correct", "xml", actualXml.getName()); 57 assertEquals("The first attribute isnt correct", "one", 58 actualXml.getAttribute("attr")); 59 assertEquals("The second attribute isnt correct", "two", 60 actualXml.getAttribute("secAttr")); 61 62 XmlElement child = actualXml.getElement(0); 63 assertEquals("The child name isnt correct", "child", child.getName()); 64 assertEquals("The childs first attribute isnt correct", "other", 65 child.getAttribute("name")); 66 } 67 } 68 | Popular Tags |