001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.internal.services;
016:
017: import java.util.Arrays;
018: import java.util.List;
019:
020: import org.apache.tapestry.ioc.internal.IOCInternalTestCase;
021: import org.apache.tapestry.ioc.services.ChainBuilder;
022: import org.testng.Assert;
023: import org.testng.annotations.Test;
024:
025: public class ChainBuilderImplTest extends IOCInternalTestCase {
026: private final ChainBuilder _builder = new ChainBuilderImpl(
027: new ClassFactoryImpl());
028:
029: @Test
030: public void simple_void_method() {
031: Runnable r1 = mockRunnable();
032: Runnable r2 = mockRunnable();
033:
034: // Training:
035:
036: r1.run();
037: r2.run();
038:
039: replay();
040:
041: Runnable chain = build(Runnable.class, r1, r2);
042:
043: chain.run();
044:
045: verify();
046:
047: Assert.assertEquals(chain.toString(),
048: "<Command chain of java.lang.Runnable>");
049: }
050:
051: @Test
052: public void int_method() {
053: ChainCommand c1 = mockChainCommand();
054: ChainCommand c2 = mockChainCommand();
055:
056: expect(c1.workInt(7)).andReturn(0);
057:
058: expect(c2.workInt(7)).andReturn(99);
059:
060: replay();
061:
062: ChainCommand chain = build(ChainCommand.class, c1, c2);
063:
064: assertEquals(chain.workInt(7), 99);
065:
066: verify();
067: }
068:
069: @Test
070: public void int_method_shortcircuits() {
071: ChainCommand c1 = mockChainCommand();
072: ChainCommand c2 = mockChainCommand();
073:
074: expect(c1.workInt(7)).andReturn(88);
075:
076: replay();
077:
078: ChainCommand chain = build(ChainCommand.class, c1, c2);
079:
080: assertEquals(chain.workInt(7), 88);
081:
082: verify();
083: }
084:
085: @Test
086: public void boolean_method() {
087: ChainCommand c1 = mockChainCommand();
088: ChainCommand c2 = mockChainCommand();
089:
090: train_workBoolean(c1, true, false);
091: train_workBoolean(c2, true, true);
092:
093: replay();
094:
095: ChainCommand chain = build(ChainCommand.class, c1, c2);
096:
097: assertEquals(chain.workBoolean(true), true);
098:
099: verify();
100: }
101:
102: protected final void train_workBoolean(ChainCommand command,
103: boolean parameter, boolean result) {
104: expect(command.workBoolean(parameter)).andReturn(result);
105: }
106:
107: @Test
108: public void string_method() {
109: ChainCommand c1 = mockChainCommand();
110: ChainCommand c2 = mockChainCommand();
111:
112: expect(c1.workString("fred")).andReturn(null);
113:
114: expect(c2.workString("fred")).andReturn("flintstone");
115:
116: replay();
117:
118: ChainCommand chain = build(ChainCommand.class, c1, c2);
119:
120: assertEquals(chain.workString("fred"), "flintstone");
121:
122: verify();
123:
124: }
125:
126: @Test
127: public void double_method() {
128: ChainCommand c1 = mockChainCommand();
129: ChainCommand c2 = mockChainCommand();
130:
131: expect(c1.workDouble(1.2d)).andReturn(0d);
132:
133: expect(c2.workDouble(1.2d)).andReturn(3.14d);
134:
135: replay();
136:
137: ChainCommand chain = build(ChainCommand.class, c1, c2);
138:
139: assertEquals(chain.workDouble(1.2d), 3.14d);
140:
141: verify();
142: }
143:
144: private ChainCommand mockChainCommand() {
145: return newMock(ChainCommand.class);
146: }
147:
148: @Test
149: public void fabricated_classes_are_reused() {
150: Runnable r1 = mockRunnable();
151: Runnable r2 = mockRunnable();
152:
153: Runnable chain1 = build(Runnable.class, r1);
154: Runnable chain2 = build(Runnable.class, r2);
155:
156: Assert.assertSame(chain1.getClass(), chain2.getClass());
157:
158: // Now make sure that the two instances are independent.
159:
160: r1.run();
161:
162: replay();
163:
164: chain1.run();
165:
166: verify();
167:
168: r2.run();
169:
170: replay();
171:
172: chain2.run();
173:
174: verify();
175: }
176:
177: private <T> T build(Class<T> commandInterface, T... commands) {
178: List<T> list = Arrays.asList(commands);
179:
180: return _builder.build(commandInterface, list);
181: }
182:
183: }
|