| 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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 4 |
Complexity Density: 0.27 |
|
| 18 |
|
public class DNSStatefulObjectTest { |
| 19 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 27 |
2
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 34 |
2
|
@Override... |
| 35 |
|
public void run() { |
| 36 |
2
|
_semaphore.waitForEvent(_timeout); |
| 37 |
2
|
_hasFinished = true; |
| 38 |
|
} |
| 39 |
|
|
| 40 |
|
|
| 41 |
|
@return |
| 42 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 43 |
4
|
public boolean hasFinished() {... |
| 44 |
4
|
return _hasFinished; |
| 45 |
|
} |
| 46 |
|
|
| 47 |
|
} |
| 48 |
|
|
| 49 |
|
DNSStatefulObjectSemaphore _semaphore; |
| 50 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 51 |
2
|
@Before... |
| 52 |
|
public void setup() { |
| 53 |
2
|
_semaphore = new DNSStatefulObjectSemaphore("test"); |
| 54 |
|
} |
| 55 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 56 |
2
|
@After... |
| 57 |
|
public void teardown() { |
| 58 |
2
|
_semaphore = null; |
| 59 |
|
} |
| 60 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
| 61 |
1
|
@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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1
PASS
|
|
| 72 |
1
|
@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 |
|
} |