示例#1
0
  @Test
  public void testSimpleCopy_ok() {
    NestedSrcMappingObject nestedSrcRef = new NestedSrcMappingObject();
    nestedSrcRef.setBigDecimalValue(BigDecimal.ONE);
    nestedSrcRef.setName("ljh");

    NestedTargetMappingObject nestedTargetRef =
        new NestedTargetMappingObject(); // 测试一下mapping到一个Object对象
    nestedSrcCopyer.copy(nestedSrcRef, nestedTargetRef);
    assertNull(nestedTargetRef.getValue()); // 属性不同,类型也不同
    assertEquals(nestedTargetRef.getName(), nestedSrcRef.getName());

    NestedSrcMappingObject newNestedSrcRef = new NestedSrcMappingObject(); // 反过来再mapping一次
    nestedTargetCopyer.copy(nestedTargetRef, newNestedSrcRef);
    assertNull(newNestedSrcRef.getBigDecimalValue()); // 属性不同,类型也不同
    assertEquals(newNestedSrcRef.getName(), nestedTargetRef.getName());
  }
  @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());
  }
  @Test
  public void testCollectionNestedMapping() { // 测试下collection的mapping
    BeanMappingBuilder builder =
        new BeanMappingBuilder() {

          protected void configure() {
            behavior()
                .debug(true)
                .mappingEmptyStrings(false)
                .mappingNullValue(false)
                .trimStrings(true); // 设置行为
            mapping(HashMap.class, HashMap.class);
            fields(
                srcField(ONE),
                targetField(ONE_OTHER, ArrayList.class)
                    .componentClasses(NestedTargetMappingObject.class));
            fields(
                srcField(TWO),
                targetField(TWO_OTHER, HashSet.class)
                    .componentClasses(NestedTargetMappingObject.class));
            fields(
                srcField(THREE),
                targetField(THREE_OTHER, HashMap[].class).componentClasses(HashMap.class));
          }
        };
    // 第二层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(nextNestedMapping);

    BeanMapping mapping = new BeanMapping(builder);
    Map src = new HashMap();
    HashMap[] mapArray = new HashMap[1];
    Map arrayNested = new HashMap();
    arrayNested.put("name", "ljh");
    arrayNested.put("value", "10");
    mapArray[0] = (HashMap) arrayNested;
    src.put(ONE, mapArray);

    List<NestedSrcMappingObject> objectList = new ArrayList<NestedSrcMappingObject>();
    NestedSrcMappingObject listNested = new NestedSrcMappingObject();
    listNested.setBigDecimalValue(BigDecimal.TEN);
    listNested.setName("ljh");
    objectList.add(listNested);
    src.put(TWO, objectList);

    Set<NestedSrcMappingObject> objectSet = new HashSet<NestedSrcMappingObject>();
    NestedSrcMappingObject setNested = new NestedSrcMappingObject();
    setNested.setBigDecimalValue(BigDecimal.TEN);
    setNested.setName("ljh");
    objectSet.add(listNested);
    src.put(THREE, objectSet);

    Map dest = new HashMap();
    mapping.mapping(src, dest);
    List<NestedTargetMappingObject> list = (List) dest.get(ONE_OTHER);
    Set<NestedTargetMappingObject> set = (Set) dest.get(TWO_OTHER);
    HashMap[] array = (HashMap[]) dest.get(THREE_OTHER);
    assertEquals(list.get(0).getName(), "ljh");
    assertEquals(list.get(0).getValue(), "10");
    NestedTargetMappingObject obj = set.iterator().next();
    assertEquals(obj.getName(), "ljh");
    assertEquals(obj.getValue(), "10");
    assertEquals(array[0].get("name"), "ljh");
    assertEquals(array[0].get("bigDecimalValue"), BigDecimal.TEN);
  }