FieldMapper { public static <T> void copy(final T source, final T dest) { copy(source, dest, null, null); }  static void copy(final T source, final T dest); static void copy(final T source, final T dest, final UpdateAction updateAction); static void copyNonNulls(final T source,
                                        final T dest,
                                        final UpdateAction updateAction); static void copyNonNulls(final T source,
                                        final T dest); static void copyNonDefaults(final T source,
                                           final T dest,
                                           final T vanillaObject,
                                           final UpdateAction updateAction); static void copyNonDefaults(final T source,
                                           final T dest,
                                           final T vanillaObject);  }
@Test void testSimple() { MyObject original = new MyObject(); original.setString("Original"); MyObject copy = new MyObject(); copy.setString("Copy"); FieldMapper.copy(original, copy); assertThat(copy.getString()) .isEqualTo(original.getString()); assertThat(System.identityHashCode(original)) .isNotEqualTo(System.identityHashCode(copy)); }
@Test void testNoChanges() { final MyParent parent1 = new MyParent(); final MyParent parent2 = new MyParent(); int parent1Id = System.identityHashCode(parent1); int parent1ChildId = System.identityHashCode(parent1.child); assertThat(parent2) .isEqualTo(parent1); assertThat(parent2.getChild()) .isEqualTo(parent1.getChild()); assertThat(System.identityHashCode(parent1)) .isNotEqualTo(System.identityHashCode(parent2)); assertThat(System.identityHashCode(parent1.child)) .isNotEqualTo(System.identityHashCode(parent2.child)); FieldMapper.copy(parent2, parent1); assertThat(parent2) .isEqualTo(parent1); assertThat(parent2.getChild()) .isEqualTo(parent1.getChild()); assertThat(System.identityHashCode(parent1)) .isNotEqualTo(System.identityHashCode(parent2)); assertThat(System.identityHashCode(parent1.child)) .isNotEqualTo(System.identityHashCode(parent2.child)); assertThat(System.identityHashCode(parent1)) .isEqualTo(parent1Id); assertThat(System.identityHashCode(parent1.child)) .isEqualTo(parent1ChildId); }
@Test void testNullSource() { MyObject original = new MyObject(); original.setString(null); MyObject copy = new MyObject(); copy.setString("copy"); FieldMapper.copy(original, copy); assertThat(copy.getString()) .isEqualTo(original.getString()); assertThat(System.identityHashCode(original)) .isNotEqualTo(System.identityHashCode(copy)); }
@Test void testNullDest() { MyObject original = new MyObject(); original.setString("NotNull"); MyObject copy = new MyObject(); copy.setString(null); FieldMapper.copy(original, copy); assertThat(copy.getString()) .isEqualTo(original.getString()); assertThat(System.identityHashCode(original)) .isNotEqualTo(System.identityHashCode(copy)); }
@Test void testDeepCopy() throws NoSuchFieldException, IllegalAccessException { final MyParent parent1 = new MyParent(); System.out.println("parent1 " + System.identityHashCode(parent1)); System.out.println("parent1 child " + System.identityHashCode(parent1.child)); int parent1Id = System.identityHashCode(parent1); int parent1ChildId = System.identityHashCode(parent1.child); final MyParent parent2 = new MyParent(); parent2.setMyInt(99); parent2.setMyString("changed"); MyChild child3 = new MyChild(); child3.setMyInt(999); child3.setMyString("changed child"); parent2.setChild(child3); assertThat(parent2) .isNotEqualTo(parent1); assertThat(parent2.getChild()) .isNotEqualTo(parent1.getChild()); System.out.println("parent2 " + System.identityHashCode(parent2)); System.out.println("parent2 child " + System.identityHashCode(parent2.child)); assertThat(System.identityHashCode(parent1)) .isNotEqualTo(System.identityHashCode(parent2)); FieldMapper.copy(parent2, parent1); System.out.println(); System.out.println("parent1 " + System.identityHashCode(parent1)); System.out.println("parent1 child " + System.identityHashCode(parent1.child)); System.out.println("parent2 " + System.identityHashCode(parent2)); System.out.println("parent2 child " + System.identityHashCode(parent2.child)); assertThat(System.identityHashCode(parent1)) .isNotEqualTo(System.identityHashCode(parent2)); assertThat(System.identityHashCode(parent1.child)) .isNotEqualTo(System.identityHashCode(parent2.child)); assertThat(parent2) .isEqualTo(parent1); assertThat(parent2.getChild()) .isEqualTo(parent1.getChild()); assertThat(System.identityHashCode(parent1)) .isEqualTo(parent1Id); assertThat(System.identityHashCode(parent1.child)) .isEqualTo(parent1ChildId); }