Skip to content

Commit 277b5e8

Browse files
committed
More unit tests for WebcamLock, etc
1 parent 7c673a8 commit 277b5e8

File tree

6 files changed

+381
-207
lines changed

6 files changed

+381
-207
lines changed

pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,37 @@
9494
<groupId>junit</groupId>
9595
<artifactId>junit</artifactId>
9696
<version>4.11</version>
97+
<scope>test</scope>
9798
</dependency>
9899
<dependency>
99100
<groupId>org.easymock</groupId>
100101
<artifactId>easymock</artifactId>
101102
<version>3.2</version>
103+
<scope>test</scope>
102104
</dependency>
103105
<dependency>
104106
<groupId>org.powermock</groupId>
105107
<artifactId>powermock-module-junit4</artifactId>
106108
<version>1.5.5</version>
109+
<scope>test</scope>
107110
</dependency>
108111
<dependency>
109112
<groupId>org.powermock</groupId>
110113
<artifactId>powermock-api-easymock</artifactId>
111114
<version>1.5.5</version>
115+
<scope>test</scope>
116+
</dependency>
117+
<dependency>
118+
<groupId>org.assertj</groupId>
119+
<artifactId>assertj-core</artifactId>
120+
<version>1.6.1</version>
121+
<scope>test</scope>
122+
</dependency>
123+
<dependency>
124+
<groupId>com.jayway.awaitility</groupId>
125+
<artifactId>awaitility</artifactId>
126+
<version>1.6.2</version>
127+
<scope>test</scope>
112128
</dependency>
113129
<dependency>
114130
<groupId>org.slf4j</groupId>
@@ -119,6 +135,7 @@
119135
<groupId>ch.qos.logback</groupId>
120136
<artifactId>logback-classic</artifactId>
121137
<version>1.0.9</version>
138+
<scope>provided</scope>
122139
</dependency>
123140
</dependencies>
124141
</dependencyManagement>

webcam-capture/pom.xml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
<artifactId>bridj</artifactId>
3030
<version>0.6.3-SNAPSHOT</version>
3131
<exclusions>
32-
<exclusion>
33-
<artifactId>dx</artifactId>
34-
<groupId>com.google.android.tools</groupId>
35-
</exclusion>
32+
<exclusion>
33+
<artifactId>dx</artifactId>
34+
<groupId>com.google.android.tools</groupId>
35+
</exclusion>
3636
</exclusions>
3737
</dependency>
3838
<dependency>
@@ -44,11 +44,29 @@
4444
<artifactId>logback-classic</artifactId>
4545
<scope>provided</scope>
4646
</dependency>
47+
48+
<!-- unit tests -->
4749
<dependency>
4850
<groupId>junit</groupId>
4951
<artifactId>junit</artifactId>
5052
<scope>test</scope>
5153
</dependency>
54+
<dependency>
55+
<groupId>org.assertj</groupId>
56+
<artifactId>assertj-core</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.easymock</groupId>
61+
<artifactId>easymock</artifactId>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>com.jayway.awaitility</groupId>
66+
<artifactId>awaitility</artifactId>
67+
<scope>test</scope>
68+
</dependency>
69+
5270
</dependencies>
5371

5472
<!--

webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamLock.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414

1515

1616
/**
17-
* This class is used as a global (system) lock preventing other processes from
18-
* using the same camera while it's open. Whenever webcam is open there is a
19-
* thread running in background which updates the lock once per 2 seconds. Lock
20-
* is being released whenever webcam is either closed or completely disposed.
21-
* Lock will remain for at least 2 seconds in case when JVM has not been
17+
* This class is used as a global (system) lock preventing other processes from using the same
18+
* camera while it's open. Whenever webcam is open there is a thread running in background which
19+
* updates the lock once per 2 seconds. Lock is being released whenever webcam is either closed or
20+
* completely disposed. Lock will remain for at least 2 seconds in case when JVM has not been
2221
* gracefully terminated (due to SIGSEGV, SIGTERM, etc).
23-
*
22+
*
2423
* @author Bartosz Firyn (sarxos)
2524
*/
2625
public class WebcamLock {
@@ -37,7 +36,7 @@ public class WebcamLock {
3736

3837
/**
3938
* Used to update lock state.
40-
*
39+
*
4140
* @author sarxos
4241
*/
4342
private class LockUpdater extends Thread {
@@ -80,21 +79,21 @@ public void run() {
8079
/**
8180
* Is webcam locked (local, not cross-VM variable).
8281
*/
83-
private AtomicBoolean locked = new AtomicBoolean(false);
82+
private final AtomicBoolean locked = new AtomicBoolean(false);
8483

8584
/**
8685
* Is lock completely disabled.
8786
*/
88-
private AtomicBoolean disabled = new AtomicBoolean(false);
87+
private final AtomicBoolean disabled = new AtomicBoolean(false);
8988

9089
/**
9190
* Lock file.
9291
*/
93-
private File lock = null;
92+
private final File lock;
9493

9594
/**
9695
* Creates global webcam lock.
97-
*
96+
*
9897
* @param webcam the webcam instance to be locked
9998
*/
10099
protected WebcamLock(Webcam webcam) {
@@ -299,8 +298,8 @@ public void lock() {
299298
}
300299

301300
/**
302-
* Completely disable locking mechanism. After this method is invoked, the
303-
* lock will not have any effect on the webcam runtime.
301+
* Completely disable locking mechanism. After this method is invoked, the lock will not have
302+
* any effect on the webcam runtime.
304303
*/
305304
public void disable() {
306305
if (disabled.compareAndSet(false, true)) {
@@ -339,7 +338,7 @@ public void unlock() {
339338

340339
/**
341340
* Check if webcam is locked.
342-
*
341+
*
343342
* @return True if webcam is locked, false otherwise
344343
*/
345344
public boolean isLocked() {
@@ -373,4 +372,8 @@ public boolean isLocked() {
373372

374373
return false;
375374
}
375+
376+
public File getLockFile() {
377+
return lock;
378+
}
376379
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.github.sarxos.webcam;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.easymock.EasyMock;
5+
import org.easymock.EasyMockRunner;
6+
import org.easymock.EasyMockSupport;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
12+
/**
13+
* This test case is to cover {@link WebcamLock} class.
14+
*
15+
* @author Bartosz Firyn (sarxos)
16+
*/
17+
@RunWith(EasyMockRunner.class)
18+
public class WebcamLockTest extends EasyMockSupport {
19+
20+
Webcam webcam;
21+
22+
@Before
23+
public void before() {
24+
25+
webcam = createNiceMock(Webcam.class);
26+
27+
EasyMock
28+
.expect(webcam.getName())
29+
.andReturn("test-webcam")
30+
.anyTimes();
31+
32+
replayAll();
33+
}
34+
35+
@Test
36+
public void test_lock() {
37+
38+
WebcamLock lock = new WebcamLock(webcam);
39+
lock.lock();
40+
41+
Assertions
42+
.assertThat(lock.isLocked())
43+
.isTrue();
44+
45+
lock.unlock();
46+
47+
Assertions
48+
.assertThat(lock.isLocked())
49+
.isFalse();
50+
}
51+
52+
@Test
53+
public void test_lock2() {
54+
55+
WebcamLock first = new WebcamLock(webcam);
56+
WebcamLock second = new WebcamLock(webcam);
57+
58+
first.lock();
59+
60+
Assertions
61+
.assertThat(second.isLocked())
62+
.isTrue();
63+
}
64+
}

0 commit comments

Comments
 (0)