diff -r 0f3e13581901 -r 40cabcdcd2be samples/privilegedcreator/test/org/apidesign/privileged/use/MutexTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/privilegedcreator/test/org/apidesign/privileged/use/MutexTest.java Thu Oct 30 21:30:10 2014 +0100 @@ -0,0 +1,51 @@ +package org.apidesign.privileged.use; + +import org.apidesign.privileged.api.Mutex; +import org.junit.Test; +import static org.junit.Assert.*; + +public class MutexTest { + // BEGIN: mutex.init + private static final Mutex.Privileged PRIVILEGED = new Mutex.Privileged(); + public static final Mutex MUTEX = new Mutex(PRIVILEGED); + // END: mutex.init + + public MutexTest() { + } + + /** + * Test of withLock method, of class Mutex. + */ + @Test + public void readAccess() { + // BEGIN: mutex.use + class R implements Runnable { + int cnt; + + public void run() { + cnt++; + } + } + R r = new R(); + MUTEX.withLock(r); + assertEquals("Counter increased", 1, r.cnt); + // END: mutex.use + } + + @Test + public void usePrivileged() { + int cnt = 0; + // BEGIN: mutex.privileged + try { + PRIVILEGED.lock(); + // do the operation + cnt++; + } finally { + PRIVILEGED.unlock(); + } + assertEquals("Counter increased", 1, cnt); + // END: mutex.privileged + + } + +}