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 SuffixBasedMessagesProviderTest {
39  
40      private SuffixBasedMessagesProvider provider;
41  
42      private Definition definition;
43  
44      private DocumentBuilder documentBuilder;
45  
46      @Before
47      public void setUp() throws Exception {
48          provider = new SuffixBasedMessagesProvider();
49          provider.setFaultSuffix("Foo");
50          WSDLFactory factory = WSDLFactory.newInstance();
51          definition = factory.newDefinition();
52          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
53          documentBuilderFactory.setNamespaceAware(true);
54          documentBuilder = documentBuilderFactory.newDocumentBuilder();
55      }
56  
57      @Test
58      public void testAddMessages() throws Exception {
59          String definitionNamespace = "http://springframework.org/spring-ws";
60          definition.addNamespace("tns", definitionNamespace);
61          definition.setTargetNamespace(definitionNamespace);
62          String schemaNamespace = "http://www.springframework.org/spring-ws/schema";
63          definition.addNamespace("schema", schemaNamespace);
64  
65          Resource resource = new ClassPathResource("schema.xsd", getClass());
66          Document schemaDocument = documentBuilder.parse(SaxUtils.createInputSource(resource));
67          Types types = definition.createTypes();
68          definition.setTypes(types);
69          Schema schema = (Schema) definition.getExtensionRegistry()
70                  .createExtension(Types.class, new QName("http://www.w3.org/2001/XMLSchema", "schema"));
71          types.addExtensibilityElement(schema);
72          schema.setElement(schemaDocument.getDocumentElement());
73  
74          provider.addMessages(definition);
75  
76          Assert.assertEquals("Invalid amount of messages created", 2, definition.getMessages().size());
77  
78          Message message = definition.getMessage(new QName(definitionNamespace, "GetOrderRequest"));
79          Assert.assertNotNull("Message not created", message);
80          Part part = message.getPart("GetOrderRequest");
81          Assert.assertNotNull("Part not created", part);
82          Assert.assertEquals("Invalid element on part", new QName(schemaNamespace, "GetOrderRequest"),
83                  part.getElementName());
84  
85          message = definition.getMessage(new QName(definitionNamespace, "GetOrderResponse"));
86          Assert.assertNotNull("Message not created", message);
87          part = message.getPart("GetOrderResponse");
88          Assert.assertNotNull("Part not created", part);
89          Assert.assertEquals("Invalid element on part", new QName(schemaNamespace, "GetOrderResponse"),
90                  part.getElementName());
91      }
92  }