Search in sources :

Example 41 with DataInput

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

the class DataSerializableJUnitTest method testStackObject.

/**
   * Tests data serializing an {@link Stack} using {@link DataSerializer#writeObject}.
   */
@Test
public void testStackObject() throws Exception {
    Random random = getRandom();
    Stack list = new Stack();
    int size = random.nextInt(50);
    for (int i = 0; i < size; i++) {
        list.add(new Long(random.nextLong()));
    }
    DataOutputStream out = getDataOutput();
    DataSerializer.writeObject(list, out);
    out.flush();
    DataInput in = getDataInput();
    Stack list2 = (Stack) DataSerializer.readObject(in);
    assertEquals(list, list2);
}
Also used : DataInput(java.io.DataInput) DataOutputStream(java.io.DataOutputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 42 with DataInput

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

the class DataSerializableJUnitTest method testIdentityHashMap.

/**
   * Tests data serializing an {@link IdentityHashMap}
   */
@Test
public void testIdentityHashMap() throws Exception {
    Random random = getRandom();
    IdentityHashMap map = new IdentityHashMap();
    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.writeIdentityHashMap(map, out);
    out.flush();
    DataInput in = getDataInput();
    IdentityHashMap map2 = DataSerializer.readIdentityHashMap(in);
    assertEquals(new HashMap(map), new HashMap(map2));
}
Also used : DataInput(java.io.DataInput) DataOutputStream(java.io.DataOutputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 43 with DataInput

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

the class DataSerializableJUnitTest method testStringEncodingLengthCrossesBoundry.

/**
   * Tests data serializing a non-<code>null</code> String whose length is > 0xFFFF, but who's utf-8
   * encoded length is < 0xFFFF See bug 40932.
   */
@Test
public void testStringEncodingLengthCrossesBoundry() throws Exception {
    StringBuffer sb = new StringBuffer(0xFFFF);
    for (int i = 0; i < 0xFFFF; i++) {
        if (i == 0) {
            sb.append(Character.MAX_VALUE);
        } else {
            sb.append("a");
        }
    }
    String value = sb.toString();
    DataOutputStream out = getDataOutput();
    DataSerializer.writeString(value, out);
    DataSerializer.writeObject(value, out);
    out.flush();
    DataInput in = getDataInput();
    String value2 = DataSerializer.readString(in);
    assertEquals(value, value2);
    value2 = (String) DataSerializer.readObject(in);
    assertEquals(value, value2);
}
Also used : DataInput(java.io.DataInput) DataOutputStream(java.io.DataOutputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 44 with DataInput

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

the class DataSerializableJUnitTest method testEnum.

/**
   * Tests Dataserializing an Enum
   */
@Test
public void testEnum() throws Exception {
    DAY_OF_WEEK e = DAY_OF_WEEK.SUN;
    MONTH m = MONTH.FEB;
    DataOutputStream out = getDataOutput();
    DataSerializer.writeEnum(e, out);
    DataSerializer.writeEnum(m, out);
    try {
        DataSerializer.writeEnum(null, out);
        fail("Expected exception not thrown");
    } catch (NullPointerException ex) {
    }
    out.flush();
    DataInput in = getDataInput();
    Class c = null;
    try {
        DataSerializer.readEnum(c, in);
        fail("Expected exception not thrown");
    } catch (NullPointerException ex) {
    }
    c = Foo.class;
    try {
        DataSerializer.readEnum(c, in);
        fail("Expected exception not thrown");
    } catch (IllegalArgumentException ex) {
    }
    DAY_OF_WEEK e2 = DataSerializer.readEnum(DAY_OF_WEEK.class, in);
    MONTH m2 = DataSerializer.readEnum(MONTH.class, in);
    assertEquals(e, e2);
    assertEquals(m, m2);
}
Also used : DataInput(java.io.DataInput) DataOutputStream(java.io.DataOutputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 45 with DataInput

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

the class DataSerializableJUnitTest method testByteObject.

/**
   * Tests data serializing a {@link Byte} using {@link DataSerializer#writeObject}.
   */
@Test
public void testByteObject() throws Exception {
    Byte value = new Byte((byte) getRandom().nextInt());
    DataOutputStream out = getDataOutput();
    DataSerializer.writeObject(value, out);
    out.flush();
    DataInput in = getDataInput();
    Byte value2 = (Byte) DataSerializer.readObject(in);
    assertEquals(value, value2);
}
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)295 Test (org.junit.Test)141 DataInputStream (java.io.DataInputStream)139 ByteArrayInputStream (java.io.ByteArrayInputStream)125 DataOutputStream (java.io.DataOutputStream)122 UnitTest (org.apache.geode.test.junit.categories.UnitTest)96 IOException (java.io.IOException)39 ByteArrayOutputStream (java.io.ByteArrayOutputStream)34 DataOutput (java.io.DataOutput)26 ArrayList (java.util.ArrayList)17 ARecord (org.apache.asterix.om.base.ARecord)14 BufferedInputStream (java.io.BufferedInputStream)11 EOFException (java.io.EOFException)11 ByteBuffer (java.nio.ByteBuffer)10 BigInteger (java.math.BigInteger)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 ChannelDataInput (org.apache.sis.internal.storage.io.ChannelDataInput)7