samples/openfixed/test/org/apidesign/openfixed/PostTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 20 Mar 2011 18:52:47 +0100
changeset 374 35da2d439e3d
child 375 3abae898011d
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.concurrent.CountDownLatch;
     4 
     5 /** Test the Calculator.createPending() behavior.
     6  *
     7  * @author Jaroslav Tulach <jtulach@netbeans.org>
     8  */
     9 public final class PostTest extends PendingTest {
    10     
    11     public PostTest(String testName) {
    12         super(testName);
    13     }
    14 
    15     @Override
    16     protected Calculator create() {
    17         return Calculator.createBatch();
    18     }
    19 
    20     public void testPostModificationEvents() throws Exception {
    21         class PostListener extends BlockingListener implements PostModificationListener {
    22             int cnt;
    23 
    24             @Override
    25             public synchronized void modification(ModificationEvent ev) {
    26                 // registers for callback when batch processing is over:
    27                 ev.postProcess(this);
    28                 super.modification(ev);
    29             }
    30 
    31             @Override
    32             public synchronized void postProcess(PostModificationEvent ev) {
    33                 cnt++;
    34             }
    35             
    36             public synchronized void assertPostProcess(String msg, int expected) throws InterruptedException {
    37                 for (int i = 0; i < 10; i++) {
    38                     if (cnt >= expected) {
    39                         break;
    40                     }
    41                     wait(1000);
    42                 }
    43                 assertEquals(msg, expected, cnt);
    44                 cnt = 0;
    45             }
    46         }
    47         PostListener bl = new PostListener();
    48         
    49         Calculator calc = create();
    50         calc.addModificationListener(bl);
    51         
    52         calc.add(10);
    53         bl.first.await();
    54         
    55         calc.add(1);
    56         calc.add(2);
    57         calc.add(3);
    58         
    59         bl.cdl.countDown();
    60         bl.assertPostProcess("Two postprocessings (one for 10), then for the rest", 2);
    61     }
    62 }