View Javadoc
1   /*
2    * Copyright 2016-2022 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.empty;
9   import static org.hamcrest.Matchers.emptyString;
10  import static org.hamcrest.Matchers.hasSize;
11  import static org.hamcrest.Matchers.is;
12  
13  import java.util.List;
14  
15  import org.junit.jupiter.api.Test;
16  
17  import oshi.PlatformEnum;
18  import oshi.SystemInfo;
19  
20  /**
21   * Test command line and returning the result of execution.
22   */
23  class ExecutingCommandTest {
24  
25      private static final String ECHO = SystemInfo.getCurrentPlatform().equals(PlatformEnum.WINDOWS)
26              ? "cmd.exe /C echo Test"
27              : "echo Test";
28      private static final String BAD_COMMAND = "noOSshouldHaveACommandNamedThis";
29  
30      @Test
31      void testRunNative() {
32          List<String> test = ExecutingCommand.runNative(ECHO);
33          assertThat("echo output", test, hasSize(1));
34          assertThat("echo output", test.get(0), is("Test"));
35          assertThat("echo first answer", ExecutingCommand.getFirstAnswer(ECHO), is("Test"));
36  
37          assertThat("bad command", ExecutingCommand.runNative(BAD_COMMAND), is(empty()));
38          assertThat("bad command first answer", ExecutingCommand.getFirstAnswer(BAD_COMMAND), is(emptyString()));
39      }
40  }