You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystemInfoTest.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonarsource.scanner.cli;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import org.sonarsource.scanner.cli.SystemInfo.System2;
  24. import static org.assertj.core.api.Assertions.assertThat;
  25. import static org.mockito.Mockito.mock;
  26. import static org.mockito.Mockito.never;
  27. import static org.mockito.Mockito.verify;
  28. import static org.mockito.Mockito.verifyNoMoreInteractions;
  29. import static org.mockito.Mockito.when;
  30. public class SystemInfoTest {
  31. private System2 mockSystem = mock(System2.class);
  32. private Logs logs = mock(Logs.class);
  33. @Before
  34. public void setUp() {
  35. SystemInfo.setSystem(mockSystem);
  36. }
  37. @Test
  38. public void test_java() {
  39. mockJava();
  40. assertThat(SystemInfo.java()).isEqualTo("Java 1.9 oracle (64-bit)");
  41. when(mockSystem.getProperty("sun.arch.data.model")).thenReturn("32");
  42. assertThat(SystemInfo.java()).isEqualTo("Java 1.9 oracle (32-bit)");
  43. when(mockSystem.getProperty("sun.arch.data.model")).thenReturn(null);
  44. assertThat(SystemInfo.java()).isEqualTo("Java 1.9 oracle");
  45. }
  46. @Test
  47. public void test_os() {
  48. mockOs();
  49. assertThat(SystemInfo.os()).isEqualTo("linux 2.5 x64");
  50. }
  51. private void mockJava() {
  52. when(mockSystem.getProperty("java.version")).thenReturn("1.9");
  53. when(mockSystem.getProperty("java.vendor")).thenReturn("oracle");
  54. when(mockSystem.getProperty("sun.arch.data.model")).thenReturn("64");
  55. }
  56. private void mockOs() {
  57. when(mockSystem.getProperty("os.version")).thenReturn("2.5");
  58. when(mockSystem.getProperty("os.arch")).thenReturn("x64");
  59. when(mockSystem.getProperty("os.name")).thenReturn("linux");
  60. }
  61. @Test
  62. public void should_print() {
  63. mockOs();
  64. mockJava();
  65. when(mockSystem.getenv("SONAR_SCANNER_OPTS")).thenReturn("arg");
  66. SystemInfo.print(logs);
  67. verify(mockSystem).getProperty("java.version");
  68. verify(mockSystem).getProperty("os.version");
  69. verify(mockSystem).getenv("SONAR_SCANNER_OPTS");
  70. verify(logs, never()).info("SonarScanner null");
  71. verify(logs).info("SonarScanner " + ScannerVersion.version());
  72. verify(logs).info("Java 1.9 oracle (64-bit)");
  73. verify(logs).info("linux 2.5 x64");
  74. verify(logs).info("SONAR_SCANNER_OPTS=arg");
  75. verifyNoMoreInteractions(logs);
  76. }
  77. }