001: // Copyright 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.internal.services;
016:
017: import org.apache.tapestry.Binding;
018: import org.apache.tapestry.annotations.Inject;
019: import org.apache.tapestry.ioc.Location;
020: import org.apache.tapestry.ioc.internal.util.TapestryException;
021: import org.apache.tapestry.test.TapestryTestCase;
022: import org.testng.annotations.Test;
023:
024: public class InheritedBindingTest extends TapestryTestCase {
025: private static final String BINDING_DESCRIPTION = "binding description";
026:
027: private static final String MESSAGE = "Exception in inner.";
028:
029: private static final Throwable _exception = new RuntimeException(
030: MESSAGE);
031:
032: @Test
033: public void to_string_and_location() {
034: Binding inner = mockBinding();
035: Location l = mockLocation();
036:
037: replay();
038:
039: InheritedBinding binding = new InheritedBinding(
040: BINDING_DESCRIPTION, inner, l);
041:
042: assertSame(binding.toString(), BINDING_DESCRIPTION);
043: assertSame(binding.getLocation(), l);
044:
045: verify();
046: }
047:
048: @Test
049: public void get_success() {
050: Binding inner = mockBinding();
051: Location l = mockLocation();
052: Object expected = new Object();
053:
054: train_get(inner, expected);
055:
056: replay();
057:
058: InheritedBinding binding = new InheritedBinding(
059: BINDING_DESCRIPTION, inner, l);
060:
061: assertSame(binding.get(), expected);
062:
063: verify();
064: }
065:
066: @Test
067: public void get_failure() {
068: Binding inner = mockBinding();
069: Location l = mockLocation();
070:
071: expect(inner.get()).andThrow(_exception);
072:
073: replay();
074:
075: InheritedBinding binding = new InheritedBinding(
076: BINDING_DESCRIPTION, inner, l);
077:
078: try {
079: binding.get();
080: unreachable();
081: } catch (TapestryException ex) {
082: checkException(ex, l);
083: }
084:
085: verify();
086: }
087:
088: @Test
089: public void get_binding_type_success() {
090: Binding inner = mockBinding();
091: Location l = mockLocation();
092: Class expected = Runnable.class;
093:
094: expect(inner.getBindingType()).andReturn(expected);
095:
096: replay();
097:
098: InheritedBinding binding = new InheritedBinding(
099: BINDING_DESCRIPTION, inner, l);
100:
101: assertSame(binding.getBindingType(), expected);
102:
103: verify();
104: }
105:
106: @Test
107: public void get_binding_type_failure() {
108: Binding inner = mockBinding();
109: Location l = mockLocation();
110: String description = BINDING_DESCRIPTION;
111:
112: expect(inner.getBindingType()).andThrow(_exception);
113:
114: replay();
115:
116: InheritedBinding binding = new InheritedBinding(description,
117: inner, l);
118:
119: try {
120: binding.getBindingType();
121: unreachable();
122: } catch (TapestryException ex) {
123: checkException(ex, l);
124: }
125:
126: verify();
127: }
128:
129: private void checkException(TapestryException ex, Location l) {
130: assertEquals(ex.getMessage(), MESSAGE);
131: assertEquals(ex.getLocation(), l);
132: assertSame(ex.getCause(), _exception);
133: }
134:
135: @Test
136: public void is_invariant_success() {
137: Binding inner = mockBinding();
138: Location l = mockLocation();
139: boolean expected = true;
140:
141: expect(inner.isInvariant()).andReturn(expected);
142:
143: replay();
144:
145: InheritedBinding binding = new InheritedBinding(
146: BINDING_DESCRIPTION, inner, l);
147:
148: assertEquals(binding.isInvariant(), expected);
149:
150: verify();
151: }
152:
153: @Test
154: public void is_invariant_failure() {
155: Binding inner = mockBinding();
156: Location l = mockLocation();
157: expect(inner.isInvariant()).andThrow(_exception);
158:
159: replay();
160:
161: InheritedBinding binding = new InheritedBinding(
162: BINDING_DESCRIPTION, inner, l);
163:
164: try {
165: binding.isInvariant();
166: unreachable();
167: } catch (TapestryException ex) {
168: checkException(ex, l);
169: }
170:
171: verify();
172: }
173:
174: @Test
175: public void set_success() {
176: Binding inner = mockBinding();
177: Location l = mockLocation();
178: String description = BINDING_DESCRIPTION;
179: Object parameter = new Object();
180:
181: inner.set(parameter);
182:
183: replay();
184:
185: InheritedBinding binding = new InheritedBinding(description,
186: inner, l);
187:
188: binding.set(parameter);
189:
190: verify();
191: }
192:
193: @Test
194: public void set_failure() {
195: Binding inner = mockBinding();
196: Location l = mockLocation();
197: Object parameter = new Object();
198:
199: inner.set(parameter);
200: getMocksControl().andThrow(_exception);
201:
202: replay();
203:
204: InheritedBinding binding = new InheritedBinding(
205: BINDING_DESCRIPTION, inner, l);
206:
207: try {
208: binding.set(parameter);
209: unreachable();
210: } catch (TapestryException ex) {
211: checkException(ex, l);
212: }
213:
214: verify();
215: }
216:
217: @Test
218: public void get_annotation_success() {
219: Binding inner = mockBinding();
220: Location l = mockLocation();
221: Inject inject = newMock(Inject.class);
222:
223: train_getAnnotation(inner, Inject.class, inject);
224:
225: replay();
226:
227: InheritedBinding binding = new InheritedBinding(
228: BINDING_DESCRIPTION, inner, l);
229:
230: assertSame(binding.getAnnotation(Inject.class), inject);
231:
232: verify();
233: }
234:
235: @Test
236: public void get_annotation_failure() {
237: Binding inner = mockBinding();
238: Location l = mockLocation();
239:
240: expect(inner.getAnnotation(Inject.class)).andThrow(_exception);
241:
242: replay();
243:
244: InheritedBinding binding = new InheritedBinding(
245: BINDING_DESCRIPTION, inner, l);
246:
247: try {
248: binding.getAnnotation(Inject.class);
249: unreachable();
250: } catch (TapestryException ex) {
251: checkException(ex, l);
252: }
253:
254: verify();
255: }
256: }
|