1   /*
2    * Copyright 2007 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.server.endpoint.mapping;
18  
19  import java.lang.reflect.Method;
20  import java.util.Collections;
21  import javax.xml.namespace.QName;
22  import javax.xml.soap.MessageFactory;
23  import javax.xml.soap.SOAPMessage;
24  import javax.xml.transform.Source;
25  
26  import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
27  import org.springframework.ws.context.DefaultMessageContext;
28  import org.springframework.ws.context.MessageContext;
29  import org.springframework.ws.server.EndpointAdapter;
30  import org.springframework.ws.server.MessageDispatcher;
31  import org.springframework.ws.server.endpoint.MethodEndpoint;
32  import org.springframework.ws.server.endpoint.adapter.PayloadMethodEndpointAdapter;
33  import org.springframework.ws.soap.saaj.SaajSoapMessage;
34  import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
35  import org.springframework.ws.soap.server.SoapMessageDispatcher;
36  
37  public class PayloadRootAnnotationMethodEndpointMappingTest extends AbstractDependencyInjectionSpringContextTests {
38  
39      private PayloadRootAnnotationMethodEndpointMapping mapping;
40  
41      protected String getConfigPath() {
42          return "applicationContext.xml";
43      }
44  
45      public void setMapping(PayloadRootAnnotationMethodEndpointMapping mapping) {
46          this.mapping = mapping;
47      }
48  
49      public void testRegistration() throws NoSuchMethodException {
50          MethodEndpoint endpoint = mapping.lookupEndpoint("{http://springframework.org/spring-ws}Request");
51          assertNotNull("MethodEndpoint not registered", endpoint);
52          Method doIt = PayloadRootEndpoint.class.getMethod("doIt", Source.class);
53          MethodEndpoint expected = new MethodEndpoint("endpoint", applicationContext, doIt);
54          assertEquals("Invalid endpoint registered", expected, endpoint);
55  
56          assertNull("Invalid endpoint registered",
57                  mapping.lookupEndpoint("{http://springframework.org/spring-ws}Request2"));
58      }
59  
60      public void testInvoke() throws Exception {
61  
62          MessageFactory messageFactory = MessageFactory.newInstance();
63          SOAPMessage request = messageFactory.createMessage();
64          request.getSOAPBody().addBodyElement(QName.valueOf("{http://springframework.org/spring-ws}Request"));
65          MessageContext messageContext =
66                  new DefaultMessageContext(new SaajSoapMessage(request), new SaajSoapMessageFactory(messageFactory));
67          EndpointAdapter adapter = new PayloadMethodEndpointAdapter();
68  
69          MessageDispatcher messageDispatcher = new SoapMessageDispatcher();
70          messageDispatcher.setApplicationContext(applicationContext);
71          messageDispatcher.setEndpointMappings(Collections.singletonList(mapping));
72          messageDispatcher.setEndpointAdapters(Collections.singletonList(adapter));
73  
74          messageDispatcher.receive(messageContext);
75  
76          PayloadRootEndpoint endpoint = (PayloadRootEndpoint) applicationContext.getBean("endpoint");
77          assertTrue("doIt() not invoked on endpoint", endpoint.isDoItInvoked());
78  
79          LogAspect aspect = (LogAspect) applicationContext.getBean("logAspect");
80          assertTrue("log() not invoked on aspect", aspect.isLogInvoked());
81      }
82  
83  }