001: /*
002: * Copyright 2006-2007, Unitils.org
003: *
004: * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.unitils.inject;
017:
018: import junit.framework.TestCase;
019: import org.unitils.core.ConfigurationLoader;
020: import org.unitils.inject.annotation.InjectIntoStatic;
021:
022: import java.util.Properties;
023:
024: /**
025: * Test for the static injection behavior of the {@link InjectModule}
026: *
027: * @author Filip Neven
028: * @author Tim Ducheyne
029: */
030: public class InjectModuleInjectIntoStaticTest extends TestCase {
031:
032: /* Tested object */
033: private InjectModule injectModule;
034:
035: private TestInjectIntoStatic_simpleSetter testInjectIntoStatic_simpleSetter = new TestInjectIntoStatic_simpleSetter();
036: private TestInjectIntoStatic_simpleField testInjectIntoStatic_simpleField = new TestInjectIntoStatic_simpleField();
037: private TestInjectIntoStatic_compositeSetter testInjectIntoStatic_compositeSetter = new TestInjectIntoStatic_compositeSetter();
038: private TestInjectIntoStatic_compositeField testInjectIntoStatic_compositeField = new TestInjectIntoStatic_compositeField();
039:
040: /**
041: * Initializes the test and test fixture.
042: */
043: protected void setUp() throws Exception {
044: super .setUp();
045:
046: Properties configuration = new ConfigurationLoader()
047: .loadConfiguration();
048: injectModule = new InjectModule();
049: injectModule.init(configuration);
050: }
051:
052: public void testInject_simpleSetter() {
053: injectModule.injectObjects(testInjectIntoStatic_simpleSetter);
054: assertSame(testInjectIntoStatic_simpleSetter.toInject,
055: InjectOnStatic.getToInject());
056: }
057:
058: public void testInject_simpleField() {
059: injectModule.injectObjects(testInjectIntoStatic_simpleField);
060: assertSame(testInjectIntoStatic_simpleField.toInject,
061: InjectOnStatic.getToInjectField());
062: }
063:
064: public void testInject_compositeSetter() {
065: injectModule
066: .injectObjects(testInjectIntoStatic_compositeSetter);
067: assertSame(testInjectIntoStatic_compositeSetter.toInject,
068: InjectOnStatic.getTestObject().getToInject());
069: }
070:
071: public void testInject_compositeField() {
072: injectModule.injectObjects(testInjectIntoStatic_compositeField);
073: assertSame(testInjectIntoStatic_compositeField.toInject,
074: InjectOnStatic.getTestObjectField().getToInject());
075: }
076:
077: public class TestInjectIntoStatic_simpleSetter {
078:
079: @InjectIntoStatic(target=InjectOnStatic.class,property="toInject")
080: private ToInject toInject;
081:
082: public TestInjectIntoStatic_simpleSetter() {
083: toInject = new ToInject();
084: }
085: }
086:
087: public class TestInjectIntoStatic_simpleField {
088:
089: @InjectIntoStatic(target=InjectOnStatic.class,property="toInjectField")
090: private ToInject toInject;
091:
092: public TestInjectIntoStatic_simpleField() {
093: toInject = new ToInject();
094: }
095: }
096:
097: public class TestInjectIntoStatic_compositeSetter {
098:
099: @InjectIntoStatic(target=InjectOnStatic.class,property="testObject.toInject")
100: private ToInject toInject;
101:
102: public TestInjectIntoStatic_compositeSetter() {
103: toInject = new ToInject();
104: }
105: }
106:
107: public class TestInjectIntoStatic_compositeField {
108:
109: @InjectIntoStatic(target=InjectOnStatic.class,property="testObjectField.toInject")
110: private ToInject toInject;
111:
112: public TestInjectIntoStatic_compositeField() {
113: toInject = new ToInject();
114: }
115: }
116:
117: private static class InjectOnStatic {
118:
119: private static ToInject toInject;
120:
121: private static ToInject toInjectField;
122:
123: private static TestObject testObject;
124:
125: private static TestObject testObjectField;
126:
127: static {
128: testObject = new TestObject();
129: testObjectField = new TestObject();
130: }
131:
132: public static void setToInject(ToInject toInject) {
133: InjectOnStatic.toInject = toInject;
134: }
135:
136: public static void setTestObject(TestObject testObject) {
137: InjectOnStatic.testObject = testObject;
138: }
139:
140: public static ToInject getToInject() {
141: return toInject;
142: }
143:
144: public static ToInject getToInjectField() {
145: return toInjectField;
146: }
147:
148: public static TestObject getTestObject() {
149: return testObject;
150: }
151:
152: public static TestObject getTestObjectField() {
153: return testObjectField;
154: }
155: }
156:
157: public static class TestObject {
158:
159: private ToInject toInject;
160:
161: public void setToInject(ToInject toInject) {
162: this .toInject = toInject;
163: }
164:
165: public ToInject getToInject() {
166: return toInject;
167: }
168: }
169:
170: public static class ToInject {
171: }
172:
173: }
|