001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.phase;
019:
020: import java.lang.reflect.Field;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.Set;
024: import java.util.SortedSet;
025: import java.util.TreeSet;
026:
027: import org.apache.cxf.common.util.SortedArraySet;
028: import org.apache.cxf.interceptor.Interceptor;
029: import org.apache.cxf.message.Message;
030: import org.easymock.classextension.EasyMock;
031: import org.easymock.classextension.IMocksControl;
032: import org.junit.After;
033: import org.junit.Assert;
034: import org.junit.Before;
035: import org.junit.Test;
036:
037: public class PhaseInterceptorChainTest extends Assert {
038:
039: private IMocksControl control;
040:
041: private PhaseInterceptorChain chain;
042:
043: private Message message;
044:
045: @Before
046: public void setUp() {
047:
048: control = EasyMock.createNiceControl();
049: message = control.createMock(Message.class);
050:
051: Phase phase1 = new Phase("phase1", 1);
052: Phase phase2 = new Phase("phase2", 2);
053: Phase phase3 = new Phase("phase3", 3);
054: SortedSet<Phase> phases = new TreeSet<Phase>();
055: phases.add(phase1);
056: phases.add(phase2);
057: phases.add(phase3);
058:
059: chain = new PhaseInterceptorChain(phases);
060: }
061:
062: @After
063: public void tearDown() {
064: control.verify();
065: }
066:
067: @Test
068: public void testAddOneInterceptor() throws Exception {
069: AbstractPhaseInterceptor p = setUpPhaseInterceptor("phase1",
070: "p1");
071: control.replay();
072: chain.add(p);
073: Iterator<Interceptor<? extends Message>> it = chain.iterator();
074: assertSame(p, it.next());
075: assertTrue(!it.hasNext());
076: }
077:
078: @Test
079: public void testForceAddSameInterceptor() throws Exception {
080:
081: AbstractPhaseInterceptor p = setUpPhaseInterceptor("phase1",
082: "p1");
083: control.replay();
084: chain.add(p, false);
085: chain.add(p, false);
086: Iterator<Interceptor<? extends Message>> it = chain.iterator();
087: assertSame(p, it.next());
088: assertTrue(!it.hasNext());
089: chain.add(p, true);
090: it = chain.iterator();
091: assertSame(p, it.next());
092: assertSame(p, it.next());
093: assertTrue(!it.hasNext());
094: }
095:
096: @Test
097: public void testForceAddSameInterceptorType() throws Exception {
098:
099: AbstractPhaseInterceptor p1 = setUpPhaseInterceptor("phase1",
100: "p1");
101: AbstractPhaseInterceptor p2 = setUpPhaseInterceptor("phase1",
102: "p1");
103: control.replay();
104: chain.add(p1, false);
105: chain.add(p2, false);
106: Iterator<Interceptor<? extends Message>> it = chain.iterator();
107: assertSame(p1, it.next());
108: assertTrue(!it.hasNext());
109: chain.add(p2, true);
110: it = chain.iterator();
111: assertSame(p1, it.next());
112: assertSame(p2, it.next());
113: assertTrue(!it.hasNext());
114: }
115:
116: @Test
117: public void testAddTwoInterceptorsSamePhase() throws Exception {
118: AbstractPhaseInterceptor p1 = setUpPhaseInterceptor("phase1",
119: "p1");
120: Set<String> after = new HashSet<String>();
121: after.add("p1");
122: AbstractPhaseInterceptor p2 = setUpPhaseInterceptor("phase1",
123: "p2", null, after);
124: control.replay();
125: chain.add(p1);
126: chain.add(p2);
127: Iterator<Interceptor<? extends Message>> it = chain.iterator();
128:
129: assertSame("Unexpected interceptor at this position.", p1, it
130: .next());
131: assertSame("Unexpected interceptor at this position.", p2, it
132: .next());
133: assertTrue(!it.hasNext());
134: }
135:
136: @Test
137: public void testThreeInterceptorSamePhaseWithOrder()
138: throws Exception {
139: AbstractPhaseInterceptor p1 = setUpPhaseInterceptor("phase1",
140: "p1");
141: Set<String> before = new HashSet<String>();
142: before.add("p1");
143: AbstractPhaseInterceptor p2 = setUpPhaseInterceptor("phase1",
144: "p2", before, null);
145: Set<String> before1 = new HashSet<String>();
146: before1.add("p2");
147: AbstractPhaseInterceptor p3 = setUpPhaseInterceptor("phase1",
148: "p3", before1, null);
149: control.replay();
150: chain.add(p3);
151: chain.add(p1);
152: chain.add(p2);
153:
154: Iterator<Interceptor<? extends Message>> it = chain.iterator();
155: assertSame("Unexpected interceptor at this position.", p3, it
156: .next());
157: assertSame("Unexpected interceptor at this position.", p2, it
158: .next());
159: assertSame("Unexpected interceptor at this position.", p1, it
160: .next());
161: assertTrue(!it.hasNext());
162: }
163:
164: @Test
165: public void testSingleInterceptorPass() throws Exception {
166: AbstractPhaseInterceptor p = setUpPhaseInterceptor("phase1",
167: "p1");
168: setUpPhaseInterceptorInvocations(p, false, false);
169: control.replay();
170: chain.add(p);
171: chain.doIntercept(message);
172: }
173:
174: @Test
175: public void testSingleInterceptorFail() throws Exception {
176: AbstractPhaseInterceptor p = setUpPhaseInterceptor("phase1",
177: "p1");
178: setUpPhaseInterceptorInvocations(p, true, true);
179: control.replay();
180: chain.add(p);
181: chain.doIntercept(message);
182: }
183:
184: @Test
185: public void testTwoInterceptorsInSamePhasePass() throws Exception {
186: AbstractPhaseInterceptor p1 = setUpPhaseInterceptor("phase1",
187: "p1");
188: setUpPhaseInterceptorInvocations(p1, false, false);
189: AbstractPhaseInterceptor p2 = setUpPhaseInterceptor("phase1",
190: "p2");
191: setUpPhaseInterceptorInvocations(p2, false, false);
192: control.replay();
193: chain.add(p2);
194: chain.add(p1);
195: chain.doIntercept(message);
196: }
197:
198: @Test
199: public void testThreeInterceptorsInSamePhaseSecondFail()
200: throws Exception {
201: AbstractPhaseInterceptor p1 = setUpPhaseInterceptor("phase1",
202: "p1");
203: setUpPhaseInterceptorInvocations(p1, false, true);
204: AbstractPhaseInterceptor p2 = setUpPhaseInterceptor("phase1",
205: "p2");
206: setUpPhaseInterceptorInvocations(p2, true, true);
207: AbstractPhaseInterceptor p3 = setUpPhaseInterceptor("phase1",
208: "p3");
209: control.replay();
210: chain.add(p1);
211: chain.add(p2);
212: chain.add(p3);
213: chain.doIntercept(message);
214: }
215:
216: @Test
217: public void testTwoInterceptorsInSamePhaseSecondFail()
218: throws Exception {
219: AbstractPhaseInterceptor p1 = setUpPhaseInterceptor("phase1",
220: "p1");
221: setUpPhaseInterceptorInvocations(p1, false, true);
222: AbstractPhaseInterceptor p2 = setUpPhaseInterceptor("phase1",
223: "p2");
224: setUpPhaseInterceptorInvocations(p2, true, true);
225: control.replay();
226: chain.add(p1);
227: chain.add(p2);
228: chain.doIntercept(message);
229: }
230:
231: @Test
232: public void testTwoInterceptorsInDifferentPhasesPass()
233: throws Exception {
234: AbstractPhaseInterceptor p1 = setUpPhaseInterceptor("phase1",
235: "p1");
236: setUpPhaseInterceptorInvocations(p1, false, false);
237: AbstractPhaseInterceptor p2 = setUpPhaseInterceptor("phase2",
238: "p2");
239: setUpPhaseInterceptorInvocations(p2, false, false);
240: control.replay();
241: chain.add(p1);
242: chain.add(p2);
243: chain.doIntercept(message);
244: }
245:
246: @Test
247: public void testTwoInterceptorsInDifferentPhasesSecondFail()
248: throws Exception {
249: AbstractPhaseInterceptor p1 = setUpPhaseInterceptor("phase1",
250: "p1");
251: setUpPhaseInterceptorInvocations(p1, false, true);
252: AbstractPhaseInterceptor p2 = setUpPhaseInterceptor("phase2",
253: "p2");
254: setUpPhaseInterceptorInvocations(p2, true, true);
255: control.replay();
256: chain.add(p1);
257: chain.add(p2);
258: chain.doIntercept(message);
259: }
260:
261: @Test
262: public void testInsertionInDifferentPhasePass() throws Exception {
263:
264: AbstractPhaseInterceptor p2 = setUpPhaseInterceptor("phase2",
265: "p2");
266: setUpPhaseInterceptorInvocations(p2, false, false);
267: AbstractPhaseInterceptor p3 = setUpPhaseInterceptor("phase3",
268: "p3");
269: setUpPhaseInterceptorInvocations(p3, false, false);
270: InsertingPhaseInterceptor p1 = new InsertingPhaseInterceptor(
271: chain, p2, "phase1", "p1");
272: control.replay();
273: chain.add(p3);
274: chain.add(p1);
275: chain.doIntercept(message);
276: assertEquals(1, p1.invoked);
277: assertEquals(0, p1.faultInvoked);
278: }
279:
280: @Test
281: public void testInsertionInSamePhasePass() throws Exception {
282:
283: AbstractPhaseInterceptor p2 = setUpPhaseInterceptor("phase1",
284: "p2");
285: setUpPhaseInterceptorInvocations(p2, false, false);
286: Set<String> after3 = new HashSet<String>();
287: after3.add("p2");
288: AbstractPhaseInterceptor p3 = setUpPhaseInterceptor("phase1",
289: "p3", null, after3);
290: setUpPhaseInterceptorInvocations(p3, false, false);
291: InsertingPhaseInterceptor p1 = new InsertingPhaseInterceptor(
292: chain, p3, "phase1", "p1");
293: p1.addBefore("p2");
294: control.replay();
295: chain.add(p1);
296: chain.add(p2);
297: chain.doIntercept(message);
298: assertEquals(1, p1.invoked);
299: assertEquals(0, p1.faultInvoked);
300: }
301:
302: @Test
303: public void testWrappedInvocation() throws Exception {
304: CountingPhaseInterceptor p1 = new CountingPhaseInterceptor(
305: "phase1", "p1");
306: WrapperingPhaseInterceptor p2 = new WrapperingPhaseInterceptor(
307: "phase2", "p2");
308: CountingPhaseInterceptor p3 = new CountingPhaseInterceptor(
309: "phase3", "p3");
310:
311: message.getInterceptorChain();
312: EasyMock.expectLastCall().andReturn(chain).anyTimes();
313:
314: control.replay();
315: chain.add(p1);
316: chain.add(p2);
317: chain.add(p3);
318: chain.doIntercept(message);
319: assertEquals(1, p1.invoked);
320: assertEquals(1, p2.invoked);
321: assertEquals(1, p3.invoked);
322: }
323:
324: @Test
325: public void testChainInvocationStartFromSpecifiedInterceptor()
326: throws Exception {
327: CountingPhaseInterceptor p1 = new CountingPhaseInterceptor(
328: "phase1", "p1");
329: CountingPhaseInterceptor p2 = new CountingPhaseInterceptor(
330: "phase2", "p2");
331: CountingPhaseInterceptor p3 = new CountingPhaseInterceptor(
332: "phase3", "p3");
333:
334: message.getInterceptorChain();
335: EasyMock.expectLastCall().andReturn(chain).anyTimes();
336:
337: control.replay();
338: chain.add(p1);
339: chain.add(p2);
340: chain.add(p3);
341: chain.doInterceptStartingAfter(message, p2.getId());
342: assertEquals(0, p1.invoked);
343: assertEquals(0, p2.invoked);
344: assertEquals(1, p3.invoked);
345: }
346:
347: AbstractPhaseInterceptor setUpPhaseInterceptor(String phase,
348: String id) throws Exception {
349: return setUpPhaseInterceptor(phase, id, null, null);
350: }
351:
352: @SuppressWarnings("unchecked")
353: AbstractPhaseInterceptor setUpPhaseInterceptor(final String phase,
354: final String id, Set<String> b, Set<String> a)
355: throws Exception {
356:
357: AbstractPhaseInterceptor p = control
358: .createMock(AbstractPhaseInterceptor.class);
359:
360: if (a == null) {
361: a = new SortedArraySet<String>();
362: }
363: if (b == null) {
364: b = new SortedArraySet<String>();
365: }
366: Field f = AbstractPhaseInterceptor.class
367: .getDeclaredField("before");
368: f.setAccessible(true);
369: f.set(p, b);
370:
371: f = AbstractPhaseInterceptor.class.getDeclaredField("after");
372: f.setAccessible(true);
373: f.set(p, a);
374:
375: f = AbstractPhaseInterceptor.class.getDeclaredField("phase");
376: f.setAccessible(true);
377: f.set(p, phase);
378:
379: f = AbstractPhaseInterceptor.class.getDeclaredField("id");
380: f.setAccessible(true);
381: f.set(p, id);
382:
383: return p;
384: }
385:
386: @SuppressWarnings("unchecked")
387: void setUpPhaseInterceptorInvocations(AbstractPhaseInterceptor p,
388: boolean fail, boolean expectFault) {
389: p.handleMessage(message);
390: if (fail) {
391: EasyMock.expectLastCall().andThrow(new RuntimeException());
392: message.setContent(EasyMock.isA(Class.class), EasyMock
393: .isA(Exception.class));
394: EasyMock.expectLastCall();
395: } else {
396: EasyMock.expectLastCall();
397: }
398: if (expectFault) {
399: p.handleFault(message);
400: EasyMock.expectLastCall();
401: }
402: }
403:
404: public class InsertingPhaseInterceptor extends
405: AbstractPhaseInterceptor<Message> {
406: int invoked;
407:
408: int faultInvoked;
409:
410: private final PhaseInterceptorChain insertionChain;
411:
412: private final AbstractPhaseInterceptor insertionInterceptor;
413:
414: public InsertingPhaseInterceptor(PhaseInterceptorChain c,
415: AbstractPhaseInterceptor i, String phase, String id) {
416: super (id, phase);
417: insertionChain = c;
418: insertionInterceptor = i;
419: }
420:
421: public void handleMessage(Message m) {
422: insertionChain.add(insertionInterceptor);
423: invoked++;
424: }
425:
426: public void handleFault(Message m) {
427: faultInvoked++;
428: }
429: }
430:
431: public class CountingPhaseInterceptor extends
432: AbstractPhaseInterceptor<Message> {
433: int invoked;
434:
435: public CountingPhaseInterceptor(String phase, String id) {
436: super (id, phase);
437: }
438:
439: public void handleMessage(Message m) {
440: invoked++;
441: }
442: }
443:
444: public class WrapperingPhaseInterceptor extends
445: CountingPhaseInterceptor {
446: public WrapperingPhaseInterceptor(String phase, String id) {
447: super (phase, id);
448: }
449:
450: public void handleMessage(Message m) {
451: super.handleMessage(m);
452: m.getInterceptorChain().doIntercept(m);
453: }
454: }
455:
456: }
|