ObjectCodecFactory implements CodecFactory { public <T> Codec<T> create(AnnotatedElement metadata, Class<T> type, ResolverContext context) { if (metadata == null) { return createCodec(type, context); } else if (metadata.isAnnotationPresent(Bound.class)) { return createCodec(type, context); } else if (metadata.isAnnotationPresent(BoundObject.class)) { return createCodec(type, context, metadata); } else { return null; } } ObjectCodecFactory();  ObjectCodecFactory(CodecFactory codecFactory);  ObjectCodecFactory(CodecFactory codecFactory,
                              BindingFactory bindingFactory); Codec<T> create(AnnotatedElement metadata, Class<T> type,
                               ResolverContext context);  }
@SuppressWarnings("unchecked") @Test public void testBoundObjectNoMembersTwoTypesNoPrefix() throws DecodingException { Codec codec1 = createMock(Codec.class); Codec codec2 = createMock(Codec.class); expect(metadata.isAnnotationPresent(Bound.class)).andReturn(false).anyTimes(); expect(metadata.isAnnotationPresent(BoundObject.class)).andReturn(true).anyTimes(); expect(metadata.getAnnotation(BoundObject.class)).andReturn(settings); expect(settings.selectFrom()).andReturn(choices).times(2); expect(choices.alternatives()).andReturn(new Choices.Choice[0]); expect(choices.defaultType()).andReturn((Class) Void.class); expect(settings.type()).andReturn((Class) Void.class); expect(settings.types()).andReturn(new Class[]{TestObject1.class, TestObject2.class}) .times(2); expect( delegate.create((AnnotatedElement) EasyMock.isNull(), EasyMock.isA(Class.class), (ResolverContext) EasyMock.isNull())).andReturn(codec1); expect(codec1.getTypes()).andReturn(new Class[]{TestObject1.class}); expect(codec2.getTypes()).andReturn(new Class[]{TestObject2.class}); expect( delegate.create((AnnotatedElement) EasyMock.isNull(), EasyMock.isA(Class.class), (ResolverContext) EasyMock.isNull())).andReturn(codec2); replay(metadata, delegate, buffer, resolver, settings, codec1, codec2, choices); ObjectCodecFactory factory = new ObjectCodecFactory(delegate); try { Codec<TestObject1> created = factory.create(metadata, TestObject1.class, null); fail("Expecting failure due to missing prefixes."); } catch (CodecConstructionException cce) { } verify(metadata, delegate, buffer, resolver, settings, choices); }
@Test public void testBoundObjectNoMembersTwoTypesWithPrefix() throws DecodingException { Codec codecTest3 = createMock(Codec.class); Codec codecTest4 = createMock(Codec.class); expect(metadata.isAnnotationPresent(Bound.class)).andReturn(false).anyTimes(); expect(metadata.isAnnotationPresent(BoundObject.class)).andReturn(true).anyTimes(); expect(settings.selectFrom()).andReturn(choices).times(2); expect(choices.alternatives()).andReturn(new Choices.Choice[0]); expect(choices.defaultType()).andReturn((Class) Void.class); expect(metadata.getAnnotation(BoundObject.class)).andReturn(settings); expect(settings.type()).andReturn((Class) Void.class); expect(settings.types()).andReturn(new Class[]{TestObject3.class, TestObject4.class}) .times(2); expect(delegate.create(null, TestObject3.class, null)).andReturn(codecTest3); expect(codecTest3.getTypes()).andReturn(new Class<?>[]{TestObject3.class}); expect(codecTest4.getTypes()).andReturn(new Class<?>[]{TestObject4.class}); expect(delegate.create(null, TestObject4.class, null)).andReturn(codecTest4); expect(buffer.readAsLong(8, ByteOrder.LittleEndian)).andReturn(0L); expect(codecTest3.decode(buffer, resolver, builder)).andReturn(new TestObject3()); replay(metadata, delegate, buffer, resolver, settings, codecTest3, codecTest4, builder, choices); ObjectCodecFactory factory = new ObjectCodecFactory(delegate); Codec<TestObject1> codec = factory.create(metadata, TestObject1.class, null); assertNotNull(codec); TestObject1 result = codec.decode(buffer, resolver, builder); assertNotNull(result); assertTrue(!(result instanceof TestObject4)); assertTrue(result instanceof TestObject3); verify(metadata, delegate, buffer, resolver, settings, codecTest3, codecTest4, builder, choices); }