SGF-375 - The <gfe:gateway-hub> XML namespace element is missing the 'max-time-between-pings' attribute.

This commit is contained in:
John Blum
2015-02-25 16:20:31 -08:00
parent 6aba6f59ca
commit a644e5dd89
11 changed files with 960 additions and 6 deletions

View File

@@ -56,7 +56,7 @@ class GatewayHubParser extends AbstractSimpleBeanDefinitionParser {
ParsingUtils.setPropertyValue(element, builder, "bind-address");
ParsingUtils.setPropertyValue(element, builder, "manual-start");
//ParsingUtils.setPropertyValue(element, builder, "max-time-between-pings", "maximumTimeBetweenPings");
ParsingUtils.setPropertyValue(element, builder, "max-time-between-pings", "maximumTimeBetweenPings");
ParsingUtils.setPropertyValue(element, builder, "socket-buffer-size");
ParsingUtils.setPropertyValue(element, builder, "startup-policy");
ParsingUtils.setPropertyValue(element, builder, "port");

View File

@@ -3033,6 +3033,13 @@ Specifies if the gateway hub is manually (true) or automatically(false) started
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="max-time-between-pings" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
Sets the maximum amount of time between client pings.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="port" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[

View File

@@ -0,0 +1,220 @@
/*
* 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.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.util.Gateway;
import com.gemstone.gemfire.cache.util.GatewayEvent;
import com.gemstone.gemfire.cache.util.GatewayEventListener;
import com.gemstone.gemfire.cache.util.GatewayHub;
import com.gemstone.gemfire.cache.util.GatewayQueueAttributes;
/**
* The GatewayHubNamespaceTest class is a test suite of test cases testing the contract and functionality
* of the GatewayHub SDG XML namespace (XSD) configuration meta-data.
*
* @author John Blum
* @see org.junit.Test
* @see org.junit.runner.RunWith
* @see org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer
* @see org.springframework.data.gemfire.wan.GatewayHubFactoryBean
* @see org.springframework.data.gemfire.wan.GatewayProxy
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
* @see com.gemstone.gemfire.cache.util.Gateway
* @see com.gemstone.gemfire.cache.util.GatewayHub
* @since 1.5.3
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers = GemfireTestApplicationContextInitializer.class)
@SuppressWarnings({"deprecation", "unused" })
public class GatewayHubNamespaceTest {
@Resource(name = "Example")
private Region<?, ?> example;
@Autowired
private GatewayHub gatewayHub;
protected Gateway getGatewayById(final List<Gateway> gateways, final String id) {
for (Gateway gateway : gateways) {
if (gateway.getId().equals(id)) {
return gateway;
}
}
return null;
}
@Test
public void testRegionConfiguration() {
assertNotNull("The '/Example' Region was not properly initialized!", example);
assertEquals("Example", example.getName());
assertEquals("/Example", example.getFullPath());
assertNotNull(example.getAttributes());
assertEquals(DataPolicy.REPLICATE, example.getAttributes().getDataPolicy());
assertTrue(example.getAttributes().getEnableGateway());
assertEquals("testGatewayHub", example.getAttributes().getGatewayHubId());
}
@Test
public void testGatewayConfiguration() {
assertNotNull("The 'TestGatewayHub' GatewayHub was not properly initialized!", gatewayHub);
assertEquals("localhost", gatewayHub.getBindAddress());
assertEquals("TestGatewayHub", gatewayHub.getId());
assertTrue(gatewayHub.getManualStart());
assertEquals(5000, gatewayHub.getMaximumTimeBetweenPings());
assertEquals(45123, gatewayHub.getPort());
assertEquals(16384, gatewayHub.getSocketBufferSize());
assertEquals("primary", gatewayHub.getStartupPolicy());
List<Gateway> gateways = gatewayHub.getGateways();
assertNotNull(gateways);
assertFalse(gateways.isEmpty());
assertEquals(3, gateways.size());
Gateway gatewayOne = getGatewayById(gateways, "gateway1");
assertNotNull(gatewayOne);
assertEquals("gateway1", gatewayOne.getId());
assertEquals(8, gatewayOne.getConcurrencyLevel());
assertEquals(Gateway.OrderPolicy.THREAD, gatewayOne.getOrderPolicy());
assertEquals(65536, gatewayOne.getSocketBufferSize());
//assertEquals(75000, gatewayOne.getSocketReadTimeout());
assertTrue(gatewayOne.getEndpoints() == null || gatewayOne.getEndpoints().isEmpty());
List<GatewayEventListener> gatewayEventListeners = gatewayOne.getListeners();
assertNotNull(gatewayEventListeners);
assertFalse(gatewayEventListeners.isEmpty());
assertEquals(2, gatewayEventListeners.size());
assertTrue(gatewayEventListeners.get(0) instanceof TestGatewayListener);
assertEquals("ListenerOne", gatewayEventListeners.get(0).toString());
assertTrue(gatewayEventListeners.get(1) instanceof TestGatewayListener);
assertEquals("ListenerTwo", gatewayEventListeners.get(1).toString());
GatewayQueueAttributes gatewayQueueAttributes = gatewayOne.getQueueAttributes();
assertNotNull(gatewayQueueAttributes);
assertEquals(99, gatewayQueueAttributes.getAlertThreshold());
assertTrue(gatewayQueueAttributes.getBatchConflation());
assertEquals(3, gatewayQueueAttributes.getBatchSize());
assertEquals(10, gatewayQueueAttributes.getBatchTimeInterval());
assertEquals("TestGatewayQueueDiskStore", gatewayQueueAttributes.getDiskStoreName());
assertFalse(gatewayQueueAttributes.getEnablePersistence());
assertEquals(5, gatewayQueueAttributes.getMaximumQueueMemory());
Gateway gatewayTwo = getGatewayById(gateways, "gateway2");
assertNotNull(gatewayTwo);
assertEquals("gateway2", gatewayTwo.getId());
assertEquals(Gateway.DEFAULT_CONCURRENCY_LEVEL, gatewayTwo.getConcurrencyLevel());
assertNull(gatewayTwo.getOrderPolicy());
assertEquals(Gateway.DEFAULT_SOCKET_BUFFER_SIZE, gatewayTwo.getSocketBufferSize());
assertEquals(Gateway.DEFAULT_SOCKET_READ_TIMEOUT, gatewayTwo.getSocketReadTimeout());
assertTrue(gatewayTwo.getListeners() == null || gatewayTwo.getListeners().isEmpty());
List gatewayEndpoints = gatewayTwo.getEndpoints();
assertNotNull(gatewayEndpoints);
assertFalse(gatewayEndpoints.isEmpty());
assertEquals(2, gatewayEndpoints.size());
Gateway.Endpoint gatewayEndpointOne = (Gateway.Endpoint) gatewayEndpoints.get(0);
assertEquals("endpoint1", gatewayEndpointOne.getId());
assertEquals("localhost", gatewayEndpointOne.getHost());
assertEquals(1234, gatewayEndpointOne.getPort());
Gateway.Endpoint gatewayEndpointTwo = (Gateway.Endpoint) gatewayEndpoints.get(1);
assertEquals("endpoint2", gatewayEndpointTwo.getId());
assertEquals("localhost", gatewayEndpointTwo.getHost());
assertEquals(4321, gatewayEndpointTwo.getPort());
gatewayQueueAttributes = gatewayTwo.getQueueAttributes();
assertNotNull(gatewayQueueAttributes);
assertEquals(GatewayQueueAttributes.DEFAULT_ALERT_THRESHOLD, gatewayQueueAttributes.getAlertThreshold());
assertFalse(gatewayQueueAttributes.getBatchConflation());
assertEquals(6, gatewayQueueAttributes.getBatchSize());
assertEquals(20, gatewayQueueAttributes.getBatchTimeInterval());
assertNull(gatewayQueueAttributes.getDiskStoreName());
assertTrue(gatewayQueueAttributes.getEnablePersistence());
assertEquals(GatewayQueueAttributes.DEFAULT_MAXIMUM_QUEUE_MEMORY, gatewayQueueAttributes.getMaximumQueueMemory());
Gateway gatewayThree = getGatewayById(gateways, "gateway3");
assertNotNull(gatewayThree);
assertEquals("gateway3", gatewayThree.getId());
assertEquals(Gateway.DEFAULT_CONCURRENCY_LEVEL, gatewayThree.getConcurrencyLevel());
assertNull(gatewayThree.getOrderPolicy());
assertEquals(Gateway.DEFAULT_SOCKET_BUFFER_SIZE, gatewayThree.getSocketBufferSize());
assertEquals(Gateway.DEFAULT_SOCKET_READ_TIMEOUT, gatewayThree.getSocketReadTimeout());
assertTrue(gatewayThree.getEndpoints() == null || gatewayThree.getEndpoints().isEmpty());
gatewayEventListeners = gatewayThree.getListeners();
assertNotNull(gatewayEventListeners);
assertFalse(gatewayEventListeners.isEmpty());
assertEquals(1, gatewayEventListeners.size());
assertTrue(gatewayEventListeners.get(0) instanceof TestGatewayListener);
assertEquals("ListenerTwo", gatewayEventListeners.get(0).toString());
}
public static final class TestGatewayListener implements GatewayEventListener {
private String name;
public void setName(final String name) {
this.name = name;
}
@Override
public boolean processEvents(final List<GatewayEvent> events) {
return false;
}
@Override
public void close() {
}
@Override
public String toString() {
return name;
}
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.test;
import org.mockito.invocation.InvocationOnMock;
import org.springframework.data.gemfire.test.support.IdentifierSequence;
import org.springframework.data.gemfire.test.support.StackTraceUtils;
/**
* The AbstractMockery class is an abstract base class supporting the creation and use of mock objects in unit tests.
*
* @author John Blum
* @see org.mockito.invocation.InvocationOnMock
* @see org.springframework.data.gemfire.test.support.IdentifierSequence
* @see org.springframework.data.gemfire.test.support.StackTraceUtils
* @since 1.5.3
*/
@SuppressWarnings("unused")
public abstract class AbstractMockerySupport {
protected static final String NOT_IMPLEMENTED = "Not Implemented";
protected Object getMockId() {
StackTraceElement element = StackTraceUtils.getTestCaller();
return (element != null ? StackTraceUtils.getCallerSimpleName(element): IdentifierSequence.nextId());
}
protected <T> T getArgumentAt(final InvocationOnMock invocation, final int index, final Class<T> type) {
return type.cast(invocation.getArguments()[index]);
}
}

View File

@@ -0,0 +1,266 @@
/*
* 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.test;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import com.gemstone.gemfire.cache.util.Gateway;
import com.gemstone.gemfire.cache.util.GatewayEventListener;
import com.gemstone.gemfire.cache.util.GatewayQueueAttributes;
/**
* The MockGatewayFactory class is a factory for creating mock GemFire Gateways, GatewayQueueAttributes
* and Gateway.Endpoints.
*
* @author John Blum
* @see org.mockito.Mockito
* @see org.springframework.data.gemfire.wan.GatewayHubFactoryBean
* @see com.gemstone.gemfire.cache.util.Gateway
* @see com.gemstone.gemfire.cache.util.GatewayHub
* @since 1.5.3
*/
@SuppressWarnings({ "deprecation", "unused" })
public class MockGatewayFactory extends AbstractMockerySupport {
private Boolean batchConflation = GatewayQueueAttributes.DEFAULT_BATCH_CONFLATION;
private Boolean persistence = GatewayQueueAttributes.DEFAULT_ENABLE_PERSISTENCE;
private Gateway.OrderPolicy orderPolicy;
private GatewayQueueAttributes queueAttributes;
private Integer alertThreshold = GatewayQueueAttributes.DEFAULT_ALERT_THRESHOLD;
private Integer batchSize = GatewayQueueAttributes.DEFAULT_BATCH_SIZE;
private Integer batchTimeInterval = GatewayQueueAttributes.DEFAULT_BATCH_TIME_INTERVAL;
private Integer maxQueueMemory = GatewayQueueAttributes.DEFAULT_MAXIMUM_QUEUE_MEMORY;
private Integer socketBufferSize = Gateway.DEFAULT_SOCKET_BUFFER_SIZE;
private Integer socketReadTimeout = Gateway.DEFAULT_SOCKET_READ_TIMEOUT;
private List<Gateway.Endpoint> endpoints;
private List<GatewayEventListener> listeners;
private String diskStoreName;
public MockGatewayFactory() {
endpoints = new ArrayList<Gateway.Endpoint>();
listeners = new ArrayList<GatewayEventListener>();
queueAttributes = mockGatewayQueueAttributes();
}
public Gateway mockGateway(final String id, final int concurrencyLevel) {
Gateway mockGateway = mock(Gateway.class, String.format("%1$s.MockGateway", getMockId()));
when(mockGateway.getConcurrencyLevel()).thenReturn(concurrencyLevel);
when(mockGateway.getEndpoints()).thenReturn(endpoints);
when(mockGateway.getId()).thenReturn(id);
when(mockGateway.getListeners()).thenReturn(listeners);
when(mockGateway.getQueueSize()).thenThrow(new UnsupportedOperationException(NOT_IMPLEMENTED));
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
orderPolicy = getArgumentAt(invocation, 0, Gateway.OrderPolicy.class);
return null;
}
}).when(mockGateway).setOrderPolicy(any(Gateway.OrderPolicy.class));
when(mockGateway.getOrderPolicy()).thenAnswer(new Answer<Gateway.OrderPolicy>() {
@Override public Gateway.OrderPolicy answer(final InvocationOnMock invocation) throws Throwable {
return orderPolicy;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
queueAttributes = getArgumentAt(invocation, 0, GatewayQueueAttributes.class);
return null;
}
}).when(mockGateway).setQueueAttributes(any(GatewayQueueAttributes.class));
when(mockGateway.getQueueAttributes()).thenAnswer(new Answer<GatewayQueueAttributes>() {
@Override public GatewayQueueAttributes answer(final InvocationOnMock invocation) throws Throwable {
return queueAttributes;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
socketBufferSize = getArgumentAt(invocation, 0, Integer.class);
return null;
}
}).when(mockGateway).setSocketBufferSize(anyInt());
when(mockGateway.getSocketBufferSize()).thenAnswer(new Answer<Integer>() {
@Override public Integer answer(final InvocationOnMock invocation) throws Throwable {
return socketBufferSize;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
socketReadTimeout = getArgumentAt(invocation, 0, Integer.class);
return null;
}
}).when(mockGateway).setSocketReadTimeout(anyInt());
when(mockGateway.getSocketReadTimeout()).thenAnswer(new Answer<Integer>() {
@Override public Integer answer(final InvocationOnMock invocation) throws Throwable {
return socketReadTimeout;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
String id = getArgumentAt(invocation, 0, String.class);
String host = getArgumentAt(invocation, 1, String.class);
int port = getArgumentAt(invocation, 2, Integer.class);
endpoints.add(mockGatewayEndpoint(id, host, port));
return null;
}
}).when(mockGateway).addEndpoint(anyString(), anyString(), anyInt());
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
listeners.add(getArgumentAt(invocation, 0, GatewayEventListener.class));
return null;
}
}).when(mockGateway).addListener(any(GatewayEventListener.class));
return mockGateway;
}
public Gateway.Endpoint mockGatewayEndpoint(final String id, final String host, final int port) {
Gateway.Endpoint mockEndpoint = mock(Gateway.Endpoint.class,
String.format("%1$s.MockGatewayEndpoint", getMockId()));
when(mockEndpoint.getId()).thenReturn(id);
when(mockEndpoint.getHost()).thenReturn(host);
when(mockEndpoint.getPort()).thenReturn(port);
return mockEndpoint;
}
public GatewayQueueAttributes mockGatewayQueueAttributes() {
GatewayQueueAttributes mockQueueAttributes = mock(GatewayQueueAttributes.class,
String.format("%1$s.MockGatewayQueueAttributes", getMockId()));
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
alertThreshold = getArgumentAt(invocation, 0, Integer.class);
return null;
}
}).when(mockQueueAttributes).setAlertThreshold(anyInt());
when(mockQueueAttributes.getAlertThreshold()).thenAnswer(new Answer<Integer>() {
@Override public Integer answer(final InvocationOnMock invocation) throws Throwable {
return alertThreshold;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
batchConflation = getArgumentAt(invocation, 0, Boolean.class);
return null;
}
}).when(mockQueueAttributes).setBatchConflation(anyBoolean());
when(mockQueueAttributes.getBatchConflation()).thenAnswer(new Answer<Boolean>() {
@Override public Boolean answer(final InvocationOnMock invocation) throws Throwable {
return batchConflation;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
batchSize = getArgumentAt(invocation, 0, Integer.class);
return null;
}
}).when(mockQueueAttributes).setBatchSize(anyInt());
when(mockQueueAttributes.getBatchSize()).thenAnswer(new Answer<Integer>() {
@Override public Integer answer(final InvocationOnMock invocation) throws Throwable {
return batchSize;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
batchTimeInterval = getArgumentAt(invocation, 0, Integer.class);
return null;
}
}).when(mockQueueAttributes).setBatchTimeInterval(anyInt());
when(mockQueueAttributes.getBatchTimeInterval()).thenAnswer(new Answer<Integer>() {
@Override public Integer answer(final InvocationOnMock invocation) throws Throwable {
return batchTimeInterval;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
diskStoreName = getArgumentAt(invocation, 0, String.class);
return null;
}
}).when(mockQueueAttributes).setDiskStoreName(anyString());
when(mockQueueAttributes.getDiskStoreName()).thenAnswer(new Answer<String>() {
@Override public String answer(final InvocationOnMock invocation) throws Throwable {
return diskStoreName;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
persistence = getArgumentAt(invocation, 0, Boolean.class);
return null;
}
}).when(mockQueueAttributes).setEnablePersistence(anyBoolean());
when(mockQueueAttributes.getEnablePersistence()).thenAnswer(new Answer<Boolean>() {
@Override public Boolean answer(final InvocationOnMock invocation) throws Throwable {
return persistence;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
maxQueueMemory = getArgumentAt(invocation, 0, Integer.class);
return null;
}
}).when(mockQueueAttributes).setMaximumQueueMemory(anyInt());
when(mockQueueAttributes.getMaximumQueueMemory()).thenAnswer(new Answer<Integer>() {
@Override public Integer answer(final InvocationOnMock invocation) throws Throwable {
return maxQueueMemory;
}
});
return mockQueueAttributes;
}
}

