1   /*
2    * Copyright ${YEAR} 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.soap.addressing.server;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import javax.xml.soap.MessageFactory;
22  import javax.xml.soap.MimeHeaders;
23  import javax.xml.soap.SOAPConstants;
24  import javax.xml.soap.SOAPException;
25  
26  import org.custommonkey.xmlunit.XMLTestCase;
27  import org.custommonkey.xmlunit.XMLUnit;
28  
29  import org.springframework.context.support.StaticApplicationContext;
30  import org.springframework.ws.context.DefaultMessageContext;
31  import org.springframework.ws.context.MessageContext;
32  import org.springframework.ws.server.EndpointInvocationChain;
33  import org.springframework.ws.server.endpoint.MethodEndpoint;
34  import org.springframework.ws.server.endpoint.annotation.Endpoint;
35  import org.springframework.ws.soap.addressing.server.annotation.Action;
36  import org.springframework.ws.soap.addressing.server.annotation.Address;
37  import org.springframework.ws.soap.saaj.SaajSoapMessage;
38  import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
39  
40  public class AnnotationActionMethodEndpointMappingTest extends XMLTestCase {
41  
42      private StaticApplicationContext applicationContext;
43  
44      private AnnotationActionEndpointMapping mapping;
45  
46      private MessageFactory messageFactory;
47  
48      protected void setUp() throws Exception {
49          messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
50          XMLUnit.setIgnoreWhitespace(true);
51          applicationContext = new StaticApplicationContext();
52          applicationContext.registerSingleton("mapping", AnnotationActionEndpointMapping.class);
53          mapping = (AnnotationActionEndpointMapping) applicationContext.getBean("mapping");
54      }
55  
56      public void testNoAddress() throws Exception {
57          applicationContext.registerSingleton("endpoint", Endpoint1.class);
58          applicationContext.refresh();
59          MessageContext messageContext = createMessageContext();
60  
61          EndpointInvocationChain chain = mapping.getEndpoint(messageContext);
62          assertNotNull("MethodEndpoint not registered", chain);
63          MethodEndpoint expected = new MethodEndpoint(applicationContext.getBean("endpoint"), "doIt", new Class[0]);
64          assertEquals("Invalid endpoint registered", expected, chain.getEndpoint());
65      }
66  
67      public void testAddress() throws Exception {
68          applicationContext.registerSingleton("endpoint", Endpoint2.class);
69          applicationContext.refresh();
70          MessageContext messageContext = createMessageContext();
71  
72          EndpointInvocationChain chain = mapping.getEndpoint(messageContext);
73          assertNotNull("MethodEndpoint not registered", chain);
74          MethodEndpoint expected = new MethodEndpoint(applicationContext.getBean("endpoint"), "doIt", new Class[0]);
75          assertEquals("Invalid endpoint registered", expected, chain.getEndpoint());
76      }
77  
78      private MessageContext createMessageContext() throws SOAPException, IOException {
79          MimeHeaders mimeHeaders = new MimeHeaders();
80          mimeHeaders.addHeader("Content-Type", " application/soap+xml");
81          InputStream is = getClass().getResourceAsStream("valid.xml");
82          assertNotNull("Could not load valid.xml", is);
83          try {
84              SaajSoapMessage message = new SaajSoapMessage(messageFactory.createMessage(mimeHeaders, is));
85              return new DefaultMessageContext(message, new SaajSoapMessageFactory(messageFactory));
86          }
87          finally {
88              is.close();
89          }
90      }
91  
92      @Endpoint
93      private static class Endpoint1 {
94  
95          @Action("http://fabrikam123.example/mail/Delete")
96          public void doIt() {
97  
98          }
99      }
100 
101     @Endpoint
102     @Address("mailto:[email protected]")
103     private static class Endpoint2 {
104 
105         @Action("http://fabrikam123.example/mail/Delete")
106         public void doIt() {
107 
108         }
109     }
110 }