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.io.Serializable;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.tapestry.ioc.internal.IOCInternalTestCase;
021: import org.apache.tapestry.ioc.services.ClassFactory;
022: import org.testng.annotations.Test;
023:
024: public class BridgeBuilderTest extends IOCInternalTestCase {
025: private ClassFactory _classFactory = new ClassFactoryImpl();
026:
027: @Test
028: public void standard_interface_and_filter() {
029: Log log = mockLog();
030:
031: replay();
032:
033: BridgeBuilder<StandardService, StandardFilter> bb = new BridgeBuilder<StandardService, StandardFilter>(
034: log, StandardService.class, StandardFilter.class,
035: _classFactory);
036:
037: StandardFilter sf = new StandardFilter() {
038: public int run(int i, StandardService ss) {
039: return ss.run(i + 1);
040: }
041: };
042:
043: StandardService ss = new StandardService() {
044: public int run(int i) {
045: return i * 3;
046: }
047: };
048:
049: StandardService bridge = bb.instantiateBridge(ss, sf);
050:
051: // The filter adds 1, then the service multiplies by 3.
052: // (5 +_1) * 3 = 18.
053:
054: assertEquals(bridge.run(5), 18);
055:
056: // Since toString() is not part of the service interface,
057: // it will be implemented in the bridge.
058:
059: assertEquals(
060: bridge.toString(),
061: "<PipelineBridge from org.apache.tapestry.ioc.internal.services.StandardService to org.apache.tapestry.ioc.internal.services.StandardFilter>");
062:
063: verify();
064: }
065:
066: @Test
067: public void toString_part_of_service_interface() {
068: Log log = mockLog();
069:
070: replay();
071:
072: BridgeBuilder<ToStringService, ToStringFilter> bb = new BridgeBuilder<ToStringService, ToStringFilter>(
073: log, ToStringService.class, ToStringFilter.class,
074: _classFactory);
075:
076: ToStringFilter f = new ToStringFilter() {
077: public String toString(ToStringService s) {
078: return s.toString().toUpperCase();
079: }
080: };
081:
082: ToStringService s = new ToStringService() {
083: @Override
084: public String toString() {
085: return "Service";
086: }
087: };
088:
089: ToStringService bridge = bb.instantiateBridge(s, f);
090:
091: assertEquals("SERVICE", bridge.toString());
092:
093: verify();
094: }
095:
096: @Test
097: public void service_interface_method_not_matched_in_filter_interface() {
098: Log log = mockLog();
099: ExtraServiceMethod next = newMock(ExtraServiceMethod.class);
100: Serializable filter = newMock(Serializable.class);
101:
102: log
103: .error(
104: "Method void extraServiceMethod() has no match in filter interface java.io.Serializable.",
105: null);
106:
107: replay();
108:
109: BridgeBuilder<ExtraServiceMethod, Serializable> bb = new BridgeBuilder<ExtraServiceMethod, Serializable>(
110: log, ExtraServiceMethod.class, Serializable.class,
111: _classFactory);
112:
113: ExtraServiceMethod esm = bb.instantiateBridge(next, filter);
114:
115: try {
116: esm.extraServiceMethod();
117: unreachable();
118: } catch (RuntimeException ex) {
119: assertEquals(
120: ex.getMessage(),
121: "Method void extraServiceMethod() has no match in filter interface java.io.Serializable.");
122: }
123:
124: verify();
125: }
126:
127: @Test
128: public void filter_interface_contains_extra_methods() {
129: Log log = mockLog();
130: Serializable next = newMock(Serializable.class);
131: ExtraFilterMethod filter = newMock(ExtraFilterMethod.class);
132:
133: log
134: .error(
135: "Method void extraFilterMethod() of filter interface "
136: + "org.apache.tapestry.ioc.internal.services.ExtraFilterMethod does not have a matching method "
137: + "in java.io.Serializable.", null);
138:
139: replay();
140:
141: BridgeBuilder<Serializable, ExtraFilterMethod> bb = new BridgeBuilder<Serializable, ExtraFilterMethod>(
142: log, Serializable.class, ExtraFilterMethod.class,
143: _classFactory);
144:
145: assertNotNull(bb.instantiateBridge(next, filter));
146:
147: verify();
148: }
149:
150: @Test
151: public void service_parameter_in_middle_of_filter_method() {
152: Log log = mockLog();
153:
154: replay();
155:
156: BridgeBuilder<MiddleService, MiddleFilter> bb = new BridgeBuilder<MiddleService, MiddleFilter>(
157: log, MiddleService.class, MiddleFilter.class,
158: _classFactory);
159:
160: MiddleFilter mf = new MiddleFilter() {
161: public void execute(int count, char ch,
162: MiddleService service, StringBuilder buffer) {
163: service.execute(count, ch, buffer);
164:
165: buffer.append(' ');
166:
167: service.execute(count + 1, Character.toUpperCase(ch),
168: buffer);
169:
170: }
171: };
172:
173: MiddleService ms = new MiddleService() {
174: public void execute(int count, char ch, StringBuilder buffer) {
175: for (int i = 0; i < count; i++)
176: buffer.append(ch);
177: }
178: };
179:
180: // This also tests building the bridge methods with a void return type.
181:
182: MiddleService bridge = bb.instantiateBridge(ms, mf);
183:
184: StringBuilder buffer = new StringBuilder("CODE: ");
185:
186: bridge.execute(3, 'a', buffer);
187:
188: assertEquals("CODE: aaa AAAA", buffer.toString());
189:
190: verify();
191: }
192: }
|