geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hiteshkhame...@apache.org
Subject [2/2] geode git commit: GEODE-2271 JSOnFormatter can generate three pdxTypeId for one json field.
Date Mon, 09 Jan 2017 18:24:30 GMT
GEODE-2271 JSOnFormatter can generate three pdxTypeId for one json field.

JSON field can have 3 values(fieldValue, NULL, fieldNotExist), this
causes 3 pdxTypeIds. To reuduce this we merged first two values in in
one pdxType.

Added unit test for it


Project: http://git-wip-us.apache.org/repos/asf/geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/e2fa24ee
Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/e2fa24ee
Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/e2fa24ee

Branch: refs/heads/develop
Commit: e2fa24ee459b8121890008b6f8535da3d124038f
Parents: 602ac4f
Author: Hitesh Khamesra <hkhamesra@pivotal.io>
Authored: Wed Jan 4 16:05:14 2017 -0800
Committer: Hitesh Khamesra <hkhamesra@pivotal.io>
Committed: Mon Jan 9 10:25:23 2017 -0800

----------------------------------------------------------------------
 .../org/apache/geode/pdx/JSONFormatter.java     |  2 +-
 .../geode/pdx/internal/PdxReaderImpl.java       | 31 +++++++-
 .../pdx/internal/json/PdxInstanceHelper.java    |  4 +-
 .../java/org/apache/geode/pdx/Employee.java     | 48 ++++++++----
 .../geode/pdx/JSONFormatterJUnitTest.java       | 77 +++++++++++++++++++-
 .../apache/geode/pdx/PdxStringJUnitTest.java    | 27 +++++++
 .../geode/pdx/TestObjectForJSONFormatter.java   | 27 ++++++-
 7 files changed, 195 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode/blob/e2fa24ee/geode-core/src/main/java/org/apache/geode/pdx/JSONFormatter.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/pdx/JSONFormatter.java b/geode-core/src/main/java/org/apache/geode/pdx/JSONFormatter.java
index ff2ce04..a96e111 100755
--- a/geode-core/src/main/java/org/apache/geode/pdx/JSONFormatter.java
+++ b/geode-core/src/main/java/org/apache/geode/pdx/JSONFormatter.java
@@ -285,7 +285,7 @@ public class JSONFormatter {
           // write string
           stringFound(currentState);
           currentState = states.SCALER_FOUND;
-          currentPdxInstance.addStringField(currentFieldName, new String(jp.getText()));
+          currentPdxInstance.addObjectField(currentFieldName, new String(jp.getText()));
           currentFieldName = null;
           break;
         }

http://git-wip-us.apache.org/repos/asf/geode/blob/e2fa24ee/geode-core/src/main/java/org/apache/geode/pdx/internal/PdxReaderImpl.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/pdx/internal/PdxReaderImpl.java b/geode-core/src/main/java/org/apache/geode/pdx/internal/PdxReaderImpl.java
index 4ebc33d..822fc99 100644
--- a/geode-core/src/main/java/org/apache/geode/pdx/internal/PdxReaderImpl.java
+++ b/geode-core/src/main/java/org/apache/geode/pdx/internal/PdxReaderImpl.java
@@ -878,8 +878,37 @@ public class PdxReaderImpl implements InternalPdxReader, java.io.Serializable
{
     if (ft.getFieldType() == FieldType.STRING) {
       return readPdxString(ft);
     } else {
-      return readField(field);
+      PdxString pdxString = getPdxStringFromObjectField(ft);
+      if (pdxString != null)
+        return pdxString;
     }
+    return readField(field);
+  }
+
+  /**
+   * This method checks whether Object field is String type. If its String then it returns
PdxString
+   * otherwise null.
+   * 
+   * @param ft
+   * @return
+   */
+  private PdxString getPdxStringFromObjectField(PdxField ft) {
+    if (ft.getFieldType() == FieldType.OBJECT) {
+      ByteSource buffer = dis.getBuffer();
+      byte[] bytes = null;
+      if (buffer.hasArray()) {
+        bytes = buffer.array();
+      } else {
+        throw new IllegalStateException();
+      }
+      int offset = getPositionForField(ft) + buffer.arrayOffset();
+      // Do not create PdxString if the field is NULL
+      if (bytes[offset] == DSCODE.STRING || bytes[offset] == DSCODE.STRING_BYTES
+          || bytes[offset] == DSCODE.HUGE_STRING || bytes[offset] == DSCODE.HUGE_STRING_BYTES)
{
+        return new PdxString(bytes, offset);
+      }
+    }
+    return null;
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/geode/blob/e2fa24ee/geode-core/src/main/java/org/apache/geode/pdx/internal/json/PdxInstanceHelper.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/pdx/internal/json/PdxInstanceHelper.java
b/geode-core/src/main/java/org/apache/geode/pdx/internal/json/PdxInstanceHelper.java
index e957cd6..a91fbd4 100755
--- a/geode-core/src/main/java/org/apache/geode/pdx/internal/json/PdxInstanceHelper.java
+++ b/geode-core/src/main/java/org/apache/geode/pdx/internal/json/PdxInstanceHelper.java
@@ -149,12 +149,12 @@ public class PdxInstanceHelper {
     }
   }
 
