Search in sources :

Example 26 with HeapDataOutputStream

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

the class AutoSerializableJUnitTest method testSanity.

/*
   * Test basic functionality.
   */
@Test
public void testSanity() throws Exception {
    setupSerializer("org.apache.geode.pdx.DomainObjectPdxAuto");
    DomainObject objOut = new DomainObjectPdxAuto(4);
    objOut.set("string_0", "test string value");
    objOut.set("long_0", 99L);
    objOut.set("string_immediate", "right now");
    objOut.set("anEnum", DomainObjectPdxAuto.Day.FRIDAY);
    List<String> list = new ArrayList<String>(4);
    list.add("string one");
    list.add("string two");
    list.add("string three");
    list.add("string four");
    objOut.set("string_list", list);
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeObject(objOut, out);
    PdxInstance pdxIn = DataSerializer.readObject(new DataInputStream(new ByteArrayInputStream(out.toByteArray())));
    assertEquals(99L, pdxIn.getField("long_0"));
    assertEquals("test string value", pdxIn.getField("string_0"));
    assertEquals("right now", pdxIn.getField("string_immediate"));
    {
        PdxInstance epi = (PdxInstance) pdxIn.getField("anEnum");
        assertEquals(true, epi.isEnum());
        assertEquals(true, epi.hasField("name"));
        assertEquals(true, epi.hasField("ordinal"));
        assertEquals("org.apache.geode.pdx.DomainObjectPdxAuto$Day", epi.getClassName());
        assertEquals("FRIDAY", epi.getField("name"));
        assertEquals(DomainObjectPdxAuto.Day.FRIDAY.ordinal(), epi.getField("ordinal"));
        assertEquals(DomainObjectPdxAuto.Day.FRIDAY, epi.getObject());
    }
    assertEquals(4, ((List) pdxIn.getField("string_list")).size());
    {
        DomainObjectPdxAuto result = (DomainObjectPdxAuto) pdxIn.getObject();
        assertEquals(99L, result.get("long_0"));
        assertEquals("test string value", result.get("string_0"));
        assertEquals("right now", result.get("string_immediate"));
        assertEquals(DomainObjectPdxAuto.Day.FRIDAY, result.get("anEnum"));
        assertEquals(4, ((List) result.get("string_list")).size());
    }
    // disable pdx instances to make sure we can deserialize without calling PdxInstance.getObject
    PdxInstanceImpl.setPdxReadSerialized(false);
    try {
        DomainObjectPdxAuto result = (DomainObjectPdxAuto) DataSerializer.readObject(new DataInputStream(new ByteArrayInputStream(out.toByteArray())));
        assertEquals(99L, result.get("long_0"));
        assertEquals("test string value", result.get("string_0"));
        assertEquals("right now", result.get("string_immediate"));
        assertEquals(DomainObjectPdxAuto.Day.FRIDAY, result.get("anEnum"));
        assertEquals(4, ((List) result.get("string_list")).size());
    } finally {
        PdxInstanceImpl.setPdxReadSerialized(true);
    }
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 27 with HeapDataOutputStream

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

the class AutoSerializableJUnitTest method testException.

/*
   * Check that an exception is appropriately thrown for a 'bad' object. In this case the bad class
   * masks a field from a superclass and defines it as a different type.
   */
@Test
public void testException() throws Exception {
    setupSerializer(stdSerializableClasses);
    DomainObject objOut = new DomainObjectBad();
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    try {
        DataSerializer.writeObject(objOut, out);
    } catch (SerializationException ex) {
    // Pass
    } catch (Exception ex) {
    }
}
Also used : SerializationException(org.apache.geode.SerializationException) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) SerializationException(org.apache.geode.SerializationException) MalformedURLException(java.net.MalformedURLException) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 28 with HeapDataOutputStream

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

the class AutoSerializableJUnitTest method testNoZeroArgConstructor.

/*
   * Test what happens with a class without a zero-arg constructor
   */
@Test
public void testNoZeroArgConstructor() throws Exception {
    setupSerializer(stdSerializableClasses);
    DomainObject objOut = new DomainObjectPdxAutoNoDefaultConstructor(4);
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    try {
        DataSerializer.writeObject(objOut, out);
    } catch (NotSerializableException ex) {
    // This passes the test
    } catch (Exception ex) {
        throw new RuntimeException("Expected NotSerializableException here but got " + ex);
    }
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) SerializationException(org.apache.geode.SerializationException) MalformedURLException(java.net.MalformedURLException) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 29 with HeapDataOutputStream

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

the class AutoSerializableJUnitTest method testGetFieldName.

@Test
public void testGetFieldName() throws IOException, ClassNotFoundException {
    setupSerializer(new ExplicitFieldNameAutoSerializer(false, "org.apache.geode.pdx.DomainObjectPdxAuto"), true);
    DomainObject objOut = new DomainObjectPdxAuto(4);
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeObject(objOut, out);
    PdxInstance pi = (PdxInstance) DataSerializer.readObject(new DataInputStream(new ByteArrayInputStream(out.toByteArray())));
    System.out.println("fieldNames=" + pi.getFieldNames());
    assertEquals(false, pi.hasField("long_0"));
    assertEquals(true, pi.hasField("_long_0"));
    assertEquals(true, pi.hasField("string_0"));
    assertEquals(objOut, pi.getObject());
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 30 with HeapDataOutputStream

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

the class AutoSerializableJUnitTest method testReadNullPrimitives.

/*
   * Test the versioning of basic primitives where no fields are written at first, but then all
   * fields need to be read back.
   */
@Test
public void testReadNullPrimitives() throws Exception {
    setupSerializer(stdSerializableClasses);
    // Don't want to write any fields
    manager.addExcludePattern(".*DomainObjectPdxAuto", "a(Char|Boolean|Byte|Short|Int|Long|Float|Double)");
    DomainObject objOut = new DomainObjectPdxAuto(4);
    objOut.set("aString", "aString has a value");
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeObject(objOut, out);
    // Now we want to read all fields.
    manager.resetCaches();
    PdxInstance pdxIn = DataSerializer.readObject(new DataInputStream(new ByteArrayInputStream(out.toByteArray())));
    // Force the object to be de-serialized without any exceptions being thrown
    DomainObjectPdxAuto result = (DomainObjectPdxAuto) pdxIn.getObject();
    assertEquals('', result.aChar);
    assertFalse(result.aBoolean);
    assertEquals(0, result.aByte);
    assertEquals(0, result.aShort);
    assertEquals(0, result.anInt);
    assertEquals(0L, result.aLong);
    assertEquals(0.0f, result.aFloat, 0.0f);
    assertEquals(0.0d, result.aDouble, 0.0f);
    assertEquals("aString has a value", result.get("aString"));
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

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