1
2
3
4
5
6
7
8
9
10 package org.mule.util;
11
12 import org.mule.tck.junit4.AbstractMuleTestCase;
13 import org.mule.tck.testmodels.fruit.Orange;
14 import org.mule.tck.testmodels.fruit.OrangeInterface;
15
16 import java.lang.reflect.InvocationHandler;
17 import java.lang.reflect.Method;
18 import java.lang.reflect.Proxy;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.junit.Before;
23 import org.junit.Test;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 public class BeanUtilsTestCase extends AbstractMuleTestCase
31 {
32 private Map<String, String> map;
33
34 @Before
35 public void createTestData()
36 {
37 map = new HashMap<String, String>();
38 map.put("brand", "Juicy!");
39 map.put("radius", "2.32");
40 map.put("segments", "22");
41 map.put("trombones", "3");
42 }
43
44 @Test
45 public void testBeanPropertiesOnAProxy() throws Exception
46 {
47 OrangeInterface o = (OrangeInterface)Proxy.newProxyInstance(getClass().getClassLoader(),
48 new Class[]{OrangeInterface.class}, new OrangeInvocationHandler(new Orange()));
49
50 BeanUtils.populateWithoutFail(o, map, true);
51
52 assertNotNull(o);
53 assertEquals("Juicy!", o.getBrand());
54 assertEquals(new Double(2.32), o.getRadius());
55 assertEquals(new Integer(22), o.getSegments());
56 }
57
58 @Test
59 public void testBeanPropertiesWithoutFail() throws Exception
60 {
61 Orange o = new Orange();
62
63 BeanUtils.populateWithoutFail(o, map, true);
64
65 assertNotNull(o);
66 assertEquals("Juicy!", o.getBrand());
67 assertEquals(new Double(2.32), o.getRadius());
68 assertEquals(new Integer(22), o.getSegments());
69 }
70
71 @Test
72 public void testBeanPropertiesWithFail() throws Exception
73 {
74 try
75 {
76 BeanUtils.populate(new Orange(), map);
77 fail("Trombones is not a valid property");
78 }
79 catch (IllegalArgumentException e)
80 {
81
82 assertTrue(e.getMessage().indexOf("trombone") > -1);
83 }
84 }
85
86 private class OrangeInvocationHandler implements InvocationHandler
87 {
88 private Orange orange;
89
90 public OrangeInvocationHandler(Orange orange)
91 {
92 this.orange = orange;
93 }
94
95 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
96 {
97 return method.invoke(orange, args);
98 }
99 }
100 }