Search in sources :

Example 21 with Instantiator

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

the class DataSerializableJUnitTest method testInstantiator2.

@Test
public void testInstantiator2() throws Exception {
    final boolean[] wasInvoked = new boolean[] { false };
    Instantiator.register(new Instantiator(DataSerializableImpl.class, 20000) {

        public DataSerializable newInstance() {
            wasInvoked[0] = true;
            return new DataSerializableImpl();
        }
    });
    try {
        byte id = (byte) 57;
        Class_testInstantiator.supClass = DataSerializableImpl.class;
        DataSerializer.register(Class_testInstantiator.class);
        try {
            Object o = new DataSerializableImpl(new Random());
            DataSerializer.writeObject(o, getDataOutput());
            Object o2 = DataSerializer.readObject(getDataInput());
            assertTrue(wasInvoked[0]);
            assertEquals(o, o2);
        } finally {
            InternalDataSerializer.unregister(id);
        }
    } finally {
        InternalInstantiator.unregister(DataSerializableImpl.class, 20000);
    }
}
Also used : CanonicalInstantiator(org.apache.geode.CanonicalInstantiator) Instantiator(org.apache.geode.Instantiator) DataSerializable(org.apache.geode.DataSerializable) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 22 with Instantiator

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

the class DataSerializableJUnitTest method testInstantiator4.

@Test
public void testInstantiator4() throws Exception {
    final boolean[] wasInvoked = new boolean[] { false };
    Instantiator.register(new Instantiator(DataSerializableImpl.class, 123456789) {

        public DataSerializable newInstance() {
            wasInvoked[0] = true;
            return new DataSerializableImpl();
        }
    });
    try {
        byte id = (byte) 57;
        Class_testInstantiator.supClass = DataSerializableImpl.class;
        DataSerializer.register(Class_testInstantiator.class);
        try {
            Object o = new DataSerializableImpl(new Random());
            DataSerializer.writeObject(o, getDataOutput());
            Object o2 = DataSerializer.readObject(getDataInput());
            assertTrue(wasInvoked[0]);
            assertEquals(o, o2);
        } finally {
            InternalDataSerializer.unregister(id);
        }
    } finally {
        InternalInstantiator.unregister(DataSerializableImpl.class, 123456789);
    }
}
Also used : CanonicalInstantiator(org.apache.geode.CanonicalInstantiator) Instantiator(org.apache.geode.Instantiator) DataSerializable(org.apache.geode.DataSerializable) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 23 with Instantiator

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

the class DataTypeJUnitTest method getDataTypeShouldReturnUserDataSeriazliable4.

@Test
public void getDataTypeShouldReturnUserDataSeriazliable4() throws IOException {
    Instantiator mockInstantiator = PowerMockito.mock(Instantiator.class);
    doReturn(CustId.class).when(mockInstantiator).getInstantiatedClass();
    mockInstantiator.getInstantiatedClass();
    int someClassId = 1;
    PowerMockito.mockStatic(InternalInstantiator.class);
    when(InternalInstantiator.getClassId(mockInstantiator.getClass())).thenReturn(someClassId);
    when(InternalInstantiator.getInstantiator(someClassId)).thenReturn(mockInstantiator);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(baos);
    out.writeByte(DSCODE.USER_DATA_SERIALIZABLE_4);
    out.writeInt(someClassId);
    byte[] bytes = baos.toByteArray();
    String type = DataType.getDataType(bytes);
    assertThat(type).isEqualTo("org.apache.geode.Instantiator:org.apache.geode.internal.cache.execute.data.CustId");
}
Also used : DataOutputStream(java.io.DataOutputStream) InternalInstantiator(org.apache.geode.internal.InternalInstantiator) Instantiator(org.apache.geode.Instantiator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with Instantiator

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

the class DataSerializableJUnitTest method testInstantiator.

/**
   * Tests that an <code>Instantiator</code> is invoked at the appropriate times.
   */
@Test
public void testInstantiator() throws Exception {
    final boolean[] wasInvoked = new boolean[] { false };
    Instantiator.register(new Instantiator(DataSerializableImpl.class, (byte) 45) {

        public DataSerializable newInstance() {
            wasInvoked[0] = true;
            return new DataSerializableImpl();
        }
    });
    try {
        byte id = (byte) 57;
        Class_testInstantiator.supClass = DataSerializableImpl.class;
        DataSerializer.register(Class_testInstantiator.class);
        try {
            Object o = new DataSerializableImpl(new Random());
            DataSerializer.writeObject(o, getDataOutput());
            Object o2 = DataSerializer.readObject(getDataInput());
            assertTrue(wasInvoked[0]);
            assertEquals(o, o2);
        } finally {
            InternalDataSerializer.unregister(id);
        }
    } finally {
        InternalInstantiator.unregister(DataSerializableImpl.class, (byte) 45);
    }
}
Also used : CanonicalInstantiator(org.apache.geode.CanonicalInstantiator) Instantiator(org.apache.geode.Instantiator) DataSerializable(org.apache.geode.DataSerializable) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

Instantiator (org.apache.geode.Instantiator)24 UnitTest (org.apache.geode.test.junit.categories.UnitTest)9 Test (org.junit.Test)9 CanonicalInstantiator (org.apache.geode.CanonicalInstantiator)7 InternalInstantiator (org.apache.geode.internal.InternalInstantiator)7 DataSerializable (org.apache.geode.DataSerializable)5 InternalCache (org.apache.geode.internal.cache.InternalCache)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DataOutputStream (java.io.DataOutputStream)3 IOException (java.io.IOException)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 DataSerializer (org.apache.geode.DataSerializer)2 CacheClosedException (org.apache.geode.cache.CacheClosedException)2 DataInput (java.io.DataInput)1 EOFException (java.io.EOFException)1 NotSerializableException (java.io.NotSerializableException)1 UTFDataFormatException (java.io.UTFDataFormatException)1