Search in sources :

Example 41 with HeapDataOutputStream

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

the class OffHeapWriteObjectAsByteArrayJUnitTest method testStringChunk.

@Test
public void testStringChunk() throws IOException, ClassNotFoundException {
    byte[] expected = EntryEventImpl.serialize("1234567890");
    StoredObject so = createStoredObject(expected, true, 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);
    assertNoMoreInput(in);
}
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 42 with HeapDataOutputStream

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

the class OffHeapStoredObjectJUnitTest method sendToShouldWriteSerializedValueToDataOutputIfValueIsSerialized.

@Test
public void sendToShouldWriteSerializedValueToDataOutputIfValueIsSerialized() throws IOException {
    OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
    OffHeapStoredObject spyChunk = spy(chunk);
    HeapDataOutputStream dataOutput = mock(HeapDataOutputStream.class);
    ByteBuffer directByteBuffer = ByteBuffer.allocate(1024);
    doReturn(directByteBuffer).when(spyChunk).createDirectByteBuffer();
    doNothing().when(dataOutput).write(directByteBuffer);
    spyChunk.sendTo(dataOutput);
    verify(dataOutput, times(1)).write(directByteBuffer);
    chunk.release();
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 43 with HeapDataOutputStream

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

the class OffHeapStoredObjectJUnitTest method sendAsByteArrayShouldWriteValueToDataOutput.

@Test
public void sendAsByteArrayShouldWriteValueToDataOutput() throws IOException {
    byte[] regionEntryValue = getValueAsByteArray();
    OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(regionEntryValue);
    // writeByte is a final method and cannot be mocked, so creating a real one
    HeapDataOutputStream dataOutput = new HeapDataOutputStream(Version.CURRENT);
    chunk.sendAsByteArray(dataOutput);
    byte[] actual = dataOutput.toByteArray();
    byte[] expected = new byte[regionEntryValue.length + 1];
    expected[0] = (byte) regionEntryValue.length;
    System.arraycopy(regionEntryValue, 0, expected, 1, regionEntryValue.length);
    assertNotNull(dataOutput);
    assertThat(actual).isEqualTo(expected);
    chunk.release();
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 44 with HeapDataOutputStream

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

the class OffHeapStoredObjectJUnitTest method sendToShouldWriteUnserializedValueToDataOutputIfValueIsUnserialized.

@Test
public void sendToShouldWriteUnserializedValueToDataOutputIfValueIsUnserialized() throws IOException {
    byte[] regionEntryValue = getValueAsByteArray();
    OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(regionEntryValue);
    // writeByte is a final method and cannot be mocked, so creating a real one
    HeapDataOutputStream dataOutput = new HeapDataOutputStream(Version.CURRENT);
    chunk.sendTo(dataOutput);
    byte[] actual = dataOutput.toByteArray();
    byte[] expected = new byte[regionEntryValue.length + 2];
    expected[0] = DSCODE.BYTE_ARRAY;
    expected[1] = (byte) regionEntryValue.length;
    System.arraycopy(regionEntryValue, 0, expected, 2, regionEntryValue.length);
    assertNotNull(dataOutput);
    assertThat(actual).isEqualTo(expected);
    chunk.release();
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 45 with HeapDataOutputStream

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

the class OffHeapValidationJUnitTest method putCompressedString.

private void putCompressedString(Region<Object, Object> region, List<ExpectedValues> expected) throws IOException {
    String key = "keyString";
    String value = "this is a string";
    region.put(key, value);
    HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeObject(value, hdos);
    byte[] uncompressedBytes = hdos.toByteArray();
    byte[] expectedValue = SnappyCompressor.getDefaultInstance().compress(uncompressedBytes);
    expected.add(new ExpectedValues(expectedValue, 32, "compressed object of size " + expectedValue.length, -1, getMemoryAddress(region, key), 1, 0, true, true));
}
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