samples/privilegedcreator/test/api/MutexTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 25 May 2009 18:35:11 +0200
changeset 331 b5228ed41a43
parent 330 be82436a7a8b
child 332 139aacf8152d
permissions -rw-r--r--
Also showing traditional API use
jtulach@71
     1
package api;
jtulach@71
     2
jtulach@330
     3
import org.apidesign.privileged.api.Mutex;
jtulach@71
     4
import org.junit.After;
jtulach@71
     5
import org.junit.AfterClass;
jtulach@71
     6
import org.junit.Before;
jtulach@71
     7
import org.junit.BeforeClass;
jtulach@71
     8
import org.junit.Test;
jtulach@71
     9
import static org.junit.Assert.*;
jtulach@71
    10
jtulach@71
    11
public class MutexTest {
jtulach@71
    12
    // BEGIN: mutex.init
jtulach@71
    13
    private static final Mutex.Privileged PRIVILEGED = new Mutex.Privileged();
jtulach@71
    14
    public static final Mutex MUTEX = new Mutex(PRIVILEGED);
jtulach@71
    15
    // END: mutex.init
jtulach@71
    16
jtulach@71
    17
    public MutexTest() {
jtulach@71
    18
    }
jtulach@71
    19
jtulach@71
    20
    @BeforeClass
jtulach@71
    21
    public static void setUpClass() throws Exception {
jtulach@71
    22
    }
jtulach@71
    23
jtulach@71
    24
    @AfterClass
jtulach@71
    25
    public static void tearDownClass() throws Exception {
jtulach@71
    26
    }
jtulach@71
    27
jtulach@71
    28
    @Before
jtulach@71
    29
    public void setUp() {
jtulach@71
    30
    }
jtulach@71
    31
jtulach@71
    32
    @After
jtulach@71
    33
    public void tearDown() {
jtulach@71
    34
    }
jtulach@71
    35
jtulach@71
    36
    /**
jtulach@71
    37
     * Test of readAccess method, of class Mutex.
jtulach@71
    38
     */
jtulach@71
    39
    @Test
jtulach@71
    40
    public void readAccess() {
jtulach@331
    41
        Mutex instance = new Mutex();
jtulach@331
    42
jtulach@331
    43
        // BEGIN: mutex.use
jtulach@71
    44
        class R implements Runnable {
jtulach@71
    45
            int cnt;
jtulach@71
    46
            
jtulach@71
    47
            public void run() {
jtulach@71
    48
                cnt++;
jtulach@71
    49
            }
jtulach@71
    50
        }
jtulach@71
    51
        R r = new R();
jtulach@71
    52
        instance.readAccess(r);
jtulach@71
    53
        assertEquals("One call to runnable", 1, r.cnt);
jtulach@331
    54
        // END: mutex.use
jtulach@71
    55
    }
jtulach@71
    56
    
jtulach@71
    57
    @Test
jtulach@71
    58
    public void usePrivileged() {
jtulach@71
    59
        Mutex.Privileged lock = new Mutex.Privileged();
jtulach@71
    60
        Mutex mutex = new Mutex(lock);
jtulach@71
    61
jtulach@71
    62
        // BEGIN: mutex.privileged
jtulach@71
    63
        lock.enterReadAccess();
jtulach@71
    64
        try {
jtulach@71
    65
          // do the operation
jtulach@71
    66
        } finally {
jtulach@71
    67
           lock.exitReadAccess();
jtulach@71
    68
        }
jtulach@71
    69
        // END: mutex.privileged
jtulach@71
    70
        
jtulach@71
    71
    }
jtulach@71
    72
jtulach@71
    73
}