samples/openfixed/test/org/apidesign/openfixed/PendingTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 20 Mar 2011 18:52:47 +0100
changeset 374 35da2d439e3d
permissions -rw-r--r--
Calculator and various ways to deliver changes in its counter to listeners
     1 package org.apidesign.openfixed;
     2 
     3 import java.util.List;
     4 import java.util.concurrent.CountDownLatch;
     5 
     6 /** Test the Calculator.createPending() behavior.
     7  *
     8  * @author Jaroslav Tulach <jtulach@netbeans.org>
     9  */
    10 public class PendingTest extends CalculatorBase {
    11     
    12     public PendingTest(String testName) {
    13         super(testName);
    14     }
    15 
    16     @Override
    17     protected Calculator create() {
    18         return Calculator.createPending();
    19     }
    20 
    21     public void testPendingEvents() throws Exception {
    22         BlockingListener bl = new BlockingListener();
    23         
    24         Calculator calc = create();
    25         calc.addModificationListener(bl);
    26         
    27         calc.add(10);
    28         bl.first.await();
    29         
    30         calc.add(1);
    31         calc.add(2);
    32         calc.add(3);
    33         
    34         bl.cdl.countDown();
    35         
    36         List<ModificationEvent> events = bl.assertEvents("Four changes together", 4);
    37         
    38         assertEquals("No pending events for first event", 0, events.get(0).getPending());
    39         assertEquals("Group of three, two remaining", 2, events.get(1).getPending());
    40         assertEquals("Group of three, one remaining", 1, events.get(2).getPending());
    41         assertEquals("Group of three, last one", 0, events.get(3).getPending());
    42     }
    43     
    44     static class BlockingListener extends MockListener {
    45         CountDownLatch first = new CountDownLatch(1);
    46         CountDownLatch cdl = new CountDownLatch(1);
    47 
    48         @Override
    49         public synchronized void modification(ModificationEvent ev) {
    50             try {
    51                 first.countDown();
    52                 cdl.await();
    53             } catch (InterruptedException ex) {
    54                 throw new IllegalStateException(ex);
    55             }
    56             super.modification(ev);
    57         }
    58     }
    59     
    60 }