1   /*
2    * IntrospectionPerformanceTest - Tests introspection on JavaBeans
3    * Copyright (C) 2007 Christian Schenk
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    * 
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
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  		// method name should ignore upper and lower case
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  		// method name mapping should ignore upper and lower case
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  }