View Javadoc

1   /*
2    * $Id: XmlUtilsTestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
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.junit4.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.junit.Test;
24  import org.xml.sax.InputSource;
25  
26  import static org.junit.Assert.assertEquals;
27  
28  public class XmlUtilsTestCase extends AbstractMuleTestCase
29  {
30  
31      private static final String SIMPLE_XML_RESOURCE = "simple.xml";
32      private static final String SIMPLE_XML_CONTENT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
33                                                       + "<just>testing</just>";
34  
35      @Test
36      public void testConvertsToW3cDocumentFromDom4jDocument() throws Exception
37      {
38          org.dom4j.Document document = XMLTestUtils.toDom4jDocument(SIMPLE_XML_RESOURCE);
39          assertToW3cDocumentSuccessfullyConvertsPayload(document);
40      }
41  
42      @Test
43      public void testConvertsToW3cDocumentFromW3cDocument() throws Exception
44      {
45          org.w3c.dom.Document document = XMLTestUtils.toW3cDocument(SIMPLE_XML_RESOURCE);
46          assertToW3cDocumentSuccessfullyConvertsPayload(document);
47      }
48  
49      @Test
50      public void testConvertsToW3cDocumentFromInputSource() throws Exception
51      {
52          InputSource payload = XMLTestUtils.toInputSource(SIMPLE_XML_RESOURCE);
53          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
54      }
55  
56      @Test
57      public void testConvertsToW3cDocumentFromSource() throws Exception
58      {
59          Source payload = XMLTestUtils.toSource(SIMPLE_XML_RESOURCE);
60          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
61      }
62  
63      @Test
64      public void testConvertsToW3cDocumentFromXmlStreamReader() throws Exception
65      {
66          XMLStreamReader payload = XMLTestUtils.toXmlStreamReader(SIMPLE_XML_RESOURCE);
67          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
68      }
69  
70      @Test
71      public void testConvertsToW3cDocumentFromInputStream() throws Exception
72      {
73          InputStream payload = XMLTestUtils.toInputStream(SIMPLE_XML_RESOURCE);
74          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
75      }
76  
77      @Test
78      public void testConvertsToW3cDocumentFromString() throws Exception
79      {
80          String payload = XMLTestUtils.toString(SIMPLE_XML_RESOURCE);
81          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
82      }
83  
84      @Test
85      public void testConvertsToW3cDocumentFromByteArray() throws Exception
86      {
87          byte[] payload = XMLTestUtils.toString(SIMPLE_XML_RESOURCE).getBytes();
88          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
89      }
90  
91      @Test
92      public void testConvertsToW3cDocumentFromFile() throws Exception
93      {
94          URL asUrl = IOUtils.getResourceAsUrl(SIMPLE_XML_RESOURCE, getClass());
95          File payload = new File(asUrl.getFile());
96          assertToW3cDocumentSuccessfullyConvertsPayload(payload);
97      }
98  
99      private void assertToW3cDocumentSuccessfullyConvertsPayload(Object payload) throws Exception
100     {
101         org.w3c.dom.Document document = XMLUtils.toW3cDocument(payload);
102         String actualXml = XMLUtils.toXml(document);
103         assertEquals(SIMPLE_XML_CONTENT, actualXml);
104     }
105 }