samples/privilegedcreator/test/api/MutexTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 25 May 2009 18:27:02 +0200
changeset 330 be82436a7a8b
parent 71 a9bd40166b60
child 331 b5228ed41a43
permissions -rw-r--r--
More code snippets for the mutex example
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@71
    41
        class R implements Runnable {
jtulach@71
    42
            int cnt;
jtulach@71
    43
            
jtulach@71
    44
            public void run() {
jtulach@71
    45
                cnt++;
jtulach@71
    46
            }
jtulach@71
    47
        }
jtulach@71
    48
        R r = new R();
jtulach@71
    49
        Mutex instance = new Mutex();
jtulach@71
    50
        instance.readAccess(r);
jtulach@71
    51
        assertEquals("One call to runnable", 1, r.cnt);
jtulach@71
    52
    }
jtulach@71
    53
    
jtulach@71
    54
    @Test
jtulach@71
    55
    public void usePrivileged() {
jtulach@71
    56
        Mutex.Privileged lock = new Mutex.Privileged();
jtulach@71
    57
        Mutex mutex = new Mutex(lock);
jtulach@71
    58
jtulach@71
    59
        // BEGIN: mutex.privileged
jtulach@71
    60
        lock.enterReadAccess();
jtulach@71
    61
        try {
jtulach@71
    62
          // do the operation
jtulach@71
    63
        } finally {
jtulach@71
    64
           lock.exitReadAccess();
jtulach@71
    65
        }
jtulach@71
    66
        // END: mutex.privileged
jtulach@71
    67
        
jtulach@71
    68
    }
jtulach@71
    69
jtulach@71
    70
}