Search in sources :

Example 11 with DataSerializable

use of org.apache.geode.DataSerializable in project geode by apache.

the class DataSerializableJUnitTest method testInstantiator4.

@Test
public void testInstantiator4() throws Exception {
    final boolean[] wasInvoked = new boolean[] { false };
    Instantiator.register(new Instantiator(DataSerializableImpl.class, 123456789) {

        public DataSerializable newInstance() {
            wasInvoked[0] = true;
            return new DataSerializableImpl();
        }
    });
    try {
        byte id = (byte) 57;
        Class_testInstantiator.supClass = DataSerializableImpl.class;
        DataSerializer.register(Class_testInstantiator.class);
        try {
            Object o = new DataSerializableImpl(new Random());
            DataSerializer.writeObject(o, getDataOutput());
            Object o2 = DataSerializer.readObject(getDataInput());
            assertTrue(wasInvoked[0]);
            assertEquals(o, o2);
        } finally {
            InternalDataSerializer.unregister(id);
        }
    } finally {
        InternalInstantiator.unregister(DataSerializableImpl.class, 123456789);
    }
}
Also used : CanonicalInstantiator(org.apache.geode.CanonicalInstantiator) Instantiator(org.apache.geode.Instantiator) DataSerializable(org.apache.geode.DataSerializable) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 12 with DataSerializable

use of org.apache.geode.DataSerializable in project geode by apache.

the class DataSerializableJUnitTest method testInstantiator.

/**
   * Tests that an <code>Instantiator</code> is invoked at the appropriate times.
   */
@Test
public void testInstantiator() throws Exception {
    final boolean[] wasInvoked = new boolean[] { false };
    Instantiator.register(new Instantiator(DataSerializableImpl.class, (byte) 45) {

        public DataSerializable newInstance() {
            wasInvoked[0] = true;
            return new DataSerializableImpl();
        }
    });
    try {
        byte id = (byte) 57;
        Class_testInstantiator.supClass = DataSerializableImpl.class;
        DataSerializer.register(Class_testInstantiator.class);
        try {
            Object o = new DataSerializableImpl(new Random());
            DataSerializer.writeObject(o, getDataOutput());
            Object o2 = DataSerializer.readObject(getDataInput());
            assertTrue(wasInvoked[0]);
            assertEquals(o, o2);
        } finally {
            InternalDataSerializer.unregister(id);
        }
    } finally {
        InternalInstantiator.unregister(DataSerializableImpl.class, (byte) 45);
    }
}
Also used : CanonicalInstantiator(org.apache.geode.CanonicalInstantiator) Instantiator(org.apache.geode.Instantiator) DataSerializable(org.apache.geode.DataSerializable) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 13 with DataSerializable

use of org.apache.geode.DataSerializable in project geode by apache.

the class PdxSerializableJUnitTest method testByteFormatForDSInsidePDX.

@Test
public void testByteFormatForDSInsidePDX() throws Exception {
    String myString1 = "ComplexClass1_myString1";
    long myLong = 15654;
    DataSerializable myDS = new DeltaTestImpl(100, "value");
    String myString2 = "ComplexClass1_myString2";
    float myFloat = 123.023f;
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    PdxSerializable pdx = new DSInsidePdx(myString1, myLong, myDS, myString2, myFloat);
    DataSerializer.writeObject(pdx, out);
    int typeId = getPdxTypeIdForClass(DSInsidePdx.class);
    HeapDataOutputStream hdos1 = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeString(myString1, hdos1);
    byte[] str1Bytes = hdos1.toByteArray();
    System.out.println("Length of string1: " + str1Bytes.length);
    HeapDataOutputStream hdos2 = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeObject(myDS, hdos2);
    byte[] dsBytes = hdos2.toByteArray();
    System.out.println("Length of DS: " + dsBytes.length);
    HeapDataOutputStream hdos3 = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeString(myString2, hdos3);
    byte[] str2Bytes = hdos3.toByteArray();
    System.out.println("Length of string2: " + str2Bytes.length);
    int length = str1Bytes.length + 8 + /* myLong */
    dsBytes.length + str2Bytes.length + 4 + /* myFloat */
    (2 * 2);
    int offset1 = 0;
    int offset2 = offset1 + 8 + str1Bytes.length;
    int offset3 = offset1 + 8 + str1Bytes.length + dsBytes.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 >> 8), // offset of myString2
    (byte) offset3, // offset of myHashMap
    (byte) (offset2 >> 8), // 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]);
    // 5
    // for:
    // 1
    // for
    // DSCODE.PDX
    // and
    // 4
    // for
    // byte
    // stream
    // length
    }
    for (int i = (dsBytes.length - 1); i >= 0; i--) {
        expected = (Byte[]) ArrayUtils.insert(expected, offset2 + PdxWriterImpl.HEADER_SIZE, dsBytes[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 + ", ");
    }
    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));
    DSInsidePdx actualVal = (DSInsidePdx) 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 : DeltaTestImpl(org.apache.geode.DeltaTestImpl) DataInputStream(java.io.DataInputStream) DataSerializable(org.apache.geode.DataSerializable) 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 14 with DataSerializable

