moving remoting.*, scheduling.* unit tests from .testsuite -> .context, .web

This commit is contained in:
Chris Beams
2008-12-17 06:13:28 +00:00
parent f5b1cae78c
commit 31f5961dce
14 changed files with 22 additions and 79 deletions

View File

@@ -0,0 +1,224 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.remoting.caucho;
import com.caucho.burlap.client.BurlapProxyFactory;
import com.caucho.hessian.client.HessianProxyFactory;
import junit.framework.TestCase;
import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
import org.springframework.remoting.RemoteAccessException;
/**
* @author Juergen Hoeller
* @since 16.05.2003
*/
public class CauchoRemotingTests extends TestCase {
public void testHessianProxyFactoryBeanWithAccessError() throws Exception {
HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
try {
factory.setServiceInterface(TestBean.class);
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// expected
}
factory.setServiceInterface(ITestBean.class);
factory.setServiceUrl("http://localhosta/testbean");
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertTrue(factory.getObject() instanceof ITestBean);
ITestBean bean = (ITestBean) factory.getObject();
try {
bean.setName("test");
fail("Should have thrown RemoteAccessException");
}
catch (RemoteAccessException ex) {
// expected
}
}
public void testHessianProxyFactoryBeanWithAuthenticationAndAccessError() throws Exception {
HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
try {
factory.setServiceInterface(TestBean.class);
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// expected
}
factory.setServiceInterface(ITestBean.class);
factory.setServiceUrl("http://localhosta/testbean");
factory.setUsername("test");
factory.setPassword("bean");
factory.setOverloadEnabled(true);
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertTrue(factory.getObject() instanceof ITestBean);
ITestBean bean = (ITestBean) factory.getObject();
try {
bean.setName("test");
fail("Should have thrown RemoteAccessException");
}
catch (RemoteAccessException ex) {
// expected
}
}
public void testHessianProxyFactoryBeanWithCustomProxyFactory() throws Exception {
TestHessianProxyFactory proxyFactory = new TestHessianProxyFactory();
HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
factory.setServiceInterface(ITestBean.class);
factory.setServiceUrl("http://localhosta/testbean");
factory.setProxyFactory(proxyFactory);
factory.setUsername("test");
factory.setPassword("bean");
factory.setOverloadEnabled(true);
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertTrue(factory.getObject() instanceof ITestBean);
ITestBean bean = (ITestBean) factory.getObject();
assertEquals(proxyFactory.user, "test");
assertEquals(proxyFactory.password, "bean");
assertTrue(proxyFactory.overloadEnabled);
try {
bean.setName("test");
fail("Should have thrown RemoteAccessException");
}
catch (RemoteAccessException ex) {
// expected
}
}
public void testBurlapProxyFactoryBeanWithAccessError() throws Exception {
BurlapProxyFactoryBean factory = new BurlapProxyFactoryBean();
factory.setServiceInterface(ITestBean.class);
factory.setServiceUrl("http://localhosta/testbean");
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertTrue(factory.getObject() instanceof ITestBean);
ITestBean bean = (ITestBean) factory.getObject();
try {
bean.setName("test");
fail("Should have thrown RemoteAccessException");
}
catch (RemoteAccessException ex) {
// expected
}
}
public void testBurlapProxyFactoryBeanWithAuthenticationAndAccessError() throws Exception {
BurlapProxyFactoryBean factory = new BurlapProxyFactoryBean();
factory.setServiceInterface(ITestBean.class);
factory.setServiceUrl("http://localhosta/testbean");
factory.setUsername("test");
factory.setPassword("bean");
factory.setOverloadEnabled(true);
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertTrue(factory.getObject() instanceof ITestBean);
ITestBean bean = (ITestBean) factory.getObject();
try {
bean.setName("test");
fail("Should have thrown RemoteAccessException");
}
catch (RemoteAccessException ex) {
// expected
}
}
public void testBurlapProxyFactoryBeanWithCustomProxyFactory() throws Exception {
TestBurlapProxyFactory proxyFactory = new TestBurlapProxyFactory();
BurlapProxyFactoryBean factory = new BurlapProxyFactoryBean();
factory.setServiceInterface(ITestBean.class);
factory.setServiceUrl("http://localhosta/testbean");
factory.setProxyFactory(proxyFactory);
factory.setUsername("test");
factory.setPassword("bean");
factory.setOverloadEnabled(true);
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertTrue(factory.getObject() instanceof ITestBean);
ITestBean bean = (ITestBean) factory.getObject();
assertEquals(proxyFactory.user, "test");
assertEquals(proxyFactory.password, "bean");
assertTrue(proxyFactory.overloadEnabled);
try {
bean.setName("test");
fail("Should have thrown RemoteAccessException");
}
catch (RemoteAccessException ex) {
// expected
}
}
private static class TestHessianProxyFactory extends HessianProxyFactory {
private String user;
private String password;
private boolean overloadEnabled;
public void setUser(String user) {
this.user = user;
}
public void setPassword(String password) {
this.password = password;
}
public void setOverloadEnabled(boolean overloadEnabled) {
this.overloadEnabled = overloadEnabled;
}
}
private static class TestBurlapProxyFactory extends BurlapProxyFactory {
private String user;
private String password;
private boolean overloadEnabled;
public void setUser(String user) {
this.user = user;
}
public void setPassword(String password) {
this.password = password;
}
public void setOverloadEnabled(boolean overloadEnabled) {
this.overloadEnabled = overloadEnabled;
}
}
}

View File

