1 /*
2  * Copyright 2002-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.remoting.jaxws;
18 
19 import java.net.MalformedURLException;
20 import java.net.URL;
21 import javax.xml.namespace.QName;
22 import javax.xml.ws.BindingProvider;
23 import javax.xml.ws.Service;
24 import javax.xml.ws.WebServiceClient;
25 import javax.xml.ws.WebServiceRef;
26 import javax.xml.ws.soap.AddressingFeature;
27 
28 import static org.junit.Assert.*;
29 import org.junit.Test;
30 
31 import org.springframework.beans.factory.BeanCreationException;
32 import org.springframework.beans.factory.support.GenericBeanDefinition;
33 import org.springframework.beans.factory.support.RootBeanDefinition;
34 import org.springframework.context.annotation.AnnotationConfigUtils;
35 import org.springframework.context.support.GenericApplicationContext;
36 
37 /**
38  * @author Juergen Hoeller
39  * @since 2.5
40  */
41 public class JaxWsSupportTests {
42 
43 	@Test
testJaxWsPortAccess()44 	public void testJaxWsPortAccess() throws Exception {
45 		doTestJaxWsPortAccess((Object[]) null);
46 	}
47 
48 	@Test
testJaxWsPortAccessWithFeatureObject()49 	public void testJaxWsPortAccessWithFeatureObject() throws Exception {
50 		doTestJaxWsPortAccess(new AddressingFeature());
51 	}
52 
53 	@Test
testJaxWsPortAccessWithFeatureClass()54 	public void testJaxWsPortAccessWithFeatureClass() throws Exception {
55 		doTestJaxWsPortAccess(AddressingFeature.class);
56 	}
57 
58 	@Test
testJaxWsPortAccessWithFeatureString()59 	public void testJaxWsPortAccessWithFeatureString() throws Exception {
60 		doTestJaxWsPortAccess("javax.xml.ws.soap.AddressingFeature");
61 	}
62 
doTestJaxWsPortAccess(Object... features)63 	private void doTestJaxWsPortAccess(Object... features) throws Exception {
64 		GenericApplicationContext ac = new GenericApplicationContext();
65 
66 		GenericBeanDefinition serviceDef = new GenericBeanDefinition();
67 		serviceDef.setBeanClass(OrderServiceImpl.class);
68 		ac.registerBeanDefinition("service", serviceDef);
69 
70 		GenericBeanDefinition exporterDef = new GenericBeanDefinition();
71 		exporterDef.setBeanClass(SimpleJaxWsServiceExporter.class);
72 		exporterDef.getPropertyValues().add("baseAddress", "http://localhost:9999/");
73 		ac.registerBeanDefinition("exporter", exporterDef);
74 
75 		GenericBeanDefinition clientDef = new GenericBeanDefinition();
76 		clientDef.setBeanClass(JaxWsPortProxyFactoryBean.class);
77 		clientDef.getPropertyValues().add("wsdlDocumentUrl", "http://localhost:9999/OrderService?wsdl");
78 		clientDef.getPropertyValues().add("namespaceUri", "http://jaxws.remoting.springframework.org/");
79 		clientDef.getPropertyValues().add("username", "juergen");
80 		clientDef.getPropertyValues().add("password", "hoeller");
81 		clientDef.getPropertyValues().add("serviceName", "OrderService");
82 		clientDef.getPropertyValues().add("serviceInterface", OrderService.class);
83 		clientDef.getPropertyValues().add("lookupServiceOnStartup", Boolean.FALSE);
84 		if (features != null) {
85 			clientDef.getPropertyValues().add("webServiceFeatures", features);
86 		}
87 		ac.registerBeanDefinition("client", clientDef);
88 
89 		GenericBeanDefinition serviceFactoryDef = new GenericBeanDefinition();
90 		serviceFactoryDef.setBeanClass(LocalJaxWsServiceFactoryBean.class);
91 		serviceFactoryDef.getPropertyValues().add("wsdlDocumentUrl", "http://localhost:9999/OrderService?wsdl");
92 		serviceFactoryDef.getPropertyValues().add("namespaceUri", "http://jaxws.remoting.springframework.org/");
93 		serviceFactoryDef.getPropertyValues().add("serviceName", "OrderService");
94 		ac.registerBeanDefinition("orderService", serviceFactoryDef);
95 
96 		ac.registerBeanDefinition("accessor", new RootBeanDefinition(ServiceAccessor.class));
97 		AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
98 
99 		try {
100 			ac.refresh();
101 
102 			OrderService orderService = ac.getBean("client", OrderService.class);
103 			assertTrue(orderService instanceof BindingProvider);
104 			((BindingProvider) orderService).getRequestContext();
105 
106 			String order = orderService.getOrder(1000);
107 			assertEquals("order 1000", order);
108 			try {
109 				orderService.getOrder(0);
110 				fail("Should have thrown OrderNotFoundException");
111 			}
112 			catch (OrderNotFoundException ex) {
113 				// expected
114 			}
115 
116 			ServiceAccessor serviceAccessor = ac.getBean("accessor", ServiceAccessor.class);
117 			order = serviceAccessor.orderService.getOrder(1000);
118 			assertEquals("order 1000", order);
119 			try {
120 				serviceAccessor.orderService.getOrder(0);
121 				fail("Should have thrown OrderNotFoundException");
122 			}
123 			catch (OrderNotFoundException ex) {
124 				// expected
125 			}
126 		}
127 		catch (BeanCreationException ex) {
128 			if ("exporter".equals(ex.getBeanName()) && ex.getRootCause() instanceof ClassNotFoundException) {
129 				// ignore - probably running on JDK < 1.6 without the JAX-WS impl present
130 			}
131 			else {
132 				throw ex;
133 			}
134 		}
135 		finally {
136 			ac.close();
137 		}
138 	}
139 
140 
141 	public static class ServiceAccessor {
142 
143 		@WebServiceRef
144 		public OrderService orderService;
145 
146 		public OrderService myService;
147 
148 		@WebServiceRef(value=OrderServiceService.class, wsdlLocation = "http://localhost:9999/OrderService?wsdl")
setMyService(OrderService myService)149 		public void setMyService(OrderService myService) {
150 			this.myService = myService;
151 		}
152 	}
153 
154 
155 	@WebServiceClient(targetNamespace = "http://jaxws.remoting.springframework.org/", name="OrderService")
156 	public static class OrderServiceService extends Service {
157 
OrderServiceService()158 		public OrderServiceService() throws MalformedURLException {
159 			super(new URL("http://localhost:9999/OrderService?wsdl"),
160 					new QName("http://jaxws.remoting.springframework.org/", "OrderService"));
161 		}
162 
OrderServiceService(URL wsdlDocumentLocation, QName serviceName)163 		public OrderServiceService(URL wsdlDocumentLocation, QName serviceName) {
164 			super(wsdlDocumentLocation, serviceName);
165 		}
166 	}
167 
168 }
169