Clover Coverage Report - JmDNS 3.4.1
Coverage timestamp: Thu Aug 25 2011 13:06:33 CEST
22   82   7   3.14
0   56   0.32   3.5
7     1  
2    
 
  DNSStatefulObjectTest       Line # 18 15 0% 4 0 100% 1.0
  DNSStatefulObjectTest.WaitingThread       Line # 20 7 0% 3 0 100% 1.0
 
  (2)
 
1    /**
2    *
3    */
4    package javax.jmdns.test;
5   
6    import static junit.framework.Assert.assertFalse;
7    import static junit.framework.Assert.assertTrue;
8   
9    import javax.jmdns.impl.DNSStatefulObject.DNSStatefulObjectSemaphore;
10   
11    import org.junit.After;
12    import org.junit.Before;
13    import org.junit.Test;
14   
15    /**
16    *
17    */
 
18    public class DNSStatefulObjectTest {
19   
 
20    public static final class WaitingThread extends Thread {
21   
22    private final DNSStatefulObjectSemaphore _semaphore;
23    private final long _timeout;
24   
25    private boolean _hasFinished;
26   
 
27  2 toggle public WaitingThread(DNSStatefulObjectSemaphore semaphore, long timeout) {
28  2 super("Waiting thread");
29  2 _semaphore = semaphore;
30  2 _timeout = timeout;
31  2 _hasFinished = false;
32    }
33   
 
34  2 toggle @Override
35    public void run() {
36  2 _semaphore.waitForEvent(_timeout);
37  2 _hasFinished = true;
38    }
39   
40    /**
41    * @return the hasFinished
42    */
 
43  4 toggle public boolean hasFinished() {
44  4 return _hasFinished;
45    }
46   
47    }
48   
49    DNSStatefulObjectSemaphore _semaphore;
50   
 
51  2 toggle @Before
52    public void setup() {
53  2 _semaphore = new DNSStatefulObjectSemaphore("test");
54    }
55   
 
56  2 toggle @After
57    public void teardown() {
58  2 _semaphore = null;
59    }
60   
 
61  1 toggle @Test
62    public void testWaitAndSignal() throws InterruptedException {
63  1 WaitingThread thread = new WaitingThread(_semaphore, Long.MAX_VALUE);
64  1 thread.start();
65  1 Thread.sleep(1);
66  1 assertFalse("The thread should be waiting.", thread.hasFinished());
67  1 _semaphore.signalEvent();
68  1 Thread.sleep(1);
69  1 assertTrue("The thread should have finished.", thread.hasFinished());
70    }
71   
 
72  1 toggle @Test
73    public void testWaitAndTimeout() throws InterruptedException {
74  1 WaitingThread thread = new WaitingThread(_semaphore, 100);
75  1 thread.start();
76  1 Thread.sleep(1);
77  1 assertFalse("The thread should be waiting.", thread.hasFinished());
78  1 Thread.sleep(150);
79  1 assertTrue("The thread should have finished.", thread.hasFinished());
80    }
81   
82    }