SGF-67 - In progress

This commit is contained in:
David Turanski
2012-11-26 08:44:46 -05:00
parent f71120138b
commit efdc27666e
127 changed files with 5208 additions and 1583 deletions

View File

@@ -32,8 +32,10 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class ForkUtil {
private static OutputStream os;
private static String TEMP_DIR = System.getProperty("java.io.tmpdir");
public static OutputStream cloneJVM(String argument) {
public static OutputStream cloneJVM(String arguments) {
String cp = System.getProperty("java.class.path");
String home = System.getProperty("java.home");
@@ -41,9 +43,9 @@ public class ForkUtil {
String sp = System.getProperty("file.separator");
String java = home + sp + "bin" + sp + "java";
String argCp = " -cp " + cp;
String argClass = argument;
String cmd = java + argCp + " " + argClass;
String cmd = java + argCp + " " + arguments;
try {
//ProcessBuilder builder = new ProcessBuilder(cmd, argCp, argClass);
//builder.redirectErrorStream(true);
@@ -107,12 +109,15 @@ public class ForkUtil {
return startCacheServer("org.springframework.data.gemfire.fork.CacheServerProcess");
}
private static OutputStream startCacheServer(String className) {
public static OutputStream startCacheServer(String args) {
String className = args.split(" ")[0];
System.out.println("main class:" + className);
if (controlFileExists(className)) {
deleteControlFile(className);
}
OutputStream os = cloneJVM(className);
OutputStream os = cloneJVM(args);
int maxTime = 30000;
int time = 0;
while (!controlFileExists(className) && time < maxTime) {

View File

@@ -18,7 +18,6 @@ package org.springframework.data.gemfire;
import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
/**

View File

@@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.Iterator;
@@ -28,7 +29,6 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.gemfire.TestUtils;
import org.springframework.data.gemfire.client.PoolConnection;
import org.springframework.data.gemfire.client.PoolFactoryBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -41,7 +41,7 @@ import com.gemstone.gemfire.cache.client.PoolManager;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("pool-ns.xml")
public class PoolNamespaceTest {
@Autowired
private ApplicationContext context;
@@ -58,10 +58,10 @@ public class PoolNamespaceTest {
assertEquals(context.getBean("gemfirePool"), PoolManager.find("gemfirePool"));
PoolFactoryBean pfb = (PoolFactoryBean) context.getBean("&gemfirePool");
Collection<PoolConnection> locators = TestUtils.readField("locators", pfb);
Collection<InetSocketAddress> locators = TestUtils.readField("locators", pfb);
assertEquals(1, locators.size());
PoolConnection locator = locators.iterator().next();
assertEquals("localhost", locator.getHost());
InetSocketAddress locator = locators.iterator().next();
assertEquals("localhost", locator.getHostName());
assertEquals(40403, locator.getPort());
}
@@ -75,15 +75,15 @@ public class PoolNamespaceTest {
assertFalse((Boolean) TestUtils.readField("multiUserAuthentication", pfb));
assertTrue((Boolean) TestUtils.readField("prSingleHopEnabled", pfb));
Collection<PoolConnection> servers = TestUtils.readField("servers", pfb);
Collection<InetSocketAddress> servers = TestUtils.readField("servers", pfb);
assertEquals(2, servers.size());
Iterator<PoolConnection> iterator = servers.iterator();
PoolConnection server = iterator.next();
assertEquals("localhost", server.getHost());
Iterator<InetSocketAddress> iterator = servers.iterator();
InetSocketAddress server = iterator.next();
assertEquals("localhost", server.getHostName());
assertEquals(40404, server.getPort());
server = iterator.next();
assertEquals("localhost", server.getHost());
assertEquals("localhost", server.getHostName());
assertEquals(40405, server.getPort());
}
}

View File

@@ -28,6 +28,7 @@ import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionFactory;
import com.gemstone.gemfire.cache.Scope;
import com.gemstone.gemfire.cache.server.CacheServer;
import com.gemstone.gemfire.distributed.DistributedSystem;
@@ -43,21 +44,19 @@ public class CacheServerProcess {
props.setProperty("name", "CqServer");
props.setProperty("log-level", "warning");
System.out.println("\nConnecting to the distributed system and creating the cache.");
DistributedSystem ds = DistributedSystem.connect(props);
Cache cache = CacheFactory.create(ds);
Cache cache = new CacheFactory(props).create();
// Create region.
AttributesFactory factory = new AttributesFactory();
// Create region.
RegionFactory<Object,Object> factory = cache.createRegionFactory();
factory.setDataPolicy(DataPolicy.REPLICATE);
factory.setScope(Scope.DISTRIBUTED_ACK);
Region testRegion = cache.createRegion("test-cq", factory.create());
Region testRegion = factory.create("test-cq");
System.out.println("Test region, " + testRegion.getFullPath() + ", created in cache.");
// Start Cache Server.
CacheServer server = cache.addCacheServer();
server.setPort(40404);
server.setNotifyBySubscription(true);
server.start();
ForkUtil.createControlFile(CacheServerProcess.class.getName());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2012 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.
@@ -20,48 +20,45 @@ import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import org.springframework.data.gemfire.ForkUtil;
import org.springframework.data.gemfire.function.MethodInvokingFunction;
import com.gemstone.gemfire.cache.AttributesFactory;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionFactory;
import com.gemstone.gemfire.cache.Scope;
import com.gemstone.gemfire.cache.execute.FunctionAdapter;
import com.gemstone.gemfire.cache.execute.FunctionContext;
import com.gemstone.gemfire.cache.execute.FunctionService;
import com.gemstone.gemfire.cache.server.CacheServer;
import com.gemstone.gemfire.distributed.DistributedSystem;
/**
* @author Costin Leau
* @author David Turanski
*/
public class FunctionCacheServerProcess {
static Region testRegion;
static Region<Object,Object> testRegion;
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.setProperty("name", "CqServer");
props.setProperty("name", "FunctionServer");
props.setProperty("log-level", "config");
System.out.println("\nConnecting to the distributed system and creating the cache.");
DistributedSystem ds = DistributedSystem.connect(props);
Cache cache = CacheFactory.create(ds);
props.setProperty("groups","g1,g2,g3");
Cache cache = new CacheFactory(props).create();
// Create region.
AttributesFactory factory = new AttributesFactory();
RegionFactory<Object,Object> factory = cache.createRegionFactory();
factory.setDataPolicy(DataPolicy.REPLICATE);
factory.setScope(Scope.DISTRIBUTED_ACK);
testRegion = cache.createRegion("test-function", factory.create());
testRegion = factory.create("test-function");
System.out.println("Test region, " + testRegion.getFullPath() + ", created in cache.");
// Start Cache Server.
CacheServer server = cache.addCacheServer();
server.setPort(40404);
server.setNotifyBySubscription(true);
server.start();
System.out.println("Server started");
@@ -73,9 +70,10 @@ public class FunctionCacheServerProcess {
System.out.println("Registering ServerFunction");
FunctionService.registerFunction(new ServerFunction());
System.out.println("Registered ServerFunction");
FunctionService.registerFunction(new MethodInvokingFunction());
System.out.println("Registered MethodInvokingFunction");
System.out.println("Registering EchoFunction");
FunctionService.registerFunction(new EchoFunction());
System.out.println("Registered EchoFunction");
ForkUtil.createControlFile(FunctionCacheServerProcess.class.getName());
@@ -106,5 +104,30 @@ public class FunctionCacheServerProcess {
}
static class EchoFunction extends FunctionAdapter {
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.FunctionAdapter#execute(com.gemstone.gemfire.cache.execute.FunctionContext)
*/
@Override
public void execute(FunctionContext functionContext) {
Object[] args = (Object[])functionContext.getArguments();
for (int i=0; i< args.length; i++){
if (i == args.length-1){
functionContext.getResultSender().lastResult(args[i]);
} else {
functionContext.getResultSender().sendResult(args[i]);
}
}
}
@Override
public String getId() {
return "echoFunction";
}
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2002-2012 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.data.gemfire.fork;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.gemfire.ForkUtil;
/**
* @author David Turanski
*
*/
public class SpringCacheServerProcess {
public static void main(String[] args) {
try {
new ClassPathXmlApplicationContext(args[0]);
ForkUtil.createControlFile(SpringCacheServerProcess.class.getName());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Waiting for shutdown");
bufferedReader.readLine();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}

View File

@@ -0,0 +1,287 @@
/*
* Copyright 2002-2012 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.data.gemfire.function;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import org.springframework.data.gemfire.function.config.Filter;
import org.springframework.data.gemfire.function.config.RegionData;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.execute.FunctionContext;
import com.gemstone.gemfire.cache.execute.RegionFunctionContext;
/**
* @author David Turanski
*
*/
public class FunctionArgumentResolverTest {
@Test
public void testDefaultFunctionArgumentResolverSingleArg() {
FunctionArgumentResolver far = new DefaultFunctionArgumentResolver();
FunctionContext functionContext = mock(FunctionContext.class);
when(functionContext.getArguments()).thenReturn("hello");
Object[] args = far.resolveFunctionArguments(functionContext);
assertEquals(1,args.length);
assertEquals("hello", args[0]);
}
@Test
public void testDefaultFunctionArgumentResolverSingleArgAsArray() {
FunctionArgumentResolver far = new DefaultFunctionArgumentResolver();
FunctionContext functionContext = mock(FunctionContext.class);
when(functionContext.getArguments()).thenReturn(new String[]{"hello"});
Object[] args = far.resolveFunctionArguments(functionContext);
assertEquals(1,args.length);
assertEquals("hello", args[0]);
}
@Test
public void testMethodWithNoSpecialArgs() throws SecurityException, NoSuchMethodException {
RegionFunctionContext functionContext = mock(RegionFunctionContext.class);
Method method = TestFunction.class.getDeclaredMethod("methodWithNoSpecialArgs", String.class,int.class,boolean.class);
FunctionArgumentResolver far = new FunctionContextInjectingArgumentResolver(method);
Object[] originalArgs = new Object[]{"hello",0,false};
when(functionContext.getArguments()).thenReturn(originalArgs);
Object[] args = far.resolveFunctionArguments(functionContext);
assertEquals(originalArgs.length, args.length);
int i = 0;
for (Object arg: args) {
assertSame(originalArgs[i++], arg);
}
}
@Test
public void testMethodWithRegionType() throws SecurityException, NoSuchMethodException {
RegionFunctionContext functionContext = mock(RegionFunctionContext.class);
@SuppressWarnings("unchecked")
Region<Object,Object> region = mock(Region.class);
Method method = TestFunction.class.getDeclaredMethod("methodWithRegionType", String.class,Region.class);
FunctionArgumentResolver far = new FunctionContextInjectingArgumentResolver(method);
Object[] originalArgs = new Object[]{"hello"};
when(functionContext.getArguments()).thenReturn(originalArgs);
when(functionContext.getDataSet()).thenReturn(region);
Object[] args = far.resolveFunctionArguments(functionContext);
assertEquals(originalArgs.length + 1, args.length);
int i = 0;
for (Object arg: args) {
if (i != 1) {
assertSame(originalArgs[i++], arg);
} else {
assertSame(region,arg);
}
}
}
@Test
public void testMethodWithOneArgRegionType() throws SecurityException, NoSuchMethodException {
RegionFunctionContext functionContext = mock(RegionFunctionContext.class);
@SuppressWarnings("unchecked")
Region<Object,Object> region = mock(Region.class);
Method method = TestFunction.class.getDeclaredMethod("methodWithOneArgRegionType", Region.class);
FunctionArgumentResolver far = new FunctionContextInjectingArgumentResolver(method);
Object[] originalArgs = new Object[]{};
when(functionContext.getArguments()).thenReturn(originalArgs);
when(functionContext.getDataSet()).thenReturn(region);
Object[] args = far.resolveFunctionArguments(functionContext);
assertEquals(1, args.length);
assertSame(region,args[0]);
}
@Test
public void testMethodWithAnnotatedRegion() throws SecurityException, NoSuchMethodException {
RegionFunctionContext functionContext = mock(RegionFunctionContext.class);
@SuppressWarnings("unchecked")
Region<Object,Object> region = mock(Region.class);
Method method = TestFunction.class.getDeclaredMethod("methodWithAnnotatedRegion", Map.class, String.class);
FunctionArgumentResolver far = new FunctionContextInjectingArgumentResolver(method);
Object[] originalArgs = new Object[]{"hello"};
when(functionContext.getArguments()).thenReturn(originalArgs);
when(functionContext.getDataSet()).thenReturn(region);
Object[] args = far.resolveFunctionArguments(functionContext);
assertEquals(2, args.length);
assertSame(region,args[0]);
assertSame(originalArgs[0],args[1]);
}
@Test
public void testMethodWithFunctionContext() throws SecurityException, NoSuchMethodException {
RegionFunctionContext functionContext = mock(RegionFunctionContext.class);
@SuppressWarnings("unchecked")
Region<Object,Object> region = mock(Region.class);
Method method = TestFunction.class.getDeclaredMethod("methodWithFunctionContext", Map.class, String.class, FunctionContext.class);
FunctionArgumentResolver far = new FunctionContextInjectingArgumentResolver(method);
Object[] originalArgs = new Object[]{"hello"};
when(functionContext.getArguments()).thenReturn(originalArgs);
when(functionContext.getDataSet()).thenReturn(region);
Object[] args = far.resolveFunctionArguments(functionContext);
assertEquals(3, args.length);
assertSame(region,args[0]);
assertSame(originalArgs[0],args[1]);
assertSame(functionContext,args[2]);
}
@SuppressWarnings("unchecked")
@Test
public void testMethodWithFilterAndRegion() throws SecurityException, NoSuchMethodException {
RegionFunctionContext functionContext = mock(RegionFunctionContext.class);
Region<Object,Object> region = mock(Region.class);
Method method = TestFunction.class.getDeclaredMethod("methodWithFilterAndRegion", Map.class, Set.class, Object.class);
FunctionArgumentResolver far = new FunctionContextInjectingArgumentResolver(method);
Object[] originalArgs = new Object[]{new Object()};
when(functionContext.getArguments()).thenReturn(originalArgs);
when(functionContext.getDataSet()).thenReturn(region);
@SuppressWarnings("rawtypes")
Set keys = new HashSet<String>();
when(functionContext.getFilter()).thenReturn(keys);
Object[] args = far.resolveFunctionArguments(functionContext);
assertEquals(3, args.length);
assertSame(region,args[0]);
assertSame(originalArgs[0],args[2]);
assertSame(keys,args[1]);
}
@Test
public void testMethodWithMultipleRegionData() throws SecurityException, NoSuchMethodException {
Method method = TestFunction.class.getDeclaredMethod("methodWithMultipleRegionData", Map.class, Map.class);
try {
new FunctionContextInjectingArgumentResolver(method);
fail("Should throw exception");
} catch (Exception e) {
}
}
@Test
public void testMethodWithMultipleRegions() throws SecurityException, NoSuchMethodException {
Method method = TestFunction.class.getDeclaredMethod("methodWithMultipleRegions", Region.class, Map.class);
try {
new FunctionContextInjectingArgumentResolver(method);
fail("Should throw exception");
} catch (Exception e) {
}
}
@Test
public void testMethodWithInvalidTypeForAnnotation() throws SecurityException, NoSuchMethodException {
Method method = TestFunction.class.getDeclaredMethod("methodWithInvalidTypeForAnnotation", Region.class);
try {
new FunctionContextInjectingArgumentResolver(method);
fail("Should throw exception");
} catch (Exception e) {
}
}
@Test
public void testMethodWithMultipleFunctionContext() throws SecurityException, NoSuchMethodException {
Method method = TestFunction.class.getDeclaredMethod("methodWithMultipleFunctionContext", FunctionContext.class, FunctionContext.class);
try {
new FunctionContextInjectingArgumentResolver(method);
fail("Should throw exception");
} catch (Exception e) {
}
}
static class TestFunction {
public void methodWithNoSpecialArgs(String s1, int i1, boolean b1) {}
public void methodWithRegionType(String s1, Region<?,?> region){}
public void methodWithOneArgRegionType(Region<?,?> region){}
public void methodWithAnnotatedRegion(@RegionData Map<?,?> data, String s1){}
public void methodWithFunctionContext(@RegionData Map<?,?> data, String s1, FunctionContext fc){}
public void methodWithFilterAndRegion(@RegionData Map<String,Object> region, @Filter Set<String> keys, Object arg){}
//Invalid Method Signatures
public void methodWithMultipleRegionData(@RegionData Map<?,?> r1, @RegionData Map<?,?> r2){}
public void methodWithMultipleRegions(Region<?,?> r1, @RegionData Map<?,?> r2){}
public void methodWithInvalidTypeForAnnotation(@Filter Region<?,?> r1){}
public void methodWithMultipleFunctionContext(FunctionContext fc1, FunctionContext fc2){}
}
}

View File

@@ -1,157 +0,0 @@
/*
* Copyright 2002-2011 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.data.gemfire.function;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.data.gemfire.ForkUtil;
import org.springframework.data.gemfire.fork.FunctionCacheServerProcess;
import org.springframework.data.gemfire.function.foo.Foo;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
import com.gemstone.gemfire.cache.client.ClientRegionFactory;
import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
import com.gemstone.gemfire.cache.client.Pool;
import com.gemstone.gemfire.cache.client.PoolFactory;
import com.gemstone.gemfire.cache.client.PoolManager;
/**
* @author David Turanski
*
*/
public class FunctionExecutionTests {
private static ClientCache cache = null;
private static Pool pool = null;
private static Region<String, Integer> clientRegion = null;
@BeforeClass
public static void startUp() throws Exception {
ForkUtil.cacheServer(FunctionCacheServerProcess.class);
Properties props = new Properties();
props.put("mcast-port", "0");
props.put("name", "function-client");
props.put("log-level", "warning");
ClientCacheFactory ccf = new ClientCacheFactory(props);
ccf.setPoolSubscriptionEnabled(true);
cache = ccf.create();
PoolFactory pf = PoolManager.createFactory();
pf.addServer("localhost", 40404);
pf.setSubscriptionEnabled(true);
pool = pf.create("client");
ClientRegionFactory<String, Integer> crf = cache.createClientRegionFactory(ClientRegionShortcut.PROXY);
crf.setPoolName("client");
clientRegion = crf.create("test-function");
}
@AfterClass
public static void cleanUp() {
ForkUtil.sendSignal();
if (clientRegion != null) {
clientRegion.destroyRegion();
}
if (pool != null) {
pool.destroy();
pool = null;
}
if (cache != null) {
cache.close();
}
cache = null;
}
@Test
public void testRegionExecution() {
RemoteMethodInvocation invocation = new RemoteMethodInvocation(Foo.class, "oneArg", "one");
RegionFunctionExecution<Integer> execution = new RegionFunctionExecution<Integer>(clientRegion,
new MethodInvokingFunction(), invocation);
int result = execution.executeAndExtract();
assertEquals(1, result);
}
@Test
public void testRegionExecutionWithRegisteredFunction() {
RemoteMethodInvocation invocation = new RemoteMethodInvocation(Foo.class, "oneArg", "one");
RegionFunctionExecution<Integer> execution = new RegionFunctionExecution<Integer>(clientRegion,
new MethodInvokingFunction().getId(), invocation);
int result = execution.executeAndExtract();
assertEquals(1, result);
}
// TODO: Filter only works on partitioned region. No effect here, but server
// won't start with a partitioned region. Probably because no locator
@Test
public void testRegionExecutionWithFilter() {
RemoteMethodInvocation invocation = new RemoteMethodInvocation(Foo.class, "oneArg", "one");
Set<String> keys = new HashSet<String>();
keys.add("two");
RegionFunctionExecution<Integer> execution = new RegionFunctionExecution<Integer>(clientRegion,
new MethodInvokingFunction().getId(), invocation);
execution.setKeys(keys);
Integer result = execution.executeAndExtract();
// assertEquals(null,result.get(0));
assertEquals(1, result.intValue());
}
@Test
public void testRegionExecutionForMap() {
RemoteMethodInvocation invocation = new RemoteMethodInvocation(Foo.class, "getMapWithNoArgs");
RegionFunctionExecution<Map<String, Integer>> execution = new RegionFunctionExecution<Map<String, Integer>>(
clientRegion, new MethodInvokingFunction().getId(), invocation);
execution.execute();
Map<String, Integer> result = execution.executeAndExtract();
assertTrue(result.containsKey("one"));
assertEquals(1, result.get("one").intValue());
}
@Test
public void testServersExecutionWithRegisteredFunction() {
assertNull(clientRegion.get("four"));
ServersFunctionExecution<?> execution = new ServersFunctionExecution<Object>(cache, "serverFunction", "four", new Integer(
4));
execution.execute();
assertNotNull(clientRegion.get("four"));
}
@Test
public void testServerExecutionWithRegisteredFunction() {
assertNull(clientRegion.get("five"));
ServerFunctionExecution<?> execution = new ServerFunctionExecution<Object>(cache, "serverFunction", "five", new Integer(5));
Object result = execution.executeAndExtract();
assertNull(result);
assertNotNull(clientRegion.get("five"));
}
}

View File

@@ -1,135 +0,0 @@
/*
* Copyright 2002-2011 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.data.gemfire.function;
/**
* @author David Turanski
*
*/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.gemfire.function.foo.Foo;
import org.springframework.data.gemfire.function.foo.IFoo;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gemstone.gemfire.cache.Region;
/**
*
* @author David Turanski
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class GemfireFunctionProxyFactoryBeanTest {
private GemfireFunctionOperations<?> functionOperations;
static Log logger = LogFactory.getLog(GemfireFunctionProxyFactoryBeanTest.class);
@Autowired
private IFoo foo;
@Autowired
private ApplicationContext context;
private Region<String,Integer> region;
@SuppressWarnings("unchecked")
@Before
public void setUp() {
assertNotNull(foo);
functionOperations = mock(GemfireFunctionOperations.class);
region = context.getBean("someRegion",Region.class);
assertNotNull(region);
region.put("one",1);
region.put("two",2);
region.put("three",3);
}
@Test
public void testInstance() throws Exception {
GemfireFunctionOperations<?> functionOperations = mock(GemfireFunctionOperations.class);
GemfireFunctionProxyFactoryBean proxy = new GemfireFunctionProxyFactoryBean(IFoo.class,Foo.class.getName(),functionOperations);
IFoo foo = (IFoo)proxy.getObject();
assertTrue(foo instanceof FilterAware);
}
@Test
public void testSetFilter() throws Exception {
GemfireFunctionProxyFactoryBean proxy = new GemfireFunctionProxyFactoryBean(IFoo.class,Foo.class.getName(),functionOperations);
IFoo foo = (IFoo)proxy.getObject();
Set<String> filter = Collections.singleton("foo");
Object obj = ((FilterAware) foo).setFilter(filter);
assertSame(obj,foo);
assertSame(filter,proxy.getFilter());
}
@Test
public void testRemoteExecutionOneArg() {
assertEquals(1,foo.oneArg("one").intValue());
((FilterAware)foo).setFilter(Collections.singleton("one"));
assertEquals(1,foo.oneArg("one").intValue());
}
@Test
public void testRemoteExecutionTwoArg() {
assertEquals(3,foo.twoArg("one","two").intValue());
}
@Test
public void testRemoteExectionArrayList() {
ArrayList<Integer> ints = new ArrayList<Integer>(Arrays.asList(new Integer[]{1,2,3}));
assertEquals(1,foo.collections(ints).get(0).intValue());
}
@Test
public void testRemoteExectionMap() {
Map<String,Integer> result = foo.getMapWithNoArgs();
assertEquals(1,result.get("one").intValue());
}
@After
public void tearDown() {
}
}

View File

@@ -1,194 +0,0 @@
/*
* Copyright 2002-2011 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.data.gemfire.function;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.data.gemfire.ForkUtil;
import org.springframework.data.gemfire.GemfireCallback;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.function.foo.Foo;
import com.gemstone.gemfire.GemFireCheckedException;
import com.gemstone.gemfire.GemFireException;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
import com.gemstone.gemfire.cache.client.ClientRegionFactory;
import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
import com.gemstone.gemfire.cache.client.Pool;
import com.gemstone.gemfire.cache.client.PoolFactory;
import com.gemstone.gemfire.cache.client.PoolManager;
import com.gemstone.gemfire.cache.execute.Execution;
import com.gemstone.gemfire.cache.execute.FunctionService;
import com.gemstone.gemfire.cache.execute.ResultCollector;
/**
* @author David Turanski
*
*/
public class MethodInvokingFunctionTests {
private static ClientCache cache = null;
private static MethodInvokingFunction methodInvokingFunction = new MethodInvokingFunction();
private static Pool pool = null;
private static Region<String,Integer> clientRegion = null;
private static GemfireTemplate gemfireTemplate;
@BeforeClass
public static void startUp() throws Exception {
ForkUtil.cacheServer();
Properties props = new Properties();
props.put("mcast-port", "0");
props.put("name", "cq-client");
props.put("log-level", "warning");
ClientCacheFactory ccf = new ClientCacheFactory(props);
ccf.setPoolSubscriptionEnabled(true);
cache = ccf.create();
PoolFactory pf = PoolManager.createFactory();
pf.addServer("localhost", 40404);
pf.setSubscriptionEnabled(true);
pool = pf.create("client");
ClientRegionFactory<String, Integer> crf = cache.createClientRegionFactory(ClientRegionShortcut.LOCAL);
crf.setPoolName("client");
clientRegion = crf.create("test-cq");
gemfireTemplate = new GemfireTemplate(clientRegion);
ForkUtil.sendSignal();
Thread.sleep(500);
}
@AfterClass
public static void cleanUp() {
ForkUtil.sendSignal();
if (clientRegion != null) {
clientRegion.destroyRegion();
}
if (pool != null) {
pool.destroy();
pool = null;
}
if (cache != null) {
cache.close();
}
cache = null;
}
@Test
public void testInvokeRemoteFunctionOneArg() throws Exception {
gemfireTemplate.setExposeNativeRegion(true);
Integer val = gemfireTemplate.execute(new GemfireCallback<Integer>() {
public Integer doInGemfire(@SuppressWarnings("rawtypes") Region region) throws GemFireCheckedException, GemFireException {
RemoteMethodInvocation invocation = new RemoteMethodInvocation(Foo.class,"oneArg","one");
Execution execution = FunctionService.onRegion(region);
ResultCollector<?, ?> resultsCollector = execution.withArgs(invocation).execute(methodInvokingFunction);
ArrayList<?> result = (ArrayList<?>)resultsCollector.getResult();
return (Integer)result.get(0);
}
});
assertEquals(1,val.intValue());
}
@Test
public void testInvokeRemoteFunctionTwoArgs() throws Exception {
gemfireTemplate.setExposeNativeRegion(true);
Integer val = gemfireTemplate.execute(new GemfireCallback<Integer>() {
public Integer doInGemfire(@SuppressWarnings("rawtypes") Region region) throws GemFireCheckedException, GemFireException {
RemoteMethodInvocation invocation = new RemoteMethodInvocation(Foo.class,"twoArg","one","three");
Execution execution = FunctionService.onRegion(region);
ResultCollector<?, ?> resultsCollector = execution.withArgs(invocation).execute(methodInvokingFunction);
ArrayList<?> result = (ArrayList<?>)resultsCollector.getResult();
return (Integer)result.get(0);
}
});
assertEquals(4,val.intValue());
}
@Test
public void testInvokeRemoteFunctionCollections() throws Exception {
gemfireTemplate.setExposeNativeRegion(true);
List<Integer> val = gemfireTemplate.execute(new GemfireCallback<List<Integer>>() {
@SuppressWarnings("unchecked")
public List<Integer> doInGemfire(@SuppressWarnings("rawtypes") Region region) throws GemFireCheckedException, GemFireException {
ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(new Integer[]{1,2,3,4,5}));
RemoteMethodInvocation invocation = new RemoteMethodInvocation(Foo.class,"collections",list);
Execution execution = FunctionService.onRegion(region);
ResultCollector<?, ?> resultsCollector = execution.withArgs(invocation).execute(methodInvokingFunction);
ArrayList<?> result = (ArrayList<?>)resultsCollector.getResult();
//If result type is a list, Gemfire merges it into the results.
return (List<Integer>)result;
}
});
assertEquals(5,val.size());
for (int i=0; i<5; i++) {
assertEquals(i+1,val.get(i).intValue());
}
}
@Test
public void testInvokeRemoteFunctionMap() throws Exception {
gemfireTemplate.setExposeNativeRegion(true);
Map<String, Integer> val = gemfireTemplate.execute(new GemfireCallback<Map<String, Integer>>() {
@SuppressWarnings("unchecked")
public Map<String, Integer> doInGemfire(@SuppressWarnings("rawtypes") Region region) throws GemFireCheckedException, GemFireException {
RemoteMethodInvocation invocation = new RemoteMethodInvocation(Foo.class,"getMapWithNoArgs");
Execution execution = FunctionService.onRegion(region);
ResultCollector<?, ?> resultsCollector = execution.withArgs(invocation).execute(methodInvokingFunction);
ArrayList<?> result = (ArrayList<?>)resultsCollector.getResult();
return (Map<String, Integer>)result.get(0);
}
});
assertEquals(3,val.size());
for (int i=0; i<3; i++) {
assertTrue(val.values().contains(i+1));
}
}
}

View File

@@ -17,6 +17,7 @@ import static org.junit.Assert.assertTrue;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -60,6 +61,13 @@ public class AnnotationDrivenFunctionsTest {
assertTrue(function.isHA());
assertTrue(function.optimizeForWrite());
assertTrue(function.hasResult());
assertTrue(FunctionService.isRegistered("injectFilter"));
function = FunctionService.getFunction("injectFilter");
assertTrue(function.isHA());
assertTrue(function.optimizeForWrite());
assertTrue(function.hasResult());
}
@Component
@@ -81,8 +89,8 @@ public class AnnotationDrivenFunctionsTest {
return null;
}
@GemfireFunction(id="injectMultipleRegions", HA=true,optimizeForWrite=true)
public List<String> injectMultipleRegions (@RegionData("someRegion") Map<?,?> someRegion, @RegionData("someOtherRegion") Map<?,?> someOtherRegion) {
@GemfireFunction(id="injectFilter", HA=true,optimizeForWrite=true)
public List<String> injectFilter (@Filter Set<?> keySet) {
return null;
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2002-2012 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.data.gemfire.function.config;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.gemfire.function.config.EnableGemfireFunctionExecutions;
import org.springframework.data.gemfire.function.execution.GemfireFunctionProxyFactoryBean;
import org.springframework.data.gemfire.function.execution.OnRegionFunctionProxyFactoryBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.Pool;
/**
* @author David Turanski
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={TestClientCacheConfig.class})
public class FunctionExecutionClientCacheTests {
@Autowired
ApplicationContext context;
@Test
public void testContextCreated() throws Exception {
//String name ="testClientOnRegionFunction";
String name ="testClientOnServerFunction";
// GemfireFunctionProxyFactoryBean factoryBean = (GemfireFunctionProxyFactoryBean)context.getBean("&"+name);
ClientCache cache = context.getBean("gemfireCache",ClientCache.class);
Pool pool = context.getBean("gemfirePool",Pool.class);
assertEquals("gemfirePool", pool.getName());
assertEquals(1, cache.getDefaultPool().getServers().size());
assertEquals(pool.getServers().get(0), cache.getDefaultPool().getServers().get(0));
context.getBean("r1",Region.class);
//ComponentScan s;
//FilterType f;
}
}
@ImportResource("/org/springframework/data/gemfire/function/config/FunctionExecutionCacheClientTests-context.xml")
@EnableGemfireFunctionExecutions (basePackages = "org.springframework.data.gemfire.function.config.three",
excludeFilters = {
@ComponentScan.Filter(type=FilterType.ANNOTATION, value=OnRegion.class)/*,
@ComponentScan.Filter(type=FilterType.ANNOTATION, value=OnServer.class)*/
}
)
@Configuration
class TestClientCacheConfig {
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2002-2012 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.data.gemfire.function.config;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ScannedGenericBeanDefinition;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.data.gemfire.function.config.one.TestFunctionExecution;
/**
* @author David Turanski
*
*/
public class FunctionExecutionComponentProviderTest {
@Test
public void testDiscovery() throws ClassNotFoundException {
List<TypeFilter> includeFilters = new ArrayList<TypeFilter>();
FunctionExecutionComponentProvider provider = new FunctionExecutionComponentProvider(includeFilters,AnnotationFunctionExecutionConfigurationSource.getFunctionExecutionAnnotationTypes());
Set<BeanDefinition> candidates = provider.findCandidateComponents(this.getClass().getPackage().getName()+".one");
ScannedGenericBeanDefinition bd = null;
for (BeanDefinition candidate: candidates) {
if (candidate.getBeanClassName().equals(TestFunctionExecution.class.getName())) {
bd = (ScannedGenericBeanDefinition)candidate;
}
}
assertNotNull(bd);
}
@Test
public void testExcludeFilter() throws ClassNotFoundException {
List<TypeFilter> includeFilters = new ArrayList<TypeFilter>();
FunctionExecutionComponentProvider provider = new FunctionExecutionComponentProvider(includeFilters,AnnotationFunctionExecutionConfigurationSource.getFunctionExecutionAnnotationTypes());
provider.addExcludeFilter(new AssignableTypeFilter(TestFunctionExecution.class));
Set<BeanDefinition> candidates = provider.findCandidateComponents(this.getClass().getPackage().getName()+".one");
for (BeanDefinition candidate: candidates) {
if (candidate.getBeanClassName().equals(TestFunctionExecution.class.getName())) {
fail(TestFunctionExecution.class.getName() + " not excluded");
}
}
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2002-2012 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.data.gemfire.function.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.gemfire.TestUtils;
import org.springframework.data.gemfire.function.config.EnableGemfireFunctionExecutions;
import org.springframework.data.gemfire.function.config.two.TestOnRegionFunction;
import org.springframework.data.gemfire.function.execution.GemfireOnRegionFunctionTemplate;
import org.springframework.data.gemfire.function.execution.OnRegionFunctionProxyFactoryBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.Pool;
/**
* @author David Turanski
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={TestConfig.class})
public class FunctionExecutionIntegrationTests {
@Autowired
ApplicationContext context;
@Test
public void testProxyFactoryBeanCreated() throws Exception {
OnRegionFunctionProxyFactoryBean factoryBean = (OnRegionFunctionProxyFactoryBean)context.getBean("&testFunction");
Class<?> serviceInterface = TestUtils.readField("serviceInterface",factoryBean);
assertEquals(serviceInterface, TestOnRegionFunction.class);
Region<?,?> r1 = context.getBean("r1",Region.class);
GemfireOnRegionFunctionTemplate template = TestUtils.readField("gemfireFunctionOperations",factoryBean);
assertSame(r1, TestUtils.readField("region",template));
}
}
@ImportResource("/org/springframework/data/gemfire/function/config/FunctionExecutionIntegrationTests-context.xml")
@EnableGemfireFunctionExecutions(basePackages = "org.springframework.data.gemfire.function.config.two")
@Configuration
class TestConfig {
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2002-2012 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.data.gemfire.function.config.one;
import java.util.Set;
import org.springframework.data.gemfire.function.config.Filter;
import org.springframework.data.gemfire.function.config.FunctionId;
import org.springframework.data.gemfire.function.config.OnMember;
@OnMember
public interface TestFunctionExecution {
@FunctionId("f1")
public String getString(Object arg1, @Filter Set<Object> keys) ;
@FunctionId("f2")
public String getString(Object arg1) ;
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2002-2012 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.data.gemfire.function.config.three;
import java.util.Set;
import org.springframework.data.gemfire.function.config.Filter;
import org.springframework.data.gemfire.function.config.FunctionId;
import org.springframework.data.gemfire.function.config.OnRegion;
/**
* @author David Turanski
*
*/
@OnRegion(id="testClientOnRegionFunction", region="r1")
public interface TestClientOnRegionFunction {
@FunctionId("f1")
public String getString(Object arg1, @Filter Set<Object> keys) ;
@FunctionId("f2")
public String getString(Object arg1) ;
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2002-2012 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.data.gemfire.function.config.three;
import java.util.Set;
import org.springframework.data.gemfire.config.GemfireConstants;
import org.springframework.data.gemfire.function.config.Filter;
import org.springframework.data.gemfire.function.config.FunctionId;
import org.springframework.data.gemfire.function.config.OnServer;
/**
* @author David Turanski
*
*/
@OnServer(id="testClientOnServerFunction",pool="gemfirePool")
public interface TestClientOnServerFunction {
@FunctionId("f1")
public String getString(Object arg1, @Filter Set<Object> keys) ;
@FunctionId("f2")
public String getString(Object arg1) ;
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2002-2012 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.data.gemfire.function.config.two;
import java.util.Set;
import org.springframework.data.gemfire.function.config.Filter;
import org.springframework.data.gemfire.function.config.FunctionId;
import org.springframework.data.gemfire.function.config.OnRegion;
import org.springframework.data.gemfire.function.config.OnServer;
/**
* @author David Turanski
*
*/
@OnRegion(id="testFunction", region="r1")
public interface TestOnRegionFunction {
@FunctionId("f1")
public String getString(Object arg1, @Filter Set<Object> keys) ;
@FunctionId("f2")
public String getString(Object arg1) ;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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
@@ -10,23 +10,23 @@
* 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.data.gemfire.function.foo;
package org.springframework.data.gemfire.function.config.two;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.data.gemfire.function.config.Filter;
import org.springframework.data.gemfire.function.config.FunctionId;
import org.springframework.data.gemfire.function.config.OnServer;
/**
* @author David Turanski
*
*/
public interface IFoo {
public abstract Integer oneArg(String key);
public abstract Integer twoArg(String akey, String bkey);
public abstract List<Integer> collections(List<Integer> args);
public abstract Map<String, Integer> getMapWithNoArgs();
}
@OnServer(id="testFunction2")
public interface TestOnRegionFunction2 {
@FunctionId("f1")
public String getString(Object arg1, @Filter Set<Object> keys) ;
@FunctionId("f2")
public String getString(Object arg1) ;
}

View File

@@ -0,0 +1,110 @@
/*
* Copyright 2002-2011 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.data.gemfire.function.execution;
import static org.junit.Assert.assertEquals;
import java.util.Iterator;
import java.util.Properties;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.data.gemfire.ForkUtil;
import org.springframework.data.gemfire.fork.FunctionCacheServerProcess;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
import com.gemstone.gemfire.cache.client.ClientRegionFactory;
import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
import com.gemstone.gemfire.cache.client.Pool;
import com.gemstone.gemfire.cache.client.PoolFactory;
import com.gemstone.gemfire.cache.client.PoolManager;
/**
* @author David Turanski
*
*/
public class FunctionExecutionTests {
private static ClientCache cache = null;
private static Pool pool = null;
private static Region<String, Integer> clientRegion = null;
@BeforeClass
public static void startUp() throws Exception {
// Registers function "echoFunction"
ForkUtil.cacheServer(FunctionCacheServerProcess.class);
Properties props = new Properties();
props.put("mcast-port", "0");
props.put("name", "function-client");
props.put("log-level", "warning");
ClientCacheFactory ccf = new ClientCacheFactory(props);
ccf.setPoolSubscriptionEnabled(true);
cache = ccf.create();
PoolFactory pf = PoolManager.createFactory();
pf.addServer("localhost", 40404);
pf.setSubscriptionEnabled(true);
pool = pf.create("client");
ClientRegionFactory<String, Integer> crf = cache.createClientRegionFactory(ClientRegionShortcut.PROXY);
crf.setPoolName("client");
clientRegion = crf.create("test-function");
}
@AfterClass
public static void cleanUp() {
ForkUtil.sendSignal();
if (clientRegion != null) {
clientRegion.destroyRegion();
}
if (pool != null) {
pool.destroy();
pool = null;
}
if (cache != null) {
cache.close();
}
cache = null;
}
@Test
public void testBasicFunctionExecutions() {
verifyfunctionExecution(new RegionFunctionExecution(clientRegion));
verifyfunctionExecution(new ServerFunctionExecution(cache));
verifyfunctionExecution(new PoolServerFunctionExecution(pool));
verifyfunctionExecution(new ServersFunctionExecution(cache));
}
private void verifyfunctionExecution(FunctionExecution functionExecution) {
Iterable<String> results = functionExecution
.setArgs("1","2","3")
.setFunctionId("echoFunction")
.execute();
Iterator<String> it = results.iterator();
for (int i = 1; i<= 3; i++) {
assertEquals(String.valueOf(i),it.next());
}
}
}

View File

@@ -0,0 +1,147 @@
/*
* Copyright 2002-2012 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.data.gemfire.function.execution;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.gemfire.ForkUtil;
import org.springframework.data.gemfire.fork.SpringCacheServerProcess;
import org.springframework.data.gemfire.function.config.GemfireFunction;
import org.springframework.data.gemfire.function.config.RegionData;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gemstone.gemfire.cache.Region;
/**
* @author David Turanski
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class FunctionIntegrationTests {
@Resource(name="test-region")
Region<String,Integer> region;
@BeforeClass
public static void startUp() throws Exception {
ForkUtil.startCacheServer(SpringCacheServerProcess.class.getName() + " "
+ " /org/springframework/data/gemfire/function/execution/FunctionIntegrationTests-server-context.xml");
}
@AfterClass
public static void cleanUp() {
ForkUtil.sendSignal();
}
@Before
public void initializeRegion() {
region.put("one", 1);
region.put("two", 2);
region.put("three",3);
}
@Test
public void testOnRegionFunctionExecution() {
GemfireOnRegionOperations template = new GemfireOnRegionFunctionTemplate(region);
Iterable<Integer> results;
results = template.execute("oneArg","two");
assertEquals(2, results.iterator().next().intValue());
Set<String> filter = new HashSet<String>();
filter.add("one");
results = template.execute("oneArg",filter,"two");
assertFalse(results.iterator().hasNext());
results = template.execute("twoArg","two","three");
assertEquals(5, results.iterator().next().intValue());
Integer result = template.executeAndExtract("twoArg","two","three");
assertEquals(5,result.intValue());
}
@Test
public void testCollectionReturnTypes() {
GemfireOnRegionOperations template = new GemfireOnRegionFunctionTemplate(region);
Object result = template.executeAndExtract("getMapWithNoArgs");
assertTrue(result instanceof Map);
@SuppressWarnings("unchecked")
Map<String,Integer> map = (Map<String,Integer>)result;
assertEquals(1,map.get("one").intValue());
assertEquals(2,map.get("two").intValue());
assertEquals(3,map.get("three").intValue());
result = template.execute("collections",Arrays.asList(new Integer[]{1,2,3,4,5}));
assertTrue(result.getClass().getName(),result instanceof List);
List<?> list = (List<?>)result;
assertEquals(5, list.size());
for (int i=1; i<= list.size(); i++) {
assertEquals(i,list.get(i-1));
}
}
/*
* This gets wrapped in a GemFire Function and registered on the forked server.
*/
@Component
public static class Foo {
@GemfireFunction(id="oneArg")
public Integer oneArg(String key, @RegionData Map<String,Integer> dataSet) {
return dataSet.get(key);
}
@GemfireFunction(id="twoArg")
public Integer twoArg(String akey, String bkey, @RegionData Map<String,Integer> dataSet) {
if (dataSet.get(akey) != null && dataSet.get(bkey) != null) {
return dataSet.get(akey) + dataSet.get(bkey);
}
return null;
}
@GemfireFunction(id="collections")
public List<Integer> collections(List<Integer> args) {
return args;
}
@GemfireFunction(id="getMapWithNoArgs")
public Map<String, Integer> getMapWithNoArgs(@RegionData Map<String,Integer> dataSet) {
if (dataSet.size() == 0) {
return null;
}
return new HashMap<String, Integer>(dataSet);
}
}
}

View File

@@ -0,0 +1,189 @@
/*
* Copyright 2002-2011 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.data.gemfire.function.execution;
/**
* @author David Turanski
*
*/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.gemfire.function.config.FunctionId;
/**
*
* @author David Turanski
*
*/
public class GemfireFunctionProxyFactoryBeanTests {
private GemfireFunctionOperations functionOperations;
@Before
public void setUp() {
functionOperations = mock(GemfireFunctionOperations.class);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testInvokeAndExtractWithAnnotatedFunctionId() throws Throwable {
GemfireFunctionProxyFactoryBean proxy = new GemfireFunctionProxyFactoryBean(IFoo.class,functionOperations);
proxy.setFunctionId(IFoo.class.getName());
MethodInvocation invocation = new TestInvocation(IFoo.class).withMethodNameAndArgTypes("oneArg",String.class);
List results = Arrays.asList(new Integer[]{1});
when(functionOperations.execute("oneArg",invocation.getArguments())).thenReturn(results);
Object result = proxy.invoke(invocation);
assertTrue(result instanceof Integer);
assertEquals(new Integer(1),result);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testInvoke() throws Throwable {
GemfireFunctionProxyFactoryBean proxy = new GemfireFunctionProxyFactoryBean(IFoo.class, functionOperations);
proxy.setFunctionId(IFoo.class.getName());
MethodInvocation invocation = new TestInvocation(IFoo.class).withMethodNameAndArgTypes("collections",List.class);
List results = Arrays.asList(new Integer[]{1,2,3});
when(functionOperations.execute(IFoo.class.getName() + ".collections",invocation.getArguments())).thenReturn(results);
Object result = proxy.invoke(invocation);
assertTrue(result instanceof List);
assertEquals(3,((List<?>)result).size());
}
static class TestInvocation implements MethodInvocation {
private Class<?>[] argTypes;
private Class<?> clazz;
private String methodName;
private Object[] arguments;
public TestInvocation(Class<?> clazz) {
this.clazz = clazz;
}
public TestInvocation withArguments(Object ...arguments){
this.arguments = arguments;
return this;
}
public TestInvocation withMethodNameAndArgTypes(String methodName,Class<?>... argTypes) {
this.methodName = methodName;
this.argTypes = argTypes;
return this;
}
/* (non-Javadoc)
* @see org.aopalliance.intercept.Invocation#getArguments()
*/
@Override
public Object[] getArguments() {
// TODO Auto-generated method stub
return this.arguments;
}
/* (non-Javadoc)
* @see org.aopalliance.intercept.Joinpoint#proceed()
*/
@Override
public Object proceed() throws Throwable {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.aopalliance.intercept.Joinpoint#getThis()
*/
@Override
public Object getThis() {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.aopalliance.intercept.Joinpoint#getStaticPart()
*/
@Override
public AccessibleObject getStaticPart() {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.aopalliance.intercept.MethodInvocation#getMethod()
*/
@Override
public Method getMethod() {
Method method = null;
try {
method = clazz.getMethod(methodName, argTypes);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return method;
}
}
public interface IFoo {
@FunctionId("oneArg")
public abstract Integer oneArg(String key);
public abstract Integer twoArg(String akey, String bkey);
public abstract List<Integer> collections(List<Integer> args);
public abstract Map<String, Integer> getMapWithNoArgs();
}
}

View File

@@ -10,12 +10,11 @@
* 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.data.gemfire.function;
package org.springframework.data.gemfire.function.execution;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.List;
import java.util.Iterator;
import java.util.Properties;
import org.junit.AfterClass;
@@ -23,7 +22,6 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.data.gemfire.ForkUtil;
import org.springframework.data.gemfire.fork.FunctionCacheServerProcess;
import org.springframework.data.gemfire.function.foo.Foo;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
@@ -84,29 +82,23 @@ public class GemfireFunctionTemplateTests {
}
cache = null;
}
@Test
public void testExecuteOnRegion() {
GemfireFunctionOperations<Integer> functionTemplate = new GemfireFunctionTemplate<Integer>(cache);
RemoteMethodInvocation invocation = new RemoteMethodInvocation(Foo.class,"oneArg", "two");
List<Integer> result = functionTemplate.executeOnRegion(new MethodInvokingFunction().getId(),"test-function",invocation);
assertEquals(2,result.get(0).intValue());
}
@Test
public void testExecuteOnRegionAndExtract() {
GemfireFunctionOperations<Integer> functionTemplate = new GemfireFunctionTemplate<Integer>(cache);
RemoteMethodInvocation invocation = new RemoteMethodInvocation(Foo.class,"twoArg", "two","three");
int result = functionTemplate.executeOnRegionAndExtract(new MethodInvokingFunction(),"test-function",invocation);
assertEquals(5,result);
}
@Test
public void testExecuteOnServer() {
assertNull(clientRegion.get("four"));
GemfireFunctionOperations<?> functionTemplate = new GemfireFunctionTemplate<Object>(cache);
functionTemplate.executeOnServers("serverFunction","four",4);
assertEquals(4,clientRegion.get("four").intValue());
public void testFunctionTemplates() {
verifyfunctionTemplateExecution( new GemfireOnServerFunctionTemplate(cache));
verifyfunctionTemplateExecution( new GemfireOnServersFunctionTemplate(cache));
verifyfunctionTemplateExecution( new GemfireOnRegionFunctionTemplate(clientRegion));
verifyfunctionTemplateExecution( new GemfireOnServerFunctionTemplate(pool));
verifyfunctionTemplateExecution( new GemfireOnServersFunctionTemplate(pool));
}
private void verifyfunctionTemplateExecution(GemfireFunctionOperations functionTemplate) {
Iterable<String> results = functionTemplate.execute("echoFunction","1","2","3");
Iterator<String> it = results.iterator();
for (int i = 1; i<= 3; i++) {
assertEquals(String.valueOf(i),it.next());
}
}
}

View File

@@ -1,73 +0,0 @@
/*
* Copyright 2002-2011 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.data.gemfire.function.foo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author David Turanski
*
*/
public class Foo implements IFoo {
private Map<String, Integer> dataSet;
public Foo(Map<String, Integer> dataSet) {
this.dataSet = dataSet;
}
public Foo() {
}
/* (non-Javadoc)
* @see org.springframework.data.gemfire.function.foo.IFoo#oneArg(java.lang.String)
*/
public Integer oneArg(String key) {
return dataSet.get(key);
}
/* (non-Javadoc)
* @see org.springframework.data.gemfire.function.foo.IFoo#twoArg(java.lang.String, java.lang.String)
*/
public Integer twoArg(String akey, String bkey) {
if (dataSet.get(akey) != null && dataSet.get(bkey) != null) {
return dataSet.get(akey) + dataSet.get(bkey);
}
else {
return null;
}
}
/* (non-Javadoc)
* @see org.springframework.data.gemfire.function.foo.IFoo#collections(java.util.List)
*/
public List<Integer> collections(List<Integer> args) {
return args;
}
/* (non-Javadoc)
* @see org.springframework.data.gemfire.function.foo.IFoo#getMapWithNoArgs()
*/
public Map<String, Integer> getMapWithNoArgs() {
if (dataSet.size() == 0) {
return null;
}
return new HashMap<String, Integer>(dataSet);
}
}

View File

@@ -15,8 +15,13 @@
*/
package org.springframework.data.gemfire.repository.support;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collection;
@@ -34,7 +39,6 @@ import org.springframework.data.repository.core.support.ReflectionEntityInformat
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gemstone.gemfire.cache.CacheListener;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionEvent;
import com.gemstone.gemfire.cache.query.SelectResults;
@@ -57,9 +61,10 @@ public class SimpleGemfireRepositoryIntegrationTest {
SimpleGemfireRepository<Person, Long> repository;
@SuppressWarnings("rawtypes")
RegionClearListener regionClearListener;
@SuppressWarnings("unchecked")
@Before
public void setUp() {
regionClearListener = new RegionClearListener();
@@ -123,6 +128,7 @@ public class SimpleGemfireRepositoryIntegrationTest {
assertThat(result, not(hasItems(dave)));
}
@SuppressWarnings("rawtypes")
public static class RegionClearListener extends CacheListenerAdapter {
public boolean eventFired;
@Override

View File

@@ -36,6 +36,7 @@ import com.gemstone.gemfire.Instantiator;
*/
public class AsmInstantiatorFactoryTest {
@SuppressWarnings("serial")
public static class SomeClass implements DataSerializable {
public static boolean instantiated = false;

View File

@@ -52,6 +52,7 @@ public class WiringInstantiatorTest {
private WiringInstantiator instantiator;
@SuppressWarnings("serial")
public static class AnnotatedBean implements DataSerializable {
@Autowired
Point point;
@@ -69,6 +70,7 @@ public class WiringInstantiatorTest {
}
}
@SuppressWarnings("serial")
public static class TemplateWiringBean implements DataSerializable {
Point point;
Beans beans;
@@ -84,6 +86,7 @@ public class WiringInstantiatorTest {
}
}
@SuppressWarnings("serial")
public static class TypeA implements DataSerializable {
public void fromData(DataInput arg0) throws IOException, ClassNotFoundException {
@@ -93,6 +96,7 @@ public class WiringInstantiatorTest {
}
}
@SuppressWarnings("serial")
public static class TypeB implements DataSerializable {
public void fromData(DataInput arg0) throws IOException, ClassNotFoundException {
@@ -136,6 +140,7 @@ public class WiringInstantiatorTest {
}
public void testInstantiatorFactoryBean() throws Exception {
@SuppressWarnings("unchecked")
List<Instantiator> list = (List<Instantiator>) ctx.getBean("instantiator-factory");
assertNotNull(list);
assertEquals(2, list.size());