@@ -0,0 +1,469 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.remoting.httpinvoker;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.remoting.support.DefaultRemoteInvocationExecutor;
import org.springframework.remoting.support.RemoteInvocation;
import org.springframework.remoting.support.RemoteInvocationFactory;
import org.springframework.remoting.support.RemoteInvocationResult;
/**
* @author Juergen Hoeller
* @since 09.08.2004
*/
public class HttpInvokerTests extends TestCase {
public void testHttpInvokerProxyFactoryBeanAndServiceExporter() throws Throwable {
doTestHttpInvokerProxyFactoryBeanAndServiceExporter(false);
}
public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithExplicitClassLoader() throws Throwable {
doTestHttpInvokerProxyFactoryBeanAndServiceExporter(true);
}
private void doTestHttpInvokerProxyFactoryBeanAndServiceExporter(boolean explicitClassLoader) throws Throwable {
TestBean target = new TestBean("myname", 99);
final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
exporter.setServiceInterface(ITestBean.class);
exporter.setService(target);
exporter.afterPropertiesSet();
HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
pfb.setServiceInterface(ITestBean.class);
pfb.setServiceUrl("http://myurl");
pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {
protected RemoteInvocationResult doExecuteRequest(
HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
assertEquals("http://myurl", config.getServiceUrl());
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setContent(baos.toByteArray());
exporter.handleRequest(request, response);
return readRemoteInvocationResult(
new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
}
});
if (explicitClassLoader) {
((BeanClassLoaderAware) pfb.getHttpInvokerRequestExecutor()).setBeanClassLoader(getClass().getClassLoader());
}
pfb.afterPropertiesSet();
ITestBean proxy = (ITestBean) pfb.getObject();
assertEquals("myname", proxy.getName());
assertEquals(99, proxy.getAge());
proxy.setAge(50);
assertEquals(50, proxy.getAge());
proxy.setStringArray(new String[] {"str1", "str2"});
assertTrue(Arrays.equals(new String[] {"str1", "str2"}, proxy.getStringArray()));
try {
proxy.exceptional(new IllegalStateException());
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
try {
proxy.exceptional(new IllegalAccessException());
fail("Should have thrown IllegalAccessException");
}
catch (IllegalAccessException ex) {
// expected
}
}
public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithIOException() throws Exception {
TestBean target = new TestBean("myname", 99);
final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
exporter.setServiceInterface(ITestBean.class);
exporter.setService(target);
exporter.afterPropertiesSet();
HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
pfb.setServiceInterface(ITestBean.class);
pfb.setServiceUrl("http://myurl");
pfb.setHttpInvokerRequestExecutor(new HttpInvokerRequestExecutor() {
public RemoteInvocationResult executeRequest(
HttpInvokerClientConfiguration config, RemoteInvocation invocation) throws IOException {
throw new IOException("argh");
}
});
pfb.afterPropertiesSet();
ITestBean proxy = (ITestBean) pfb.getObject();
try {
proxy.setAge(50);
fail("Should have thrown RemoteAccessException");
}
catch (RemoteAccessException ex) {
// expected
assertTrue(ex.getCause() instanceof IOException);
}
}
public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithGzipCompression() throws Throwable {
TestBean target = new TestBean("myname", 99);
final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter() {
protected InputStream decorateInputStream(HttpServletRequest request, InputStream is) throws IOException {
if ("gzip".equals(request.getHeader("Compression"))) {
return new GZIPInputStream(is);
}
else {
return is;
}
}
protected OutputStream decorateOutputStream(
HttpServletRequest request, HttpServletResponse response, OutputStream os) throws IOException {
if ("gzip".equals(request.getHeader("Compression"))) {
return new GZIPOutputStream(os);
}
else {
return os;
}
}
};
exporter.setServiceInterface(ITestBean.class);
exporter.setService(target);
exporter.afterPropertiesSet();
HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
pfb.setServiceInterface(ITestBean.class);
pfb.setServiceUrl("http://myurl");
pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {
protected RemoteInvocationResult doExecuteRequest(
HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)
throws IOException, ClassNotFoundException {
assertEquals("http://myurl", config.getServiceUrl());
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Compression", "gzip");
MockHttpServletResponse response = new MockHttpServletResponse();
request.setContent(baos.toByteArray());
try {
exporter.handleRequest(request, response);
}
catch (ServletException ex) {
throw new IOException(ex.toString());
}
return readRemoteInvocationResult(
new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
}
protected OutputStream decorateOutputStream(OutputStream os) throws IOException {
return new GZIPOutputStream(os);
}
protected InputStream decorateInputStream(InputStream is) throws IOException {
return new GZIPInputStream(is);
}
});
pfb.afterPropertiesSet();
ITestBean proxy = (ITestBean) pfb.getObject();
assertEquals("myname", proxy.getName());
assertEquals(99, proxy.getAge());
proxy.setAge(50);
assertEquals(50, proxy.getAge());
try {
proxy.exceptional(new IllegalStateException());
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
try {
proxy.exceptional(new IllegalAccessException());
fail("Should have thrown IllegalAccessException");
}
catch (IllegalAccessException ex) {
// expected
}
}
public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithWrappedInvocations() throws Throwable {
TestBean target = new TestBean("myname", 99);
final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter() {
protected RemoteInvocation doReadRemoteInvocation(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
Object obj = ois.readObject();
if (!(obj instanceof TestRemoteInvocationWrapper)) {
throw new IOException("Deserialized object needs to be assignable to type [" +
TestRemoteInvocationWrapper.class.getName() + "]: " + obj);
}
return ((TestRemoteInvocationWrapper) obj).remoteInvocation;
}
protected void doWriteRemoteInvocationResult(RemoteInvocationResult result, ObjectOutputStream oos)
throws IOException {
oos.writeObject(new TestRemoteInvocationResultWrapper(result));
}
};
exporter.setServiceInterface(ITestBean.class);
exporter.setService(target);
exporter.afterPropertiesSet();
HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
pfb.setServiceInterface(ITestBean.class);
pfb.setServiceUrl("http://myurl");
pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {
protected RemoteInvocationResult doExecuteRequest(
HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
assertEquals("http://myurl", config.getServiceUrl());
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setContent(baos.toByteArray());
exporter.handleRequest(request, response);
return readRemoteInvocationResult(
new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
}
protected void doWriteRemoteInvocation(RemoteInvocation invocation, ObjectOutputStream oos) throws IOException {
oos.writeObject(new TestRemoteInvocationWrapper(invocation));
}
protected RemoteInvocationResult doReadRemoteInvocationResult(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
Object obj = ois.readObject();
if (!(obj instanceof TestRemoteInvocationResultWrapper)) {
throw new IOException("Deserialized object needs to be assignable to type ["
+ TestRemoteInvocationResultWrapper.class.getName() + "]: " + obj);
}
return ((TestRemoteInvocationResultWrapper) obj).remoteInvocationResult;
}
});
pfb.afterPropertiesSet();
ITestBean proxy = (ITestBean) pfb.getObject();
assertEquals("myname", proxy.getName());
assertEquals(99, proxy.getAge());
proxy.setAge(50);
assertEquals(50, proxy.getAge());
try {
proxy.exceptional(new IllegalStateException());
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
try {
proxy.exceptional(new IllegalAccessException());
fail("Should have thrown IllegalAccessException");
}
catch (IllegalAccessException ex) {
// expected
}
}
public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithInvocationAttributes() throws Exception {
TestBean target = new TestBean("myname", 99);
final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
exporter.setServiceInterface(ITestBean.class);
exporter.setService(target);
exporter.setRemoteInvocationExecutor(new DefaultRemoteInvocationExecutor() {
public Object invoke(RemoteInvocation invocation, Object targetObject)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
assertNotNull(invocation.getAttributes());
assertEquals(1, invocation.getAttributes().size());
assertEquals("myValue", invocation.getAttributes().get("myKey"));
assertEquals("myValue", invocation.getAttribute("myKey"));
return super.invoke(invocation, targetObject);
}
});
exporter.afterPropertiesSet();
HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
pfb.setServiceInterface(ITestBean.class);
pfb.setServiceUrl("http://myurl");
pfb.setRemoteInvocationFactory(new RemoteInvocationFactory() {
public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
RemoteInvocation invocation = new RemoteInvocation(methodInvocation);
invocation.addAttribute("myKey", "myValue");
try {
invocation.addAttribute("myKey", "myValue");
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected: already defined
}
assertNotNull(invocation.getAttributes());
assertEquals(1, invocation.getAttributes().size());
assertEquals("myValue", invocation.getAttributes().get("myKey"));
assertEquals("myValue", invocation.getAttribute("myKey"));
return invocation;
}
});
pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {
protected RemoteInvocationResult doExecuteRequest(
HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
assertEquals("http://myurl", config.getServiceUrl());
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setContent(baos.toByteArray());
exporter.handleRequest(request, response);
return readRemoteInvocationResult(
new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
}
});
pfb.afterPropertiesSet();
ITestBean proxy = (ITestBean) pfb.getObject();
assertEquals("myname", proxy.getName());
assertEquals(99, proxy.getAge());
}
public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithCustomInvocationObject() throws Exception {
TestBean target = new TestBean("myname", 99);
final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
exporter.setServiceInterface(ITestBean.class);
exporter.setService(target);
exporter.setRemoteInvocationExecutor(new DefaultRemoteInvocationExecutor() {
public Object invoke(RemoteInvocation invocation, Object targetObject)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
assertTrue(invocation instanceof TestRemoteInvocation);
assertNull(invocation.getAttributes());
assertNull(invocation.getAttribute("myKey"));
return super.invoke(invocation, targetObject);
}
});
exporter.afterPropertiesSet();
HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
pfb.setServiceInterface(ITestBean.class);
pfb.setServiceUrl("http://myurl");
pfb.setRemoteInvocationFactory(new RemoteInvocationFactory() {
public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
RemoteInvocation invocation = new TestRemoteInvocation(methodInvocation);
assertNull(invocation.getAttributes());
assertNull(invocation.getAttribute("myKey"));
return invocation;
}
});
pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {
protected RemoteInvocationResult doExecuteRequest(
HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
assertEquals("http://myurl", config.getServiceUrl());
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setContent(baos.toByteArray());
exporter.handleRequest(request, response);
return readRemoteInvocationResult(
new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
}
});
pfb.afterPropertiesSet();
ITestBean proxy = (ITestBean) pfb.getObject();
assertEquals("myname", proxy.getName());
assertEquals(99, proxy.getAge());
}
public void testHttpInvokerWithSpecialLocalMethods() throws Exception {
String serviceUrl = "http://myurl";
HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
pfb.setServiceInterface(ITestBean.class);
pfb.setServiceUrl(serviceUrl);
pfb.setHttpInvokerRequestExecutor(new HttpInvokerRequestExecutor() {
public RemoteInvocationResult executeRequest(
HttpInvokerClientConfiguration config, RemoteInvocation invocation) throws IOException {
throw new IOException("argh");
}
});
pfb.afterPropertiesSet();
ITestBean proxy = (ITestBean) pfb.getObject();
// shouldn't go through to remote service
assertTrue(proxy.toString().indexOf("HTTP invoker") != -1);
assertTrue(proxy.toString().indexOf(serviceUrl) != -1);
assertEquals(proxy.hashCode(), proxy.hashCode());
assertTrue(proxy.equals(proxy));
// should go through
try {
proxy.setAge(50);
fail("Should have thrown RemoteAccessException");
}
catch (RemoteAccessException ex) {
// expected
assertTrue(ex.getCause() instanceof IOException);
}
}
private static class TestRemoteInvocation extends RemoteInvocation {
public TestRemoteInvocation(MethodInvocation methodInvocation) {
super(methodInvocation);
}
}
private static class TestRemoteInvocationWrapper implements Serializable {
private final RemoteInvocation remoteInvocation;
public TestRemoteInvocationWrapper(RemoteInvocation remoteInvocation) {
this.remoteInvocation = remoteInvocation;
}
}
private static class TestRemoteInvocationResultWrapper implements Serializable {
private final RemoteInvocationResult remoteInvocationResult;
public TestRemoteInvocationResultWrapper(RemoteInvocationResult remoteInvocationResult) {
this.remoteInvocationResult = remoteInvocationResult;
}
}
}

