1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.christianschenk.beanintrospect;
20
21 import org.christianschenk.beanintrospect.beans.A;
22 import org.christianschenk.beanintrospect.beans.B;
23 import org.christianschenk.beanintrospect.beans.C;
24 import org.junit.Before;
25 import org.junit.Test;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.fail;
30
31 public class BeanIntrospectorTest {
32
33 private BeanIntrospector beanIntrospector;
34
35 @Before
36 public void setUp() {
37 this.beanIntrospector = new BeanIntrospector();
38 }
39
40 @Test
41 public void copy() {
42 final A a1 = new A(1, 2);
43 final A a2 = new A();
44 this.beanIntrospector.fill(a1, a2);
45 assertEquals(a1.getI(), a2.getI());
46 assertEquals(a1.getJ(), a2.getJ());
47 }
48
49 @Test
50 public void fillSmallerWithBiggerBean() {
51 final A a = new A();
52 final B b = new B(1, 2);
53 this.beanIntrospector.fill(a, b);
54 assertEquals(a.getI(), b.getI());
55 assertEquals(a.getJ(), b.getJ());
56 }
57
58 @Test
59 public void fillBiggerWithSmallerBean() {
60 final A a = new A(1, 2);
61 final B b = new B();
62
63 try {
64 this.beanIntrospector.fill(b, a);
65 fail("Should throw exception");
66 } catch (final Exception ex) {
67 }
68
69
70 this.beanIntrospector.ignoreMethodName(B.class, "StR");
71 this.beanIntrospector.fill(b, a);
72 assertEquals(a.getI(), b.getI());
73 assertEquals(a.getJ(), b.getJ());
74 assertNull(b.getStr());
75 }
76
77 @Test
78 public void mappingMethodNames() {
79 final B b = new B(1, 2, "henner");
80 final C c = new C();
81
82 this.beanIntrospector.addMethodNameMapping(B.class, "StR", C.class, "hURz");
83 this.beanIntrospector.fill(c, b);
84 assertEquals(b.getStr(), c.getHurz());
85
86 try {
87 this.beanIntrospector.fill(b, c);
88 fail("Should throw exception");
89 } catch (final Exception ex) {
90 }
91
92 this.beanIntrospector.ignoreMethodName(B.class, "i");
93 this.beanIntrospector.ignoreMethodName(B.class, "j");
94 this.beanIntrospector.fill(b, c);
95 assertEquals(b.getStr(), c.getHurz());
96 }
97 }