/** * 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); } }
InternalDataSerializer.unregister(id); InternalDataSerializer.unregister(id);
/** * Tests that a custom serializer is consulted */ @Test public void testCustomSerializer() throws Exception { Random random = new Random(); Class c = NonDataSerializable.NonDSSerializer.class; byte id = (byte) 100; DataSerializer.register(c); Object o = new NonDataSerializable(random); try { DataSerializer.writeObject(o, getDataOutput()); Object o2 = DataSerializer.readObject(getDataInput()); assertEquals(o, o2); } finally { InternalDataSerializer.unregister(id); } }
/** * Tests that only one serializer is invoked when a serializer specifies its supported classes. * Alos tests UDDS1. */ @Test public void testSupportedClasses() throws Exception { DataSerializer ds1 = DataSerializer.register(Class_testSupportedClasses1.class); int id = ds1.getId(); DataSerializer ds2 = DataSerializer.register(Class_testSupportedClasses2.class); int id2 = ds2.getId(); try { Object o = new NonDataSerializable(new Random()); DataSerializer.writeObject(o, getDataOutput()); assertTrue(Class_testSupportedClasses2.wasInvoked); assertTrue(Class_testSupportedClasses2.toDataInvoked); assertFalse(Class_testSupportedClasses2.fromDataInvoked); Object o2 = DataSerializer.readObject(getDataInput()); assertTrue(Class_testSupportedClasses2.fromDataInvoked); assertEquals(o, o2); } finally { InternalDataSerializer.unregister(id); InternalDataSerializer.unregister(id2); } }
/** * Tests that an <code>IOException</code> is thrown when the serializer for an object cannot be * found. */ @Test public void testNoDeSerializer() throws Exception { Random random = new Random(); byte id = (byte) 100; Class c = NonDataSerializable.NonDSSerializer.class; DataSerializer.register(c); Object o = new NonDataSerializable(random); DataSerializer.writeObject(o, getDataOutput()); InternalDataSerializer.unregister(id); int savVal = InternalDataSerializer.GetMarker.WAIT_MS; InternalDataSerializer.GetMarker.WAIT_MS = 10; try { DataSerializer.readObject(getDataInput()); fail("Should have thrown an IOException"); } catch (IOException ex) { // pass... } finally { InternalDataSerializer.GetMarker.WAIT_MS = savVal; } }
/** * 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); } }
/** * Make sure a user defined ds with an id of42 bytes works. */ @Test public void testUDDS4() throws Exception { DataSerializer ds2 = DataSerializer.register(Class_testSupportedClasses4.class); int id2 = ds2.getId(); try { Object o = new NonDataSerializable(new Random()); DataSerializer.writeObject(o, getDataOutput()); assertTrue(Class_testSupportedClasses4.wasInvoked); assertTrue(Class_testSupportedClasses4.toDataInvoked); assertFalse(Class_testSupportedClasses4.fromDataInvoked); Object o2 = DataSerializer.readObject(getDataInput()); assertTrue(Class_testSupportedClasses4.fromDataInvoked); assertEquals(o, o2); } finally { InternalDataSerializer.unregister(id2); } }
@Test public void testInstantiator2() throws Exception { final boolean[] wasInvoked = new boolean[] {false}; Instantiator.register(new Instantiator(DataSerializableImpl.class, 20000) { @Override 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); } }
@Test public void testInstantiator4() throws Exception { final boolean[] wasInvoked = new boolean[] {false}; Instantiator.register(new Instantiator(DataSerializableImpl.class, 123456789) { @Override 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); } }
/** * 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) { @Override 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); } }
assertTrue(o == o2); } finally { InternalDataSerializer.unregister(id);
InternalDataSerializer.unregister(id); InternalDataSerializer.removeRegistrationListener(l1); InternalDataSerializer.removeRegistrationListener(l2);
private void potentiallyReRegisterThisSerializer() { if (InternalDataSerializer.getSerializer(getId()) != null) { InternalDataSerializer.unregister(getId()); DataSerializer.register(getClass()); } }
private void potentiallyReRegisterThisSerializer() { if (InternalDataSerializer.getSerializer(getId()) != null) { InternalDataSerializer.unregister(getId()); DataSerializer.register(getClass()); } }