Search in sources :

Example 81 with DataInput

use of java.io.DataInput in project geode by apache.

the class RemotePutAllMessageTest method shouldBeMockable.

@Test
public void shouldBeMockable() throws Exception {
    RemotePutAllMessage mockRemotePutAllMessage = mock(RemotePutAllMessage.class);
    DataInput mockDataInput = mock(DataInput.class);
    mockRemotePutAllMessage.fromData(mockDataInput);
    verify(mockRemotePutAllMessage, times(1)).fromData(mockDataInput);
}
Also used : DataInput(java.io.DataInput) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 82 with DataInput

use of java.io.DataInput 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 83 with DataInput

use of java.io.DataInput 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 84 with DataInput

use of java.io.DataInput in project geode by apache.

the class DataSerializableJUnitTest method testLongArrayObject.

/**
   * Tests data serializing a <code>long</code> array using {@link DataSerializer#writeObject}.
   */
@Test
public void testLongArrayObject() throws Exception {
    long[] array = new long[] { 4, 5, 6 };
    DataOutputStream out = getDataOutput();
    DataSerializer.writeObject(array, out);
    out.flush();
    DataInput in = getDataInput();
    long[] array2 = (long[]) DataSerializer.readObject(in);
    assertEquals(array.length, array2.length);
    for (int i = 0; i < array.length; i++) {
        assertEquals(array[i], array2[i]);
    }
}
Also used : DataInput(java.io.DataInput) DataOutputStream(java.io.DataOutputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 85 with DataInput

use of java.io.DataInput in project geode by apache.

the class DataSerializableJUnitTest method testHashtable.

/**
   * Tests data serializing an {@link Hashtable}
   */
@Test
public void testHashtable() throws Exception {
    Random random = getRandom();
    Hashtable map = new Hashtable();
    int size = random.nextInt(50);
    for (int i = 0; i < size; i++) {
        Object key = new Long(random.nextLong());
        Object value = String.valueOf(random.nextLong());
        map.put(key, value);
    }
    DataOutputStream out = getDataOutput();
    DataSerializer.writeHashtable(map, out);
    out.flush();
    DataInput in = getDataInput();
    Hashtable map2 = DataSerializer.readHashtable(in);
    assertEquals(map, map2);
}
Also used : DataInput(java.io.DataInput) DataOutputStream(java.io.DataOutputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

DataInput (java.io.DataInput)247 Test (org.junit.Test)132 DataOutputStream (java.io.DataOutputStream)117 DataInputStream (java.io.DataInputStream)112 ByteArrayInputStream (java.io.ByteArrayInputStream)106 UnitTest (org.apache.geode.test.junit.categories.UnitTest)96 ByteArrayOutputStream (java.io.ByteArrayOutputStream)29 DataOutput (java.io.DataOutput)23 IOException (java.io.IOException)21 ArrayList (java.util.ArrayList)16 ARecord (org.apache.asterix.om.base.ARecord)14 EOFException (java.io.EOFException)9 File (java.io.File)9 BufferedInputStream (java.io.BufferedInputStream)8 HashMap (java.util.HashMap)7 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)7 MembershipTest (org.apache.geode.test.junit.categories.MembershipTest)7 GrowableArray (org.apache.hyracks.data.std.util.GrowableArray)7 FileInputStream (java.io.FileInputStream)6 ByteBuffer (java.nio.ByteBuffer)6