View Javadoc

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