View Javadoc

1   /*
2    * $Id: ProxyServiceServingWsdlMule4092TestCase.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.cxf.issues;
12  
13  import org.mule.tck.FunctionalTestCase;
14  import org.mule.util.IOUtils;
15  import org.mule.util.SystemUtils;
16  
17  import java.io.IOException;
18  import java.io.StringReader;
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  
22  import org.custommonkey.xmlunit.XMLUnit;
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Element;
25  import org.xml.sax.InputSource;
26  import org.xml.sax.SAXException;
27  
28  import javax.xml.parsers.DocumentBuilder;
29  import javax.xml.parsers.DocumentBuilderFactory;
30  import javax.xml.parsers.ParserConfigurationException;
31  
32  public class ProxyServiceServingWsdlMule4092TestCase extends FunctionalTestCase
33  {
34      private String expectedWsdlFileName;
35  
36      @Override
37      protected void doSetUp() throws Exception
38      {
39          super.doSetUp();
40          XMLUnit.setIgnoreWhitespace(true);
41          setupExpectedWsdlFileName();
42      }
43  
44      /**
45       * The WSDL generated by CXF is basically the same but slightly differs in whitespace and
46       * element ordering (which does not matter). XMLUnit's javadoc says it can ignore element
47       * ordering but obviously that does not work, hence this hack.
48       */
49      private void setupExpectedWsdlFileName()
50      {
51          if (SystemUtils.isSunJDK() || SystemUtils.isAppleJDK())
52          {
53              expectedWsdlFileName = "test.wsdl";
54          }
55          else if (SystemUtils.isIbmJDK())
56          {
57              if (SystemUtils.isJavaVersionAtLeast(160))
58              {
59                  expectedWsdlFileName = "test.wsdl.ibmjdk-6";
60              }
61              else
62              {
63                  expectedWsdlFileName = "test.wsdl.ibmjdk-5";
64              }
65          }
66          else
67          {
68              fail("Unknown JDK");
69          }
70      }
71  
72      @Override
73      protected String getConfigResources()
74      {
75          return "issues/proxy-service-serving-wsdl-mule4092.xml";
76      }
77  
78      public void testProxyServiceWSDL() throws MalformedURLException, IOException, Exception
79      {
80          String expected = getXML("issues/" + expectedWsdlFileName);
81  
82          URL url = new URL("http://localhost:8777/services/onlinestore?wsdl");
83          String wsdlFromService = IOUtils.toString(url.openStream());
84  
85          // The exact string representation may differ, so we'll spot check the WSDL contents
86          //assertTrue(compareResults(expected, wsdlFromService));
87          Document expectedDom = buildDOM(expected);
88          Document actualDom = buildDOM(wsdlFromService);
89  
90          // Check that it's WSDL
91          Element topElement = expectedDom.getDocumentElement();
92          String wsdlNamespace = topElement.getNamespaceURI();
93          assertEquals(wsdlNamespace, actualDom.getDocumentElement().getNamespaceURI());
94          assertEquals(topElement.getLocalName(), actualDom.getDocumentElement().getLocalName());
95  
96          Element expectedService = (Element) expectedDom.getElementsByTagNameNS(wsdlNamespace, "service").item(0);
97          Element actualService = (Element) actualDom.getElementsByTagNameNS(wsdlNamespace, "service").item(0);
98          assertNotNull(actualService);
99          assertEquals(expectedService.getAttribute("name"), actualService.getAttribute("name"));
100 
101         Element expectedPort = (Element) expectedDom.getElementsByTagNameNS(wsdlNamespace, "port").item(0);
102         Element actualPort = (Element) actualDom.getElementsByTagNameNS(wsdlNamespace, "port").item(0);
103         assertNotNull(actualPort);
104         assertEquals(expectedPort.getAttribute("name"), actualPort.getAttribute("name"));
105 
106         int expectedNumberOfMessages =  expectedDom.getElementsByTagNameNS(wsdlNamespace, "message").getLength();
107         int actualNumberOfmMessages =  actualDom.getElementsByTagNameNS(wsdlNamespace, "message").getLength();
108         assertEquals(expectedNumberOfMessages, actualNumberOfmMessages);
109     }
110 
111     protected String getXML(String requestFile) throws Exception
112     {
113         String xml = IOUtils.toString(IOUtils.getResourceAsStream(requestFile, this.getClass()), "UTF-8");
114         if (xml != null)
115         {
116             return xml;
117         }
118         else
119         {
120             fail("Unable to load test request file");
121             return null;
122         }
123     }
124 
125     protected boolean compareResults(String expected, String result)
126     {
127         try
128         {
129             String expectedString = this.normalizeString(expected);
130             String resultString = this.normalizeString(result);
131             return XMLUnit.compareXML(expectedString, resultString).similar();
132         }
133         catch (Exception ex)
134         {
135             return false;
136         }
137     }
138 
139     protected String normalizeString(String rawString)
140     {
141         rawString = rawString.replaceAll("\r", "");
142         rawString = rawString.replaceAll("\n", "");
143         return rawString.replaceAll("\t", "");
144     }
145 
146     private Document buildDOM(String xmlString) throws ParserConfigurationException, IOException, SAXException
147     {
148         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
149         factory.setNamespaceAware(true);
150         DocumentBuilder builder = factory.newDocumentBuilder();
151         InputSource source = new InputSource(new StringReader(xmlString));
152         return builder.parse(source);
153     }
154 }