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.closeTo;
9   import static org.hamcrest.Matchers.either;
10  import static org.hamcrest.Matchers.greaterThan;
11  import static org.hamcrest.Matchers.greaterThanOrEqualTo;
12  import static org.hamcrest.Matchers.is;
13  
14  import java.util.List;
15  
16  import org.junit.jupiter.api.Test;
17  
18  import oshi.SystemInfo;
19  
20  /**
21   * Test Power Source
22   */
23  class PowerSourceTest {
24      /**
25       * Test power source.
26       */
27      @Test
28      void testPowerSource() {
29          SystemInfo si = new SystemInfo();
30          List<PowerSource> psArr = si.getHardware().getPowerSources();
31          for (PowerSource ps : psArr) {
32              assertThat("Power Source's remaining capacity shouldn't be negative", ps.getRemainingCapacityPercent(),
33                      is(greaterThanOrEqualTo(0d)));
34              double epsilon = 1E-6;
35              assertThat(
36                      "Power Source's estimated remaining time should be greater than zero or within error margin of -1 or within error margin of -2",
37                      ps.getTimeRemainingEstimated(),
38                      is(either(greaterThan(0d)).or(closeTo(-1d, epsilon)).or(closeTo(-2d, epsilon))));
39          }
40      }
41  }