View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.ThreadSafeAccess;
11  import org.mule.model.seda.SedaService;
12  import org.mule.session.DefaultMuleSession;
13  import org.mule.tck.MuleTestUtils;
14  import org.mule.tck.junit4.AbstractMuleContextTestCase;
15  
16  import java.util.Map;
17  
18  import static org.junit.Assert.assertEquals;
19  
20  public abstract class AbstractThreadSafeAccessTestCase extends AbstractMuleContextTestCase
21  {
22      protected ThreadSafeAccess dummyEvent() throws Exception
23      {
24          MuleMessage message = new DefaultMuleMessage(new Object(), (Map) null, muleContext);
25          return new DefaultMuleEvent(message,
26                  MuleTestUtils.getTestInboundEndpoint("test", MessageExchangePattern.ONE_WAY, muleContext, null),
27                  new DefaultMuleSession(new SedaService(muleContext), muleContext));
28      }
29  
30      protected void resetAccessControl(ThreadSafeAccess target) throws InterruptedException
31      {
32          target.assertAccess(true);
33          newThread(target, true, new boolean[]{true});
34          target.resetAccessControl();
35          newThread(target, false, new boolean[]{true});
36      }
37  
38      protected void basicPattern(ThreadSafeAccess target) throws InterruptedException
39      {
40          newThread(target, false, new boolean[]{true, true, false, true});
41          newThread(target, false, new boolean[]{false});
42          newThread(target, true, new boolean[]{true});
43      }
44  
45      protected void newCopy(ThreadSafeAccess target) throws InterruptedException
46      {
47          basicPattern(target);
48          basicPattern(target.newThreadCopy());
49      }
50  
51      protected void newThread(ThreadSafeAccess target, boolean error, boolean[] pattern) throws InterruptedException
52      {
53          Caller caller = new Caller(target, pattern);
54          Thread thread =  new Thread(caller);
55          thread.start();
56          thread.join();
57          assertEquals(error, caller.isError());
58      }
59  
60      protected static class Caller implements Runnable
61      {
62  
63          private boolean isError = false;
64          private ThreadSafeAccess target;
65          private boolean[] write;
66  
67          public Caller(ThreadSafeAccess target, boolean[] write)
68          {
69              this.target = target;
70              this.write = write;
71          }
72  
73          public void run()
74          {
75              try
76              {
77                  for (int i = 0; i < write.length; i++)
78                  {
79                      target.assertAccess(write[i]);
80                  }
81              }
82              catch (IllegalStateException e)
83              {
84                  isError = true;
85              }
86          }
87  
88          public boolean isError()
89          {
90              return isError;
91          }
92      }
93  }