1   /*
2    * $Id: XmlTransformerFunctionalTestCase.java 10957 2008-02-22 18:45:32Z holger $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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();
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();
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      public void testXmlObjectOut() throws MuleException
90      {
91          request(sendObject(), "xml-object-out", Parent.class);
92      }
93  
94      public void testXmlJxpathOut() throws Exception
95      {
96          String xml = (String) request(sendXml(), "xml-jxpath-out", String.class);
97          assertEquals("1", xml);
98      }
99  
100 
101     protected Object request(MuleClient client, String endpoint, Class clazz) throws MuleException
102     {
103         MuleMessage message = client.request(endpoint, TIMEOUT);
104         assertNotNull(message);
105         assertNotNull(message.getPayload());
106         assertTrue(message.getPayload().getClass().getName(), clazz.isAssignableFrom(message.getPayload().getClass()));
107         return message.getPayload();
108     }
109 
110 
111     public static class Parent
112     {
113         private Child child;
114 
115         public Parent()
116         {
117             this(null);
118         }
119 
120         public Parent(Child child)
121         {
122             setChild(child);
123         }
124 
125         public Child getChild()
126         {
127             return child;
128         }
129 
130         public void setChild(Child child)
131         {
132             this.child = child;
133         }
134     }
135 
136     public static class Child
137     {
138         // nothing here
139     }
140 
141 }