From d23233d53b50e4aa93cd2095ec7227a14bdffd02 Mon Sep 17 00:00:00 2001 From: Klaus Reimer Date: Tue, 16 Apr 2013 19:13:16 +0200 Subject: [PATCH] Run USB unit tests also when lsusb command line tool can be executed and returns at least two devices --- .../java/de/ailis/usb4java/UsbAssume.java | 47 ++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/test/java/de/ailis/usb4java/UsbAssume.java b/src/test/java/de/ailis/usb4java/UsbAssume.java index 8f8ee8f..f54fc59 100644 --- a/src/test/java/de/ailis/usb4java/UsbAssume.java +++ b/src/test/java/de/ailis/usb4java/UsbAssume.java @@ -7,6 +7,10 @@ package de.ailis.usb4java; import static org.junit.Assume.assumeTrue; +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; + /** * USB-related assumptions. * @@ -14,14 +18,53 @@ import static org.junit.Assume.assumeTrue; */ public class UsbAssume { + /** If USB tests are to be executed. */ + private static Boolean usbTests; + /** * Assume that USB tests are enabled. Call this in the first line of * tests method or preparation methods when you want to ignore the - * tests when the property USB_TESTS is not set. + * tests when the system is not able to run the tests anyway. + * + * USB tests can be controlled with the system property USB_TESTS. When + * set to true then USB tests are always run, if set to false then they + * are never run. If this property is not set then the command-line tool + * lsusb is called. If this tool returned at least two lines of text then + * USB tests are enabled. In all other cases they are disabled. */ public static void assumeUsbTestsEnabled() { + if (usbTests == null && System.getProperty("USB_TESTS") != null) + usbTests = Boolean.valueOf(System.getProperty("USB_TESTS")); + if (usbTests == null) + { + usbTests = false; + try + { + final Process proc = Runtime.getRuntime().exec("lsusb"); + if (proc.waitFor() == 0) + { + InputStream inputStream = proc.getInputStream(); + try + { + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + int lines = 0; + while (reader.readLine() != null) lines++; + usbTests = lines >= 2; + } + finally + { + inputStream.close(); + } + } + } + catch (Exception e) + { + // Ignored. USB tests are silently disabled when lsusb could + // not be run. + } + } assumeTrue("This test is ignored when USB_TESTS property is not set", - System.getProperty("USB_TESTS") != null); + usbTests); } }