3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.scanner.mediumtest.log;
22 import java.util.Collections;
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;
31 import static org.assertj.core.api.Assertions.assertThatThrownBy;
33 public class ExceptionHandlingMediumTest {
36 private static ErrorGlobalSettingsLoader loader;
39 public static void beforeClass() {
40 loader = new ErrorGlobalSettingsLoader();
43 public void setUp(boolean verbose) {
44 Batch.Builder builder = Batch.builder()
45 .setEnableLoggingConfiguration(true)
48 new EnvironmentInformation("mediumTest", "1.0"));
51 builder.setBootstrapProperties(Collections.singletonMap("sonar.verbose", "true"));
53 batch = builder.build();
57 public void test() throws Exception {
59 loader.withCause = false;
61 assertThatThrownBy(() -> batch.execute())
62 .isInstanceOf(MessageException.class)
63 .hasMessage("Error loading settings");
67 public void testWithCause() throws Exception {
69 loader.withCause = true;
71 assertThatThrownBy(() -> batch.execute())
72 .isInstanceOf(MessageException.class)
73 .hasMessage("Error loading settings")
74 .hasCauseInstanceOf(Throwable.class)
75 .hasRootCauseMessage("Code 401");
79 public void testWithVerbose() {
82 assertThatThrownBy(() -> batch.execute())
83 .isInstanceOf(IllegalStateException.class)
84 .hasMessageContaining("Unable to load component class");
87 private static class ErrorGlobalSettingsLoader implements GlobalSettingsLoader {
88 boolean withCause = false;
91 public Map<String, String> loadGlobalSettings() {
93 IllegalStateException cause = new IllegalStateException("Code 401");
94 throw MessageException.of("Error loading settings", cause);
96 throw MessageException.of("Error loading settings");