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.injection;
018:
019: import java.lang.reflect.Field;
020: import java.util.Arrays;
021: import java.util.List;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.wicket.injection.CompoundFieldValueFactory;
026: import org.apache.wicket.injection.IFieldValueFactory;
027: import org.easymock.MockControl;
028:
029: /**
030: * Test {@link CompoundFieldValueFactory}
031: *
032: * @author Igor Vaynberg (ivaynberg)
033: *
034: */
035: public class CompoundFieldValueFactoryTest extends TestCase {
036: private Integer testField;
037:
038: private Field field;
039:
040: private MockControl[] ctrl = new MockControl[4];
041:
042: private IFieldValueFactory[] fact = new IFieldValueFactory[4];
043:
044: protected void setUp() throws Exception {
045: Field field = CompoundFieldValueFactoryTest.class
046: .getDeclaredField("testField");
047:
048: for (int i = 0; i < 4; i++) {
049: ctrl[i] = MockControl
050: .createControl(IFieldValueFactory.class);
051: fact[i] = (IFieldValueFactory) ctrl[i].getMock();
052: }
053: }
054:
055: protected void prepare(int cnt) {
056: for (int i = 0; i < cnt; i++) {
057: ctrl[i].expectAndReturn(fact[i].getFieldValue(field, this ),
058: null);
059: ctrl[i].replay();
060: }
061: }
062:
063: protected void verify(int cnt) {
064: for (int i = 0; i < cnt; i++) {
065: ctrl[i].verify();
066: }
067: }
068:
069: /**
070: * Test array constructor
071: */
072: public void testArrayConstructor() {
073: prepare(2);
074: CompoundFieldValueFactory f = new CompoundFieldValueFactory(
075: new IFieldValueFactory[] { fact[0], fact[1] });
076: f.getFieldValue(field, this );
077: verify(2);
078:
079: try {
080: f = new CompoundFieldValueFactory(
081: (IFieldValueFactory[]) null);
082: fail();
083: } catch (IllegalArgumentException e) {
084: // noop
085: }
086: }
087:
088: /**
089: * Test list constructor
090: */
091: public void testListConstructor() {
092: prepare(4);
093: List list = Arrays.asList(new IFieldValueFactory[] { fact[0],
094: fact[1], fact[2], fact[3] });
095: CompoundFieldValueFactory f = new CompoundFieldValueFactory(
096: list);
097: f.getFieldValue(field, this );
098: verify(4);
099:
100: try {
101: f = new CompoundFieldValueFactory((List) null);
102: fail();
103: } catch (IllegalArgumentException e) {
104: // noop
105: }
106:
107: }
108:
109: /**
110: * Test list constructor
111: */
112: public void testABConstructor() {
113: prepare(2);
114: CompoundFieldValueFactory f = new CompoundFieldValueFactory(
115: fact[0], fact[1]);
116: f.getFieldValue(field, this );
117: verify(2);
118:
119: try {
120: f = new CompoundFieldValueFactory(fact[0], null);
121: fail();
122: } catch (IllegalArgumentException e) {
123: // noop
124: }
125: try {
126: f = new CompoundFieldValueFactory(null, fact[1]);
127: fail();
128: } catch (IllegalArgumentException e) {
129: // noop
130: }
131:
132: }
133:
134: /**
135: * Test list constructor
136: */
137: public void testBreakOnNonNullReturn() {
138: prepare(2);
139: ctrl[2].expectAndReturn(fact[2].getFieldValue(field, this ),
140: new Object());
141: ctrl[2].replay();
142: ctrl[3].replay();
143: List list = Arrays.asList(new IFieldValueFactory[] { fact[0],
144: fact[1], fact[2], fact[3] });
145: CompoundFieldValueFactory f = new CompoundFieldValueFactory(
146: list);
147:
148: f.getFieldValue(field, this );
149:
150: verify(4);
151: }
152:
153: /**
154: * Test addFactory()
155: */
156: public void testAdd() {
157: prepare(3);
158: CompoundFieldValueFactory f = new CompoundFieldValueFactory(
159: new IFieldValueFactory[] { fact[0], fact[1] });
160: f.addFactory(fact[2]);
161: f.getFieldValue(field, this );
162: verify(3);
163:
164: try {
165: f.addFactory(null);
166: fail();
167: } catch (IllegalArgumentException e) {
168: // noop
169: }
170: }
171:
172: }
|