You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ds...@apache.org on 2019/01/14 22:41:04 UTC

[geode] branch feature/GEODE-6272 updated: added createStablePdxInstanceFactory APIs and unit tests

This is an automated email from the ASF dual-hosted git repository.

dschneider pushed a commit to branch feature/GEODE-6272
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/feature/GEODE-6272 by this push:
     new 91c4fd3  added createStablePdxInstanceFactory APIs and unit tests
91c4fd3 is described below

commit 91c4fd3bd2ad471b97fe66d5c64bcd4e20952011
Author: Darrel Schneider <ds...@pivotal.io>
AuthorDate: Mon Jan 14 14:40:12 2019 -0800

    added createStablePdxInstanceFactory APIs and unit tests
---
 .../geode/pdx/PdxInstanceFactoryJUnitTest.java     | 85 ++++++++++++++++++++++
 .../java/org/apache/geode/cache/RegionService.java | 16 ++++
 .../geode/cache/client/internal/ProxyCache.java    |  5 ++
 .../geode/internal/cache/GemFireCacheImpl.java     |  5 ++
 .../cache/InternalCacheForClientAccess.java        |  5 ++
 .../internal/cache/xmlcache/CacheCreation.java     |  5 ++
 6 files changed, 121 insertions(+)

diff --git a/geode-core/src/integrationTest/java/org/apache/geode/pdx/PdxInstanceFactoryJUnitTest.java b/geode-core/src/integrationTest/java/org/apache/geode/pdx/PdxInstanceFactoryJUnitTest.java
index 4a6d90d..07f1874 100644
--- a/geode-core/src/integrationTest/java/org/apache/geode/pdx/PdxInstanceFactoryJUnitTest.java
+++ b/geode-core/src/integrationTest/java/org/apache/geode/pdx/PdxInstanceFactoryJUnitTest.java
@@ -15,6 +15,8 @@
 package org.apache.geode.pdx;
 
 import static org.apache.geode.distributed.ConfigurationProperties.MCAST_PORT;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
@@ -1227,4 +1229,87 @@ public class PdxInstanceFactoryJUnitTest {
     return DataSerializer.readObject(new DataInputStream(new ByteArrayInputStream(bytes)));
   }
 
+  @Test
+  public void stablePdxInstanceGetObjectReturnsThePdxInstance() {
+    PdxInstanceFactory factory = cache.createStablePdxInstanceFactory("myPdxInstanceType");
+    PdxInstance instance = factory.create();
+
+    Object object = instance.getObject();
+
+    assertThat(object).isSameAs(instance);
+  }
+
+  @Test
+  public void normalPdxInstanceGetObjectThrowsClassNotFoundGivenABadClass() {
+    PdxInstanceFactory factory = cache.createPdxInstanceFactory("badClass");
+    PdxInstance instance = factory.create();
+
+    assertThatThrownBy(() -> instance.getObject()).isInstanceOf(PdxSerializationException.class)
+        .hasCauseInstanceOf(ClassNotFoundException.class);
+  }
+
+  @Test
+  public void stablePdxInstanceAddedToRegionWithPdxReadSerializedFalseReturnsEqualPdxInstanceWhenRegionGet() {
+    // make sure the cache has pdx-read-serialized set to false
+    this.cache.close();
+    this.cache = (GemFireCacheImpl) new CacheFactory().set(MCAST_PORT, "0")
+        .setPdxReadSerialized(false).create();
+    PdxInstanceFactory factory = cache.createStablePdxInstanceFactory("myPdxInstanceType");
+    factory.writeString("fieldOne", "valueOne");
+    PdxInstance instance = factory.create();
+    Region region = cache.createRegionFactory(RegionShortcut.PARTITION).create("myRegion");
+    region.put("key", instance);
+
+    Object getValue = region.get("key");
+
+    assertThat(getValue).isEqualTo(instance);
+  }
+
+  @Test
+  public void stablePdxInstanceCanBeUsedAsRegionKey() {
+    // make sure the cache has pdx-read-serialized set to false
+    this.cache.close();
+    this.cache = (GemFireCacheImpl) new CacheFactory().set(MCAST_PORT, "0")
+        .setPdxReadSerialized(false).create();
+    PdxInstanceFactory factory = cache.createStablePdxInstanceFactory("myPdxInstanceType");
+    factory.writeString("fieldOne", "valueOne");
+    PdxInstance putKey = factory.create();
+    Region region = cache.createRegionFactory(RegionShortcut.PARTITION).create("myRegion");
+    region.put(putKey, "value");
+    factory = cache.createStablePdxInstanceFactory("myPdxInstanceType");
+    factory.writeString("fieldOne", "valueOne");
+    PdxInstance getKey = factory.create();
+
+    Object getValue = region.get(getKey);
+
+    assertThat(getValue).isEqualTo("value");
+  }
+
+  @Test
+  public void stablePdxInstanceWithDifferentTypeNameAreNotEqual() {
+    PdxInstanceFactory factory = cache.createStablePdxInstanceFactory("myPdxInstanceType");
+    factory.writeString("fieldOne", "valueOne");
+    PdxInstance instance = factory.create();
+    factory = cache.createStablePdxInstanceFactory("myPdxInstanceType2");
+    factory.writeString("fieldOne", "valueOne");
+    PdxInstance instance2 = factory.create();
+
+    assertThat(instance).isNotEqualTo(instance2);
+  }
+
+  @Test
+  public void normalPdxInstanceAddedToRegionWithPdxReadSerializedFalseAndABadClassThrowsClassNotFoundWhenRegionGet() {
+    // make sure the cache has pdx-read-serialized set to false
+    this.cache.close();
+    this.cache = (GemFireCacheImpl) new CacheFactory().set(MCAST_PORT, "0")
+        .setPdxReadSerialized(false).create();
+    PdxInstanceFactory factory = cache.createPdxInstanceFactory("badClass");
+    PdxInstance instance = factory.create();
+    Region region = cache.createRegionFactory(RegionShortcut.PARTITION).create("myRegion");
+    region.put("key", instance);
+
+    assertThatThrownBy(() -> region.get("key")).isInstanceOf(PdxSerializationException.class)
+        .hasCauseInstanceOf(ClassNotFoundException.class);
+  }
+
 }
