This is an automated email from the ASF dual-hosted git repository.
gosullivan pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git
The following commit(s) were added to refs/heads/develop by this push:
new 8dc364f GEODE-4653: Add JSONFormatterFailuresTest with simple
examples. (#1441)
8dc364f is described below
commit 8dc364f54cef8cce2ac53894a54e9083ce59fd4a
Author: Galen O'Sullivan <[email protected]>
AuthorDate: Fri Feb 16 09:07:39 2018 -0800
GEODE-4653: Add JSONFormatterFailuresTest with simple examples. (#1441)
* GEODE-4653: Add JSONFormatterFailuresTest with simple examples.
Also JSONFormatter test cleanup.
* Clean up the setUp method because it's old-fashioned and overly
complicated.
* Remove unneccessary comments, catch-and-fails, etc.
* Split `testJSONFormatterAPIs` into three tests.
* Have `JSONPdxClientServerDUnitTest` call
`JSONFormatterJUnitTest.verifyJsonWithJavaObject` rather than
duplicating code.
* Add the UnitTest category to the test.
---
.../geode/pdx/JSONFormatterBasicJUnitTest.java | 105 ++++++++
.../apache/geode/pdx/JSONFormatterJUnitTest.java | 297 +++++++++------------
.../geode/pdx/JSONPdxClientServerDUnitTest.java | 126 +++------
3 files changed, 256 insertions(+), 272 deletions(-)
diff --git
a/geode-core/src/test/java/org/apache/geode/pdx/JSONFormatterBasicJUnitTest.java
b/geode-core/src/test/java/org/apache/geode/pdx/JSONFormatterBasicJUnitTest.java
new file mode 100644
index 0000000..96f5e4c
--- /dev/null
+++
b/geode-core/src/test/java/org/apache/geode/pdx/JSONFormatterBasicJUnitTest.java
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional
information regarding
+ * copyright ownership. The ASF licenses this file to You 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.apache.geode.pdx;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.geode.cache.Cache;
+import org.apache.geode.cache.CacheFactory;
+import org.apache.geode.test.junit.categories.IntegrationTest;
+
+/**
+ * A test class to document and make clear what JSONFormatter will and won't
parse as far as simple
+ * examples.
+ */
+@Category(IntegrationTest.class)
+public class JSONFormatterBasicJUnitTest {
+ // This is needed because the JsonFormatter needs to access the PDX region,
which requires a
+ // running Cache.
+ private static Cache cache;
+
+ @BeforeClass
+ public static void setUp() throws Exception {
+ cache = new CacheFactory().create();
+ }
+
+ @AfterClass
+ public static void tearDown() {
+ cache.close();
+ }
+
+ @Test(expected = JSONFormatterException.class)
+ public void emptyListFailsToParse() {
+ JSONFormatter.fromJSON("[]");
+ }
+
+ @Test(expected = JSONFormatterException.class)
+ public void listOfASingleNumberFailsToParse() {
+ JSONFormatter.fromJSON("[1]");
+ }
+
+ @Test(expected = JSONFormatterException.class)
+ public void numberFailsToParse() {
+ JSONFormatter.fromJSON("1");
+ }
+
+ @Test(expected = JSONFormatterException.class)
+ public void emptyInputFailsToParse() {
+ JSONFormatter.fromJSON("");
+ }
+
+ @Test(expected = JSONFormatterException.class)
+ public void emptyInputInStringFailsToParse() {
+ JSONFormatter.fromJSON("\"\"");
+ }
+
+ @Test(expected = JSONFormatterException.class)
+ public void arbitraryInputFailsToParse() {
+ JSONFormatter.fromJSON("hi");
+ }
+
+ @Test(expected = JSONFormatterException.class)
+ public void simpleStringFailsToParse() {
+ JSONFormatter.fromJSON("\"hi\"");
+ }
+
+ @Test(expected = JSONFormatterException.class)
+ public void nullFailsToParse() {
+ JSONFormatter.fromJSON("null");
+ }
+
+ @Test(expected = JSONFormatterException.class)
+ public void falseFailsToParse() {
+ JSONFormatter.fromJSON("false");
+ }
+
+ @Test(expected = JSONFormatterException.class)
+ public void trueFailsToParse() {
+ JSONFormatter.fromJSON("true");
+ }
+
+ @Test
+ public void emptyObjectParses() {
+ JSONFormatter.fromJSON("{}");
+ }
+
+ @Test
+ public void simpleObjectParses() {
+ JSONFormatter.fromJSON("{\"a\": 2}");
+ }
+}
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 211ccc7..facf37c 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
@@ -14,8 +14,9 @@
*/
package org.apache.geode.pdx;
-import static org.apache.geode.distributed.ConfigurationProperties.*;
-import static org.junit.Assert.*;
+import static org.apache.geode.distributed.ConfigurationProperties.MCAST_PORT;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import java.text.SimpleDateFormat;
import java.util.List;
@@ -30,50 +31,36 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
-import org.apache.geode.cache.AttributesFactory;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.CacheFactory;
import org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.Region;
-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.FlakyTest;
import org.apache.geode.test.junit.categories.IntegrationTest;
import org.apache.geode.test.junit.categories.SerializationTest;
@Category({IntegrationTest.class, SerializationTest.class})
public class JSONFormatterJUnitTest {
-
- private GemFireCacheImpl c;
- private final String PRIMITIVE_KV_STORE_REGION = "primitiveKVStore";
+ public static final String REGION_NAME = "primitiveKVStore";
+ private Cache cache;
+ private Region<Object, Object> region;
@Before
public void setUp() throws Exception {
- this.c = (GemFireCacheImpl) new CacheFactory().set(MCAST_PORT,
"0").setPdxReadSerialized(true)
- .create();
-
- // Create region, primitiveKVStore
- final AttributesFactory<Object, Object> af1 = new
AttributesFactory<Object, Object>();
- af1.setDataPolicy(DataPolicy.PARTITION);
- final RegionAttributes<Object, Object> rAttributes = af1.create();
- c.createRegion(PRIMITIVE_KV_STORE_REGION, rAttributes);
+ this.cache = new CacheFactory().set(MCAST_PORT,
"0").setPdxReadSerialized(true).create();
+
+ region =
cache.createRegionFactory().setDataPolicy(DataPolicy.PARTITION).create(REGION_NAME);
+
}
@After
public void tearDown() {
- // shutdown and clean up the manager node.
- this.c.close();
+ this.cache.close();
}
- private void ValidatePdxInstanceToJsonConversion() {
-
- Cache c = CacheFactory.getAnyInstance();
- Region region = c.getRegion("primitiveKVStore");
-
+ @Test
+ public void ValidatePdxInstanceToJsonConversion() {
TestObjectForJSONFormatter actualTestObject = new
TestObjectForJSONFormatter();
actualTestObject.defaultInitialization();
@@ -82,75 +69,55 @@ public class JSONFormatterJUnitTest {
region.put("201", actualTestObject);
Object receivedObject = region.get("201");
- // PdxInstance->Json conversion
- if (receivedObject instanceof PdxInstance) {
- PdxInstance pi = (PdxInstance) receivedObject;
- String json = JSONFormatter.toJSON(pi);
+ assertTrue("receivedObject is expected to be of type PdxInstance",
+ receivedObject instanceof PdxInstance);
- verifyJsonWithJavaObject(json, actualTestObject);
- } else {
- fail("receivedObject is expected to be of type PdxInstance");
- }
+ // PdxInstance->Json conversion
+ PdxInstance pi = (PdxInstance) receivedObject;
+ String json = JSONFormatter.toJSON(pi);
+ verifyJsonWithJavaObject(json, actualTestObject);
}
- // Testcase-2: validate Json->PdxInstance-->Java conversion
- private void verifyJsonToPdxInstanceConversion() {
+ @Test
+ public void verifyJsonToPdxInstanceConversion() throws JSONException,
JsonProcessingException {
TestObjectForJSONFormatter expectedTestObject = new
TestObjectForJSONFormatter();
expectedTestObject.defaultInitialization();
- Cache c = CacheFactory.getAnyInstance();
- Region region = c.getRegion("primitiveKVStore");
- // 1.gets pdxInstance using R.put() and R.get()
+ // 1.gets pdxInstance using region.put() and region.get()
region.put("501", expectedTestObject);
Object receivedObject = region.get("501");
- if (receivedObject instanceof PdxInstance) {
- PdxInstance expectedPI = (PdxInstance) receivedObject;
-
- // 2. Get the JSON string from actualTestObject using jackson
ObjectMapper.
- ObjectMapper objectMapper = new ObjectMapper();
- objectMapper.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
-
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
-
- String json;
- try {
- json = objectMapper.writeValueAsString(expectedTestObject);
-
- 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();
-
- if (actualTestObject instanceof TestObjectForJSONFormatter) {
- boolean isObjectEqual = actualTestObject.equals(expectedTestObject);
- Assert.assertTrue(isObjectEqual,
- "actualTestObject and expectedTestObject should be equal");
- } else {
- fail("actualTestObject is expected to be of type PdxInstance");
- }
- } catch (JsonProcessingException e1) {
- fail("JsonProcessingException occurred:" + e1.getMessage());
- } catch (JSONException e) {
- fail("JSONException occurred:" + e.getMessage());
- }
- } else {
- fail("receivedObject is expected to be of type PdxInstance");
- }
+ assertTrue("receivedObject is expected to be of type PdxInstance",
+ receivedObject instanceof PdxInstance);
+
+ // 2. Get the JSON string from actualTestObject using jackson ObjectMapper.
+ ObjectMapper objectMapper = new ObjectMapper();
+ objectMapper.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
+ objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
+
+ String json = objectMapper.writeValueAsString(expectedTestObject);
+
+ String jsonWithClassType = expectedTestObject.addClassTypeToJson(json);
+
+ // 3. Get PdxInstance from the Json String and Validate pi.getObject() API.
+ PdxInstance receivedPdxInstance =
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 = receivedPdxInstance.getObject();
+
+ assertTrue(actualTestObject instanceof TestObjectForJSONFormatter);
+ assertEquals(actualTestObject, expectedTestObject);
+
}
- // Testcase-2: validate Json->PdxInstance-->Java conversion
- private void verifyJsonToPdxInstanceConversionWithJSONFormatter() {
+ @Test
+ public 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);
@@ -160,93 +127,75 @@ public class JSONFormatterJUnitTest {
PdxInstance expectedPI = (PdxInstance) receivedObject;
- String json;
- try {
- json = JSONFormatter.toJSON(expectedPI);
+ String json = JSONFormatter.toJSON(expectedPI);
- String jsonWithClassType = expectedTestObject.addClassTypeToJson(json);
+ 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.
+ // 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();
+ Object actualTestObject = actualPI.getObject();
- assertEquals("receivedObject is expected to be of type PdxInstance",
- TestObjectForJSONFormatter.class, actualTestObject.getClass());
+ 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());
- }
+ assertEquals("actualTestObject and expectedTestObject should be equal",
expectedTestObject,
+ actualTestObject);
}
- private void verifyJsonWithJavaObject(String json,
TestObjectForJSONFormatter testObject) {
- try {
- JSONObject jsonObject = new JSONObject(json);
-
- // Testcase-1: Validate json string against the pdxInstance.
- // validation for primitive types
- assertEquals("VerifyPdxInstanceToJson: Int type values are not matched",
- testObject.getP_int(), jsonObject.getInt(testObject.getP_intFN()));
- assertEquals("VerifyPdxInstanceToJson: long type values are not matched",
- testObject.getP_long(),
jsonObject.getLong(testObject.getP_longFN()));
-
- // validation for wrapper types
- assertEquals("VerifyPdxInstanceToJson: Boolean type values are not
matched",
- testObject.getW_bool().booleanValue(),
jsonObject.getBoolean(testObject.getW_boolFN()));
- assertEquals("VerifyPdxInstanceToJson: Float type values are not
matched",
- testObject.getW_double().doubleValue(),
jsonObject.getDouble(testObject.getW_doubleFN()),
- 0);
- assertEquals("VerifyPdxInstanceToJson: bigDec type values are not
matched",
- testObject.getW_bigDec().longValue(),
jsonObject.getLong(testObject.getW_bigDecFN()));
-
- // vlidation for array types
- assertEquals("VerifyPdxInstanceToJson: Byte[] type values are not
matched",
- (int) testObject.getW_byteArray()[1],
- jsonObject.getJSONArray(testObject.getW_byteArrayFN()).getInt(1));
- assertEquals("VerifyPdxInstanceToJson: Double[] type values are not
matched",
- testObject.getW_doubleArray()[0],
-
jsonObject.getJSONArray(testObject.getW_doubleArrayFN()).getDouble(0), 0);
- assertEquals("VerifyPdxInstanceToJson: String[] type values are not
matched",
- testObject.getW_strArray()[2],
- jsonObject.getJSONArray(testObject.getW_strArrayFN()).getString(2));
-
- // validation for collection types
- assertEquals("VerifyPdxInstanceToJson: list type values are not matched",
- testObject.getC_list().get(0),
- jsonObject.getJSONArray(testObject.getC_listFN()).getString(0));
-
- assertEquals("VerifyPdxInstanceToJson: stack type values are not
matched",
- testObject.getC_stack().get(2),
- jsonObject.getJSONArray(testObject.getC_stackFN()).getString(2));
-
- // validation for Map
- assertEquals("VerifyPdxInstanceToJson: Map type values are not matched",
- testObject.getM_empByCity().get("Ahmedabad").get(0).getFname(),
-
jsonObject.getJSONObject(testObject.getM_empByCityFN()).getJSONArray("Ahmedabad")
- .getJSONObject(0).getString("fname"));
-
- // validation Enum
- assertEquals("VerifyPdxInstanceToJson: Enum type values are not matched",
- testObject.getDay().toString(),
jsonObject.getString(testObject.getDayFN()));
-
- } catch (JSONException e) {
- throw new AssertionError(
- "Error in VerifyPdxInstanceToJson, Malformed json, can not create
JSONArray from it", e);
- }
- }
-
- @Test
- public void testJSONFormatterAPIs() {
- ValidatePdxInstanceToJsonConversion();
- verifyJsonToPdxInstanceConversion();
- verifyJsonToPdxInstanceConversionWithJSONFormatter();
+ static void verifyJsonWithJavaObject(String json, TestObjectForJSONFormatter
testObject) {
+ JSONObject jsonObject = new JSONObject(json);
+
+ // Testcase-1: Validate json string against the pdxInstance.
+ // validation for primitive types
+ assertEquals("VerifyPdxInstanceToJson: Int type values are not matched",
testObject.getP_int(),
+ jsonObject.getInt(testObject.getP_intFN()));
+ assertEquals("VerifyPdxInstanceToJson: long type values are not matched",
+ testObject.getP_long(), jsonObject.getLong(testObject.getP_longFN()));
+
+ // validation for wrapper types
+ assertEquals("VerifyPdxInstanceToJson: Boolean type values are not
matched",
+ testObject.getW_bool().booleanValue(),
jsonObject.getBoolean(testObject.getW_boolFN()));
+ assertEquals("VerifyPdxInstanceToJson: Float type values are not matched",
+ testObject.getW_double().doubleValue(),
jsonObject.getDouble(testObject.getW_doubleFN()),
+ 0);
+ assertEquals("VerifyPdxInstanceToJson: bigDec type values are not matched",
+ testObject.getW_bigDec().longValue(),
jsonObject.getLong(testObject.getW_bigDecFN()));
+
+ // vlidation for array types
+ assertEquals("VerifyPdxInstanceToJson: Byte[] type values are not matched",
+ (int) testObject.getW_byteArray()[1],
+ jsonObject.getJSONArray(testObject.getW_byteArrayFN()).getInt(1));
+ assertEquals("VerifyPdxInstanceToJson: Double[] type values are not
matched",
+ testObject.getW_doubleArray()[0],
+ jsonObject.getJSONArray(testObject.getW_doubleArrayFN()).getDouble(0),
0);
+ assertEquals("VerifyPdxInstanceToJson: String[] type values are not
matched",
+ testObject.getW_strArray()[2],
+ jsonObject.getJSONArray(testObject.getW_strArrayFN()).getString(2));
+
+ // validation for collection types
+ assertEquals("VerifyPdxInstanceToJson: list type values are not matched",
+ testObject.getC_list().get(0),
+ jsonObject.getJSONArray(testObject.getC_listFN()).getString(0));
+
+ assertEquals("VerifyPdxInstanceToJson: stack type values are not matched",
+ testObject.getC_stack().get(2),
+ jsonObject.getJSONArray(testObject.getC_stackFN()).getString(2));
+
+ // validation for Map
+ assertEquals("VerifyPdxInstanceToJson: Map type values are not matched",
+ testObject.getM_empByCity().get("Ahmedabad").get(0).getFname(),
+
jsonObject.getJSONObject(testObject.getM_empByCityFN()).getJSONArray("Ahmedabad")
+ .getJSONObject(0).getString("fname"));
+
+ // validation Enum
+ assertEquals("VerifyPdxInstanceToJson: Enum type values are not matched",
+ testObject.getDay().toString(),
jsonObject.getString(testObject.getDayFN()));
}
/**
@@ -255,17 +204,12 @@ public class JSONFormatterJUnitTest {
*/
@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).keySet().size();
+ if (cache.getRegion(PeerTypeRegistration.REGION_FULL_PATH) != null) {
+ pdxTypes =
cache.getRegion(PeerTypeRegistration.REGION_FULL_PATH).keySet().size();
}
- Region region = c.getRegion("primitiveKVStore");
-
String js = "{name:\"ValueExist\", age:14}";
region.put(1, JSONFormatter.fromJSON(js));
@@ -274,19 +218,15 @@ public class JSONFormatterJUnitTest {
region.put(2, JSONFormatter.fromJSON(js2));
- assertEquals(pdxTypes + 1,
c.getRegion(PeerTypeRegistration.REGION_FULL_PATH).keySet().size());
+ assertEquals(pdxTypes + 1,
+
cache.getRegion(PeerTypeRegistration.REGION_FULL_PATH).keySet().size());
}
@Test
public void testJSONStringSortedFields() {
-
try {
System.setProperty(JSONFormatter.SORT_JSON_FIELD_NAMES_PROPERTY, "true");
- Cache c = CacheFactory.getAnyInstance();
-
- Region region = c.getRegion("primitiveKVStore");
-
String js = "{b:\"b\", age:14, c:\"c' go\", bb:23}";
region.put(1, JSONFormatter.fromJSON(js));
@@ -310,14 +250,15 @@ public class JSONFormatterJUnitTest {
int pdxTypes = 0;
- if (c.getRegion(PeerTypeRegistration.REGION_FULL_PATH) != null) {
- pdxTypes =
c.getRegion(PeerTypeRegistration.REGION_FULL_PATH).keySet().size();
+ if (cache.getRegion(PeerTypeRegistration.REGION_FULL_PATH) != null) {
+ pdxTypes =
cache.getRegion(PeerTypeRegistration.REGION_FULL_PATH).keySet().size();
}
String js2 = "{c:\"c' go\", bb:23, b:\"b\", age:14 }";
region.put(2, JSONFormatter.fromJSON(js2));
- assertEquals(pdxTypes,
c.getRegion(PeerTypeRegistration.REGION_FULL_PATH).keySet().size());
+ assertEquals(pdxTypes,
+
cache.getRegion(PeerTypeRegistration.REGION_FULL_PATH).keySet().size());
} finally {
System.setProperty(JSONFormatter.SORT_JSON_FIELD_NAMES_PROPERTY,
"false");
diff --git
a/geode-core/src/test/java/org/apache/geode/pdx/JSONPdxClientServerDUnitTest.java
b/geode-core/src/test/java/org/apache/geode/pdx/JSONPdxClientServerDUnitTest.java
index ae64bf2..61c200f 100755
---
a/geode-core/src/test/java/org/apache/geode/pdx/JSONPdxClientServerDUnitTest.java
+++
b/geode-core/src/test/java/org/apache/geode/pdx/JSONPdxClientServerDUnitTest.java
@@ -178,7 +178,7 @@ public class JSONPdxClientServerDUnitTest extends
JUnit4CacheTestCase {
});
}
- public void VerifyPdxInstanceAndJsonConversion() {
+ public void VerifyPdxInstanceAndJsonConversion() throws
JsonProcessingException {
Region region = getRootRegion("testSimplePdx");
// Create Object and initialize its members.
@@ -189,65 +189,12 @@ public class JSONPdxClientServerDUnitTest extends
JUnit4CacheTestCase {
region.put("101", testObject);
// Get the object as PdxInstance
- Object result = (Object) region.get("101");
- if (result instanceof PdxInstance) {
- PdxInstance pi = (PdxInstance) result;
- String json = JSONFormatter.toJSON(pi);
+ Object result = region.get("101");
+ assertTrue(result instanceof PdxInstance);
+ PdxInstance pi = (PdxInstance) result;
+ String json = JSONFormatter.toJSON(pi);
- try {
- JSONObject jsonObject = new JSONObject(json);
-
- // Testcase-1: Validate json string against the pdxInstance.
- // validation for primitive types
- assertEquals("VerifyPdxInstanceToJson: Int type values are not
matched",
- testObject.getP_int(), jsonObject.getInt(testObject.getP_intFN()));
- assertEquals("VerifyPdxInstanceToJson: long type values are not
matched",
- testObject.getP_long(),
jsonObject.getLong(testObject.getP_longFN()));
-
- // validation for wrapper types
- assertEquals("VerifyPdxInstanceToJson: Boolean type values are not
matched",
- testObject.getW_bool().booleanValue(),
jsonObject.getBoolean(testObject.getW_boolFN()));
- assertEquals("VerifyPdxInstanceToJson: Float type values are not
matched",
- testObject.getW_double().doubleValue(),
- jsonObject.getDouble(testObject.getW_doubleFN()), 0);
- assertEquals("VerifyPdxInstanceToJson: bigDec type values are not
matched",
- testObject.getW_bigDec().longValue(),
jsonObject.getLong(testObject.getW_bigDecFN()));
-
- // vlidation for array types
- assertEquals("VerifyPdxInstanceToJson: Byte[] type values are not
matched",
- (int) testObject.getW_byteArray()[1],
- jsonObject.getJSONArray(testObject.getW_byteArrayFN()).getInt(1));
- assertEquals("VerifyPdxInstanceToJson: Double[] type values are not
matched",
- testObject.getW_doubleArray()[0],
-
jsonObject.getJSONArray(testObject.getW_doubleArrayFN()).getDouble(0), 0);
- assertEquals("VerifyPdxInstanceToJson: String[] type values are not
matched",
- testObject.getW_strArray()[2],
-
jsonObject.getJSONArray(testObject.getW_strArrayFN()).getString(2));
-
- // validation for collection types
- assertEquals("VerifyPdxInstanceToJson: list type values are not
matched",
- testObject.getC_list().get(0),
- jsonObject.getJSONArray(testObject.getC_listFN()).getString(0));
-
- assertEquals("VerifyPdxInstanceToJson: stack type values are not
matched",
- testObject.getC_stack().get(2),
- jsonObject.getJSONArray(testObject.getC_stackFN()).getString(2));
-
- // validation for Map
- assertEquals("VerifyPdxInstanceToJson: Map type values are not
matched",
- testObject.getM_empByCity().get("Ahmedabad").get(0).getFname(),
-
jsonObject.getJSONObject(testObject.getM_empByCityFN()).getJSONArray("Ahmedabad")
- .getJSONObject(0).getString("fname"));
-
- // validation Enum
- assertEquals("VerifyPdxInstanceToJson: Enum type values are not
matched",
- testObject.getDay().toString(),
jsonObject.getString(testObject.getDayFN()));
- } catch (JSONException e) {
- fail("Error in VerifyPdxInstanceToJson, Malformed json, can not create
JSONArray from it");
- }
- } else {
- fail("Error in VerifyPdxInstanceToJson, result must be of type
PdxInstance");
- }
+ JSONFormatterJUnitTest.verifyJsonWithJavaObject(json, testObject);
// TestCase-2 : Validate Java-->JSON-->PdxInstance --> Java Mapping
TestObjectForJSONFormatter actualTestObject = new
TestObjectForJSONFormatter();
@@ -255,44 +202,35 @@ public class JSONPdxClientServerDUnitTest extends
JUnit4CacheTestCase {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
+ validateReceivedJSON(region, actualTestObject, objectMapper);
- try {
- // 1. get the json from the object using Jackosn Object Mapper
- String json = objectMapper.writeValueAsString(actualTestObject);
- String jsonWithClassType = actualTestObject.addClassTypeToJson(json);
-
- // 2. convert json into the PdxInstance and put it into the region
- PdxInstance pi = JSONFormatter.fromJSON(jsonWithClassType);
- region.put("201", pi);
-
- // 3. get the value on key "201" and validate PdxInstance.getObject()
API.
- Object receivedObject = region.get("201");
- if (receivedObject instanceof PdxInstance) {
- PdxInstance receivedPdxInstance = (PdxInstance) receivedObject;
-
- // 4. get the actualType testObject from the pdxInstance and compare
it with
- // actualTestObject
- Object getObj = receivedPdxInstance.getObject();
- if (getObj instanceof TestObjectForJSONFormatter) {
- TestObjectForJSONFormatter receivedTestObject =
(TestObjectForJSONFormatter) getObj;
-
- boolean isEqual = actualTestObject.equals(receivedTestObject);
- Assert.assertTrue(isEqual, "actualTestObject and receivedTestObject
should be equal");
- } else {
- fail("getObj is expected to be an instance of
TestObjectForJSONFormatter");
- }
- } else {
- fail("receivedObject is expected to be of type PdxInstance");
- }
- } catch (JsonProcessingException e) {
- fail("JsonProcessingException: error encountered while converting JSON
from Java object: "
- + e.getMessage());
+ }
+
+ private void validateReceivedJSON(Region region, TestObjectForJSONFormatter
actualTestObject,
+ ObjectMapper objectMapper) throws JsonProcessingException {
+ // 1. get the json from the object using Jackson Object Mapper
+ String json = objectMapper.writeValueAsString(actualTestObject);
+ String jsonWithClassType = actualTestObject.addClassTypeToJson(json);
- } catch (JSONException e) {
- fail("JSONException: error encountered while adding @type classType
into Json: "
- + e.getMessage());
- }
+ // 2. convert json into the PdxInstance and put it into the region
+ PdxInstance pi = JSONFormatter.fromJSON(jsonWithClassType);
+ region.put("201", pi);
+
+ // 3. get the value on key "201" and validate PdxInstance.getObject() API.
+ Object receivedObject = region.get("201");
+ assertTrue(receivedObject instanceof PdxInstance);
+ PdxInstance receivedPdxInstance = (PdxInstance) receivedObject;
+
+ // 4. get the actualType testObject from the pdxInstance and compare it
with
+ // actualTestObject
+ Object getObj = receivedPdxInstance.getObject();
+
+ assertTrue(getObj instanceof TestObjectForJSONFormatter);
+
+ TestObjectForJSONFormatter receivedTestObject =
(TestObjectForJSONFormatter) getObj;
+
+ assertEquals(actualTestObject, receivedTestObject);
}
String getJSONDir(String file) {
--
To stop receiving notification emails like this one, please contact
[email protected].