View Javadoc

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