From 41d7c186dc866c05a60c14ebbbd2424a87d982b9 Mon Sep 17 00:00:00 2001 From: Klaus Reimer Date: Wed, 17 Apr 2013 21:44:50 +0200 Subject: [PATCH] Run TCK tests automatically if TCK test device is found --- .../de/ailis/usb4java/test/TCKRunner.java | 11 ++- .../de/ailis/usb4java/test/UsbAssume.java | 72 +++++++++++++++++-- 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/test/java/de/ailis/usb4java/test/TCKRunner.java b/src/test/java/de/ailis/usb4java/test/TCKRunner.java index 0f37223..2572303 100644 --- a/src/test/java/de/ailis/usb4java/test/TCKRunner.java +++ b/src/test/java/de/ailis/usb4java/test/TCKRunner.java @@ -5,6 +5,7 @@ package de.ailis.usb4java.test; +import org.junit.internal.AssumptionViolatedException; import org.junit.internal.runners.JUnit38ClassRunner; import org.junit.runner.Description; import org.junit.runner.manipulation.Filter; @@ -34,7 +35,15 @@ public class TCKRunner extends JUnit38ClassRunner @Override public boolean shouldRun(Description description) { - return System.getProperty("TCK_TESTS") != null; + try + { + UsbAssume.assumeTckTestsEnabled(); + return true; + } + catch (AssumptionViolatedException e) + { + return false; + } } @Override diff --git a/src/test/java/de/ailis/usb4java/test/UsbAssume.java b/src/test/java/de/ailis/usb4java/test/UsbAssume.java index e53fd32..fdf9c08 100644 --- a/src/test/java/de/ailis/usb4java/test/UsbAssume.java +++ b/src/test/java/de/ailis/usb4java/test/UsbAssume.java @@ -20,7 +20,10 @@ public class UsbAssume { /** If USB tests are to be executed. */ private static Boolean usbTests; - + + /** If TCK tests are to be executed. */ + private static Boolean tckTests; + /** * Assume that USB tests are enabled. Call this in the first line of * tests method or preparation methods when you want to ignore the @@ -47,9 +50,12 @@ public class UsbAssume InputStream inputStream = proc.getInputStream(); try { - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + BufferedReader reader = + new BufferedReader(new InputStreamReader( + inputStream)); int lines = 0; - while (reader.readLine() != null) lines++; + while (reader.readLine() != null) + lines++; usbTests = lines >= 2; } finally @@ -65,6 +71,64 @@ public class UsbAssume } } assumeTrue("This test is ignored when USB_TESTS property is not set", - usbTests); + usbTests); + } + + /** + * Assume that TCK tests are enabled. Call this in the first line of + * tests method or preparation methods when you want to ignore the + * tests when the system is not able to run the TCK tests anyway. + * + * TCK tests can be controlled with the system property TCK_TESTS. When + * set to true then TCK 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 to scan for the TCK hardware. If this tool found the + * TCK hardware then TCK tests are enabled. In all other cases they are + * disabled. + */ + public static void assumeTckTestsEnabled() + { + assumeUsbTestsEnabled(); + if (tckTests == null && System.getProperty("TCK_TESTS") != null) + tckTests = Boolean.valueOf(System.getProperty("TCK_TESTS")); + if (tckTests == null) + { + tckTests = 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)); + String line = reader.readLine(); + while (line != null) + { + if (line.contains("0547:1002")) + { + tckTests = true; + break; + } + line = reader.readLine(); + } + } + finally + { + inputStream.close(); + } + } + } + catch (Exception e) + { + // Ignored. USB tests are silently disabled when lsusb could + // not be run. + } + } + assumeTrue("This test is ignored when TCK_TESTS property is not set", + tckTests); } }