1   /*
2    * $Id: InterceptorStackTestCase.java 7976 2007-08-21 14:26:13Z 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  /**
29   * TODO: document this class
30   * 
31   * @author <a href="mailto:gnt@codehaus.org">Guillaume Nodet</a>
32   * @version $Revision: 7976 $
33   */
34  public class InterceptorStackTestCase extends AbstractMuleTestCase
35  {
36  
37      public static class DummyInvocation extends Invocation
38      {
39          public DummyInvocation(UMODescriptor d, UMOMessage msg)
40          {
41              super(d, msg, null);
42          }
43  
44          public UMOMessage execute() throws UMOException
45          {
46              return getMessage();
47          }
48      }
49  
50      public void testStack() throws Exception
51      {
52          final AtomicInteger c = new AtomicInteger(0);
53          final UMOMessage m1 = (UMOMessage)new Mock(UMOMessage.class).proxy();
54          final UMOMessage m2 = (UMOMessage)new Mock(UMOMessage.class).proxy();
55          final UMOMessage m3 = (UMOMessage)new Mock(UMOMessage.class).proxy();
56          final UMOMessage m4 = (UMOMessage)new Mock(UMOMessage.class).proxy();
57          final UMOMessage m5 = (UMOMessage)new Mock(UMOMessage.class).proxy();
58          final UMODescriptor d = (UMODescriptor)new Mock(UMODescriptor.class).proxy();
59  
60          InterceptorStack s = new InterceptorStack();
61          List interceptors = new ArrayList();
62          interceptors.add(new UMOInterceptor()
63          {
64              public UMOMessage intercept(Invocation invocation) throws UMOException
65              {
66                  assertEquals(0, c.get());
67                  c.incrementAndGet();
68                  assertTrue(m1 == invocation.getMessage());
69                  invocation.setMessage(m2);
70                  UMOMessage msg = invocation.execute();
71                  assertEquals(3, c.get());
72                  c.incrementAndGet();
73                  assertTrue(m4 == msg);
74                  assertTrue(d == invocation.getDescriptor());
75                  return m5;
76              }
77          });
78          interceptors.add(new UMOInterceptor()
79          {
80              public UMOMessage intercept(Invocation invocation) throws UMOException
81              {
82                  assertEquals(1, c.get());
83                  c.incrementAndGet();
84                  assertTrue(m2 == invocation.getMessage());
85                  invocation.setMessage(m3);
86                  UMOMessage msg = invocation.execute();
87                  assertEquals(2, c.get());
88                  c.incrementAndGet();
89                  assertTrue(m3 == msg);
90                  assertTrue(d == invocation.getDescriptor());
91                  return m4;
92              }
93          });
94          s.setInterceptors(interceptors);
95  
96          UMOMessage r = s.intercept(new DummyInvocation(d, m1));
97          assertTrue(r == m5);
98          assertEquals(4, c.get());
99      }
100 
101 }