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
     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         Mutex instance = new Mutex();
    42 
    43         // BEGIN: mutex.use
    44         class R implements Runnable {
    45             int cnt;
    46             
    47             public void run() {
    48                 cnt++;
    49             }
    50         }
    51         R r = new R();
    52         instance.readAccess(r);
    53         assertEquals("One call to runnable", 1, r.cnt);
    54         // END: mutex.use
    55     }
    56     
    57     @Test
    58     public void usePrivileged() {
    59         Mutex.Privileged lock = new Mutex.Privileged();
    60         Mutex mutex = new Mutex(lock);
    61 
    62         // BEGIN: mutex.privileged
    63         lock.enterReadAccess();
    64         try {
    65           // do the operation
    66         } finally {
    67            lock.exitReadAccess();
    68         }
    69         // END: mutex.privileged
    70         
    71     }
    72 
    73 }