001 /**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.kcb.util;
017
018 import org.junit.Assert;
019 import org.junit.Before;
020 import org.junit.Test;
021 import org.springframework.beans.factory.support.StaticListableBeanFactory;
022
023 import java.lang.reflect.Proxy;
024
025
026 /**
027 * Tests BeanFactoryIinvocationHandler class
028 *
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031 public class BeanFactoryInvocationHandlerTest {
032 private static interface BadInterface {
033 public int add(int a, int b);
034
035 public void notAGetter();
036
037 public void get();
038
039 public Object getBean();
040 }
041
042 private BadInterface bad;
043
044 @Before
045 public void createBadInstance() {
046 StaticListableBeanFactory bf = new StaticListableBeanFactory();
047 bf.addBean("bean", "This is a bean");
048 bad = (BadInterface)
049 Proxy.newProxyInstance(this.getClass().getClassLoader(),
050 new Class[]{BadInterface.class},
051 new BeanFactoryInvocationHandler(bf));
052 }
053
054 @Test(expected = RuntimeException.class)
055 public void testRandomMethod() {
056 int result = bad.add(2, 2);
057 }
058
059 @Test(expected = RuntimeException.class)
060 public void testNotAGetter() {
061 bad.notAGetter();
062 }
063
064 @Test(expected = RuntimeException.class)
065 public void testAnotherBadGetter() {
066 bad.get();
067 }
068
069 @Test
070 public void testGoodGetter() {
071 Assert.assertEquals("This is a bean", bad.getBean());
072 }
073 }