KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > util > SemaphoreTest


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.util;
17
18 import junit.framework.TestCase;
19
20 import org.columba.core.base.Semaphore;
21 import org.columba.core.base.StopWatch;
22
23 /**
24  * @author redsolo
25  */

26 public class SemaphoreTest extends TestCase {
27
28     /**
29      * Tests the constructors.
30      */

31     public void testConstructors() {
32         Semaphore semaphore = new Semaphore();
33         assertTrue("The semaphore is not holding.", semaphore.isHolding());
34
35         semaphore.hold();
36         assertTrue("The semaphore is not holding.", semaphore.isHolding());
37
38         semaphore.release();
39         assertTrue("The semaphore has not been released", !semaphore.isHolding());
40
41         semaphore = new Semaphore(false);
42         assertTrue("The semaphore is holding.", !semaphore.isHolding());
43
44         semaphore.hold();
45         assertTrue("The semaphore is not holding.", semaphore.isHolding());
46
47         semaphore.release();
48         assertTrue("The semaphore has not been released", !semaphore.isHolding());
49     }
50
51     /**
52      * Test the hold() and release() methods.
53      */

54     public void testHold() {
55         Semaphore semaphore = new Semaphore();
56         assertTrue("The semaphore is not holding.", semaphore.isHolding());
57
58         semaphore.hold();
59         assertTrue("The semaphore is not holding.", semaphore.isHolding());
60
61         semaphore.release();
62         assertTrue("The semaphore has not been released", !semaphore.isHolding());
63
64         semaphore.hold();
65         assertTrue("The semaphore is not holding.", semaphore.isHolding());
66     }
67
68     /**
69      * Test a single thread wait on the semaphore.
70      * @throws InterruptedException thrown for any good reason if the test failed.
71      */

72     public void testSingleWait() throws InterruptedException JavaDoc {
73         Semaphore semaphore = new Semaphore();
74
75         StopWatch timer = new StopWatch();
76         semaphore.waitUntilReleased(50);
77         if (timer.getTiming() < 25) {
78             fail("Single thread did not wait for semaphore.");
79         }
80
81         semaphore.hold();
82         timer.start();
83         semaphore.waitUntilReleased(50);
84         if (timer.getTiming() < 25) {
85             fail("Single thread did not wait for semaphore.");
86         }
87
88         semaphore.release();
89         timer.start();
90         semaphore.waitUntilReleased(100);
91         if (timer.getTiming() > 25) {
92             fail("Single thread did wait for semaphore.");
93         }
94     }
95 }
96
Popular Tags