Package org.dozer

Examples of org.dozer.Mapper


  }

  @Test
  public void testInjectMapperUsingSpring() throws Exception {
    // Try to get mapper from spring. Mapping files are injected via Spring config.
    Mapper mapper = (Mapper) ApplicationBeanFactory.getBean(Mapper.class);
    DozerBeanMapper mapperImpl = (DozerBeanMapper) mapper;

    Mapper cleanMapper = (Mapper) ApplicationBeanFactory.getBean("cleanMapper");

    assertNotNull("mapper should not be null", mapper);
    assertNotNull("mapping file names should not be null", mapperImpl.getMappingFiles());
    assertTrue("mapping file names should not be empty", mapperImpl.getMappingFiles().size() > 0);

    // Do some mapping so that the mapping files are actually loaded
    assertCommon(mapper);

    // make sure that the customconverter was injected
    Car car = new Car();
    Van van = cleanMapper.map(car, Van.class);
    assertEquals("injectedName", van.getName());
    // map back
    Car carDest = cleanMapper.map(van, Car.class);
    assertEquals("injectedName", carDest.getName());

    // test that we get customconverter even though it wasn't defined in the mapping file
    Moped moped = new Moped();
    Bus bus = cleanMapper.map(moped, Bus.class);
    assertEquals("injectedName", bus.getName());

    // map back
    Moped mopedDest = cleanMapper.map(bus, Moped.class);
    assertEquals("injectedName", mopedDest.getName());
  }
View Full Code Here


  @Test
  public void testSpringNoMappingFilesSpecified() throws Exception {
    // Mapper can be used without specifying any mapping files. Fields that have the same name will be mapped
    // automatically.
    Mapper mapper = (Mapper) ApplicationBeanFactory.getBean("NoExplicitMappingsMapperIF");

    assertCommon(mapper);
  }
View Full Code Here

    assertCommon(mapper);
  }

  @Test(expected=IllegalArgumentException.class)
  public void testDetectDuplicateMapping() throws Exception {
    Mapper myMapper = null;
    List<String> mappingFiles = new ArrayList<String>();
    mappingFiles.add("duplicateMapping.xml");
    myMapper = new DozerBeanMapper(mappingFiles);

    myMapper.map(new org.dozer.vo.SuperSuperSuperClass(), org.dozer.vo.SuperSuperSuperClassPrime.class);
    fail("should have thrown exception");
  }
View Full Code Here

    // -----------------------------------------------------------
    // Test that java beans get created with explicitly specified
    // custom bean factory
    // -----------------------------------------------------------

    Mapper mapper = getNewMapper(new String[]{"customfactorymapping.xml"});

    TestObjectPrime prime = mapper.map(testDataFactory.getInputGeneralMappingTestObject(), TestObjectPrime.class);
    TestObject source = mapper.map(prime, TestObject.class);

    // The following asserts will verify that the ClassMap beanFactory attr gets applied to both classes
    assertNotNull("created by factory name should not be null", prime.getCreatedByFactoryName());
    assertNotNull("created by factory name should not be null", source.getCreatedByFactoryName());
    assertEquals(SampleCustomBeanFactory.class.getName(), prime.getCreatedByFactoryName());
    assertEquals(SampleCustomBeanFactory.class.getName(), source.getCreatedByFactoryName());

    // The following asserts will verify that default configuration is being applied
    assertNotNull("created by factory name should not be null", source.getThree().getCreatedByFactoryName());
    assertEquals(SampleDefaultBeanFactory.class.getName(), source.getThree().getCreatedByFactoryName());

    // The following asserts will verify that dest or src class level attr's override classMap and default config attr's
    assertNotNull("created by factory name should not be null", prime.getThreePrime().getCreatedByFactoryName());
    assertEquals(SampleCustomBeanFactory2.class.getName(), prime.getThreePrime().getCreatedByFactoryName());

    // test returning an Interface
    Van van = new Van();
    van.setName("testName");
    MetalThingyIF car = mapper.map(van, MetalThingyIF.class);
    assertEquals("testName", car.getName());
  }
View Full Code Here

    if (mappingFiles != null) {
      for (int i = 0; i < mappingFiles.length; i++) {
        list.add(mappingFiles[i]);
      }
    }
    Mapper mapper = new DozerBeanMapper();
    ((DozerBeanMapper) mapper).setMappingFiles(list);
    return mapper;
  }
View Full Code Here

  }

  @Test
  public void testNoClassMappings() throws Exception {
    Mapper mapper = new DozerBeanMapper();
    // Should attempt mapping even though it is not in the beanmapping.xml file
    NoCustomMappingsObjectPrime dest1 = mapper.map(testDataFactory.getInputTestNoClassMappingsNoCustomMappingsObject(),
        NoCustomMappingsObjectPrime.class);
    NoCustomMappingsObject source = mapper.map(dest1, NoCustomMappingsObject.class);
    NoCustomMappingsObjectPrime dest3 = mapper.map(source, NoCustomMappingsObjectPrime.class);
    assertEquals(dest1, dest3);
  }
View Full Code Here

    assertNotNull("field1 should have been mapped", dest.getField1());
  }

  @Test
  public void testBidirectionalWithCustomMapping() throws Exception {
    Mapper mapper = getMapper(new String[] { "infiniteLoopMapping.xml" });
    LoopObjectParent loopObjectParent = newInstance(LoopObjectParent.class);
    LoopObjectChild loopObjectChild = newInstance(LoopObjectChild.class);
    loopObjectChild.setParent(loopObjectParent);
    loopObjectParent.setChild(loopObjectChild);

    LoopObjectParentPrime loopObjectParentPrime = mapper.map(loopObjectParent, LoopObjectParentPrime.class);
    assertNotNull(loopObjectParentPrime);
    assertNotNull(loopObjectParentPrime.getChildPrime());
  }
View Full Code Here

      org.dozer.vo.C c = newInstance(org.dozer.vo.C.class);
      c.setValue(Integer.toString(i));
      a.getCs()[i] = c;
    }

    Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
    mapper.map(a, b);

    // Check if C object nr i holds value i after the mapping
    for (int i = 0; i < b.getCs().length; i++) {
      assertEquals("Wrong value ", b.getCs()[i].getValue(), Integer.toString(i));
    }
View Full Code Here

  protected Mapper getMapper(String ... mappingFiles) {
    List<String> list = new ArrayList<String>();
    if (mappingFiles != null) {
      list.addAll(Arrays.asList(mappingFiles));
    }
    Mapper result = new DozerBeanMapper();
    ((DozerBeanMapper) result).setMappingFiles(list);
    return result;
  }
View Full Code Here

    }

    // get mapper
    List<String> mappingFiles = new ArrayList<String>();
    mappingFiles.add("interface-recursion-mappings.xml");
    Mapper mapper = new DozerBeanMapper(mappingFiles);

    // do mapping
    UserGroupPrime userGroupPrime = null;
    try {
      userGroupPrime = mapper.map(userGroup, UserGroupPrime.class);
    } catch (StackOverflowError e) {
      fail("Recursive mapping caused a stack overflow.");
    }

    // check mapped group
View Full Code Here

TOP

Related Classes of org.dozer.Mapper

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.