View File

@@ -0,0 +1,166 @@
/*
* 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.test;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import com.gemstone.gemfire.cache.util.Gateway;
import com.gemstone.gemfire.cache.util.GatewayHub;
/**
* The MockGatewayHubFactory class is a factory for creating mock GemFire GatewayHubs.
*
* @author John Blum
* @see org.mockito.Mockito
* @see org.springframework.data.gemfire.test.AbstractMockerySupport
* @see org.springframework.data.gemfire.wan.GatewayHubFactoryBean
* @see com.gemstone.gemfire.cache.util.Gateway
* @see com.gemstone.gemfire.cache.util.GatewayHub
* @since 1.5.3
*/
@SuppressWarnings({ "deprecation", "unused" })
public class MockGatewayHubFactory extends AbstractMockerySupport {
private static final AtomicLong ID_SEQUENCE = new AtomicLong(System.currentTimeMillis());
private Boolean manualStart = GatewayHub.DEFAULT_MANUAL_START;
private Integer maximumTimeBetweenPings = GatewayHub.DEFAULT_MAXIMUM_TIME_BETWEEN_PINGS;
private Integer socketBufferSize = GatewayHub.DEFAULT_SOCKET_BUFFER_SIZE;
private List<Gateway> gateways = new ArrayList<Gateway>();
private String bindAddress = GatewayHub.DEFAULT_BIND_ADDRESS;
private String startupPolicy = GatewayHub.DEFAULT_STARTUP_POLICY;
public GatewayHub mockGatewayHub(final String id, final int port) {
final GatewayHub mockGatewayHub = mock(GatewayHub.class, String.format("%1$s.MockGatewayHub", getMockId()));
when(mockGatewayHub.getGateways()).thenReturn(gateways);
when(mockGatewayHub.getGatewayIds()).thenReturn(getGatewayIds(gateways));
when(mockGatewayHub.getId()).thenReturn(id);
when(mockGatewayHub.getPort()).thenReturn(port);
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
bindAddress = getArgumentAt(invocation, 0, String.class);
return null;
}
}).when(mockGatewayHub).setBindAddress(anyString());
when(mockGatewayHub.getBindAddress()).thenAnswer(new Answer<String>() {
@Override public String answer(final InvocationOnMock invocation) throws Throwable {
return bindAddress;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
manualStart = getArgumentAt(invocation, 0, Boolean.class);
return null;
}
}).when(mockGatewayHub).setManualStart(anyBoolean());
when(mockGatewayHub.getManualStart()).thenAnswer(new Answer<Boolean>() {
@Override public Boolean answer(final InvocationOnMock invocation) throws Throwable {
return manualStart;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
maximumTimeBetweenPings = getArgumentAt(invocation, 0, Integer.class);
return null;
}
}).when(mockGatewayHub).setMaximumTimeBetweenPings(anyInt());
when(mockGatewayHub.getMaximumTimeBetweenPings()).thenAnswer(new Answer<Integer>() {
@Override public Integer answer(final InvocationOnMock invocation) throws Throwable {
return maximumTimeBetweenPings;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
socketBufferSize = getArgumentAt(invocation, 0, Integer.class);
return null;
}
}).when(mockGatewayHub).setSocketBufferSize(anyInt());
when(mockGatewayHub.getSocketBufferSize()).thenAnswer(new Answer<Integer>() {
@Override public Integer answer(final InvocationOnMock invocation) throws Throwable {
return socketBufferSize;
}
});
doAnswer(new Answer<Void>() {
@Override public Void answer(final InvocationOnMock invocation) throws Throwable {
startupPolicy = getArgumentAt(invocation, 0, String.class);
return null;
}
}).when(mockGatewayHub).setStartupPolicy(anyString());
when(mockGatewayHub.getStartupPolicy()).thenAnswer(new Answer<String>() {
@Override public String answer(final InvocationOnMock invocation) throws Throwable {
return startupPolicy;
}
});
when(mockGatewayHub.addGateway(anyString())).thenAnswer(new Answer<Gateway>() {
@Override public Gateway answer(final InvocationOnMock invocation) throws Throwable {
String id = getArgumentAt(invocation, 0, String.class);
return mockGatewayHub.addGateway(id, Gateway.DEFAULT_CONCURRENCY_LEVEL);
}
});
when(mockGatewayHub.addGateway(anyString(), anyInt())).thenAnswer(new Answer<Gateway>() {
@Override public Gateway answer(final InvocationOnMock invocation) throws Throwable {
String id = getArgumentAt(invocation, 0, String.class);
Integer concurrencyLevel = getArgumentAt(invocation, 1, Integer.class);
Gateway mockGateway = new MockGatewayFactory().mockGateway(id, concurrencyLevel);
when(mockGateway.getGatewayHubId()).thenReturn(id);
gateways.add(mockGateway);
return mockGateway;
}
});
return mockGatewayHub;
}
private List<String> getGatewayIds(final List<Gateway> gateways) {
List<String> gatewayIds = new ArrayList<String>(gateways.size());
for (Gateway gateway : gateways) {
gatewayIds.add(gateway.getId());
}
return gatewayIds;
}
}

