コード例 #1
0
  @Test
  public void shouldExplicitlySpecifyConstructor() {
    // given
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

    mapperFactory
        .classMap(BasicPerson.class, BasicPersonDtoWithConstructor.class)
        .constructorB("birthDate")
        .fieldAToB("name", "fullName")
        //                .field("age", "currentAge")
        .byDefault()
        .register();

    BasicPerson bp = createBasicPerson("Jan", 20, Calendar.getInstance().getTime());

    // when
    MapperFacade mapperFacade = mapperFactory.getMapperFacade();
    BasicPersonDtoWithConstructor result =
        mapperFacade.map(bp, BasicPersonDtoWithConstructor.class);

    // then
    assertThat(result.getFullName()).isEqualTo(bp.getName());
    assertThat(result.getCurrentAge()).isEqualTo(0); // !!
    assertThat(result.getBirthDate()).isNotNull();
  }
コード例 #2
0
  @Test
  public void shouldCustomizeMapping() {
    // given
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

    mapperFactory
        .classMap(BasicPerson.class, BasicPersonDto.class)
        .field("age", "currentAge")
        .byDefault()
        .customize(
            new CustomMapper<BasicPerson, BasicPersonDto>() {
              @Override
              public void mapAtoB(
                  BasicPerson basicPerson, BasicPersonDto basicPersonDto, MappingContext context) {
                Joiner joiner = Joiner.on(" ");
                String fullName =
                    joiner
                        .appendTo(
                            new StringBuilder(basicPerson.getName()).append(" "),
                            basicPerson.getNameParts())
                        .toString();

                basicPersonDto.setFullName(fullName);
              }
            })
        .register();

    BasicPerson bp = createBasicPerson("Jan", 20, Calendar.getInstance().getTime());
    bp.setNameParts(Lists.asList("von", new String[] {"Kowalski"}));

    // when
    MapperFacade mapperFacade = mapperFactory.getMapperFacade();
    BasicPersonDto result = mapperFacade.map(bp, BasicPersonDto.class);

    // then
    assertThat(result.getFullName()).isEqualTo("Jan von Kowalski");
    assertThat(result.getCurrentAge()).isEqualTo(bp.getAge());
    assertThat(result.getBirthDate()).isNotNull();
  }
コード例 #3
0
  @Test
  public void shouldRegisterClassMap() {
    // given
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

    mapperFactory
        .classMap(BasicPerson.class, BasicPersonDto.class)
        .field("name", "fullName")
        .field("age", "currentAge")
        .register();

    BasicPerson bp = createBasicPerson("Jan", 20, Calendar.getInstance().getTime());

    // when
    MapperFacade mapperFacade = mapperFactory.getMapperFacade();
    BasicPersonDto result = mapperFacade.map(bp, BasicPersonDto.class);

    // then
    assertThat(result.getFullName()).isEqualTo(bp.getName());
    assertThat(result.getCurrentAge()).isEqualTo(bp.getAge());
    assertThat(result.getBirthDate()).isNull();
  }
コード例 #4
-9
  @Test
  public void shouldUseOnlyOneDirectionMapping() {
    // given
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

    mapperFactory
        .classMap(BasicPerson.class, BasicPersonDto.class)
        .fieldAToB("name", "fullName")
        .field("age", "currentAge")
        .field("birthDate", "birthDate")
        .register();

    BasicPerson bpSrc = createBasicPerson("Jan", 20, Calendar.getInstance().getTime());

    BasicPersonDto bpDtoSrc = new BasicPersonDto();
    bpDtoSrc.setFullName("Jan");
    bpDtoSrc.setCurrentAge(20);
    bpDtoSrc.setBirthDate(Calendar.getInstance().getTime());

    // when
    BoundMapperFacade<BasicPerson, BasicPersonDto> boundMapper =
        mapperFactory.getMapperFacade(BasicPerson.class, BasicPersonDto.class);

    BasicPersonDto bpDtoDest = boundMapper.map(bpSrc);
    BasicPerson bpDest = boundMapper.mapReverse(bpDtoSrc);

    // then
    assertThat(bpDtoDest.getCurrentAge()).isEqualTo(bpSrc.getAge());
    assertThat(bpDtoDest.getFullName()).isEqualTo(bpSrc.getName());
    assertThat(bpDtoDest.getBirthDate()).isEqualTo(bpSrc.getBirthDate());

    assertThat(bpDest.getAge()).isEqualTo(bpDtoSrc.getCurrentAge());
    assertThat(bpDest.getName()).isNull();
    assertThat(bpDest.getBirthDate()).isEqualTo(bpDtoSrc.getBirthDate());
  }