diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2011-12-16 19:16:25 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2011-12-16 19:19:43 +0100 |
commit | 898957b3012d1ce6449a1e8427544d9656ac7e51 (patch) | |
tree | 50c0b05e9e1d5355f383325cbca1db444ae7edfa /sonar-core | |
parent | 8c6e23469be5e7b675b95c9e98c6b6b21d4f7a3d (diff) | |
download | sonarqube-898957b3012d1ce6449a1e8427544d9656ac7e51.tar.gz sonarqube-898957b3012d1ce6449a1e8427544d9656ac7e51.zip |
SONAR-983 create table RESOURCE_INDEX
The related myBatis classes are in the package org.sonar.persistence.resource
Diffstat (limited to 'sonar-core')
18 files changed, 375 insertions, 61 deletions
diff --git a/sonar-core/pom.xml b/sonar-core/pom.xml index bf523da6e2c..0f7b07d3536 100644 --- a/sonar-core/pom.xml +++ b/sonar-core/pom.xml @@ -48,6 +48,10 @@ </dependency> <dependency> <groupId>org.apache.derby</groupId> + <artifactId>derbyclient</artifactId> + </dependency> + <dependency> + <groupId>org.apache.derby</groupId> <artifactId>derbynet</artifactId> </dependency> <dependency> diff --git a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java b/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java index 81e9315bedb..1b69803b7c8 100644 --- a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java +++ b/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java @@ -42,7 +42,7 @@ public class SchemaMigration { - complete the Derby DDL file used for unit tests : sonar-testing-harness/src/main/resources/org/sonar/test/persistence/sonar-test.ddl */ - public static final int LAST_VERSION = 236; + public static final int LAST_VERSION = 237; public final static String TABLE_NAME = "schema_migrations"; diff --git a/sonar-core/src/main/java/org/sonar/persistence/DatabaseUtils.java b/sonar-core/src/main/java/org/sonar/persistence/DatabaseUtils.java index c3044feb397..3ce9cbc37e9 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/DatabaseUtils.java +++ b/sonar-core/src/main/java/org/sonar/persistence/DatabaseUtils.java @@ -67,6 +67,7 @@ public final class DatabaseUtils { "project_measures", "properties", "quality_models", + "resource_index", "reviews", "review_comments", "rules", diff --git a/sonar-core/src/main/java/org/sonar/persistence/MyBatis.java b/sonar-core/src/main/java/org/sonar/persistence/MyBatis.java index af790601f94..59331636f09 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/MyBatis.java +++ b/sonar-core/src/main/java/org/sonar/persistence/MyBatis.java @@ -19,31 +19,19 @@ */ package org.sonar.persistence; -import java.io.IOException; -import java.io.InputStream; - import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.ibatis.builder.xml.XMLMapperBuilder; import org.apache.ibatis.mapping.Environment; -import org.apache.ibatis.session.Configuration; -import org.apache.ibatis.session.ExecutorType; -import org.apache.ibatis.session.SqlSession; -import org.apache.ibatis.session.SqlSessionFactory; -import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.apache.ibatis.session.*; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; -import org.sonar.persistence.dashboard.ActiveDashboardDto; -import org.sonar.persistence.dashboard.ActiveDashboardMapper; -import org.sonar.persistence.dashboard.DashboardDto; -import org.sonar.persistence.dashboard.DashboardMapper; -import org.sonar.persistence.dashboard.WidgetDto; -import org.sonar.persistence.dashboard.WidgetMapper; -import org.sonar.persistence.dashboard.WidgetPropertyDto; -import org.sonar.persistence.dashboard.WidgetPropertyMapper; +import org.sonar.persistence.dashboard.*; import org.sonar.persistence.duplication.DuplicationMapper; import org.sonar.persistence.duplication.DuplicationUnitDto; +import org.sonar.persistence.resource.ResourceIndexDto; +import org.sonar.persistence.resource.ResourceIndexMapper; import org.sonar.persistence.review.ReviewDto; import org.sonar.persistence.review.ReviewMapper; import org.sonar.persistence.rule.RuleDto; @@ -51,6 +39,9 @@ import org.sonar.persistence.rule.RuleMapper; import org.sonar.persistence.template.LoadedTemplateDto; import org.sonar.persistence.template.LoadedTemplateMapper; +import java.io.IOException; +import java.io.InputStream; + public class MyBatis implements BatchComponent, ServerComponent { private Database database; @@ -71,6 +62,7 @@ public class MyBatis implements BatchComponent, ServerComponent { loadAlias(conf, "DuplicationUnit", DuplicationUnitDto.class); loadAlias(conf, "LoadedTemplate", LoadedTemplateDto.class); loadAlias(conf, "Review", ReviewDto.class); + loadAlias(conf, "ResourceIndex", ResourceIndexDto.class); loadAlias(conf, "Rule", RuleDto.class); loadAlias(conf, "Widget", WidgetDto.class); loadAlias(conf, "WidgetProperty", WidgetPropertyDto.class); @@ -80,6 +72,7 @@ public class MyBatis implements BatchComponent, ServerComponent { loadMapper(conf, DuplicationMapper.class); loadMapper(conf, LoadedTemplateMapper.class); loadMapper(conf, ReviewMapper.class); + loadMapper(conf, ResourceIndexMapper.class); loadMapper(conf, RuleMapper.class); loadMapper(conf, WidgetMapper.class); loadMapper(conf, WidgetPropertyMapper.class); @@ -115,7 +108,7 @@ public class MyBatis implements BatchComponent, ServerComponent { private InputStream getPathToMapper(Class mapperClass) { InputStream input = getClass().getResourceAsStream( - "/" + StringUtils.replace(mapperClass.getName(), ".", "/") + "-" + database.getDialect().getId() + ".xml"); + "/" + StringUtils.replace(mapperClass.getName(), ".", "/") + "-" + database.getDialect().getId() + ".xml"); if (input == null) { input = getClass().getResourceAsStream("/" + StringUtils.replace(mapperClass.getName(), ".", "/") + ".xml"); } diff --git a/sonar-core/src/main/java/org/sonar/persistence/dashboard/ActiveDashboardDto.java b/sonar-core/src/main/java/org/sonar/persistence/dashboard/ActiveDashboardDto.java index 2e8ad023144..1a37b1c5022 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/dashboard/ActiveDashboardDto.java +++ b/sonar-core/src/main/java/org/sonar/persistence/dashboard/ActiveDashboardDto.java @@ -22,7 +22,7 @@ package org.sonar.persistence.dashboard; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; -public class ActiveDashboardDto { +public final class ActiveDashboardDto { private Long id; private Long dashboardId; @@ -37,8 +37,7 @@ public class ActiveDashboardDto { } /** - * @param id - * the id to set + * @param id the id to set */ public void setId(Long id) { this.id = id; @@ -52,8 +51,7 @@ public class ActiveDashboardDto { } /** - * @param dashboardId - * the dashboardId to set + * @param dashboardId the dashboardId to set */ public void setDashboardId(Long dashboardId) { this.dashboardId = dashboardId; @@ -67,8 +65,7 @@ public class ActiveDashboardDto { } /** - * @param userId - * the userId to set + * @param userId the userId to set */ public void setUserId(Long userId) { this.userId = userId; @@ -82,8 +79,7 @@ public class ActiveDashboardDto { } /** - * @param orderIndex - * the orderIndex to set + * @param orderIndex the orderIndex to set */ public void setOrderIndex(Integer orderIndex) { this.orderIndex = orderIndex; @@ -96,7 +92,7 @@ public class ActiveDashboardDto { */ @Override public int hashCode() { - return HashCodeBuilder.reflectionHashCode(this, new String[] { "id" }); + return HashCodeBuilder.reflectionHashCode(this, new String[]{"id"}); } /* @@ -106,7 +102,7 @@ public class ActiveDashboardDto { */ @Override public boolean equals(Object obj) { - return EqualsBuilder.reflectionEquals(this, obj, new String[] { "id" }); + return EqualsBuilder.reflectionEquals(this, obj, new String[]{"id"}); } } diff --git a/sonar-core/src/main/java/org/sonar/persistence/dashboard/WidgetDto.java b/sonar-core/src/main/java/org/sonar/persistence/dashboard/WidgetDto.java index df1c25bdfbc..520c098bae0 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/dashboard/WidgetDto.java +++ b/sonar-core/src/main/java/org/sonar/persistence/dashboard/WidgetDto.java @@ -19,13 +19,13 @@ */ package org.sonar.persistence.dashboard; +import com.google.common.collect.Lists; + import java.util.Collection; import java.util.Date; import java.util.List; -import com.google.common.collect.Lists; - -public class WidgetDto { +public final class WidgetDto { private Long id; private Long dashboardId; @@ -47,8 +47,7 @@ public class WidgetDto { } /** - * @param id - * the id to set + * @param id the id to set */ public void setId(Long id) { this.id = id; @@ -62,8 +61,7 @@ public class WidgetDto { } /** - * @param dashboardId - * the dashboardId to set + * @param dashboardId the dashboardId to set */ public void setDashboardId(Long dashboardId) { this.dashboardId = dashboardId; @@ -77,8 +75,7 @@ public class WidgetDto { } /** - * @param key - * the key to set + * @param key the key to set */ public void setKey(String key) { this.key = key; @@ -92,8 +89,7 @@ public class WidgetDto { } /** - * @param name - * the name to set + * @param name the name to set */ public void setName(String name) { this.name = name; @@ -107,8 +103,7 @@ public class WidgetDto { } /** - * @param description - * the description to set + * @param description the description to set */ public void setDescription(String description) { this.description = description; @@ -122,8 +117,7 @@ public class WidgetDto { } /** - * @param columnIndex - * the columnIndex to set + * @param columnIndex the columnIndex to set */ public void setColumnIndex(Integer columnIndex) { this.columnIndex = columnIndex; @@ -137,8 +131,7 @@ public class WidgetDto { } /** - * @param rowIndex - * the rowIndex to set + * @param rowIndex the rowIndex to set */ public void setRowIndex(Integer rowIndex) { this.rowIndex = rowIndex; @@ -152,8 +145,7 @@ public class WidgetDto { } /** - * @param configured - * the configured to set + * @param configured the configured to set */ public void setConfigured(boolean configured) { this.configured = configured; @@ -167,8 +159,7 @@ public class WidgetDto { } /** - * @param createdAt - * the createdAt to set + * @param createdAt the createdAt to set */ public void setCreatedAt(Date createdAt) { this.createdAt = createdAt; @@ -182,8 +173,7 @@ public class WidgetDto { } /** - * @param updatedAt - * the updatedAt to set + * @param updatedAt the updatedAt to set */ public void setUpdatedAt(Date updatedAt) { this.updatedAt = updatedAt; @@ -197,8 +187,7 @@ public class WidgetDto { } /** - * @param widgetPropertyDto - * the widgetProperty to set + * @param widgetPropertyDto the widgetProperty to set */ public void addWidgetProperty(WidgetPropertyDto widgetPropertyDto) { widgetPropertyDtos.add(widgetPropertyDto); diff --git a/sonar-core/src/main/java/org/sonar/persistence/dashboard/WidgetPropertyDto.java b/sonar-core/src/main/java/org/sonar/persistence/dashboard/WidgetPropertyDto.java index 01a85391022..eeabded8edc 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/dashboard/WidgetPropertyDto.java +++ b/sonar-core/src/main/java/org/sonar/persistence/dashboard/WidgetPropertyDto.java @@ -19,7 +19,7 @@ */ package org.sonar.persistence.dashboard; -public class WidgetPropertyDto { +public final class WidgetPropertyDto { private Long id; private Long widgetId; diff --git a/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexDao.java b/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexDao.java new file mode 100644 index 00000000000..395be04b431 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexDao.java @@ -0,0 +1,84 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.persistence.resource; + +import org.apache.commons.lang.StringUtils; +import org.apache.ibatis.session.ExecutorType; +import org.apache.ibatis.session.SqlSession; +import org.sonar.persistence.MyBatis; + +import java.util.Collections; +import java.util.List; + +public class ResourceIndexDao { + + public static final int MINIMUM_SEARCH_SIZE = 3; + public static final int MINIMUM_KEY_SIZE = 3; + + private final MyBatis mybatis; + + public ResourceIndexDao(MyBatis mybatis) { + this.mybatis = mybatis; + } + + public List<ResourceIndexDto> search(String input) { + if (StringUtils.isBlank(input) || input.length() < MINIMUM_SEARCH_SIZE) { + return Collections.emptyList(); + } + SqlSession sqlSession = mybatis.openSession(); + try { + ResourceIndexMapper mapper = sqlSession.getMapper(ResourceIndexMapper.class); + return mapper.selectLikeKey(normalize(input) + "%"); + } finally { + sqlSession.close(); + } + } + + public void index(String resourceName, int resourceId, int projectId) { + if (StringUtils.isBlank(resourceName)) { + return; + } + String normalizedName = normalize(resourceName); + if (normalizedName.length() >= MINIMUM_KEY_SIZE) { + SqlSession sqlSession = mybatis.openSession(ExecutorType.BATCH); + try { + ResourceIndexMapper mapper = sqlSession.getMapper(ResourceIndexMapper.class); + ResourceIndexDto dto = new ResourceIndexDto().setResourceId(resourceId).setProjectId(projectId); + + for (int position = 0; position <= normalizedName.length() - MINIMUM_KEY_SIZE; position++) { + dto.setPosition(position); + dto.setKey(StringUtils.substring(normalizedName, position)); + mapper.insert(dto); + } + + sqlSession.commit(); + + } finally { + sqlSession.close(); + } + } + } + + static String normalize(String input) { + String result = StringUtils.trim(input); + result = StringUtils.lowerCase(result); + return result; + } +} diff --git a/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexDto.java b/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexDto.java new file mode 100644 index 00000000000..1a351cd0bb5 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexDto.java @@ -0,0 +1,64 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.persistence.resource; + +public final class ResourceIndexDto { + + private String key; + private int position; + private int resourceId; + private int projectId; + + public String getKey() { + return key; + } + + public ResourceIndexDto setKey(String key) { + this.key = key; + return this; + } + + public int getPosition() { + return position; + } + + public ResourceIndexDto setPosition(int position) { + this.position = position; + return this; + } + + public int getResourceId() { + return resourceId; + } + + public ResourceIndexDto setResourceId(int resourceId) { + this.resourceId = resourceId; + return this; + } + + public int getProjectId() { + return projectId; + } + + public ResourceIndexDto setProjectId(int projectId) { + this.projectId = projectId; + return this; + } +} diff --git a/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexMapper.java b/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexMapper.java new file mode 100644 index 00000000000..110195b37ab --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/persistence/resource/ResourceIndexMapper.java @@ -0,0 +1,29 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.persistence.resource; + +import java.util.List; + +public interface ResourceIndexMapper { + + List<ResourceIndexDto> selectLikeKey(String key); + + void insert(ResourceIndexDto dto); +} diff --git a/sonar-core/src/main/resources/org/sonar/persistence/resource/ResourceIndexMapper.xml b/sonar-core/src/main/resources/org/sonar/persistence/resource/ResourceIndexMapper.xml new file mode 100644 index 00000000000..f2a98725242 --- /dev/null +++ b/sonar-core/src/main/resources/org/sonar/persistence/resource/ResourceIndexMapper.xml @@ -0,0 +1,30 @@ +<?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.persistence.resource.ResourceIndexMapper"> + + <resultMap id="resourceIndexMap" type="ResourceIndex"> + <result property="key" column="kee"/> + <result property="position" column="position"/> + <result property="resourceId" column="resource_id"/> + <result property="projectId" column="project_id"/> + </resultMap> + + <sql id="resourceIndexColumns"> + kee, position, resource_id, project_id + </sql> + + <select id="selectLikeKey" parameterType="String" resultMap="resourceIndexMap"> + select + <include refid="resourceIndexColumns"/> + from resource_index + where kee like #{id} + order by position asc + </select> + + <insert id="insert" parameterType="ResourceIndex" useGeneratedKeys="false"> + insert into resource_index (kee, position, resource_id, project_id) + values (#{key}, #{position}, #{resourceId}, #{projectId}) + </insert> +</mapper> + diff --git a/sonar-core/src/main/resources/org/sonar/persistence/rows-derby.sql b/sonar-core/src/main/resources/org/sonar/persistence/rows-derby.sql index 294d38fd155..fa0381c613c 100644 --- a/sonar-core/src/main/resources/org/sonar/persistence/rows-derby.sql +++ b/sonar-core/src/main/resources/org/sonar/persistence/rows-derby.sql @@ -165,6 +165,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('233'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('234'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('235'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('236'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('237'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '2011-09-26 22:27:48.0', '2011-09-26 22:27:48.0', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-core/src/main/resources/org/sonar/persistence/schema-derby.ddl b/sonar-core/src/main/resources/org/sonar/persistence/schema-derby.ddl index 47c09d21f48..a08c965bdcf 100644 --- a/sonar-core/src/main/resources/org/sonar/persistence/schema-derby.ddl +++ b/sonar-core/src/main/resources/org/sonar/persistence/schema-derby.ddl @@ -460,6 +460,14 @@ CREATE TABLE "LOADED_TEMPLATES" ( "TEMPLATE_TYPE" VARCHAR(15) ); +CREATE TABLE "RESOURCE_INDEX" ( + "KEE" VARCHAR(100) NOT NULL, + "POSITION" INTEGER NOT NULL, + "RESOURCE_ID" INTEGER NOT NULL, + "PROJECT_ID" INTEGER NOT NULL +); + + -- ---------------------------------------------- -- DDL Statements for indexes -- ---------------------------------------------- @@ -542,6 +550,8 @@ CREATE INDEX "MANUAL_MEASURES_RESOURCE_ID" ON "MANUAL_MEASURES" ("RESOURCE_ID"); CREATE INDEX "PROJECTS_KEE" ON "PROJECTS" ("KEE"); +CREATE INDEX "RESOURCE_INDEX_KEE" ON "RESOURCE_INDEX" ("KEE"); + -- ---------------------------------------------- -- DDL Statements for keys -- ---------------------------------------------- diff --git a/sonar-core/src/test/java/org/sonar/persistence/resource/ResourceIndexDaoTest.java b/sonar-core/src/test/java/org/sonar/persistence/resource/ResourceIndexDaoTest.java new file mode 100644 index 00000000000..8d8ec54c797 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/persistence/resource/ResourceIndexDaoTest.java @@ -0,0 +1,86 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.persistence.resource; + +import org.hamcrest.core.Is; +import org.junit.Before; +import org.junit.Test; +import org.sonar.persistence.DaoTestCase; + +import java.util.List; + +import static org.junit.Assert.assertThat; + +public class ResourceIndexDaoTest extends DaoTestCase { + + private static ResourceIndexDao dao; + + @Before + public void createDao() { + dao = new ResourceIndexDao(getMyBatis()); + } + + @Test + public void testSearch() { + setupData("testSearch"); + + List<ResourceIndexDto> rows = dao.search("zip"); + assertThat(rows.size(), Is.is(1)); + assertThat(rows.get(0).getPosition(), Is.is(0)); + assertThat(rows.get(0).getKey(), Is.is("ziputils")); + + rows = dao.search("util"); + assertThat(rows.size(), Is.is(2)); + assertThat(rows.get(0).getPosition(), Is.is(3)); + assertThat(rows.get(0).getKey(), Is.is("utils")); + assertThat(rows.get(0).getResourceId(), Is.is(10)); + + assertThat(rows.get(1).getPosition(), Is.is(4)); + assertThat(rows.get(1).getKey(), Is.is("utils")); + assertThat(rows.get(1).getResourceId(), Is.is(130)); + } + + @Test + public void shouldNotBeCaseSensitiveSearch() { + setupData("testSearch"); + + List<ResourceIndexDto> rows = dao.search("ZipU"); + assertThat(rows.size(), Is.is(1)); + assertThat(rows.get(0).getKey(), Is.is("ziputils")); + } + + @Test + public void testMinimumSizeOfSearchInput() { + setupData("testSearch"); + + List<ResourceIndexDto> rows = dao.search("zi"); + assertThat(rows.size(), Is.is(0)); + } + + @Test + public void testIndex() { + setupData("testIndex"); + + dao.index("ZipUtils", 10, 8); + + checkTables("testIndex", "resource_index"); + } + +} diff --git a/sonar-core/src/test/java/org/sonar/persistence/review/ReviewDaoTest.java b/sonar-core/src/test/java/org/sonar/persistence/review/ReviewDaoTest.java index 18c9beb948d..710edbf28b0 100644 --- a/sonar-core/src/test/java/org/sonar/persistence/review/ReviewDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/persistence/review/ReviewDaoTest.java @@ -37,12 +37,12 @@ public class ReviewDaoTest extends DaoTestCase { private ReviewDao dao; @Before - public void createDao() throws Exception { + public void createDao() { dao = new ReviewDao(getMyBatis()); } @Test - public void shouldSelectById() throws Exception { + public void shouldSelectById() { setupData("shared"); ReviewDto reviewDto = dao.selectById(100L); @@ -59,14 +59,14 @@ public class ReviewDaoTest extends DaoTestCase { } @Test - public void shouldReturnNullIfIdNotFound() throws Exception { + public void shouldReturnNullIfIdNotFound() { setupData("shared"); assertNull(dao.selectById(12345L)); } @Test - public void shouldSelectByResource() throws Exception { + public void shouldSelectByResource() { setupData("shared"); List<ReviewDto> reviewDtos = dao.selectByResource(400); @@ -78,7 +78,7 @@ public class ReviewDaoTest extends DaoTestCase { } @Test - public void shouldSelectByQuery() throws Exception { + public void shouldSelectByQuery() { setupData("shared"); List<ReviewDto> reviewDtos = dao.selectByQuery(ReviewQuery.create().setResourceId(400)); @@ -90,7 +90,7 @@ public class ReviewDaoTest extends DaoTestCase { } @Test - public void shouldSelectByQuery_booleanCriteria() throws Exception { + public void shouldSelectByQuery_booleanCriteria() { setupData("shared"); List<ReviewDto> reviewDtos = dao.selectByQuery(ReviewQuery.create().setResourceId(400).setManualViolation(true)); diff --git a/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndex-result.xml b/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndex-result.xml new file mode 100644 index 00000000000..6b11fda8cb5 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndex-result.xml @@ -0,0 +1,8 @@ +<dataset> + <resource_index kee="ziputils" position="0" resource_id="10" project_id="8"/> + <resource_index kee="iputils" position="1" resource_id="10" project_id="8"/> + <resource_index kee="putils" position="2" resource_id="10" project_id="8"/> + <resource_index kee="utils" position="3" resource_id="10" project_id="8"/> + <resource_index kee="tils" position="4" resource_id="10" project_id="8"/> + <resource_index kee="ils" position="5" resource_id="10" project_id="8"/> +</dataset>
\ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndex.xml b/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndex.xml new file mode 100644 index 00000000000..5ed00ba028b --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testIndex.xml @@ -0,0 +1 @@ +<dataset></dataset>
\ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testSearch.xml b/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testSearch.xml new file mode 100644 index 00000000000..95dc517e22c --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/persistence/resource/ResourceIndexDaoTest/testSearch.xml @@ -0,0 +1,18 @@ +<dataset> + + <!-- ZipUtils --> + <resource_index kee="ziputils" position="0" resource_id="10" project_id="8"/> + <resource_index kee="iputils" position="1" resource_id="10" project_id="8"/> + <resource_index kee="putils" position="2" resource_id="10" project_id="8"/> + <resource_index kee="utils" position="3" resource_id="10" project_id="8"/> + <resource_index kee="tils" position="4" resource_id="10" project_id="8"/> + + <!-- DateUtils --> + <resource_index kee="dateutils" position="0" resource_id="130" project_id="120"/> + <resource_index kee="ateutils" position="1" resource_id="130" project_id="120"/> + <resource_index kee="teutils" position="2" resource_id="130" project_id="120"/> + <resource_index kee="eutils" position="3" resource_id="130" project_id="120"/> + <resource_index kee="utils" position="4" resource_id="130" project_id="120"/> + <resource_index kee="tils" position="5" resource_id="130" project_id="120"/> + +</dataset>
\ No newline at end of file |