View Javadoc

1   /*
2    * $Id: BeanUtilsTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.util;
11  
12  import org.mule.tck.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  public class BeanUtilsTestCase extends AbstractMuleTestCase
23  {
24      private Map<String, String> map;
25  
26      @Override
27      protected void doSetUp() throws Exception
28      {
29          super.doSetUp();
30  
31          map = new HashMap<String, String>();
32          map.put("brand", "Juicy!");
33          map.put("radius", "2.32");
34          map.put("segments", "22");
35          map.put("trombones", "3");
36      }
37  
38      public void testBeanPropertiesOnAProxy() throws Exception
39      {
40          OrangeInterface o = (OrangeInterface)Proxy.newProxyInstance(getClass().getClassLoader(),
41                  new Class[]{OrangeInterface.class}, new OrangeInvocationHandler(new Orange()));
42  
43          BeanUtils.populateWithoutFail(o, map, true);
44  
45          assertNotNull(o);
46          assertEquals("Juicy!", o.getBrand());
47          assertEquals(new Double(2.32), o.getRadius());
48          assertEquals(new Integer(22), o.getSegments());
49      }
50  
51      public void testBeanPropertiesWithoutFail() throws Exception
52      {
53          Orange o = new Orange();
54  
55          BeanUtils.populateWithoutFail(o, map, true);
56  
57          assertNotNull(o);
58          assertEquals("Juicy!", o.getBrand());
59          assertEquals(new Double(2.32), o.getRadius());
60          assertEquals(new Integer(22), o.getSegments());
61      }
62  
63      public void testBeanPropertiesWithFail() throws Exception
64      {
65          try
66          {
67              BeanUtils.populate(new Orange(), map);
68              fail("Trombones is not a valid property");
69          }
70          catch (IllegalArgumentException e)
71          {
72              //expected
73              assertTrue(e.getMessage().indexOf("trombone") > -1);
74          }
75      }
76  
77      private class OrangeInvocationHandler implements InvocationHandler
78      {
79          private Orange orange;
80  
81          public OrangeInvocationHandler(Orange orange)
82          {
83              this.orange = orange;
84          }
85  
86          public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
87          {
88              return method.invoke(orange, args); 
89          }
90      }
91  }