01: /*
02: * Created on 2003-nov-20
03: */
04: package org.columba.core.xml;
05:
06: import java.io.ByteArrayInputStream;
07: import java.io.ByteArrayOutputStream;
08: import java.io.IOException;
09:
10: import junit.framework.TestCase;
11:
12: /**
13: * @author Erik Mattsson
14: */
15: public class XmlIOTest extends TestCase {
16: /**
17: * Test for writing a Xml Element that has been passed in the constructor XmlIO(XmlElement).
18: * @throws IOException thrown if the test fails.
19: */
20: public void testXmlElement() throws IOException {
21: // Setup the XML that is to be written
22: XmlElement expected = new XmlElement("big_name");
23: expected.addAttribute("anattr", "avalue");
24: expected.addAttribute("other", "value");
25:
26: XmlElement child1 = new XmlElement("child1");
27: child1.addAttribute("othername", "nooname");
28: child1.addAttribute("onemore", "ok");
29: expected.addElement(child1);
30: expected.addElement(new XmlElement("child2"));
31:
32: XmlIO xmlIO = new XmlIO(expected);
33: ByteArrayOutputStream baos = new ByteArrayOutputStream();
34: xmlIO.write(baos);
35:
36: xmlIO = new XmlIO();
37: assertTrue("Could not parse the written XML", xmlIO
38: .load(new ByteArrayInputStream(baos.toByteArray())));
39:
40: XmlElement actual = xmlIO.getRoot().getElement(0);
41: assertEquals(
42: "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 expected = "<xml attr=\"one\" secAttr=\"two\"><child name=\"other\"/></xml>";
51: XmlIO xmlIO = new XmlIO();
52: assertTrue("The XML could not be loaded", xmlIO
53: .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
64: .getName());
65: assertEquals("The childs first attribute isnt correct",
66: "other", child.getAttribute("name"));
67: }
68: }
|