samples/openfixed/test/org/apidesign/openfixed/PendingTest.java
changeset 374 35da2d439e3d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/openfixed/test/org/apidesign/openfixed/PendingTest.java	Sun Mar 20 18:52:47 2011 +0100
     1.3 @@ -0,0 +1,60 @@
     1.4 +package org.apidesign.openfixed;
     1.5 +
     1.6 +import java.util.List;
     1.7 +import java.util.concurrent.CountDownLatch;
     1.8 +
     1.9 +/** Test the Calculator.createPending() behavior.
    1.10 + *
    1.11 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.12 + */
    1.13 +public class PendingTest extends CalculatorBase {
    1.14 +    
    1.15 +    public PendingTest(String testName) {
    1.16 +        super(testName);
    1.17 +    }
    1.18 +
    1.19 +    @Override
    1.20 +    protected Calculator create() {
    1.21 +        return Calculator.createPending();
    1.22 +    }
    1.23 +
    1.24 +    public void testPendingEvents() throws Exception {
    1.25 +        BlockingListener bl = new BlockingListener();
    1.26 +        
    1.27 +        Calculator calc = create();
    1.28 +        calc.addModificationListener(bl);
    1.29 +        
    1.30 +        calc.add(10);
    1.31 +        bl.first.await();
    1.32 +        
    1.33 +        calc.add(1);
    1.34 +        calc.add(2);
    1.35 +        calc.add(3);
    1.36 +        
    1.37 +        bl.cdl.countDown();
    1.38 +        
    1.39 +        List<ModificationEvent> events = bl.assertEvents("Four changes together", 4);
    1.40 +        
    1.41 +        assertEquals("No pending events for first event", 0, events.get(0).getPending());
    1.42 +        assertEquals("Group of three, two remaining", 2, events.get(1).getPending());
    1.43 +        assertEquals("Group of three, one remaining", 1, events.get(2).getPending());
    1.44 +        assertEquals("Group of three, last one", 0, events.get(3).getPending());
    1.45 +    }
    1.46 +    
    1.47 +    static class BlockingListener extends MockListener {
    1.48 +        CountDownLatch first = new CountDownLatch(1);
    1.49 +        CountDownLatch cdl = new CountDownLatch(1);
    1.50 +
    1.51 +        @Override
    1.52 +        public synchronized void modification(ModificationEvent ev) {
    1.53 +            try {
    1.54 +                first.countDown();
    1.55 +                cdl.await();
    1.56 +            } catch (InterruptedException ex) {
    1.57 +                throw new IllegalStateException(ex);
    1.58 +            }
    1.59 +            super.modification(ev);
    1.60 +        }
    1.61 +    }
    1.62 +    
    1.63 +}