| author | Jaroslav Tulach <jtulach@netbeans.org> |
| Sat Feb 20 18:20:10 2010 +0100 | |
| changeset 347 | 481fbbbcc7b7 |
| parent 334 | 0f3e13581901 |
| permissions | -rw-r--r-- |
| jtulach@335 | 1 |
package org.apidesign.privileged.use; |
| jtulach@71 | 2 |
|
| jtulach@330 | 3 |
import org.apidesign.privileged.api.Mutex; |
| jtulach@71 | 4 |
import org.junit.Test; |
| jtulach@71 | 5 |
import static org.junit.Assert.*; |
| jtulach@71 | 6 |
|
| jtulach@71 | 7 |
public class MutexTest {
|
| jtulach@71 | 8 |
// BEGIN: mutex.init |
| jtulach@71 | 9 |
private static final Mutex.Privileged PRIVILEGED = new Mutex.Privileged(); |
| jtulach@71 | 10 |
public static final Mutex MUTEX = new Mutex(PRIVILEGED); |
| jtulach@71 | 11 |
// END: mutex.init |
| jtulach@71 | 12 |
|
| jtulach@71 | 13 |
public MutexTest() {
|
| jtulach@71 | 14 |
} |
| jtulach@71 | 15 |
|
| jtulach@71 | 16 |
/** |
| jtulach@335 | 17 |
* Test of withLock method, of class Mutex. |
| jtulach@71 | 18 |
*/ |
| jtulach@71 | 19 |
@Test |
| jtulach@71 | 20 |
public void readAccess() {
|
| jtulach@331 | 21 |
// BEGIN: mutex.use |
| jtulach@71 | 22 |
class R implements Runnable {
|
| jtulach@71 | 23 |
int cnt; |
| jtulach@71 | 24 |
|
| jtulach@71 | 25 |
public void run() {
|
| jtulach@71 | 26 |
cnt++; |
| jtulach@71 | 27 |
} |
| jtulach@71 | 28 |
} |
| jtulach@71 | 29 |
R r = new R(); |
| jtulach@335 | 30 |
MUTEX.withLock(r); |
| jtulach@332 | 31 |
assertEquals("Counter increased", 1, r.cnt);
|
| jtulach@331 | 32 |
// END: mutex.use |
| jtulach@71 | 33 |
} |
| jtulach@71 | 34 |
|
| jtulach@71 | 35 |
@Test |
| jtulach@71 | 36 |
public void usePrivileged() {
|
| jtulach@332 | 37 |
int cnt = 0; |
| jtulach@71 | 38 |
// BEGIN: mutex.privileged |
| jtulach@71 | 39 |
try {
|
| jtulach@335 | 40 |
PRIVILEGED.lock(); |
| jtulach@335 | 41 |
// do the operation |
| jtulach@332 | 42 |
cnt++; |
| jtulach@71 | 43 |
} finally {
|
| jtulach@335 | 44 |
PRIVILEGED.unlock(); |
| jtulach@71 | 45 |
} |
| jtulach@332 | 46 |
assertEquals("Counter increased", 1, cnt);
|
| jtulach@71 | 47 |
// END: mutex.privileged |
| jtulach@71 | 48 |
|
| jtulach@71 | 49 |
} |
| jtulach@71 | 50 |
|
| jaroslav@334 | 51 |
} |