Package com.inspiresoftware.lib.dto.geda.adapter

Examples of com.inspiresoftware.lib.dto.geda.adapter.ExtensibleBeanFactory


    @Test
    public void testByInterface() throws Exception {

        final IMocksControl ctrl = EasyMock.createControl();

        final ExtensibleBeanFactory bf = ctrl.createMock("bf", ExtensibleBeanFactory.class);

        final com.inspiresoftware.lib.dto.geda.dsl.Registry registry = new DefaultDSLRegistry(bf);

        expect(bf.getClazz("myDto")).andReturn(MyDtoClass.class);
        expect(bf.getClazz("myDtoField3Dto")).andReturn(MyDtoField3Class.class);
        expect(bf.getClazz("myEntityField3Entity")).andReturn(MyEntityField3Class.class);
        expect(bf.getClazz("field4ParentDto")).andReturn(MyDtoField4Class.class);
        expect(bf.getClazz("field4ParentEntity")).andReturn(MyEntityField4Class.class);
        expect(bf.get("myDtoField3Dto")).andReturn(new MyDtoField3Class());
        expect(bf.get("field4ParentDto")).andReturn(new MyDtoField4Class());

        ctrl.replay();

        registry
                // main mapping
                .dto("myDto").forEntity(MyEntity.class)
                // field 1
                .withField("field1").forField("field1")
                .readOnly()
                .converter("field1Converter")
                        // field 2
                .and()
                .withField("field2").forField("field2.subField1").entityBeanKeys("field2")
                // field 3
                .and()
                .withField("field3")
                .dtoBeanKey("myDtoField3Dto")
                .entityBeanKeys("myEntityField3Entity")
                        // field 4
                .and()
                .withField("field4parent")
                .dtoBeanKey("field4ParentDto")
                .entityBeanKeys("field4ParentEntity")
                .dtoParent("id")
                .retriever("parentFieldEntityById")
                        // field 5
                .and()
                .withField("field5virtual").forVirtual()
                .converter("field5VirtualConverter")
                        // field 6
                .and()
                .withCollection("field6").forField("field6")
                .dtoBeanKey("field6CollectionDtoItem")
                .entityBeanKeys("field6CollectionEntityItem")
                .dtoToEntityMatcherKey("field6CollectionMatcher")
                        // field 7
                .and()
                .withMap("field7").forField("field7")
                .dtoBeanKey("field7MapDtoItem")
                .entityBeanKeys("field7MapEntityItem")
                .dtoToEntityMatcherKey("field7MapMatcher")
        ;

        registry
                .dto("myDtoField3Dto").forEntity("myEntityField3Entity")
                .withField("subField1")
        ;

        registry
                .dto("field4ParentDto").forEntity("field4ParentEntity")
                .withField("id")
                .and().withField("subField1")
        ;

        final Map<String, Object> conv = new HashMap<String, Object>();
        conv.put("field1Converter", new ValueConverter() {
            public Object convertToDto(final Object object, final BeanFactory beanFactory) {
                final MyEntity.Field1 field1 = (MyEntity.Field1) object;
                return Boolean.valueOf(field1 == MyEntity.Field1.YES);
            }

            public Object convertToEntity(final Object object, final Object oldEntity, final BeanFactory beanFactory) {
                if ((Boolean) object) {
                    return MyEntity.Field1.YES;
                }
                return MyEntity.Field1.NO;
            }
        });
        conv.put("field5VirtualConverter", new ValueConverter() {
            public Object convertToDto(final Object object, final BeanFactory beanFactory) {
                return String.valueOf(object.hashCode());
            }

            public Object convertToEntity(final Object object, final Object oldEntity, final BeanFactory beanFactory) {
                return null;
            }
        });
        conv.put("parentFieldEntityById", new EntityRetriever() {
            public Object retrieveByPrimaryKey(final Class entityInterface, final Class entityClass, final Object primaryKey) {
                final MyEntityField4Class parent = new MyEntityField4Class();
                parent.setId(99L);
                parent.setSubField1("Parent 99");
                return parent;
            }
        });

        // create asm for interface
        DTOAssembler.newAssembler(MyDtoClass.class, MyEntity.class, registry);

        final MyEntity entity = new MyEntityClass();

        entity.setField1(MyEntity.Field1.YES);

        entity.setField2(new MyEntityField2Class());
        entity.getField2().setSubField1("my sub data 1");

        entity.setField3(new MyEntityField3Class());
        entity.getField3().setSubField1("my sub data 2");

        entity.setField4parent(new MyEntityField4Class());
        entity.getField4parent().setId(99L);
        entity.getField4parent().setSubField1("Parent 99");

        final MyDtoClass dto = new MyDtoClass();

        // create asm for class and make sure it picks up on interface
        DTOAssembler.newAssembler(MyDtoClass.class, entity.getClass(), registry).assembleDto(dto, entity, conv, bf);

        assertTrue(dto.getField1());

        assertEquals(dto.getField2(), "my sub data 1");

        final MyDtoField3Class field3 = dto.getField3();
        assertNotNull(field3);
        assertEquals(field3.getSubField1(), "my sub data 2");

        final MyDtoField4Class field4 = dto.getField4parent();
        assertNotNull(field4);
        assertEquals(field4.getId(), Long.valueOf(99L));
        assertEquals(field4.getSubField1(), "Parent 99");

        assertEquals(dto.getField5virtual(), String.valueOf(entity.hashCode()));

        ctrl.verify();


        ctrl.reset();

        expect(bf.get("field2")).andReturn(new MyEntityField2Class());
        expect(bf.get("myEntityField3Entity")).andReturn(new MyEntityField3Class());
        expect(bf.get("field4ParentEntity")).andReturn(new MyEntityField4Class());

        ctrl.replay();

        final MyEntity toEntity = new MyEntityClass();
View Full Code Here


     * the beanKey approach, for others just use the appropriate class instead
     * of beanKey.
     */
    public void withSameFieldsByKey() {

        final ExtensibleBeanFactory bf = new SimpleMapExtensibleBeanFactory();

        final com.inspiresoftware.lib.dto.geda.dsl.Registry registry = Registries.registry(bf);

        bf.registerEntity("myEntityWithSameFields",
                MyEntityWithSameFieldsClass.class.getCanonicalName(),
                MyEntityWithSameFieldsInterface.class.getCanonicalName());

        registry
                // main mapping
View Full Code Here

    @Test
    public void testByClassWithAlias() throws Exception {

        final IMocksControl ctrl = EasyMock.createControl();

        final ExtensibleBeanFactory bf = ctrl.createMock("bf", ExtensibleBeanFactory.class);

        final com.inspiresoftware.lib.dto.geda.dsl.Registry registry = new DefaultDSLRegistry(bf);

        expect(bf.getClazz("myDto")).andReturn(MyDtoClass.class);
        expect(bf.getClazz("myDtoField3Dto")).andReturn(MyDtoField3Class.class);
        expect(bf.getClazz("myEntityField3Entity")).andReturn(MyEntityField3Class.class);
        expect(bf.getClazz("field4ParentDto")).andReturn(MyDtoField4Class.class);
        expect(bf.getClazz("field4ParentEntity")).andReturn(MyEntityField4Class.class);
        expect(bf.get("myDtoField3Dto")).andReturn(new MyDtoField3Class());
        expect(bf.get("field4ParentDto")).andReturn(new MyDtoField4Class());
        bf.registerEntity("myEntity", "com.inspiresoftware.lib.dto.geda.assembler.dsl.impl.MyEntityClass", "com.inspiresoftware.lib.dto.geda.assembler.dsl.impl.MyEntity");

        ctrl.replay();

        registry
                // main mapping
                .dto("myDto").forEntity(MyEntityClass.class).alias("myEntity", MyEntity.class)
                // field 1
                .withField("field1").forField("field1")
                .readOnly()
                .converter("field1Converter")
                        // field 2
                .and()
                .withField("field2").forField("field2.subField1").entityBeanKeys("field2")
                // field 3
                .and()
                .withField("field3")
                .dtoBeanKey("myDtoField3Dto")
                .entityBeanKeys("myEntityField3Entity")
                        // field 4
                .and()
                .withField("field4parent")
                .dtoBeanKey("field4ParentDto")
                .entityBeanKeys("field4ParentEntity")
                .dtoParent("id")
                .retriever("parentFieldEntityById")
                        // field 5
                .and()
                .withField("field5virtual").forVirtual()
                .converter("field5VirtualConverter")
                        // field 6
                .and()
                .withCollection("field6").forField("field6")
                .dtoBeanKey("field6CollectionDtoItem")
                .entityBeanKeys("field6CollectionEntityItem")
                .dtoToEntityMatcherKey("field6CollectionMatcher")
                        // field 7
                .and()
                .withMap("field7").forField("field7")
                .dtoBeanKey("field7MapDtoItem")
                .entityBeanKeys("field7MapEntityItem")
                .dtoToEntityMatcherKey("field7MapMatcher")
        ;

        registry
                .dto("myDtoField3Dto").forEntity("myEntityField3Entity")
                .withField("subField1")
        ;

        registry
                .dto("field4ParentDto").forEntity("field4ParentEntity")
                .withField("id")
                .and().withField("subField1")
        ;

        final Map<String, Object> conv = new HashMap<String, Object>();
        conv.put("field1Converter", new ValueConverter() {
            public Object convertToDto(final Object object, final BeanFactory beanFactory) {
                final MyEntity.Field1 field1 = (MyEntity.Field1) object;
                return Boolean.valueOf(field1 == MyEntity.Field1.YES);
            }

            public Object convertToEntity(final Object object, final Object oldEntity, final BeanFactory beanFactory) {
                if ((Boolean) object) {
                    return MyEntity.Field1.YES;
                }
                return MyEntity.Field1.NO;
            }
        });
        conv.put("field5VirtualConverter", new ValueConverter() {
            public Object convertToDto(final Object object, final BeanFactory beanFactory) {
                return String.valueOf(object.hashCode());
            }

            public Object convertToEntity(final Object object, final Object oldEntity, final BeanFactory beanFactory) {
                return null;
            }
        });
        conv.put("parentFieldEntityById", new EntityRetriever() {
            public Object retrieveByPrimaryKey(final Class entityInterface, final Class entityClass, final Object primaryKey) {
                final MyEntityField4Class parent = new MyEntityField4Class();
                parent.setId(99L);
                parent.setSubField1("Parent 99");
                return parent;
            }
        });

        // create asm for interface
        DTOAssembler.newAssembler(MyDtoClass.class, MyEntity.class, registry);

        final MyEntity entity = new MyEntityClass();

        entity.setField1(MyEntity.Field1.YES);

        entity.setField2(new MyEntityField2Class());
        entity.getField2().setSubField1("my sub data 1");

        entity.setField3(new MyEntityField3Class());
        entity.getField3().setSubField1("my sub data 2");

        entity.setField4parent(new MyEntityField4Class());
        entity.getField4parent().setId(99L);
        entity.getField4parent().setSubField1("Parent 99");

        final MyDtoClass dto = new MyDtoClass();

        // create asm for class and make sure it picks up on interface
        DTOAssembler.newAssembler(MyDtoClass.class, entity.getClass(), registry).assembleDto(dto, entity, conv, bf);

        assertTrue(dto.getField1());

        assertEquals(dto.getField2(), "my sub data 1");

        final MyDtoField3Class field3 = dto.getField3();
        assertNotNull(field3);
        assertEquals(field3.getSubField1(), "my sub data 2");

        final MyDtoField4Class field4 = dto.getField4parent();
        assertNotNull(field4);
        assertEquals(field4.getId(), Long.valueOf(99L));
        assertEquals(field4.getSubField1(), "Parent 99");

        assertEquals(dto.getField5virtual(), String.valueOf(entity.hashCode()));

        ctrl.verify();


        ctrl.reset();

        expect(bf.get("field2")).andReturn(new MyEntityField2Class());
        expect(bf.get("myEntityField3Entity")).andReturn(new MyEntityField3Class());
        expect(bf.get("field4ParentEntity")).andReturn(new MyEntityField4Class());

        ctrl.replay();

        final MyEntity toEntity = new MyEntityClass();
View Full Code Here

     * Example showing how to use exclusions with auto mapping of same name same type fields.
     * The basic principle is the same - all you need is to specify the exclusion field names.
     */
    public void withSameFieldsByKeyExcluding() {

        final ExtensibleBeanFactory bf = new SimpleMapExtensibleBeanFactory();

        final com.inspiresoftware.lib.dto.geda.dsl.Registry registry = Registries.registry(bf);

        bf.registerEntity("myEntityWithSameFields",
                MyEntityWithSameFieldsClass.class.getCanonicalName(),
                MyEntityWithSameFieldsInterface.class.getCanonicalName());

        registry
                // main mapping
View Full Code Here

    @Test
    public void testGeneric() throws Exception {

        final IMocksControl ctrl = EasyMock.createControl();

        final ExtensibleBeanFactory bf = ctrl.createMock("bf", ExtensibleBeanFactory.class);

        final com.inspiresoftware.lib.dto.geda.dsl.Registry registry = new DefaultDSLRegistry(bf);

        expect(bf.getClazz("myDto")).andReturn(MyDtoClass.class);
        expect(bf.getClazz("myDtoField3Dto")).andReturn(MyDtoField3Class.class);
        expect(bf.getClazz("field4ParentDto")).andReturn(MyDtoField4Class.class);
        expect(bf.get("myDtoField3Dto")).andReturn(new MyDtoField3Class());
        expect(bf.get("field4ParentDto")).andReturn(new MyDtoField4Class());

        ctrl.replay();

        registry
                // main mapping
                .dto("myDto").forEntityGeneric()
                // field 1
                .withField("field1").forField("field1")
                .readOnly()
                .converter("field1Converter")
                        // field 2
                .and()
                .withField("field2").forField("field2.subField1").entityBeanKeys("field2")
                // field 3
                .and()
                .withField("field3")
                .dtoBeanKey("myDtoField3Dto")
                .entityBeanKeys("myEntityField3Entity")
                        // field 4
                .and()
                .withField("field4parent")
                .dtoBeanKey("field4ParentDto")
                .entityBeanKeys("field4ParentEntity")
                .dtoParent("id")
                .retriever("parentFieldEntityById")
                        // field 5
                .and()
                .withField("field5virtual").forVirtual()
                .converter("field5VirtualConverter")
                        // field 6
                .and()
                .withCollection("field6").forField("field6")
                .dtoBeanKey("field6CollectionDtoItem")
                .entityBeanKeys("field6CollectionEntityItem")
                .dtoToEntityMatcherKey("field6CollectionMatcher")
                        // field 7
                .and()
                .withMap("field7").forField("field7")
                .dtoBeanKey("field7MapDtoItem")
                .entityBeanKeys("field7MapEntityItem")
                .dtoToEntityMatcherKey("field7MapMatcher")
        ;

        registry
                .dto("myDtoField3Dto").forEntityGeneric()
                .withField("subField1")
        ;

        registry
                .dto("field4ParentDto").forEntityGeneric()
                .withField("id")
                .and().withField("subField1")
        ;

        final Map<String, Object> conv = new HashMap<String, Object>();
        conv.put("field1Converter", new ValueConverter() {
            public Object convertToDto(final Object object, final BeanFactory beanFactory) {
                final MyEntity.Field1 field1 = (MyEntity.Field1) object;
                return Boolean.valueOf(field1 == MyEntity.Field1.YES);
            }

            public Object convertToEntity(final Object object, final Object oldEntity, final BeanFactory beanFactory) {
                if ((Boolean) object) {
                    return MyEntity.Field1.YES;
                }
                return MyEntity.Field1.NO;
            }
        });
        conv.put("field5VirtualConverter", new ValueConverter() {
            public Object convertToDto(final Object object, final BeanFactory beanFactory) {
                return String.valueOf(object.hashCode());
            }

            public Object convertToEntity(final Object object, final Object oldEntity, final BeanFactory beanFactory) {
                return null;
            }
        });
        conv.put("parentFieldEntityById", new EntityRetriever() {
            public Object retrieveByPrimaryKey(final Class entityInterface, final Class entityClass, final Object primaryKey) {
                final MyEntityField4Class parent = new MyEntityField4Class();
                parent.setId(99L);
                parent.setSubField1("Parent 99");
                return parent;
            }
        });

        // create asm for interface
        DTOAssembler.newAssembler(MyDtoClass.class, MyEntity.class, registry);

        final MyEntity entity = new MyEntityClass();

        entity.setField1(MyEntity.Field1.YES);

        entity.setField2(new MyEntityField2Class());
        entity.getField2().setSubField1("my sub data 1");

        entity.setField3(new MyEntityField3Class());
        entity.getField3().setSubField1("my sub data 2");

        entity.setField4parent(new MyEntityField4Class());
        entity.getField4parent().setId(99L);
        entity.getField4parent().setSubField1("Parent 99");

        final MyDtoClass dto = new MyDtoClass();

        // create asm for class and make sure it picks up on interface
        DTOAssembler.newAssembler(MyDtoClass.class, entity.getClass(), registry).assembleDto(dto, entity, conv, bf);

        assertTrue(dto.getField1());

        assertEquals(dto.getField2(), "my sub data 1");

        final MyDtoField3Class field3 = dto.getField3();
        assertNotNull(field3);
        assertEquals(field3.getSubField1(), "my sub data 2");

        final MyDtoField4Class field4 = dto.getField4parent();
        assertNotNull(field4);
        assertEquals(field4.getId(), Long.valueOf(99L));
        assertEquals(field4.getSubField1(), "Parent 99");

        assertEquals(dto.getField5virtual(), String.valueOf(entity.hashCode()));

        ctrl.verify();

        ctrl.reset();

        expect(bf.get("field2")).andReturn(new MyEntityField2Class());
        expect(bf.get("myEntityField3Entity")).andReturn(new MyEntityField3Class());
        expect(bf.get("field4ParentEntity")).andReturn(new MyEntityField4Class());

        ctrl.replay();

        final MyEntity toEntity = new MyEntityClass();
View Full Code Here

    @Test
    public void testReuseMapping() throws Exception {

        final IMocksControl ctrl = EasyMock.createControl();

        final ExtensibleBeanFactory bf = ctrl.createMock("bf", ExtensibleBeanFactory.class);

        final com.inspiresoftware.lib.dto.geda.dsl.Registry registry = new DefaultDSLRegistry(bf);

        expect(bf.getClazz("myDto")).andReturn(MyDtoClass.class).anyTimes();

        ctrl.replay();

        registry
                // main mapping
View Full Code Here

    @Test
    public void testWithSameFieldsByKey() throws Exception {

        final IMocksControl ctrl = EasyMock.createControl();

        final ExtensibleBeanFactory bf = ctrl.createMock("bf", ExtensibleBeanFactory.class);

        final com.inspiresoftware.lib.dto.geda.dsl.Registry registry = new DefaultDSLRegistry(bf);

        expect(bf.getClazz("myEntityWithSameFields")).andReturn(MyEntityWithSameFieldsInterface.class).anyTimes();

        ctrl.replay();

        registry
                // main mapping
View Full Code Here

    @Test
    public void testWithSameFieldsByKeyExcluding() throws Exception {

        final IMocksControl ctrl = EasyMock.createControl();

        final ExtensibleBeanFactory bf = ctrl.createMock("bf", ExtensibleBeanFactory.class);

        final com.inspiresoftware.lib.dto.geda.dsl.Registry registry = new DefaultDSLRegistry(bf);

        expect(bf.getClazz("myEntityWithSameFields")).andReturn(MyEntityWithSameFieldsInterface.class).anyTimes();

        ctrl.replay();

        registry
                // main mapping
View Full Code Here

    @Test
    public void testWithSameFieldsByInterface() throws Exception {

        final IMocksControl ctrl = EasyMock.createControl();

        final ExtensibleBeanFactory bf = ctrl.createMock("bf", ExtensibleBeanFactory.class);

        final com.inspiresoftware.lib.dto.geda.dsl.Registry registry = new DefaultDSLRegistry(bf);

        ctrl.replay();
View Full Code Here

    @Test
    public void testWithSameFieldsByInterfaceExcluding() throws Exception {

        final IMocksControl ctrl = EasyMock.createControl();

        final ExtensibleBeanFactory bf = ctrl.createMock("bf", ExtensibleBeanFactory.class);

        final com.inspiresoftware.lib.dto.geda.dsl.Registry registry = new DefaultDSLRegistry(bf);

        ctrl.replay();
View Full Code Here

TOP

Related Classes of com.inspiresoftware.lib.dto.geda.adapter.ExtensibleBeanFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.