View File

@@ -0,0 +1,705 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.remoting.jaxrpc;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.Stub;
import junit.framework.TestCase;
import org.easymock.ArgumentsMatcher;
import org.easymock.MockControl;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.remoting.RemoteLookupFailureException;
import org.springframework.util.ObjectUtils;
/**
* @author Juergen Hoeller
* @since 18.12.2003
*/
public class JaxRpcSupportTests extends TestCase {
public void testLocalJaxRpcServiceFactoryBeanWithServiceNameAndNamespace() throws Exception {
LocalJaxRpcServiceFactoryBean factory = new LocalJaxRpcServiceFactoryBean();
factory.setServiceFactoryClass(MockServiceFactory.class);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myService1");
factory.afterPropertiesSet();
assertEquals(MockServiceFactory.service1, factory.getObject());
}
public void testLocalJaxRpcServiceFactoryBeanWithServiceNameAndWsdl() throws Exception {
LocalJaxRpcServiceFactoryBean factory = new LocalJaxRpcServiceFactoryBean();
factory.setServiceFactoryClass(MockServiceFactory.class);
factory.setServiceName("myService2");
factory.setWsdlDocumentUrl(new URL("http://myUrl1"));
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertEquals(MockServiceFactory.service2, factory.getObject());
}
public void testLocalJaxRpcServiceFactoryBeanWithServiceNameAndWsdlAndProperties() throws Exception {
LocalJaxRpcServiceFactoryBean factory = new LocalJaxRpcServiceFactoryBean();
factory.setServiceFactoryClass(MockServiceFactory.class);
factory.setServiceName("myService2");
factory.setWsdlDocumentUrl(new URL("http://myUrl1"));
Properties props = new Properties();
props.setProperty("myKey", "myValue");
factory.setJaxRpcServiceProperties(props);
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertEquals(MockServiceFactory.service1, factory.getObject());
}
public void testLocalJaxRpcServiceFactoryBeanWithJaxRpcServiceInterface() throws Exception {
LocalJaxRpcServiceFactoryBean factory = new LocalJaxRpcServiceFactoryBean();
factory.setServiceFactoryClass(MockServiceFactory.class);
factory.setJaxRpcServiceInterface(IRemoteBean.class);
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertEquals(MockServiceFactory.service2, factory.getObject());
}
public void testLocalJaxRpcServiceFactoryBeanWithJaxRpcServiceInterfaceAndWsdl() throws Exception {
LocalJaxRpcServiceFactoryBean factory = new LocalJaxRpcServiceFactoryBean();
factory.setServiceFactoryClass(MockServiceFactory.class);
factory.setWsdlDocumentUrl(new URL("http://myUrl1"));
factory.setJaxRpcServiceInterface(IRemoteBean.class);
Properties props = new Properties();
props.setProperty("myKey", "myValue");
factory.setJaxRpcServiceProperties(props);
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertEquals(MockServiceFactory.service1, factory.getObject());
}
public void testJaxRpcPortProxyFactoryBean() throws Exception {
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactoryClass(MockServiceFactory.class);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myService1");
factory.setPortName("myPort");
factory.setPortInterface(IRemoteBean.class);
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertTrue(factory.getPortStub() instanceof Stub);
assertTrue(factory.getObject() instanceof IRemoteBean);
IRemoteBean proxy = (IRemoteBean) factory.getObject();
proxy.setName("myName");
assertEquals("myName", RemoteBean.name);
MockServiceFactory.service1Control.verify();
}
public void testJaxRpcPortProxyFactoryBeanWithProperties() throws Exception {
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactoryClass(MockServiceFactory.class);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myService1");
factory.setPortName("myPort");
factory.setUsername("user");
factory.setPassword("pw");
factory.setEndpointAddress("ea");
factory.setMaintainSession(true);
factory.setPortInterface(IRemoteBean.class);
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertTrue(factory.getPortStub() instanceof Stub);
Stub stub = (Stub) factory.getPortStub();
assertEquals("user", stub._getProperty(Stub.USERNAME_PROPERTY));
assertEquals("pw", stub._getProperty(Stub.PASSWORD_PROPERTY));
assertEquals("ea", stub._getProperty(Stub.ENDPOINT_ADDRESS_PROPERTY));
assertTrue(((Boolean) stub._getProperty(Stub.SESSION_MAINTAIN_PROPERTY)).booleanValue());
assertTrue(factory.getObject() instanceof IRemoteBean);
IRemoteBean proxy = (IRemoteBean) factory.getObject();
proxy.setName("myName");
assertEquals("myName", RemoteBean.name);
MockServiceFactory.service1Control.verify();
}
public void testJaxRpcPortProxyFactoryBeanWithCustomProperties() throws Exception {
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactoryClass(MockServiceFactory.class);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myService1");
factory.setPortName("myPort");
factory.setUsername("user");
factory.setPassword("pw");
Properties customProps = new Properties();
customProps.setProperty("myProp", "myValue");
factory.setCustomProperties(customProps);
factory.setPortInterface(IRemoteBean.class);
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertTrue(factory.getPortStub() instanceof Stub);
Stub stub = (Stub) factory.getPortStub();
assertEquals("user", stub._getProperty(Stub.USERNAME_PROPERTY));
assertEquals("pw", stub._getProperty(Stub.PASSWORD_PROPERTY));
assertEquals("myValue", stub._getProperty("myProp"));
assertTrue(factory.getObject() instanceof IRemoteBean);
IRemoteBean proxy = (IRemoteBean) factory.getObject();
proxy.setName("myName");
assertEquals("myName", RemoteBean.name);
MockServiceFactory.service1Control.verify();
}
public void testJaxRpcPortProxyFactoryBeanWithCustomPropertyMap() throws Exception {
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactoryClass(MockServiceFactory.class);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myService1");
factory.setPortName("myPort");
factory.setEndpointAddress("ea");
factory.setMaintainSession(true);
Map customProps = new HashMap();
customProps.put("myProp", new Integer(1));
factory.setCustomPropertyMap(customProps);
factory.addCustomProperty("myOtherProp", "myOtherValue");
factory.setPortInterface(IRemoteBean.class);
factory.afterPropertiesSet();
assertTrue("Correct singleton value", factory.isSingleton());
assertTrue(factory.getPortStub() instanceof Stub);
Stub stub = (Stub) factory.getPortStub();
assertEquals("ea", stub._getProperty(Stub.ENDPOINT_ADDRESS_PROPERTY));
assertTrue(((Boolean) stub._getProperty(Stub.SESSION_MAINTAIN_PROPERTY)).booleanValue());
assertEquals(new Integer(1), stub._getProperty("myProp"));
assertEquals("myOtherValue", stub._getProperty("myOtherProp"));
assertTrue(factory.getObject() instanceof IRemoteBean);
IRemoteBean proxy = (IRemoteBean) factory.getObject();
proxy.setName("myName");
assertEquals("myName", RemoteBean.name);
MockServiceFactory.service1Control.verify();
}
public void testJaxRpcPortProxyFactoryBeanWithDynamicCalls() throws Exception {
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactoryClass(CallMockServiceFactory.class);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myService1");
factory.setPortName("myPort");
factory.setServiceInterface(IBusinessBean.class);
factory.afterPropertiesSet();
assertNull(factory.getPortStub());
assertTrue(factory.getObject() instanceof IBusinessBean);
IBusinessBean proxy = (IBusinessBean) factory.getObject();
proxy.setName("myName");
MockServiceFactory.service1Control.verify();
CallMockServiceFactory.call1Control.verify();
}
public void testJaxRpcPortProxyFactoryBeanWithDynamicCallsAndProperties() throws Exception {
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactoryClass(CallWithPropertiesMockServiceFactory.class);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myService1");
factory.setPortName("myPort");
factory.setUsername("user");
factory.setPassword("pw");
factory.setEndpointAddress("ea");
factory.setMaintainSession(true);
factory.setServiceInterface(IBusinessBean.class);
factory.afterPropertiesSet();
assertNull(factory.getPortStub());
assertTrue(factory.getObject() instanceof IBusinessBean);
IBusinessBean proxy = (IBusinessBean) factory.getObject();
proxy.setName("myName");
MockServiceFactory.service1Control.verify();
CallMockServiceFactory.call1Control.verify();
}
public void testJaxRpcPortProxyFactoryBeanWithDynamicCallsAndServiceException() throws Exception {
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactoryClass(CallMockServiceFactory.class);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myServiceX");
factory.setPortName("myPort");
factory.setServiceInterface(IRemoteBean.class);
try {
factory.afterPropertiesSet();
fail("Should have thrown RemoteLookupFailureException");
}
catch (RemoteLookupFailureException ex) {
// expected
}
}
public void testJaxRpcPortProxyFactoryBeanWithDynamicCallsAndLazyLookupAndServiceException() throws Exception {
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactoryClass(CallMockServiceFactory.class);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myServiceX");
factory.setPortName("myPort");
factory.setServiceInterface(IRemoteBean.class);
factory.setLookupServiceOnStartup(false);
factory.afterPropertiesSet();
assertTrue(factory.getObject() instanceof IRemoteBean);
IRemoteBean proxy = (IRemoteBean) factory.getObject();
try {
proxy.setName("exception");
fail("Should have thrown RemoteException");
}
catch (RemoteLookupFailureException ex) {
// expected
assertTrue(ex.getCause() instanceof ServiceException);
}
}
public void testJaxRpcPortProxyFactoryBeanWithDynamicCallsAndRemoteException() throws Exception {
ExceptionCallMockServiceFactory serviceFactory = new ExceptionCallMockServiceFactory();
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactory(serviceFactory);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myService1");
factory.setPortName("myPort");
factory.setServiceInterface(IRemoteBean.class);
factory.afterPropertiesSet();
assertTrue(factory.getObject() instanceof IRemoteBean);
IRemoteBean proxy = (IRemoteBean) factory.getObject();
try {
proxy.setName("exception");
fail("Should have thrown RemoteException");
}
catch (RemoteException ex) {
// expected
}
proxy.setName("myName");
assertEquals("myName", RemoteBean.name);
proxy.setName("myName");
assertEquals("myName", RemoteBean.name);
assertEquals(1, serviceFactory.serviceCount);
MockServiceFactory.service1Control.verify();
CallMockServiceFactory.call1Control.verify();
ExceptionCallMockServiceFactory.call2Control.verify();
}
public void testJaxRpcPortProxyFactoryBeanWithDynamicCallsAndRemoteExceptionAndRefresh() throws Exception {
ExceptionCallMockServiceFactory serviceFactory = new ExceptionCallMockServiceFactory();
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactory(serviceFactory);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myService1");
factory.setPortName("myPort");
factory.setServiceInterface(IRemoteBean.class);
factory.setRefreshServiceAfterConnectFailure(true);
factory.afterPropertiesSet();
assertTrue(factory.getObject() instanceof IRemoteBean);
IRemoteBean proxy = (IRemoteBean) factory.getObject();
try {
proxy.setName("exception");
fail("Should have thrown RemoteException");
}
catch (RemoteException ex) {
// expected
}
proxy.setName("myName");
assertEquals("myName", RemoteBean.name);
proxy.setName("myName");
assertEquals("myName", RemoteBean.name);
assertEquals(2, serviceFactory.serviceCount);
MockServiceFactory.service1Control.verify();
CallMockServiceFactory.call1Control.verify();
ExceptionCallMockServiceFactory.call2Control.verify();
}
public void testJaxRpcPortProxyFactoryBeanWithPortInterface() throws Exception {
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactoryClass(MockServiceFactory.class);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myService1");
factory.setPortName("myPort");
factory.setPortInterface(IRemoteBean.class);
factory.setServiceInterface(IBusinessBean.class);
factory.afterPropertiesSet();
assertTrue(factory.getObject() instanceof IBusinessBean);
assertFalse(factory.getObject() instanceof IRemoteBean);
IBusinessBean proxy = (IBusinessBean) factory.getObject();
proxy.setName("myName");
assertEquals("myName", RemoteBean.name);
MockServiceFactory.service1Control.verify();
}
public void testJaxRpcPortProxyFactoryBeanWithPortInterfaceAndServiceException() throws Exception {
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactoryClass(MockServiceFactory.class);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myServiceX");
factory.setPortInterface(IRemoteBean.class);
factory.setPortName("myPort");
factory.setServiceInterface(IRemoteBean.class);
try {
factory.afterPropertiesSet();
fail("Should have thrown RemoteLookupFailureException");
}
catch (RemoteLookupFailureException ex) {
// expected
}
}
public void testJaxRpcPortProxyFactoryBeanWithPortInterfaceAndLazyLookupAndServiceException() throws Exception {
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactoryClass(MockServiceFactory.class);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myServiceX");
factory.setPortName("myPort");
factory.setPortInterface(IRemoteBean.class);
factory.setServiceInterface(IRemoteBean.class);
factory.setLookupServiceOnStartup(false);
factory.afterPropertiesSet();
assertTrue(factory.getObject() instanceof IRemoteBean);
IRemoteBean proxy = (IRemoteBean) factory.getObject();
try {
proxy.setName("exception");
fail("Should have thrown Service");
}
catch (RemoteLookupFailureException ex) {
// expected
assertTrue(ex.getCause() instanceof ServiceException);
}
}
public void testJaxRpcPortProxyFactoryBeanWithPortInterfaceAndRemoteException() throws Exception {
MockServiceFactory serviceFactory = new MockServiceFactory();
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactory(serviceFactory);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myService1");
factory.setPortName("myPort");
factory.setPortInterface(IRemoteBean.class);
factory.setServiceInterface(IBusinessBean.class);
factory.afterPropertiesSet();
assertTrue(factory.getObject() instanceof IBusinessBean);
assertFalse(factory.getObject() instanceof IRemoteBean);
IBusinessBean proxy = (IBusinessBean) factory.getObject();
try {
proxy.setName("exception");
fail("Should have thrown RemoteAccessException");
}
catch (RemoteAccessException ex) {
// expected
}
proxy.setName("myName");
assertEquals("myName", RemoteBean.name);
proxy.setName("myName");
assertEquals("myName", RemoteBean.name);
assertEquals(1, serviceFactory.serviceCount);
MockServiceFactory.service1Control.verify();
}
public void testJaxRpcPortProxyFactoryBeanWithPortInterfaceAndRemoteExceptionAndRefresh() throws Exception {
ExceptionMockServiceFactory serviceFactory = new ExceptionMockServiceFactory();
JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
factory.setServiceFactory(serviceFactory);
factory.setNamespaceUri("myNamespace");
factory.setServiceName("myService1");
factory.setPortName("myPort");
factory.setPortInterface(IRemoteBean.class);
factory.setServiceInterface(IBusinessBean.class);
factory.setRefreshServiceAfterConnectFailure(true);
factory.afterPropertiesSet();
assertTrue(factory.getObject() instanceof IBusinessBean);
assertFalse(factory.getObject() instanceof IRemoteBean);
IBusinessBean proxy = (IBusinessBean) factory.getObject();
try {
proxy.setName("exception");
fail("Should have thrown RemoteAccessException");
}
catch (RemoteAccessException ex) {
// expected
}
proxy.setName("myName");
assertEquals("myName", RemoteBean.name);
proxy.setName("myName");
assertEquals("myName", RemoteBean.name);
assertEquals(2, serviceFactory.serviceCount);
MockServiceFactory.service1Control.verify();
}
public static class MockServiceFactory extends ServiceFactory {
protected static MockControl service1Control;
protected static Service service1;
protected static MockControl service2Control;
protected static Service service2;
protected int serviceCount = 0;
public MockServiceFactory() throws Exception {
service1Control = MockControl.createControl(Service.class);
service1 = (Service) service1Control.getMock();
service2Control = MockControl.createControl(Service.class);
service2 = (Service) service2Control.getMock();
initMocks();
service1Control.replay();
}
protected void initMocks() throws Exception {
service1.getPort(new QName("myNamespace", "myPort"), IRemoteBean.class);
service1Control.setReturnValue(new RemoteBean());
}
public Service createService(QName qName) throws ServiceException {
if (!"myNamespace".equals(qName.getNamespaceURI()) || !"myService1".equals(qName.getLocalPart())) {
throw new ServiceException("not supported");
}
serviceCount++;
return service1;
}
public Service createService(URL url, QName qName) throws ServiceException {
try {
if (!(new URL("http://myUrl1")).equals(url) || !"".equals(qName.getNamespaceURI()) ||
!"myService2".equals(qName.getLocalPart())) {
throw new ServiceException("not supported");
}
}
catch (MalformedURLException ex) {
}
serviceCount++;
return service2;
}
public Service loadService(URL url, QName qName, Properties props) throws ServiceException {
try {
if (!(new URL("http://myUrl1")).equals(url) || !"".equals(qName.getNamespaceURI()) ||
!"myService2".equals(qName.getLocalPart())) {
throw new ServiceException("not supported");
}
}
catch (MalformedURLException ex) {
}
if (props == null || !"myValue".equals(props.getProperty("myKey"))) {
throw new ServiceException("invalid properties");
}
serviceCount++;
return service1;
}
public Service loadService(Class ifc) throws ServiceException {
if (!IRemoteBean.class.equals(ifc)) {
throw new ServiceException("not supported");
}
serviceCount++;
return service2;
}
public Service loadService(URL url, Class ifc, Properties props) throws ServiceException {
try {
if (!(new URL("http://myUrl1")).equals(url) || !IRemoteBean.class.equals(ifc)) {
throw new ServiceException("not supported");
}
}
catch (MalformedURLException ex) {
}
if (props == null || !"myValue".equals(props.getProperty("myKey"))) {
throw new ServiceException("invalid properties");
}
serviceCount++;
return service1;
}
}
public static class ExceptionMockServiceFactory extends MockServiceFactory {
public ExceptionMockServiceFactory() throws Exception {
super();
}
protected void initMocks() throws Exception {
super.initMocks();
service1.getPort(new QName("myNamespace", "myPort"), IRemoteBean.class);
service1Control.setReturnValue(new RemoteBean());
}
}
public static class CallMockServiceFactory extends MockServiceFactory {
protected static MockControl call1Control;
protected static Call call1;
public CallMockServiceFactory() throws Exception {
super();
}
protected void initMocks() throws Exception {
initStandardCall(1);
}
protected void initStandardCall(int count) throws Exception {
call1Control = MockControl.createControl(Call.class);
call1 = (Call) call1Control.getMock();
service1.createCall(new QName("myNamespace", "myPort"), "setName");
service1Control.setReturnValue(call1, count);
call1.invoke(new Object[] {"myName"});
call1Control.setMatcher(new ArgumentsMatcher() {
public boolean matches(Object[] objects, Object[] objects1) {
return Arrays.equals((Object[]) objects[0], (Object[]) objects1[0]);
}
public String toString(Object[] objects) {
return ObjectUtils.nullSafeToString(objects[0]);
}
});
call1Control.setReturnValue(null, count);
extendStandardCall();
call1Control.replay();
}
protected void extendStandardCall() {
}
}
public static class ExceptionCallMockServiceFactory extends CallMockServiceFactory {
protected static MockControl call2Control;
protected static Call call2;
public ExceptionCallMockServiceFactory() throws Exception {
}
protected void initMocks() throws Exception {
initExceptionCall();
initStandardCall(2);
}
protected void initExceptionCall() throws Exception {
call2Control = MockControl.createControl(Call.class);
call2 = (Call) call2Control.getMock();
service1.createCall(new QName("myNamespace", "myPort"), "setName");
service1Control.setReturnValue(call2);
call2.invoke(new Object[] {"exception"});
call2Control.setMatcher(new ArgumentsMatcher() {
public boolean matches(Object[] objects, Object[] objects1) {
return Arrays.equals((Object[]) objects[0], (Object[]) objects1[0]);
}
public String toString(Object[] objects) {
return ObjectUtils.nullSafeToString(objects[0]);
}
});
call2Control.setThrowable(new RemoteException());
call2Control.replay();
}
}
public static class CallWithPropertiesMockServiceFactory extends CallMockServiceFactory {
public CallWithPropertiesMockServiceFactory() throws Exception {
}
protected void extendStandardCall() {
call1.setProperty(Call.USERNAME_PROPERTY, "user");
call1Control.setVoidCallable();
call1.setProperty(Call.PASSWORD_PROPERTY, "pw");
call1Control.setVoidCallable();
call1.setTargetEndpointAddress("ea");
call1Control.setVoidCallable();
call1.setProperty(Call.SESSION_MAINTAIN_PROPERTY, Boolean.TRUE);
call1Control.setVoidCallable();
}
}
public static interface IBusinessBean {
public void setName(String name);
}
public static interface IRemoteBean extends Remote {
public void setName(String name) throws RemoteException;
}
public static class RemoteBean implements IRemoteBean, Stub {
private static String name;
private static Map properties;
public RemoteBean() {
properties = new HashMap();
}
public void setName(String nam) throws RemoteException {
if ("exception".equals(nam)) {
throw new RemoteException();
}
name = nam;
}
public void _setProperty(String key, Object o) {
properties.put(key, o);
}
public Object _getProperty(String key) {
return properties.get(key);
}
public Iterator _getPropertyNames() {
return properties.keySet().iterator();
}
}
}

