📄 jaxrpcsupporttests.java
字号:
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 ServiceException");
}
catch (ServiceException 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 (RemoteException ex) {
// expected
assertTrue(ex.detail instanceof ServiceException);
}
}
public void testJaxRpcPortProxyFactoryBeanWithPortInterfaceAndRemoteException() 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();
try {
proxy.setName("exception");
fail("Should have thrown RemoteAccessException");
}
catch (RemoteAccessException ex) {
// expected
}
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;
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");
}
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) {
}
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");
}
return service1;
}
public Service loadService(Class ifc) throws ServiceException {
if (!IRemoteBean.class.equals(ifc)) {
throw new ServiceException("not supported");
}
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");
}
return service1;
}
}
public static class CallMockServiceFactory extends MockServiceFactory {
protected static MockControl call1Control;
protected static Call call1;
public CallMockServiceFactory() throws Exception {
super();
}
protected void initMocks() throws Exception {
call1Control = MockControl.createControl(Call.class);
call1 = (Call) call1Control.getMock();
service1.createCall(new QName("myNamespace", "myPort"), "setName");
service1Control.setReturnValue(call1);
initCall();
call1Control.replay();
}
protected void initCall() throws Exception {
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 null;
}
});
call1Control.setReturnValue(null);
}
}
public static class CallWithPropertiesMockServiceFactory extends CallMockServiceFactory {
public CallWithPropertiesMockServiceFactory() throws Exception {
}
protected void initCall() throws Exception {
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();
super.initCall();
}
}
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();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -