Search in sources :

Example 36 with HeapDataOutputStream

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

the class OldValueImporterTestBase method testValueSerialization.

@Test
public void testValueSerialization() throws Exception {
    byte[] bytes = new byte[1024];
    HeapDataOutputStream hdos = new HeapDataOutputStream(bytes);
    OldValueImporter imsg = createImporter();
    // null byte array value
    {
        OldValueImporter omsg = createImporter();
        omsg.importOldBytes(null, false);
        toData(omsg, hdos);
        fromData(imsg, bytes);
        assertEquals(null, getOldValueFromImporter(imsg));
    }
    // null object value
    {
        OldValueImporter omsg = createImporter();
        omsg.importOldObject(null, true);
        toData(omsg, hdos);
        fromData(imsg, bytes);
        assertEquals(null, getOldValueFromImporter(imsg));
    }
    // simple byte array
    {
        byte[] baValue = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        OldValueImporter omsg = createImporter();
        omsg.importOldBytes(baValue, false);
        hdos = new HeapDataOutputStream(bytes);
        toData(omsg, hdos);
        fromData(imsg, bytes);
        assertArrayEquals(baValue, (byte[]) getOldValueFromImporter(imsg));
    }
    // String in serialized form
    {
        String stringValue = "1,2,3,4,5,6,7,8,9";
        byte[] stringValueBlob = EntryEventImpl.serialize(stringValue);
        OldValueImporter omsg = createImporter();
        omsg.importOldBytes(stringValueBlob, true);
        hdos = new HeapDataOutputStream(bytes);
        toData(omsg, hdos);
        fromData(imsg, bytes);
        assertArrayEquals(stringValueBlob, ((VMCachedDeserializable) getOldValueFromImporter(imsg)).getSerializedValue());
    }
    // String in object form
    {
        String stringValue = "1,2,3,4,5,6,7,8,9";
        byte[] stringValueBlob = EntryEventImpl.serialize(stringValue);
        OldValueImporter omsg = createImporter();
        omsg.importOldObject(stringValue, true);
        hdos = new HeapDataOutputStream(bytes);
        toData(omsg, hdos);
        fromData(imsg, bytes);
        assertArrayEquals(stringValueBlob, ((VMCachedDeserializable) getOldValueFromImporter(imsg)).getSerializedValue());
    }
    // off-heap DataAsAddress byte array
    {
        MemoryAllocatorImpl sma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[] { new SlabImpl(1024 * 1024) });
        try {
            byte[] baValue = new byte[] { 1, 2 };
            TinyStoredObject baValueSO = (TinyStoredObject) sma.allocateAndInitialize(baValue, false, false);
            OldValueImporter omsg = createImporter();
            omsg.importOldObject(baValueSO, false);
            hdos = new HeapDataOutputStream(bytes);
            toData(omsg, hdos);
            fromData(imsg, bytes);
            assertArrayEquals(baValue, (byte[]) getOldValueFromImporter(imsg));
        } finally {
            MemoryAllocatorImpl.freeOffHeapMemory();
        }
    }
    // off-heap Chunk byte array
    {
        MemoryAllocatorImpl sma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[] { new SlabImpl(1024 * 1024) });
        try {
            byte[] baValue = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 };
            OffHeapStoredObject baValueSO = (OffHeapStoredObject) sma.allocateAndInitialize(baValue, false, false);
            OldValueImporter omsg = createImporter();
            omsg.importOldObject(baValueSO, false);
            hdos = new HeapDataOutputStream(bytes);
            toData(omsg, hdos);
            fromData(imsg, bytes);
            assertArrayEquals(baValue, (byte[]) getOldValueFromImporter(imsg));
        } finally {
            MemoryAllocatorImpl.freeOffHeapMemory();
        }
    }
    // off-heap DataAsAddress String
    {
        MemoryAllocatorImpl sma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[] { new SlabImpl(1024 * 1024) });
        try {
            String baValue = "12";
            byte[] baValueBlob = BlobHelper.serializeToBlob(baValue);
            TinyStoredObject baValueSO = (TinyStoredObject) sma.allocateAndInitialize(baValueBlob, true, false);
            OldValueImporter omsg = createImporter();
            omsg.importOldObject(baValueSO, true);
            hdos = new HeapDataOutputStream(bytes);
            toData(omsg, hdos);
            fromData(imsg, bytes);
            assertArrayEquals(baValueBlob, ((VMCachedDeserializable) getOldValueFromImporter(imsg)).getSerializedValue());
        } finally {
            MemoryAllocatorImpl.freeOffHeapMemory();
        }
    }
    // off-heap Chunk String
    {
        MemoryAllocatorImpl sma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[] { new SlabImpl(1024 * 1024) });
        try {
            String baValue = "12345678";
            byte[] baValueBlob = BlobHelper.serializeToBlob(baValue);
            OffHeapStoredObject baValueSO = (OffHeapStoredObject) sma.allocateAndInitialize(baValueBlob, true, false);
            OldValueImporter omsg = createImporter();
            omsg.importOldObject(baValueSO, true);
            hdos = new HeapDataOutputStream(bytes);
            toData(omsg, hdos);
            fromData(imsg, bytes);
            assertArrayEquals(baValueBlob, ((VMCachedDeserializable) getOldValueFromImporter(imsg)).getSerializedValue());
        } finally {
            MemoryAllocatorImpl.freeOffHeapMemory();
        }
    }
}
Also used : OffHeapStoredObject(org.apache.geode.internal.offheap.OffHeapStoredObject) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) OldValueImporter(org.apache.geode.internal.cache.EntryEventImpl.OldValueImporter) MemoryAllocatorImpl(org.apache.geode.internal.offheap.MemoryAllocatorImpl) NullOutOfOffHeapMemoryListener(org.apache.geode.internal.offheap.NullOutOfOffHeapMemoryListener) SlabImpl(org.apache.geode.internal.offheap.SlabImpl) NullOffHeapMemoryStats(org.apache.geode.internal.offheap.NullOffHeapMemoryStats) TinyStoredObject(org.apache.geode.internal.offheap.TinyStoredObject) Test(org.junit.Test)

