View Javadoc
1   /*
2    * Copyright 2016-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.util;
6   
7   import static org.hamcrest.MatcherAssert.assertThat;
8   import static org.hamcrest.Matchers.greaterThan;
9   import static org.hamcrest.Matchers.is;
10  
11  import org.junit.jupiter.api.Test;
12  
13  /**
14   * Test general utility methods
15   */
16  class UtilTest {
17  
18      @Test
19      void testSleep() {
20          // Windows counters may be up to 1/64 second (16ms) off
21          long now = System.nanoTime();
22          Util.sleep(100);
23          assertThat(System.nanoTime() - now, is(greaterThan(84_375_000L)));
24      }
25  
26      @Test
27      void testWildcardMatch() {
28          assertThat("Test should not match est", Util.wildcardMatch("Test", "est"), is(false));
29          assertThat("Test should match ^est", Util.wildcardMatch("Test", "^est"), is(true));
30          assertThat("Test should not match ^^est", Util.wildcardMatch("Test", "^^est"), is(false));
31          assertThat("Test should match ?est", Util.wildcardMatch("Test", "?est"), is(true));
32          assertThat("Test should not match ^?est", Util.wildcardMatch("Test", "^?est"), is(false));
33          assertThat("Test should match *est", Util.wildcardMatch("Test", "*est"), is(true));
34          assertThat("Test should not match ^*est", Util.wildcardMatch("Test", "^*est"), is(false));
35  
36          assertThat("Test should not match T?t", Util.wildcardMatch("Test", "T?t"), is(false));
37          assertThat("Test should match T??t", Util.wildcardMatch("Test", "T??t"), is(true));
38          assertThat("Test should match T*t", Util.wildcardMatch("Test", "T*t"), is(true));
39  
40          assertThat("Test should not match Tes", Util.wildcardMatch("Test", "Tes"), is(false));
41          assertThat("Test should match Tes?", Util.wildcardMatch("Test", "Tes?"), is(true));
42          assertThat("Test should match Tes*", Util.wildcardMatch("Test", "Tes*"), is(true));
43  
44          assertThat("Test should not match Te?", Util.wildcardMatch("Test", "Te?"), is(false));
45          assertThat("Test should match Te*", Util.wildcardMatch("Test", "Te*"), is(true));
46      }
47  
48      @Test
49      void testIsBlank() {
50          assertThat("\"\" should be Blank", Util.isBlank(""), is(true));
51          assertThat("null should be Blank", Util.isBlank(null), is(true));
52          assertThat("\"Not blank\" should not be Blank", Util.isBlank("Not blank"), is(false));
53      }
54  
55      @Test
56      void testIsBlankOrUnknown() {
57          assertThat("\"\" should be Blank", Util.isBlankOrUnknown(""), is(true));
58          assertThat("null should be Blank", Util.isBlankOrUnknown(null), is(true));
59          assertThat("unknown should be unknown", Util.isBlankOrUnknown(Constants.UNKNOWN), is(true));
60          assertThat("\"Not blank\" should not be Blank", Util.isBlankOrUnknown("Not blank"), is(false));
61      }
62  
63      @Test
64      void testIsSessionValid() {
65          assertThat("Session is invalid because user is empty", Util.isSessionValid("", "device", (long) 0), is(false));
66          assertThat("Session is invalid because device is empty", Util.isSessionValid("user", "", (long) 0), is(false));
67          assertThat("Session is invalid because loginTime is greater than current system time",
68                  Util.isSessionValid("user", "device", Long.MAX_VALUE), is(false));
69          assertThat("Session is invalid because loginTime is lesser than zero",
70                  Util.isSessionValid("user", "device", Long.MIN_VALUE), is(false));
71          assertThat("Session is valid because all the arguments are appropriate",
72                  Util.isSessionValid("user", "device", (long) 999999999), is(true));
73      }
74  }