1   /*
2    * $Id: InterceptorStackTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.mule.interceptors;
12  
13  import org.mule.interceptors.InterceptorStack;
14  import org.mule.tck.AbstractMuleTestCase;
15  import org.mule.umo.Invocation;
16  import org.mule.umo.UMODescriptor;
17  import org.mule.umo.UMOException;
18  import org.mule.umo.UMOInterceptor;
19  import org.mule.umo.UMOMessage;
20  
21  import com.mockobjects.dynamic.Mock;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
27  
28  public class InterceptorStackTestCase extends AbstractMuleTestCase
29  {
30  
31      public static class DummyInvocation extends Invocation
32      {
33          public DummyInvocation(UMODescriptor d, UMOMessage msg)
34          {
35              super(d, msg, null);
36          }
37  
38          public UMOMessage execute() throws UMOException
39          {
40              return getMessage();
41          }
42      }
43  
44      public void testStack() throws Exception
45      {
46          final AtomicInteger c = new AtomicInteger(0);
47          final UMOMessage m1 = (UMOMessage)new Mock(UMOMessage.class).proxy();
48          final UMOMessage m2 = (UMOMessage)new Mock(UMOMessage.class).proxy();
49          final UMOMessage m3 = (UMOMessage)new Mock(UMOMessage.class).proxy();
50          final UMOMessage m4 = (UMOMessage)new Mock(UMOMessage.class).proxy();
51          final UMOMessage m5 = (UMOMessage)new Mock(UMOMessage.class).proxy();
52          final UMODescriptor d = (UMODescriptor)new Mock(UMODescriptor.class).proxy();
53  
54          InterceptorStack s = new InterceptorStack();
55          List interceptors = new ArrayList();
56          interceptors.add(new UMOInterceptor()
57          {
58              public UMOMessage intercept(Invocation invocation) throws UMOException
59              {
60                  assertEquals(0, c.get());
61                  c.incrementAndGet();
62                  assertTrue(m1 == invocation.getMessage());
63                  invocation.setMessage(m2);
64                  UMOMessage msg = invocation.execute();
65                  assertEquals(3, c.get());
66                  c.incrementAndGet();
67                  assertTrue(m4 == msg);
68                  assertTrue(d == invocation.getDescriptor());
69                  return m5;
70              }
71          });
72          interceptors.add(new UMOInterceptor()
73          {
74              public UMOMessage intercept(Invocation invocation) throws UMOException
75              {
76                  assertEquals(1, c.get());
77                  c.incrementAndGet();
78                  assertTrue(m2 == invocation.getMessage());
79                  invocation.setMessage(m3);
80                  UMOMessage msg = invocation.execute();
81                  assertEquals(2, c.get());
82                  c.incrementAndGet();
83                  assertTrue(m3 == msg);
84                  assertTrue(d == invocation.getDescriptor());
85                  return m4;
86              }
87          });
88          s.setInterceptors(interceptors);
89  
90          UMOMessage r = s.intercept(new DummyInvocation(d, m1));
91          assertTrue(r == m5);
92          assertEquals(4, c.get());
93      }
94  
95  }