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
     1 package api;
     2 
     3 import org.apidesign.privileged.api.Mutex;
     4 import org.junit.After;
     5 import org.junit.AfterClass;
     6 import org.junit.Before;
     7 import org.junit.BeforeClass;
     8 import org.junit.Test;
     9 import static org.junit.Assert.*;
    10 
    11 public class MutexTest {
    12     // BEGIN: mutex.init
    13     private static final Mutex.Privileged PRIVILEGED = new Mutex.Privileged();
    14     public static final Mutex MUTEX = new Mutex(PRIVILEGED);
    15     // END: mutex.init
    16 
    17     public MutexTest() {
    18     }
    19 
    20     @BeforeClass
    21     public static void setUpClass() throws Exception {
    22     }
    23 
    24     @AfterClass
    25     public static void tearDownClass() throws Exception {
    26     }
    27 
    28     @Before
    29     public void setUp() {
    30     }
    31 
    32     @After
    33     public void tearDown() {
    34     }
    35 
    36     /**
    37      * Test of readAccess method, of class Mutex.
    38      */
    39     @Test
    40     public void readAccess() {
    41         class R implements Runnable {
    42             int cnt;
    43             
    44             public void run() {
    45                 cnt++;
    46             }
    47         }
    48         R r = new R();
    49         Mutex instance = new Mutex();
    50         instance.readAccess(r);
    51         assertEquals("One call to runnable", 1, r.cnt);
    52     }
    53     
    54     @Test
    55     public void usePrivileged() {
    56         Mutex.Privileged lock = new Mutex.Privileged();
    57         Mutex mutex = new Mutex(lock);
    58 
    59         // BEGIN: mutex.privileged
    60         lock.enterReadAccess();
    61         try {
    62           // do the operation
    63         } finally {
    64            lock.exitReadAccess();
    65         }
    66         // END: mutex.privileged
    67         
    68     }
    69 
    70 }