samples/openfixed/test/org/apidesign/openfixed/PostTest.java
changeset 374 35da2d439e3d
child 375 3abae898011d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/openfixed/test/org/apidesign/openfixed/PostTest.java	Sun Mar 20 18:52:47 2011 +0100
     1.3 @@ -0,0 +1,62 @@
     1.4 +package org.apidesign.openfixed;
     1.5 +
     1.6 +import java.util.concurrent.CountDownLatch;
     1.7 +
     1.8 +/** Test the Calculator.createPending() behavior.
     1.9 + *
    1.10 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.11 + */
    1.12 +public final class PostTest extends PendingTest {
    1.13 +    
    1.14 +    public PostTest(String testName) {
    1.15 +        super(testName);
    1.16 +    }
    1.17 +
    1.18 +    @Override
    1.19 +    protected Calculator create() {
    1.20 +        return Calculator.createBatch();
    1.21 +    }
    1.22 +
    1.23 +    public void testPostModificationEvents() throws Exception {
    1.24 +        class PostListener extends BlockingListener implements PostModificationListener {
    1.25 +            int cnt;
    1.26 +
    1.27 +            @Override
    1.28 +            public synchronized void modification(ModificationEvent ev) {
    1.29 +                // registers for callback when batch processing is over:
    1.30 +                ev.postProcess(this);
    1.31 +                super.modification(ev);
    1.32 +            }
    1.33 +
    1.34 +            @Override
    1.35 +            public synchronized void postProcess(PostModificationEvent ev) {
    1.36 +                cnt++;
    1.37 +            }
    1.38 +            
    1.39 +            public synchronized void assertPostProcess(String msg, int expected) throws InterruptedException {
    1.40 +                for (int i = 0; i < 10; i++) {
    1.41 +                    if (cnt >= expected) {
    1.42 +                        break;
    1.43 +                    }
    1.44 +                    wait(1000);
    1.45 +                }
    1.46 +                assertEquals(msg, expected, cnt);
    1.47 +                cnt = 0;
    1.48 +            }
    1.49 +        }
    1.50 +        PostListener bl = new PostListener();
    1.51 +        
    1.52 +        Calculator calc = create();
    1.53 +        calc.addModificationListener(bl);
    1.54 +        
    1.55 +        calc.add(10);
    1.56 +        bl.first.await();
    1.57 +        
    1.58 +        calc.add(1);
    1.59 +        calc.add(2);
    1.60 +        calc.add(3);
    1.61 +        
    1.62 +        bl.cdl.countDown();
    1.63 +        bl.assertPostProcess("Two postprocessings (one for 10), then for the rest", 2);
    1.64 +    }
    1.65 +}