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 * @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 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()); } }
private PdxInstance checkPdxInstance(PdxInstance pi) throws IOException, ClassNotFoundException { // serialize the pi and make sure it can be deserialized PdxInstance pi2 = (PdxInstance) serializeAndDeserialize(pi); assertEquals(pi, pi2); assertEquals(pi.hashCode(), pi2.hashCode()); assertEquals(pi.getFieldNames(), pi2.getFieldNames()); return pi2; }
public void testEnums() throws IOException, ClassNotFoundException { PdxInstance e0 = this.cache.createPdxEnum(Coin.class.getName(), Coin.HEADS.name(), Coin.HEADS.ordinal()); assertEquals(true, e0.isEnum()); assertEquals(true, e0.hasField("name")); assertEquals(true, e0.hasField("ordinal")); assertEquals(false, e0.hasField("bogus")); assertEquals(null, e0.getField("bogus")); assertEquals(Coin.class.getName(), e0.getClassName()); assertEquals(Coin.HEADS.name(), e0.getField("name")); assertEquals(Coin.HEADS.ordinal(), e0.getField("ordinal")); assertEquals(Coin.HEADS, e0.getObject()); PdxInstanceEnum pie1 = new PdxInstanceEnum(Coin.TAILS); assertEquals(e1, pie1); assertEquals(e1.hashCode(), pie1.hashCode()); assertEquals(0, c1.compareTo(pie1)); assertEquals(true, c0.compareTo(pie1) < 0);
in = new DataInputStream(new ByteArrayInputStream(actual)); PdxInstance pi = (PdxInstance)DataSerializer.readObject(in); actualVal = (SimpleClass1)pi.getObject(); assertTrue("Mismatch in write and read value: Value Write..." + pdx + " Value Read..." + actualVal, pdx.equals(actualVal)); assertTrue(pi.hasField("myFlag")); assertTrue(pi.hasField("myShort")); assertTrue(pi.hasField("myString1")); assertTrue(pi.hasField("myLong")); assertTrue(pi.hasField("myString2")); assertTrue(pi.hasField("myString3")); assertTrue(pi.hasField("myInt")); assertTrue(pi.hasField("myFloat")); assertEquals(pdx.isMyFlag(), pi.getField("myFlag")); assertEquals(pdx.getMyShort(), pi.getField("myShort")); assertEquals(pdx.getMyString1(), pi.getField("myString1")); assertEquals(pdx.getMyLong(), pi.getField("myLong")); assertEquals(pdx.getMyString2(), pi.getField("myString2")); assertEquals(pdx.getMyString3(), pi.getField("myString3")); assertEquals(pdx.getMyInt(), pi.getField("myInt")); assertEquals(pdx.getMyFloat(), pi.getField("myFloat")); PdxReaderImpl reader = (PdxReaderImpl)pi; PdxType type = reader.getPdxType();
/** * 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 testGetFieldName() throws IOException, ClassNotFoundException { setupSerializer(new ExplicitFieldNameAutoSerializer(false, "com.gemstone.gemfire.pdx.DomainObjectPdxAuto"), true); DomainObject objOut = new DomainObjectPdxAuto(4); HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT); DataSerializer.writeObject(objOut, out); PdxInstance pi = (PdxInstance)DataSerializer.readObject(new DataInputStream( new ByteArrayInputStream(out.toByteArray()))); System.out.println("fieldNames=" + pi.getFieldNames()); assertEquals(false, pi.hasField("long_0")); assertEquals(true, pi.hasField("_long_0")); assertEquals(true, pi.hasField("string_0")); assertEquals(objOut, pi.getObject()); }
private Object extractPdxIfNeeded(Object object) { if (object instanceof PdxInstance) { object = ((PdxInstance) object).getObject(); } else if (object instanceof PdxString) { object = ((PdxString) object).toString(); } return object; }
public static void doEnumValidation(PdxInstance pdxInst) throws TestException { boolean doEnumValidation = PdxPrms.getDoEnumValidation(); if (!doEnumValidation) { return; // we are running in a 661 mode in a 662 jvm; 661 does not have pdx enums // or are converting from 661 to 662 (this might be a 662 jvm, but it could // do a get from a 661 jvm, then the returned object will not be in 662 form // for enums } if (pdxInst.isEnum()) { throw new TestException("isEnum() on " + pdxInst + " returned true"); } String className = pdxInst.getClassName(); if (!className.equals("util.VersionedValueHolder") && !className.equals("util.PdxVersionedValueHolder")) { throw new TestException("getClassName() on " + pdxInst + " returned " + className); } Object enumObj = pdxInst.getField("aDay"); if (enumObj != null) { if (!(enumObj instanceof PdxInstance)) { throw new TestException("Expected enum field aDay to be a PdxInstance but it is " + TestHelper.toString(enumObj)); } PdxInstance enumPdxInst = (PdxInstance)enumObj; className = enumPdxInst.getClassName(); if (!className.equals("util.VersionedValueHolder$Day")) { throw new TestException("getClassName() on " + enumPdxInst + " returned " + className); } } }
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 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")); }
public void testIsIdentityField() throws IOException, ClassNotFoundException { setupSerializer(new ExplicitIdentityAutoSerializer(false, "com.gemstone.gemfire.pdx.DomainObjectPdxAuto"), true); DomainObject objOut = new DomainObjectPdxAuto(4); HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT); DataSerializer.writeObject(objOut, out); DataSerializer.writeObject(objOut, out); DataInputStream dis = new DataInputStream( new ByteArrayInputStream(out.toByteArray())); PdxInstance pi = (PdxInstance)DataSerializer.readObject(dis); PdxInstance pi2 = (PdxInstance)DataSerializer.readObject(dis); assertEquals(true, pi.isIdentityField("long_0")); assertEquals(false, pi.isIdentityField("string_0")); assertEquals(false, pi.isEnum()); assertEquals(objOut.getClass().getName(), pi.getClassName()); assertEquals(objOut, pi.getObject()); assertEquals(objOut, pi2.getObject()); } public static class ExplicitIncludedAutoSerializer extends ReflectionBasedAutoSerializer {
public void testObjectArrayPdxInstance() throws IOException, ClassNotFoundException { DefaultQuery.setPdxReadSerialized(true); PdxReaderImpl.TESTHOOK_TRACKREADS = true; try { LongFieldHolder[] v = new LongFieldHolder[]{new LongFieldHolder(1), new LongFieldHolder(2)}; PdxInstance pi = (PdxInstance) serializeAndDeserialize(new ObjectArrayHolder(v)); ObjectArrayHolder oah = (ObjectArrayHolder) pi.getObject(); LongFieldHolder[] nv = (LongFieldHolder[])oah.getObjectArray(); if (!Arrays.equals(v, nv)) { fail("expected " + Arrays.toString(v) + " but had " + Arrays.toString(nv)); } Object[] oa = (Object[]) pi.getField("f1"); assertTrue(oa[0] instanceof PdxInstance); assertTrue(oa[1] instanceof PdxInstance); LongFieldHolder[] nv2 = new LongFieldHolder[2]; nv2[0] = (LongFieldHolder)((PdxInstance)oa[0]).getObject(); nv2[1] = (LongFieldHolder)((PdxInstance)oa[1]).getObject(); if (!Arrays.equals(v, nv2)) { fail("expected " + Arrays.toString(v) + " but had " + Arrays.toString(nv2)); } } finally { DefaultQuery.setPdxReadSerialized(false); PdxReaderImpl.TESTHOOK_TRACKREADS = false; } } public void testLongField() throws IOException, ClassNotFoundException {
PdxInstance pi = InternalDataSerializer.readPdxInstance(cdValBytes, GemFireCacheImpl.getForPdx("Could not check value equality")); if (pi != null) { return pi.equals(pdx); } else { if (obj.getClass().getName().equals(pdx.getClassName())) { GemFireCacheImpl gfc = GemFireCacheImpl.getForPdx("Could not access Pdx registry"); if (gfc != null) {
/** * Returns the keys from the given object or the indexes from an array * * @param obj an array or an object * @return the keys for an object or the indexes for an array */ public Collection<String> getPropertyKeys(Object obj) { if (isArray(obj)) { List l = (List) obj; List<String> keys = new ArrayList<String>(l.size()); for (int i = 0; i < l.size(); i++) { keys.add(String.valueOf(i)); } return keys; } else { return ((PdxInstance)obj).getFieldNames(); } } }
if(left instanceof PdxInstance && !(right instanceof PdxInstance) && ((PdxInstance)left).getClassName().equals(right.getClass().getName())) { left = ((PdxInstance)left).getObject(); } else if(right instanceof PdxInstance && !(left instanceof PdxInstance) && ((PdxInstance)right).getClassName().equals(left.getClass().getName())) { right = ((PdxInstance)right).getObject();
public void testIsFieldIncluded() throws IOException, ClassNotFoundException { setupSerializer(new ExplicitIncludedAutoSerializer(false, "com.gemstone.gemfire.pdx.DomainObjectPdxAuto"), true); DomainObject objOut = new DomainObjectPdxAuto(4); HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT); DataSerializer.writeObject(objOut, out); PdxInstance pi = (PdxInstance)DataSerializer.readObject(new DataInputStream( new ByteArrayInputStream(out.toByteArray()))); assertEquals(true, pi.hasField("long_0")); assertEquals(false, pi.hasField("string_0")); } public static class ExplicitFieldNameAutoSerializer extends ReflectionBasedAutoSerializer {
public void testObjectPdxInstance() throws IOException, ClassNotFoundException { DefaultQuery.setPdxReadSerialized(true); PdxReaderImpl.TESTHOOK_TRACKREADS = true; try { PdxInstance pi = (PdxInstance) serializeAndDeserialize(new ObjectHolder("hello")); ObjectHolder v3 = (ObjectHolder)pi.getObject(); WritablePdxInstance wpi = pi.createWriter(); wpi.setField("f1", "goodbye"); assertEquals("goodbye", wpi.getField("f1")); ObjectHolder v = (ObjectHolder)wpi.getObject(); ObjectHolder v2 = (ObjectHolder)wpi.getObject(); assertEquals("goodbye", v.getObject()); assertEquals("goodbye", v2.getObject()); assertEquals("hello", v3.getObject()); assertEquals("goodbye", wpi.getField("f1")); } finally { DefaultQuery.setPdxReadSerialized(false); PdxReaderImpl.TESTHOOK_TRACKREADS = false; } } public void testObjectArrayPdxInstance() throws IOException, ClassNotFoundException {
public void testEqualsDifferentTypes() throws IOException, ClassNotFoundException { PdxInstance instance1 = getPdx(new TestPdx() { public void toData(PdxWriter writer) { writer.writeBoolean("field1", true); } }); PdxInstance instance2 = getPdx(new TestPdx() { public void toData(PdxWriter writer) { writer.writeBoolean("field1", true); } }); //These are different classes, so they shouldn't match. assertFalse(instance1.equals(instance2)); assertFalse(instance1.isIdentityField("field1")); }