From: Simon Brandhof Date: Fri, 10 Feb 2017 00:08:56 +0000 (+0100) Subject: SONAR-8761 clean-up ResourceDao X-Git-Tag: 6.3-RC1~17 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f22279cfd17b1af30e631ee305acdb59c3f7ab21;p=sonarqube.git SONAR-8761 clean-up ResourceDao --- diff --git a/sonar-db/src/main/java/org/sonar/db/AbstractDao.java b/sonar-db/src/main/java/org/sonar/db/AbstractDao.java deleted file mode 100644 index fddc22c3890..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/AbstractDao.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program 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. - * - * This program 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.db; - -import org.sonar.api.utils.System2; - -public abstract class AbstractDao implements Dao { - - private final MyBatis myBatis; - private final System2 system2; - - public AbstractDao(MyBatis myBatis, System2 system2) { - this.myBatis = myBatis; - this.system2 = system2; - } - - protected MyBatis myBatis() { - return myBatis; - } - - protected long now() { - return system2.now(); - } -} 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 107cb5c2164..70c84928174 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 @@ -19,102 +19,20 @@ */ package org.sonar.db.component; -import java.util.List; -import javax.annotation.CheckForNull; import org.apache.ibatis.session.SqlSession; import org.sonar.api.utils.System2; -import org.sonar.db.AbstractDao; -import org.sonar.db.DbSession; -import org.sonar.db.MyBatis; +import org.sonar.db.Dao; -public class ResourceDao extends AbstractDao { +public class ResourceDao implements Dao { - public ResourceDao(MyBatis myBatis, System2 system2) { - super(myBatis, system2); - } - - @CheckForNull - private static ResourceDto selectResource(ResourceQuery query, DbSession session) { - List resources = getResources(query, session); - if (!resources.isEmpty()) { - return resources.get(0); - } - return null; - } - - private static List getResources(ResourceQuery query, SqlSession session) { - return session.getMapper(ResourceMapper.class).selectResources(query); - } - - @CheckForNull - public ResourceDto selectResource(String componentUuid) { - SqlSession session = myBatis().openSession(false); - try { - return selectResource(componentUuid, session); - } finally { - MyBatis.closeQuietly(session); - } - } + private final System2 system2; - @CheckForNull - private static ResourceDto selectResource(String componentUuid, SqlSession session) { - return session.getMapper(ResourceMapper.class).selectResourceByUuid(componentUuid); + public ResourceDao(System2 system2) { + this.system2 = system2; } public void updateAuthorizationDate(Long projectId, SqlSession session) { - session.getMapper(ResourceMapper.class).updateAuthorizationDate(projectId, now()); - } - - /** - * Return the root project of a component. - * Will return the component itself if it's already the root project - * Can return null if the component does not exists. - * - * The implementation should rather use a new column already containing the root project, see https://jira.sonarsource.com/browse/SONAR-5188. - */ - @CheckForNull - private static ResourceDto getRootProjectByComponentKey(DbSession session, String componentKey) { - ResourceDto component = selectResource(ResourceQuery.create().setKey(componentKey), session); - if (component != null) { - String rootUuid = component.getRootUuid(); - if (rootUuid.equals(component.getUuid())) { - return component; - } else { - return getParentModuleByComponentUuid(rootUuid, session); - } - } - return null; - } - - @CheckForNull - public ResourceDto getRootProjectByComponentKey(String componentKey) { - DbSession session = myBatis().openSession(false); - try { - return getRootProjectByComponentKey(session, componentKey); - } finally { - MyBatis.closeQuietly(session); - } - } - - @CheckForNull - private static ResourceDto getParentModuleByComponentUuid(String componentUUid, DbSession session) { - ResourceDto component = selectResource(componentUUid, session); - if (component != null) { - String rootUuid = component.getRootUuid(); - if (rootUuid.equals(component.getUuid())) { - return component; - } else { - return getParentModuleByComponentUuid(rootUuid, session); - } - } - return null; - } - - /** - * Return provisioned project with given key - */ - public ResourceDto selectProvisionedProject(DbSession session, String key) { - return session.getMapper(ResourceMapper.class).selectProvisionedProject(key); + session.getMapper(ResourceMapper.class).updateAuthorizationDate(projectId, system2.now()); } } diff --git a/sonar-db/src/main/java/org/sonar/db/component/ResourceMapper.java b/sonar-db/src/main/java/org/sonar/db/component/ResourceMapper.java index fc7c805a74e..06bc63393c9 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/ResourceMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/component/ResourceMapper.java @@ -19,15 +19,9 @@ */ package org.sonar.db.component; -import java.util.List; import org.apache.ibatis.annotations.Param; public interface ResourceMapper { - ResourceDto selectResourceByUuid(String uuid); - - List selectResources(ResourceQuery query); - - ResourceDto selectProvisionedProject(@Param("key") String key); void updateAuthorizationDate(@Param("projectId") Long projectId, @Param("authorizationDate") Long authorizationDate); diff --git a/sonar-db/src/main/java/org/sonar/db/component/ResourceQuery.java b/sonar-db/src/main/java/org/sonar/db/component/ResourceQuery.java deleted file mode 100644 index df0fb7c79d3..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/component/ResourceQuery.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program 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. - * - * This program 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.db.component; - -/** - * @since 3.0 - */ -public class ResourceQuery { - private String[] qualifiers = null; - private String key = null; - private boolean excludeDisabled = false; - - private ResourceQuery() { - } - - public static ResourceQuery create() { - return new ResourceQuery(); - } - - public String[] getQualifiers() { - return qualifiers; - } - - public ResourceQuery setQualifiers(String[] qualifiers) { - this.qualifiers = qualifiers; - return this; - } - - public String getKey() { - return key; - } - - public ResourceQuery setKey(String key) { - this.key = key; - return this; - } - - public boolean isExcludeDisabled() { - return excludeDisabled; - } - - public ResourceQuery setExcludeDisabled(boolean b) { - this.excludeDisabled = b; - return this; - } -} diff --git a/sonar-db/src/main/resources/org/sonar/db/component/ResourceMapper.xml b/sonar-db/src/main/resources/org/sonar/db/component/ResourceMapper.xml index 81e6f131052..f6488d64de7 100644 --- a/sonar-db/src/main/resources/org/sonar/db/component/ResourceMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/component/ResourceMapper.xml @@ -3,78 +3,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - update projects set authorization_updated_at=#{authorizationDate} where id=#{projectId} 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 9008f263530..aa2a4b72132 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 @@ -24,7 +24,6 @@ import org.junit.Test; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; -import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -37,37 +36,6 @@ public class ResourceDaoTest { private ResourceDao underTest = dbTester.getDbClient().resourceDao(); - @Test - public void get_resource_by_uuid() { - dbTester.prepareDbUnit(getClass(), "fixture.xml"); - - ResourceDto resource = underTest.selectResource("ABCD"); - - assertThat(resource.getUuid()).isEqualTo("ABCD"); - assertThat(resource.getProjectUuid()).isEqualTo("ABCD"); - assertThat(resource.getPath()).isNull(); - assertThat(resource.getName()).isEqualTo("Struts"); - assertThat(resource.getLongName()).isEqualTo("Apache Struts"); - assertThat(resource.getScope()).isEqualTo("PRJ"); - assertThat(resource.getDescription()).isEqualTo("the description"); - assertThat(resource.getLanguage()).isEqualTo("java"); - assertThat(resource.isEnabled()).isTrue(); - assertThat(resource.getAuthorizationUpdatedAt()).isNotNull(); - assertThat(resource.getCreatedAt()).isNotNull(); - } - - @Test - public void find_root_project_by_component_key() { - dbTester.prepareDbUnit(getClass(), "fixture.xml"); - - assertThat(underTest.getRootProjectByComponentKey("org.struts:struts-core:src/org/struts/RequestContext.java").getKey()).isEqualTo("org.struts:struts"); - assertThat(underTest.getRootProjectByComponentKey("org.struts:struts-core:src/org/struts").getKey()).isEqualTo("org.struts:struts"); - assertThat(underTest.getRootProjectByComponentKey("org.struts:struts-core").getKey()).isEqualTo("org.struts:struts"); - assertThat(underTest.getRootProjectByComponentKey("org.struts:struts").getKey()).isEqualTo("org.struts:struts"); - - assertThat(underTest.getRootProjectByComponentKey("unknown")).isNull(); - } - @Test public void update_authorization_date() { dbTester.prepareDbUnit(getClass(), "update_authorization_date.xml"); diff --git a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/fixture-including-ghost-projects-and-technical-project.xml b/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/fixture-including-ghost-projects-and-technical-project.xml deleted file mode 100644 index 6e388d370f8..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/fixture-including-ghost-projects-and-technical-project.xml +++ /dev/null @@ -1,372 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/fixture.xml b/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/fixture.xml deleted file mode 100644 index 84308102cde..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/fixture.xml +++ /dev/null @@ -1,153 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/getResources_exclude_disabled.xml b/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/getResources_exclude_disabled.xml deleted file mode 100644 index 9887c0c19f5..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/getResources_exclude_disabled.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - diff --git a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/insert-result.xml b/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/insert-result.xml deleted file mode 100644 index 7d74a43413e..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/insert-result.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - diff --git a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/insert.xml b/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/insert.xml deleted file mode 100644 index 871dedcb5e9..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/insert.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/update-result.xml b/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/update-result.xml deleted file mode 100644 index 0f72d1716ab..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/update-result.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - diff --git a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/update.xml b/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/update.xml deleted file mode 100644 index 106c3b93d30..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/component/ResourceDaoTest/update.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - -