001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.spring.injection.annot;
018:
019: import java.lang.reflect.Field;
020:
021: import junit.framework.TestCase;
022:
023: import org.apache.wicket.proxy.ILazyInitProxy;
024: import org.apache.wicket.spring.ISpringContextLocator;
025: import org.apache.wicket.spring.SpringBeanLocator;
026: import org.apache.wicket.spring.injection.util.Bean;
027: import org.apache.wicket.spring.injection.util.Bean2;
028: import org.apache.wicket.spring.injection.util.Injectable;
029: import org.apache.wicket.spring.test.ApplicationContextMock;
030: import org.springframework.context.ApplicationContext;
031:
032: /**
033: * Tests for BeanAnnotLocatorFactory
034: *
035: * @author igor
036: *
037: */
038: public class AnnotProxyFieldValueFactoryTest extends TestCase {
039: ISpringContextLocator mockCtxLocator = new ISpringContextLocator() {
040: private static final long serialVersionUID = 1L;
041:
042: public ApplicationContext getSpringContext() {
043: ApplicationContextMock mock = new ApplicationContextMock();
044: mock.putBean(new Bean());
045: mock.putBean("somebean", new Bean2());
046: return mock;
047: }
048: };
049:
050: Injectable obj = new Injectable();
051:
052: AnnotProxyFieldValueFactory factory = new AnnotProxyFieldValueFactory(
053: mockCtxLocator);
054:
055: /**
056: * Test the factory
057: *
058: * @throws Exception
059: */
060: public void testFactory() throws Exception {
061: SpringBeanLocator locator = null;
062: Object proxy = null;
063:
064: Field field = obj.getClass().getDeclaredField("nobean");
065: proxy = factory.getFieldValue(field, obj);
066: assertNull(proxy);
067:
068: field = obj.getClass().getDeclaredField("beanByClass");
069: proxy = factory.getFieldValue(field, obj);
070: locator = (SpringBeanLocator) ((ILazyInitProxy) proxy)
071: .getObjectLocator();
072: assertTrue(locator.getBeanType().equals(Bean.class));
073: assertTrue(locator.getSpringContextLocator() == mockCtxLocator);
074: assertTrue(factory.getFieldValue(field, obj) instanceof ILazyInitProxy);
075:
076: field = obj.getClass().getDeclaredField("beanByName");
077: proxy = factory.getFieldValue(field, obj);
078: locator = (SpringBeanLocator) ((ILazyInitProxy) proxy)
079: .getObjectLocator();
080: assertTrue(locator.getBeanName().equals("somebean"));
081: assertTrue(locator.getBeanType().equals(Bean2.class));
082: assertTrue(locator.getSpringContextLocator() == mockCtxLocator);
083: assertTrue(factory.getFieldValue(field, obj) instanceof ILazyInitProxy);
084: }
085:
086: /**
087: * test the cache, make sure the same proxy is returned for the same
088: * dependency it represents
089: *
090: * @throws Exception
091: */
092: public void testCache() throws Exception {
093: Field field = obj.getClass().getDeclaredField("beanByClass");
094: Object proxy1 = factory.getFieldValue(field, obj);
095: Object proxy2 = factory.getFieldValue(field, obj);
096: assertTrue(proxy1 == proxy2);
097:
098: field = obj.getClass().getDeclaredField("beanByName");
099: proxy1 = factory.getFieldValue(field, obj);
100: proxy2 = factory.getFieldValue(field, obj);
101: assertTrue(proxy1 == proxy2);
102: }
103:
104: /**
105: * Test creation fails with null springcontextlocator
106: */
107: public void testNullContextLocator() {
108: try {
109: new AnnotProxyFieldValueFactory(null);
110: fail();
111: } catch (IllegalArgumentException e) {
112: // noop
113: }
114: }
115:
116: public void testFailsIfBeanWithIdIsNotFound() throws Exception {
117: InjectableWithReferenceToNonexistingBean obj = new InjectableWithReferenceToNonexistingBean();
118: Field field = obj.getClass().getDeclaredField("nonExisting");
119: try {
120: factory.getFieldValue(field, obj);
121: fail();
122: } catch (RuntimeException e) {
123: }
124: }
125:
126: static class InjectableWithReferenceToNonexistingBean {
127: @SpringBean(name="nonExisting")
128: @SuppressWarnings("unused")
129: private Bean nonExisting;
130: }
131: }
|