Search in sources :

Example 6 with DataSerializer

use of org.apache.geode.DataSerializer in project geode by apache.

the class DataSerializableJUnitTest method testRegistrationListeners.

/**
   * Tests that <code>RegistrationListener</code>s are invoked at the proper times.
   */
@Test
public void testRegistrationListeners() {
    final DataSerializer[] array = new DataSerializer[2];
    TestRegistrationListener l1 = new TestRegistrationListener() {

        @Override
        public void newDataSerializer2(DataSerializer ds) {
            array[0] = ds;
        }
    };
    TestRegistrationListener l2 = new TestRegistrationListener() {

        @Override
        public void newDataSerializer2(DataSerializer ds) {
            array[1] = ds;
        }
    };
    InternalDataSerializer.addRegistrationListener(l1);
    InternalDataSerializer.addRegistrationListener(l2);
    byte id = (byte) 42;
    try {
        DataSerializer ds = DataSerializer.register(DS42.class);
        assertTrue(l1.wasInvoked());
        assertSame(ds, array[0]);
        assertTrue(l2.wasInvoked());
        assertSame(ds, array[1]);
    } finally {
        InternalDataSerializer.unregister(id);
        InternalDataSerializer.removeRegistrationListener(l1);
        InternalDataSerializer.removeRegistrationListener(l2);
    }
    Class c = DataSerializableImpl.class;
    id = (byte) 100;
    final Instantiator inst0 = new Instantiator(c, id) {

        @Override
        public DataSerializable newInstance() {
            return new DataSerializableImpl();
        }
    };
    TestRegistrationListener l3 = new TestRegistrationListener() {

        @Override
        public void newInstantiator2(Instantiator inst) {
            assertEquals(inst0, inst);
        }
    };
    TestRegistrationListener l4 = new TestRegistrationListener() {

        @Override
        public void newInstantiator2(Instantiator inst) {
            assertEquals(inst0, inst);
        }
    };
    InternalDataSerializer.addRegistrationListener(l3);
    InternalDataSerializer.addRegistrationListener(l4);
    try {
        Instantiator.register(inst0);
        assertTrue(l3.wasInvoked());
        assertTrue(l4.wasInvoked());
    } finally {
        InternalInstantiator.unregister(c, id);
        InternalDataSerializer.removeRegistrationListener(l3);
        InternalDataSerializer.removeRegistrationListener(l4);
    }
}
Also used : CanonicalInstantiator(org.apache.geode.CanonicalInstantiator) Instantiator(org.apache.geode.Instantiator) DataSerializer(org.apache.geode.DataSerializer) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 7 with DataSerializer

use of org.apache.geode.DataSerializer in project geode by apache.

the class DataSerializableJUnitTest method testRegisterTwoSerializers.

/**
   * Tests that registering two <code>Serializer</code>s with the same id throws an exception.
   */
@Test
public void testRegisterTwoSerializers() {
    byte id = (byte) 42;
    DataSerializer.register(DS42.class);
    DataSerializer serializer2 = new DS42() {
    };
    try {
        DataSerializer.register(serializer2.getClass());
        fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
    // pass...
    } finally {
        InternalDataSerializer.unregister(id);
    }
}
Also used : DataSerializer(org.apache.geode.DataSerializer) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 8 with DataSerializer

use of org.apache.geode.DataSerializer in project geode by apache.

the class DataSerializableJUnitTest method testUDDS2.

/**
   * Make sure a user defined ds with an id of 2 bytes works.
   */
@Test
public void testUDDS2() throws Exception {
    DataSerializer ds2 = DataSerializer.register(Class_testSupportedClasses3.class);
    int id2 = ds2.getId();
    try {
        Object o = new NonDataSerializable(new Random());
        DataSerializer.writeObject(o, getDataOutput());
        assertTrue(Class_testSupportedClasses3.wasInvoked);
        assertTrue(Class_testSupportedClasses3.toDataInvoked);
        assertFalse(Class_testSupportedClasses3.fromDataInvoked);
        Object o2 = DataSerializer.readObject(getDataInput());
        assertTrue(Class_testSupportedClasses3.fromDataInvoked);
        assertEquals(o, o2);
    } finally {
        InternalDataSerializer.unregister(id2);
    }
}
Also used : DataSerializer(org.apache.geode.DataSerializer) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 9 with DataSerializer

use of org.apache.geode.DataSerializer in project geode by apache.

the class DSObjectLocalOnly method verifyDataSerializers.

public static void verifyDataSerializers(final int numOfDataSerializers, final boolean allowNonLocal) {
    WaitCriterion wc = new StoppableWaitCriterion() {

        String excuse;

        private DataSerializer[] getSerializers() {
            allowNonLocalTL.set(allowNonLocal);
            try {
                return InternalDataSerializer.getSerializers();
            } finally {
                allowNonLocalTL.remove();
            }
        }

        public boolean done() {
            return getSerializers().length == numOfDataSerializers;
        }

        public String description() {
            return "expected " + numOfDataSerializers + " but got this " + InternalDataSerializer.getSerializers().length + " serializers=" + java.util.Arrays.toString(getSerializers());
        }

        public boolean stopWaiting() {
            return getSerializers().length > numOfDataSerializers;
        }
    };
    Wait.waitForCriterion(wc, 60 * 1000, 1000, true);
}
Also used : StoppableWaitCriterion(org.apache.geode.test.dunit.StoppableWaitCriterion) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) StoppableWaitCriterion(org.apache.geode.test.dunit.StoppableWaitCriterion) DataSerializer(org.apache.geode.DataSerializer) TestDataSerializer(org.apache.geode.TestDataSerializer) InternalDataSerializer(org.apache.geode.internal.InternalDataSerializer)

Example 10 with DataSerializer

use of org.apache.geode.DataSerializer in project geode by apache.

the class DiskInitFile method cmnDataSerializerId.

public void cmnDataSerializerId(Class dsc) {
    if (dsc != null) {
        DataSerializer ds = InternalDataSerializer.register(dsc, /* dsId, */
        true);
        this.dsIds.add(ds.getId());
    }
    this.ifLiveRecordCount++;
    this.ifTotalRecordCount++;
}
Also used : DataSerializer(org.apache.geode.DataSerializer) InternalDataSerializer(org.apache.geode.internal.InternalDataSerializer)

Aggregations

DataSerializer (org.apache.geode.DataSerializer)19 ObjectStreamClass (java.io.ObjectStreamClass)7 UnitTest (org.apache.geode.test.junit.categories.UnitTest)5 Test (org.junit.Test)5 InternalDataSerializer (org.apache.geode.internal.InternalDataSerializer)4 BigInteger (java.math.BigInteger)3 ArrayList (java.util.ArrayList)3 Entry (java.util.Map.Entry)2 Instantiator (org.apache.geode.Instantiator)2 StringId (org.apache.geode.i18n.StringId)2 EventID (org.apache.geode.internal.cache.EventID)2 LogMarker (org.apache.geode.internal.logging.log4j.LogMarker)2 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CancelException (org.apache.geode.CancelException)1 CanonicalInstantiator (org.apache.geode.CanonicalInstantiator)1