package org.jboss.cache.marshall;
import org.jboss.cache.CacheSPI;
import org.jboss.cache.DefaultCacheFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.invocation.RemoteCacheInvocationDelegate;
import org.jboss.cache.misc.TestingUtil;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
/**
* Tests whether remote calls to RPC methods suppress return values (as sometimes expected)
*
* @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
* @since 2.0.0
*/
@Test(groups = {"functional"})
public class RemoteCallerReturnValuesTest
{
private CacheSPI<Object, Object> cache;
private Fqn fqn = Fqn.fromString("/a");
private Object key = "key";
private Object value = "value";
private RemoteCacheInvocationDelegate rcid;
private Method invokeMethod;
@BeforeMethod(alwaysRun = true)
public void setUp() throws Exception
{
cache = (CacheSPI<Object, Object>) new DefaultCacheFactory().createCache();
cache.put(fqn, key, value);
rcid = TestingUtil.getRemoteDelegate(cache);
invokeMethod = cache.getClass().getSuperclass().getDeclaredMethod("invoke", MethodCall.class);
}
@AfterMethod(alwaysRun = true)
public void tearDown()
{
cache.stop();
}
public void testMethodsThatShouldReturnValues() throws Throwable
{
MethodCall mc = MethodCallFactory.create(MethodDeclarations.getKeysMethodLocal_id, fqn);
assert rcid._replicate(mc) == null;
List l = rcid.clusteredGet(mc, false);
assert l != null;
assert (Boolean) l.get(0);
assert l.get(1).equals(Collections.singleton(key));
}
/**
* The purpose of this test is to ensure that these remote method calls - when called using _replicate - will always return null
* since a return value is not neeeded.
*
* @throws Throwable
*/
public void testMethodsThatShouldReturnNull() throws Throwable
{
doNullReturnTest(MethodDeclarations.getDataMapMethodLocal_id, Collections.singletonMap(key, value), fqn);
doNullReturnTest(MethodDeclarations.getNodeMethodLocal_id, cache.getNode(fqn), fqn);
doNullReturnTest(MethodDeclarations.getKeysMethodLocal_id, Collections.singleton(key), fqn);
doNullReturnTest(MethodDeclarations.getChildrenNamesMethodLocal_id, Collections.emptySet(), fqn);
doNullReturnTest(MethodDeclarations.putKeyValMethodLocal_id, value, null, fqn, key, value, true);
doNullReturnTest(MethodDeclarations.removeKeyMethodLocal_id, value, null, fqn, key, true);
}
private void doNullReturnTest(int methodId, Object expectedretValForLocalTest, Object... args) throws Throwable
{
MethodCall mc = MethodCallFactory.create(methodId, args);
// "local" version of the call:
Object retVal = invokeCallDirectly(mc);
assert retVal != null && retVal.equals(expectedretValForLocalTest);
// now try this using the replicate method.
Object retVal2 = rcid._replicate(mc);
assert retVal2 == null;
}
private Object invokeCallDirectly(MethodCall mc) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
{
invokeMethod.setAccessible(true);
return invokeMethod.invoke(cache, mc);
}
}