diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-02 20:13:22 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-02 21:29:42 +0200 |
commit | 29ae9b385a7bf720ae89637582c862df43b7fd0d (patch) | |
tree | c42098461a73b77d0fa8f2d91b09dac10250efdc /sonar-core | |
parent | 25ff4124dbb2e2e5455fe94a4726c8b4d841f860 (diff) | |
download | sonarqube-29ae9b385a7bf720ae89637582c862df43b7fd0d.tar.gz sonarqube-29ae9b385a7bf720ae89637582c862df43b7fd0d.zip |
SONAR-6588 drop db connection from batch
Diffstat (limited to 'sonar-core')
28 files changed, 14 insertions, 1196 deletions
diff --git a/sonar-core/pom.xml b/sonar-core/pom.xml index 73d735b8a6b..cb7784aaefc 100644 --- a/sonar-core/pom.xml +++ b/sonar-core/pom.xml @@ -59,36 +59,6 @@ <artifactId>sonar-home</artifactId> </dependency> <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate-core</artifactId> - <!-- provided only by batch --> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate-annotations</artifactId> - <!-- provided only by batch --> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate-commons-annotations</artifactId> - <!-- provided only by batch --> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate-entitymanager</artifactId> - <!-- provided only by batch --> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>geronimo-spec</groupId> - <artifactId>geronimo-spec-jta</artifactId> - <!-- provided only by batch --> - <scope>provided</scope> - </dependency> - <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> </dependency> diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/Database.java b/sonar-core/src/main/java/org/sonar/core/persistence/Database.java index df6e67a2163..27b03f01c63 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/Database.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/Database.java @@ -19,12 +19,10 @@ */ package org.sonar.core.persistence; +import javax.sql.DataSource; import org.picocontainer.Startable; import org.sonar.core.persistence.dialect.Dialect; -import javax.sql.DataSource; -import java.util.Properties; - /** * @since 2.12 */ @@ -38,6 +36,4 @@ public interface Database extends Startable { * @return the dialect or null if start() has not been executed */ Dialect getDialect(); - - Properties getHibernateProperties(); } 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 46fbd4ef2fc..d7c88f8da3e 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 @@ -20,11 +20,16 @@ package org.sonar.core.persistence; import com.google.common.annotations.VisibleForTesting; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.lang.StringUtils; -import org.hibernate.cfg.Environment; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.config.Settings; @@ -32,15 +37,6 @@ import org.sonar.api.database.DatabaseProperties; import org.sonar.core.persistence.dialect.Dialect; import org.sonar.core.persistence.dialect.DialectUtils; import org.sonar.core.persistence.profiling.ProfiledDataSource; -import org.sonar.jpa.session.CustomHibernateConnectionProvider; - -import javax.sql.DataSource; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; -import java.util.Map; -import java.util.Properties; /** * @since 2.12 @@ -51,7 +47,6 @@ public class DefaultDatabase implements Database { private static final String DEFAULT_URL = "jdbc:h2:tcp://localhost/sonar"; private static final String SONAR_JDBC = "sonar.jdbc."; - private static final String SONAR_HIBERNATE = "sonar.hibernate."; private static final String SONAR_JDBC_DIALECT = "sonar.jdbc.dialect"; private static final String SONAR_JDBC_URL = "sonar.jdbc.url"; private static final String VALIDATE = "validate"; @@ -89,7 +84,6 @@ public class DefaultDatabase implements Database { void initSettings() { properties = new Properties(); completeProperties(settings, properties, SONAR_JDBC); - completeProperties(settings, properties, SONAR_HIBERNATE); completeDefaultProperties(properties); doCompleteProperties(properties); @@ -137,21 +131,6 @@ public class DefaultDatabase implements Database { } @Override - public Properties getHibernateProperties() { - Properties props = new Properties(); - - List<String> hibernateKeys = settings.getKeysStartingWith(SONAR_HIBERNATE); - for (String hibernateKey : hibernateKeys) { - props.put(StringUtils.removeStart(hibernateKey, "sonar."), settings.getString(hibernateKey)); - } - props.put(Environment.DIALECT, getDialect().getHibernateDialectClass().getName()); - props.put("hibernate.generate_statistics", "false"); - props.put(Environment.CONNECTION_PROVIDER, CustomHibernateConnectionProvider.class.getName()); - - return props; - } - - @Override public final DataSource getDataSource() { return datasource; } @@ -195,7 +174,6 @@ public class DefaultDatabase implements Database { completeDefaultProperty(props, DatabaseProperties.PROP_URL, DEFAULT_URL); completeDefaultProperty(props, DatabaseProperties.PROP_USER, props.getProperty(DatabaseProperties.PROP_USER_DEPRECATED, DatabaseProperties.PROP_USER_DEFAULT_VALUE)); completeDefaultProperty(props, DatabaseProperties.PROP_PASSWORD, DatabaseProperties.PROP_PASSWORD_DEFAULT_VALUE); - completeDefaultProperty(props, "sonar.jdbc.hibernate.hbm2ddl", VALIDATE); } private static void completeDefaultProperty(Properties props, String key, String defaultValue) { diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Dialect.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Dialect.java index bcaeb161fc9..7ab00f27ce8 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Dialect.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Dialect.java @@ -32,11 +32,6 @@ public interface Dialect { String getId(); /** - * @return the hibernate dialect class to be used - */ - Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass(); - - /** * @return the activerecord dialect to be used */ String getActiveRecordDialectCode(); diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/H2.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/H2.java index 9361095d7d6..ef2812c4e87 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/H2.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/H2.java @@ -20,7 +20,6 @@ package org.sonar.core.persistence.dialect; import org.apache.commons.lang.StringUtils; -import org.hibernate.dialect.H2Dialect; /** * @since 1.12 @@ -34,11 +33,6 @@ public class H2 extends AbstractDialect { } @Override - public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() { - return H2Dialect.class; - } - - @Override public boolean matchesJdbcURL(String jdbcConnectionURL) { return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:h2:"); } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MsSql.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MsSql.java index aff31dd5d2e..dbf98f2c319 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MsSql.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MsSql.java @@ -20,10 +20,6 @@ package org.sonar.core.persistence.dialect; import org.apache.commons.lang.StringUtils; -import org.hibernate.dialect.SQLServerDialect; -import org.sonar.api.database.DatabaseProperties; - -import java.sql.Types; public class MsSql extends AbstractDialect { @@ -34,11 +30,6 @@ public class MsSql extends AbstractDialect { } @Override - public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() { - return MsSqlDialect.class; - } - - @Override public boolean matchesJdbcURL(String jdbcConnectionURL) { return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:microsoft:sqlserver:") || StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:jtds:sqlserver:"); @@ -48,25 +39,4 @@ public class MsSql extends AbstractDialect { public boolean supportsMigration() { return true; } - - public static class MsSqlDialect extends SQLServerDialect { - public MsSqlDialect() { - super(); - registerColumnType(Types.DOUBLE, "decimal"); - registerColumnType(Types.VARCHAR, 255, "nvarchar($l)"); - registerColumnType(Types.VARCHAR, DatabaseProperties.MAX_TEXT_SIZE, "nvarchar(max)"); - registerColumnType(Types.CHAR, "nchar(1)"); - registerColumnType(Types.CLOB, "nvarchar(max)"); - } - - @Override - public String getTypeName(int code, int length, int precision, int scale) { - if (code != 2005) { - return super.getTypeName(code, length, precision, scale); - } else { - return "ntext"; - } - } - } } - diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MySql.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MySql.java index 756a39a891c..01518fe1e86 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MySql.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MySql.java @@ -20,10 +20,6 @@ package org.sonar.core.persistence.dialect; import org.apache.commons.lang.StringUtils; -import org.hibernate.dialect.MySQLDialect; -import org.sonar.api.database.DatabaseProperties; - -import java.sql.Types; /** * @since 1.12 @@ -37,25 +33,10 @@ public class MySql extends AbstractDialect { } @Override - public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() { - return MySqlWithDecimalDialect.class; - } - - @Override public boolean matchesJdbcURL(String jdbcConnectionURL) { return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:mysql:"); } - public static class MySqlWithDecimalDialect extends MySQLDialect { - public MySqlWithDecimalDialect() { - super(); - registerColumnType(Types.DOUBLE, "decimal precision"); - registerColumnType(Types.VARCHAR, DatabaseProperties.MAX_TEXT_SIZE, "longtext"); - registerColumnType(Types.CLOB, "longtext"); - registerColumnType(Types.BLOB, "blob"); - } - } - @Override public int getScrollDefaultFetchSize() { return Integer.MIN_VALUE; diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Oracle.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Oracle.java index 7333f13f1f0..03aac3c2c8e 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Oracle.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Oracle.java @@ -20,10 +20,6 @@ package org.sonar.core.persistence.dialect; import org.apache.commons.lang.StringUtils; -import org.hibernate.dialect.Oracle10gDialect; -import org.sonar.api.database.DatabaseProperties; - -import java.sql.Types; /** * @since 1.12 @@ -37,11 +33,6 @@ public class Oracle extends AbstractDialect { } @Override - public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() { - return Oracle10gWithDecimalDialect.class; - } - - @Override public boolean matchesJdbcURL(String jdbcConnectionURL) { return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:oracle:"); } @@ -50,18 +41,4 @@ public class Oracle extends AbstractDialect { public boolean supportsMigration() { return true; } - - public static class Oracle10gWithDecimalDialect extends Oracle10gDialect { - public Oracle10gWithDecimalDialect() { - super(); - registerColumnType(Types.DOUBLE, "number($p,$s)"); - registerColumnType(Types.VARCHAR, DatabaseProperties.MAX_TEXT_SIZE, "clob"); - registerColumnType(Types.VARBINARY, "blob"); - } - - @Override - public Class getNativeIdentifierGeneratorClass() { - return OracleSequenceGenerator.class; - } - } } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/OracleSequenceGenerator.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/OracleSequenceGenerator.java deleted file mode 100644 index 94d34f8367e..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/OracleSequenceGenerator.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 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.core.persistence.dialect; - -import org.apache.commons.lang.StringUtils; -import org.hibernate.dialect.Dialect; -import org.hibernate.id.PersistentIdentifierGenerator; -import org.hibernate.id.SequenceGenerator; -import org.hibernate.type.Type; - -import java.util.Properties; - -/** - * @since 1.10 - */ -public class OracleSequenceGenerator extends SequenceGenerator { - - public static final String SEQUENCE_NAME_SUFFIX = "_SEQ"; - - @Override - public void configure(Type type, Properties params, Dialect dialect) { - String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE); - if (tableName != null) { - StringBuilder sequenceNameBuilder = new StringBuilder(); - sequenceNameBuilder.append(tableName); - sequenceNameBuilder.append(SEQUENCE_NAME_SUFFIX); - params.setProperty(SEQUENCE, StringUtils.upperCase(sequenceNameBuilder.toString())); - } - super.configure(type, params, dialect); - } - -} diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSQLSequenceGenerator.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSQLSequenceGenerator.java deleted file mode 100644 index 2f8dffc34d6..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSQLSequenceGenerator.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 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.core.persistence.dialect; - -import org.hibernate.dialect.Dialect; -import org.hibernate.id.PersistentIdentifierGenerator; -import org.hibernate.id.SequenceGenerator; -import org.hibernate.type.Type; - -import java.util.Properties; - -/** - * if the underlying database is PostgreSQL, the sequence - * naming convention is different and includes the primary key - * column name - * - * @since 1.10 - */ -public class PostgreSQLSequenceGenerator extends SequenceGenerator { - - public static final String SEQUENCE_NAME_SEPARATOR = "_"; - public static final String SEQUENCE_NAME_SUFFIX = "seq"; - - @Override - public void configure(Type type, Properties params, Dialect dialect) { - - String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE); - String columnName = params.getProperty(PersistentIdentifierGenerator.PK); - - if (tableName != null && columnName != null) { - StringBuilder sequenceNameBuilder = new StringBuilder(); - - sequenceNameBuilder.append(tableName); - sequenceNameBuilder.append(SEQUENCE_NAME_SEPARATOR); - sequenceNameBuilder.append(columnName); - sequenceNameBuilder.append(SEQUENCE_NAME_SEPARATOR); - sequenceNameBuilder.append(SEQUENCE_NAME_SUFFIX); - - params.setProperty(SEQUENCE, sequenceNameBuilder.toString()); - } - - super.configure(type, params, dialect); - } - -} diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSql.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSql.java index b9bfc5eff26..bf6d01d4871 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSql.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSql.java @@ -20,11 +20,8 @@ package org.sonar.core.persistence.dialect; import com.google.common.collect.ImmutableList; -import org.apache.commons.lang.StringUtils; -import org.hibernate.dialect.PostgreSQLDialect; - -import java.sql.Types; import java.util.List; +import org.apache.commons.lang.StringUtils; /** * @since 1.12 @@ -39,11 +36,6 @@ public class PostgreSql extends AbstractDialect { } @Override - public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() { - return PostgreSQLWithDecimalDialect.class; - } - - @Override public boolean matchesJdbcURL(String jdbcConnectionURL) { return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:postgresql:"); } @@ -57,17 +49,4 @@ public class PostgreSql extends AbstractDialect { public boolean supportsMigration() { return true; } - - public static class PostgreSQLWithDecimalDialect extends PostgreSQLDialect { - - public PostgreSQLWithDecimalDialect() { - super(); - registerColumnType(Types.DOUBLE, "numeric($p,$s)"); - } - @Override - public Class getNativeIdentifierGeneratorClass() { - return PostgreSQLSequenceGenerator.class; - } - - } } diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDto.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDto.java index 00184d0050b..b671b187a87 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDto.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDto.java @@ -21,6 +21,8 @@ package org.sonar.core.qualityprofile.db; import com.google.common.base.Preconditions; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; @@ -30,10 +32,6 @@ import org.sonar.core.persistence.Dto; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.SeverityUtil; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import javax.persistence.Transient; - public class ActiveRuleDto extends Dto<ActiveRuleKey> { public static final String INHERITED = ActiveRule.INHERITED; @@ -66,7 +64,6 @@ public class ActiveRuleDto extends Dto<ActiveRuleKey> { } // This field do not exists in db, it's only retrieve by joins - @Transient private Integer parentId; public Integer getId() { diff --git a/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java b/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java index 09214a071de..2d16c7137d0 100644 --- a/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java +++ b/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java @@ -27,8 +27,6 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.RequiresDB; import org.sonar.api.config.Settings; import org.sonar.api.database.model.Snapshot; import org.sonar.api.i18n.I18n; @@ -36,8 +34,6 @@ import org.sonar.api.server.ServerSide; import static org.sonar.api.utils.DateUtils.longToDate; -@RequiresDB -@BatchSide @ServerSide public class Periods { diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/AbstractDatabaseConnector.java b/sonar-core/src/main/java/org/sonar/jpa/session/AbstractDatabaseConnector.java deleted file mode 100644 index 5aacf76dd03..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/session/AbstractDatabaseConnector.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 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.jpa.session; - -import java.util.Map; -import java.util.Properties; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.core.persistence.Database; -import org.sonar.core.persistence.dialect.Dialect; - -public abstract class AbstractDatabaseConnector implements DatabaseConnector { - protected static final Logger LOG = LoggerFactory.getLogger(AbstractDatabaseConnector.class); - - protected Database database; - private EntityManagerFactory factory = null; - - protected AbstractDatabaseConnector(Database database) { - this.database = database; - } - - public void start() { - LOG.info("Initializing Hibernate"); - factory = createEntityManagerFactory(); - - } - - public void stop() { - if (factory != null && factory.isOpen()) { - factory.close(); - factory = null; - } - database = null; - } - - protected EntityManagerFactory createEntityManagerFactory() { - // other settings are stored into /META-INF/persistence.xml - Properties props = database.getHibernateProperties(); - logHibernateSettings(props); - return Persistence.createEntityManagerFactory("sonar", props); - } - - private void logHibernateSettings(Properties props) { - if (LOG.isDebugEnabled()) { - for (Map.Entry<Object, Object> entry : props.entrySet()) { - LOG.debug(entry.getKey() + ": " + entry.getValue()); - } - } - } - - @Override - public EntityManager createEntityManager() { - return factory.createEntityManager(); - } - - @Override - public final Dialect getDialect() { - return database.getDialect(); - } -} diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/CustomHibernateConnectionProvider.java b/sonar-core/src/main/java/org/sonar/jpa/session/CustomHibernateConnectionProvider.java deleted file mode 100644 index f8656d5eec0..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/session/CustomHibernateConnectionProvider.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 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.jpa.session; - -import org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider; - -import javax.sql.DataSource; -import java.util.Properties; - -public class CustomHibernateConnectionProvider extends InjectedDataSourceConnectionProvider { - - private static DataSource datasourceForConfig; - - static void setDatasourceForConfig(DataSource ds) { - CustomHibernateConnectionProvider.datasourceForConfig = ds; - } - - @Override - public void configure(Properties props) { - setDataSource(datasourceForConfig); - super.configure(props); - } -} diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseConnector.java b/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseConnector.java deleted file mode 100644 index c76f10bc55e..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseConnector.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 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.jpa.session; - -import java.sql.Connection; -import java.sql.SQLException; -import javax.persistence.EntityManager; -import org.sonar.core.persistence.dialect.Dialect; - -public interface DatabaseConnector { - - Dialect getDialect(); - - Connection getConnection() throws SQLException; - - EntityManager createEntityManager(); - -} diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseSessionFactory.java b/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseSessionFactory.java deleted file mode 100644 index e22f79be589..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseSessionFactory.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 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.jpa.session; - -import org.sonar.api.database.DatabaseSession; - -/** - * @deprecated replaced by mybatis - */ -@Deprecated -public interface DatabaseSessionFactory { - - DatabaseSession getSession(); - - void clear(); -} diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/DefaultDatabaseConnector.java b/sonar-core/src/main/java/org/sonar/jpa/session/DefaultDatabaseConnector.java deleted file mode 100644 index 643ba19c45f..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/session/DefaultDatabaseConnector.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 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.jpa.session; - -import org.sonar.api.utils.SonarException; -import org.sonar.core.persistence.Database; - -import java.sql.Connection; -import java.sql.SQLException; - -public class DefaultDatabaseConnector extends AbstractDatabaseConnector { - - public DefaultDatabaseConnector(Database database) { - super(database); - } - - @Override - public void start() { - createDatasource(); - super.start(); - } - - private void createDatasource() { - try { - CustomHibernateConnectionProvider.setDatasourceForConfig(database.getDataSource()); - } catch (Exception e) { - throw new SonarException("Fail to connect to database", e); - } - } - - @Override - public Connection getConnection() throws SQLException { - return database != null && database.getDataSource() != null ? database.getDataSource().getConnection() : null; - } -} diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java b/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java deleted file mode 100644 index 6c1973a9e66..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java +++ /dev/null @@ -1,301 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 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.jpa.session; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Maps; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import javax.persistence.EntityManager; -import javax.persistence.NonUniqueResultException; -import javax.persistence.PersistenceException; -import javax.persistence.Query; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.database.DatabaseSession; - -public class JpaDatabaseSession extends DatabaseSession { - - private final DatabaseConnector connector; - private EntityManager entityManager = null; - private int index = 0; - private boolean inTransaction = false; - - public JpaDatabaseSession(DatabaseConnector connector) { - this.connector = connector; - } - - /** - * Note that usage of this method is discouraged, because it allows to construct and execute queries without additional exception handling, - * which done in methods of this class. - */ - @Override - public EntityManager getEntityManager() { - if (entityManager == null) { - entityManager = connector.createEntityManager(); - } - return entityManager; - } - - @Override - public void start() { - getEntityManager(); - index = 0; - } - - @Override - public void stop() { - commitAndClose(); - } - - @Override - public void commitAndClose() { - commit(); - if (entityManager != null && entityManager.isOpen()) { - entityManager.close(); - entityManager = null; - } - } - - @Override - public void commit() { - if (inTransaction) { - if (getEntityManager().isOpen()) { - if (getEntityManager().getTransaction().getRollbackOnly()) { - getEntityManager().getTransaction().rollback(); - } else { - getEntityManager().getTransaction().commit(); - } - getEntityManager().clear(); - index = 0; - } - inTransaction = false; - } - } - - @Override - public void rollback() { - if (inTransaction) { - getEntityManager().getTransaction().rollback(); - inTransaction = false; - } - } - - @Override - public <T> T save(T model) { - startTransaction(); - internalSave(model, true); - return model; - } - - @Override - public Object saveWithoutFlush(Object model) { - startTransaction(); - internalSave(model, false); - return model; - } - - @Override - public boolean contains(Object model) { - startTransaction(); - return getEntityManager().contains(model); - } - - @Override - public void save(Object... models) { - startTransaction(); - for (Object model : models) { - save(model); - } - } - - private void internalSave(Object model, boolean flushIfNeeded) { - try { - getEntityManager().persist(model); - } catch (PersistenceException e) { - /* - * See http://jira.sonarsource.com/browse/SONAR-2234 - * In some cases Hibernate can throw exceptions without meaningful information about context, so we improve them here. - */ - throw new PersistenceException("Unable to persist : " + model, e); - } - if (flushIfNeeded && (++index % BATCH_SIZE == 0)) { - commit(); - } - } - - @Override - public Object merge(Object model) { - startTransaction(); - return getEntityManager().merge(model); - } - - @Override - public void remove(Object model) { - startTransaction(); - getEntityManager().remove(model); - if (++index % BATCH_SIZE == 0) { - commit(); - } - } - - @Override - public void removeWithoutFlush(Object model) { - startTransaction(); - getEntityManager().remove(model); - } - - @Override - public <T> T reattach(Class<T> entityClass, Object primaryKey) { - startTransaction(); - return getEntityManager().getReference(entityClass, primaryKey); - } - - private void startTransaction() { - if (!inTransaction) { - getEntityManager().getTransaction().begin(); - inTransaction = true; - } - } - - /** - * Note that not recommended to directly execute {@link Query#getSingleResult()}, because it will bypass exception handling, - * which done in {@link #getSingleResult(Query, Object)}. - */ - @Override - public Query createQuery(String hql) { - startTransaction(); - return getEntityManager().createQuery(hql); - } - - @Override - public Query createNativeQuery(String sql) { - startTransaction(); - return getEntityManager().createNativeQuery(sql); - } - - /** - * @return the result or <code>defaultValue</code>, if not found - * @throws NonUniqueResultException if more than one result - */ - @Override - public <T> T getSingleResult(Query query, T defaultValue) { - /* - * See http://jira.sonarsource.com/browse/SONAR-2225 - * By default Hibernate throws NonUniqueResultException without meaningful information about context, - * so we improve it here by adding all results in error message. - * Note that in some rare situations we can receive too many results, which may lead to OOME, - * but actually it will mean that database is corrupted as we don't expect more than one result - * and in fact org.hibernate.ejb.QueryImpl#getSingleResult() anyway does loading of several results under the hood. - */ - List<T> result = query.getResultList(); - - if (result.size() == 1) { - return result.get(0); - - } else if (result.isEmpty()) { - return defaultValue; - - } else { - Set<T> uniqueResult = new HashSet<>(result); - if (uniqueResult.size() > 1) { - throw new NonUniqueResultException("Expected single result, but got : " + result.toString()); - } else { - return uniqueResult.iterator().next(); - } - } - } - - @Override - public <T> T getEntity(Class<T> entityClass, Object id) { - startTransaction(); - return getEntityManager().find(entityClass, id); - } - - /** - * @return the result or <code>null</code>, if not found - * @throws NonUniqueResultException if more than one result - */ - @Override - public <T> T getSingleResult(Class<T> entityClass, Object... criterias) { - try { - return getSingleResult(getQueryForCriterias(entityClass, true, criterias), (T) null); - - } catch (NonUniqueResultException ex) { - NonUniqueResultException e = new NonUniqueResultException("Expected single result for entitiy " + entityClass.getSimpleName() - + " with criterias : " + StringUtils.join(criterias, ",")); - throw (NonUniqueResultException) e.initCause(ex); - } - } - - @Override - public <T> List<T> getResults(Class<T> entityClass, Object... criterias) { - return getQueryForCriterias(entityClass, true, criterias).getResultList(); - } - - @Override - public <T> List<T> getResults(Class<T> entityClass) { - return getQueryForCriterias(entityClass, false, (Object[]) null).getResultList(); - } - - private Query getQueryForCriterias(Class<?> entityClass, boolean raiseError, Object... criterias) { - if (criterias == null && raiseError) { - throw new IllegalStateException("criterias parameter must be provided"); - } - startTransaction(); - StringBuilder hql = new StringBuilder("SELECT o FROM ").append(entityClass.getSimpleName()).append(" o"); - if (criterias != null) { - hql.append(" WHERE "); - Map<String, Object> mappedCriterias = Maps.newHashMap(); - for (int i = 0; i < criterias.length; i += 2) { - mappedCriterias.put((String) criterias[i], criterias[i + 1]); - } - buildCriteriasHQL(hql, mappedCriterias); - Query query = getEntityManager().createQuery(hql.toString()); - - for (Map.Entry<String, Object> entry : mappedCriterias.entrySet()) { - if (entry.getValue() != null) { - query.setParameter(entry.getKey(), entry.getValue()); - } - } - return query; - } - return getEntityManager().createQuery(hql.toString()); - } - - @VisibleForTesting - void buildCriteriasHQL(StringBuilder hql, Map<String, Object> mappedCriterias) { - for (Iterator<Map.Entry<String, Object>> i = mappedCriterias.entrySet().iterator(); i.hasNext();) { - Map.Entry<String, Object> entry = i.next(); - hql.append("o.").append(entry.getKey()); - if (entry.getValue() == null) { - hql.append(" IS NULL"); - } else { - hql.append("=:").append(entry.getKey()); - } - if (i.hasNext()) { - hql.append(" AND "); - } - } - } - -} diff --git a/sonar-core/src/main/resources/META-INF/persistence.xml b/sonar-core/src/main/resources/META-INF/persistence.xml deleted file mode 100644 index 24ad54095ca..00000000000 --- a/sonar-core/src/main/resources/META-INF/persistence.xml +++ /dev/null @@ -1,28 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<persistence xmlns="http://java.sun.com/xml/ns/persistence" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" - version="1.0"> - <persistence-unit name="sonar" transaction-type="RESOURCE_LOCAL"> - <provider>org.hibernate.ejb.HibernatePersistence</provider> - - <class>org.sonar.api.database.model.Snapshot</class> - <class>org.sonar.api.measures.Metric</class> - <class>org.sonar.api.database.model.ResourceModel</class> - - <exclude-unlisted-classes>true</exclude-unlisted-classes> - <properties> - <property name="hibernate.current_session_context_class" value="thread"/> - <property name="hibernate.connection.release_mode" value="after_transaction"/> - <property name="hibernate.bytecode.use_reflection_optimizer" value="true"/> - <property name="hibernate.query.factory_class" value="org.hibernate.hql.ast.ASTQueryTranslatorFactory"/> - <property name="hibernate.jdbc.batch_size" value="30"/> - <property name="hibernate.connection.useUnicode" value="true"/> - <property name="hibernate.connection.charSet" value="UTF-8"/> - <property name="hibernate.connection.characterEncoding" value="UTF-8"/> - <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/> - <property name="hibernate.cache.use_second_level_cache" value="false"/> - <property name="hibernate.cache.use_query_cache" value="false"/> - </properties> - </persistence-unit> -</persistence> 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 5de19270781..109846319be 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 @@ -125,16 +125,4 @@ public class DefaultDatabaseTest { 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"); - DefaultDatabase database = new DefaultDatabase(settings); - database.initSettings(); - - Properties hibernateProps = database.getHibernateProperties(); - - assertThat(hibernateProps.getProperty("hibernate.generate_statistics")).isEqualTo("false"); - } } 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 4119f0b70dd..07035468cf9 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 @@ -19,18 +19,13 @@ */ package org.sonar.core.persistence; +import java.sql.Connection; +import java.sql.SQLException; +import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbutils.DbUtils; -import org.hibernate.cfg.Environment; 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.Connection; -import java.sql.SQLException; -import java.util.Properties; /** * H2 in-memory database, used for unit tests only. @@ -113,13 +108,6 @@ public class H2Database implements Database { return new H2(); } - public Properties getHibernateProperties() { - Properties properties = new Properties(); - properties.put("hibernate.hbm2ddl.auto", "validate"); - properties.put(Environment.CONNECTION_PROVIDER, CustomHibernateConnectionProvider.class.getName()); - return properties; - } - @Override public String toString() { return "H2 Database[" + name + "]"; diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/dialect/OracleSequenceGeneratorTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/dialect/OracleSequenceGeneratorTest.java deleted file mode 100644 index b7e7814fe0e..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/persistence/dialect/OracleSequenceGeneratorTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 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.core.persistence.dialect; - -import org.hibernate.id.PersistentIdentifierGenerator; -import org.junit.Test; - -import java.util.Properties; - -import static org.assertj.core.api.Assertions.assertThat; - -public class OracleSequenceGeneratorTest { - - @Test - public void sequenceNameShouldFollowRailsConventions() { - Properties props = new Properties(); - props.setProperty(PersistentIdentifierGenerator.TABLE, "my_table"); - props.setProperty(PersistentIdentifierGenerator.PK, "id"); - - OracleSequenceGenerator generator = new OracleSequenceGenerator(); - generator.configure(null, props, new Oracle.Oracle10gWithDecimalDialect()); - assertThat(generator.getSequenceName()).isEqualTo("MY_TABLE_SEQ"); - } - - @Test - public void should_not_fail_if_table_name_can_not_be_loaded() { - Properties props = new Properties(); - OracleSequenceGenerator generator = new OracleSequenceGenerator(); - generator.configure(null, props, new Oracle.Oracle10gWithDecimalDialect()); - assertThat(generator.getSequenceName()).isNotEmpty(); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/dialect/PostgreSQLSequenceGeneratorTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/dialect/PostgreSQLSequenceGeneratorTest.java deleted file mode 100644 index d90dcaeee27..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/persistence/dialect/PostgreSQLSequenceGeneratorTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 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.core.persistence.dialect; - -import org.hibernate.id.PersistentIdentifierGenerator; -import org.junit.Test; - -import java.util.Properties; - -import static org.assertj.core.api.Assertions.assertThat; - -public class PostgreSQLSequenceGeneratorTest { - - @Test - public void sequenceNameShouldFollowRailsConventions() { - Properties props = new Properties(); - props.setProperty(PersistentIdentifierGenerator.TABLE, "my_table"); - props.setProperty(PersistentIdentifierGenerator.PK, "id"); - - PostgreSQLSequenceGenerator generator = new PostgreSQLSequenceGenerator(); - generator.configure(null, props, new PostgreSql.PostgreSQLWithDecimalDialect()); - assertThat(generator.getSequenceName()).isEqualTo("my_table_id_seq"); - } - - @Test - public void should_not_fail_if_table_name_can_not_be_loaded() { - Properties props = new Properties(); - PostgreSQLSequenceGenerator generator = new PostgreSQLSequenceGenerator(); - generator.configure(null, props, new PostgreSql.PostgreSQLWithDecimalDialect()); - assertThat(generator.getSequenceName()).isNotEmpty(); - } -} diff --git a/sonar-core/src/test/java/org/sonar/jpa/session/DatabaseSessionTest.java b/sonar-core/src/test/java/org/sonar/jpa/session/DatabaseSessionTest.java deleted file mode 100644 index d66bd9fae00..00000000000 --- a/sonar-core/src/test/java/org/sonar/jpa/session/DatabaseSessionTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 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.jpa.session; - -import java.util.List; -import javax.persistence.NonUniqueResultException; -import org.hamcrest.Matchers; -import org.hamcrest.core.IsCollectionContaining; -import org.junit.Before; -import org.junit.Test; -import org.sonar.api.database.model.ResourceModel; -import org.sonar.jpa.test.AbstractDbUnitTestCase; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -public class DatabaseSessionTest extends AbstractDbUnitTestCase { - - private ResourceModel project1; - private ResourceModel project2; - - @Before - public void setup() { - project1 = new ResourceModel(ResourceModel.SCOPE_PROJECT, "mygroup:myartifact", "JAV", null, "my name"); - project2 = new ResourceModel(ResourceModel.SCOPE_PROJECT, "mygroup:myartifact1", "JAV", null, "my name 2"); - } - - @Test - public void testGetSingleResultWithNoResults() { - assertNull(getSession().getSingleResult(ResourceModel.class, "name", "test")); - } - - @Test(expected = IllegalStateException.class) - public void testGetSingleResultWithNoCriterias() { - assertNull(getSession().getSingleResult(ResourceModel.class, (Object[]) null)); - } - - @Test - public void testGetSingleResultWithOneResult() { - getSession().save(project1); - ResourceModel hit = getSession().getSingleResult(ResourceModel.class, "name", "my name"); - assertNotNull(hit); - assertEquals(project1, hit); - } - - @Test(expected = NonUniqueResultException.class) - public void testGetSingleResultWithTwoResults() { - getSession().save(project1, project2); - getSession().getSingleResult(ResourceModel.class, "qualifier", "JAV"); - } - - @Test - public void testGetResultsWithNoResults() { - List<ResourceModel> hits = getSession().getResults(ResourceModel.class, "name", "foo"); - assertTrue(hits.isEmpty()); - } - - @Test - public void testGetResultsWithMultipleResults() { - ResourceModel project3 = new ResourceModel(ResourceModel.SCOPE_PROJECT, "mygroup:myartifact3", "BRC", null, "my name 3"); - getSession().save(project1, project2, project3); - - List<ResourceModel> hits = getSession().getResults(ResourceModel.class, "qualifier", "JAV"); - assertFalse(hits.isEmpty()); - assertThat(hits, IsCollectionContaining.hasItems(project1, project2)); - assertThat(hits, Matchers.not(IsCollectionContaining.hasItem(project3))); - } - -} diff --git a/sonar-core/src/test/java/org/sonar/jpa/session/JpaDatabaseSessionTest.java b/sonar-core/src/test/java/org/sonar/jpa/session/JpaDatabaseSessionTest.java deleted file mode 100644 index 2c6f92b5733..00000000000 --- a/sonar-core/src/test/java/org/sonar/jpa/session/JpaDatabaseSessionTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 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.jpa.session; - -import com.google.common.collect.Maps; -import org.junit.Before; -import org.junit.Test; - -import javax.persistence.NonUniqueResultException; -import javax.persistence.Query; -import java.util.Arrays; -import java.util.Collections; -import java.util.Map; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class JpaDatabaseSessionTest { - - private JpaDatabaseSession session; - - @Before - public void setUp() { - session = new JpaDatabaseSession(null); - } - - @Test(expected = NonUniqueResultException.class) - public void shouldThrowNonUniqueResultException() { - Query query = mock(Query.class); - when(query.getResultList()).thenReturn(Arrays.asList("foo", "bar")); - session.getSingleResult(query, null); - } - - @Test - public void shouldReturnSingleResult() { - Query query = mock(Query.class); - - when(query.getResultList()).thenReturn(Arrays.asList("foo", "foo"), Arrays.asList("bar")); - assertThat(session.getSingleResult(query, "default"), is("foo")); - assertThat(session.getSingleResult(query, "default"), is("bar")); - } - - @Test - public void shouldReturnDefaultValue() { - Query query = mock(Query.class); - when(query.getResultList()).thenReturn(Collections.emptyList()); - assertThat(session.getSingleResult(query, "default"), is("default")); - } - - @Test - public void shouldBuildCriteriasHQL() { - StringBuilder hql = new StringBuilder(); - Map<String, Object> mappedCriterias = Maps.newLinkedHashMap(); - mappedCriterias.put("foo", "value"); - mappedCriterias.put("bar", null); - session.buildCriteriasHQL(hql, mappedCriterias); - assertThat(hql.toString(), is("o.foo=:foo AND o.bar IS NULL")); - } - -} 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 7d70b9b2a97..cb7cc117c9f 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 @@ -37,10 +37,8 @@ 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.After; import org.junit.Before; import org.junit.BeforeClass; -import org.sonar.api.database.DatabaseSession; import org.sonar.core.cluster.NullQueue; import org.sonar.core.config.Logback; import org.sonar.core.persistence.Database; @@ -49,14 +47,10 @@ import org.sonar.core.persistence.DatabaseVersion; import org.sonar.core.persistence.H2Database; import org.sonar.core.persistence.MyBatis; import org.sonar.core.persistence.SchemaMigrationMapper; -import org.sonar.jpa.session.DatabaseSessionFactory; -import org.sonar.jpa.session.DefaultDatabaseConnector; -import org.sonar.jpa.session.JpaDatabaseSession; import static org.junit.Assert.fail; /** - * Heavily duplicates AbstractDaoTestCase as long as Hibernate is in use. * @deprecated this class does not support non-H2 databases */ @Deprecated @@ -64,9 +58,7 @@ public abstract class AbstractDbUnitTestCase { private static Database database; private static MyBatis myBatis; private static DatabaseCommands databaseCommands; - private static DefaultDatabaseConnector dbConnector; private IDatabaseTester databaseTester; - private JpaDatabaseSession session; @BeforeClass public static void startDatabase() throws SQLException { @@ -82,9 +74,6 @@ public abstract class AbstractDbUnitTestCase { session.getMapper(SchemaMigrationMapper.class).insert(String.valueOf(DatabaseVersion.LAST_VERSION)); session.commit(); } - - dbConnector = new DefaultDatabaseConnector(database); - dbConnector.start(); } } @@ -92,19 +81,6 @@ public abstract class AbstractDbUnitTestCase { public void startDbUnit() throws Exception { databaseCommands.truncateDatabase(database.getDataSource()); databaseTester = new DataSourceDatabaseTester(database.getDataSource()); - session = new JpaDatabaseSession(dbConnector); - session.start(); - } - - @After - public void stopDbUnit() throws Exception { - if (session != null) { - session.rollback(); - } - } - - protected DatabaseSession getSession() { - return session; } protected MyBatis getMyBatis() { @@ -115,17 +91,6 @@ public abstract class AbstractDbUnitTestCase { return database; } - protected DatabaseSessionFactory getSessionFactory() { - return new DatabaseSessionFactory() { - public DatabaseSession getSession() { - return session; - } - - public void clear() { - } - }; - } - protected void setupData(String... testNames) { InputStream[] streams = new InputStream[testNames.length]; try { @@ -262,9 +227,4 @@ public abstract class AbstractDbUnitTestCase { runtimeException.setStackTrace(cause.getStackTrace()); return runtimeException; } - - protected Long getHQLCount(Class<?> hqlClass) { - String hqlCount = "SELECT count(o) from " + hqlClass.getSimpleName() + " o"; - return (Long) getSession().createQuery(hqlCount).getSingleResult(); - } } diff --git a/sonar-core/src/test/resources/logback-test.xml b/sonar-core/src/test/resources/logback-test.xml index 605652c2007..8822a606d96 100644 --- a/sonar-core/src/test/resources/logback-test.xml +++ b/sonar-core/src/test/resources/logback-test.xml @@ -11,10 +11,6 @@ </encoder> </appender> - <logger name="org.hibernate"> - <level value="WARN"/> - </logger> - <logger name="org.dbunit"> <level value="WARN"/> </logger> @@ -39,4 +35,4 @@ <appender-ref ref="STDOUT"/> </root> -</configuration>
\ No newline at end of file +</configuration> |