View Javadoc

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