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.

ExceptionHandlingMediumTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.scanner.mediumtest.log;
  21. import java.util.Collections;
  22. import java.util.Map;
  23. import javax.annotation.Priority;
  24. import org.junit.BeforeClass;
  25. import org.junit.Test;
  26. import org.sonar.api.utils.MessageException;
  27. import org.sonar.batch.bootstrapper.Batch;
  28. import org.sonar.batch.bootstrapper.EnvironmentInformation;
  29. import org.sonar.scanner.repository.settings.GlobalSettingsLoader;
  30. import org.springframework.beans.factory.UnsatisfiedDependencyException;
  31. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  32. public class ExceptionHandlingMediumTest {
  33. private Batch batch;
  34. private static ErrorGlobalSettingsLoader loader;
  35. @BeforeClass
  36. public static void beforeClass() {
  37. loader = new ErrorGlobalSettingsLoader();
  38. }
  39. public void setUp(boolean verbose) {
  40. Batch.Builder builder = Batch.builder()
  41. .setEnableLoggingConfiguration(true)
  42. .addComponents(
  43. loader,
  44. new EnvironmentInformation("mediumTest", "1.0"));
  45. if (verbose) {
  46. builder.setBootstrapProperties(Collections.singletonMap("sonar.verbose", "true"));
  47. }
  48. batch = builder.build();
  49. }
  50. @Test
  51. public void test() throws Exception {
  52. setUp(false);
  53. loader.withCause = false;
  54. assertThatThrownBy(() -> batch.execute())
  55. .isInstanceOf(MessageException.class)
  56. .hasMessage("Error loading settings");
  57. }
  58. @Test
  59. public void testWithCause() throws Exception {
  60. setUp(false);
  61. loader.withCause = true;
  62. assertThatThrownBy(() -> batch.execute())
  63. .isInstanceOf(MessageException.class)
  64. .hasMessage("Error loading settings")
  65. .hasCauseInstanceOf(Throwable.class)
  66. .hasRootCauseMessage("Code 401");
  67. }
  68. @Test
  69. public void testWithVerbose() {
  70. setUp(true);
  71. assertThatThrownBy(() -> batch.execute())
  72. .isInstanceOf(UnsatisfiedDependencyException.class)
  73. .hasMessageContaining("Error loading settings");
  74. }
  75. @Priority(1)
  76. private static class ErrorGlobalSettingsLoader implements GlobalSettingsLoader {
  77. boolean withCause = false;
  78. @Override
  79. public Map<String, String> loadGlobalSettings() {
  80. if (withCause) {
  81. IllegalStateException cause = new IllegalStateException("Code 401");
  82. throw MessageException.of("Error loading settings", cause);
  83. } else {
  84. throw MessageException.of("Error loading settings");
  85. }
  86. }
  87. }
  88. }