diff --git a/geode-core/src/main/java/org/apache/geode/cache/RegionService.java b/geode-core/src/main/java/org/apache/geode/cache/RegionService.java
index aa79ba5..779a80f 100644
--- a/geode-core/src/main/java/org/apache/geode/cache/RegionService.java
+++ b/geode-core/src/main/java/org/apache/geode/cache/RegionService.java
@@ -81,6 +81,22 @@ public interface RegionService extends AutoCloseable {
   PdxInstanceFactory createPdxInstanceFactory(String className);
 
   /**
+   * Returns a factory that can create a "stable" {@link PdxInstance}.
+   * A "stable" PdxInstance is one that will always deserialize to itself
+   * instead of to a domain class.
+   *
+   * @param className since stable PDX instances do not have a domain class, this string
+   *        does not need to be the name of an actual class. It will be returned when
+   *        PdxInstance.getClassName is called.
+   *        This "class name" will be used to identify the type of the PDX instance. Two PDX
+   *        instances are only equals
+   *        if they have the same "class name".
+   * @return the factory
+   * @since Geode 1.9
+   */
+  PdxInstanceFactory createStablePdxInstanceFactory(String className);
+
+  /**
    * Creates and returns a PdxInstance that represents an enum value.
    *
    * @param className the name of the enum class
diff --git a/geode-core/src/main/java/org/apache/geode/cache/client/internal/ProxyCache.java b/geode-core/src/main/java/org/apache/geode/cache/client/internal/ProxyCache.java
index 324176d..084f5cd 100755
--- a/geode-core/src/main/java/org/apache/geode/cache/client/internal/ProxyCache.java
+++ b/geode-core/src/main/java/org/apache/geode/cache/client/internal/ProxyCache.java
@@ -220,6 +220,11 @@ public class ProxyCache implements RegionService {
     return PdxInstanceFactoryImpl.newCreator(className, true, cache);
   }
 
+  @Override
+  public PdxInstanceFactory createStablePdxInstanceFactory(String className) {
+    return PdxInstanceFactoryImpl.newCreator(className, false, cache);
+  }
+
   public PdxInstanceFactory createPdxInstanceFactory(String className, boolean expectDomainClass) {
     return PdxInstanceFactoryImpl.newCreator(className, expectDomainClass, cache);
   }
diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/GemFireCacheImpl.java b/geode-core/src/main/java/org/apache/geode/internal/cache/GemFireCacheImpl.java
index 63eb8c6..79a3fb9 100755
--- a/geode-core/src/main/java/org/apache/geode/internal/cache/GemFireCacheImpl.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/cache/GemFireCacheImpl.java
@@ -5206,6 +5206,11 @@ public class GemFireCacheImpl implements InternalCache, InternalClientCache, Has
   }
 
   @Override
+  public PdxInstanceFactory createStablePdxInstanceFactory(String className) {
+    return PdxInstanceFactoryImpl.newCreator(className, false, this);
+  }
+
+  @Override
   public PdxInstanceFactory createPdxInstanceFactory(String className, boolean expectDomainClass) {
     return PdxInstanceFactoryImpl.newCreator(className, expectDomainClass, this);
   }
diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/InternalCacheForClientAccess.java b/geode-core/src/main/java/org/apache/geode/internal/cache/InternalCacheForClientAccess.java
index 759aa1f..b5b6cb8 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/cache/InternalCacheForClientAccess.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/cache/InternalCacheForClientAccess.java
@@ -586,6 +586,11 @@ public class InternalCacheForClientAccess implements InternalCache {
   }
 
   @Override
+  public PdxInstanceFactory createStablePdxInstanceFactory(String className) {
+    return delegate.createStablePdxInstanceFactory(className);
+  }
+
+  @Override
   public PdxInstance createPdxEnum(String className, String enumName, int enumOrdinal) {
     return delegate.createPdxEnum(className, enumName, enumOrdinal);
   }
diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/xmlcache/CacheCreation.java b/geode-core/src/main/java/org/apache/geode/internal/cache/xmlcache/CacheCreation.java
index d571903..77cf62e 100755
--- a/geode-core/src/main/java/org/apache/geode/internal/cache/xmlcache/CacheCreation.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/cache/xmlcache/CacheCreation.java
@@ -1859,6 +1859,11 @@ public class CacheCreation implements InternalCache {
   }
 
   @Override
+  public PdxInstanceFactory createStablePdxInstanceFactory(String className) {
+    throw new UnsupportedOperationException("Should not be invoked");
+  }
+
+  @Override
   public PdxInstance createPdxEnum(String className, String enumName, int enumOrdinal) {
     throw new UnsupportedOperationException("Should not be invoked");
   }