Exemplo n.º 1
0
  @Test
  public void testCopy_ok() {
    SrcMappingObject srcRef = new SrcMappingObject();
    srcRef.setIntegerValue(1);
    srcRef.setIntValue(1);
    srcRef.setName("ljh");
    srcRef.setStart(true);

    // NestedSrcMappingObject nestedSrcRef = new NestedSrcMappingObject();
    // nestedSrcRef.setBigDecimalValue(BigDecimal.ONE);
    // srcRef.setMapping(nestedSrcRef);

    TargetMappingObject targetRef = new TargetMappingObject(); // 测试一下mapping到一个Object对象
    srcCopyer.copy(srcRef, targetRef);
    assertEquals(targetRef.getIntegerValue(), srcRef.getIntegerValue());
    assertEquals(targetRef.getIntValue(), srcRef.getIntValue());
    assertNull(targetRef.getTargetName()); // 为null,因为属性不匹配
    assertEquals(targetRef.isStart(), srcRef.isStart());

    SrcMappingObject newSrcRef = new SrcMappingObject(); // 反过来再mapping一次
    targetCopyer.copy(targetRef, newSrcRef);
    assertEquals(newSrcRef.getIntegerValue(), targetRef.getIntegerValue());
    assertEquals(newSrcRef.getIntValue(), targetRef.getIntValue());
    assertNull(newSrcRef.getName()); // 为null,因为属性不匹配
    assertEquals(newSrcRef.isStart(), targetRef.isStart());
  }
  @Test
  public void testNestedMapping() { // 测试嵌套mapping处理
    BeanMappingBuilder builder =
        new BeanMappingBuilder() {

          protected void configure() {
            behavior().debug(true); // 设置行为
            mapping(HashMap.class, HashMap.class);
            fields(srcField(ONE, String.class), targetField(ONE_OTHER, BigInteger.class));
            fields(srcField(TWO, BigInteger.class), targetField(TWO_OTHER, Integer.class));
            fields(
                    srcField(THREE, SrcMappingObject.class),
                    targetField(THREE_OTHER, TargetMappingObject.class))
                .recursiveMapping(true);
          }
        };
    // 第二层mapping
    BeanMappingBuilder nestedMapping =
        new BeanMappingBuilder() {

          protected void configure() {
            behavior().debug(true); // 设置行为
            mapping(SrcMappingObject.class, TargetMappingObject.class);
            fields(srcField("intValue"), targetField("intValue"));
            fields(srcField("integerValue"), targetField("integerValue"));
            fields(srcField("start"), targetField("start"));
            fields(srcField("name"), targetField("targetName"));
            fields(srcField("mapping"), targetField("mapping")).recursiveMapping(true);
          }
        };
    // 第三层mapping
    BeanMappingBuilder nextNestedMapping =
        new BeanMappingBuilder() {

          protected void configure() {
            behavior().debug(true); // 设置行为
            mapping(NestedSrcMappingObject.class, NestedTargetMappingObject.class);
            fields(srcField("name"), targetField("name"));
            fields(srcField("bigDecimalValue"), targetField("value"));
          }
        };

    BeanMappingConfigHelper.getInstance().register(nestedMapping);
    BeanMappingConfigHelper.getInstance().register(nextNestedMapping);

    BeanMapping mapping = new BeanMapping(builder);
    Map src = new HashMap();
    src.put(ONE, "10");
    src.put(TWO, BigInteger.TEN);
    SrcMappingObject srcRef = new SrcMappingObject();
    srcRef.setIntegerValue(1);
    srcRef.setIntValue(1);
    srcRef.setName("ljh");
    srcRef.setStart(true);

    NestedSrcMappingObject nestedSrcRef = new NestedSrcMappingObject();
    nestedSrcRef.setBigDecimalValue(BigDecimal.ONE);
    nestedSrcRef.setName("ljh");
    srcRef.setMapping(nestedSrcRef);
    src.put(THREE, srcRef);

    Map dest = new HashMap();
    mapping.mapping(src, dest);
    assertEquals(dest.get(ONE_OTHER), BigInteger.valueOf(10));
    assertEquals(dest.get(TWO_OTHER), Integer.valueOf(10));

    TargetMappingObject targetRef = (TargetMappingObject) dest.get(THREE_OTHER);
    assertNotNull(targetRef.getMapping());
    assertEquals(targetRef.getIntValue(), srcRef.getIntValue());
    assertEquals(targetRef.getIntegerValue(), srcRef.getIntegerValue());
    assertEquals(targetRef.getTargetName(), srcRef.getName());
    assertEquals(targetRef.getMapping().getName(), srcRef.getMapping().getName());
    assertEquals(
        targetRef.getMapping().getValue(), srcRef.getMapping().getBigDecimalValue().toString());
  }