1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.wsdl.wsdl11.provider;
18  
19  import javax.wsdl.Definition;
20  import javax.wsdl.Message;
21  import javax.wsdl.Part;
22  import javax.wsdl.Types;
23  import javax.wsdl.extensions.schema.Schema;
24  import javax.wsdl.factory.WSDLFactory;
25  import javax.xml.namespace.QName;
26  import javax.xml.parsers.DocumentBuilder;
27  import javax.xml.parsers.DocumentBuilderFactory;
28  
29  import org.springframework.core.io.ClassPathResource;
30  import org.springframework.core.io.Resource;
31  import org.springframework.xml.sax.SaxUtils;
32  
33  import org.junit.Assert;
34  import org.junit.Before;
35  import org.junit.Test;
36  import org.w3c.dom.Document;
37  
38  public class DefaultMessagesProviderTest {
39  
40      private DefaultMessagesProvider provider;
41  
42      private Definition definition;
43  
44      private DocumentBuilder documentBuilder;
45  
46      @Before
47      public void setUp() throws Exception {
48          provider = new DefaultMessagesProvider();
49          WSDLFactory factory = WSDLFactory.newInstance();
50          definition = factory.newDefinition();
51          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
52          documentBuilderFactory.setNamespaceAware(true);
53          documentBuilder = documentBuilderFactory.newDocumentBuilder();
54      }
55  
56      @Test
57      public void testAddMessages() throws Exception {
58          String definitionNamespace = "http://springframework.org/spring-ws";
59          definition.addNamespace("tns", definitionNamespace);
60          definition.setTargetNamespace(definitionNamespace);
61          String schemaNamespace = "http://www.springframework.org/spring-ws/schema";
62          definition.addNamespace("schema", schemaNamespace);
63  
64          Resource resource = new ClassPathResource("schema.xsd", getClass());
65          Document schemaDocument = documentBuilder.parse(SaxUtils.createInputSource(resource));
66          Types types = definition.createTypes();
67          definition.setTypes(types);
68          Schema schema = (Schema) definition.getExtensionRegistry()
69                  .createExtension(Types.class, new QName("http://www.w3.org/2001/XMLSchema", "schema"));
70          types.addExtensibilityElement(schema);
71          schema.setElement(schemaDocument.getDocumentElement());
72  
73          provider.addMessages(definition);
74  
75          Assert.assertEquals("Invalid amount of messages created", 3, definition.getMessages().size());
76  
77          Message message = definition.getMessage(new QName(definitionNamespace, "GetOrderRequest"));
78          Assert.assertNotNull("Message not created", message);
79          Part part = message.getPart("GetOrderRequest");
80          Assert.assertNotNull("Part not created", part);
81          Assert.assertEquals("Invalid element on part", new QName(schemaNamespace, "GetOrderRequest"),
82                  part.getElementName());
83  
84          message = definition.getMessage(new QName(definitionNamespace, "GetOrderResponse"));
85          Assert.assertNotNull("Message not created", message);
86          part = message.getPart("GetOrderResponse");
87          Assert.assertNotNull("Part not created", part);
88          Assert.assertEquals("Invalid element on part", new QName(schemaNamespace, "GetOrderResponse"),
89                  part.getElementName());
90  
91          message = definition.getMessage(new QName(definitionNamespace, "GetOrderFault"));
92          Assert.assertNotNull("Message not created", message);
93          part = message.getPart("GetOrderFault");
94          Assert.assertNotNull("Part not created", part);
95          Assert.assertEquals("Invalid element on part", new QName(schemaNamespace, "GetOrderFault"),
96                  part.getElementName());
97      }
98  }