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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2016 SonarSource SA
  4. * mailto:contact 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.fest.assertions.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. System2 mockSystem;
  32. Logs logs;
  33. @Before
  34. public void setUp() {
  35. mockSystem = mock(System2.class);
  36. logs = mock(Logs.class);
  37. SystemInfo.setSystem(mockSystem);
  38. }
  39. @Test
  40. public void test_java() {
  41. mockJava();
  42. assertThat(SystemInfo.java()).isEqualTo("Java 1.9 oracle (64-bit)");
  43. when(mockSystem.getProperty("sun.arch.data.model")).thenReturn("32");
  44. assertThat(SystemInfo.java()).isEqualTo("Java 1.9 oracle (32-bit)");
  45. when(mockSystem.getProperty("sun.arch.data.model")).thenReturn(null);
  46. assertThat(SystemInfo.java()).isEqualTo("Java 1.9 oracle");
  47. }
  48. @Test
  49. public void test_os() {
  50. mockOs();
  51. assertThat(SystemInfo.os()).isEqualTo("linux 2.5 x64");
  52. }
  53. private void mockJava() {
  54. when(mockSystem.getProperty("java.version")).thenReturn("1.9");
  55. when(mockSystem.getProperty("java.vendor")).thenReturn("oracle");
  56. when(mockSystem.getProperty("sun.arch.data.model")).thenReturn("64");
  57. }
  58. private void mockOs() {
  59. when(mockSystem.getProperty("os.version")).thenReturn("2.5");
  60. when(mockSystem.getProperty("os.arch")).thenReturn("x64");
  61. when(mockSystem.getProperty("os.name")).thenReturn("linux");
  62. }
  63. @Test
  64. public void should_print() {
  65. mockOs();
  66. mockJava();
  67. when(mockSystem.getenv("SONAR_RUNNER_OPTS")).thenReturn("arg");
  68. SystemInfo.print(logs);
  69. verify(mockSystem).getProperty("java.version");
  70. verify(mockSystem).getProperty("os.version");
  71. verify(mockSystem).getenv("SONAR_RUNNER_OPTS");
  72. verify(logs, never()).info("SonarQube Scanner null");
  73. verify(logs).info("SonarQube Scanner " + ScannerVersion.version());
  74. verify(logs).info("Java 1.9 oracle (64-bit)");
  75. verify(logs).info("linux 2.5 x64");
  76. verify(logs).info("SONAR_RUNNER_OPTS=arg");
  77. verifyNoMoreInteractions(logs);
  78. }
  79. }