samples/privilegedcreator/test/org/apidesign/privileged/use/MutexTest.java
changeset 409 40cabcdcd2be
parent 334 0f3e13581901
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/privilegedcreator/test/org/apidesign/privileged/use/MutexTest.java	Thu Oct 30 21:30:10 2014 +0100
     1.3 @@ -0,0 +1,51 @@
     1.4 +package org.apidesign.privileged.use;
     1.5 +
     1.6 +import org.apidesign.privileged.api.Mutex;
     1.7 +import org.junit.Test;
     1.8 +import static org.junit.Assert.*;
     1.9 +
    1.10 +public class MutexTest {
    1.11 +    // BEGIN: mutex.init
    1.12 +    private static final Mutex.Privileged PRIVILEGED = new Mutex.Privileged();
    1.13 +    public static final Mutex MUTEX = new Mutex(PRIVILEGED);
    1.14 +    // END: mutex.init
    1.15 +
    1.16 +    public MutexTest() {
    1.17 +    }
    1.18 +
    1.19 +    /**
    1.20 +     * Test of withLock method, of class Mutex.
    1.21 +     */
    1.22 +    @Test
    1.23 +    public void readAccess() {
    1.24 +        // BEGIN: mutex.use
    1.25 +        class R implements Runnable {
    1.26 +            int cnt;
    1.27 +            
    1.28 +            public void run() {
    1.29 +                cnt++;
    1.30 +            }
    1.31 +        }
    1.32 +        R r = new R();
    1.33 +        MUTEX.withLock(r);
    1.34 +        assertEquals("Counter increased", 1, r.cnt);
    1.35 +        // END: mutex.use
    1.36 +    }
    1.37 +    
    1.38 +    @Test
    1.39 +    public void usePrivileged() {
    1.40 +        int cnt = 0;
    1.41 +        // BEGIN: mutex.privileged
    1.42 +        try {
    1.43 +            PRIVILEGED.lock();
    1.44 +            // do the operation
    1.45 +            cnt++;
    1.46 +        } finally {
    1.47 +           PRIVILEGED.unlock();
    1.48 +        }
    1.49 +        assertEquals("Counter increased", 1, cnt);
    1.50 +        // END: mutex.privileged
    1.51 +        
    1.52 +    }
    1.53 +
    1.54 +}