Search in sources :

Example 56 with HeapDataOutputStream

use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.

the class AutoSerializableJUnitTest method testReadNullObjects.

/*
   * Test that when primitive wrapper classes are null, and get serialized, they remain as null when
   * being deserialized.
   */
@Test
public void testReadNullObjects() throws Exception {
    setupSerializer(stdSerializableClasses);
    // Don't want to write any fields
    DomainObjectPdxAuto objOut = new DomainObjectPdxAuto(4);
    objOut.anInteger = null;
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeObject(objOut, out);
    // Now we want to read all fields.
    manager.resetCaches();
    PdxInstance pdxIn = DataSerializer.readObject(new DataInputStream(new ByteArrayInputStream(out.toByteArray())));
    DomainObjectPdxAuto result = (DomainObjectPdxAuto) pdxIn.getObject();
    assertNull(result.anInteger);
    assertNull(result.anEnum);
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 57 with HeapDataOutputStream

use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.

the class AutoSerializableJUnitTest method testConfigWithIdentity1.

/*
   * Test use of the identity param
   */
@Test
public void testConfigWithIdentity1() throws Exception {
    setupSerializer();
    Properties props = new Properties();
    props.put("classes", "org.apache.geode.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"));
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 58 with HeapDataOutputStream

use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.

the class PdxSerializableJUnitTest method testByteFormatForNestedPDX.

@Test
public void testByteFormatForNestedPDX() throws Exception {
    String myString1 = "ComplexClass1_myString1";
    long myLong = 15654;
    HashMap<String, PdxSerializable> myHashMap = new HashMap<String, PdxSerializable>();
    String myString2 = "ComplexClass1_myString2";
    float myFloat = 123.023f;
    for (int i = 0; i < 5; i++) {
        myHashMap.put("KEY_" + i, new SimpleClass(i, (byte) 5));
    }
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    PdxSerializable pdx = new NestedPdx(myString1, myLong, myHashMap, myString2, myFloat);
    DataSerializer.writeObject(pdx, out);
    int typeId = getPdxTypeIdForClass(NestedPdx.class);
    HeapDataOutputStream hdos1 = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeString(myString1, hdos1);
    byte[] str1Bytes = hdos1.toByteArray();
    HeapDataOutputStream hdosForMap = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeObject(myHashMap, hdosForMap);
    byte[] mapBytes = hdosForMap.toByteArray();
    HeapDataOutputStream hdos2 = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeString(myString2, hdos2);
    byte[] str2Bytes = hdos2.toByteArray();
    int length = str1Bytes.length + 8 + /* myLong */
    mapBytes.length + str2Bytes.length + 4 + /* myFloat */
    (2 * 1);
    int offset1 = 0;
    int offset2 = offset1 + 8 + str1Bytes.length;
    int offset3 = offset1 + 8 + str1Bytes.length + mapBytes.length;
    byte[] actual = out.toByteArray();
    int floatBytes = Float.floatToRawIntBits(myFloat);
    Byte[] expected = new Byte[] { // byte
    DSCODE.PDX, // int -
    (byte) (length >> 24), // int -
    (byte) (length >> 16), // int -
    (byte) (length >> 8), // int -
    (byte) length, // int -
    (byte) (typeId >> 24), // int -
    (byte) (typeId >> 16), // int -
    (byte) (typeId >> 8), // int -
    (byte) typeId, // typeId
    (byte) (myLong >> 56), (byte) (myLong >> 48), (byte) (myLong >> 40), (byte) (myLong >> 32), // long -
    (byte) (myLong >> 24), // long -
    (byte) (myLong >> 16), // long -
    (byte) (myLong >> 8), // long -
    (byte) myLong, // myLong
    (byte) (floatBytes >> 24), (byte) (floatBytes >> 16), (byte) (floatBytes >> 8), // float - myFloat
    (byte) floatBytes, // offset of myString2
    (byte) offset3, // offset of myHashMap
    (byte) offset2 };
    for (int i = (str1Bytes.length - 1); i >= 0; i--) {
        expected = (Byte[]) ArrayUtils.insert(expected, offset1 + PdxWriterImpl.HEADER_SIZE, str1Bytes[i]);
    }
    for (int i = (mapBytes.length - 1); i >= 0; i--) {
        expected = (Byte[]) ArrayUtils.insert(expected, offset2 + PdxWriterImpl.HEADER_SIZE, mapBytes[i]);
    }
    for (int i = (str2Bytes.length - 1); i >= 0; i--) {
        expected = (Byte[]) ArrayUtils.insert(expected, offset3 + PdxWriterImpl.HEADER_SIZE, str2Bytes[i]);
    }
    StringBuffer msg = new StringBuffer("Actual output: ");
    for (byte val : actual) {
        msg.append(val + ", ");
    }
    msg.append("\nExpected output: ");
    for (byte val : expected) {
        msg.append(val + ", ");
    }
    if (actual.length != expected.length) {
        System.out.println(msg.toString());
    }
    assertTrue("Mismatch in length, actual.length: " + actual.length + " and expected length: " + expected.length, actual.length == expected.length);
    for (int i = 0; i < actual.length; i++) {
        if (actual[i] != expected[i]) {
            System.out.println(msg.toString());
        }
        assertTrue("Mismatch at index " + i, actual[i] == expected[i]);
    }
    System.out.println("\n");
    DataInput in = new DataInputStream(new ByteArrayInputStream(actual));
    NestedPdx actualVal = (NestedPdx) DataSerializer.readObject(in);
    // System.out.println("actualVal..."+actualVal);
    assertTrue("Mismatch in write and read value: Value Write..." + pdx + " Value Read..." + actualVal, pdx.equals(actualVal));
    System.out.println("\n");
}
Also used : HashMap(java.util.HashMap) DataInputStream(java.io.DataInputStream) DataInput(java.io.DataInput) ByteArrayInputStream(java.io.ByteArrayInputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 59 with HeapDataOutputStream

use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.

the class PdxSerializableJUnitTest method testByteFormatForLongStrings.

@Test
public void testByteFormatForLongStrings() throws Exception {
    boolean myFlag = true;
    short myShort = 25;
    String myString1 = "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.";
    long myLong = 15654;
    String myString2 = "Class4_myString2";
    String myString3 = "Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. " + "Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. " + "Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. " + "Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. " + "Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. " + "Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. ";
    int myInt = 1420;
    float myFloat = 123.023f;
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    SimpleClass1 pdx = new SimpleClass1(myFlag, myShort, myString1, myLong, myString2, myString3, myInt, myFloat);
    DataSerializer.writeObject(pdx, out);
    int typeId = getPdxTypeIdForClass(SimpleClass1.class);
    HeapDataOutputStream hdos1 = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeString(myString1, hdos1);
    byte[] str1Bytes = hdos1.toByteArray();
    HeapDataOutputStream hdos2 = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeString(myString2, hdos2);
    byte[] str2Bytes = hdos2.toByteArray();
    HeapDataOutputStream hdos3 = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeString(myString3, hdos3);
    byte[] str3Bytes = hdos3.toByteArray();
    int length = 1 + /* myFlag */
    2 + /* myShort */
    8 + /* myLong */
    4 + /* myInt */
    4 + /* myFloat */
    str1Bytes.length + str2Bytes.length + str3Bytes.length + (2 * 2);
    int offset1 = 1 + 2;
    int offset2 = offset1 + 8 + str1Bytes.length;
    int offset3 = offset1 + 8 + str1Bytes.length + str2Bytes.length;
    byte[] actual = out.toByteArray();
    int floatBytes = Float.floatToRawIntBits(myFloat);
    Byte[] expected = new Byte[] { // byte
    DSCODE.PDX, // int -
    (byte) (length >> 24), // int -
    (byte) (length >> 16), // int -
    (byte) (length >> 8), // int -
    (byte) length, // int -
    (byte) (typeId >> 24), // int -
    (byte) (typeId >> 16), // int -
    (byte) (typeId >> 8), // int -
    (byte) typeId, // boolean - myFlag = true
    1, // short - myShort
    (byte) (myShort >> 8), // short - myShort
    (byte) myShort, (byte) (myLong >> 56), (byte) (myLong >> 48), (byte) (myLong >> 40), (byte) (myLong >> 32), // long -
    (byte) (myLong >> 24), // long -
    (byte) (myLong >> 16), // long -
    (byte) (myLong >> 8), // long -
    (byte) myLong, // int -
    (byte) (myInt >> 24), // int -
    (byte) (myInt >> 16), // int -
    (byte) (myInt >> 8), // int -
    (byte) myInt, // myInt
    (byte) (floatBytes >> 24), (byte) (floatBytes >> 16), (byte) (floatBytes >> 8), // float - myFloat
    (byte) floatBytes, // offset of myString3
    (byte) (offset3 >> 8), // offset of myString3
    (byte) offset3, // offset of myString2
    (byte) (offset2 >> 8), // offset of myString2
    (byte) offset2 };
    for (int i = (str1Bytes.length - 1); i >= 0; i--) {
        expected = // +
        (Byte[]) ArrayUtils.insert(expected, offset1 + PdxWriterImpl.HEADER_SIZE, str1Bytes[i]);
    // 5
    // for:
    // 1
    // for
    // DSCODE.PDX
    // and
    // 4
    // for
    // byte
    // stream
    // length
    }
    for (int i = (str2Bytes.length - 1); i >= 0; i--) {
        expected = (Byte[]) ArrayUtils.insert(expected, offset2 + PdxWriterImpl.HEADER_SIZE, str2Bytes[i]);
    }
    for (int i = (str3Bytes.length - 1); i >= 0; i--) {
        expected = (Byte[]) ArrayUtils.insert(expected, offset3 + PdxWriterImpl.HEADER_SIZE, str3Bytes[i]);
    }
    StringBuffer msg = new StringBuffer("Actual output: ");
    for (byte val : actual) {
        msg.append(val + ", ");
    }
    msg.append("\nExpected output: ");
    for (byte val : expected) {
        msg.append(val + ", ");
    }
    if (actual.length != expected.length) {
        System.out.println(msg.toString());
    }
    assertTrue("Mismatch in length, actual.length: " + actual.length + " and expected length: " + expected.length, actual.length == expected.length);
    for (int i = 0; i < actual.length; i++) {
        if (actual[i] != expected[i]) {
            System.out.println(msg.toString());
        }
        assertTrue("Mismatch at index " + i, actual[i] == expected[i]);
    }
    System.out.println("\n");
    DataInput in = new DataInputStream(new ByteArrayInputStream(actual));
    SimpleClass1 actualVal = (SimpleClass1) DataSerializer.readObject(in);
    // System.out.println("actualVal..."+actualVal);
    assertTrue("Mismatch in write and read value: Value Write..." + pdx + " Value Read..." + actualVal, pdx.equals(actualVal));
    c.setReadSerialized(true);
    try {
        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();
        assertEquals(SimpleClass1.class.getName(), type.getClassName());
        assertEquals(8, type.getFieldCount());
        assertEquals(2, type.getVariableLengthFieldCount());
        assertEquals(0, type.getPdxField("myFlag").getFieldIndex());
        assertEquals(1, type.getPdxField("myShort").getFieldIndex());
        assertEquals(2, type.getPdxField("myString1").getFieldIndex());
        assertEquals(3, type.getPdxField("myLong").getFieldIndex());
        assertEquals(4, type.getPdxField("myString2").getFieldIndex());
        assertEquals(5, type.getPdxField("myString3").getFieldIndex());
        assertEquals(6, type.getPdxField("myInt").getFieldIndex());
        assertEquals(7, type.getPdxField("myFloat").getFieldIndex());
        assertEquals(FieldType.BOOLEAN, type.getPdxField("myFlag").getFieldType());
        assertEquals(FieldType.SHORT, type.getPdxField("myShort").getFieldType());
        assertEquals(FieldType.STRING, type.getPdxField("myString1").getFieldType());
        assertEquals(FieldType.LONG, type.getPdxField("myLong").getFieldType());
        assertEquals(FieldType.STRING, type.getPdxField("myString2").getFieldType());
        assertEquals(FieldType.STRING, type.getPdxField("myString3").getFieldType());
        assertEquals(FieldType.INT, type.getPdxField("myInt").getFieldType());
        assertEquals(FieldType.FLOAT, type.getPdxField("myFloat").getFieldType());
        assertEquals("myFlag", type.getPdxField("myFlag").getFieldName());
        assertEquals("myShort", type.getPdxField("myShort").getFieldName());
        assertEquals("myString1", type.getPdxField("myString1").getFieldName());
        assertEquals("myLong", type.getPdxField("myLong").getFieldName());
        assertEquals("myString2", type.getPdxField("myString2").getFieldName());
        assertEquals("myString3", type.getPdxField("myString3").getFieldName());
        assertEquals("myInt", type.getPdxField("myInt").getFieldName());
        assertEquals("myFloat", type.getPdxField("myFloat").getFieldName());
        assertEquals(0, type.getPdxField("myFlag").getVarLenFieldSeqId());
        assertEquals(0, type.getPdxField("myShort").getVarLenFieldSeqId());
        assertEquals(0, type.getPdxField("myString1").getVarLenFieldSeqId());
        assertEquals(0, type.getPdxField("myLong").getVarLenFieldSeqId());
        assertEquals(1, type.getPdxField("myString2").getVarLenFieldSeqId());
        assertEquals(2, type.getPdxField("myString3").getVarLenFieldSeqId());
        assertEquals(2, type.getPdxField("myInt").getVarLenFieldSeqId());
        assertEquals(2, type.getPdxField("myFloat").getVarLenFieldSeqId());
        assertEquals(false, type.getPdxField("myFlag").isVariableLengthType());
        assertEquals(false, type.getPdxField("myShort").isVariableLengthType());
        assertEquals(true, type.getPdxField("myString1").isVariableLengthType());
        assertEquals(false, type.getPdxField("myLong").isVariableLengthType());
        assertEquals(true, type.getPdxField("myString2").isVariableLengthType());
        assertEquals(true, type.getPdxField("myString3").isVariableLengthType());
        assertEquals(false, type.getPdxField("myInt").isVariableLengthType());
        assertEquals(false, type.getPdxField("myFloat").isVariableLengthType());
        assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (pdx.isMyFlag() ? 1 : 0) }), reader.getRaw(0));
        assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (pdx.getMyShort() >> 8), (byte) pdx.getMyShort() }), reader.getRaw(1));
        assertEquals(ByteSourceFactory.wrap(str1Bytes), reader.getRaw(2));
        assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (pdx.getMyLong() >> 56), (byte) (pdx.getMyLong() >> 48), (byte) (pdx.getMyLong() >> 40), (byte) (pdx.getMyLong() >> 32), (byte) (pdx.getMyLong() >> 24), (byte) (pdx.getMyLong() >> 16), (byte) (pdx.getMyLong() >> 8), (byte) pdx.getMyLong() }), reader.getRaw(3));
        assertEquals(ByteSourceFactory.wrap(str2Bytes), reader.getRaw(4));
        assertEquals(ByteSourceFactory.wrap(str3Bytes), reader.getRaw(5));
        assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (pdx.getMyInt() >> 24), (byte) (pdx.getMyInt() >> 16), (byte) (pdx.getMyInt() >> 8), (byte) pdx.getMyInt() }), reader.getRaw(6));
        assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (floatBytes >> 24), (byte) (floatBytes >> 16), (byte) (floatBytes >> 8), (byte) floatBytes }), reader.getRaw(7));
    } finally {
        c.setReadSerialized(false);
    }
}
Also used : PdxType(org.apache.geode.pdx.internal.PdxType) DataInputStream(java.io.DataInputStream) DataInput(java.io.DataInput) PdxReaderImpl(org.apache.geode.pdx.internal.PdxReaderImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 60 with HeapDataOutputStream

use of org.apache.geode.internal.HeapDataOutputStream in project geode by apache.

the class PdxInstanceFactoryJUnitTest method serializeAndDeserialize.

private Object serializeAndDeserialize(Object in) throws IOException, ClassNotFoundException {
    HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeObject(in, hdos);
    byte[] bytes = hdos.toByteArray();
    // System.out.println("Serialized bytes = " + Arrays.toString(bytes));
    return DataSerializer.readObject(new DataInputStream(new ByteArrayInputStream(bytes)));
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream)

Aggregations

HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)134 Test (org.junit.Test)55 IOException (java.io.IOException)40 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)36 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)33 DataInputStream (java.io.DataInputStream)29 ByteArrayInputStream (java.io.ByteArrayInputStream)23 UnitTest (org.apache.geode.test.junit.categories.UnitTest)15 DiskAccessException (org.apache.geode.cache.DiskAccessException)12 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)11 PdxSerializerObject (org.apache.geode.internal.PdxSerializerObject)10 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)8 Version (org.apache.geode.internal.Version)8 DataInput (java.io.DataInput)7 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)7 OutputStream (java.io.OutputStream)6 Properties (java.util.Properties)6 ByteBuffer (java.nio.ByteBuffer)5 HashMap (java.util.HashMap)5 InternalGemFireException (org.apache.geode.InternalGemFireException)5