/*
 * Copyright (C) 2013 Klaus Reimer <k@ailis.de>
 * See LICENSE.md for licensing information.
 */

package de.ailis.usb4java.test;

import static org.junit.Assume.assumeTrue;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * USB-related assumptions.
 * 
 * @author Klaus Reimer (k@ailis.de)
 */
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
     * 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",
            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);
    }
}

@@ -1843,32 +1843,90 @@
  inputStream));%0A
+                        try%0A                        %7B%0A    
                 
@@ -1948,16 +1948,20 @@
 es = 0;%0A
+    
         
@@ -2034,24 +2034,28 @@
             
+    
 lines++;%0A   
@@ -2051,16 +2051,20 @@
 ines++;%0A
+    
         
@@ -2102,16 +2102,170 @@
 s %3E= 2;%0A
+                        %7D%0A                        finally%0A                        %7B%0A                            reader.close();%0A                        %7D%0A
         
@@ -4085,32 +4085,90 @@
  inputStream));%0A
+                        try%0A                        %7B%0A    
                 
@@ -4200,32 +4200,36 @@
 der.readLine();%0A
+    
                 
@@ -4273,34 +4273,42 @@
                 
-%7B%0A
+    %7B%0A    
                 
@@ -4371,34 +4371,42 @@
                 
-%7B%0A
+    %7B%0A    
                 
@@ -4474,15 +4474,23 @@
     
+    
 break;%0A
+    
     
@@ -4539,24 +4539,28 @@
             
+    
 line = reade
@@ -4565,32 +4565,190 @@
 der.readLine();%0A
+                            %7D%0A                        %7D%0A                        finally%0A                        %7B%0A                            reader.close();%0A
                 

Fix handle leak warnings