01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.injection;
18:
19: import java.lang.reflect.Field;
20:
21: import org.apache.wicket.injection.IFieldValueFactory;
22: import org.apache.wicket.injection.Injector;
23: import org.apache.wicket.injection.util.InternalTestObject;
24: import org.apache.wicket.injection.util.MockDependency;
25: import org.apache.wicket.injection.util.TestObject;
26:
27: import junit.framework.TestCase;
28:
29: /**
30: * Tests {@link Injector}
31: *
32: * @author Igor Vaynberg (ivaynberg)
33: *
34: */
35: public class InjectorTest extends TestCase {
36: private static MockDependency dependency = new MockDependency(
37: "inject");
38:
39: private static IFieldValueFactory factory = new IFieldValueFactory() {
40:
41: public Object getFieldValue(Field field, Object fieldOwner) {
42: return dependency;
43: }
44:
45: public boolean supportsField(Field field) {
46: return true;
47: }
48:
49: };
50:
51: /**
52: * Test injection
53: */
54: public void testInjection() {
55: TestObject testObject = new TestObject();
56:
57: Injector.getInstance().inject(testObject, factory);
58:
59: assertEquals(testObject.getDependency1().getMessage(), "inject");
60: assertEquals(testObject.getDependency2().getMessage(),
61: "dont-inject");
62: assertEquals(testObject.getDependency3().getMessage(),
63: "dont-inject");
64: assertEquals(testObject.getDependency4().getMessage(), "inject");
65: }
66:
67: /**
68: * Test abort injection on boundary class
69: */
70: public void testBreakOnBoundary() {
71: Injector injector = new Injector() {
72: protected boolean isBoundaryClass(Class clazz) {
73: return clazz.equals(InternalTestObject.class);
74: }
75: };
76:
77: TestObject testObject = new TestObject();
78:
79: injector.inject(testObject, factory);
80:
81: assertTrue(testObject.getDependency1() == null);
82: assertEquals(testObject.getDependency2().getMessage(),
83: "dont-inject");
84: assertEquals(testObject.getDependency3().getMessage(),
85: "dont-inject");
86: assertEquals(testObject.getDependency4().getMessage(), "inject");
87:
88: }
89:
90: }
|