samples/privilegedcreator/test/org/apidesign/privileged/use/MutexTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
parent 334 0f3e13581901
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
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
}