View Javadoc

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