View File

@@ -411,8 +411,11 @@ public class StubCache implements Cache {
* @see com.gemstone.gemfire.cache.Cache#addGatewayHub(java.lang.String, int)
*/
@Override
public GatewayHub addGatewayHub(String name, int port) {
return mockGatewayHub();
public GatewayHub addGatewayHub(String id, int port) {
GatewayHub gatewayHub = getGatewayHub(id);
gatewayHub = (gatewayHub != null ? gatewayHub : new MockGatewayHubFactory().mockGatewayHub(id, port));
gatewayHubs.add(gatewayHub);
return gatewayHub;
}
/* (non-Javadoc)
@@ -557,15 +560,21 @@ public class StubCache implements Cache {
@Override
@Deprecated
public GatewayHub getGatewayHub() {
return mockGatewayHub();
return (gatewayHubs.isEmpty() ? null : gatewayHubs.get(0));
}
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.Cache#getGatewayHub(java.lang.String)
*/
@Override
public GatewayHub getGatewayHub(String name) {
return mockGatewayHub();
public GatewayHub getGatewayHub(String id) {
for (GatewayHub gatewayHub : gatewayHubs) {
if (gatewayHub.getId().equals(id)) {
return gatewayHub;
}
}
return null;
}
/* (non-Javadoc)

View File

@@ -0,0 +1,38 @@
/*
* 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.test.support;
import java.util.concurrent.atomic.AtomicLong;
/**
* The IdentifierSequence class is an Identifier (ID) generator generating unique IDs in sequence.
*
* @author John Blum
* @see java.lang.System#currentTimeMillis()
* @see java.util.concurrent.atomic.AtomicLong
* @since 1.5.3
*/
@SuppressWarnings("unused")
public abstract class IdentifierSequence {
private static final AtomicLong ID_SEQUENCE = new AtomicLong(System.currentTimeMillis());
public static long nextId() {
return ID_SEQUENCE.incrementAndGet();
}
}

View File

@@ -0,0 +1,79 @@
/*
* 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.test.support;
/**
* The StackTraceUtils class is a utility class for working with stack trace frames (elements) of the current Thread.
*
* @author John Blum
* @see java.lang.StackTraceElement
* @see java.lang.Thread
* @see org.springframework.data.gemfire.test.support.ThreadUtils
* @since 1.5.3
*/
@SuppressWarnings("unused")
public abstract class StackTraceUtils extends ThreadUtils {
public static StackTraceElement getCaller() {
return getCaller(Thread.currentThread());
}
public static StackTraceElement getCaller(final Thread thread) {
return thread.getStackTrace()[2];
}
public static String getCallerName(final StackTraceElement element) {
return String.format("%1$%s.%2$s", element.getClass().getName(), element.getMethodName());
}
public static String getCallerSimpleName(final StackTraceElement element) {
return String.format("%1$%s.%2$s", element.getClass().getSimpleName(), element.getMethodName());
}
public static StackTraceElement getTestCaller() {
return getTestCaller(Thread.currentThread());
}
public static StackTraceElement getTestCaller(final Thread thread) {
for (StackTraceElement stackTraceElement : thread.getStackTrace()) {
if (isTestSuiteClass(stackTraceElement) && isTestCaseMethod(stackTraceElement)) {
return stackTraceElement;
}
}
return null;
}
private static boolean isTestCaseMethod(final StackTraceElement element) {
boolean result = element.getMethodName().toLowerCase().startsWith("test");
try {
result |= element.getClass().getMethod(element.getMethodName()).isAnnotationPresent(org.junit.Test.class);
}
catch (NoSuchMethodException ignore) {
}
return result;
}
private static boolean isTestSuiteClass(final StackTraceElement element) {
boolean result = element.getClass().getSimpleName().toLowerCase().endsWith("test");
result |= element.getClass().isAssignableFrom(junit.framework.TestCase.class);
return result;
}
}

View File

@@ -0,0 +1,73 @@
/*
* 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.test.support;
import java.util.concurrent.TimeUnit;
/**
* The ThreadUtils class is a utility class for working with Java Threads.
*
* @author John Blum
* @see java.lang.Thread
* @since 1.5.0
*/
@SuppressWarnings("unused")
public abstract class ThreadUtils {
public static boolean sleep(final long milliseconds) {
try {
Thread.sleep(milliseconds);
return true;
}
catch (InterruptedException ignore) {
return false;
}
}
public static void timedWait(final long milliseconds) {
timedWait(milliseconds, milliseconds);
}
public static void timedWait(final long milliseconds, final long interval) {
timedWait(milliseconds, interval, new WaitCondition() {
@Override public boolean waiting() {
return true;
}
});
}
public static void timedWait(final long milliseconds, long interval, final WaitCondition waitCondition) {
final long timeout = (System.currentTimeMillis() + milliseconds);
interval = Math.min(interval, milliseconds);
while (waitCondition.waiting() && (System.currentTimeMillis() < timeout)) {
try {
synchronized (waitCondition) {
TimeUnit.MILLISECONDS.timedWait(waitCondition, interval);
}
}
catch (InterruptedException ignore) {
}
}
}
public static interface WaitCondition {
boolean waiting();
}
}

View File

@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:gfe="http://www.springframework.org/schema/gemfire"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
">
<util:properties id="gemfireProperties">
<prop key="name">GatewayHubNamespaceTest</prop>
<prop key="mcast-port">0</prop>
<prop key="log-level">warning</prop>
</util:properties>
<gfe:cache properties-ref="gemfireProperties"/>
<gfe:replicated-region id="Example" persistent="false" enable-gateway="true" hub-id="testGatewayHub"/>
<gfe:disk-store id="TestGatewayQueueDiskStore" auto-compact="true" allow-force-compaction="true" queue-size="50">
<gfe:disk-dir location="./gemfire/GatewayHubNamespaceTest/Gateway/Queue"/>
</gfe:disk-store>
<bean name="ListenerTwo" class="org.springframework.data.gemfire.config.GatewayHubNamespaceTest.TestGatewayListener" p:name="ListenerTwo"/>
<gfe:gateway-hub id="TestGatewayHub" bind-address="localhost" manual-start="true" max-time-between-pings="5000"
port="45123" socket-buffer-size="16384" startup-policy="primary">
<gfe:gateway gateway-id="gateway1" concurrency-level="8" order-policy="thread" socket-buffer-size="65536">
<!-- socket-read-timeout="75000">-->
<gfe:gateway-listener>
<bean class="org.springframework.data.gemfire.config.GatewayHubNamespaceTest.TestGatewayListener" p:name="ListenerOne"/>
<ref bean="ListenerTwo"/>
</gfe:gateway-listener>
<gfe:gateway-queue alert-threshold="99" enable-batch-conflation="true" batch-size="3" batch-time-interval="10"
disk-store-ref="TestGatewayQueueDiskStore" maximum-queue-memory="5" persistent="false"/>
</gfe:gateway>
<gfe:gateway gateway-id="gateway2">
<gfe:gateway-endpoint endpoint-id="endpoint1" host="localhost" port="1234"/>
<gfe:gateway-endpoint endpoint-id="endpoint2" host="localhost" port="4321"/>
<gfe:gateway-queue batch-size="6" batch-time-interval="20" enable-batch-conflation="false" persistent="true"/>
</gfe:gateway>
<gfe:gateway gateway-id="gateway3">
<gfe:gateway-listener ref="ListenerTwo"/>
</gfe:gateway>
</gfe:gateway-hub>
</beans>