View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kcb.util;
17  
18  import org.junit.Assert;
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.springframework.beans.factory.support.StaticListableBeanFactory;
22  
23  import java.lang.reflect.Proxy;
24  
25  
26  /**
27   * Tests BeanFactoryIinvocationHandler class
28   *
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class BeanFactoryInvocationHandlerTest {
32      private static interface BadInterface {
33          public int add(int a, int b);
34  
35          public void notAGetter();
36  
37          public void get();
38  
39          public Object getBean();
40      }
41  
42      private BadInterface bad;
43  
44      @Before
45      public void createBadInstance() {
46          StaticListableBeanFactory bf = new StaticListableBeanFactory();
47          bf.addBean("bean", "This is a bean");
48          bad = (BadInterface)
49                  Proxy.newProxyInstance(this.getClass().getClassLoader(),
50                          new Class[]{BadInterface.class},
51                          new BeanFactoryInvocationHandler(bf));
52      }
53  
54      @Test(expected = RuntimeException.class)
55      public void testRandomMethod() {
56          int result = bad.add(2, 2);
57      }
58  
59      @Test(expected = RuntimeException.class)
60      public void testNotAGetter() {
61          bad.notAGetter();
62      }
63  
64      @Test(expected = RuntimeException.class)
65      public void testAnotherBadGetter() {
66          bad.get();
67      }
68  
69      @Test
70      public void testGoodGetter() {
71          Assert.assertEquals("This is a bean", bad.getBean());
72      }
73  }