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