⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mappertest.java

📁 一个javabean的转换与copy非常的好用希望大家好好研究一下
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    int[] prims = { 1, 2, 3, 1, 2, 3 };
    houseClone.getOwner().setPrim(prims);
    // add two more rooms
    Room room1 = new Room();
    room1.setName("Living");
    Room room2 = new Room();
    room2.setName("kitchen");
    Van van = new Van();
    van.setName("van2");
    houseClone.getRooms().add(room1);
    houseClone.getRooms().add(room2);
    houseClone.getCustomSetGetMethod().add(van);
    assertEquals(houseClone, src);
  }

  public void testDeepMapping() throws Exception {
    SrcDeepObj src = TestDataFactory.getSrcDeepObj();
    DestDeepObj dest = (DestDeepObj) mapper.map(src, DestDeepObj.class);
    SrcDeepObj src2 = (SrcDeepObj) mapper.map(dest, SrcDeepObj.class);
    DestDeepObj dest2 = (DestDeepObj) mapper.map(src2, DestDeepObj.class);

    assertEquals(src, src2);
    assertEquals(dest, dest2);

  }

  public void testMethodMapping() throws Exception {
    MethodFieldTestObject sourceObj = new MethodFieldTestObject();
    sourceObj.setIntegerStr("1500");
    sourceObj.setPriceItem("3500");
    sourceObj.setFieldOne("fieldOne");
    MethodFieldTestObject2 result = (MethodFieldTestObject2) mapper.map(sourceObj, MethodFieldTestObject2.class);
    assertEquals("invalid result object size", 1, result.getIntegerList().size());
    assertEquals("invalid result object value", 3500, result.getTotalPrice());
    assertEquals("invalid result object value", "fieldOne", result.getFieldOne());
    // map back
    MethodFieldTestObject result2 = (MethodFieldTestObject) mapper.map(result, MethodFieldTestObject.class);
    // if no exceptions we thrown we are good. stopOnErrors = true. both values will be null
    // since this is a one-way mapping we shouldn't have a value
    assertNull(result2.getFieldOne());
  }

  public void testCustomConverterMapping() throws Exception {
    TestCustomConverterObject obj = new TestCustomConverterObject();
    CustomDoubleObjectIF doub = new CustomDoubleObject();
    doub.setTheDouble(15);
    CustomDoubleObjectIF doub2 = new CustomDoubleObject();
    doub2.setTheDouble(15);
    obj.setAttribute(doub);

    Collection list = new ArrayList();
    list.add(doub2);

    obj.setNames(list);

    TestCustomConverterObjectPrime dest = (TestCustomConverterObjectPrime) mapper.map(obj,
        TestCustomConverterObjectPrime.class);

    assertEquals("Custom Converter failed", dest.getDoubleAttribute().doubleValue() + "", "15.0");
    assertEquals("Custom Converter failed", ((Double) dest.getNames().iterator().next()).doubleValue() + "", "15.0");

    TestCustomConverterObjectPrime objp = new TestCustomConverterObjectPrime();

    objp.setDoubleAttribute(new Double(15));

    Collection list2 = new ArrayList();
    objp.setNames(list2);
    objp.getNames().add(new Double(10));

    TestCustomConverterObject destp = (TestCustomConverterObject) mapper.map(objp, TestCustomConverterObject.class);

    assertEquals("Custom Converter failed", destp.getAttribute().getTheDouble() + "", "15.0");
    assertEquals("Custom Converter failed", ((CustomDoubleObjectIF) destp.getNames().iterator().next()).getTheDouble()
        + "", "10.0");

    destp.getAttribute().setName("testName");

    // pass by reference
    mapper.map(objp, destp);

    assertEquals("Custom Converter failed", destp.getAttribute().getTheDouble() + "", "15.0");
    assertEquals("testName", destp.getAttribute().getName());

    // test primitive double
    TestCustomConverterObjectPrime prime = new TestCustomConverterObjectPrime();
    prime.setPrimitiveDoubleAttribute(25.00);
    TestCustomConverterObject obj2 = (TestCustomConverterObject) mapper.map(prime, TestCustomConverterObject.class);
    CustomDoubleObjectIF customDouble = obj2.getPrimitiveDoubleAttribute();
    assertNotNull(customDouble);
    assertTrue(prime.getPrimitiveDoubleAttribute() == obj2.getPrimitiveDoubleAttribute().getTheDouble());

    // test conversion in the other direction
    prime = (TestCustomConverterObjectPrime) mapper.map(obj2, TestCustomConverterObjectPrime.class);
    assertTrue(prime.getPrimitiveDoubleAttribute() == obj2.getPrimitiveDoubleAttribute().getTheDouble());

  }

  public void testCustomConverterWithPrimitive() throws Exception {

    // test primitive double
    TestCustomConverterObjectPrime prime = new TestCustomConverterObjectPrime();
    prime.setPrimitiveDoubleAttribute(25.00);
    prime.setDoubleAttribute(new Double(30.00));
    TestCustomConverterObject obj2 = (TestCustomConverterObject) mapper.map(prime, TestCustomConverterObject.class);
    CustomDoubleObjectIF customDouble = obj2.getPrimitiveDoubleAttribute();
    assertNotNull(customDouble);
    assertTrue(prime.getPrimitiveDoubleAttribute() == obj2.getPrimitiveDoubleAttribute().getTheDouble());

    // test conversion in the other direction
    prime = (TestCustomConverterObjectPrime) mapper.map(obj2, TestCustomConverterObjectPrime.class);
    assertTrue(prime.getPrimitiveDoubleAttribute() == obj2.getPrimitiveDoubleAttribute().getTheDouble());

  }

  public void testCustomConverterHashMapMapping() throws Exception {
    TestCustomConverterHashMapObject testCustomConverterHashMapObject = new TestCustomConverterHashMapObject();
    TestObject to = new TestObject();
    to.setOne("one");
    testCustomConverterHashMapObject.setTestObject(to);
    TestObjectPrime top = new TestObjectPrime();
    top.setOnePrime("onePrime");
    testCustomConverterHashMapObject.setTestObjectPrime(top);
    TestCustomConverterHashMapPrimeObject dest = (TestCustomConverterHashMapPrimeObject) mapper.map(
        testCustomConverterHashMapObject, TestCustomConverterHashMapPrimeObject.class);
    assertEquals(to, dest.getTestObjects().get("object1"));
    assertEquals(top, dest.getTestObjects().get("object2"));

  }

  public void testStringToDateMapping() throws Exception {
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss:SS");
    String dateStr = "01/29/1975 10:45:13:25";
    TestObject sourceObj = new TestObject();
    sourceObj.setDateStr(dateStr);
    TestObjectPrime result = (TestObjectPrime) mapper.map(sourceObj, TestObjectPrime.class);
    assertEquals(df.parse(dateStr), result.getDateFromStr());
    assertEquals(dateStr, df.format(result.getDateFromStr()));

    TestObject result2 = (TestObject) mapper.map(result, TestObject.class);
    assertEquals(df.format(result.getDateFromStr()), result2.getDateStr());
    assertEquals(result.getDateFromStr(), df.parse(result2.getDateStr()));
  }

  public void testNullDestClass() throws Exception {
    try {
      mapper.map(new TestObject(), null);
      fail("should have thrown mapping exception");
    } catch (MappingException e) {
    }
  }

  public void testNullDestObj() throws Exception {
    try {
      Object destObj = null;
      mapper.map(new TestObject(), destObj);
      fail("should have thrown mapping exception");
    } catch (MappingException e) {
    }
  }

  public void testNoGetReadMethod() throws Exception {
    try {
      mapper.map(new NoReadMethod(), NoReadMethodPrime.class);
      fail("should have thrown mapping exception");
    } catch (MappingException e) {
      assertEquals(
          "net.sf.dozer.util.mapping.MappingException: Unable to determine read method for field: noReadMethod class: class net.sf.dozer.util.mapping.vo.NoReadMethod",
          e.getMessage());
    }
  }

  public void testNoGetWriteMethod() throws Exception {
    try {
      mapper.map(new NoWriteMethod(), NoWriteMethodPrime.class);
      fail("should have thrown mapping exception");
    } catch (MappingException e) {
      assertEquals(
          "net.sf.dozer.util.mapping.MappingException: Unable to determine write method for field: noWriteMethod class: class net.sf.dozer.util.mapping.vo.NoWriteMethodPrime",
          e.getMessage());
    }
  }

  public void testOneWayMapping() throws Exception {
    // Map
    OneWayObject owo = new OneWayObject();
    OneWayObjectPrime owop = new OneWayObjectPrime();
    SrcNestedDeepObj nested = new SrcNestedDeepObj();
    nested.setSrc1("src1");
    owo.setNested(nested);
    owop.setOneWayPrimeField("oneWayField");
    owop.setSetOnlyField("setOnly");
    List list = new ArrayList();
    list.add("stringToList");
    list.add("src1");
    owop.setStringList(list);
    owo.setOneWayField("oneWayField");
    owo.setStringToList("stringToList");
    OneWayObjectPrime prime = (OneWayObjectPrime) mapper.map(owo, OneWayObjectPrime.class);

    assertEquals(owop, prime);

    OneWayObject source = (OneWayObject) mapper.map(prime, OneWayObject.class);
    // should have not mapped this way
    assertEquals(null, source.getOneWayField());
  }

  public void testNullField() throws Exception {
    AnotherTestObject src = new AnotherTestObject();
    src.setField2(null);
    AnotherTestObjectPrime dest = new AnotherTestObjectPrime();
    dest.setField2(Integer.valueOf("555"));
    // check that null overrides an existing value
    mapper.map(src, dest);
    assertNull("dest field should be null", dest.getField2());
  }

  public void testNullField2() throws Exception {
    // Test that String --> String with an empty String input value results
    // in the destination field being an empty String and not null.
    String input = "";
    TestObject src = new TestObject();
    src.setOne(input);

    TestObjectPrime dest = (TestObjectPrime) mapper.map(src, TestObjectPrime.class);
    assertNotNull("dest field should not be null", dest.getOnePrime());
    assertEquals("invalid dest field value", input, dest.getOnePrime());
  }

  public void testNullToPrimitive() throws Exception {
    AnotherTestObject src = new AnotherTestObject();
    AnotherTestObjectPrime prime = new AnotherTestObjectPrime();
    TestObject to = new TestObject();
    to.setThePrimitive(AnotherTestObjectPrime.DEFAULT_FIELD1);
    prime.setTo(to);
    mapper.map(src, prime);
    // check primitive on deep field
    // primitive should still be default
    assertEquals("invalid field value", AnotherTestObjectPrime.DEFAULT_FIELD1, prime.getField1());
    assertEquals("invalid field value", AnotherTestObjectPrime.DEFAULT_FIELD1, prime.getTo().getThePrimitive());
  }

  public void testMapByReference() throws Exception {
    // Map
    TestReferenceObject tro = new TestReferenceObject();
    TestReferenceFoo foo1 = new TestReferenceFoo();
    foo1.setA("a");
    TestReferenceFoo foo = new TestReferenceFoo();
    foo.setA("a");
    foo.setB(null);
    foo.setC("c");
    List list2 = new ArrayList();
    list2.add(foo);
    tro.setListA(list2);
    tro.setArrayToArrayCumulative(new Object[] { foo1 });
    TestReferenceFoo foo2 = new TestReferenceFoo();
    foo2.setA("a");
    foo2.setB(null);
    foo2.setC("c");
    TestReferenceFoo foo3 = new TestReferenceFoo();
    foo3.setA("a");
    foo3.setB(null);
    foo3.setC("c");
    tro.setArrayToArrayNoncumulative(new Object[] { foo2 });
    List list3 = new ArrayList();
    list3.add("string1");
    list3.add("string2");
    tro.setListToArray(list3);
    int[] pa = { 1, 2, 3 };
    tro.setPrimitiveArray(pa);
    Integer[] integerArray = { new Integer(1), new Integer(2) };
    tro.setPrimitiveArrayWrapper(integerArray);
    Set set = new HashSet();
    TestReferenceFoo foo4 = new TestReferenceFoo();
    foo4.setA("a");
    set.add(foo4);
    tro.setSetToSet(set);
    Car car = new Car();
    car.setName("myName");
    tro.setCars(new Car[] { car });
    Car car2 = new Car();
    car2.setName("myName");
    List vehicles = new ArrayList();
    vehicles.add(car2);
    tro.setVehicles(vehicles);
    TestReferenceObject toClone = (TestReferenceObject) SerializationUtils.clone(tro);
    TestReferencePrimeObject trop = (TestReferencePrimeObject) mapper.map(tro, TestReferencePrimeObject.class);
    assertEquals("myName", ((Van) trop.getVans()[0]).getName());
    assertEquals("myName", ((Van) trop.getMoreVans()[0]).getName());

    TestReferenceFooPrime fooPrime = (TestReferenceFooPrime) trop.getListAPrime().get(0);
    fooPrime.setB("b");
    TestReferenceFooPrime fooPrime2 = (TestReferenceFooPrime) trop.getArrayToArrayNoncumulative()[0];
    fooPrime2.setB("b");
    mapper.map(trop, tro);
    // make sure we update the array list and didnt lose the value 'c' - non-cumulative
    assertEquals("c", ((TestReferenceFoo) tro.getListA().get(0)).getC());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -