aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2012-11-19 15:38:33 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2012-11-19 15:38:33 +0100
commitce0b16e02987e6938f5aeebcdedb726489f167ba (patch)
tree27d2fbd4f82ca47c3a4722c2658b14c8676d0261 /sonar-core
parent4b8b230dcb7dd04415c31f4ab9ab7db44e7f284e (diff)
parente4c6cb42bb5d10930408e5b5ab93a47dc6a4721a (diff)
downloadsonarqube-ce0b16e02987e6938f5aeebcdedb726489f167ba.tar.gz
sonarqube-ce0b16e02987e6938f5aeebcdedb726489f167ba.zip
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/pom.xml6
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/DefaultDatabase.java5
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java51
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/DefaultDatabaseTest.java58
-rw-r--r--sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java5
5 files changed, 98 insertions, 27 deletions
diff --git a/sonar-core/pom.xml b/sonar-core/pom.xml
index 108a800bdb2..ef84e57477b 100644
--- a/sonar-core/pom.xml
+++ b/sonar-core/pom.xml
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
@@ -172,6 +173,9 @@
<include>org/sonar/core/**/*DaoTest.java</include>
<include>org/sonar/core/measure/MeasureFilterExecutorTest.java</include>
</includes>
+ <systemPropertyVariables>
+ <orchestrator.configUrl>${orchestrator.configUrl}</orchestrator.configUrl>
+ </systemPropertyVariables>
</configuration>
</plugin>
</plugins>
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DefaultDatabase.java b/sonar-core/src/main/java/org/sonar/core/persistence/DefaultDatabase.java
index 0cf49601983..cff06153078 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/DefaultDatabase.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/DefaultDatabase.java
@@ -240,4 +240,9 @@ public class DefaultDatabase implements Database {
props.setProperty(key, defaultValue);
}
}
+
+ @Override
+ public String toString() {
+ return "Database[" + properties.getProperty("sonar.jdbc.url") + "]";
+ }
}
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java b/sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java
index 560cc8d8ea6..a11e3ef5102 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/AbstractDaoTestCase.java
@@ -22,46 +22,66 @@ package org.sonar.core.persistence;
import com.google.common.collect.Maps;
import com.google.common.io.Closeables;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.text.StrSubstitutor;
import org.dbunit.Assertion;
import org.dbunit.DataSourceDatabaseTester;
import org.dbunit.DatabaseUnitException;
import org.dbunit.IDatabaseTester;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.IDatabaseConnection;
-import org.dbunit.dataset.*;
+import org.dbunit.dataset.CompositeDataSet;
+import org.dbunit.dataset.DataSetException;
+import org.dbunit.dataset.IDataSet;
+import org.dbunit.dataset.ITable;
+import org.dbunit.dataset.ReplacementDataSet;
import org.dbunit.dataset.filter.DefaultColumnFilter;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.ext.mssql.InsertIdentityOperation;
import org.dbunit.operation.DatabaseOperation;
import org.junit.Assert;
import org.junit.Before;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.sonar.api.config.Settings;
import org.sonar.core.config.Logback;
+import java.io.IOException;
import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.SQLException;
+import java.util.Map;
+import java.util.Properties;
import static org.junit.Assert.fail;
public abstract class AbstractDaoTestCase {
+ private static Logger LOG = LoggerFactory.getLogger(AbstractDaoTestCase.class);
private static Database database;
private static DatabaseCommands databaseCommands;
private static IDatabaseTester databaseTester;
private static MyBatis myBatis;
@Before
- public void startDatabase() throws SQLException {
+ public void startDatabase() throws Exception {
if (database == null) {
Settings settings = new Settings().setProperties(Maps.fromProperties(System.getProperties()));
-
+ if (settings.hasKey("orchestrator.configUrl")) {
+ loadOrchestratorSettings(settings);
+ }
+ for (String key : settings.getKeysStartingWith("sonar.jdbc")) {
+ LOG.info(key + ": " + settings.getString(key));
+ }
boolean hasDialect = settings.hasKey("sonar.jdbc.dialect");
if (hasDialect) {
database = new DefaultDatabase(settings);
} else {
- database = new H2Database("sonarMyBatis");
+ database = new H2Database("h2Tests");
}
database.start();
+ LOG.info("Test Database: " + database);
databaseCommands = DatabaseCommands.forDialect(database.getDialect());
databaseTester = new DataSourceDatabaseTester(database.getDataSource());
@@ -73,6 +93,29 @@ public abstract class AbstractDaoTestCase {
databaseCommands.truncateDatabase(database.getDataSource());
}
+ private void loadOrchestratorSettings(Settings settings) throws URISyntaxException, IOException {
+ URI uri = new URI(settings.getString("orchestrator.configUrl"));
+ HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+ int responseCode = connection.getResponseCode();
+ if (responseCode >= 400) {
+ throw new IllegalStateException("Fail to request: " + uri + ". Status code=" + responseCode);
+ }
+
+ InputStream input = connection.getInputStream();
+ try {
+ Properties props = new Properties();
+ props.load(input);
+ settings.addProperties(props);
+ for (Map.Entry<String, String> entry : settings.getProperties().entrySet()) {
+ String interpolatedValue = StrSubstitutor.replace(entry.getValue(), System.getenv(), "${", "}");
+ settings.setProperty(entry.getKey(), interpolatedValue);
+ }
+
+ } finally {
+ IOUtils.closeQuietly(input);
+ }
+ }
+
protected MyBatis getMyBatis() {
return myBatis;
}
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DefaultDatabaseTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/DefaultDatabaseTest.java
index 64945262ef2..e2bc586cd88 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/DefaultDatabaseTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/DefaultDatabaseTest.java
@@ -20,7 +20,6 @@
package org.sonar.core.persistence;
import org.apache.commons.dbcp.BasicDataSource;
-import org.hamcrest.core.Is;
import org.junit.Test;
import org.sonar.api.config.Settings;
import org.sonar.core.persistence.dialect.Oracle;
@@ -28,8 +27,8 @@ import org.sonar.core.persistence.dialect.PostgreSql;
import java.util.Properties;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
+
public class DefaultDatabaseTest {
@@ -39,10 +38,11 @@ public class DefaultDatabaseTest {
db.initSettings();
Properties props = db.getProperties();
- assertThat(props.getProperty("sonar.jdbc.username"), Is.is("sonar"));
- assertThat(props.getProperty("sonar.jdbc.password"), Is.is("sonar"));
- assertThat(props.getProperty("sonar.jdbc.url"), Is.is("jdbc:h2:tcp://localhost/sonar"));
- assertThat(props.getProperty("sonar.jdbc.driverClassName"), Is.is("org.h2.Driver"));
+ assertThat(props.getProperty("sonar.jdbc.username")).isEqualTo("sonar");
+ assertThat(props.getProperty("sonar.jdbc.password")).isEqualTo("sonar");
+ assertThat(props.getProperty("sonar.jdbc.url")).isEqualTo("jdbc:h2:tcp://localhost/sonar");
+ assertThat(props.getProperty("sonar.jdbc.driverClassName")).isEqualTo("org.h2.Driver");
+ assertThat(db.toString()).isEqualTo("Database[jdbc:h2:tcp://localhost/sonar]");
}
@Test
@@ -55,8 +55,8 @@ public class DefaultDatabaseTest {
db.initSettings();
Properties props = db.getProperties();
- assertThat(props.getProperty("sonar.jdbc.username"), Is.is("me"));
- assertThat(props.getProperty("sonar.jdbc.driverClassName"), Is.is("my.Driver"));
+ assertThat(props.getProperty("sonar.jdbc.username")).isEqualTo("me");
+ assertThat(props.getProperty("sonar.jdbc.driverClassName")).isEqualTo("my.Driver");
}
@Test
@@ -68,9 +68,9 @@ public class DefaultDatabaseTest {
Properties commonsDbcpProps = DefaultDatabase.extractCommonsDbcpProperties(props);
- assertThat(commonsDbcpProps.getProperty("username"), Is.is("me"));
- assertThat(commonsDbcpProps.getProperty("driverClassName"), Is.is("my.Driver"));
- assertThat(commonsDbcpProps.getProperty("maxActive"), Is.is("5"));
+ assertThat(commonsDbcpProps.getProperty("username")).isEqualTo("me");
+ assertThat(commonsDbcpProps.getProperty("driverClassName")).isEqualTo("my.Driver");
+ assertThat(commonsDbcpProps.getProperty("maxActive")).isEqualTo("5");
}
@Test
@@ -87,7 +87,7 @@ public class DefaultDatabaseTest {
Properties props = db.getProperties();
- assertThat(props.getProperty("sonar.jdbc.maxActive"), Is.is("2"));
+ assertThat(props.getProperty("sonar.jdbc.maxActive")).isEqualTo("2");
}
@Test
@@ -103,8 +103,8 @@ public class DefaultDatabaseTest {
db.start();
db.stop();
- assertThat(db.getDialect().getId(), Is.is("h2"));
- assertThat(((BasicDataSource) db.getDataSource()).getMaxActive(), Is.is(1));
+ assertThat(db.getDialect().getId()).isEqualTo("h2");
+ assertThat(((BasicDataSource) db.getDataSource()).getMaxActive()).isEqualTo(1);
}
@Test
@@ -115,7 +115,7 @@ public class DefaultDatabaseTest {
DefaultDatabase database = new DefaultDatabase(settings);
database.initSettings();
- assertThat(database.getSchema(), Is.is("my_schema"));
+ assertThat(database.getSchema()).isEqualTo("my_schema");
}
@Test
@@ -127,7 +127,7 @@ public class DefaultDatabaseTest {
DefaultDatabase database = new DefaultDatabase(settings);
database.initSettings();
- assertThat(database.getSchema(), Is.is("my_schema"));
+ assertThat(database.getSchema()).isEqualTo("my_schema");
}
@Test
@@ -138,7 +138,7 @@ public class DefaultDatabaseTest {
DefaultDatabase database = new DefaultDatabase(settings);
database.initSettings();
- assertThat(database.getSchema(), nullValue());
+ assertThat(database.getSchema()).isNull();
}
@Test
@@ -150,7 +150,7 @@ public class DefaultDatabaseTest {
DefaultDatabase database = new DefaultDatabase(settings);
database.initSettings();
- assertThat(database.getSchema(), Is.is("my_schema"));
+ assertThat(database.getSchema()).isEqualTo("my_schema");
}
@Test
@@ -161,7 +161,7 @@ public class DefaultDatabaseTest {
DefaultDatabase database = new DefaultDatabase(settings);
database.initSettings();
- assertThat(database.getSchema(), nullValue());
+ assertThat(database.getSchema()).isNull();
}
@Test
@@ -172,7 +172,7 @@ public class DefaultDatabaseTest {
DefaultDatabase database = new DefaultDatabase(settings);
database.initSettings();
- assertThat(database.getDialect().getId(), Is.is(PostgreSql.ID));
+ assertThat(database.getDialect().getId()).isEqualTo(PostgreSql.ID);
}
@Test
@@ -183,6 +183,20 @@ public class DefaultDatabaseTest {
DefaultDatabase database = new DefaultDatabase(settings);
database.initSettings();
- assertThat(database.getProperties().getProperty("sonar.jdbc.driverClassName"), Is.is("org.postgresql.Driver"));
+ assertThat(database.getProperties().getProperty("sonar.jdbc.driverClassName")).isEqualTo("org.postgresql.Driver");
+ }
+
+ @Test
+ public void shouldSetHibernateProperties() {
+ Settings settings = new Settings();
+ settings.setProperty("sonar.jdbc.url", "jdbc:postgresql://localhost/sonar");
+ settings.setProperty("sonar.hibernate.default_schema", "foo");
+ DefaultDatabase database = new DefaultDatabase(settings);
+ database.initSettings();
+
+ Properties hibernateProps = database.getHibernateProperties();
+
+ assertThat(hibernateProps.getProperty("hibernate.hbm2ddl.auto")).isEqualTo("validate");
+ assertThat(hibernateProps.getProperty("hibernate.default_schema")).isEqualTo("foo");
}
}
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java b/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java
index 8b2431dc67c..b05cba2266b 100644
--- a/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java
+++ b/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java
@@ -104,4 +104,9 @@ public class H2Database implements Database {
properties.put(Environment.CONNECTION_PROVIDER, CustomHibernateConnectionProvider.class.getName());
return properties;
}
+
+ @Override
+ public String toString() {
+ return "H2 Database[" + name + "]";
+ }
}