use of org.apache.geode.DataSerializable in project geode by apache.

the class PdxSerializableJUnitTest method testByteFormatForPDXInsideDS.

@Test
public void testByteFormatForPDXInsideDS() throws Exception {
    String myString1 = "ComplexClass5_myString1";
    long myLong = 15654;
    String myString2 = "ComplexClass5_myString2";
    float myFloat = 123.023f;
    HashMap<String, PdxSerializable> myHashMap = new HashMap<String, PdxSerializable>();
    for (int i = 0; i < 2; i++) {
        myHashMap.put("KEY_" + i, new SimpleClass(i, (byte) i));
    }
    PdxSerializable myPDX = new NestedPdx(myString1, myLong, myHashMap, myString2, myFloat);
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    DataSerializable ds = new PdxInsideDS(myString1, myLong, myPDX, myString2);
    DataSerializer.writeObject(ds, out);
    HeapDataOutputStream hdosString1 = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeString(myString1, hdosString1);
    byte[] str1Bytes = hdosString1.toByteArray();
    System.out.println("Length of string1: " + str1Bytes.length);
    HeapDataOutputStream hdosMyPDX = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeObject(myPDX, hdosMyPDX);
    byte[] pdxBytes = hdosMyPDX.toByteArray();
    System.out.println("Length of myPDX: " + pdxBytes.length);
    HeapDataOutputStream hdosString2 = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeString(myString2, hdosString2);
    byte[] str2Bytes = hdosString2.toByteArray();
    System.out.println("Length of string2: " + str2Bytes.length);
    Class classInstance = ds.getClass();
    HeapDataOutputStream hdosClass = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeClass(classInstance, hdosClass);
    byte[] dsInitBytes = hdosClass.toByteArray();
    int offsetStr1 = 1 + dsInitBytes.length;
    int offsetPDX = 1 + dsInitBytes.length + str1Bytes.length + 8;
    int offsetStr2 = 1 + dsInitBytes.length + str1Bytes.length + 8 + pdxBytes.length;
    byte[] actual = out.toByteArray();
    int floatBytes = Float.floatToRawIntBits(myFloat);
    Byte[] expected = new Byte[] { // byte
    DSCODE.DATA_SERIALIZABLE, (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 };
    for (int i = (dsInitBytes.length - 1); i >= 0; i--) {
        expected = (Byte[]) ArrayUtils.insert(expected, 1, dsInitBytes[i]);
    }
    for (int i = (str1Bytes.length - 1); i >= 0; i--) {
        expected = (Byte[]) ArrayUtils.insert(expected, offsetStr1, str1Bytes[i]);
    }
    for (int i = (pdxBytes.length - 1); i >= 0; i--) {
        expected = (Byte[]) ArrayUtils.insert(expected, offsetPDX, pdxBytes[i]);
    }
    for (int i = (str2Bytes.length - 1); i >= 0; i--) {
        expected = (Byte[]) ArrayUtils.insert(expected, offsetStr2, str2Bytes[i]);
    }
    checkBytes(expected, actual);
    DataInput in = new DataInputStream(new ByteArrayInputStream(actual));
    PdxInsideDS actualVal = (PdxInsideDS) DataSerializer.readObject(in);
    // System.out.println("actualVal..."+actualVal);
    assertTrue("Mismatch in write and read value: Value Write..." + ds + " Value Read..." + actualVal, ds.equals(actualVal));
}
Also used : HashMap(java.util.HashMap) DataInputStream(java.io.DataInputStream) DataSerializable(org.apache.geode.DataSerializable) 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)

Aggregations

DataSerializable (org.apache.geode.DataSerializable)14 Test (org.junit.Test)9 CanonicalInstantiator (org.apache.geode.CanonicalInstantiator)6 UnitTest (org.apache.geode.test.junit.categories.UnitTest)6 IOException (java.io.IOException)5 Instantiator (org.apache.geode.Instantiator)5 DataInput (java.io.DataInput)4 NotSerializableException (java.io.NotSerializableException)4 ObjectStreamClass (java.io.ObjectStreamClass)4 CancelException (org.apache.geode.CancelException)4 GemFireIOException (org.apache.geode.GemFireIOException)4 ToDataException (org.apache.geode.ToDataException)4 EOFException (java.io.EOFException)3 UTFDataFormatException (java.io.UTFDataFormatException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 SerializationException (org.apache.geode.SerializationException)3 CacheClosedException (org.apache.geode.cache.CacheClosedException)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2