1   /*
2    * $Id: ThreadSafeAccessTestCase.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.impl;
12  
13  import org.mule.config.MuleProperties;
14  import org.mule.impl.endpoint.MuleEndpoint;
15  import org.mule.impl.model.direct.DirectComponent;
16  import org.mule.providers.DefaultMessageAdapter;
17  import org.mule.umo.UMOMessage;
18  import org.mule.umo.endpoint.MalformedEndpointException;
19  
20  import java.util.Map;
21  
22  import junit.framework.TestCase;
23  
24  public class ThreadSafeAccessTestCase extends TestCase
25  {
26  
27      protected void setUp() throws Exception
28      {
29          System.setProperty(MuleProperties.MULE_THREAD_UNSAFE_MESSAGES_PROPERTY, "false");
30      }
31  
32      public void testMessage() throws InterruptedException
33      {
34          basicPattern(new MuleMessage(new Object(), (Map)null));
35          newCopy(new MuleMessage(new Object(), (Map)null));
36          resetAccessControl(new MuleMessage(new Object(), (Map)null));
37      }
38  
39      public void testAdapter() throws InterruptedException
40      {
41          basicPattern(new DefaultMessageAdapter(new Object()));
42          newCopy(new DefaultMessageAdapter(new Object()));
43          resetAccessControl(new DefaultMessageAdapter(new Object()));
44      }
45  
46      public void testEvent() throws InterruptedException, MalformedEndpointException
47      {
48          basicPattern(dummyEvent());
49          newCopy(dummyEvent());
50          resetAccessControl(dummyEvent());
51      }
52  
53      public void testDisable() throws InterruptedException
54      {
55          try
56          {
57              System.setProperty(MuleProperties.MULE_THREAD_UNSAFE_MESSAGES_PROPERTY, "true");
58              ThreadSafeAccess target = new DefaultMessageAdapter(new Object());
59              newThread(target, false, new boolean[]{true, true, false, true});
60              newThread(target, false, new boolean[]{false});
61              newThread(target, false, new boolean[]{true});
62          }
63          finally
64          {
65              System.getProperties().remove(MuleProperties.MULE_THREAD_UNSAFE_MESSAGES_PROPERTY);
66          }
67      }
68  
69      protected ThreadSafeAccess dummyEvent()
70      {
71          UMOMessage message = new MuleMessage(new Object(), (Map)null);
72          return new MuleEvent(message, new MuleEndpoint(),
73                  new MuleSession(new DirectComponent(new MuleDescriptor(""), null)), false);
74      }
75  
76      protected void resetAccessControl(ThreadSafeAccess target) throws InterruptedException
77      {
78          target.assertAccess(true);
79          newThread(target, true, new boolean[]{true});
80          target.resetAccessControl();
81          newThread(target, false, new boolean[]{true});
82      }
83  
84      protected void basicPattern(ThreadSafeAccess target) throws InterruptedException
85      {
86          newThread(target, false, new boolean[]{true, true, false, true});
87          newThread(target, false, new boolean[]{false});
88          newThread(target, true, new boolean[]{true});
89      }
90  
91      protected void newCopy(ThreadSafeAccess target) throws InterruptedException
92      {
93          basicPattern(target);
94          basicPattern(target.newThreadCopy());
95      }
96  
97      protected void newThread(ThreadSafeAccess target, boolean error, boolean[] pattern) throws InterruptedException
98      {
99          Caller caller = new Caller(target, pattern);
100         Thread thread =  new Thread(caller);
101         thread.start();
102         thread.join();
103         assertEquals(error, caller.isError());
104     }
105 
106     protected static class Caller implements Runnable
107     {
108 
109         private boolean isError = false;
110         private ThreadSafeAccess target;
111         private boolean[] write;
112 
113         public Caller(ThreadSafeAccess target, boolean[] write)
114         {
115             this.target = target;
116             this.write = write;
117         }
118 
119         public void run()
120         {
121             try
122             {
123                 for (int i = 0; i < write.length; i++)
124                 {
125                     target.assertAccess(write[i]);
126                 }
127             }
128             catch (IllegalStateException e)
129             {
130                 isError = true;
131             }
132         }
133 
134         public boolean isError()
135         {
136             return isError;
137         }
138 
139     }
140 
141 }