1   /*
2    * $Id$
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;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.ThreadSafeAccess;
15  import org.mule.model.direct.DirectService;
16  import org.mule.tck.AbstractMuleTestCase;
17  import org.mule.tck.MuleTestUtils;
18  
19  import java.util.Map;
20  
21  public abstract class AbstractThreadSafeAccessTestCase extends AbstractMuleTestCase
22  {
23      protected ThreadSafeAccess dummyEvent() throws Exception
24      {
25          MuleMessage message = new DefaultMuleMessage(new Object(), (Map) null);
26          return new DefaultMuleEvent(message,
27                  MuleTestUtils.getTestInboundEndpoint("test", muleContext),
28                  new DefaultMuleSession(new DirectService(), muleContext), false);
29      }
30  
31      protected void resetAccessControl(ThreadSafeAccess target) throws InterruptedException
32      {
33          target.assertAccess(true);
34          newThread(target, true, new boolean[]{true});
35          target.resetAccessControl();
36          newThread(target, false, new boolean[]{true});
37      }
38  
39      protected void basicPattern(ThreadSafeAccess target) throws InterruptedException
40      {
41          newThread(target, false, new boolean[]{true, true, false, true});
42          newThread(target, false, new boolean[]{false});
43          newThread(target, true, new boolean[]{true});
44      }
45  
46      protected void newCopy(ThreadSafeAccess target) throws InterruptedException
47      {
48          basicPattern(target);
49          basicPattern(target.newThreadCopy());
50      }
51  
52      protected void newThread(ThreadSafeAccess target, boolean error, boolean[] pattern) throws InterruptedException
53      {
54          Caller caller = new Caller(target, pattern);
55          Thread thread =  new Thread(caller);
56          thread.start();
57          thread.join();
58          assertEquals(error, caller.isError());
59      }
60  
61      protected static class Caller implements Runnable
62      {
63  
64          private boolean isError = false;
65          private ThreadSafeAccess target;
66          private boolean[] write;
67  
68          public Caller(ThreadSafeAccess target, boolean[] write)
69          {
70              this.target = target;
71              this.write = write;
72          }
73  
74          public void run()
75          {
76              try
77              {
78                  for (int i = 0; i < write.length; i++)
79                  {
80                      target.assertAccess(write[i]);
81                  }
82              }
83              catch (IllegalStateException e)
84              {
85                  isError = true;
86              }
87          }
88  
89          public boolean isError()
90          {
91              return isError;
92          }
93      }
94  }