View Javadoc

1   /*
2    * $Id: XmlTransformerFunctionalTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
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.functional;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  
17  import org.custommonkey.xmlunit.XMLAssert;
18  import org.w3c.dom.Document;
19  
20  public class XmlTransformerFunctionalTestCase extends AbstractXmlFunctionalTestCase
21  {
22  
23      public static final String SIMPLE_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<parent><child name=\"poot\"/></parent>";
24      public static final String CHILDLESS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<parent/>";
25      public static final String SERIALIZED = "<org.mule.module.xml.functional.XmlTransformerFunctionalTestCase_-Parent>\n" +
26              "  <child/>\n" +
27              "</org.mule.module.xml.functional.XmlTransformerFunctionalTestCase_-Parent>";
28  
29      protected String getConfigResources()
30      {
31          return "org/mule/module/xml/xml-transformer-functional-test.xml";
32      }
33  
34      protected MuleClient sendXml() throws MuleException
35      {
36          MuleClient client = new MuleClient(muleContext);
37          client.dispatch("xml-in", SIMPLE_XML, null);
38          return client;
39      }
40  
41      protected MuleClient sendObject() throws MuleException
42      {
43          return sendObject("object-in");
44      }
45  
46      protected MuleClient sendObject(String endpoint) throws MuleException
47      {
48          MuleClient client = new MuleClient(muleContext);
49          client.dispatch(endpoint, new Parent(new Child()), null);
50          return client;
51      }
52  
53      public void testXmlOut() throws Exception
54      {
55          String xml = (String) request(sendXml(), "xml-out", String.class);
56          XMLAssert.assertXMLEqual(SIMPLE_XML, xml);
57      }
58  
59      public void testXmlDomOut() throws MuleException
60      {
61          Document dom = (Document) request(sendXml(), "xml-dom-out", Document.class);
62          assertEquals("parent", dom.getDocumentElement().getLocalName());
63      }
64  
65      public void testXmlXsltOut() throws Exception
66      {
67          String xml = (String) request(sendXml(), "xml-xslt-out-string", String.class);
68          XMLAssert.assertXMLEqual(CHILDLESS_XML, xml);
69      }
70  
71      public void testDomXmlOut() throws Exception
72      {
73          String xml = (String) request(sendXml(), "dom-xml-out", String.class);
74          XMLAssert.assertXMLEqual(SIMPLE_XML, xml);
75      }
76  
77      public void testObjectOut() throws Exception
78      {
79          request(sendObject(), "object-out", Parent.class);
80      }
81  
82      public void testObjectXmlOut() throws Exception
83      {
84          String xml = (String) request(sendObject(), "object-xml-out", String.class);
85          System.out.println(xml);
86          XMLAssert.assertXMLEqual(SERIALIZED, xml);
87      }
88  
89      // MULE-5038
90      //public void testXmlObjectOut() throws MuleException
91      //{
92      //    request(sendObject(), "xml-object-out", Parent.class);
93      //}
94  
95      public void testXmlJxpathOut() throws Exception
96      {
97          String xml = (String) request(sendXml(), "xml-jxpath-out", String.class);
98          assertEquals("1", xml);
99      }
100 
101 
102     protected Object request(MuleClient client, String endpoint, Class clazz) throws MuleException
103     {
104         MuleMessage message = client.request(endpoint, TIMEOUT);
105         assertNotNull(message);
106         assertNotNull(message.getPayload());
107         assertTrue(message.getPayload().getClass().getName(), clazz.isAssignableFrom(message.getPayload().getClass()));
108         return message.getPayload();
109     }
110 
111 
112     public static class Parent
113     {
114         private Child child;
115 
116         public Parent()
117         {
118             this(null);
119         }
120 
121         public Parent(Child child)
122         {
123             setChild(child);
124         }
125 
126         public Child getChild()
127         {
128             return child;
129         }
130 
131         public void setChild(Child child)
132         {
133             this.child = child;
134         }
135     }
136 
137     public static class Child
138     {
139         // nothing here
140     }
141 
142 }