Example 37 with HeapDataOutputStream

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

the class PersistentRecoveryOrderDUnitTest method getRVV.

protected RegionVersionVector getRVV(VM vm) throws IOException, ClassNotFoundException {
    SerializableCallable createData = new SerializableCallable("getRVV") {

        public Object call() throws Exception {
            Cache cache = getCache();
            LocalRegion region = (LocalRegion) cache.getRegion(REGION_NAME);
            RegionVersionVector rvv = region.getVersionVector();
            rvv = rvv.getCloneForTransmission();
            HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
            // Using gemfire serialization because
            // RegionVersionVector is not java serializable
            DataSerializer.writeObject(rvv, hdos);
            return hdos.toByteArray();
        }
    };
    byte[] result = (byte[]) vm.invoke(createData);
    ByteArrayInputStream bais = new ByteArrayInputStream(result);
    return DataSerializer.readObject(new DataInputStream(bais));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) RegionVersionVector(org.apache.geode.internal.cache.versions.RegionVersionVector) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DataInputStream(java.io.DataInputStream) Cache(org.apache.geode.cache.Cache)

Example 38 with HeapDataOutputStream

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

the class OffHeapWriteObjectAsByteArrayJUnitTest method testByteArrayDataAsAddress.

@Test
public void testByteArrayDataAsAddress() throws IOException, ClassNotFoundException {
    byte[] expected = new byte[] { 1, 2, 3 };
    StoredObject so = createStoredObject(expected, false, false);
    assertTrue(so instanceof TinyStoredObject);
    HeapDataOutputStream hdos = new HeapDataOutputStream(new byte[1024]);
    DataSerializer.writeObjectAsByteArray(so, hdos);
    DataInputStream in = createInput(hdos);
    byte[] actual = DataSerializer.readByteArray(in);
    assertArrayEquals(expected, actual);
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) DataInputStream(java.io.DataInputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 39 with HeapDataOutputStream

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

the class OffHeapWriteObjectAsByteArrayJUnitTest method testStringDataAsAddress.

@Test
public void testStringDataAsAddress() throws IOException, ClassNotFoundException {
    byte[] expected = EntryEventImpl.serialize("1234");
    StoredObject so = createStoredObject(expected, true, false);
    assertTrue(so instanceof TinyStoredObject);
    HeapDataOutputStream hdos = new HeapDataOutputStream(new byte[1024]);
    DataSerializer.writeObjectAsByteArray(so, hdos);
    DataInputStream in = createInput(hdos);
    byte[] actual = DataSerializer.readByteArray(in);
    assertArrayEquals(expected, actual);
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) DataInputStream(java.io.DataInputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 40 with HeapDataOutputStream

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

the class OffHeapWriteObjectAsByteArrayJUnitTest method testByteArrayChunk.

@Test
public void testByteArrayChunk() throws IOException, ClassNotFoundException {
    byte[] expected = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
    StoredObject so = createStoredObject(expected, false, false);
    assertTrue(so instanceof OffHeapStoredObject);
    HeapDataOutputStream hdos = new HeapDataOutputStream(new byte[1024]);
    DataSerializer.writeObjectAsByteArray(so, hdos);
    DataInputStream in = createInput(hdos);
    byte[] actual = DataSerializer.readByteArray(in);
    assertArrayEquals(expected, actual);
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) DataInputStream(java.io.DataInputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

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