author | Jaroslav Tulach <jtulach@netbeans.org> |
Tue, 26 May 2009 09:01:40 +0200 | |
changeset 335 | 3a98792518f0 |
parent 334 | samples/privilegedcreator/test/api/MutexTest.java@0f3e13581901 |
permissions | -rw-r--r-- |
1 package org.apidesign.privileged.use;
3 import org.apidesign.privileged.api.Mutex;
4 import org.junit.Test;
5 import static org.junit.Assert.*;
7 public class MutexTest {
8 // BEGIN: mutex.init
9 private static final Mutex.Privileged PRIVILEGED = new Mutex.Privileged();
10 public static final Mutex MUTEX = new Mutex(PRIVILEGED);
11 // END: mutex.init
13 public MutexTest() {
14 }
16 /**
17 * Test of withLock method, of class Mutex.
18 */
19 @Test
20 public void readAccess() {
21 // BEGIN: mutex.use
22 class R implements Runnable {
23 int cnt;
25 public void run() {
26 cnt++;
27 }
28 }
29 R r = new R();
30 MUTEX.withLock(r);
31 assertEquals("Counter increased", 1, r.cnt);
32 // END: mutex.use
33 }
35 @Test
36 public void usePrivileged() {
37 int cnt = 0;
38 // BEGIN: mutex.privileged
39 try {
40 PRIVILEGED.lock();
41 // do the operation
42 cnt++;
43 } finally {
44 PRIVILEGED.unlock();
45 }
46 assertEquals("Counter increased", 1, cnt);
47 // END: mutex.privileged
49 }
51 }