Navigation Menu

Skip to content

Commit

Permalink
remotemock simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
ccaner committed Mar 6, 2012
1 parent 428420d commit af76fe6
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 50 deletions.
@@ -1,6 +1,6 @@
package play.remotemock.mock;

public interface Remotable<T> {
public interface Remotable {

void attachRemote(String rmiUrl);

Expand Down
Expand Up @@ -4,30 +4,26 @@
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.remoting.rmi.RmiServiceExporter;
import play.remotemock.mock.util.RmiRegistry;
import play.remotemock.mock.util.RmiUtil;

import java.rmi.RemoteException;

public class RemotableExportProcessor implements BeanPostProcessor, ApplicationContextAware {
public class RemotableExposeProcessor implements BeanPostProcessor, ApplicationContextAware {

ApplicationContext context;

Integer registryPort = 1299;
RmiRegistry rmiRegistry;

public RemotableExportProcessor(Integer registryPort) {
this.registryPort = registryPort;
public RemotableExposeProcessor(RmiRegistry rmiRegistry) {
this.rmiRegistry = rmiRegistry;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
try {
if (bean instanceof Remotable) {
RmiServiceExporter exporter = new RmiServiceExporter();
exporter.setRegistryPort(registryPort);
exporter.setService(bean);
exporter.setServiceName(beanName + "Remote");
exporter.setServiceInterface(Remotable.class);
exporter.afterPropertiesSet();
rmiRegistry.exportService((Remotable) bean, beanName + "Remote", Remotable.class);
}
} catch (RemoteException e) {
e.printStackTrace();
Expand Down
@@ -1,18 +1,14 @@
package play.remotemock.mock;

import org.springframework.remoting.rmi.RmiProxyFactoryBean;
import play.remotemock.MyService;
import play.remotemock.MyServiceImpl;
import play.remotemock.stub.StubMyServiceImpl;
import play.remotemock.mock.util.RmiUtil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.rmi.server.RemoteObject;

public class RemotableObjectFactory {

public static <T> T createRemotableProxy(final T defaultService, final Class<? super T> serviceInterface) {
public static <T> T createRemotableProxy(final T defaultService, final Class<T> serviceInterface) {

Object obj = Proxy.newProxyInstance(RemotableObjectFactory.class.getClassLoader(),
new Class<?>[]{serviceInterface, Remotable.class}, new InvocationHandler() {
Expand All @@ -35,26 +31,22 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
return (T) obj;
}

private static class RemotableProxy<T> implements Remotable<T> {
private static class RemotableProxy<T> implements Remotable {

T remoteObject;
T defaultObject;
Class<? super T> serviceInterface;
private T remoteObject;
private T defaultObject;
private Class<T> serviceInterface;

boolean remoteModeOn = false;
private boolean remoteModeOn = false;

private RemotableProxy(Class<? super T> serviceInterface, T defaultObject) {
private RemotableProxy(Class<T> serviceInterface, T defaultObject) {
this.serviceInterface = serviceInterface;
this.defaultObject = defaultObject;
}

@Override
public void attachRemote(String url) {
RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean();
rmiProxyFactoryBean.setServiceUrl(url);
rmiProxyFactoryBean.setServiceInterface(serviceInterface);
rmiProxyFactoryBean.afterPropertiesSet();
remoteObject = (T) rmiProxyFactoryBean.getObject();
remoteObject = RmiUtil.clientProxy(url, serviceInterface);
}

public void switchRemoteModeOn() {
Expand Down
14 changes: 10 additions & 4 deletions remotemock/stub-service/src/main/resources/applicationContext.xml
Expand Up @@ -7,15 +7,21 @@
<constructor-arg value="mock."/>
</bean>

<bean class="play.remotemock.mock.RemotableExportProcessor">
<constructor-arg value="1299" />
</bean>

<bean name="stub.myService" class="play.remotemock.stub.StubMyServiceImpl" />

<bean name="mock.myService" class="play.remotemock.mock.RemotableObjectFactory" factory-method="createRemotableProxy">
<constructor-arg ref="stub.myService"/>
<constructor-arg value="play.remotemock.MyService" />
</bean>

<bean name="rmiRegistry" class="play.remotemock.mock.util.RmiRegistry">
<constructor-arg index="0" value="localhost"/>
<constructor-arg index="1" value="1299"/>
</bean>

<bean class="play.remotemock.mock.RemotableExposeProcessor">
<constructor-arg ref="rmiRegistry" />
</bean>


</beans>
11 changes: 5 additions & 6 deletions remotemock/tests/src/test/java/remotemock/it/RemoteTest.java
Expand Up @@ -14,6 +14,7 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import play.remotemock.MyService;
import play.remotemock.mock.Remotable;
import static remotemock.it.util.RemoteTestUtil.*;

import java.io.*;
import java.net.MalformedURLException;
Expand All @@ -25,25 +26,23 @@ public class RemoteTest {

@Autowired
MyService myService;
@Autowired
Remotable<MyService> myServiceRemote;

@Test
public void testStubCall() throws Exception {
String response = getResponse("/doSomething");
Assert.assertEquals("Stub message expected", "Stub do something", response);
Assert.assertEquals("Stub message expected", "Stub: do something", response);
}

@Test
public void testMock() throws Exception {
myServiceRemote.attachRemote("rmi://localhost:1199/MyService");
myServiceRemote.switchRemoteModeOn();
switchRemoteModeOn(myService);

when(myService.returnSomething()).thenReturn("Mocked response");

String response = getResponse("/doSomething");
Assert.assertEquals("Mocked message expected", "Mocked response", response);

myServiceRemote.switchRemoteModeOff();
switchRemoteModeOff(myService);
}

private String getResponse(String path) throws IOException {
Expand Down
26 changes: 15 additions & 11 deletions remotemock/tests/src/test/resources/test-applicationConfig.xml
Expand Up @@ -3,20 +3,24 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean name="mockMyService" class="org.mockito.Mockito" factory-method="mock" >
<constructor-arg value="play.remotemock.MyService" />
<bean name="localRegistry" class="play.remotemock.mock.util.RmiRegistry">
<constructor-arg index="0" value="localhost"/>
<constructor-arg index="1" value="1199"/>
</bean>

<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="MyService"/>
<property name="service" ref="mockMyService"/>
<property name="serviceInterface" value="play.remotemock.MyService"/>
<property name="registryPort" value="1199"/>
<bean name="remoteRegistry" class="play.remotemock.mock.util.RmiRegistry">
<constructor-arg index="0" value="localhost"/>
<constructor-arg index="1" value="1299"/>
</bean>

<bean id="myService.remote" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1299/MyServiceRemote"/>
<property name="serviceInterface" value="play.remotemock.mock.Remotable"/>
<bean name="mockFactory" class="remotemock.it.util.RemotableMockFactory">
<constructor-arg index="0" ref="remoteRegistry"/>
<constructor-arg index="1" ref="localRegistry"/>
</bean>


<bean name="mockMyService" factory-bean="mockFactory" factory-method="mockAndAttach" >
<constructor-arg value="play.remotemock.MyService" />
<constructor-arg value="myService" />
</bean>

</beans>

0 comments on commit af76fe6

Please sign in to comment.