From 2214f910aaf56b18a5c4ae09839390edea00c995 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 29 Jul 2014 09:14:52 +0200 Subject: [PATCH] SONAR-5487 fix quality flaw --- .../org/sonar/server/db/DatabaseChecker.java | 24 ++++++++++++++----- .../sonar/server/db/DatabaseCheckerTest.java | 4 +++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/DatabaseChecker.java b/server/sonar-server/src/main/java/org/sonar/server/db/DatabaseChecker.java index c37b192f919..1b3fc5aeedd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/DatabaseChecker.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/DatabaseChecker.java @@ -19,8 +19,10 @@ */ package org.sonar.server.db; +import com.google.common.base.Throwables; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.lang.StringUtils; +import org.picocontainer.Startable; import org.slf4j.LoggerFactory; import org.sonar.api.ServerComponent; import org.sonar.core.persistence.Database; @@ -30,7 +32,7 @@ import org.sonar.core.persistence.dialect.Oracle; import java.sql.Connection; import java.sql.SQLException; -public class DatabaseChecker implements ServerComponent { +public class DatabaseChecker implements ServerComponent, Startable { private final Database db; @@ -38,14 +40,24 @@ public class DatabaseChecker implements ServerComponent { this.db = db; } - public void start() throws SQLException { - if (H2.ID.equals(db.getDialect().getId())) { - LoggerFactory.getLogger(DatabaseChecker.class).warn("H2 database should be used for evaluation purpose only"); - } else if (Oracle.ID.equals(db.getDialect().getId())) { - checkOracleDriverVersion(); + @Override + public void start() { + try { + if (H2.ID.equals(db.getDialect().getId())) { + LoggerFactory.getLogger(DatabaseChecker.class).warn("H2 database should be used for evaluation purpose only"); + } else if (Oracle.ID.equals(db.getDialect().getId())) { + checkOracleDriverVersion(); + } + } catch (Exception e) { + Throwables.propagate(e); } } + @Override + public void stop() { + // nothing to do + } + private void checkOracleDriverVersion() throws SQLException { Connection connection = db.getDataSource().getConnection(); try { diff --git a/server/sonar-server/src/test/java/org/sonar/server/db/DatabaseCheckerTest.java b/server/sonar-server/src/test/java/org/sonar/server/db/DatabaseCheckerTest.java index 9baaac36dd3..c4c5844e68e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/db/DatabaseCheckerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/db/DatabaseCheckerTest.java @@ -62,7 +62,9 @@ public class DatabaseCheckerTest { @Test public void log_warning_if_h2() throws Exception { Database db = mockDb(new H2(), "13.4"); - new DatabaseChecker(db).start(); + DatabaseChecker checker = new DatabaseChecker(db); + checker.start(); + checker.stop(); // TODO test log } -- 2.39.5