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.containsString;
9   import static org.hamcrest.Matchers.greaterThanOrEqualTo;
10  import static org.hamcrest.Matchers.is;
11  import static org.hamcrest.Matchers.notNullValue;
12  
13  import java.io.IOException;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  import org.junit.jupiter.api.Test;
18  
19  import oshi.SystemInfo;
20  
21  /**
22   * Test Disks
23   */
24  class DisksTest {
25  
26      /**
27       * Test disks extraction.
28       *
29       * @throws IOException Signals that an I/O exception has occurred.
30       */
31      @Test
32      void testDisks() throws IOException {
33          SystemInfo si = new SystemInfo();
34  
35          for (HWDiskStore disk : si.getHardware().getDiskStores()) {
36              assertThat(disk, is(notNullValue()));
37              List<HWPartition> parts = disk.getPartitions();
38              List<HWPartition> partList = new ArrayList<>(parts.size());
39              for (HWPartition part : parts) {
40                  partList.add(new HWPartition(part.getIdentification(), part.getName(), part.getType(), part.getUuid(),
41                          part.getSize(), part.getMajor(), part.getMinor(), part.getMountPoint()));
42              }
43  
44              assertThat("Disk name should not be null", disk.getName(), is(notNullValue()));
45              assertThat("Disk model should not be null", disk.getModel(), is(notNullValue()));
46              assertThat("Disk serial number should not be null", disk.getSerial(), is(notNullValue()));
47              assertThat("Disk size should be greater or equal 0", disk.getSize(), is(greaterThanOrEqualTo(0L)));
48              assertThat("Number of read operations from the disk should be greater or equal to 0", disk.getReads(),
49                      is(greaterThanOrEqualTo(0L)));
50              assertThat("Number of read bytes should be greater or equal to 0", disk.getReadBytes(),
51                      is(greaterThanOrEqualTo(0L)));
52              assertThat("Number of write operations from the disk should be greater or equal to 0", disk.getWrites(),
53                      is(greaterThanOrEqualTo(0L)));
54              assertThat("Number of write bytes should be greater or equal to 0", disk.getWriteBytes(),
55                      is(greaterThanOrEqualTo(0L)));
56              assertThat("Transfer times should be greater or equal 0", disk.getTransferTime(),
57                      is(greaterThanOrEqualTo(0L)));
58              assertThat("Update time for statistics should be greater or equal 0", disk.getTimeStamp(),
59                      is(greaterThanOrEqualTo(0L)));
60              assertThat("toString method should contain the disk name", disk.toString(), containsString(disk.getName()));
61  
62              long oldReads = disk.getReads();
63              long oldReadBytes = disk.getReadBytes();
64              if (oldReads > 0) {
65                  assertThat("Updating the disk statistics should work for " + disk.getName(), disk.updateAttributes(),
66                          is(true));
67                  assertThat("Number of reads from the disk has not been updated", disk.getReads(),
68                          is(greaterThanOrEqualTo(oldReads)));
69                  assertThat("Number of read bytes from the disk has not been updated", disk.getReadBytes(),
70                          is(greaterThanOrEqualTo(oldReadBytes)));
71              }
72              for (HWPartition partition : disk.getPartitions()) {
73                  assertThat("Identification of partition is null", partition.getIdentification(), is(notNullValue()));
74                  assertThat("Name of partition is null", partition.getName(), is(notNullValue()));
75                  assertThat("Type of partition is null", partition.getType(), is(notNullValue()));
76                  assertThat("UUID of partition is null", partition.getUuid(), is(notNullValue()));
77                  assertThat("Mount point of partition is null", partition.getMountPoint(), is(notNullValue()));
78                  assertThat("Partition size of partition " + partition.getName() + " is < 0", partition.getSize(),
79                          is(greaterThanOrEqualTo(0L)));
80                  assertThat("Major device ID of partition " + partition.getName() + "is < 0", partition.getMajor(),
81                          is(greaterThanOrEqualTo(0)));
82                  assertThat("Minor device ID of partition " + partition.getName() + "is < 0", partition.getMinor(),
83                          is(greaterThanOrEqualTo(0)));
84                  assertThat("Partition's toString() method should contain the partitions identification.",
85                          partition.toString(), containsString(partition.getIdentification()));
86              }
87          }
88      }
89  }