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