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.internal.services;
016:
017: import org.apache.tapestry.ComponentEventHandler;
018: import org.apache.tapestry.internal.test.InternalBaseTestCase;
019: import org.apache.tapestry.ioc.services.TypeCoercer;
020: import org.apache.tapestry.runtime.Component;
021: import org.apache.tapestry.runtime.ComponentEvent;
022: import org.testng.annotations.AfterClass;
023: import org.testng.annotations.BeforeClass;
024: import org.testng.annotations.Test;
025:
026: public class ComponentEventImplTest extends InternalBaseTestCase {
027: private TypeCoercer _coercer;
028:
029: @BeforeClass
030: public void setup_coercer() {
031: _coercer = getObject(TypeCoercer.class, null);
032: }
033:
034: @AfterClass
035: public void cleanup_coercer() {
036: _coercer = null;
037: }
038:
039: @Test
040: public void matches_on_event_type() {
041: ComponentEventHandler handler = mockComponentEventHandler();
042:
043: replay();
044:
045: ComponentEvent event = new ComponentEventImpl("eventType",
046: "someId", null, handler, _coercer);
047:
048: assertTrue(event.matchesByEventType("eventType"));
049: assertFalse(event.matchesByEventType("foo"));
050:
051: verify();
052: }
053:
054: @Test
055: public void event_type_match_is_case_insensitive() {
056: ComponentEventHandler handler = mockComponentEventHandler();
057:
058: replay();
059:
060: ComponentEvent event = new ComponentEventImpl("eventType",
061: "someId", null, handler, _coercer);
062:
063: assertTrue(event.matchesByEventType("EVENTTYPE"));
064:
065: verify();
066: }
067:
068: @Test
069: public void matches_on_component_id() {
070: ComponentEventHandler handler = mockComponentEventHandler();
071:
072: replay();
073:
074: ComponentEvent event = new ComponentEventImpl("eventType",
075: "someId", null, handler, _coercer);
076:
077: assertTrue(event.matchesByComponentId("someId"));
078:
079: assertFalse(event.matchesByComponentId("bar"));
080:
081: verify();
082: }
083:
084: @Test
085: public void component_id_matches_are_case_insensitive() {
086: ComponentEventHandler handler = mockComponentEventHandler();
087:
088: replay();
089: ComponentEvent event = new ComponentEventImpl("eventType",
090: "someId", null, handler, _coercer);
091:
092: assertTrue(event.matchesByComponentId("SOMEID"));
093:
094: verify();
095: }
096:
097: @Test
098: public void coerce_context() {
099: ComponentEventHandler handler = mockComponentEventHandler();
100:
101: replay();
102:
103: ComponentEvent event = new ComponentEventImpl("eventType",
104: "someId", new String[] { "27" }, handler, _coercer);
105:
106: assertEquals(event.coerceContext(0, "java.lang.Integer"),
107: new Integer(27));
108:
109: verify();
110: }
111:
112: @Test
113: public void coerce_when_not_enough_context() {
114: ComponentEventHandler handler = mockComponentEventHandler();
115: Component component = mockComponent();
116:
117: replay();
118:
119: ComponentEvent event = new ComponentEventImpl("eventType",
120: "someId", new String[] { "27" }, handler, _coercer);
121:
122: event.setSource(component, "foo.Bar.baz()");
123:
124: try {
125: event.coerceContext(1, "java.lang.Integer");
126: } catch (IllegalArgumentException ex) {
127: assertEquals(
128: ex.getMessage(),
129: "Method foo.Bar.baz() has more parameters than there are context values for this component event.");
130: }
131:
132: verify();
133: }
134:
135: @Test
136: public void unable_to_coerce() {
137: ComponentEventHandler handler = mockComponentEventHandler();
138: Component component = mockComponent();
139:
140: replay();
141:
142: ComponentEvent event = new ComponentEventImpl("eventType",
143: "someId", new String[] { "abc" }, handler, _coercer);
144:
145: event.setSource(component, "foo.Bar.baz()");
146:
147: try {
148: event.coerceContext(0, "java.lang.Integer");
149: unreachable();
150: } catch (IllegalArgumentException ex) {
151: // Different JVMs will report the conversion error slightly differently,
152: // so we don't try to check that part of the error message.
153:
154: assertTrue(ex.getMessage().startsWith(
155: "Exception in method foo.Bar.baz(), parameter #1:"));
156: }
157:
158: verify();
159: }
160:
161: @Test
162: public void store_result_and_abort() {
163: Object result = new Object();
164: String methodDescription = "foo.Bar.baz()";
165: Component component = mockComponent();
166:
167: ComponentEventHandler handler = mockComponentEventHandler();
168:
169: train_handleResult(handler, result, component,
170: methodDescription, true);
171:
172: replay();
173:
174: ComponentEvent event = new ComponentEventImpl("eventType",
175: "someId", null, handler, _coercer);
176:
177: event.setSource(component, methodDescription);
178:
179: assertFalse(event.isAborted());
180:
181: assertTrue(event.storeResult(result));
182:
183: assertTrue(event.isAborted());
184:
185: verify();
186: }
187:
188: @Test
189: public void store_result_and_continue() {
190: Object result = new Object();
191: String methodDescription = "foo.Bar.baz()";
192: Component component = mockComponent();
193: ComponentEventHandler handler = mockComponentEventHandler();
194:
195: train_handleResult(handler, result, component,
196: methodDescription, false);
197:
198: replay();
199:
200: ComponentEvent event = new ComponentEventImpl("eventType",
201: "someId", null, handler, _coercer);
202:
203: event.setSource(component, methodDescription);
204:
205: assertFalse(event.storeResult(result));
206:
207: assertFalse(event.isAborted());
208:
209: verify();
210: }
211:
212: @Test
213: public void store_null_result_does_not_abort_or_invoke_handler() {
214: Component component = mockComponent();
215: ComponentEventHandler handler = mockComponentEventHandler();
216:
217: replay();
218:
219: ComponentEvent event = new ComponentEventImpl("eventType",
220: "someId", null, handler, _coercer);
221:
222: event.setSource(component, "foo.Bar.baz()");
223:
224: assertFalse(event.storeResult(null));
225:
226: assertFalse(event.isAborted());
227:
228: verify();
229: }
230:
231: @SuppressWarnings("unchecked")
232: @Test
233: public void store_result_when_aborted_is_failure() {
234: Object result = new Object();
235: ComponentEventHandler handler = mockComponentEventHandler();
236: Component component = mockComponent();
237:
238: expect(handler.handleResult(result, component, "foo.Bar.baz()"))
239: .andReturn(true);
240:
241: replay();
242:
243: ComponentEvent event = new ComponentEventImpl("eventType",
244: "someId", null, handler, _coercer);
245:
246: event.setSource(component, "foo.Bar.baz()");
247: event.storeResult(result);
248:
249: try {
250: event.setSource(component, "foo.Bar.biff()");
251: event.storeResult(null);
252: unreachable();
253: } catch (IllegalStateException ex) {
254: assertEquals(ex.getMessage(), ServicesMessages
255: .componentEventIsAborted("foo.Bar.biff()"));
256: }
257:
258: verify();
259: }
260: }
|