-  public void addObjectField(String fieldName, PdxInstance member) {
+  public void addObjectField(String fieldName, Object member) {
     if (logger.isTraceEnabled()) {
       logger.trace("addObjectField fieldName: {}", fieldName);
     }
     if (fieldName == null)
-      throw new IllegalStateException("addObjectField:PdxInstance should have fieldname");
+      throw new IllegalStateException("addObjectField:Object should have fieldname");
     m_pdxInstanceFactory.writeObject(fieldName, member);
   }
 

http://git-wip-us.apache.org/repos/asf/geode/blob/e2fa24ee/geode-core/src/test/java/org/apache/geode/pdx/Employee.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/pdx/Employee.java b/geode-core/src/test/java/org/apache/geode/pdx/Employee.java
index cfb46b5..9cfc900 100644
--- a/geode-core/src/test/java/org/apache/geode/pdx/Employee.java
+++ b/geode-core/src/test/java/org/apache/geode/pdx/Employee.java
@@ -15,24 +15,24 @@
 package org.apache.geode.pdx;
 
 public class Employee implements PdxSerializable {
-  private Long Id;
+  private Long id;
   private String fname;
   private String lname;
 
   public Employee() {}
 
   public Employee(Long id, String fn, String ln) {
-    this.Id = id;
+    this.id = id;
     this.fname = fn;
     this.lname = ln;
   }
 
   public Long getId() {
-    return Id;
+    return id;
   }
 
   public void setId(Long id) {
-    Id = id;
+    this.id = id;
   }
 
   public String getFname() {
@@ -53,19 +53,34 @@ public class Employee implements PdxSerializable {
 
   @Override
   public void fromData(PdxReader in) {
-    this.Id = in.readLong("Id");
+    this.id = in.readLong("id");
     this.fname = in.readString("fname");
     this.lname = in.readString("lname");
   }
 
   @Override
   public void toData(PdxWriter out) {
-    out.writeLong("Id", Id);
+    out.writeLong("id", id);
     out.writeString("fname", fname);
     out.writeString("lname", lname);
   }
 
   @Override
+  public String toString() {
+    return "Employee [Id=" + id + ", fname=" + fname + ", lname=" + lname + "]";
+  }
+
+  @Override
+  public int hashCode() {
+    final int prime = 31;
+    int result = 1;
+    result = prime * result + ((fname == null) ? 0 : fname.hashCode());
+    result = prime * result + ((id == null) ? 0 : id.hashCode());
+    result = prime * result + ((lname == null) ? 0 : lname.hashCode());
+    return result;
+  }
+
+  @Override
   public boolean equals(Object obj) {
     if (this == obj)
       return true;
@@ -73,17 +88,24 @@ public class Employee implements PdxSerializable {
       return false;
     if (getClass() != obj.getClass())
       return false;
-
     Employee other = (Employee) obj;
-    if (Id.longValue() != other.Id.longValue())
+    if (fname == null) {
+      if (other.fname != null)
+        return false;
+    } else if (!fname.equals(other.fname))
       return false;
-
-    if (!fname.equals(other.fname))
+    if (id == null) {
+      if (other.id != null)
+        return false;
+    } else if (!id.equals(other.id))
       return false;
-
-    if (!lname.equals(other.lname))
+    if (lname == null) {
+      if (other.lname != null)
+        return false;
+    } else if (!lname.equals(other.lname))
       return false;
-
     return true;
   }
+
+
 }

http://git-wip-us.apache.org/repos/asf/geode/blob/e2fa24ee/geode-core/src/test/java/org/apache/geode/pdx/JSONFormatterJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/pdx/JSONFormatterJUnitTest.java b/geode-core/src/test/java/org/apache/geode/pdx/JSONFormatterJUnitTest.java
index 979da13..4ad3906 100755
--- a/geode-core/src/test/java/org/apache/geode/pdx/JSONFormatterJUnitTest.java
+++ b/geode-core/src/test/java/org/apache/geode/pdx/JSONFormatterJUnitTest.java
@@ -40,6 +40,8 @@ import org.apache.geode.cache.RegionAttributes;
 import org.apache.geode.cache.server.CacheServer;
 import org.apache.geode.internal.Assert;
 import org.apache.geode.internal.cache.GemFireCacheImpl;
+import org.apache.geode.pdx.internal.PdxInstanceImpl;
+import org.apache.geode.pdx.internal.PeerTypeRegistration;
 import org.apache.geode.test.junit.categories.IntegrationTest;
 
 @Category({IntegrationTest.class, SerializationTest.class})
@@ -55,9 +57,6 @@ public class JSONFormatterJUnitTest {
 
     // start cache-server
     CacheServer server = c.addCacheServer();
-    final int serverPort = 40405;
-    server.setPort(serverPort);
-    server.start();
 
     // Create region, primitiveKVStore
     final AttributesFactory<Object, Object> af1 = new AttributesFactory<Object,
Object>();
@@ -118,6 +117,7 @@ public class JSONFormatterJUnitTest {
       String json;
       try {
         json = objectMapper.writeValueAsString(expectedTestObject);
+
         String jsonWithClassType = expectedTestObject.addClassTypeToJson(json);
 
         // 3. Get PdxInstance from the Json String and Validate pi.getObject() API.
@@ -129,6 +129,7 @@ public class JSONFormatterJUnitTest {
         // and hence actualPI.equals(expectedPI) will returns false.
 
         Object actualTestObject = actualPI.getObject();
+
         if (actualTestObject instanceof TestObjectForJSONFormatter) {
           boolean isObjectEqual = actualTestObject.equals(expectedTestObject);
           Assert.assertTrue(isObjectEqual,
@@ -146,6 +147,47 @@ public class JSONFormatterJUnitTest {
     }
   }
 
+  // Testcase-2: validate Json->PdxInstance-->Java conversion
+  private void verifyJsonToPdxInstanceConversionWithJSONFormatter() {
+    TestObjectForJSONFormatter expectedTestObject = new TestObjectForJSONFormatter();
+    expectedTestObject.defaultInitialization();
+    Cache c = CacheFactory.getAnyInstance();
+    Region region = c.getRegion("primitiveKVStore");
+
+    // 1.gets pdxInstance using R.put() and R.get()
+    region.put("501", expectedTestObject);
+    Object receivedObject = region.get("501");
+    assertEquals("receivedObject is expected to be of type PdxInstance", PdxInstanceImpl.class,
+        receivedObject.getClass());
+
+    PdxInstance expectedPI = (PdxInstance) receivedObject;
+
+    String json;
+    try {
+      json = JSONFormatter.toJSON(expectedPI);
+
+      String jsonWithClassType = expectedTestObject.addClassTypeToJson(json);
+
+      // 3. Get PdxInstance from the Json String and Validate pi.getObject() API.
+      PdxInstance actualPI = JSONFormatter.fromJSON(jsonWithClassType);
+      // Note: expectedPI will contains those fields that are part of toData()
+      // expectedPI.className = "org.apache.geode.pdx.TestObjectForJSONFormatter"
+      // actualPI will contains all the fields that are member of the class.
+      // actualPI..className = __GEMFIRE_JSON
+      // and hence actualPI.equals(expectedPI) will returns false.
+
+      Object actualTestObject = actualPI.getObject();
+
+      assertEquals("receivedObject is expected to be of type PdxInstance",
+          TestObjectForJSONFormatter.class, actualTestObject.getClass());
+
+      assertEquals("actualTestObject and expectedTestObject should be equal", expectedTestObject,
+          actualTestObject);
+    } catch (JSONException e) {
+      fail("JSONException occurred:" + e.getMessage());
+    }
+  }
+
   private void verifyJsonWithJavaObject(String json, TestObjectForJSONFormatter testObject)
{
     try {
       JSONObject jsonObject = new JSONObject(json);
@@ -208,6 +250,35 @@ public class JSONFormatterJUnitTest {
   public void testJSONFormatterAPIs() {
     ValidatePdxInstanceToJsonConversion();
     verifyJsonToPdxInstanceConversion();
+    verifyJsonToPdxInstanceConversionWithJSONFormatter();
+  }
+
+  /**
+   * this test validates json document, where field has value and null Then it verifies we
create
+   * only one pdx type id for that
+   */
+  @Test
+  public void testJSONStringAsPdxObject() {
+
+    Cache c = CacheFactory.getAnyInstance();
+
+    int pdxTypes = 0;
+
+    if (c.getRegion(PeerTypeRegistration.REGION_FULL_PATH) != null) {
+      pdxTypes = c.getRegion(PeerTypeRegistration.REGION_FULL_PATH).keys().size();
+    }
+
+    Region region = c.getRegion("primitiveKVStore");
+
+    String js = "{name:\"ValueExist\", age:14}";
+
+    region.put(1, JSONFormatter.fromJSON(js));
+
+    String js2 = "{name:null, age:14}";
+
+    region.put(2, JSONFormatter.fromJSON(js2));
+
+    assertEquals(pdxTypes + 1, c.getRegion(PeerTypeRegistration.REGION_FULL_PATH).keys().size());
   }
 }
 

http://git-wip-us.apache.org/repos/asf/geode/blob/e2fa24ee/geode-core/src/test/java/org/apache/geode/pdx/PdxStringJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/pdx/PdxStringJUnitTest.java b/geode-core/src/test/java/org/apache/geode/pdx/PdxStringJUnitTest.java
index c072e14..c80a3f8 100644
--- a/geode-core/src/test/java/org/apache/geode/pdx/PdxStringJUnitTest.java
+++ b/geode-core/src/test/java/org/apache/geode/pdx/PdxStringJUnitTest.java
@@ -157,6 +157,33 @@ public class PdxStringJUnitTest {
 
   }
 
+  /**
+   * this test verifies that pdxstream with pdxType.Object tyep for string and checks whether
we
+   * create PdxString or not
+   * 
+   * @throws Exception
+   */
+  @Test
+  public void testJSONFieldNameAsPdxString() throws Exception {
+    String verifyString = "ValueExist";
+    String jsonString = "{name:\"" + verifyString + "\", age:14}";
+    PdxString pdx = new PdxString(verifyString);
+    assertEquals(verifyString, pdx.toString());
+
+    PdxInstanceImpl pi = (PdxInstanceImpl) JSONFormatter.fromJSON(jsonString);
+    pdx = (PdxString) pi.getRawField("name");
+    assertEquals(verifyString, pdx.toString());
+  }
+
+  @Test
+  public void testJSONFieldNameAsNull() throws Exception {
+    String verifyString = null;
+    String jsonString = "{name:null, age:14}";
+
+    PdxInstanceImpl pi = (PdxInstanceImpl) JSONFormatter.fromJSON(jsonString);
+    PdxString pdx = (PdxString) pi.getRawField("name");
+    assertEquals(verifyString, pdx);
+  }
 
 
 }

http://git-wip-us.apache.org/repos/asf/geode/blob/e2fa24ee/geode-core/src/test/java/org/apache/geode/pdx/TestObjectForJSONFormatter.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/pdx/TestObjectForJSONFormatter.java
b/geode-core/src/test/java/org/apache/geode/pdx/TestObjectForJSONFormatter.java
index ca0abc3..5d85d66 100755
--- a/geode-core/src/test/java/org/apache/geode/pdx/TestObjectForJSONFormatter.java
+++ b/geode-core/src/test/java/org/apache/geode/pdx/TestObjectForJSONFormatter.java
@@ -113,7 +113,7 @@ public class TestObjectForJSONFormatter implements PdxSerializable {
     Employee e3 = new Employee(3L, "Shankar", "Hundekar");
     Employee e4 = new Employee(4L, "Avinash", "Dongre");
     Employee e5 = new Employee(5L, "supriya", "Patil");
-    Employee e6 = new Employee(6L, "Rajesh", "Kumar");
+    Employee e6 = new Employee(6L, "Rajesh", null);
     Employee e7 = new Employee(7L, "Vishal", "Rao");
     Employee e8 = new Employee(8L, "Hitesh", "Khamesara");
     Employee e9 = new Employee(9L, "Sudhir", "Menon");
@@ -990,6 +990,31 @@ public class TestObjectForJSONFormatter implements PdxSerializable {
         return false;
     return true;
   }
+
+  @Override
+  public String toString() {
+    return "TestObjectForJSONFormatter [p_bool=" + p_bool + ", p_byte=" + p_byte + ", p_short="
+        + p_short + ", p_int=" + p_int + ", p_long=" + p_long + ", p_float=" + p_float
+        + ", p_double=" + p_double + ", w_bool=" + w_bool + ", w_byte=" + w_byte + ", w_short="
+        + w_short + ", w_int=" + w_int + ", w_long=" + w_long + ", w_bigInt=" + w_bigInt
+        + ", w_float=" + w_float + ", w_bigDec=" + w_bigDec + ", w_double=" + w_double
+        + ", w_string=" + w_string + ", p_boolArray=" + Arrays.toString(p_boolArray)
+        + ", p_byteArray=" + Arrays.toString(p_byteArray) + ", p_shortArray="
+        + Arrays.toString(p_shortArray) + ", p_intArray=" + Arrays.toString(p_intArray)
+        + ", p_longArray=" + Arrays.toString(p_longArray) + ", p_floatArray="
+        + Arrays.toString(p_floatArray) + ", p_doubleArray=" + Arrays.toString(p_doubleArray)
+        + ", w_boolArray=" + Arrays.toString(w_boolArray) + ", w_byteArray="
+        + Arrays.toString(w_byteArray) + ", w_shortArray=" + Arrays.toString(w_shortArray)
+        + ", w_intArray=" + Arrays.toString(w_intArray) + ", w_longArray="
+        + Arrays.toString(w_longArray) + ", w_bigIntArray=" + Arrays.toString(w_bigIntArray)
+        + ", w_floatArray=" + Arrays.toString(w_floatArray) + ", w_bigDecArray="
+        + Arrays.toString(w_bigDecArray) + ", w_doubleArray=" + Arrays.toString(w_doubleArray)
+        + ", w_strArray=" + Arrays.toString(w_strArray) + ", c_list=" + c_list + ", c_set="
+ c_set
+        + ", c_queue=" + c_queue + ", c_deque=" + c_deque + ", c_stack=" + c_stack
+        + ", m_empByCity=" + m_empByCity + ", day=" + day + ", employee=" + employee + "]";
+  }
+
+
 }
 
 


Mime
View raw message