blob: e2e5024ef6b3899feb1f1af8f2117a105a0976b9 [file] [log] [blame]
/*=========================================================================
* Copyright (c) 2010-2014 Pivotal Software, Inc. All Rights Reserved.
* This product is protected by U.S. and international copyright
* and intellectual property laws. Pivotal products are covered by
* one or more patents listed at http://www.pivotal.io/patents.
*=========================================================================
*/
package com.gemstone.gemfire.cache;
import java.io.File;
import java.util.Arrays;
import org.junit.experimental.categories.Category;
import junit.framework.TestCase;
import com.gemstone.gemfire.cache.util.BridgeLoader;
import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
import com.gemstone.gemfire.cache.util.CacheWriterAdapter;
import com.gemstone.gemfire.compression.SnappyCompressor;
import com.gemstone.gemfire.test.junit.categories.UnitTest;
/**
* Tests the functionality of the {@link AttributesFactory} class.
*
* @author David Whitlock
*
* @since 3.0
*/
@Category(UnitTest.class)
public class AttributesFactoryJUnitTest extends TestCase {
public AttributesFactoryJUnitTest(String name) {
super(name);
}
/////////////////////// Test Methods ///////////////////////
public void testCopyConstructor() {
AttributesFactory f1 = new AttributesFactory();
f1.setLockGrantor(true);
RegionAttributes origAttrs = f1.create();
assertEquals(true, origAttrs.isLockGrantor());
AttributesFactory f2 = new AttributesFactory(origAttrs);
RegionAttributes attrs = f2.create();
assertEquals(true, attrs.isLockGrantor());
}
/**
* Tests the {@link AttributesFactory#create} throws
* the appropriate exception with poorly-configured factory.
*/
public void testInvalidConfigurations() {
AttributesFactory factory;
ExpirationAttributes invalidate =
new ExpirationAttributes(1, ExpirationAction.LOCAL_INVALIDATE);
ExpirationAttributes destroy =
new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY);
// DataPolicy.REPLICATE is incompatible with
// ExpirationAction.LOCAL_INVALIDATE
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.REPLICATE);
factory.setEntryIdleTimeout(invalidate);
factory.setStatisticsEnabled(true);
{
RegionAttributes ra = factory.create();
assertEquals(DataPolicy.PRELOADED, ra.getDataPolicy());
assertEquals(new SubscriptionAttributes(InterestPolicy.ALL),
ra.getSubscriptionAttributes());
}
// DataPolicy.REPLICATE is incompatible with
// ExpirationAction.LOCAL_DESTROY.
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.REPLICATE);
factory.setEntryIdleTimeout(destroy);
factory.setStatisticsEnabled(true);
{
RegionAttributes ra = factory.create();
assertEquals(DataPolicy.PRELOADED, ra.getDataPolicy());
assertEquals(new SubscriptionAttributes(InterestPolicy.ALL),
ra.getSubscriptionAttributes());
}
// MirrorType KEYS is incompatible with
// ExpirationAction.LOCAL_INVALIDATE
factory = new AttributesFactory();
factory.setMirrorType(MirrorType.KEYS);
factory.setEntryIdleTimeout(destroy);
factory.setStatisticsEnabled(true);
{
RegionAttributes ra = factory.create();
assertEquals(DataPolicy.PRELOADED, ra.getDataPolicy());
assertEquals(new SubscriptionAttributes(InterestPolicy.ALL),
ra.getSubscriptionAttributes());
}
// MirrorType.KEYS are incompatible with
// ExpirationAction.LOCAL_DESTROY.
factory = new AttributesFactory();
factory.setMirrorType(MirrorType.KEYS);
factory.setEntryIdleTimeout(destroy);
factory.setStatisticsEnabled(true);
{
RegionAttributes ra = factory.create();
assertEquals(DataPolicy.PRELOADED, ra.getDataPolicy());
assertEquals(new SubscriptionAttributes(InterestPolicy.ALL),
ra.getSubscriptionAttributes());
}
// Entry idle expiration requires that
// statistics are enabled
factory = new AttributesFactory();
factory.setEntryIdleTimeout(destroy);
factory.setStatisticsEnabled(false);
try {
factory.create();
fail("Should have thrown an IllegalStateException");
} catch (Exception ex) {
assertTrue(ex instanceof IllegalStateException);
// pass...
}
// Entry ttl expiration requires that
// statistics are enabled
factory = new AttributesFactory();
factory.setEntryTimeToLive(destroy);
factory.setStatisticsEnabled(false);
try {
factory.create();
fail("Should have thrown an IllegalStateException");
} catch (Exception ex) {
assertTrue(ex instanceof IllegalStateException);
// pass...
}
// MembershipAttributes (required roles)
// requires distributed scope
factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
MembershipAttributes ra = new MembershipAttributes(new String[] {"A"});
factory.setMembershipAttributes(ra);
try {
factory.create();
fail("Should have thrown an IllegalStateException");
} catch (Exception ex) {
assertTrue(ex instanceof IllegalStateException);
// pass...
}
// Used mixed mode API for disk store and DWA
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
factory.setDiskStoreName("ds1");
DiskWriteAttributesFactory dwaf = new DiskWriteAttributesFactory();
try {
factory.setDiskWriteAttributes(dwaf.create());
fail("Should have thrown an IllegalStateException");
} catch (Exception ex) {
assertTrue(ex instanceof IllegalStateException);
// pass...
}
// Used mixed mode API for disk store and DWA
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
DiskWriteAttributesFactory dwaf2 = new DiskWriteAttributesFactory();
factory.setDiskWriteAttributes(dwaf2.create());
try {
factory.setDiskStoreName("ds1");
fail("Should have thrown an IllegalStateException");
} catch (Exception ex) {
assertTrue(ex instanceof IllegalStateException);
// pass...
}
// Used mixed mode API for disk store and DWA
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
File[] dirs1 = new File[] {new File("").getAbsoluteFile()};
factory.setDiskStoreName("ds1");
try {
factory.setDiskDirs(dirs1);
fail("Should have thrown an IllegalStateException");
} catch (Exception ex) {
assertTrue(ex instanceof IllegalStateException);
// pass...
}
// Used mixed mode API for disk store and DWA
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
File[] dirs2 = new File[] {new File("").getAbsoluteFile()};
factory.setDiskDirs(dirs2);
try {
factory.setDiskStoreName("ds1");
fail("Should have thrown an IllegalStateException");
} catch (Exception ex) {
assertTrue(ex instanceof IllegalStateException);
// pass...
}
// Cloning cannot be disabled when a compressor is set
factory = new AttributesFactory();
factory.setCompressor(SnappyCompressor.getDefaultInstance());
factory.setCloningEnabled(false);
try {
RegionAttributes ccra = factory.create();
fail("Should have thrown an IllegalStateException");
} catch (IllegalStateException expected) {
// Expected
}
}
/**
* Tests that the {@link AttributesFactory#AttributesFactory()
* default} attributes factory has the advertised default
* configuration.
*/
public void testDefaultConfiguration() {
AttributesFactory factory = new AttributesFactory();
RegionAttributes attrs = factory.create();
assertNull(attrs.getCacheLoader());
assertNull(attrs.getCacheWriter());
assertNull(attrs.getCacheListener());
assertEquals(Arrays.asList(new CacheListener[0]), Arrays.asList(attrs.getCacheListeners()));
assertEquals(0, attrs.getRegionTimeToLive().getTimeout());
assertEquals(0, attrs.getRegionIdleTimeout().getTimeout());
assertEquals(0, attrs.getEntryTimeToLive().getTimeout());
assertEquals(null, attrs.getCustomEntryTimeToLive());
assertEquals(0, attrs.getEntryIdleTimeout().getTimeout());
assertEquals(null, attrs.getCustomEntryIdleTimeout());
assertEquals(Scope.DISTRIBUTED_NO_ACK, attrs.getScope());
assertEquals(DataPolicy.DEFAULT, attrs.getDataPolicy());
assertEquals(InterestPolicy.DEFAULT, attrs.getSubscriptionAttributes().getInterestPolicy());
assertEquals(MirrorType.NONE, attrs.getMirrorType());
assertEquals(null, attrs.getDiskStoreName());
assertEquals(AttributesFactory.DEFAULT_DISK_SYNCHRONOUS, attrs.isDiskSynchronous());
assertNull(attrs.getKeyConstraint());
assertEquals(16, attrs.getInitialCapacity());
assertEquals(0.75, attrs.getLoadFactor(), 0.0);
assertFalse(attrs.getStatisticsEnabled());
assertFalse(attrs.getPersistBackup());
DiskWriteAttributes dwa = attrs.getDiskWriteAttributes();
assertNotNull(dwa);
{
DiskWriteAttributesFactory dwaf = new DiskWriteAttributesFactory();
dwaf.setSynchronous(AttributesFactory.DEFAULT_DISK_SYNCHRONOUS);
assertEquals(dwaf.create(), dwa);
}
assertNull(attrs.getDiskStoreName());
File[] diskDirs = attrs.getDiskDirs();
assertNotNull(diskDirs);
assertEquals(1, diskDirs.length);
assertEquals(new File("."), diskDirs[0]);
int[] diskSizes = attrs.getDiskDirSizes();
assertNotNull(diskSizes);
assertEquals(1, diskSizes.length);
assertEquals(DiskStoreFactory.DEFAULT_DISK_DIR_SIZE, diskSizes[0]);
}
public void testDiskSynchronous() {
{
AttributesFactory factory = new AttributesFactory();
factory.setDiskSynchronous(true);
RegionAttributes attrs = factory.create();
assertEquals(true, attrs.isDiskSynchronous());
assertEquals(true, attrs.getDiskWriteAttributes().isSynchronous());
}
{
AttributesFactory factory = new AttributesFactory();
factory.setDiskSynchronous(false);
RegionAttributes attrs = factory.create();
assertEquals(false, attrs.isDiskSynchronous());
assertEquals(false, attrs.getDiskWriteAttributes().isSynchronous());
}
// Test backwards compat interaction with diskSync.
// If the old apis are used then we should get the old default of async.
{
DiskWriteAttributesFactory dwaf = new DiskWriteAttributesFactory();
AttributesFactory factory = new AttributesFactory();
factory.setDiskWriteAttributes(dwaf.create());
RegionAttributes attrs = factory.create();
assertEquals(false, attrs.getDiskWriteAttributes().isSynchronous());
assertEquals(false, attrs.isDiskSynchronous());
}
{
AttributesFactory factory = new AttributesFactory();
factory.setDiskDirs(new File[] {new File("").getAbsoluteFile()});
RegionAttributes attrs = factory.create();
assertEquals(false, attrs.getDiskWriteAttributes().isSynchronous());
assertEquals(false, attrs.isDiskSynchronous());
}
{
AttributesFactory factory = new AttributesFactory();
factory.setDiskDirsAndSizes(new File[] {new File("").getAbsoluteFile()},
new int[] {100});
RegionAttributes attrs = factory.create();
assertEquals(false, attrs.getDiskWriteAttributes().isSynchronous());
assertEquals(false, attrs.isDiskSynchronous());
}
}
/**
* Tests the cacheListener functionality
* @since 5.0
*/
public void testCacheListeners() {
RegionAttributes ra;
CacheListener cl1 = new MyCacheListener();
CacheListener cl2 = new MyCacheListener();
assertFalse(cl1.equals(cl2));
assertFalse((Arrays.asList(new CacheListener[]{cl1, cl2})).equals(Arrays.asList(new CacheListener[]{cl2, cl1})));
AttributesFactory factory = new AttributesFactory();
try {
factory.addCacheListener(null);
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
factory.initCacheListeners(new CacheListener[]{null});
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
ra = factory.create();
assertEquals(null, ra.getCacheListener());
assertEquals(Arrays.asList(new CacheListener[0]), Arrays.asList(ra.getCacheListeners()));
factory.setCacheListener(cl1);
ra = factory.create();
assertEquals(cl1, ra.getCacheListener());
assertEquals(Arrays.asList(new CacheListener[]{cl1}), Arrays.asList(ra.getCacheListeners()));
factory.setCacheListener(cl2);
ra = factory.create();
assertEquals(cl2, ra.getCacheListener());
assertEquals(Arrays.asList(new CacheListener[]{cl2}), Arrays.asList(ra.getCacheListeners()));
factory.setCacheListener(null);
ra = factory.create();
assertEquals(null, ra.getCacheListener());
assertEquals(Arrays.asList(new CacheListener[0]), Arrays.asList(ra.getCacheListeners()));
factory.setCacheListener(cl1);
factory.initCacheListeners(new CacheListener[]{cl1, cl2});
ra = factory.create();
try {
ra.getCacheListener();
fail("expected IllegalStateException");
} catch (IllegalStateException expected) {
}
assertEquals(Arrays.asList(new CacheListener[]{cl1, cl2}), Arrays.asList(ra.getCacheListeners()));
factory.initCacheListeners(null);
ra = factory.create();
assertEquals(Arrays.asList(new CacheListener[0]), Arrays.asList(ra.getCacheListeners()));
factory.initCacheListeners(new CacheListener[0]);
ra = factory.create();
assertEquals(Arrays.asList(new CacheListener[0]), Arrays.asList(ra.getCacheListeners()));
factory.addCacheListener(cl1);
ra = factory.create();
assertEquals(Arrays.asList(new CacheListener[]{cl1}), Arrays.asList(ra.getCacheListeners()));
factory.addCacheListener(cl2);
ra = factory.create();
assertEquals(Arrays.asList(new CacheListener[]{cl1, cl2}), Arrays.asList(ra.getCacheListeners()));
factory.initCacheListeners(new CacheListener[]{cl2});
ra = factory.create();
assertEquals(Arrays.asList(new CacheListener[]{cl2}), Arrays.asList(ra.getCacheListeners()));
}
/**
* @since 5.7
*/
public void testConnectionPool() {
CacheLoader cl = new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
return null;
}
public void close() {}
};
AttributesFactory factory = new AttributesFactory();
factory.setPoolName("mypool");
factory = new AttributesFactory();
factory.setCacheWriter(new CacheWriterAdapter());
factory.setPoolName("mypool");
factory = new AttributesFactory();
factory.setCacheLoader(cl);
factory.setPoolName("mypool");
factory = new AttributesFactory();
factory.setCacheLoader(new BridgeLoader());
try {
factory.setPoolName("mypool");
fail("expected IllegalStateException");
} catch (IllegalStateException expected) {
}
factory = new AttributesFactory();
factory.setCacheLoader(new BridgeLoader());
try {
factory.setPoolName("mypool");
fail("expected IllegalStateException");
} catch (IllegalStateException expected) {
}
}
/**
* Trivial cache listener impl
* @since 5.0
*/
public static class MyCacheListener extends CacheListenerAdapter {
// empty impl
}
}