aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-07-06 14:45:46 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-07-06 14:58:05 +0200
commit769e017b27513adf3e0991ac8affb090a1f35053 (patch)
tree471a449f8f40cb49e7dcd81acbb37289b480c5ad /sonar-db
parentb7d8cfe1c2550d3431056260050c991fdd63022a (diff)
downloadsonarqube-769e017b27513adf3e0991ac8affb090a1f35053.tar.gz
sonarqube-769e017b27513adf3e0991ac8affb090a1f35053.zip
Fix quality flaws in Dao
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/pom.xml7
-rw-r--r--sonar-db/src/main/java/org/sonar/db/ResultSetIterator.java3
-rw-r--r--sonar-db/src/main/java/org/sonar/db/component/ResourceDao.java9
-rw-r--r--sonar-db/src/main/java/org/sonar/db/compute/AnalysisReportDao.java8
-rw-r--r--sonar-db/src/main/java/org/sonar/db/dashboard/WidgetDao.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/db/dashboard/WidgetPropertyDao.java1
-rw-r--r--sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java1
-rw-r--r--sonar-db/src/main/java/org/sonar/db/property/PropertiesDao.java1
-rw-r--r--sonar-db/src/main/java/org/sonar/db/user/AuthorDao.java1
-rw-r--r--sonar-db/src/main/java/org/sonar/db/user/AuthorizationDao.java1
-rw-r--r--sonar-db/src/main/java/org/sonar/db/user/GroupMembershipDao.java1
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/source/SnapshotDataMapper.xml39
-rw-r--r--sonar-db/src/test/java/org/sonar/db/AbstractDaoTestCase.java9
-rw-r--r--sonar-db/src/test/java/org/sonar/db/component/ResourceDaoTest.java297
-rw-r--r--sonar-db/src/test/java/org/sonar/db/component/ResourceIndexDaoTest.java6
-rw-r--r--sonar-db/src/test/java/org/sonar/db/compute/AnalysisReportDaoTest.java5
-rw-r--r--sonar-db/src/test/java/org/sonar/db/issue/ActionPlanDaoTest.java6
-rw-r--r--sonar-db/src/test/java/org/sonar/db/issue/IssueChangeMapperTest.java23
-rw-r--r--sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java3
-rw-r--r--sonar-db/src/test/java/org/sonar/db/notification/NotificationQueueDaoTest.java6
-rw-r--r--sonar-db/src/test/java/org/sonar/db/qualitygate/QualityGateConditionDaoTest.java1
-rw-r--r--sonar-db/src/test/java/org/sonar/db/source/FileSourceDaoTest.java26
22 files changed, 76 insertions, 380 deletions
diff --git a/sonar-db/pom.xml b/sonar-db/pom.xml
index 422677b6342..070b72f0696 100644
--- a/sonar-db/pom.xml
+++ b/sonar-db/pom.xml
@@ -121,6 +121,13 @@
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <skipTests>${skipServerTests}</skipTests>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
diff --git a/sonar-db/src/main/java/org/sonar/db/ResultSetIterator.java b/sonar-db/src/main/java/org/sonar/db/ResultSetIterator.java
index e96a9577141..74dcf4fdee4 100644
--- a/sonar-db/src/main/java/org/sonar/db/ResultSetIterator.java
+++ b/sonar-db/src/main/java/org/sonar/db/ResultSetIterator.java
@@ -19,14 +19,13 @@
*/
package org.sonar.db;
-import org.apache.commons.dbutils.DbUtils;
-
import java.io.Closeable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.NoSuchElementException;
+import org.apache.commons.dbutils.DbUtils;
/**
* Forward-only {@link java.util.Iterator} over a {@link java.sql.ResultSet}. Rows are
diff --git a/sonar-db/src/main/java/org/sonar/db/component/ResourceDao.java b/sonar-db/src/main/java/org/sonar/db/component/ResourceDao.java
index 80addc6dd54..0156e11cd2a 100644
--- a/sonar-db/src/main/java/org/sonar/db/component/ResourceDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/component/ResourceDao.java
@@ -80,15 +80,6 @@ public class ResourceDao extends AbstractDao {
return null;
}
- public List<Long> getResourceIds(ResourceQuery query) {
- SqlSession session = myBatis().openSession(false);
- try {
- return session.getMapper(ResourceMapper.class).selectResourceIds(query);
- } finally {
- myBatis().closeQuietly(session);
- }
- }
-
public ResourceDto getResource(long projectId) {
SqlSession session = myBatis().openSession(false);
try {
diff --git a/sonar-db/src/main/java/org/sonar/db/compute/AnalysisReportDao.java b/sonar-db/src/main/java/org/sonar/db/compute/AnalysisReportDao.java
index d805044aae6..18196f9f53e 100644
--- a/sonar-db/src/main/java/org/sonar/db/compute/AnalysisReportDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/compute/AnalysisReportDao.java
@@ -21,16 +21,12 @@
package org.sonar.db.compute;
import com.google.common.annotations.VisibleForTesting;
+import java.util.List;
+import javax.annotation.CheckForNull;
import org.sonar.api.utils.System2;
-import org.sonar.db.compute.AnalysisReportDto;
-import org.sonar.db.compute.AnalysisReportMapper;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
-import javax.annotation.CheckForNull;
-
-import java.util.List;
-
import static org.sonar.db.compute.AnalysisReportDto.Status.PENDING;
import static org.sonar.db.compute.AnalysisReportDto.Status.WORKING;
diff --git a/sonar-db/src/main/java/org/sonar/db/dashboard/WidgetDao.java b/sonar-db/src/main/java/org/sonar/db/dashboard/WidgetDao.java
index dec981655e0..e0ee86b405f 100644
--- a/sonar-db/src/main/java/org/sonar/db/dashboard/WidgetDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/dashboard/WidgetDao.java
@@ -20,8 +20,6 @@
package org.sonar.db.dashboard;
import java.util.Collection;
-import org.sonar.db.dashboard.WidgetDto;
-import org.sonar.db.dashboard.WidgetMapper;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
diff --git a/sonar-db/src/main/java/org/sonar/db/dashboard/WidgetPropertyDao.java b/sonar-db/src/main/java/org/sonar/db/dashboard/WidgetPropertyDao.java
index 0102d3338a0..af69ea26624 100644
--- a/sonar-db/src/main/java/org/sonar/db/dashboard/WidgetPropertyDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/dashboard/WidgetPropertyDao.java
@@ -24,7 +24,6 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.sonar.db.Dao;
-import org.sonar.db.DaoUtils;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java
index ff4b710d9ca..87280d2ced9 100644
--- a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java
@@ -27,7 +27,6 @@ import java.util.List;
import java.util.Set;
import javax.annotation.CheckForNull;
import org.sonar.db.Dao;
-import org.sonar.db.DaoUtils;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
import org.sonar.db.component.SnapshotDto;
diff --git a/sonar-db/src/main/java/org/sonar/db/property/PropertiesDao.java b/sonar-db/src/main/java/org/sonar/db/property/PropertiesDao.java
index c69532dc8ea..0855274c3c3 100644
--- a/sonar-db/src/main/java/org/sonar/db/property/PropertiesDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/property/PropertiesDao.java
@@ -35,7 +35,6 @@ import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.sonar.api.resources.Scopes;
import org.sonar.db.Dao;
-import org.sonar.db.DaoUtils;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
diff --git a/sonar-db/src/main/java/org/sonar/db/user/AuthorDao.java b/sonar-db/src/main/java/org/sonar/db/user/AuthorDao.java
index 56a4eb312fc..d062151dd60 100644
--- a/sonar-db/src/main/java/org/sonar/db/user/AuthorDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/user/AuthorDao.java
@@ -26,7 +26,6 @@ import java.util.Date;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.sonar.db.Dao;
-import org.sonar.db.DaoUtils;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.MyBatis;
import org.sonar.db.component.ComponentDto;
diff --git a/sonar-db/src/main/java/org/sonar/db/user/AuthorizationDao.java b/sonar-db/src/main/java/org/sonar/db/user/AuthorizationDao.java
index c4c8a118dce..a168e5db606 100644
--- a/sonar-db/src/main/java/org/sonar/db/user/AuthorizationDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/user/AuthorizationDao.java
@@ -29,7 +29,6 @@ import javax.annotation.Nullable;
import org.apache.ibatis.session.SqlSession;
import org.sonar.api.server.ServerSide;
import org.sonar.db.Dao;
-import org.sonar.db.DaoUtils;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
diff --git a/sonar-db/src/main/java/org/sonar/db/user/GroupMembershipDao.java b/sonar-db/src/main/java/org/sonar/db/user/GroupMembershipDao.java
index e27e6fe5d98..1c6d9b69f3e 100644
--- a/sonar-db/src/main/java/org/sonar/db/user/GroupMembershipDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/user/GroupMembershipDao.java
@@ -33,7 +33,6 @@ import javax.annotation.Nonnull;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.sonar.db.Dao;
-import org.sonar.db.DaoUtils;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
diff --git a/sonar-db/src/main/resources/org/sonar/db/source/SnapshotDataMapper.xml b/sonar-db/src/main/resources/org/sonar/db/source/SnapshotDataMapper.xml
deleted file mode 100644
index 022381e4df8..00000000000
--- a/sonar-db/src/main/resources/org/sonar/db/source/SnapshotDataMapper.xml
+++ /dev/null
@@ -1,39 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" ?>
-<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
-<mapper namespace="org.sonar.db.source.SnapshotDataMapper">
-
- <sql id="snapshotDataColumns">
- snapshot_id AS "snapshotId",
- snapshot_data AS "data",
- data_type AS "dataType"
- </sql>
-
- <select id="selectSnapshotData" parameterType="map" resultType="SnapshotData">
- SELECT
- <include refid="snapshotDataColumns"/>
- FROM snapshot_data
- WHERE snapshot_id = #{sid}
- AND data_type IN
- <foreach item="dataType" index="index" collection="dataTypes" open="(" separator="," close=")">#{dataType}</foreach>
- </select>
-
- <select id="selectSnapshotDataByComponentKey" parameterType="map" resultType="SnapshotData">
- SELECT
- <include refid="snapshotDataColumns"/>
- FROM snapshot_data sd
- INNER JOIN projects p ON p.id=sd.resource_id and p.enabled=${_true}
- WHERE p.kee = #{componentKey}
- AND data_type IN
- <foreach item="dataType" index="index" collection="dataTypes" open="(" separator="," close=")">#{dataType}</foreach>
- </select>
-
- <insert id="insert" parameterType="SnapshotData" useGeneratedKeys="false">
- insert into snapshot_data
- (resource_id, snapshot_id, snapshot_data, data_type, created_at, updated_at)
- values (
- #{resourceId,jdbcType=INTEGER}, #{snapshotId,jdbcType=INTEGER}, #{data,jdbcType=CLOB}, #{dataType,jdbcType=VARCHAR},
- CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
- </insert>
-</mapper>
-
diff --git a/sonar-db/src/test/java/org/sonar/db/AbstractDaoTestCase.java b/sonar-db/src/test/java/org/sonar/db/AbstractDaoTestCase.java
index 43b8c75e036..af1f4ff67e3 100644
--- a/sonar-db/src/test/java/org/sonar/db/AbstractDaoTestCase.java
+++ b/sonar-db/src/test/java/org/sonar/db/AbstractDaoTestCase.java
@@ -34,13 +34,16 @@ import org.sonar.test.DbTests;
@Deprecated
public abstract class AbstractDaoTestCase {
+ private static final String XML_SUFFIX = ".xml";
+ private static final String XML_RESULT_SUFFIX = "-result.xml";
+
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
protected void setupData(String... testNames) {
List<String> filenames = new ArrayList<>();
for (String testName : testNames) {
- filenames.add(testName + (testName.endsWith(".xml") ? "" : ".xml"));
+ filenames.add(testName + (testName.endsWith(XML_SUFFIX) ? "" : XML_SUFFIX));
}
dbTester.prepareDbUnit(getClass(), filenames.toArray(new String[filenames.size()]));
}
@@ -50,11 +53,11 @@ public abstract class AbstractDaoTestCase {
}
protected void checkTables(String testName, String[] excludedColumnNames, String... tables) {
- dbTester.assertDbUnit(getClass(), testName + (testName.endsWith("-result.xml") ? "" : "-result.xml"), excludedColumnNames, tables);
+ dbTester.assertDbUnit(getClass(), testName + (testName.endsWith(XML_RESULT_SUFFIX) ? "" : XML_RESULT_SUFFIX), excludedColumnNames, tables);
}
protected void checkTable(String testName, String table, String... columns) {
- dbTester.assertDbUnitTable(getClass(), testName + (testName.endsWith("-result.xml") ? "" : "-result.xml"), table, columns);
+ dbTester.assertDbUnitTable(getClass(), testName + (testName.endsWith(XML_RESULT_SUFFIX) ? "" : XML_RESULT_SUFFIX), table, columns);
}
@Deprecated
diff --git a/sonar-db/src/test/java/org/sonar/db/component/ResourceDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/ResourceDaoTest.java
index 0936f6c8f76..3e838777f45 100644
--- a/sonar-db/src/test/java/org/sonar/db/component/ResourceDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/component/ResourceDaoTest.java
@@ -21,48 +21,42 @@ package org.sonar.db.component;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
-import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
-import org.apache.ibatis.session.SqlSession;
-import org.junit.After;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.experimental.categories.Category;
import org.sonar.api.component.Component;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.System2;
-import org.sonar.db.AbstractDaoTestCase;
-import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+import org.sonar.test.DbTests;
import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public class ResourceDaoTest extends AbstractDaoTestCase {
+@Category(DbTests.class)
+public class ResourceDaoTest {
- private DbSession session;
+ System2 system2 = mock(System2.class);
- private ResourceDao dao;
- private System2 system2;
+ @Rule
+ public DbTester dbTester = DbTester.create(system2);
+
+ ResourceDao dao = dbTester.getDbClient().resourceDao();
@Before
public void createDao() {
- session = getMyBatis().openSession(false);
- system2 = mock(System2.class);
when(system2.now()).thenReturn(1_500_000_000_000L);
- dao = new ResourceDao(getMyBatis(), system2);
- }
-
- @After
- public void tearDown() {
- session.close();
}
@Test
public void testDescendantProjects_do_not_include_self() {
- setupData("fixture");
+ dbTester.prepareDbUnit(getClass(), "fixture.xml");
List<ResourceDto> resources = dao.getDescendantProjects(1L);
@@ -71,7 +65,7 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
@Test
public void testDescendantProjects_id_not_found() {
- setupData("fixture");
+ dbTester.prepareDbUnit(getClass(), "fixture.xml");
List<ResourceDto> resources = dao.getDescendantProjects(33333L);
@@ -80,7 +74,7 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
@Test
public void get_resource_by_id() {
- setupData("fixture");
+ dbTester.prepareDbUnit(getClass(), "fixture.xml");
ResourceDto resource = dao.getResource(1L);
@@ -99,7 +93,7 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
@Test
public void get_resource_by_uuid() {
- setupData("fixture");
+ dbTester.prepareDbUnit(getClass(), "fixture.xml");
ResourceDto resource = dao.getResource("ABCD");
@@ -118,7 +112,7 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
@Test
public void get_resource_path_and_module_key() {
- setupData("fixture");
+ dbTester.prepareDbUnit(getClass(), "fixture.xml");
ResourceDto dir = dao.getResource(3L);
assertThat(dir.getPath()).isEqualTo("src/org/struts");
@@ -129,7 +123,7 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
@Test
public void get_uuid() {
- setupData("fixture");
+ dbTester.prepareDbUnit(getClass(), "fixture.xml");
ResourceDto file = dao.getResource(4L);
assertThat(file.getUuid()).isEqualTo("DEFG");
@@ -140,14 +134,14 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
@Test
public void getResource_not_found() {
- setupData("fixture");
+ dbTester.prepareDbUnit(getClass(), "fixture.xml");
assertThat(dao.getResource(987654321L)).isNull();
}
@Test
public void getResources_all() {
- setupData("fixture");
+ dbTester.prepareDbUnit(getClass(), "fixture.xml");
List<ResourceDto> resources = dao.getResources(ResourceQuery.create());
@@ -156,7 +150,7 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
@Test
public void getResources_filter_by_qualifier() {
- setupData("fixture");
+ dbTester.prepareDbUnit(getClass(), "fixture.xml");
List<ResourceDto> resources = dao.getResources(ResourceQuery.create().setQualifiers(new String[] {"TRK", "BRC"}));
assertThat(resources).extracting("qualifier").containsOnly("TRK", "BRC");
@@ -170,7 +164,7 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
@Test
public void getResources_filter_by_key() {
- setupData("fixture");
+ dbTester.prepareDbUnit(getClass(), "fixture.xml");
ResourceQuery query = ResourceQuery.create().setKey("org.struts:struts-core");
List<ResourceDto> resources = dao.getResources(query);
@@ -181,39 +175,8 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
}
@Test
- public void getResourceIds_all() {
- setupData("fixture");
-
- List<Long> ids = dao.getResourceIds(ResourceQuery.create());
-
- assertThat(ids).hasSize(4);
- }
-
- @Test
- public void getResourceIds_filter_by_qualifier() {
- setupData("fixture");
-
- List<Long> ids = dao.getResourceIds(ResourceQuery.create().setQualifiers(new String[] {"TRK", "BRC"}));
- assertThat(ids).containsOnly(1L, 2L);
-
- ids = dao.getResourceIds(ResourceQuery.create().setQualifiers(new String[] {"XXX"}));
- assertThat(ids).isEmpty();
-
- ids = dao.getResourceIds(ResourceQuery.create().setQualifiers(new String[] {}));
- assertThat(ids).hasSize(4);
- }
-
- @Test
- public void getResources_exclude_disabled() {
- setupData("getResources_exclude_disabled");
-
- assertThat(dao.getResourceIds(ResourceQuery.create().setExcludeDisabled(false))).containsOnly(1L, 2L);
- assertThat(dao.getResourceIds(ResourceQuery.create().setExcludeDisabled(true))).containsOnly(2L);
- }
-
- @Test
public void find_root_project_by_component_key() {
- setupData("fixture");
+ dbTester.prepareDbUnit(getClass(), "fixture.xml");
assertThat(dao.getRootProjectByComponentKey("org.struts:struts-core:src/org/struts/RequestContext.java").getKey()).isEqualTo("org.struts:struts");
assertThat(dao.getRootProjectByComponentKey("org.struts:struts-core:src/org/struts").getKey()).isEqualTo("org.struts:struts");
@@ -225,7 +188,7 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
@Test
public void find_root_project_by_component_Id() {
- setupData("fixture");
+ dbTester.prepareDbUnit(getClass(), "fixture.xml");
assertThat(dao.getRootProjectByComponentId(4l).getKey()).isEqualTo("org.struts:struts");
assertThat(dao.getRootProjectByComponentId(3l).getKey()).isEqualTo("org.struts:struts");
@@ -235,18 +198,17 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
@Test
public void find_parent_by_component_id() {
- setupData("fixture");
+ dbTester.prepareDbUnit(getClass(), "fixture.xml");
- assertThat(dao.getParentModuleByComponentId(4l, session).getKey()).isEqualTo("org.struts:struts");
- assertThat(dao.getParentModuleByComponentId(3l, session).getKey()).isEqualTo("org.struts:struts");
- assertThat(dao.getParentModuleByComponentId(2l, session).getKey()).isEqualTo("org.struts:struts");
- assertThat(dao.getParentModuleByComponentId(1l, session).getKey()).isEqualTo("org.struts:struts");
+ assertThat(dao.getParentModuleByComponentId(4l, dbTester.getSession()).getKey()).isEqualTo("org.struts:struts");
+ assertThat(dao.getParentModuleByComponentId(3l, dbTester.getSession()).getKey()).isEqualTo("org.struts:struts");
+ assertThat(dao.getParentModuleByComponentId(2l, dbTester.getSession()).getKey()).isEqualTo("org.struts:struts");
+ assertThat(dao.getParentModuleByComponentId(1l, dbTester.getSession()).getKey()).isEqualTo("org.struts:struts");
}
@Test
public void should_update() {
- setupData("update");
-
+ dbTester.prepareDbUnit(getClass(), "update.xml");
ResourceDto project = new ResourceDto()
.setKey("org.struts:struts")
.setDeprecatedKey("deprecated key").setScope(Scopes.PROJECT).setQualifier(Qualifiers.PROJECT)
@@ -257,206 +219,7 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
dao.insertOrUpdate(project);
assertThat(project.getId()).isNotNull();
- checkTables("update", "projects");
- }
-
- @Test
- public void should_insert() {
- setupData("insert");
-
- ResourceDto file1 = new ResourceDto()
- .setUuid("ABCD").setProjectUuid("EFGH").setModuleUuid("EFGH").setModuleUuidPath(".EFGH.")
- .setKey("org.struts:struts:/src/main/java/org/struts/Action.java")
- .setDeprecatedKey("org.struts:struts:org.struts.Action").setScope(Scopes.FILE).setQualifier(Qualifiers.FILE)
- .setLanguage("java").setName("Action").setLongName("org.struts.Action").setPath("/foo/bar");
- ResourceDto file2 = new ResourceDto()
- .setUuid("BCDE").setProjectUuid("FGHI").setModuleUuid("FGHI").setModuleUuidPath(".FGHI.")
- .setKey("org.struts:struts:/src/main/java/org/struts/Filter.java")
- .setDeprecatedKey("org.struts:struts:org.struts.Filter").setScope(Scopes.FILE).setQualifier(Qualifiers.FILE)
- .setLanguage("java").setName("Filter").setLongName("org.struts.Filter");
-
- dao.insertOrUpdate(file1, file2);
-
- assertThat(file1.getId()).isNotNull();
- assertThat(file2.getId()).isNotNull();
- checkTables("insert", new String[] {"authorization_updated_at", "created_at"}, "projects");
-
- // SONAR-3636 : created_at must be fed when inserting a new entry in the 'projects' table
- ResourceDto fileLoadedFromDB = dao.getResource(file1.getId());
- assertThat(fileLoadedFromDB.getCreatedAt()).isNotNull();
- assertThat(fileLoadedFromDB.getAuthorizationUpdatedAt()).isNotNull();
- }
-
- @Test
- public void should_insert_using_existing_session() {
- setupData("insert");
-
- ResourceDto file1 = new ResourceDto().setUuid("ABCD")
- .setKey("org.struts:struts:/src/main/java/org/struts/Action.java")
- .setDeprecatedKey("org.struts:struts:org.struts.Action").setScope(Scopes.FILE).setQualifier(Qualifiers.FILE)
- .setLanguage("java").setName("Action").setLongName("org.struts.Action");
- ResourceDto file2 = new ResourceDto().setUuid("BCDE")
- .setKey("org.struts:struts:/src/main/java/org/struts/Filter.java")
- .setDeprecatedKey("org.struts:struts:org.struts.Filter").setScope(Scopes.FILE).setQualifier(Qualifiers.FILE)
- .setLanguage("java").setName("Filter").setLongName("org.struts.Filter");
-
- SqlSession session = getMyBatis().openSession();
-
- dao.insertUsingExistingSession(file1, session);
- dao.insertUsingExistingSession(file2, session);
-
- session.rollback();
-
- assertThat(dbTester.countRowsOfTable("projects")).isZero();
- }
-
- @Test
- public void insert_add_uuids_on_project_if_missing() {
- setupData("insert");
-
- ResourceDto project = new ResourceDto()
- .setKey("org.struts:struts:struts")
- .setScope(Scopes.PROJECT)
- .setQualifier(Qualifiers.PROJECT);
-
- ResourceDto file = new ResourceDto()
- .setKey("org.struts:struts:/src/main/java/org/struts/Action.java")
- .setScope(Scopes.FILE)
- .setQualifier(Qualifiers.FILE);
-
- dao.insertOrUpdate(project, file);
-
- assertThat(project.getUuid()).isNotNull();
- assertThat(project.getProjectUuid()).isEqualTo(project.getUuid());
- assertThat(project.getModuleUuidPath()).isEmpty();
-
- assertThat(file.getUuid()).isNull();
- assertThat(file.getProjectUuid()).isNull();
- assertThat(file.getModuleUuidPath()).isNull();
- }
-
- @Test
- public void should_find_component_by_key() {
- setupData("fixture");
-
- assertThat(dao.findByKey("org.struts:struts")).isNotNull();
- Component component = dao.findByKey("org.struts:struts-core:src/org/struts/RequestContext.java");
- assertThat(component).isNotNull();
- assertThat(component.path()).isEqualTo("src/org/struts/RequestContext.java");
- assertThat(dao.findByKey("unknown")).isNull();
- }
-
- @Test
- public void should_find_component_by_id() {
- setupData("fixture");
-
- assertThat(dao.findById(1L, session)).isNotNull();
- assertThat(dao.findById(4L, session)).isNotNull();
- assertThat(dao.findById(555L, session)).isNull();
- }
-
- @Test
- public void should_select_projects_by_qualifiers() {
- setupData("fixture-including-ghost-projects-and-technical-project");
-
- List<Component> components = dao.selectProjectsByQualifiers(newArrayList("TRK"));
- assertThat(components).hasSize(1);
- assertThat(components.get(0).key()).isEqualTo("org.struts:struts");
- assertThat(((ComponentDto) components.get(0)).getId()).isEqualTo(1L);
-
- assertThat(dao.selectProjectsIncludingNotCompletedOnesByQualifiers(newArrayList("unknown"))).isEmpty();
- assertThat(dao.selectProjectsIncludingNotCompletedOnesByQualifiers(Collections.<String>emptyList())).isEmpty();
- }
-
- @Test
- public void should_select_projects_including_not_finished_by_qualifiers() {
- setupData("fixture-including-ghost-projects-and-technical-project");
-
- List<Component> components = dao.selectProjectsIncludingNotCompletedOnesByQualifiers(newArrayList("TRK"));
- assertThat(getKeys(components)).containsOnly("org.struts:struts", "org.apache.shindig", "org.sample:sample");
-
- assertThat(dao.selectProjectsIncludingNotCompletedOnesByQualifiers(newArrayList("unknown"))).isEmpty();
- assertThat(dao.selectProjectsIncludingNotCompletedOnesByQualifiers(Collections.<String>emptyList())).isEmpty();
- }
-
- @Test
- public void should_select_ghosts_projects_by_qualifiers() {
- setupData("fixture-including-ghost-projects-and-technical-project");
-
- List<Component> components = dao.selectGhostsProjects(newArrayList("TRK"));
- assertThat(components).hasSize(1);
- assertThat(getKeys(components)).containsOnly("org.apache.shindig");
-
- assertThat(dao.selectGhostsProjects(newArrayList("unknown"))).isEmpty();
- assertThat(dao.selectGhostsProjects(Collections.<String>emptyList())).isEmpty();
- }
-
- @Test
- public void should_select_provisioned_projects_by_qualifiers() {
- setupData("fixture-including-ghost-projects-and-technical-project");
-
- List<ResourceDto> components = dao.selectProvisionedProjects(newArrayList("TRK"));
- assertThat(components).hasSize(1);
- assertThat(components.get(0).getKey()).isEqualTo("org.sample:sample");
-
- assertThat(dao.selectProvisionedProjects(newArrayList("unknown"))).isEmpty();
- assertThat(dao.selectProvisionedProjects(Collections.<String>emptyList())).isEmpty();
- }
-
- @Test
- public void should_select_provisioned_project_by_key() {
- setupData("fixture-including-ghost-projects-and-technical-project");
-
- String key = "org.sample:sample";
- assertThat(dao.selectProvisionedProject(key).getKey()).isEqualTo(key);
- assertThat(dao.selectProvisionedProject("unknown")).isNull();
- }
-
- @Test
- public void get_last_snapshot_by_component_uuid() {
- setupData("get_last_snapshot_by_component_uuid");
-
- SnapshotDto snapshotDto = dao.getLastSnapshotByResourceUuid("ABCD", session);
- assertThat(snapshotDto.getId()).isEqualTo(1);
-
- assertThat(snapshotDto.getPeriodMode(1)).isEqualTo("previous_analysis");
- assertThat(snapshotDto.getPeriodModeParameter(1)).isNull();
- assertThat(snapshotDto.getPeriodDate(1)).isNull();
-
- assertThat(snapshotDto.getPeriodMode(2)).isEqualTo("days");
- assertThat(snapshotDto.getPeriodModeParameter(2)).isEqualTo("30");
- assertThat(snapshotDto.getPeriodDate(2)).isEqualTo(1_316_815_200_000L);
-
- assertThat(snapshotDto.getPeriodMode(3)).isEqualTo("days");
- assertThat(snapshotDto.getPeriodModeParameter(3)).isEqualTo("90");
- assertThat(snapshotDto.getPeriodDate(3)).isEqualTo(1_311_631_200_000L);
-
- assertThat(snapshotDto.getPeriodMode(4)).isEqualTo("previous_analysis");
- assertThat(snapshotDto.getPeriodModeParameter(4)).isNull();
- assertThat(snapshotDto.getPeriodDate(4)).isNull();
-
- assertThat(snapshotDto.getPeriodMode(5)).isEqualTo("previous_version");
- assertThat(snapshotDto.getPeriodModeParameter(5)).isNull();
- assertThat(snapshotDto.getPeriodDate(5)).isNull();
-
- snapshotDto = dao.getLastSnapshotByResourceUuid("EFGH", session);
- assertThat(snapshotDto.getId()).isEqualTo(2L);
-
- snapshotDto = dao.getLastSnapshotByResourceUuid("GHIJ", session);
- assertThat(snapshotDto.getId()).isEqualTo(3L);
-
- assertThat(dao.getLastSnapshotByResourceUuid("UNKNOWN", session)).isNull();
- }
-
- @Test
- public void update_authorization_date() {
- setupData("update_authorization_date");
-
- when(system2.now()).thenReturn(987654321L);
- dao.updateAuthorizationDate(1L, session);
- session.commit();
-
- checkTables("update_authorization_date", "projects");
+ dbTester.assertDbUnit(getClass(), "update-result.xml", "projects");
}
private List<String> getKeys(final List<Component> components) {
diff --git a/sonar-db/src/test/java/org/sonar/db/component/ResourceIndexDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/ResourceIndexDaoTest.java
index df1195d46ff..21c24b41e4d 100644
--- a/sonar-db/src/test/java/org/sonar/db/component/ResourceIndexDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/component/ResourceIndexDaoTest.java
@@ -20,6 +20,7 @@
package org.sonar.db.component;
import java.sql.SQLException;
+import org.junit.Before;
import org.junit.Test;
import org.sonar.api.resources.Qualifiers;
import org.sonar.db.AbstractDaoTestCase;
@@ -31,6 +32,11 @@ public class ResourceIndexDaoTest extends AbstractDaoTestCase {
ResourceIndexDao dao = dbTester.getDbClient().componentIndexDao();
+ @Before
+ public void setUp() throws Exception {
+ dbTester.truncateTables();
+ }
+
@Test
public void shouldIndexResource() {
setupData("shouldIndexResource");
diff --git a/sonar-db/src/test/java/org/sonar/db/compute/AnalysisReportDaoTest.java b/sonar-db/src/test/java/org/sonar/db/compute/AnalysisReportDaoTest.java
index 17bb66813b7..fe061532489 100644
--- a/sonar-db/src/test/java/org/sonar/db/compute/AnalysisReportDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/compute/AnalysisReportDaoTest.java
@@ -20,6 +20,7 @@
package org.sonar.db.compute;
+import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
@@ -28,15 +29,11 @@ import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.utils.System2;
-import org.sonar.db.compute.AnalysisReportDao;
-import org.sonar.db.compute.AnalysisReportDto;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.MyBatis;
import org.sonar.test.DbTests;
-import java.util.List;
-
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
diff --git a/sonar-db/src/test/java/org/sonar/db/issue/ActionPlanDaoTest.java b/sonar-db/src/test/java/org/sonar/db/issue/ActionPlanDaoTest.java
index 85439f61db8..533c06b5041 100644
--- a/sonar-db/src/test/java/org/sonar/db/issue/ActionPlanDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/issue/ActionPlanDaoTest.java
@@ -22,6 +22,7 @@ package org.sonar.db.issue;
import java.util.Collection;
import java.util.List;
+import org.junit.Before;
import org.junit.Test;
import org.sonar.db.AbstractDaoTestCase;
@@ -32,6 +33,11 @@ public class ActionPlanDaoTest extends AbstractDaoTestCase {
ActionPlanDao dao = dbTester.getDbClient().actionPlanDao();
+ @Before
+ public void setUp() throws Exception {
+ dbTester.truncateTables();
+ }
+
@Test
public void should_insert_new_action_plan() {
ActionPlanDto actionPlanDto = new ActionPlanDto().setKey("ABC").setName("Long term").setDescription("Long term action plan").setStatus("OPEN")
diff --git a/sonar-db/src/test/java/org/sonar/db/issue/IssueChangeMapperTest.java b/sonar-db/src/test/java/org/sonar/db/issue/IssueChangeMapperTest.java
index dc2fef893e6..97dec4e0e06 100644
--- a/sonar-db/src/test/java/org/sonar/db/issue/IssueChangeMapperTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/issue/IssueChangeMapperTest.java
@@ -19,26 +19,15 @@
*/
package org.sonar.db.issue;
-import org.apache.ibatis.session.SqlSession;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.sonar.db.AbstractDaoTestCase;
-import org.sonar.db.MyBatis;
public class IssueChangeMapperTest extends AbstractDaoTestCase {
- SqlSession session;
- IssueChangeMapper mapper;
@Before
- public void setUp() {
- session = getMyBatis().openSession();
- mapper = session.getMapper(IssueChangeMapper.class);
- }
-
- @After
- public void tearDown() {
- MyBatis.closeQuietly(session);
+ public void setUp() throws Exception {
+ dbTester.truncateTables();
}
@Test
@@ -52,8 +41,8 @@ public class IssueChangeMapperTest extends AbstractDaoTestCase {
dto.setCreatedAt(1_500_000_000_000L);
dto.setUpdatedAt(1_500_000_000_000L);
dto.setIssueChangeCreationDate(1_500_000_000_000L);
- mapper.insert(dto);
- session.commit();
+ dbTester.getSession().getMapper(IssueChangeMapper.class).insert(dto);
+ dbTester.getSession().commit();
checkTables("insert_diff", new String[] {"id"}, "issue_changes");
}
@@ -68,8 +57,8 @@ public class IssueChangeMapperTest extends AbstractDaoTestCase {
dto.setChangeData("the comment");
dto.setCreatedAt(1_500_000_000_000L);
dto.setUpdatedAt(1_500_000_000_000L);
- mapper.insert(dto);
- session.commit();
+ dbTester.getSession().getMapper(IssueChangeMapper.class).insert(dto);
+ dbTester.getSession().commit();
checkTables("insert_comment", new String[] {"id"}, "issue_changes");
}
diff --git a/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java b/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java
index 9621d6aa945..5867761b3b8 100644
--- a/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java
@@ -31,9 +31,6 @@ import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
-import org.sonar.db.measure.MeasureDao;
-import org.sonar.db.measure.MeasureDto;
-import org.sonar.db.measure.PastMeasureDto;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.test.DbTests;
diff --git a/sonar-db/src/test/java/org/sonar/db/notification/NotificationQueueDaoTest.java b/sonar-db/src/test/java/org/sonar/db/notification/NotificationQueueDaoTest.java
index 2c2f0ebd65c..18f21326780 100644
--- a/sonar-db/src/test/java/org/sonar/db/notification/NotificationQueueDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/notification/NotificationQueueDaoTest.java
@@ -31,11 +31,11 @@ import static org.assertj.core.api.Assertions.assertThat;
public class NotificationQueueDaoTest extends AbstractDaoTestCase {
- NotificationQueueDao dao;
+ NotificationQueueDao dao = new NotificationQueueDao(dbTester.myBatis());
@Before
- public void createDao() {
- dao = new NotificationQueueDao(getMyBatis());
+ public void setUp() throws Exception {
+ dbTester.truncateTables();
}
@Test
diff --git a/sonar-db/src/test/java/org/sonar/db/qualitygate/QualityGateConditionDaoTest.java b/sonar-db/src/test/java/org/sonar/db/qualitygate/QualityGateConditionDaoTest.java
index daa5b1d5777..a60c84881bc 100644
--- a/sonar-db/src/test/java/org/sonar/db/qualitygate/QualityGateConditionDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/qualitygate/QualityGateConditionDaoTest.java
@@ -19,7 +19,6 @@
*/
package org.sonar.db.qualitygate;
-import org.junit.Before;
import org.junit.Test;
import org.sonar.db.AbstractDaoTestCase;
diff --git a/sonar-db/src/test/java/org/sonar/db/source/FileSourceDaoTest.java b/sonar-db/src/test/java/org/sonar/db/source/FileSourceDaoTest.java
index 09d7fbcff1b..3704d37c992 100644
--- a/sonar-db/src/test/java/org/sonar/db/source/FileSourceDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/source/FileSourceDaoTest.java
@@ -25,30 +25,20 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import org.apache.commons.io.IOUtils;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.sonar.db.AbstractDaoTestCase;
-import org.sonar.db.DbSession;
import org.sonar.db.source.FileSourceDto.Type;
import static org.assertj.core.api.Assertions.assertThat;
public class FileSourceDaoTest extends AbstractDaoTestCase {
- DbSession session;
-
- FileSourceDao sut;
+ FileSourceDao sut = dbTester.getDbClient().fileSourceDao();
@Before
- public void setUpTestData() {
- session = getMyBatis().openSession(false);
- sut = new FileSourceDao(getMyBatis());
- }
-
- @After
- public void tearDown() {
- session.close();
+ public void setUp() throws Exception {
+ dbTester.truncateTables();
}
@Test
@@ -71,7 +61,7 @@ public class FileSourceDaoTest extends AbstractDaoTestCase {
setupData("shared");
ReaderToStringFunction fn = new ReaderToStringFunction();
- sut.readLineHashesStream(session, "FILE1_UUID", fn);
+ sut.readLineHashesStream(dbTester.getSession(), "FILE1_UUID", fn);
assertThat(fn.result).isEqualTo("ABC\\nDEF\\nGHI");
}
@@ -81,7 +71,7 @@ public class FileSourceDaoTest extends AbstractDaoTestCase {
setupData("shared");
ReaderToStringFunction fn = new ReaderToStringFunction();
- sut.readLineHashesStream(session, "unknown", fn);
+ sut.readLineHashesStream(dbTester.getSession(), "unknown", fn);
assertThat(fn.result).isNull();
}
@@ -91,7 +81,7 @@ public class FileSourceDaoTest extends AbstractDaoTestCase {
setupData("no_line_hashes_when_only_test_data");
ReaderToStringFunction fn = new ReaderToStringFunction();
- sut.readLineHashesStream(session, "FILE1_UUID", fn);
+ sut.readLineHashesStream(dbTester.getSession(), "FILE1_UUID", fn);
assertThat(fn.result).isNull();
}
@@ -136,8 +126,8 @@ public class FileSourceDaoTest extends AbstractDaoTestCase {
public void update_date_when_updated_date_is_zero() {
setupData("update_date_when_updated_date_is_zero");
- sut.updateDateWhenUpdatedDateIsZero(session, "ABCD", 1500000000002L);
- session.commit();
+ sut.updateDateWhenUpdatedDateIsZero(dbTester.getSession(), "ABCD", 1500000000002L);
+ dbTester.getSession().commit();
checkTable("update_date_when_updated_date_is_zero", "file_sources", "project_uuid", "file_uuid", "data_hash", "line_hashes", "src_hash", "created_at", "updated_at",
"data_type");