1
2
3
4
5
6
7 package org.mule.module.xml.util;
8
9 import org.mule.tck.junit4.AbstractMuleTestCase;
10 import org.mule.util.IOUtils;
11
12 import java.io.File;
13 import java.io.InputStream;
14 import java.net.URL;
15
16 import javax.xml.stream.XMLStreamReader;
17 import javax.xml.transform.Source;
18
19 import org.junit.Test;
20 import org.xml.sax.InputSource;
21
22 import static org.junit.Assert.assertEquals;
23
24 public class XmlUtilsTestCase extends AbstractMuleTestCase
25 {
26
27 private static final String SIMPLE_XML_RESOURCE = "simple.xml";
28 private static final String SIMPLE_XML_CONTENT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
29 + "<just>testing</just>";
30
31 @Test
32 public void testConvertsToW3cDocumentFromDom4jDocument() throws Exception
33 {
34 org.dom4j.Document document = XMLTestUtils.toDom4jDocument(SIMPLE_XML_RESOURCE);
35 assertToW3cDocumentSuccessfullyConvertsPayload(document);
36 }
37
38 @Test
39 public void testConvertsToW3cDocumentFromW3cDocument() throws Exception
40 {
41 org.w3c.dom.Document document = XMLTestUtils.toW3cDocument(SIMPLE_XML_RESOURCE);
42 assertToW3cDocumentSuccessfullyConvertsPayload(document);
43 }
44
45 @Test
46 public void testConvertsToW3cDocumentFromInputSource() throws Exception
47 {
48 InputSource payload = XMLTestUtils.toInputSource(SIMPLE_XML_RESOURCE);
49 assertToW3cDocumentSuccessfullyConvertsPayload(payload);
50 }
51
52 @Test
53 public void testConvertsToW3cDocumentFromSource() throws Exception
54 {
55 Source payload = XMLTestUtils.toSource(SIMPLE_XML_RESOURCE);
56 assertToW3cDocumentSuccessfullyConvertsPayload(payload);
57 }
58
59 @Test
60 public void testConvertsToW3cDocumentFromXmlStreamReader() throws Exception
61 {
62 XMLStreamReader payload = XMLTestUtils.toXmlStreamReader(SIMPLE_XML_RESOURCE);
63 assertToW3cDocumentSuccessfullyConvertsPayload(payload);
64 }
65
66 @Test
67 public void testConvertsToW3cDocumentFromInputStream() throws Exception
68 {
69 InputStream payload = XMLTestUtils.toInputStream(SIMPLE_XML_RESOURCE);
70 assertToW3cDocumentSuccessfullyConvertsPayload(payload);
71 }
72
73 @Test
74 public void testConvertsToW3cDocumentFromString() throws Exception
75 {
76 String payload = XMLTestUtils.toString(SIMPLE_XML_RESOURCE);
77 assertToW3cDocumentSuccessfullyConvertsPayload(payload);
78 }
79
80 @Test
81 public void testConvertsToW3cDocumentFromByteArray() throws Exception
82 {
83 byte[] payload = XMLTestUtils.toString(SIMPLE_XML_RESOURCE).getBytes();
84 assertToW3cDocumentSuccessfullyConvertsPayload(payload);
85 }
86
87 @Test
88 public void testConvertsToW3cDocumentFromFile() throws Exception
89 {
90 URL asUrl = IOUtils.getResourceAsUrl(SIMPLE_XML_RESOURCE, getClass());
91 File payload = new File(asUrl.getFile());
92 assertToW3cDocumentSuccessfullyConvertsPayload(payload);
93 }
94
95 private void assertToW3cDocumentSuccessfullyConvertsPayload(Object payload) throws Exception
96 {
97 org.w3c.dom.Document document = XMLUtils.toW3cDocument(payload);
98 String actualXml = XMLUtils.toXml(document);
99 assertEquals(SIMPLE_XML_CONTENT, actualXml);
100 }
101 }