samples/openfixed/test/org/apidesign/openfixed/PostTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
parent 374 35da2d439e3d
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
     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         // BEGIN: openfixed.usemount
    22         class PostListener extends BlockingListener 
    23         implements PostModificationListener {
    24             int cnt;
    25 
    26             @Override
    27             public synchronized void modification(ModificationEvent ev) {
    28                 // registers for callback when batch processing is over:
    29                 ev.postProcess(this);
    30                 super.modification(ev);
    31             }
    32 
    33             @Override
    34             public synchronized void postProcess(PostModificationEvent ev) {
    35                 // called when batch processing is over
    36                 cnt++;
    37             }
    38         // FINISH: openfixed.usemount
    39             
    40             public synchronized void assertPostProcess(String msg, int expected) throws InterruptedException {
    41                 for (int i = 0; i < 10; i++) {
    42                     if (cnt >= expected) {
    43                         break;
    44                     }
    45                     wait(1000);
    46                 }
    47                 assertEquals(msg, expected, cnt);
    48                 cnt = 0;
    49             }
    50         }
    51         PostListener bl = new PostListener();
    52         
    53         Calculator calc = create();
    54         calc.addModificationListener(bl);
    55         
    56         calc.add(10);
    57         bl.first.await();
    58         
    59         calc.add(1);
    60         calc.add(2);
    61         calc.add(3);
    62         
    63         bl.cdl.countDown();
    64         bl.assertPostProcess("Two postprocessings (one for 10), then for the rest", 2);
    65     }
    66 }