Fixes JIRA issues SGF-271 - <cq-listener-container> element missing the 'auto-startup' attribute, SGF-275 - <cq-listener-container> element's phase attribute is ignored, SGF-278 - ContinuousQueryListenerContainer class's 'taskExecutor' property is not set properly by the GemfireListenerContainerParser, SGF-279 - <cq-listener-container> element is missing the 'error-handler' attribute, and finally, SGF-280 - the ContinuousQueryListenerContainer class is not Thread-safe.

This commit is contained in:
John Blum
2014-04-18 23:01:02 -07:00
parent 3e300ddaf5
commit 163e36497c
10 changed files with 921 additions and 632 deletions

View File

@@ -0,0 +1,137 @@
/*
* Copyright 2010-2013 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.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
import javax.annotation.Resource;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.gemfire.ForkUtil;
import org.springframework.data.gemfire.TestUtils;
import org.springframework.data.gemfire.listener.ContinuousQueryListener;
import org.springframework.data.gemfire.listener.ContinuousQueryListenerContainer;
import org.springframework.data.gemfire.listener.GemfireMDP;
import org.springframework.data.gemfire.listener.adapter.ContinuousQueryListenerAdapter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.ErrorHandler;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.query.CqListener;
import com.gemstone.gemfire.cache.query.CqQuery;
/**
* The ContinuousQueryListenerContainerNamespaceTest class is a test suite of test cases testing the SDG XML namespace
* for proper configuration and initialization of a ContinuousQueryListenerContainer bean component
* in the Spring context.
*
* @author John Blum
* @see org.junit.Test
* @see org.junit.runner.RunWith
* @see org.springframework.data.gemfire.listener.ContinuousQueryListenerContainer
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
* @see com.gemstone.gemfire.cache.client.ClientCache
* @see com.gemstone.gemfire.cache.query.CqListener
* @see com.gemstone.gemfire.cache.query.CqQuery
* @since 1.4.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SuppressWarnings("unused")
public class ContinuousQueryListenerContainerNamespaceTest {
@BeforeClass
public static void setupBeforeClass() {
ForkUtil.cacheServer();
}
@AfterClass
public static void tearDownAfterClass() {
ForkUtil.sendSignal();
}
@Autowired
private ClientCache gemfireCache;
@Autowired
private ContinuousQueryListenerContainer container;
@Resource(name = "testErrorHandler")
private ErrorHandler testErrorHandler;
@Resource(name = "testTaskExecutor")
private Executor testTaskExecutor;
@Test
public void testContainerConfiguration() throws Exception {
assertNotNull("The ContinuousQueryListenerContainer was not properly configured!", container);
assertTrue("The CQ Listener Container should be active (initialized)!", container.isActive());
assertFalse("The CQ Listener container should not be configured to auto-start!", container.isAutoStartup());
assertFalse("The CQ Listener Container should not be running!", container.isRunning());
assertEquals(4, container.getPhase());
assertNotNull(testErrorHandler);
assertSame(testErrorHandler, TestUtils.readField("errorHandler", container));
assertNotNull(testTaskExecutor);
assertSame(testTaskExecutor, TestUtils.readField("taskExecutor", container));
CqQuery[] queries = gemfireCache.getQueryService().getCqs();
assertNotNull(queries);
assertEquals(3, queries.length);
List<String> actualNames = new ArrayList<String>(3);
for (CqQuery query : queries) {
actualNames.add(query.getName());
assertEquals("SELECT * FROM /test-cq", query.getQueryString());
assertEquals("Q3".equalsIgnoreCase(query.getName()), query.isDurable());
CqListener cqListener = query.getCqAttributes().getCqListener();
assertNotNull(cqListener);
// the CqListener object should be an instance of...
// org.springframework.data.gemfire.listener.ContinuousQueryListenerContainer.EventDispatcherAdapter
// So, get the SDG "ContinuousQueryListener"...
ContinuousQueryListener listener = TestUtils.readField("delegate", cqListener);
assertTrue(listener instanceof ContinuousQueryListenerAdapter);
assertTrue(((ContinuousQueryListenerAdapter) listener).getDelegate() instanceof GemfireMDP);
if ("Q2".equalsIgnoreCase(query.getName())) {
assertEquals("handleQuery", TestUtils.readField("defaultListenerMethod", listener));
}
}
actualNames.containsAll(Arrays.asList("Q1", "Q2", "Q3"));
}
}

View File

@@ -20,10 +20,8 @@ import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import org.springframework.data.gemfire.ForkUtil;
import com.gemstone.gemfire.cache.AttributesFactory;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.DataPolicy;
@@ -31,48 +29,49 @@ 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;
/**
* @author Costin Leau
* @author John Blum
*/
public class CacheServerProcess {
public static void main(String[] args) throws Exception {
public static void main(final String[] args) throws Exception {
Properties props = new Properties();
props.setProperty("name", "CqServer");
props.setProperty("mcast-port", "0");
props.setProperty("log-level", "warning");
Cache cache = new CacheFactory(props).create();
// Create region.
// Create region.
RegionFactory<Object,Object> factory = cache.createRegionFactory();
factory.setDataPolicy(DataPolicy.REPLICATE);
factory.setScope(Scope.DISTRIBUTED_ACK);
Region testRegion = factory.create("test-cq");
System.out.println("Test region, " + testRegion.getFullPath() + ", created in cache.");
// Start Cache Server.
RegionFactory<String, Integer> regionFactory = cache.createRegionFactory();
regionFactory.setDataPolicy(DataPolicy.REPLICATE);
regionFactory.setScope(Scope.DISTRIBUTED_ACK);
Region<String, Integer> testRegion = regionFactory.create("test-cq");
System.out.printf("Test Region '%1$s' created in Cache '%2$s.%n", testRegion.getFullPath(), cache.getName());
CacheServer server = cache.addCacheServer();
server.setPort(40404);
server.start();
ForkUtil.createControlFile(CacheServerProcess.class.getName());
System.out.println("Waiting for signal");
// wait for signal
System.out.println("Waiting for signal...");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
bufferedReader.readLine();
System.out.println("Received signal");
System.out.println("Signal received!");
testRegion.put("one", 1);
testRegion.put("two", 2);
testRegion.put("three", 3);
System.out.println("Waiting for shutdown");
System.out.println("Waiting for shutdown...");
bufferedReader.readLine();
}
}

View File

@@ -17,6 +17,8 @@
package org.springframework.data.gemfire.listener.adapter;
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 org.junit.AfterClass;
@@ -24,8 +26,6 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.data.gemfire.ForkUtil;
import org.springframework.data.gemfire.listener.ContinuousQueryDefinition;
import org.springframework.data.gemfire.listener.ContinuousQueryListener;
import org.springframework.data.gemfire.listener.ContinuousQueryListenerContainer;
import com.gemstone.gemfire.cache.Cache;
@@ -34,6 +34,7 @@ import com.gemstone.gemfire.cache.query.CqQuery;
/**
* @author Costin Leau
* @author John Blum
*/
public class ContainerXmlSetupTest {
@@ -47,28 +48,38 @@ public class ContainerXmlSetupTest {
ForkUtil.sendSignal();
}
@Test
public void testContainerSetup() throws Exception {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(
"/org/springframework/data/gemfire/listener/container.xml");
GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(
"/org/springframework/data/gemfire/listener/container.xml");
ContinuousQueryListenerContainer container = ctx.getBean(ContinuousQueryListenerContainer.class);
assertTrue(container.isRunning());
try {
ContinuousQueryListenerContainer container = applicationContext.getBean(
ContinuousQueryListenerContainer.class);
Cache cache = ctx.getBean("gemfireCache", Cache.class);
Pool pool = ctx.getBean("client", Pool.class);
// Test getting container listener bean by ID
ctx.getBean("testContainerId");
assertNotNull(container);
assertTrue(container.isRunning());
CqQuery[] cqs = cache.getQueryService().getCqs();
CqQuery[] pcqs = pool.getQueryService().getCqs();
assertTrue(pool.getQueryService().getCq("test-bean-1") != null);
assertEquals(3, cqs.length);
assertEquals(3, pcqs.length);
ForkUtil.sendSignal();
ctx.close();
// test getting container listener by bean ID
ContinuousQueryListenerContainer container2 = applicationContext.getBean("testContainerId",
ContinuousQueryListenerContainer.class);
assertSame(container, container2);
Cache cache = applicationContext.getBean("gemfireCache", Cache.class);
Pool pool = applicationContext.getBean("client", Pool.class);
CqQuery[] cqs = cache.getQueryService().getCqs();
CqQuery[] poolCqs = pool.getQueryService().getCqs();
assertTrue(pool.getQueryService().getCq("test-bean-1") != null);
assertEquals(3, cqs.length);
assertEquals(3, poolCqs.length);
}
finally {
ForkUtil.sendSignal();
applicationContext.close();
}
}
}