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