View Javadoc

1   /*
2    * $Id: XmlUtilsTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.xml.util;
12  
13  import org.mule.tck.AbstractMuleTestCase;
14  import org.mule.util.IOUtils;
15  
16  import java.io.File;
17  import java.io.InputStream;
18  import java.net.URL;
19  
20  import javax.xml.stream.XMLStreamReader;
21  import javax.xml.transform.Source;
22  
23  import org.xml.sax.InputSource;
24  
25  public class XmlUtilsTestCase extends AbstractMuleTestCase
26  {
27  
28      private static final String SIMPLE_XML_RESOURCE = "simple.xml";
29      private static final String SIMPLE_XML_CONTENT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
30                                                       + "<just>testing</just>";
31  
32      public void testConvertsToW3cDocumentFromDom4jDocument() throws Exception
33      {
34          org.dom4j.Document document = XMLTestUtils.toDom4jDocument(SIMPLE_XML_RESOURCE);
35          assertToW3cDocumentSuccessfullyConvertsPayload(document);
36      }
37  
38      public void testConvertsToW3cDocumentFromW3cDocument() throws Exception
39      {
40          org.w3c.dom.Document document = XMLTestUtils.toW3cDocument(SIMPLE_XML_RESOURCE);
41          assertToW3cDocumentSuccessfullyConvertsPayload(document);
42      }
43  
44      public void testConvertsToW3cDocumentFromInputSource() throws Exception
45      {
46          InputSource payload = XMLTestUtils.toInputSource(SIMPLE_XML_RESOURCE);
47          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
48      }
49  
50      public void testConvertsToW3cDocumentFromSource() throws Exception
51      {
52          Source payload = XMLTestUtils.toSource(SIMPLE_XML_RESOURCE);
53          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
54      }
55  
56      public void testConvertsToW3cDocumentFromXmlStreamReader() throws Exception
57      {
58          XMLStreamReader payload = XMLTestUtils.toXmlStreamReader(SIMPLE_XML_RESOURCE);
59          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
60      }
61  
62      public void testConvertsToW3cDocumentFromInputStream() throws Exception
63      {
64          InputStream payload = XMLTestUtils.toInputStream(SIMPLE_XML_RESOURCE);
65          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
66      }
67  
68      public void testConvertsToW3cDocumentFromString() throws Exception
69      {
70          String payload = XMLTestUtils.toString(SIMPLE_XML_RESOURCE);
71          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
72      }
73  
74      public void testConvertsToW3cDocumentFromByteArray() throws Exception
75      {
76          byte[] payload = XMLTestUtils.toString(SIMPLE_XML_RESOURCE).getBytes();
77          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
78      }
79  
80      public void testConvertsToW3cDocumentFromFile() throws Exception
81      {
82          URL asUrl = IOUtils.getResourceAsUrl(SIMPLE_XML_RESOURCE, getClass());
83          File payload = new File(asUrl.getFile());
84          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
85      }
86  
87      private void assertToW3cDocumentSuccessfullyConvertsPayload(Object payload) throws Exception
88      {
89          org.w3c.dom.Document document = XMLUtils.toW3cDocument(payload);
90          String actualXml = XMLUtils.toXml(document);
91          assertEquals(SIMPLE_XML_CONTENT, actualXml);
92      }
93  }