KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > xml > XmlIOTest


1 /*
2  * Created on 2003-nov-20
3  */

4 package org.columba.core.xml;
5
6 import java.io.ByteArrayInputStream JavaDoc;
7 import java.io.ByteArrayOutputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9
10 import junit.framework.TestCase;
11
12
13 /**
14  * @author Erik Mattsson
15  */

16 public class XmlIOTest extends TestCase {
17     /**
18  * Test for writing a Xml Element that has been passed in the constructor XmlIO(XmlElement).
19  * @throws IOException thrown if the test fails.
20  */

21     public void testXmlElement() throws IOException JavaDoc {
22         // Setup the XML that is to be written
23
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 JavaDoc baos = new ByteArrayOutputStream JavaDoc();
35         xmlIO.write(baos);
36
37         xmlIO = new XmlIO();
38         assertTrue("Could not parse the written XML",
39             xmlIO.load(new ByteArrayInputStream JavaDoc(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     /**
47  * Test the load(InputStream) method.
48  */

49     public void testReadInputStream() {
50         String JavaDoc 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 JavaDoc(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