3 * Copyright (C) 2009-2023 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 javax.annotation.Priority;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.sonar.api.utils.MessageException;
28 import org.sonar.batch.bootstrapper.Batch;
29 import org.sonar.batch.bootstrapper.EnvironmentInformation;
30 import org.sonar.scanner.repository.settings.GlobalSettingsLoader;
31 import org.springframework.beans.factory.UnsatisfiedDependencyException;
33 import static org.assertj.core.api.Assertions.assertThatThrownBy;
35 public class ExceptionHandlingMediumTest {
38 private static ErrorGlobalSettingsLoader loader;
41 public static void beforeClass() {
42 loader = new ErrorGlobalSettingsLoader();
45 public void setUp(boolean verbose) {
46 Batch.Builder builder = Batch.builder()
47 .setEnableLoggingConfiguration(true)
50 new EnvironmentInformation("mediumTest", "1.0"));
53 builder.setBootstrapProperties(Collections.singletonMap("sonar.verbose", "true"));
55 batch = builder.build();
59 public void test() throws Exception {
61 loader.withCause = false;
63 assertThatThrownBy(() -> batch.execute())
64 .isInstanceOf(MessageException.class)
65 .hasMessage("Error loading settings");
69 public void testWithCause() throws Exception {
71 loader.withCause = true;
73 assertThatThrownBy(() -> batch.execute())
74 .isInstanceOf(MessageException.class)
75 .hasMessage("Error loading settings")
76 .hasCauseInstanceOf(Throwable.class)
77 .hasRootCauseMessage("Code 401");
81 public void testWithVerbose() {
83 assertThatThrownBy(() -> batch.execute())
84 .isInstanceOf(UnsatisfiedDependencyException.class)
85 .hasMessageContaining("Error loading settings");
89 private static class ErrorGlobalSettingsLoader implements GlobalSettingsLoader {
90 boolean withCause = false;
93 public Map<String, String> loadGlobalSettings() {
95 IllegalStateException cause = new IllegalStateException("Code 401");
96 throw MessageException.of("Error loading settings", cause);
98 throw MessageException.of("Error loading settings");