samples/privilegedcreator/test/api/MutexTest.java
changeset 71 a9bd40166b60
child 330 be82436a7a8b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/privilegedcreator/test/api/MutexTest.java	Sat Jun 14 09:53:28 2008 +0200
     1.3 @@ -0,0 +1,69 @@
     1.4 +package api;
     1.5 +
     1.6 +import org.junit.After;
     1.7 +import org.junit.AfterClass;
     1.8 +import org.junit.Before;
     1.9 +import org.junit.BeforeClass;
    1.10 +import org.junit.Test;
    1.11 +import static org.junit.Assert.*;
    1.12 +
    1.13 +public class MutexTest {
    1.14 +    // BEGIN: mutex.init
    1.15 +    private static final Mutex.Privileged PRIVILEGED = new Mutex.Privileged();
    1.16 +    public static final Mutex MUTEX = new Mutex(PRIVILEGED);
    1.17 +    // END: mutex.init
    1.18 +
    1.19 +    public MutexTest() {
    1.20 +    }
    1.21 +
    1.22 +    @BeforeClass
    1.23 +    public static void setUpClass() throws Exception {
    1.24 +    }
    1.25 +
    1.26 +    @AfterClass
    1.27 +    public static void tearDownClass() throws Exception {
    1.28 +    }
    1.29 +
    1.30 +    @Before
    1.31 +    public void setUp() {
    1.32 +    }
    1.33 +
    1.34 +    @After
    1.35 +    public void tearDown() {
    1.36 +    }
    1.37 +
    1.38 +    /**
    1.39 +     * Test of readAccess method, of class Mutex.
    1.40 +     */
    1.41 +    @Test
    1.42 +    public void readAccess() {
    1.43 +        class R implements Runnable {
    1.44 +            int cnt;
    1.45 +            
    1.46 +            public void run() {
    1.47 +                cnt++;
    1.48 +            }
    1.49 +        }
    1.50 +        R r = new R();
    1.51 +        Mutex instance = new Mutex();
    1.52 +        instance.readAccess(r);
    1.53 +        assertEquals("One call to runnable", 1, r.cnt);
    1.54 +    }
    1.55 +    
    1.56 +    @Test
    1.57 +    public void usePrivileged() {
    1.58 +        Mutex.Privileged lock = new Mutex.Privileged();
    1.59 +        Mutex mutex = new Mutex(lock);
    1.60 +
    1.61 +        // BEGIN: mutex.privileged
    1.62 +        lock.enterReadAccess();
    1.63 +        try {
    1.64 +          // do the operation
    1.65 +        } finally {
    1.66 +           lock.exitReadAccess();
    1.67 +        }
    1.68 +        // END: mutex.privileged
    1.69 +        
    1.70 +    }
    1.71 +
    1.72 +}
    1.73 \ No newline at end of file