1   /*
2    * $Id: WaitableBooleanTestCase.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.util.concurrent;
12  
13  import org.mule.tck.AbstractMuleTestCase;
14  
15  public class WaitableBooleanTestCase extends AbstractMuleTestCase
16  {
17      protected final WaitableBoolean TRUE = new WaitableBoolean(true);
18      protected final WaitableBoolean FALSE = new WaitableBoolean(false);
19  
20      public void testCompareToBoolean()
21      {
22          assertEquals(0, TRUE.compareTo(true));
23          assertEquals(1, TRUE.compareTo(false));
24          assertEquals(0, FALSE.compareTo(false));
25          assertEquals(-1, FALSE.compareTo(true));
26      }
27  
28      public void testCompareToWaitableBoolean()
29      {
30          assertEquals(0, TRUE.compareTo(new WaitableBoolean(true)));
31          assertEquals(1, TRUE.compareTo(new WaitableBoolean(false)));
32          assertEquals(0, FALSE.compareTo(new WaitableBoolean(false)));
33          assertEquals(-1, FALSE.compareTo(new WaitableBoolean(true)));
34          assertEquals(0, TRUE.compareTo((Object)TRUE));
35      }
36  
37      public void testCompareToObject()
38      {
39          assertEquals(0, TRUE.compareTo((Object)TRUE));
40      }
41  
42      public void testEquals()
43      {
44          assertTrue(TRUE.equals(TRUE));
45          assertFalse(TRUE.equals(FALSE));
46          assertFalse(FALSE.equals(TRUE));
47          assertTrue(TRUE.equals(new WaitableBoolean(true)));
48          assertTrue(FALSE.equals(new WaitableBoolean(false)));
49          assertFalse(TRUE.equals(":-)"));
50      }
51  
52      public void testHashCode()
53      {
54          assertTrue(TRUE.hashCode() != FALSE.hashCode());
55          assertEquals(TRUE.hashCode(), (new WaitableBoolean(true)).hashCode());
56          assertEquals(FALSE.hashCode(), (new WaitableBoolean(false)).hashCode());
57      }
58  
59      public void testToString()
60      {
61          assertEquals("true", TRUE.toString());
62          assertEquals("false", FALSE.toString());
63      }
64  
65      public void testGet()
66      {
67          assertTrue(TRUE.get());
68          assertFalse(FALSE.get());
69      }
70  
71      public void testSet()
72      {
73          assertTrue(TRUE.set(true));
74          assertTrue(TRUE.set(false));
75          assertFalse(TRUE.set(true));
76          assertFalse(FALSE.set(false));
77          assertFalse(FALSE.set(true));
78          assertTrue(FALSE.set(true));
79      }
80  
81      public void testCommit()
82      {
83          assertTrue(TRUE.compareAndSet(true, true));
84          assertTrue(TRUE.get());
85          assertFalse(TRUE.compareAndSet(false, true));
86          assertTrue(TRUE.compareAndSet(true, false));
87          assertFalse(TRUE.get());
88          assertTrue(TRUE.compareAndSet(false, true));
89          assertTrue(TRUE.get());
90      }
91  
92      public void testComplement()
93      {
94          assertFalse(TRUE.complement());
95          assertFalse(TRUE.get());
96  
97          assertTrue(FALSE.complement());
98          assertTrue(FALSE.get());
99      }
100 
101     public void testAnd()
102     {
103         assertTrue((new WaitableBoolean(true)).and(true));
104         assertFalse((new WaitableBoolean(true)).and(false));
105         assertFalse((new WaitableBoolean(false)).and(false));
106         assertFalse((new WaitableBoolean(false)).and(true));
107 
108         assertTrue(TRUE.and(true));
109         assertTrue(TRUE.get());
110         assertFalse(TRUE.and(false));
111         assertFalse(TRUE.get());
112     }
113 
114     public void testOr()
115     {
116         assertTrue((new WaitableBoolean(true)).or(true));
117         assertTrue((new WaitableBoolean(true)).or(false));
118         assertFalse((new WaitableBoolean(false)).or(false));
119         assertTrue((new WaitableBoolean(false)).or(true));
120 
121         assertTrue(TRUE.or(true));
122         assertTrue(TRUE.get());
123         assertTrue(TRUE.or(false));
124         assertTrue(TRUE.get());
125     }
126 
127     public void testXor()
128     {
129         assertFalse((new WaitableBoolean(true)).xor(true));
130         assertTrue((new WaitableBoolean(true)).xor(false));
131         assertFalse((new WaitableBoolean(false)).xor(false));
132         assertTrue((new WaitableBoolean(false)).xor(true));
133     }
134 
135     public void testWhenFalse() throws InterruptedException
136     {
137         final WaitableBoolean blocker = new WaitableBoolean(true);
138         final WaitableBoolean actionPerformed = new WaitableBoolean(false);
139 
140         Runnable switcher = new Runnable()
141         {
142             public void run()
143             {
144                 try
145                 {
146                     Thread.sleep(500);
147                     blocker.set(false);
148                 }
149                 catch (InterruptedException iex)
150                 {
151                     Thread.currentThread().interrupt();
152                     throw new RuntimeException(iex);
153                 }
154             }
155         };
156 
157         Runnable action = new Runnable()
158         {
159             public void run()
160             {
161                 actionPerformed.set(true);
162             }
163         };
164 
165         new Thread(switcher).start();
166 
167         blocker.whenFalse(action);
168         assertFalse(blocker.get());
169         assertTrue(actionPerformed.get());
170     }
171 
172     public void testWhenFalseAlreadyFalse() throws InterruptedException
173     {
174         final WaitableBoolean blocker = new WaitableBoolean(false);
175         final WaitableBoolean actionPerformed = new WaitableBoolean(false);
176 
177         Runnable action = new Runnable()
178         {
179             public void run()
180             {
181                 actionPerformed.set(true);
182             }
183         };
184 
185         blocker.whenFalse(action);
186         assertFalse(blocker.get());
187         assertTrue(actionPerformed.get());
188     }
189 
190     public void testWhenTrue() throws InterruptedException
191     {
192         final WaitableBoolean blocker = new WaitableBoolean(false);
193         final WaitableBoolean actionPerformed = new WaitableBoolean(false);
194 
195         Runnable switcher = new Runnable()
196         {
197             public void run()
198             {
199                 try
200                 {
201                     Thread.sleep(500);
202                     blocker.set(true);
203                 }
204                 catch (InterruptedException iex)
205                 {
206                     Thread.currentThread().interrupt();
207                     throw new RuntimeException(iex);
208                 }
209             }
210         };
211 
212         Runnable action = new Runnable()
213         {
214             public void run()
215             {
216                 actionPerformed.set(true);
217             }
218         };
219 
220         new Thread(switcher).start();
221         blocker.whenTrue(action);
222         assertTrue(blocker.get());
223         assertTrue(actionPerformed.get());
224     }
225 
226     public void testWhenTrueAlreadyTrue() throws InterruptedException
227     {
228         final WaitableBoolean blocker = new WaitableBoolean(true);
229         final WaitableBoolean actionPerformed = new WaitableBoolean(false);
230 
231         Runnable action = new Runnable()
232         {
233             public void run()
234             {
235                 actionPerformed.set(true);
236             }
237         };
238 
239         blocker.whenTrue(action);
240         assertTrue(blocker.get());
241         assertTrue(actionPerformed.get());
242     }
243 
244     public void testGetLock()
245     {
246         WaitableBoolean b = new WaitableBoolean(true);
247         assertSame(b, b.getLock());
248 
249         b = new WaitableBoolean(true, this);
250         assertSame(this, b.getLock());
251     }
252 
253 }