Skip to content

Commit

Permalink
More unit tests for WebcamLock, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Jan 12, 2015
1 parent 7c673a8 commit 277b5e8
Show file tree
Hide file tree
Showing 6 changed files with 381 additions and 207 deletions.
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,37 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.5.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>1.5.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand All @@ -119,6 +135,7 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down
26 changes: 22 additions & 4 deletions webcam-capture/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
<artifactId>bridj</artifactId>
<version>0.6.3-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>dx</artifactId>
<groupId>com.google.android.tools</groupId>
</exclusion>
<exclusion>
<artifactId>dx</artifactId>
<groupId>com.google.android.tools</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand All @@ -44,11 +44,29 @@
<artifactId>logback-classic</artifactId>
<scope>provided</scope>
</dependency>

<!-- unit tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@


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

/**
* Used to update lock state.
*
*
* @author sarxos
*/
private class LockUpdater extends Thread {
Expand Down Expand Up @@ -80,21 +79,21 @@ public void run() {
/**
* Is webcam locked (local, not cross-VM variable).
*/
private AtomicBoolean locked = new AtomicBoolean(false);
private final AtomicBoolean locked = new AtomicBoolean(false);

/**
* Is lock completely disabled.
*/
private AtomicBoolean disabled = new AtomicBoolean(false);
private final AtomicBoolean disabled = new AtomicBoolean(false);

/**
* Lock file.
*/
private File lock = null;
private final File lock;

/**
* Creates global webcam lock.
*
*
* @param webcam the webcam instance to be locked
*/
protected WebcamLock(Webcam webcam) {
Expand Down Expand Up @@ -299,8 +298,8 @@ public void lock() {
}

/**
* Completely disable locking mechanism. After this method is invoked, the
* lock will not have any effect on the webcam runtime.
* Completely disable locking mechanism. After this method is invoked, the lock will not have
* any effect on the webcam runtime.
*/
public void disable() {
if (disabled.compareAndSet(false, true)) {
Expand Down Expand Up @@ -339,7 +338,7 @@ public void unlock() {

/**
* Check if webcam is locked.
*
*
* @return True if webcam is locked, false otherwise
*/
public boolean isLocked() {
Expand Down Expand Up @@ -373,4 +372,8 @@ public boolean isLocked() {

return false;
}

public File getLockFile() {
return lock;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.github.sarxos.webcam;

import org.assertj.core.api.Assertions;
import org.easymock.EasyMock;
import org.easymock.EasyMockRunner;
import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;


/**
* This test case is to cover {@link WebcamLock} class.
*
* @author Bartosz Firyn (sarxos)
*/
@RunWith(EasyMockRunner.class)
public class WebcamLockTest extends EasyMockSupport {

Webcam webcam;

@Before
public void before() {

webcam = createNiceMock(Webcam.class);

EasyMock
.expect(webcam.getName())
.andReturn("test-webcam")
.anyTimes();

replayAll();
}

@Test
public void test_lock() {

WebcamLock lock = new WebcamLock(webcam);
lock.lock();

Assertions
.assertThat(lock.isLocked())
.isTrue();

lock.unlock();

Assertions
.assertThat(lock.isLocked())
.isFalse();
}

@Test
public void test_lock2() {

WebcamLock first = new WebcamLock(webcam);
WebcamLock second = new WebcamLock(webcam);

first.lock();

Assertions
.assertThat(second.isLocked())
.isTrue();
}
}
Loading

0 comments on commit 277b5e8

Please sign in to comment.