Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AbstractCommandTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.sonar.application.command;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.Properties;
  26. import java.util.Random;
  27. import java.util.stream.Collectors;
  28. import java.util.stream.IntStream;
  29. import org.junit.Rule;
  30. import org.junit.Test;
  31. import org.junit.rules.ExpectedException;
  32. import org.junit.rules.TemporaryFolder;
  33. import org.mockito.Mockito;
  34. import org.sonar.process.ProcessId;
  35. import org.sonar.process.System2;
  36. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  37. import static org.assertj.core.api.Assertions.assertThat;
  38. import static org.mockito.Mockito.when;
  39. public class AbstractCommandTest {
  40. @Rule
  41. public TemporaryFolder temp = new TemporaryFolder();
  42. @Rule
  43. public ExpectedException expectedException = ExpectedException.none();
  44. @Test
  45. public void constructor_throws_NPE_of_ProcessId_is_null() throws IOException {
  46. expectedException.expect(NullPointerException.class);
  47. expectedException.expectMessage("ProcessId can't be null");
  48. new AbstractCommand<AbstractCommand>(null, temp.newFolder(), System2.INSTANCE) {
  49. };
  50. }
  51. @Test
  52. public void constructor_throws_NPE_of_workDir_is_null() {
  53. expectedException.expect(NullPointerException.class);
  54. expectedException.expectMessage("workDir can't be null");
  55. new AbstractCommand<AbstractCommand>(ProcessId.WEB_SERVER, null, System2.INSTANCE) {
  56. };
  57. }
  58. @Test
  59. public void setEnvVariable_fails_with_NPE_if_key_is_null() throws IOException {
  60. File workDir = temp.newFolder();
  61. AbstractCommand underTest = new AbstractCommand(ProcessId.ELASTICSEARCH, workDir, System2.INSTANCE) {
  62. };
  63. expectedException.expect(NullPointerException.class);
  64. expectedException.expectMessage("key can't be null");
  65. underTest.setEnvVariable(null, randomAlphanumeric(30));
  66. }
  67. @Test
  68. public void setEnvVariable_fails_with_NPE_if_value_is_null() throws IOException {
  69. File workDir = temp.newFolder();
  70. AbstractCommand underTest = new AbstractCommand(ProcessId.ELASTICSEARCH, workDir, System2.INSTANCE) {
  71. };
  72. expectedException.expect(NullPointerException.class);
  73. expectedException.expectMessage("value can't be null");
  74. underTest.setEnvVariable(randomAlphanumeric(30), null);
  75. }
  76. @Test
  77. public void constructor_puts_System_getEnv_into_map_of_env_variables() throws IOException {
  78. File workDir = temp.newFolder();
  79. System2 system2 = Mockito.mock(System2.class);
  80. Map<String, String> env = IntStream.range(0, 1 + new Random().nextInt(99)).mapToObj(String::valueOf).collect(Collectors.toMap(i -> "key" + i, j -> "value" + j));
  81. when(system2.getenv()).thenReturn(env);
  82. AbstractCommand underTest = new AbstractCommand(ProcessId.ELASTICSEARCH, workDir, system2) {
  83. };
  84. assertThat(underTest.getEnvVariables()).isEqualTo(env);
  85. }
  86. @Test
  87. public void suppressEnvVariable_remove_existing_env_variable_and_add_variable_to_set_of_suppressed_variables() throws IOException {
  88. File workDir = temp.newFolder();
  89. System2 system2 = Mockito.mock(System2.class);
  90. Map<String, String> env = new HashMap<>();
  91. String key1 = randomAlphanumeric(3);
  92. env.put(key1, randomAlphanumeric(9));
  93. when(system2.getenv()).thenReturn(env);
  94. AbstractCommand underTest = new AbstractCommand(ProcessId.ELASTICSEARCH, workDir, system2) {
  95. };
  96. underTest.suppressEnvVariable(key1);
  97. assertThat(underTest.getEnvVariables()).doesNotContainKey(key1);
  98. assertThat(underTest.getSuppressedEnvVariables()).containsOnly(key1);
  99. }
  100. }