001 /*
002 * Copyright 2007-2009 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 java.lang.reflect.Proxy;
019
020 import junit.framework.Assert;
021
022 import org.junit.Test;
023 import org.kuali.rice.kcb.util.BeanFactoryInvocationHandler;
024 import org.springframework.beans.factory.support.StaticListableBeanFactory;
025 import org.springframework.test.AssertThrows;
026
027
028 /**
029 * Tests BeanFactoryIinvocationHandler class
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032 public class BeanFactoryInvocationHandlerTest {
033 private static interface BadInterface {
034 public int add(int a, int b);
035 public void notAGetter();
036 public void get();
037 public Object getBean();
038 }
039
040 @Test
041 public void test() {
042 StaticListableBeanFactory bf = new StaticListableBeanFactory();
043 bf.addBean("bean", "This is a bean");
044 final BadInterface bad = (BadInterface)
045 Proxy.newProxyInstance(this.getClass().getClassLoader(),
046 new Class[] { BadInterface.class },
047 new BeanFactoryInvocationHandler(bf));
048 new AssertThrows(RuntimeException.class) {
049 public void test() {
050 int result = bad.add(2, 2);
051 }
052 }.runTest();
053 new AssertThrows(RuntimeException.class) {
054 public void test() {
055 bad.notAGetter();
056 }
057 }.runTest();
058 new AssertThrows(RuntimeException.class) {
059 public void test() {
060 bad.get();
061 }
062 }.runTest();
063 Assert.assertEquals("This is a bean", bad.getBean());
064 }
065 }