View Javadoc
1   /*
2    * Copyright 2016-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware;
6   
7   import static org.hamcrest.MatcherAssert.assertThat;
8   import static org.hamcrest.Matchers.emptyString;
9   import static org.hamcrest.Matchers.is;
10  import static org.hamcrest.Matchers.not;
11  import static org.hamcrest.Matchers.notNullValue;
12  
13  import java.util.List;
14  
15  import org.junit.jupiter.api.Test;
16  
17  import oshi.SystemInfo;
18  
19  /**
20   * Test USB device
21   */
22  class UsbDeviceTest {
23      /**
24       * Test USB Devices
25       */
26      @Test
27      void testUsbDevices() {
28          SystemInfo si = new SystemInfo();
29          List<UsbDevice> usbList = si.getHardware().getUsbDevices(true);
30          for (UsbDevice usb : usbList) {
31              assertThat("USB object shouldn't be null", usb, is(notNullValue()));
32              testUsbRecursive(usb);
33          }
34      }
35  
36      private void testUsbRecursive(UsbDevice usb) {
37          assertThat("USB name shouldn't be blank", usb.getName(), is(not(emptyString())));
38          assertThat("USB vendor shouldn't be null", usb.getVendor(), is(notNullValue()));
39          assertThat("USB product ID shouldn't be null", usb.getProductId(), is(notNullValue()));
40          assertThat("USB vendor ID shouldn't be null", usb.getVendorId(), is(notNullValue()));
41          assertThat("USB serial number shouldn't be null", usb.getSerialNumber(), is(notNullValue()));
42  
43          for (UsbDevice nested : usb.getConnectedDevices()) {
44              testUsbRecursive(nested);
45          }
46      }
47  }