From f79d261ea38f061cdaac760cb6e7976dc4fdb36b Mon Sep 17 00:00:00 2001 From: johnniang Date: Mon, 4 Mar 2019 13:58:00 +0800 Subject: [PATCH] Add BeanUtilsTest --- .../java/cc/ryanc/halo/utils/BeanUtils.java | 3 +- .../cc/ryanc/halo/utils/BeanUtilsTest.java | 98 +++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/test/java/cc/ryanc/halo/utils/BeanUtilsTest.java diff --git a/src/main/java/cc/ryanc/halo/utils/BeanUtils.java b/src/main/java/cc/ryanc/halo/utils/BeanUtils.java index d4e8bd387..21ea19391 100644 --- a/src/main/java/cc/ryanc/halo/utils/BeanUtils.java +++ b/src/main/java/cc/ryanc/halo/utils/BeanUtils.java @@ -45,7 +45,8 @@ public class BeanUtils { // Init the instance try { // New instance for the target class - T targetInstance = targetClass.getDeclaredConstructor().newInstance(); + // TODO Class.newInstance() is deprecated in Java 9 + T targetInstance = targetClass.newInstance(); // Copy properties org.springframework.beans.BeanUtils.copyProperties(source, targetInstance, getNullPropertyNames(source)); // Return the target instance diff --git a/src/test/java/cc/ryanc/halo/utils/BeanUtilsTest.java b/src/test/java/cc/ryanc/halo/utils/BeanUtilsTest.java new file mode 100644 index 000000000..0d4c389d5 --- /dev/null +++ b/src/test/java/cc/ryanc/halo/utils/BeanUtilsTest.java @@ -0,0 +1,98 @@ +package cc.ryanc.halo.utils; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +/** + * BeanUtils test. + * + * @author johnniang + */ +public class BeanUtilsTest { + + @Test + public void transformFrom() { + TestA a = new TestA(1, 2); + + TestC c = BeanUtils.transformFrom(a, TestC.class); + assertEquals(a.getA(), c.getA()); + assertEquals(a.getB(), c.getB()); + + TestB b = BeanUtils.transformFrom(a, TestB.class); + assertEquals(a.getB(), b.getB()); + assertNull(b.getC()); + + } + + @Test + public void transformFromInBatch() { + TestA[] as = { + new TestA(1, 2), + new TestA(3, 4) + }; + + List aList = Arrays.asList(as); + + List cs = BeanUtils.transformFromInBatch(aList, TestC.class); + assertEquals(as.length, cs.size()); + for (int i = 0; i < cs.size(); i++) { + assertEquals(as[i].getA(), cs.get(i).getA()); + assertEquals(as[i].getB(), cs.get(i).getB()); + } + + List bs = BeanUtils.transformFromInBatch(aList, TestB.class); + assertEquals(as.length, bs.size()); + for (int i = 0; i < bs.size(); i++) { + assertEquals(as[i].getB(), bs.get(i).getB()); + assertNull(bs.get(i).getC()); + } + } + + @Test + public void updateProperties() { + TestA a = new TestA(1, 2); + TestB b = new TestB(3, 4); + TestC c = new TestC(5, 6); + + BeanUtils.updateProperties(a, b); + assertEquals(b.getB(), a.getB()); + assertEquals(b.getC(), Integer.valueOf(4)); + + BeanUtils.updateProperties(a, c); + assertEquals(c.getA(), a.getA()); + assertEquals(c.getB(), a.getB()); + } + + @Data + @NoArgsConstructor + @AllArgsConstructor + static class TestA { + private Integer a; + private Integer b; + } + + @Data + @NoArgsConstructor + @AllArgsConstructor + static class TestB { + private Integer b; + private Integer c; + } + + @Data + @NoArgsConstructor + @AllArgsConstructor + static class TestC { + private Integer a; + private Integer b; + } + +} \ No newline at end of file