import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.System2;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import static org.apache.commons.lang.StringUtils.repeat;
import static org.assertj.core.api.Assertions.assertThat;
-
public class ResourceIndexDaoTest {
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
- ResourceIndexDao dao = dbTester.getDbClient().componentIndexDao();
+ ResourceIndexDao underTest = dbTester.getDbClient().componentIndexDao();
@Before
public void setUp() throws Exception {
public void shouldIndexResource() {
dbTester.prepareDbUnit(getClass(), "shouldIndexResource.xml");
- dao.indexResource(10, "ZipUtils", "FIL", 8);
+ underTest.indexResource(10, "ZipUtils", "FIL", 8);
- dbTester.assertDbUnit(getClass(), "shouldIndexResource-result.xml", new String[]{"id"}, "resource_index");
+ dbTester.assertDbUnit(getClass(), "shouldIndexResource-result.xml", new String[] {"id"}, "resource_index");
}
@Test
public void shouldIndexProjects() {
dbTester.prepareDbUnit(getClass(), "shouldIndexProjects.xml");
- dao.indexProjects();
+ underTest.indexProjects();
- dbTester.assertDbUnit(getClass(), "shouldIndexProjects-result.xml", new String[]{"id"}, "resource_index");
+ dbTester.assertDbUnit(getClass(), "shouldIndexProjects-result.xml", new String[] {"id"}, "resource_index");
}
@Test
public void shouldIndexMultiModulesProject() {
dbTester.prepareDbUnit(getClass(), "shouldIndexMultiModulesProject.xml");
- dao.indexProject(1);
+ underTest.indexProject(1);
- dbTester.assertDbUnit(getClass(), "shouldIndexMultiModulesProject-result.xml", new String[]{"id"}, "resource_index");
+ dbTester.assertDbUnit(getClass(), "shouldIndexMultiModulesProject-result.xml", new String[] {"id"}, "resource_index");
}
@Test
public void shouldReindexProjectAfterRenaming() {
dbTester.prepareDbUnit(getClass(), "shouldReindexProjectAfterRenaming.xml");
- dao.indexProject(1);
+ underTest.indexProject(1);
- dbTester.assertDbUnit(getClass(), "shouldReindexProjectAfterRenaming-result.xml", new String[]{"id"}, "resource_index");
+ dbTester.assertDbUnit(getClass(), "shouldReindexProjectAfterRenaming-result.xml", new String[] {"id"}, "resource_index");
}
@Test
public void shouldNotIndexPackages() {
dbTester.prepareDbUnit(getClass(), "shouldNotIndexPackages.xml");
- dao.indexProject(1);
+ underTest.indexProject(1);
// project
assertThat(dbTester.countSql("select count(1) from resource_index where resource_id=1")).isGreaterThan(0);
// directory
public void shouldIndexTwoLettersLongResources() {
dbTester.prepareDbUnit(getClass(), "shouldIndexTwoLettersLongResource.xml");
- dao.indexResource(10, "AB", Qualifiers.PROJECT, 3);
+ underTest.indexResource(10, "AB", Qualifiers.PROJECT, 3);
- dbTester.assertDbUnit(getClass(), "shouldIndexTwoLettersLongResource-result.xml", new String[]{"id"}, "resource_index");
+ dbTester.assertDbUnit(getClass(), "shouldIndexTwoLettersLongResource-result.xml", new String[] {"id"}, "resource_index");
}
@Test
public void shouldReIndexTwoLettersLongResources() {
dbTester.prepareDbUnit(getClass(), "shouldReIndexTwoLettersLongResource.xml");
- dao.indexResource(1, "AS", Qualifiers.PROJECT, 1);
+ underTest.indexResource(1, "AS", Qualifiers.PROJECT, 1);
- dbTester.assertDbUnit(getClass(), "shouldReIndexTwoLettersLongResource-result.xml", new String[]{"id"}, "resource_index");
+ dbTester.assertDbUnit(getClass(), "shouldReIndexTwoLettersLongResource-result.xml", new String[] {"id"}, "resource_index");
}
@Test
public void shouldReIndexNewTwoLettersLongResource() {
dbTester.prepareDbUnit(getClass(), "shouldReIndexNewTwoLettersLongResource.xml");
- dao.indexResource(1, "AS", Qualifiers.PROJECT, 1);
+ underTest.indexResource(1, "AS", Qualifiers.PROJECT, 1);
- dbTester.assertDbUnit(getClass(), "shouldReIndexNewTwoLettersLongResource-result.xml", new String[]{"id"}, "resource_index");
+ dbTester.assertDbUnit(getClass(), "shouldReIndexNewTwoLettersLongResource-result.xml", new String[] {"id"}, "resource_index");
}
@Test
public void shouldReindexResource() {
dbTester.prepareDbUnit(getClass(), "shouldReindexResource.xml");
- dao.indexResource(1, "New Struts", Qualifiers.PROJECT, 1);
+ underTest.indexResource(1, "New Struts", Qualifiers.PROJECT, 1);
- dbTester.assertDbUnit(getClass(), "shouldReindexResource-result.xml", new String[]{"id"}, "resource_index");
+ dbTester.assertDbUnit(getClass(), "shouldReindexResource-result.xml", new String[] {"id"}, "resource_index");
}
@Test
public void shouldNotReindexUnchangedResource() {
dbTester.prepareDbUnit(getClass(), "shouldNotReindexUnchangedResource.xml");
- dao.indexResource(1, "Struts", Qualifiers.PROJECT, 1);
+ underTest.indexResource(1, "Struts", Qualifiers.PROJECT, 1);
- dbTester.assertDbUnit(getClass(), "shouldNotReindexUnchangedResource-result.xml", new String[]{"id"}, "resource_index");
+ dbTester.assertDbUnit(getClass(), "shouldNotReindexUnchangedResource-result.xml", new String[] {"id"}, "resource_index");
}
@Test
String viewUuid = "EFGH";
DbSession session = dbTester.getSession();
- assertThat(dao.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "project", viewUuid)).containsOnly(1L, 2L);
- assertThat(dao.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "one", viewUuid)).containsOnly(1L);
- assertThat(dao.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "two", viewUuid)).containsOnly(2L);
- assertThat(dao.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "unknown", viewUuid)).isEmpty();
+ assertThat(underTest.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "project", viewUuid)).containsOnly(1L, 2L);
+ assertThat(underTest.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "one", viewUuid)).containsOnly(1L);
+ assertThat(underTest.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "two", viewUuid)).containsOnly(2L);
+ assertThat(underTest.selectProjectIdsFromQueryAndViewOrSubViewUuid(session, "unknown", viewUuid)).isEmpty();
+ }
+
+ /**
+ * SONAR-7594
+ * PROJECTS.NAME is 2'000 characters long whereas RESOURCE_INDEX.KEE is only 400 characters. As there's no functional need
+ * to increase size of the latter column, indexed values must be truncated.
+ */
+ @Test
+ public void restrict_indexed_combinations_to_400_characters() {
+ String longName = repeat("a", 2_000);
+ ComponentDto project = new ComponentDto().setId(1L).setKey("the_key").setName(longName).setScope(Scopes.PROJECT).setQualifier(Qualifiers.PROJECT);
+ DbSession session = dbTester.getSession();
+ dbTester.getDbClient().componentDao().insert(session, project);
+ dbTester.getDbClient().snapshotDao().insert(session, new SnapshotDto().setComponentId(project.getId()).setRootProjectId(project.getId()).setLast(true));
+
+ underTest.indexProject(project.getId(), session);
+ session.commit();
+
+ assertThat(dbTester.countRowsOfTable("resource_index")).isEqualTo(longName.length() - ResourceIndexDao.MINIMUM_KEY_SIZE + 1);
}
}