diff --git a/webcam-capture-drivers/driver-gstreamer/pom.xml b/webcam-capture-drivers/driver-gstreamer/pom.xml index 12b3da96..64acc2c5 100644 --- a/webcam-capture-drivers/driver-gstreamer/pom.xml +++ b/webcam-capture-drivers/driver-gstreamer/pom.xml @@ -34,6 +34,13 @@ --> + + + junit + junit + 4.11 + test + diff --git a/webcam-capture-drivers/driver-gstreamer/src/main/java/com/github/sarxos/webcam/ds/gstreamer/impl/VideoDeviceFilenameFilter.java b/webcam-capture-drivers/driver-gstreamer/src/main/java/com/github/sarxos/webcam/ds/gstreamer/impl/VideoDeviceFilenameFilter.java index 01f4d575..1889cc91 100644 --- a/webcam-capture-drivers/driver-gstreamer/src/main/java/com/github/sarxos/webcam/ds/gstreamer/impl/VideoDeviceFilenameFilter.java +++ b/webcam-capture-drivers/driver-gstreamer/src/main/java/com/github/sarxos/webcam/ds/gstreamer/impl/VideoDeviceFilenameFilter.java @@ -10,7 +10,8 @@ public class VideoDeviceFilenameFilter implements FilenameFilter { @Override public boolean accept(File dir, String name) { - return dir.getName().equals("dev") && name.startsWith("video") && Character.isDigit(name.charAt(5)); + return dir.getName().equals("dev") && name.startsWith("video") + && (name.length() > 5 && Character.isDigit(name.charAt(5))); } public File[] getVideoFiles() { diff --git a/webcam-capture-drivers/driver-gstreamer/src/test/java/com/github/sarxos/webcam/ds/gstreamer/impl/VideoDeviceFilenameFilterTest.java b/webcam-capture-drivers/driver-gstreamer/src/test/java/com/github/sarxos/webcam/ds/gstreamer/impl/VideoDeviceFilenameFilterTest.java new file mode 100644 index 00000000..6e5ae4cf --- /dev/null +++ b/webcam-capture-drivers/driver-gstreamer/src/test/java/com/github/sarxos/webcam/ds/gstreamer/impl/VideoDeviceFilenameFilterTest.java @@ -0,0 +1,36 @@ +package com.github.sarxos.webcam.ds.gstreamer.impl; + +import org.junit.Test; + +import java.io.File; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author Dan Rollo + * Date: 3/8/14 + * Time: 10:44 PM + */ +public class VideoDeviceFilenameFilterTest { + + /** + * Accept method was failing with exception: String index out of range: 5 + * This occurs on opensuse 11 where video device files do not all have a suffix. The files are created like so: + * $ ls -l /dev/video* + * /dev/video -> video0 + * /dev/video0 + * + * In this case, the link name 'video' is less that 6 characters long, so the filter statement: + * Character.isDigit(name.charAt(5)) + * causes the exception. + * + * Fix is to also check for length before checking for isDigit(). + */ + @Test + public void testAcceptHandlesShortVideoDeviceFilename() { + final VideoDeviceFilenameFilter videoDeviceFilenameFilter = new VideoDeviceFilenameFilter(); + assertFalse(videoDeviceFilenameFilter.accept(new File("/dev"), "video")); + assertTrue(videoDeviceFilenameFilter.accept(new File("/dev"), "video0")); + } +}