import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings;
import org.sonar.api.database.DatabaseProperties;
-import org.sonar.core.persistence.BadDatabaseVersion;
+import org.sonar.api.utils.MessageException;
import org.sonar.core.persistence.DatabaseVersion;
/**
message.append(" / *****)\n\t- Server side: check the configuration at ");
message.append(server.getURL());
message.append("/system\n");
- throw new BadDatabaseVersion(message.toString());
+ throw new MessageException(message.toString());
}
}
private void checkDatabaseStatus() {
DatabaseVersion.Status status = version.getStatus();
if (status == DatabaseVersion.Status.REQUIRES_DOWNGRADE) {
- throw new BadDatabaseVersion("Database relates to a more recent version of SonarQube. Please check your settings (JDBC settings, version of Maven plugin)");
+ throw new MessageException("Database relates to a more recent version of SonarQube. Please check your settings (JDBC settings, version of Maven plugin)");
}
if (status == DatabaseVersion.Status.REQUIRES_UPGRADE) {
- throw new BadDatabaseVersion("Database must be upgraded. Please browse " + server.getURL() + "/setup");
+ throw new MessageException("Database must be upgraded. Please browse " + server.getURL() + "/setup");
}
if (status != DatabaseVersion.Status.UP_TO_DATE) {
// Support other future values
- throw new BadDatabaseVersion("Unknown database status: " + status);
+ throw new MessageException("Unknown database status: " + status);
}
}
import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings;
import org.sonar.api.database.DatabaseProperties;
-import org.sonar.core.persistence.BadDatabaseVersion;
+import org.sonar.api.utils.MessageException;
import org.sonar.core.persistence.DatabaseVersion;
import static org.mockito.Mockito.mock;
public void shouldFailIfRequiresDowngrade() {
when(databaseVersion.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_DOWNGRADE);
- thrown.expect(BadDatabaseVersion.class);
+ thrown.expect(MessageException.class);
thrown.expectMessage("Database relates to a more recent version of SonarQube. Please check your settings (JDBC settings, version of Maven plugin)");
new DatabaseCompatibility(databaseVersion, server, settings).start();
public void shouldFailIfRequiresUpgrade() {
when(databaseVersion.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_UPGRADE);
- thrown.expect(BadDatabaseVersion.class);
+ thrown.expect(MessageException.class);
thrown.expectMessage("Database must be upgraded.");
new DatabaseCompatibility(databaseVersion, server, settings).start();
public void shouldFailIfNotSameServerId() throws Exception {
settings.setProperty(CoreProperties.SERVER_ID, "11111111");
- thrown.expect(BadDatabaseVersion.class);
+ thrown.expect(MessageException.class);
thrown.expectMessage("The current batch process and the configured remote server do not share the same DB configuration.");
thrown.expectMessage("- Batch side: jdbc:postgresql://localhost/foo (bar / *****)");
thrown.expectMessage("- Server side: check the configuration at http://localhost:9000/system");
settings.removeProperty(DatabaseProperties.PROP_USER);
- thrown.expect(BadDatabaseVersion.class);
+ thrown.expect(MessageException.class);
thrown.expectMessage("- Batch side: jdbc:postgresql://localhost/foo (sonar / *****)");
new DatabaseCompatibility(databaseVersion, server, settings).start();
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.utils;
+
+/**
+ * Runtime exception for "functional" errors. It aims to be displayed to end-users, without any technical information
+ * like stack traces.
+ *
+ * @since 4.0
+ */
+public class MessageException extends RuntimeException {
+
+ public MessageException(String s) {
+ super(s);
+ }
+
+ /**
+ * Does not fill in the stack trace
+ *
+ * @see java.lang.Throwable#fillInStackTrace()
+ */
+ @Override
+ public synchronized Throwable fillInStackTrace() {
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return getMessage();
+ }
+
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.utils;
+
+import org.junit.Test;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class MessageExceptionTest {
+
+ /**
+ * The exception should log only the message, without the "org.sonar.api.utils.MessageException" prefix
+ * and stack traces
+ */
+ @Test
+ public void should_not_print_stacktrace() throws Exception {
+ String message = "the message";
+ try {
+ throw new MessageException(message);
+
+ } catch (MessageException e) {
+ StringWriter writer = new StringWriter();
+ e.printStackTrace(new PrintWriter(writer));
+
+ assertThat(e.getStackTrace()).isEmpty();
+ assertThat(e.getMessage()).isEqualTo(message);
+ assertThat(writer.toString()).isEqualTo(message + System.getProperty("line.separator"));
+ }
+ }
+}
import org.slf4j.LoggerFactory;
import org.sonar.api.ServerComponent;
-import org.sonar.core.persistence.BadDatabaseVersion;
+import org.sonar.api.utils.MessageException;
import org.sonar.core.persistence.DatabaseVersion;
public class DatabaseServerCompatibility implements ServerComponent {
public void start() {
DatabaseVersion.Status status = version.getStatus();
if (status== DatabaseVersion.Status.REQUIRES_DOWNGRADE) {
- throw new BadDatabaseVersion("Database relates to a more recent version of sonar. Please check your settings.");
+ throw new MessageException("Database relates to a more recent version of sonar. Please check your settings.");
}
if (status== DatabaseVersion.Status.REQUIRES_UPGRADE) {
LoggerFactory.getLogger(DatabaseServerCompatibility.class).info("Database must be upgraded. Please browse /setup");
package org.sonar.server.platform;
import org.junit.Test;
-import org.sonar.core.persistence.BadDatabaseVersion;
+import org.sonar.api.utils.MessageException;
import org.sonar.core.persistence.DatabaseVersion;
import static org.mockito.Mockito.mock;
public class DatabaseServerCompatibilityTest {
- @Test(expected = BadDatabaseVersion.class)
+ @Test(expected = MessageException.class)
public void shouldFailIfRequiresDowngrade() {
DatabaseVersion version = mock(DatabaseVersion.class);
when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_DOWNGRADE);