/** * Extracts a value from an map * * @param obj a map * @param key property key * @return the map entry */ public Object getPDXValue(Object obj, String key){ return ((PdxInstance) obj).getField(key); }
private String getJSONString(JsonGenerator jg, PdxInstance pdxInstance) throws JsonGenerationException, IOException { jg.writeStartObject(); List<String> pdxFields = pdxInstance.getFieldNames(); for (String pf : pdxFields) { Object value = pdxInstance.getField(pf); jg.writeFieldName(pf); writeValue(jg, value, pf); } jg.writeEndObject(); return null; }
/** * Extracts a value from an map * * @param obj a map * @param key property key * @return the map entry */ public Object getPDXValue(Object obj, String key){ return ((PdxInstance) obj).getField(key); }
public void testGetField() throws IOException, ClassNotFoundException { PdxInstance instance = getPdx(new TestPdx() { public void toData(PdxWriter out) { out.writeBoolean("field1", false); out.writeInt("field2", 53); out.writeObject("field3", new TestPdx() { public void toData(PdxWriter writer) { writer.writeString("afield", "hello"); } }); } }); assertEquals(Arrays.asList(new String[]{"field1", "field2", "field3"}), instance.getFieldNames()); assertEquals(instance.getField("field2"), Integer.valueOf(53)); assertEquals(instance.getField("field1"), Boolean.FALSE); PdxInstance fieldInstance = (PdxInstance) instance.getField("field3"); assertEquals(Arrays.asList(new String[]{"afield"}), fieldInstance.getFieldNames()); assertEquals("hello", fieldInstance.getField("afield")); }
/** * Extracts a value from an map * * @param obj a map * @param key property key * @return the map entry */ public Object getPDXValue(Object obj, String key){ return ((PdxInstance) obj).getField(key); }
private static String pdxToJson(PdxInstance obj) { if(obj!=null){ try{ GfJsonObject json = new GfJsonObject(); for(String field : obj.getFieldNames()){ Object fieldValue = obj.getField(field); if(fieldValue!=null){ if(JsonUtil.isPrimitiveOrWrapper(fieldValue.getClass())){ json.put(field, fieldValue); }else{ json.put(field, fieldValue.getClass()); } } } return json.toString(); }catch(GfJsonException e){ return null; } } return null; }
@Override public boolean equals(Object o) { if (o instanceof MyObject) { MyObject obj = (MyObject) o; return f1 == obj.f1 && f2.equals(obj.f2); } else if (o instanceof PdxInstance) { PdxInstance pdx = (PdxInstance) o; return pdx.getField("f1").equals(f1) && pdx.getField("f2").equals(f2); } return false; }
private static String pdxToJson(PdxInstance obj) { if(obj!=null){ try{ GfJsonObject json = new GfJsonObject(); for(String field : obj.getFieldNames()){ Object fieldValue = obj.getField(field); if(fieldValue!=null){ if(JsonUtil.isPrimitiveOrWrapper(fieldValue.getClass())){ json.put(field, fieldValue); }else{ json.put(field, fieldValue.getClass()); } } } return json.toString(); }catch(GfJsonException e){ return null; } } return null; }
/** * Extracts a value from an map * * @param obj a map * @param key property key * @param signalUndefined if true the constant {@link com.jayway.jsonpath.spi.json.JsonProvider#UNDEFINED} is returned for missing properties * @return the map entry */ public Object getPDXValue(Object obj, String key, boolean signalUndefined){ PdxInstance m = (PdxInstance) obj; if(!/*m.containsKey(key)*/ m.hasField(key) && signalUndefined){ return JsonProvider.UNDEFINED; } else { return ((PdxInstance) obj).getField(key); } }
/** * Extracts a value from an map * * @param obj a map * @param key property key * @param signalUndefined if true the constant {@link com.jayway.jsonpath.spi.json.JsonProvider#UNDEFINED} is returned for missing properties * @return the map entry */ public Object getPDXValue(Object obj, String key, boolean signalUndefined){ PdxInstance m = (PdxInstance) obj; if(!/*m.containsKey(key)*/ m.hasField(key) && signalUndefined){ return JsonProvider.UNDEFINED; } else { return ((PdxInstance) obj).getField(key); } }
/** * @param anObj * @return * @throws TestException */ protected Object getValueForBB(Object anObj) throws TestException { Object valueToPutInBB = null; if (anObj instanceof BaseValueHolder) { valueToPutInBB = ((BaseValueHolder)anObj).myValue; } else if (anObj instanceof PdxInstance) { valueToPutInBB = ((PdxInstance)anObj).getField("myValue"); } else { throw new TestException("Unexpected value class " + anObj.getClass().getName()); } return valueToPutInBB; }
/** * Extracts a value from an map * * @param obj a map * @param key property key * @param signalUndefined if true the constant {@link com.jayway.jsonpath.spi.json.JsonProvider#UNDEFINED} is returned for missing properties * @return the map entry */ public Object getPDXValue(Object obj, String key, boolean signalUndefined){ PdxInstance m = (PdxInstance) obj; if(!/*m.containsKey(key)*/ m.hasField(key) && signalUndefined){ return JsonProvider.UNDEFINED; } else { return ((PdxInstance) obj).getField(key); } }
public void testConfigWithIdentity1() throws Exception { setupSerializer(); Properties props = new Properties(); props.put("classes", "com.gemstone.gemfire.pdx.DomainObjectPdxAuto#identity=long.*"); serializer.init(props); DomainObject objOut = new DomainObjectPdxAuto(4); objOut.set("string_0", "test string value"); objOut.set("long_0", 99L); HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT); DataSerializer.writeObject(objOut, out); PdxInstance pdxIn = DataSerializer.readObject(new DataInputStream( new ByteArrayInputStream(out.toByteArray()))); assertEquals(99L, pdxIn.getField("long_0")); assertTrue(pdxIn.isIdentityField("long_0")); }
public void testConfigWithIdentityAndExclude2() throws Exception { setupSerializer(); Properties props = new Properties(); props.put("classes", "com.gemstone.gemfire.pdx.DomainObjectPdxAuto#identity=long.*#exclude=string.*#, com.another.class.Foo"); serializer.init(props); assertEquals(27, manager.getFields(DomainObjectPdxAuto.class).size()); DomainObject objOut = new DomainObjectPdxAuto(4); objOut.set("string_0", "test string value"); objOut.set("long_0", 99L); HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT); DataSerializer.writeObject(objOut, out); PdxInstance pdxIn = DataSerializer.readObject(new DataInputStream( new ByteArrayInputStream(out.toByteArray()))); // This means we're not doing anything with this string assertNull(pdxIn.getField("string_0")); assertTrue(pdxIn.isIdentityField("long_0")); }
public void testConfigWithIdentityAndExclude1() throws Exception { setupSerializer(); Properties props = new Properties(); props.put("classes", "com.gemstone.gemfire.pdx.DomainObjectPdxAuto#identity=long.*#exclude=string.*"); serializer.init(props); assertEquals(27, manager.getFields(DomainObjectPdxAuto.class).size()); DomainObject objOut = new DomainObjectPdxAuto(4); objOut.set("string_0", "test string value"); objOut.set("long_0", 99L); HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT); DataSerializer.writeObject(objOut, out); PdxInstance pdxIn = DataSerializer.readObject(new DataInputStream( new ByteArrayInputStream(out.toByteArray()))); // This means we're not doing anything with this string assertNull(pdxIn.getField("string_0")); assertTrue(pdxIn.isIdentityField("long_0")); }
public void testConfigWithIdentityAndExclude3() throws Exception { setupSerializer(); Properties props = new Properties(); props.put("classes", "com.gemstone.gemfire.pdx.DomainObjectPdxAuto#identity=long.*#exclude=string.*, com.another.class.Foo"); serializer.init(props); assertEquals(27, manager.getFields(DomainObjectPdxAuto.class).size()); DomainObject objOut = new DomainObjectPdxAuto(4); objOut.set("string_0", "test string value"); objOut.set("long_0", 99L); HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT); DataSerializer.writeObject(objOut, out); PdxInstance pdxIn = DataSerializer.readObject(new DataInputStream( new ByteArrayInputStream(out.toByteArray()))); // This means we're not doing anything with this string assertNull(pdxIn.getField("string_0")); assertTrue(pdxIn.isIdentityField("long_0")); }
/** * Have a PDX with an Object[] whose element is a DataSerializable which * has a Long[]. Its a bug if the array nested in the DS becomes an Object[] * during deserialization. * Demonstrates bug 43838. */ public void testNestedDS() throws IOException, ClassNotFoundException { PdxInstanceFactory c = PdxInstanceFactoryImpl.newCreator("nestedDS", false); c.writeObject("obj", new MyDS()); PdxInstance pi = c.create(); pi.getField("obj"); checkPdxInstance(pi); }
/** * Have a pdx with an array of pdx. * Make sure the array class does not get loaded during * getField, hashCode, and equals. */ public void testNestedArray() throws IOException, ClassNotFoundException { PdxInstance pi; { PdxInstanceFactory c = PdxInstanceFactoryImpl.newCreator("nestedArray", false); MyPdx[] array = new MyPdx[]{new MyPdx()}; c.writeObjectArray("array", array); pi = c.create(); } pi.getField("array"); checkPdxInstance(pi); }
public void testHashCodeAndEqualsSameType() throws IOException, ClassNotFoundException { PdxInstance instance = getAllFields(0); assertEquals(instance, getAllFields(0)); assertEquals(instance.hashCode(), getAllFields(0).hashCode()); for(int i =1; i < allFieldCount + 1; i++) { PdxInstance other = getAllFields(i); assertFalse("One field " + i + " hashcode have been unequal but were equal" + instance.getField("field" + (i-1)) +", " + other.getField("field" + (i-1)) + ", " + instance + ", " + other, instance.equals(other)); //Technically, this could be true. If this asserts fails I guess we can just change the test. assertFalse("One field " + i + " hashcode have been unequal but were equal" + instance +", " + other, instance.hashCode() == other.hashCode()); } }
public void testNestedPdxInstance() throws IOException, ClassNotFoundException { PdxInstanceFactory c = PdxInstanceFactoryImpl.newCreator("testNestedPdxInstanceChild", false); c.writeField("f", 37, int.class); PdxInstance child = c.create(); c = PdxInstanceFactoryImpl.newCreator("testNestedPdxInstanceParent", false); c.writeObject("f", child); WritablePdxInstance parent = c.create().createWriter(); checkPdxInstance(child); checkPdxInstance(parent); WritablePdxInstance child2 = ((PdxInstance)parent.getField("f")).createWriter(); assertEquals(child, child2); assertTrue(child != child2); child2.setField("f", 38); assertFalse(child.equals(child2)); assertEquals(child, parent.getField("f")); parent.setField("f", child2); assertFalse(child.equals(parent.getField("f"))); assertEquals(child2, parent.getField("f")); PdxInstance parentCopy = checkPdxInstance(parent); assertEquals(child2, parentCopy.getField("f")); }