diff options
Diffstat (limited to 'sonar-core')
19 files changed, 140 insertions, 500 deletions
diff --git a/sonar-core/pom.xml b/sonar-core/pom.xml index 9870817f428..e1aff5e0bb8 100644 --- a/sonar-core/pom.xml +++ b/sonar-core/pom.xml @@ -42,18 +42,6 @@ <artifactId>mybatis</artifactId> </dependency> <dependency> - <groupId>org.apache.derby</groupId> - <artifactId>derby</artifactId> - </dependency> - <dependency> - <groupId>org.apache.derby</groupId> - <artifactId>derbyclient</artifactId> - </dependency> - <dependency> - <groupId>org.apache.derby</groupId> - <artifactId>derbynet</artifactId> - </dependency> - <dependency> <groupId>org.codehaus.sonar</groupId> <artifactId>sonar-update-center-common</artifactId> </dependency> @@ -160,6 +148,11 @@ <artifactId>jtds</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + <scope>test</scope> + </dependency> </dependencies> <profiles> diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java index 32b7ba37697..66521373392 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java @@ -26,7 +26,7 @@ import org.sonar.api.ServerComponent; import java.sql.Connection; /** - * Restore schema by executing DDL scripts. Only Derby database is supported. + * Restore schema by executing DDL scripts. Only H2 database is supported. * Other databases are created by Ruby on Rails migrations. * * @since 2.12 diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DdlUtils.java b/sonar-core/src/main/java/org/sonar/core/persistence/DdlUtils.java index 19e145e502c..530103614c3 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DdlUtils.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DdlUtils.java @@ -37,7 +37,7 @@ public final class DdlUtils { } public static boolean supportsDialect(String dialect) { - return "derby".equals(dialect); + return "h2".equals(dialect); } /** 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 c94d883e88c..2d6cb1cbec9 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 @@ -27,10 +27,15 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.config.Settings; import org.sonar.api.database.DatabaseProperties; -import org.sonar.core.persistence.dialect.*; +import org.sonar.core.persistence.dialect.Dialect; +import org.sonar.core.persistence.dialect.DialectUtils; +import org.sonar.core.persistence.dialect.H2; +import org.sonar.core.persistence.dialect.Oracle; +import org.sonar.core.persistence.dialect.PostgreSql; import org.sonar.jpa.session.CustomHibernateConnectionProvider; import javax.sql.DataSource; + import java.sql.SQLException; import java.util.Arrays; import java.util.List; @@ -84,8 +89,8 @@ public class DefaultDatabase implements Database { if (dialect == null) { throw new IllegalStateException("Can not guess the JDBC dialect. Please check the property sonar.jdbc.url."); } - if (Derby.ID.equals(dialect.getId())) { - LoggerFactory.getLogger(DefaultDatabase.class).warn("Derby database should be used for evaluation purpose only"); + if (H2.ID.equals(dialect.getId())) { + LoggerFactory.getLogger(DefaultDatabase.class).warn("H2 database should be used for evaluation purpose only"); } if (!properties.containsKey("sonar.jdbc.driverClassName")) { properties.setProperty("sonar.jdbc.driverClassName", dialect.getDefaultDriverClassName()); @@ -107,7 +112,7 @@ public class DefaultDatabase implements Database { } } - private void initDatasource() throws Exception {//NOSONAR this exception is thrown by BasicDataSourceFactory + private void initDatasource() throws Exception {// NOSONAR this exception is thrown by BasicDataSourceFactory // but it's correctly caught by start() LOG.info("Create JDBC datasource"); diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/DialectUtils.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/DialectUtils.java index 6618d536064..3a5f6821b97 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/DialectUtils.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/DialectUtils.java @@ -32,7 +32,7 @@ public final class DialectUtils { private DialectUtils() { } - private static final Dialect[] DIALECTS = new Dialect[]{new Derby(), new MySql(), new Oracle(), new PostgreSql(), new MsSql()}; + private static final Dialect[] DIALECTS = new Dialect[] {new H2(), new MySql(), new Oracle(), new PostgreSql(), new MsSql()}; public static Dialect find(final String dialectId, final String jdbcConnectionUrl) { Dialect match = StringUtils.isNotBlank(dialectId) ? findById(dialectId) : findByJdbcUrl(jdbcConnectionUrl); diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Derby.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/H2.java index e35d27ae8a9..00879be648b 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Derby.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/H2.java @@ -20,25 +20,21 @@ package org.sonar.core.persistence.dialect; import org.apache.commons.lang.StringUtils; -import org.hibernate.dialect.DerbyDialect; -import org.hibernate.id.IdentityGenerator; -import org.sonar.api.database.DatabaseProperties; - -import java.sql.Types; +import org.hibernate.dialect.H2Dialect; /** * @since 1.12 */ -public class Derby implements Dialect { +public class H2 implements Dialect { - public static final String ID = "derby"; + public static final String ID = "h2"; public String getId() { return ID; } public String getActiveRecordDialectCode() { - return "derby"; + return "h2"; } public String getActiveRecordJdbcAdapter() { @@ -46,51 +42,21 @@ public class Derby implements Dialect { } public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() { - return DerbyWithDecimalDialect.class; + return H2Dialect.class; } public boolean matchesJdbcURL(String jdbcConnectionURL) { - return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:derby:"); + return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:h2:"); } public String getDefaultDriverClassName() { - return "org.apache.derby.jdbc.ClientDriver"; + return "org.h2.Driver"; } public String getConnectionInitStatement(String schema) { return null; } - public static class DerbyWithDecimalDialect extends DerbyDialect { - public DerbyWithDecimalDialect() { - super(); - registerColumnType(Types.DOUBLE, "decimal"); - registerColumnType(Types.VARCHAR, DatabaseProperties.MAX_TEXT_SIZE, "clob"); - registerColumnType(Types.VARBINARY, "blob"); - - // Not possible to do alter column types in Derby - registerColumnType(Types.BIGINT, "integer"); - - registerColumnType(Types.BIT, "boolean"); - } - - @Override - public String toBooleanValueString(boolean bool) { - return bool ? "true" : "false"; - } - - /** - * To be compliant with Oracle, we define on each model (ch.hortis.sonar.model classes) - * a sequence generator. It works on mySQL because strategy = GenerationType.AUTO, so - * it equals GenerationType.IDENTITY. - * But on derby, AUTO becomes TABLE instead of IDENTITY. So we explicitly change this behavior. - */ - @Override - public Class getNativeIdentifierGeneratorClass() { - return IdentityGenerator.class; - } - } - public String getTrueSqlValue() { return "true"; } diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql index 681838d3d8b..681838d3d8b 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-derby.ddl b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl index 917688365a1..b597ddde8f4 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-derby.ddl +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl @@ -1,6 +1,6 @@ -- Structure of Sonar Database --- Initially this file has been generated from existing Derby DB +-- Initially this file has been generated from existing H2 DB -- Should be updated manually, during addition of new migrations CREATE TABLE "QUALITY_MODELS" ( @@ -24,13 +24,13 @@ CREATE TABLE "CRITERIA" ( "FAMILY" VARCHAR(100), "KEE" VARCHAR(100), "OPERATOR" VARCHAR(20), - "VALUE" DECIMAL(30,20), + "VALUE" DOUBLE, "TEXT_VALUE" VARCHAR(256), "VARIATION" BOOLEAN ); CREATE TABLE "DEPENDENCIES" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "FROM_SNAPSHOT_ID" INTEGER, "FROM_RESOURCE_ID" INTEGER, "TO_SNAPSHOT_ID" INTEGER, @@ -102,9 +102,9 @@ CREATE TABLE "FILTER_COLUMNS" ( CREATE TABLE "MEASURE_DATA" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "MEASURE_ID" INTEGER, + "MEASURE_ID" BIGINT, "SNAPSHOT_ID" INTEGER, - "DATA" BLOB(2147483647) + "DATA" BINARY(167772150) ); CREATE TABLE "GROUPS" ( @@ -171,7 +171,7 @@ CREATE TABLE "RULES" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "PLUGIN_RULE_KEY" VARCHAR(200) NOT NULL, "PLUGIN_NAME" VARCHAR(255) NOT NULL, - "DESCRIPTION" CLOB(2147483647), + "DESCRIPTION" VARCHAR(16777215), "PRIORITY" INTEGER, "ENABLED" BOOLEAN, "CARDINALITY" VARCHAR(10), @@ -238,7 +238,7 @@ CREATE TABLE "REVIEW_COMMENTS" ( "UPDATED_AT" TIMESTAMP, "REVIEW_ID" INTEGER, "USER_ID" INTEGER, - "REVIEW_TEXT" CLOB(2147483647) + "REVIEW_TEXT" VARCHAR(16777215) ); CREATE TABLE "ACTIVE_RULE_CHANGES" ( @@ -254,8 +254,8 @@ CREATE TABLE "ACTIVE_RULE_CHANGES" ( ); CREATE TABLE "PROJECT_MEASURES" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "VALUE" DECIMAL(30,20), + "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "VALUE" DOUBLE, "METRIC_ID" INTEGER NOT NULL, "SNAPSHOT_ID" INTEGER, "RULE_ID" INTEGER, @@ -271,11 +271,11 @@ CREATE TABLE "PROJECT_MEASURES" ( "RULE_PRIORITY" INTEGER, "CHARACTERISTIC_ID" INTEGER, "PERSON_ID" INTEGER, - "VARIATION_VALUE_1" DECIMAL(30,20), - "VARIATION_VALUE_2" DECIMAL(30,20), - "VARIATION_VALUE_3" DECIMAL(30,20), - "VARIATION_VALUE_4" DECIMAL(30,20), - "VARIATION_VALUE_5" DECIMAL(30,20) + "VARIATION_VALUE_1" DOUBLE, + "VARIATION_VALUE_2" DOUBLE, + "VARIATION_VALUE_3" DOUBLE, + "VARIATION_VALUE_4" DOUBLE, + "VARIATION_VALUE_5" DOUBLE ); CREATE TABLE "SNAPSHOT_SOURCES" ( @@ -321,10 +321,10 @@ CREATE TABLE "REVIEWS" ( ); CREATE TABLE "MANUAL_MEASURES" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "METRIC_ID" INTEGER NOT NULL, "RESOURCE_ID" INTEGER, - "VALUE" DECIMAL(30,20), + "VALUE" DOUBLE, "TEXT_VALUE" VARCHAR(4000), "USER_LOGIN" VARCHAR(40), "DESCRIPTION" VARCHAR(4000), @@ -343,7 +343,7 @@ CREATE TABLE "ACTIVE_RULES" ( CREATE TABLE "NOTIFICATIONS" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "CREATED_AT" TIMESTAMP, - "DATA" BLOB(2147483647) + "DATA" BINARY(167772150) ); CREATE TABLE "USER_ROLES" ( @@ -371,7 +371,7 @@ CREATE TABLE "CHARACTERISTIC_PROPERTIES" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "CHARACTERISTIC_ID" INTEGER, "KEE" VARCHAR(100), - "VALUE" DECIMAL(30,20), + "VALUE" DOUBLE, "TEXT_VALUE" VARCHAR(4000) ); @@ -421,7 +421,7 @@ CREATE TABLE "RULE_FAILURES" ( "FAILURE_LEVEL" INTEGER NOT NULL, "MESSAGE" VARCHAR(4000), "LINE" INTEGER, - "COST" DECIMAL(30,20), + "COST" DOUBLE, "CREATED_AT" TIMESTAMP, "CHECKSUM" VARCHAR(1000), "PERMANENT_ID" INTEGER, @@ -441,8 +441,8 @@ CREATE TABLE "METRICS" ( "USER_MANAGED" BOOLEAN DEFAULT FALSE, "ENABLED" BOOLEAN DEFAULT TRUE, "ORIGIN" VARCHAR(3), - "WORST_VALUE" DECIMAL(30,20), - "BEST_VALUE" DECIMAL(30,20), + "WORST_VALUE" DOUBLE, + "BEST_VALUE" DOUBLE, "OPTIMIZED_BEST_VALUE" BOOLEAN, "HIDDEN" BOOLEAN, "DELETE_HISTORICAL_DATA" BOOLEAN @@ -486,7 +486,7 @@ CREATE TABLE "ACTIVE_RULE_NOTES" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "ACTIVE_RULE_ID" INTEGER, "USER_LOGIN" VARCHAR(40), - "DATA" BLOB(2147483647), + "DATA" BINARY(167772150), "CREATED_AT" TIMESTAMP, "UPDATED_AT" TIMESTAMP ); @@ -495,7 +495,7 @@ CREATE TABLE "RULE_NOTES" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "RULE_ID" INTEGER, "USER_LOGIN" VARCHAR(40), - "DATA" BLOB(2147483647), + "DATA" BINARY(167772150), "CREATED_AT" TIMESTAMP, "UPDATED_AT" TIMESTAMP ); @@ -507,194 +507,3 @@ CREATE TABLE "AUTHORS" ( "CREATED_AT" TIMESTAMP, "UPDATED_AT" TIMESTAMP ); - - --- ---------------------------------------------- --- DDL Statements for indexes --- ---------------------------------------------- - -CREATE INDEX "GROUP_ROLES_RESOURCE" ON "GROUP_ROLES" ("RESOURCE_ID"); - -CREATE INDEX "GROUP_ROLES_GROUP" ON "GROUP_ROLES" ("GROUP_ID"); - -CREATE INDEX "USER_ROLES_RESOURCE" ON "USER_ROLES" ("RESOURCE_ID"); - -CREATE INDEX "USER_ROLES_USER" ON "USER_ROLES" ("USER_ID"); - -CREATE INDEX "DUPLICATIONS_INDEX_HASH" ON "DUPLICATIONS_INDEX" ("HASH"); - -CREATE INDEX "DUPLICATIONS_INDEX_SID" ON "DUPLICATIONS_INDEX" ("SNAPSHOT_ID"); - -CREATE INDEX "DUPLICATIONS_INDEX_PSID" ON "DUPLICATIONS_INDEX" ("PROJECT_SNAPSHOT_ID"); - -CREATE INDEX "SNAP_SOURCES_SNAPSHOT_ID" ON "SNAPSHOT_SOURCES" ("SNAPSHOT_ID"); - -CREATE INDEX "INDEX_GROUPS_USERS_ON_GROUP_ID" ON "GROUPS_USERS" ("GROUP_ID"); - -CREATE INDEX "INDEX_GROUPS_USERS_ON_USER_ID" ON "GROUPS_USERS" ("USER_ID"); - -CREATE INDEX "DEPS_TO_SID" ON "DEPENDENCIES" ("TO_SNAPSHOT_ID"); - -CREATE INDEX "DEPS_FROM_SID" ON "DEPENDENCIES" ("FROM_SNAPSHOT_ID"); - -CREATE INDEX "DEPS_PRJ_SID" ON "DEPENDENCIES" ("PROJECT_SNAPSHOT_ID"); - -CREATE INDEX "MEASURES_SID_METRIC" ON "PROJECT_MEASURES" ("SNAPSHOT_ID", "METRIC_ID"); - -CREATE INDEX "ACTIVE_RULE_CHANGES_PID" ON "ACTIVE_RULE_CHANGES" ("PROFILE_ID"); - -CREATE INDEX "CHARACTERISTIC_PROPERTIES_CID" ON "CHARACTERISTIC_PROPERTIES" ("CHARACTERISTIC_ID"); - -CREATE UNIQUE INDEX "METRICS_UNIQUE_NAME" ON "METRICS" ("NAME"); - -CREATE INDEX "ACTIVE_RULE_PARAM_CHANGES_CID" ON "ACTIVE_RULE_PARAM_CHANGES" ("ACTIVE_RULE_CHANGE_ID"); - -CREATE INDEX "M_DATA_SID" ON "MEASURE_DATA" ("SNAPSHOT_ID"); - -CREATE INDEX "MEASURE_DATA_MEASURE_ID" ON "MEASURE_DATA" ("MEASURE_ID"); - -CREATE INDEX "RF_PERMANENT_ID" ON "RULE_FAILURES" ("PERMANENT_ID"); - -CREATE INDEX "RULE_FAILURE_RULE_ID" ON "RULE_FAILURES" ("RULE_ID"); - -CREATE INDEX "RULE_FAILURE_SNAPSHOT_ID" ON "RULE_FAILURES" ("SNAPSHOT_ID"); - -CREATE INDEX "EVENTS_SNAPSHOT_ID" ON "EVENTS" ("SNAPSHOT_ID"); - -CREATE INDEX "EVENTS_RESOURCE_ID" ON "EVENTS" ("RESOURCE_ID"); - -CREATE INDEX "WIDGETS_WIDGETKEY" ON "WIDGETS" ("WIDGET_KEY"); - -CREATE INDEX "WIDGETS_DASHBOARDS" ON "WIDGETS" ("DASHBOARD_ID"); - -CREATE INDEX "SNAPSHOTS_QUALIFIER" ON "SNAPSHOTS" ("QUALIFIER"); - -CREATE INDEX "SNAPSHOTS_ROOT" ON "SNAPSHOTS" ("ROOT_SNAPSHOT_ID"); - -CREATE INDEX "SNAPSHOTS_PARENT" ON "SNAPSHOTS" ("PARENT_SNAPSHOT_ID"); - -CREATE INDEX "SNAPSHOT_PROJECT_ID" ON "SNAPSHOTS" ("PROJECT_ID"); - -CREATE INDEX "RULES_PARAMETERS_RULE_ID" ON "RULES_PARAMETERS" ("RULE_ID"); - -CREATE INDEX "ACTIVE_DASHBOARDS_DASHBOARDID" ON "ACTIVE_DASHBOARDS" ("DASHBOARD_ID"); - -CREATE INDEX "ACTIVE_DASHBOARDS_USERID" ON "ACTIVE_DASHBOARDS" ("USER_ID"); - -CREATE UNIQUE INDEX "UNIQUE_SCHEMA_MIGRATIONS" ON "SCHEMA_MIGRATIONS" ("VERSION"); - -CREATE INDEX "WIDGET_PROPERTIES_WIDGETS" ON "WIDGET_PROPERTIES" ("WIDGET_ID"); - -CREATE INDEX "PROPERTIES_KEY" ON "PROPERTIES" ("PROP_KEY"); - -CREATE INDEX "MANUAL_MEASURES_RESOURCE_ID" ON "MANUAL_MEASURES" ("RESOURCE_ID"); - -CREATE INDEX "PROJECTS_KEE" ON "PROJECTS" ("KEE"); - -CREATE INDEX "RESOURCE_INDEX_KEE" ON "RESOURCE_INDEX" ("KEE"); - -CREATE INDEX "RESOURCE_INDEX_RID" ON "RESOURCE_INDEX" ("RESOURCE_ID"); - -CREATE INDEX "INDEX_ACTION_PLANS_ON_PROJET_ID" ON "ACTION_PLANS" ("PROJECT_ID"); - -CREATE INDEX "INDEX_ACTION_PLANS_REVIEWS_ON_ACTION_PLAN_ID" ON "ACTION_PLANS_REVIEWS" ("ACTION_PLAN_ID"); - -CREATE INDEX "INDEX_ACTION_PLANS_REVIEWS_ON_REVIEW_ID" ON "ACTION_PLANS_REVIEWS" ("REVIEW_ID"); - -CREATE INDEX "INDEX_ACTIVE_RULE_NOTES_ON_ACTIVE_RULE_ID" ON "ACTIVE_RULE_NOTES" ("ACTIVE_RULE_ID"); - -CREATE INDEX "INDEX_RULE_NOTES_ON_ACTIVE_RULE_ID" ON "RULE_NOTES" ("RULE_ID"); - -CREATE INDEX "REVIEWS_RID" ON "REVIEWS" ("RESOURCE_ID"); - - --- ---------------------------------------------- --- DDL Statements for keys --- ---------------------------------------------- - --- primary/unique -ALTER TABLE "GROUP_ROLES" ADD CONSTRAINT "SQL110927104437910" PRIMARY KEY ("ID"); - -ALTER TABLE "REVIEWS" ADD CONSTRAINT "SQL110927104440700" PRIMARY KEY ("ID"); - -ALTER TABLE "RULES" ADD CONSTRAINT "SQL110927104437080" PRIMARY KEY ("ID"); - -ALTER TABLE "USER_ROLES" ADD CONSTRAINT "SQL110927104437940" PRIMARY KEY ("ID"); - -ALTER TABLE "SNAPSHOT_SOURCES" ADD CONSTRAINT "SQL110927104437590" PRIMARY KEY ("ID"); - -ALTER TABLE "NOTIFICATIONS" ADD CONSTRAINT "SQL110927104441030" PRIMARY KEY ("ID"); - -ALTER TABLE "DEPENDENCIES" ADD CONSTRAINT "SQL110927104438330" PRIMARY KEY ("ID"); - -ALTER TABLE "PROJECT_MEASURES" ADD CONSTRAINT "SQL110927104437040" PRIMARY KEY ("ID"); - -ALTER TABLE "ACTIVE_RULE_CHANGES" ADD CONSTRAINT "SQL110927104440770" PRIMARY KEY ("ID"); - -ALTER TABLE "CHARACTERISTIC_PROPERTIES" ADD CONSTRAINT "SQL110927104439660" PRIMARY KEY ("ID"); - -ALTER TABLE "QUALITY_MODELS" ADD CONSTRAINT "SQL110927104439440" PRIMARY KEY ("ID"); - -ALTER TABLE "USERS" ADD CONSTRAINT "SQL110927104437310" PRIMARY KEY ("ID"); - -ALTER TABLE "CRITERIA" ADD CONSTRAINT "SQL110927104438720" PRIMARY KEY ("ID"); - -ALTER TABLE "METRICS" ADD CONSTRAINT "SQL110927104436990" PRIMARY KEY ("ID"); - -ALTER TABLE "ACTIVE_RULE_PARAM_CHANGES" ADD CONSTRAINT "SQL110927104440790" PRIMARY KEY ("ID"); - -ALTER TABLE "MEASURE_DATA" ADD CONSTRAINT "SQL110927104437810" PRIMARY KEY ("ID"); - -ALTER TABLE "RULE_FAILURES" ADD CONSTRAINT "SQL110927104437100" PRIMARY KEY ("ID"); - -ALTER TABLE "EVENTS" ADD CONSTRAINT "SQL110927104437690" PRIMARY KEY ("ID"); - -ALTER TABLE "WIDGETS" ADD CONSTRAINT "SQL110927104439750" PRIMARY KEY ("ID"); - -ALTER TABLE "SNAPSHOTS" ADD CONSTRAINT "SQL110927104436960" PRIMARY KEY ("ID"); - -ALTER TABLE "ACTIVE_RULES" ADD CONSTRAINT "SQL110927104437550" PRIMARY KEY ("ID"); - -ALTER TABLE "CHARACTERISTICS" ADD CONSTRAINT "SQL110927104439450" PRIMARY KEY ("ID"); - -ALTER TABLE "RULES_PARAMETERS" ADD CONSTRAINT "SQL110927104437130" PRIMARY KEY ("ID"); - -ALTER TABLE "ACTIVE_DASHBOARDS" ADD CONSTRAINT "SQL110927104439710" PRIMARY KEY ("ID"); - -ALTER TABLE "FILTER_COLUMNS" ADD CONSTRAINT "SQL110927104438710" PRIMARY KEY ("ID"); - -ALTER TABLE "REVIEW_COMMENTS" ADD CONSTRAINT "SQL110927104440710" PRIMARY KEY ("ID"); - -ALTER TABLE "WIDGET_PROPERTIES" ADD CONSTRAINT "SQL110927104439770" PRIMARY KEY ("ID"); - -ALTER TABLE "PROPERTIES" ADD CONSTRAINT "SQL110927104437750" PRIMARY KEY ("ID"); - -ALTER TABLE "DASHBOARDS" ADD CONSTRAINT "SQL110927104439740" PRIMARY KEY ("ID"); - -ALTER TABLE "GROUPS" ADD CONSTRAINT "SQL110927104437850" PRIMARY KEY ("ID"); - -ALTER TABLE "PROJECT_LINKS" ADD CONSTRAINT "SQL110927104437150" PRIMARY KEY ("ID"); - -ALTER TABLE "FILTERS" ADD CONSTRAINT "SQL110927104438690" PRIMARY KEY ("ID"); - -ALTER TABLE "MANUAL_MEASURES" ADD CONSTRAINT "SQL110927104440930" PRIMARY KEY ("ID"); - -ALTER TABLE "ALERTS" ADD CONSTRAINT "SQL110927104437730" PRIMARY KEY ("ID"); - -ALTER TABLE "PROJECTS" ADD CONSTRAINT "SQL110927104436930" PRIMARY KEY ("ID"); - -ALTER TABLE "RULES_PROFILES" ADD CONSTRAINT "SQL110927104437540" PRIMARY KEY ("ID"); - -ALTER TABLE "ACTIVE_RULE_PARAMETERS" ADD CONSTRAINT "SQL110927104437560" PRIMARY KEY ("ID"); - -ALTER TABLE "LOADED_TEMPLATES" ADD CONSTRAINT "SQL110927104437650" PRIMARY KEY ("ID"); - -ALTER TABLE "ACTION_PLANS" ADD CONSTRAINT "SQL110927104447650" PRIMARY KEY ("ID"); - -ALTER TABLE "ACTIVE_RULE_NOTES" ADD CONSTRAINT "SQL110927104847650" PRIMARY KEY ("ID"); - -ALTER TABLE "RULE_NOTES" ADD PRIMARY KEY ("ID"); - -ALTER TABLE "AUTHORS" ADD PRIMARY KEY ("ID"); - -ALTER TABLE "RESOURCE_INDEX" ADD PRIMARY KEY ("ID"); diff --git a/sonar-core/src/test/java/org/sonar/core/config/ConfigurationUtilsTest.java b/sonar-core/src/test/java/org/sonar/core/config/ConfigurationUtilsTest.java index 6707b019125..fa6d490276f 100644 --- a/sonar-core/src/test/java/org/sonar/core/config/ConfigurationUtilsTest.java +++ b/sonar-core/src/test/java/org/sonar/core/config/ConfigurationUtilsTest.java @@ -36,13 +36,13 @@ public class ConfigurationUtilsTest { input.setProperty("url", "${env:SONAR_JDBC_URL}"); input.setProperty("do_not_change", "${SONAR_JDBC_URL}"); Map<String, String> variables = Maps.newHashMap(); - variables.put("SONAR_JDBC_URL", "jdbc:derby:mem"); + variables.put("SONAR_JDBC_URL", "jdbc:h2:mem"); Properties output = ConfigurationUtils.interpolateVariables(input, variables); assertThat(output.size(), is(3)); assertThat(output.getProperty("hello"), is("world")); - assertThat(output.getProperty("url"), is("jdbc:derby:mem")); + assertThat(output.getProperty("url"), is("jdbc:h2:mem")); assertThat(output.getProperty("do_not_change"), is("${SONAR_JDBC_URL}")); // input is not changed diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DaoTestCase.java b/sonar-core/src/test/java/org/sonar/core/persistence/DaoTestCase.java index bf868da93c5..bd26337937a 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/DaoTestCase.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/DaoTestCase.java @@ -60,17 +60,21 @@ public abstract class DaoTestCase { public static void startDatabase() throws Exception { Settings settings = new Settings(); settings.setProperties(Maps.fromProperties(System.getProperties())); - if (settings.hasKey("sonar.jdbc.dialect")) { - database = new DefaultDatabase(settings); - } else { - database = new InMemoryDatabase(); - } - database.start(); + boolean hasDialect = settings.hasKey("sonar.jdbc.dialect"); + + if ((null == database) || (hasDialect)) { // Create database only once per vm (Only for in mempry database) + if (hasDialect) { + database = new DefaultDatabase(settings); + } else { + database = new H2Database(); + } + database.start(); - myBatis = new MyBatis(database); - myBatis.start(); + myBatis = new MyBatis(database); + myBatis.start(); - databaseCommands = DatabaseCommands.forDialect(database.getDialect()); + databaseCommands = DatabaseCommands.forDialect(database.getDialect()); + } } @Before @@ -145,7 +149,7 @@ public abstract class DaoTestCase { } protected final void checkTables(String testName, String... tables) { - checkTables(testName, new String[]{}, tables); + checkTables(testName, new String[] {}, tables); } protected final void checkTables(String testName, String[] excludedColumnNames, String... tables) { diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseCommands.java b/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseCommands.java index 521d1aaf70f..0eb249ab34a 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseCommands.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/DatabaseCommands.java @@ -20,15 +20,19 @@ package org.sonar.core.persistence; import org.apache.commons.lang.StringUtils; -import org.dbunit.dataset.datatype.DefaultDataTypeFactory; import org.dbunit.dataset.datatype.IDataTypeFactory; +import org.dbunit.ext.h2.H2DataTypeFactory; import org.dbunit.ext.mssql.InsertIdentityOperation; import org.dbunit.ext.mssql.MsSqlDataTypeFactory; import org.dbunit.ext.mysql.MySqlDataTypeFactory; import org.dbunit.ext.oracle.Oracle10DataTypeFactory; import org.dbunit.ext.postgresql.PostgresqlDataTypeFactory; import org.dbunit.operation.DatabaseOperation; -import org.sonar.core.persistence.dialect.*; +import org.sonar.core.persistence.dialect.Dialect; +import org.sonar.core.persistence.dialect.MsSql; +import org.sonar.core.persistence.dialect.MySql; +import org.sonar.core.persistence.dialect.Oracle; +import org.sonar.core.persistence.dialect.PostgreSql; import java.sql.Connection; import java.sql.SQLException; @@ -64,7 +68,7 @@ public abstract class DatabaseCommands { return DatabaseOperation.CLEAN_INSERT; } - static final DatabaseCommands DERBY = new DatabaseCommands(new DefaultDataTypeFactory()) { + static final DatabaseCommands H2 = new DatabaseCommands(new H2DataTypeFactory()) { @Override public String truncate(String table) { return "TRUNCATE TABLE " + table; @@ -117,7 +121,7 @@ public abstract class DatabaseCommands { return Arrays.asList( "DROP SEQUENCE " + sequence, "CREATE SEQUENCE " + sequence + " INCREMENT BY 1 MINVALUE 1 START WITH 1" - ); + ); } @Override @@ -143,10 +147,9 @@ public abstract class DatabaseCommands { } }; - public static DatabaseCommands forDialect(Dialect dialect) { - if (Derby.ID.equals(dialect.getId())) { - return DERBY; + if (org.sonar.core.persistence.dialect.H2.ID.equals(dialect.getId())) { + return H2; } if (MsSql.ID.equals(dialect.getId())) { return MSSQL; diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DdlUtilsTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/DdlUtilsTest.java index b00dc8dfc28..a2a6887ab95 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/DdlUtilsTest.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/DdlUtilsTest.java @@ -19,8 +19,7 @@ */ package org.sonar.core.persistence; -import org.apache.derby.jdbc.EmbeddedDriver; -import org.hamcrest.core.Is; +import org.h2.Driver; import org.junit.Test; import java.sql.Connection; @@ -28,39 +27,37 @@ import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; -import static org.hamcrest.Matchers.greaterThan; -import static org.junit.Assert.assertThat; +import static org.fest.assertions.Assertions.assertThat; public class DdlUtilsTest { - static { - DerbyUtils.fixDerbyLogs(); - } - @Test - public void shouldSupportOnlyDerby() { - assertThat(DdlUtils.supportsDialect("derby"), Is.is(true)); - assertThat(DdlUtils.supportsDialect("mysql"), Is.is(false)); - assertThat(DdlUtils.supportsDialect("oracle"), Is.is(false)); - assertThat(DdlUtils.supportsDialect("mssql"), Is.is(false)); + public void shouldSupportOnlyH2() { + assertThat(DdlUtils.supportsDialect("h2")).isTrue(); + assertThat(DdlUtils.supportsDialect("mysql")).isFalse(); + assertThat(DdlUtils.supportsDialect("oracle")).isFalse(); + assertThat(DdlUtils.supportsDialect("mssql")).isFalse(); } @Test - public void shouldCreateDerbySchema() throws SQLException { - int tables = 0; - DriverManager.registerDriver(new EmbeddedDriver()); - Connection connection = DriverManager.getConnection("jdbc:derby:memory:sonar;create=true"); - DdlUtils.createSchema(connection, "derby"); + public void shouldCreateSchema() throws SQLException { + DriverManager.registerDriver(new Driver()); + Connection connection = DriverManager.getConnection("jdbc:h2:mem:sonar_test"); + DdlUtils.createSchema(connection, "h2"); + + int tables = countTables(connection); + + connection.close(); + assertThat(tables).isGreaterThan(30); + } - ResultSet resultSet = connection.getMetaData().getTables("", null, null, new String[]{"TABLE"}); + private int countTables(Connection connection) throws SQLException { + int count = 0; + ResultSet resultSet = connection.getMetaData().getTables(null, null, null, new String[] {"TABLE"}); while (resultSet.next()) { - tables++; + count++; } resultSet.close(); - connection.commit(); - connection.close(); - assertThat(tables, greaterThan(30)); - - DerbyUtils.dropInMemoryDatabase(); + return count; } } 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 048c5d3df49..24723714c4e 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 @@ -33,10 +33,6 @@ import static org.junit.Assert.assertThat; public class DefaultDatabaseTest { - static { - DerbyUtils.fixDerbyLogs(); - } - @Test public void shouldLoadDefaultValues() { DefaultDatabase db = new DefaultDatabase(new Settings()); @@ -45,8 +41,8 @@ public class DefaultDatabaseTest { 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:derby://localhost:1527/sonar")); - assertThat(props.getProperty("sonar.jdbc.driverClassName"), Is.is("org.apache.derby.jdbc.ClientDriver")); + assertThat(props.getProperty("sonar.jdbc.url"), Is.is("jdbc:h2:tcp://localhost/sonar")); + assertThat(props.getProperty("sonar.jdbc.driverClassName"), Is.is("org.h2.Driver")); } @Test @@ -97,21 +93,17 @@ public class DefaultDatabaseTest { @Test public void shouldStart() { Settings settings = new Settings(); - settings.setProperty("sonar.jdbc.url", "jdbc:derby:memory:sonar;create=true;user=sonar;password=sonar"); - settings.setProperty("sonar.jdbc.driverClassName", "org.apache.derby.jdbc.EmbeddedDriver"); + settings.setProperty("sonar.jdbc.url", "jdbc:h2:mem:sonar"); + settings.setProperty("sonar.jdbc.driverClassName", "org.h2.Driver"); settings.setProperty("sonar.jdbc.username", "sonar"); settings.setProperty("sonar.jdbc.password", "sonar"); settings.setProperty("sonar.jdbc.maxActive", "1"); - try { - DefaultDatabase db = new DefaultDatabase(settings); - db.start(); + DefaultDatabase db = new DefaultDatabase(settings); + db.start(); - assertThat(db.getDialect().getId(), Is.is("derby")); - assertThat(((BasicDataSource) db.getDataSource()).getMaxActive(), Is.is(1)); - } finally { - DerbyUtils.dropInMemoryDatabase(); - } + assertThat(db.getDialect().getId(), Is.is("h2")); + assertThat(((BasicDataSource) db.getDataSource()).getMaxActive(), Is.is(1)); } @Test diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DerbyUtils.java b/sonar-core/src/test/java/org/sonar/core/persistence/DerbyUtils.java deleted file mode 100644 index f801bf34587..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/persistence/DerbyUtils.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar 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. - * - * Sonar 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 Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.persistence; - -import java.io.OutputStream; -import java.sql.DriverManager; - -public final class DerbyUtils { - - private DerbyUtils() { - } - - public static final OutputStream DEV_NULL = new OutputStream() { - @Override - public void write(int b) { - } - }; - - /** - * Disables Derby log. - * Note: in order to work properly this method should be called before Derby boot. - * See http://db.apache.org/derby/docs/10.7/ref/rrefproper33027.html - */ - public static void fixDerbyLogs() { - System.setProperty("derby.stream.error.field", "org.sonar.core.persistence.DerbyUtils.DEV_NULL"); - } - - public static void dropInMemoryDatabase() { - try { - DriverManager.getConnection("jdbc:derby:memory:sonar;drop=true"); - } catch (Exception e) { - // silently ignore - } - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/InMemoryDatabase.java b/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java index ed6320c36fa..a6528c11172 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/InMemoryDatabase.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java @@ -22,33 +22,29 @@ package org.sonar.core.persistence; import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; import org.hibernate.cfg.Environment; -import org.sonar.core.persistence.dialect.Derby; import org.sonar.core.persistence.dialect.Dialect; +import org.sonar.core.persistence.dialect.H2; import org.sonar.jpa.session.CustomHibernateConnectionProvider; import javax.sql.DataSource; -import java.sql.*; + +import java.sql.Connection; +import java.sql.SQLException; import java.util.Properties; /** - * Derby in-memory database, used for unit tests only. + * H2 in-memory database, used for unit tests only. * - * @since 2.12 + * @since 3.2 */ -public class InMemoryDatabase implements Database { - - static { - DerbyUtils.fixDerbyLogs(); - } - +public class H2Database implements Database { private static BasicDataSource datasource; - public InMemoryDatabase start() { + public H2Database start() { if (datasource == null) { startDatabase(); createSchema(); } - truncateTables(); return this; } @@ -58,10 +54,10 @@ public class InMemoryDatabase implements Database { void startDatabase() { try { Properties properties = new Properties(); - properties.put("driverClassName", "org.apache.derby.jdbc.EmbeddedDriver"); + properties.put("driverClassName", "org.h2.Driver"); properties.put("username", "sonar"); properties.put("password", "sonar"); - properties.put("url", "jdbc:derby:memory:sonar2;create=true;user=sonar;password=sonar"); + properties.put("url", "jdbc:h2:mem:sonar2"); // limit to 2 because of Hibernate and MyBatis properties.put("maxActive", "2"); @@ -69,7 +65,7 @@ public class InMemoryDatabase implements Database { datasource = (BasicDataSource) BasicDataSourceFactory.createDataSource(properties); } catch (Exception e) { - throw new IllegalStateException("Fail to start Derby", e); + throw new IllegalStateException("Fail to start H2", e); } } @@ -77,67 +73,26 @@ public class InMemoryDatabase implements Database { Connection connection = null; try { connection = datasource.getConnection(); - DdlUtils.createSchema(connection, "derby"); - + DdlUtils.createSchema(connection, "h2"); } catch (SQLException e) { throw new IllegalStateException("Fail to create schema", e); - } finally { closeQuietly(connection); } } - private void truncateTables() { - Connection connection = null; - try { - connection = datasource.getConnection(); - - DatabaseMetaData meta = connection.getMetaData(); - Statement statement = connection.createStatement(); - - ResultSet res = meta.getTables(null, null, null, new String[]{"TABLE"}); - while (res.next()) { - String tableName = res.getString("TABLE_NAME"); - statement.executeUpdate("TRUNCATE TABLE " + tableName); - } - res.close(); - - // See https://issues.apache.org/jira/browse/DERBY-5403 - res = meta.getColumns(null, null, null, "ID"); - while (res.next()) { - String tableName = res.getString("TABLE_NAME"); - statement.executeUpdate("ALTER TABLE " + tableName + " ALTER COLUMN ID RESTART WITH 1"); - } - res.close(); - - statement.close(); - } catch (SQLException e) { - throw new IllegalStateException("Fail to truncate tables", e); - - } finally { - closeQuietly(connection); // Important, otherwise tests can stuck - } - } - public static void stopDatabase() { try { if (datasource != null) { datasource.close(); datasource = null; } - DriverManager.getConnection("jdbc:derby:;shutdown=true"); - } catch (SQLException e) { - // See http://db.apache.org/derby/docs/dev/getstart/rwwdactivity3.html - // XJ015 indicates successful shutdown of Derby - // 08006 successful shutdown of a single database - if (!"XJ015".equals(e.getSQLState())) { - throw new IllegalStateException("Fail to stop Derby", e); - } + // Ignore error } } - public InMemoryDatabase stop() { + public H2Database stop() { return this; } @@ -146,7 +101,7 @@ public class InMemoryDatabase implements Database { } public Dialect getDialect() { - return new Derby(); + return new H2(); } public String getSchema() { diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/InMemoryDatabaseTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/H2DatabaseTest.java index 3e6bb1b6fa6..e815dd452e7 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/InMemoryDatabaseTest.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/H2DatabaseTest.java @@ -31,16 +31,12 @@ import static org.hamcrest.number.OrderingComparisons.greaterThan; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; -public class InMemoryDatabaseTest { - - static { - DerbyUtils.fixDerbyLogs(); - } +public class H2DatabaseTest { @Test public void shouldExecuteDdlAtStartup() throws SQLException { int tables = 0; - InMemoryDatabase db = new InMemoryDatabase(); + H2Database db = new H2Database(); try { db.start(); assertNotNull(db.getDataSource()); @@ -60,7 +56,7 @@ public class InMemoryDatabaseTest { @Test public void shouldLimitThePoolSize() { - InMemoryDatabase db = new InMemoryDatabase(); + H2Database db = new H2Database(); try { db.startDatabase(); assertThat(((BasicDataSource) db.getDataSource()).getMaxActive(), Is.is(2)); diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/MyBatisTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/MyBatisTest.java index 592bca54484..bff3c199a5e 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/MyBatisTest.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/MyBatisTest.java @@ -32,16 +32,12 @@ import static org.junit.Assert.assertThat; public class MyBatisTest { - static { - DerbyUtils.fixDerbyLogs(); - } - private static MyBatis myBatis; - private static InMemoryDatabase database; + private static H2Database database; @BeforeClass public static void start() { - database = new InMemoryDatabase(); + database = new H2Database(); myBatis = new MyBatis(database.start()); myBatis.start(); } diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/dialect/DerbyTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/dialect/H2Test.java index c101c978de1..a57517bba71 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/dialect/DerbyTest.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/dialect/H2Test.java @@ -24,19 +24,19 @@ import org.junit.Test; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; -public class DerbyTest { +public class H2Test { - private Derby derby = new Derby(); + private H2 h2 = new H2(); @Test public void matchesJdbcURL() { - assertThat(derby.matchesJdbcURL("jdbc:derby:foo"), is(true)); - assertThat(derby.matchesJdbcURL("jdbc:hsql:foo"), is(false)); + assertThat(h2.matchesJdbcURL("jdbc:h2:foo"), is(true)); + assertThat(h2.matchesJdbcURL("jdbc:hsql:foo"), is(false)); } @Test public void testBooleanSqlValues() { - assertThat(derby.getTrueSqlValue(), is("true")); - assertThat(derby.getFalseSqlValue(), is("false")); + assertThat(h2.getTrueSqlValue(), is("true")); + assertThat(h2.getFalseSqlValue(), is("false")); } } diff --git a/sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java b/sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java index 3e27d0ccb38..732da687b79 100644 --- a/sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java +++ b/sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java @@ -24,32 +24,30 @@ 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.Column; import org.dbunit.dataset.CompositeDataSet; import org.dbunit.dataset.DataSetException; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.ITable; -import org.dbunit.dataset.ITableMetaData; import org.dbunit.dataset.ReplacementDataSet; import org.dbunit.dataset.filter.DefaultColumnFilter; import org.dbunit.dataset.xml.FlatXmlDataSet; +import org.dbunit.ext.h2.H2DataTypeFactory; import org.dbunit.operation.DatabaseOperation; import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.sonar.api.database.DatabaseSession; import org.sonar.core.persistence.Database; import org.sonar.core.persistence.DatabaseCommands; -import org.sonar.core.persistence.InMemoryDatabase; +import org.sonar.core.persistence.H2Database; import org.sonar.jpa.session.DatabaseSessionFactory; import org.sonar.jpa.session.DefaultDatabaseConnector; import org.sonar.jpa.session.JpaDatabaseSession; import org.sonar.jpa.session.MemoryDatabaseConnector; import java.io.InputStream; -import java.sql.ResultSet; import java.sql.SQLException; import static org.junit.Assert.fail; @@ -68,13 +66,15 @@ public abstract class AbstractDbUnitTestCase { @BeforeClass public static void startDatabase() throws Exception { - database = new InMemoryDatabase(); - database.start(); + if (null == database) { // Create only once per vm + database = new H2Database(); + database.start(); - dbConnector = new MemoryDatabaseConnector(database); - dbConnector.start(); + dbConnector = new MemoryDatabaseConnector(database); + dbConnector.start(); - databaseCommands = DatabaseCommands.forDialect(database.getDialect()); + databaseCommands = DatabaseCommands.forDialect(database.getDialect()); + } } @Before @@ -96,12 +96,6 @@ public abstract class AbstractDbUnitTestCase { session.stop(); } - @AfterClass - public static void stopDatabase() { - dbConnector.stop(); - database.stop(); - } - public DatabaseSession getSession() { return session; } @@ -152,32 +146,14 @@ public abstract class AbstractDbUnitTestCase { databaseTester.setDataSet(compositeDataSet); connection = databaseTester.getConnection(); + connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new H2DataTypeFactory()); DatabaseOperation.CLEAN_INSERT.execute(connection, databaseTester.getDataSet()); - resetDerbySequence(compositeDataSet); } catch (Exception e) { throw translateException("Could not setup DBUnit data", e); } } - private void resetDerbySequence(CompositeDataSet compositeDataSet) throws DataSetException, SQLException { - for (ITable table : compositeDataSet.getTables()) { - ITableMetaData tableMetaData = table.getTableMetaData(); - String tableName = tableMetaData.getTableName(); - for (Column column : tableMetaData.getColumns()) { - if ("id".equalsIgnoreCase(column.getColumnName())) { // TODO hard-coded value - String maxSql = "SELECT MAX(id) FROM " + tableName; - ResultSet res = connection.getConnection().prepareStatement(maxSql).executeQuery(); - res.next(); - int max = res.getInt(1); - res.close(); - String alterSql = "ALTER TABLE " + tableName + " ALTER COLUMN id RESTART WITH " + (max + 1); - connection.getConnection().prepareStatement(alterSql).execute(); - } - } - } - } - protected final void checkTables(String testName, String... tables) { checkTables(testName, new String[0], tables); } |