# HG changeset patch # User Jaroslav Tulach # Date 1243321300 -7200 # Node ID 3a98792518f09ee2106bdd5c5376236e6f452f1d # Parent 0f3e135819012b350668fea4c47faaf661bf4a17 Applying suggestions by --Jeffrey D Smith 21:48, 25 May 2009 (CEST) From http://wiki.apidesign.org/wiki/Talk:Privileged_API diff -r 0f3e13581901 -r 3a98792518f0 samples/privilegedcreator/src/org/apidesign/privileged/api/Mutex.java --- a/samples/privilegedcreator/src/org/apidesign/privileged/api/Mutex.java Mon May 25 21:49:25 2009 +0200 +++ b/samples/privilegedcreator/src/org/apidesign/privileged/api/Mutex.java Tue May 26 09:01:40 2009 +0200 @@ -19,7 +19,7 @@ public Mutex() { } - public void readAccess(Runnable r) { + public void withLock(Runnable r) { try { lock.lock(); r.run(); @@ -40,11 +40,11 @@ public static final class Privileged { private Mutex mutex; - public void enterReadAccess() { + public void lock() { mutex.lock.lock(); } - public void exitReadAccess() { + public void unlock() { mutex.lock.unlock(); } } diff -r 0f3e13581901 -r 3a98792518f0 samples/privilegedcreator/test/api/MutexTest.java --- a/samples/privilegedcreator/test/api/MutexTest.java Mon May 25 21:49:25 2009 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -package api; - -import org.apidesign.privileged.api.Mutex; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -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 readAccess 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.readAccess(r); - assertEquals("Counter increased", 1, r.cnt); - // END: mutex.use - } - - @Test - public void usePrivileged() { - int cnt = 0; - // BEGIN: mutex.privileged - PRIVILEGED.enterReadAccess(); - try { - // do the operation - cnt++; - } finally { - PRIVILEGED.exitReadAccess(); - } - assertEquals("Counter increased", 1, cnt); - // END: mutex.privileged - - } - -} diff -r 0f3e13581901 -r 3a98792518f0 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 Tue May 26 09:01:40 2009 +0200 @@ -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 + + } + +}