View File

@@ -0,0 +1,144 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.remoting.jaxws;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceRef;
import junit.framework.TestCase;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.support.GenericApplicationContext;
/**
* @author Juergen Hoeller
* @since 2.5
*/
public class JaxWsSupportTests extends TestCase {
public void testJaxWsPortAccess() throws Exception {
GenericApplicationContext ac = new GenericApplicationContext();
GenericBeanDefinition serviceDef = new GenericBeanDefinition();
serviceDef.setBeanClass(OrderServiceImpl.class);
ac.registerBeanDefinition("service", serviceDef);
GenericBeanDefinition exporterDef = new GenericBeanDefinition();
exporterDef.setBeanClass(SimpleJaxWsServiceExporter.class);
exporterDef.getPropertyValues().addPropertyValue("baseAddress", "http://localhost:9999/");
ac.registerBeanDefinition("exporter", exporterDef);
GenericBeanDefinition clientDef = new GenericBeanDefinition();
clientDef.setBeanClass(JaxWsPortProxyFactoryBean.class);
clientDef.getPropertyValues().addPropertyValue("wsdlDocumentUrl", "http://localhost:9999/OrderService?wsdl");
clientDef.getPropertyValues().addPropertyValue("namespaceUri", "http://jaxws.remoting.springframework.org/");
clientDef.getPropertyValues().addPropertyValue("username", "juergen");
clientDef.getPropertyValues().addPropertyValue("password", "hoeller");
clientDef.getPropertyValues().addPropertyValue("serviceName", "OrderService");
clientDef.getPropertyValues().addPropertyValue("serviceInterface", OrderService.class);
clientDef.getPropertyValues().addPropertyValue("lookupServiceOnStartup", Boolean.FALSE);
ac.registerBeanDefinition("client", clientDef);
GenericBeanDefinition serviceFactoryDef = new GenericBeanDefinition();
serviceFactoryDef.setBeanClass(LocalJaxWsServiceFactoryBean.class);
serviceFactoryDef.getPropertyValues().addPropertyValue("wsdlDocumentUrl", "http://localhost:9999/OrderService?wsdl");
serviceFactoryDef.getPropertyValues().addPropertyValue("namespaceUri", "http://jaxws.remoting.springframework.org/");
serviceFactoryDef.getPropertyValues().addPropertyValue("serviceName", "OrderService");
ac.registerBeanDefinition("orderService", serviceFactoryDef);
ac.registerBeanDefinition("accessor", new RootBeanDefinition(ServiceAccessor.class));
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
try {
ac.refresh();
OrderService orderService = (OrderService) ac.getBean("client", OrderService.class);
assertTrue(orderService instanceof BindingProvider);
((BindingProvider) orderService).getRequestContext();
String order = orderService.getOrder(1000);
assertEquals("order 1000", order);
try {
orderService.getOrder(0);
fail("Should have thrown OrderNotFoundException");
}
catch (OrderNotFoundException ex) {
// expected
}
ServiceAccessor serviceAccessor = (ServiceAccessor) ac.getBean("accessor", ServiceAccessor.class);
order = serviceAccessor.orderService.getOrder(1000);
assertEquals("order 1000", order);
try {
serviceAccessor.orderService.getOrder(0);
fail("Should have thrown OrderNotFoundException");
}
catch (OrderNotFoundException ex) {
// expected
}
}
catch (BeanCreationException ex) {
if ("exporter".equals(ex.getBeanName()) && ex.getRootCause() instanceof ClassNotFoundException) {
// ignore - probably running on JDK < 1.6 without the JAX-WS impl present
}
else {
throw ex;
}
}
finally {
ac.close();
}
}
public static class ServiceAccessor {
@WebServiceRef
public OrderService orderService;
public OrderService myService;
@WebServiceRef(value=OrderServiceService.class, wsdlLocation = "http://localhost:9999/OrderService?wsdl")
public void setMyService(OrderService myService) {
this.myService = myService;
}
}
@WebServiceClient(targetNamespace = "http://jaxws.remoting.springframework.org/", name="OrderService")
public static class OrderServiceService extends Service {
public OrderServiceService() throws MalformedURLException {
super(new URL("http://localhost:9999/OrderService?wsdl"),
new QName("http://jaxws.remoting.springframework.org/", "OrderService"));
}
public OrderServiceService(URL wsdlDocumentLocation, QName serviceName) {
super(wsdlDocumentLocation, serviceName);
}
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.remoting.jaxws;
import javax.xml.ws.WebFault;
/**
* @author Juergen Hoeller
*/
@WebFault
public class OrderNotFoundException extends Exception {
private String faultInfo;
public OrderNotFoundException(String message) {
super(message);
}
public OrderNotFoundException(String message, String faultInfo) {
super(message);
this.faultInfo = faultInfo;
}
public String getFaultInfo() {
return this.faultInfo;
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.remoting.jaxws;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
/**
* @author Juergen Hoeller
*/
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface OrderService {
String getOrder(int id) throws OrderNotFoundException;
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.remoting.jaxws;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import org.springframework.util.Assert;
/**
* @author Juergen Hoeller
*/
@WebService(serviceName="OrderService", portName="OrderService", endpointInterface = "org.springframework.remoting.jaxws.OrderService")
public class OrderServiceImpl implements OrderService {
@Resource
private WebServiceContext webServiceContext;
public String getOrder(int id) throws OrderNotFoundException {
Assert.notNull(this.webServiceContext, "WebServiceContext has not been injected");
if (id == 0) {
throw new OrderNotFoundException("Order 0 not found");
}
return "order " + id;
}
}