1   package com.atlassian.core.test.util;
2   
3   import org.apache.commons.lang.ArrayUtils;
4   
5   import java.util.Map;
6   import java.util.HashMap;
7   import java.lang.reflect.Method;
8   
9   /**
10   * Handles getters and setters by storing their values in a map. Calling a method
11   * that is not a getter or a setter will throw UnsupportedOperationException.
12   * <p/>
13   * Make sure you create one of these per test, as they are stateful.
14   */
15  public class JavaBeanMethodHandler implements DuckTypeProxy.UnimplementedMethodHandler
16  {
17      private Map<String, Object> data = new HashMap<String, Object>();
18  
19      public Object methodNotImplemented(Method method, Object[] args)
20      {
21          final String name = method.getName();
22          String key = (name.length() < 4) ? null :
23              name.substring(3, 4).toLowerCase() + name.substring(4);
24  
25          if (name.startsWith("get") && ArrayUtils.getLength(args) == 0)
26          {
27              return data.get(key);
28          }
29          else if (name.startsWith("set") && ArrayUtils.getLength(args) == 1)
30          {
31              data.put(key, args[0]);
32              return null;
33          }
34  
35          throw new UnsupportedOperationException(method.toString());
36      }
37  }