diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-02-06 13:44:20 +0100 |
---|---|---|
committer | Fabrice Bellingard <bellingard@gmail.com> | 2012-02-06 16:27:33 +0100 |
commit | 93a7bc8f307a431f2a1cb82b8401c4fd48042e79 (patch) | |
tree | b2339310da0cb699ff650c637330105ed6031bce /sonar-core/src | |
parent | ec02a8faea5132013a21349ef75872bbb888a8f4 (diff) | |
download | sonarqube-93a7bc8f307a431f2a1cb82b8401c4fd48042e79.tar.gz sonarqube-93a7bc8f307a431f2a1cb82b8401c4fd48042e79.zip |
Fix some quality flaws
Diffstat (limited to 'sonar-core/src')
6 files changed, 187 insertions, 7 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java index de93c7b503b..baf8fc38680 100644 --- a/sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java +++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeDao.java @@ -43,7 +43,8 @@ public class PurgeDao { SqlSession session = mybatis.openBatchSession(); PurgeMapper purgeMapper = session.getMapper(PurgeMapper.class); try { - List<Long> projectIds = resourceDao.getDescendantProjectIdsAndSelf(rootProjectId, session); + List<Long> projectIds = Lists.newArrayList(rootProjectId); + projectIds.addAll(resourceDao.getDescendantProjectIds(rootProjectId, session)); for (Long projectId : projectIds) { purgeProject(projectId, session, purgeMapper); } diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java index b9b1b44be9d..5e9d085372f 100644 --- a/sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java +++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java @@ -51,7 +51,7 @@ public final class PurgeSnapshotQuery { } public PurgeSnapshotQuery setScopes(String[] scopes) { - this.scopes = scopes; + this.scopes = scopes; //NOSONAR May expose internal representation by incorporating reference to mutable object return this; } @@ -60,7 +60,7 @@ public final class PurgeSnapshotQuery { } public PurgeSnapshotQuery setQualifiers(String[] qualifiers) { - this.qualifiers = qualifiers; + this.qualifiers = qualifiers;//NOSONAR May expose internal representation by incorporating reference to mutable object return this; } diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java index 3703c6b7ed4..421eca7304d 100644 --- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java +++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java @@ -32,16 +32,16 @@ public class ResourceDao { this.mybatis = mybatis; } - public List<Long> getDescendantProjectIdsAndSelf(long projectId) { + public List<Long> getDescendantProjectIds(long projectId) { SqlSession session = mybatis.openSession(); try { - return getDescendantProjectIdsAndSelf(projectId, session); + return getDescendantProjectIds(projectId, session); } finally { MyBatis.closeQuietly(session); } } - public List<Long> getDescendantProjectIdsAndSelf(long projectId, SqlSession session) { + public List<Long> getDescendantProjectIds(long projectId, SqlSession session) { ResourceMapper mapper = session.getMapper(ResourceMapper.class); List<Long> ids = Lists.newArrayList(); appendChildProjectIds(projectId, mapper, ids); @@ -49,7 +49,6 @@ public class ResourceDao { } private void appendChildProjectIds(long projectId, ResourceMapper mapper, List<Long> ids) { - ids.add(projectId); List<Long> subProjectIds = mapper.selectDescendantProjectIds(projectId); for (Long subProjectId : subProjectIds) { ids.add(subProjectId); diff --git a/sonar-core/src/test/java/org/sonar/core/purge/PurgeableSnapshotDtoTest.java b/sonar-core/src/test/java/org/sonar/core/purge/PurgeableSnapshotDtoTest.java new file mode 100644 index 00000000000..48fb4907e49 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/purge/PurgeableSnapshotDtoTest.java @@ -0,0 +1,56 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 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.core.purge; + +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.number.OrderingComparisons.greaterThan; +import static org.junit.Assert.assertThat; + +public class PurgeableSnapshotDtoTest { + @Test + public void testEquals() { + PurgeableSnapshotDto dto1 = new PurgeableSnapshotDto().setSnapshotId(3L); + PurgeableSnapshotDto dto2 = new PurgeableSnapshotDto().setSnapshotId(4L); + assertThat(dto1.equals(dto2), is(false)); + assertThat(dto2.equals(dto1), is(false)); + assertThat(dto1.equals(dto1), is(true)); + assertThat(dto1.equals(new PurgeableSnapshotDto().setSnapshotId(3L)), is(true)); + assertThat(dto1.equals("bi_bop_a_lou_la"), is(false)); + assertThat(dto1.equals(null), is(false)); + } + + @Test + public void testHasCode() { + PurgeableSnapshotDto dto = new PurgeableSnapshotDto().setSnapshotId(3L); + assertThat(dto.hashCode(), is(dto.hashCode())); + + // no id + dto = new PurgeableSnapshotDto(); + assertThat(dto.hashCode(), is(dto.hashCode())); + } + + @Test + public void testToString() { + PurgeableSnapshotDto dto = new PurgeableSnapshotDto().setSnapshotId(3L); + assertThat(dto.toString().length(), greaterThan(0)); + } +} diff --git a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java new file mode 100644 index 00000000000..0e22ee43154 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java @@ -0,0 +1,63 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 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.core.resource; + +import org.hamcrest.core.Is; +import org.junit.Before; +import org.junit.Test; +import org.sonar.core.persistence.DaoTestCase; + +import java.util.List; + +import static org.hamcrest.core.IsNot.not; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertThat; +import static org.junit.matchers.JUnitMatchers.hasItems; + +public class ResourceDaoTest extends DaoTestCase { + + private ResourceDao dao; + + @Before + public void createDao() { + dao = new ResourceDao(getMyBatis()); + } + + @Test + public void testDescendantProjectIdsAndSelf() { + setupData("fixture"); + + List<Long> ids = dao.getDescendantProjectIds(1L); + + assertThat(ids, hasItems(2L)); + assertThat(ids.size(), Is.is(1)); + } + + @Test + public void testDescendantProjectIdsAndSelf_id_not_found() { + setupData("fixture"); + + List<Long> ids = dao.getDescendantProjectIds(33333L); + + assertThat(ids, not(nullValue())); + assertThat(ids.size(), Is.is(0)); + } +} + diff --git a/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/fixture.xml b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/fixture.xml new file mode 100644 index 00000000000..41ab615c627 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/fixture.xml @@ -0,0 +1,61 @@ +<dataset> + + <!-- root project --> + <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts" + description="[null]" long_name="Struts" + enabled="[true]" language="java" copy_resource_id="[null]"/> + <snapshots id="1" project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]" + status="P" islast="[false]" purge_status="[null]" + period1_mode="[null]" period1_param="[null]" period1_date="[null]" + period2_mode="[null]" period2_param="[null]" period2_date="[null]" + period3_mode="[null]" period3_param="[null]" period3_date="[null]" + period4_mode="[null]" period4_param="[null]" period4_date="[null]" + period5_mode="[null]" period5_param="[null]" period5_date="[null]" + depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00" + version="[null]" path="[null]"/> + + <!-- project --> + <projects id="2" root_id="1" kee="org.struts:struts-core" name="Struts Core" + scope="PRJ" qualifier="BRC" long_name="Struts Core" + description="[null]" enabled="[true]" language="java" copy_resource_id="[null]"/> + <snapshots id="2" project_id="2" parent_snapshot_id="1" root_project_id="1" root_snapshot_id="1" + status="P" islast="[false]" purge_status="[null]" + period1_mode="[null]" period1_param="[null]" period1_date="[null]" + period2_mode="[null]" period2_param="[null]" period2_date="[null]" + period3_mode="[null]" period3_param="[null]" period3_date="[null]" + period4_mode="[null]" period4_param="[null]" period4_date="[null]" + period5_mode="[null]" period5_param="[null]" period5_date="[null]" + depth="[null]" scope="PRJ" qualifier="BRC" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00" + version="[null]" path="[null]"/> + + <!-- directory --> + <projects long_name="org.struts" id="3" scope="DIR" qualifier="PAC" kee="org.struts:struts:org.struts" + name="org.struts" root_id="1" + description="[null]" + enabled="[true]" language="java" copy_resource_id="[null]"/> + <snapshots id="3" project_id="3" parent_snapshot_id="2" root_project_id="1" root_snapshot_id="1" + status="P" islast="[false]" purge_status="[null]" + period1_mode="[null]" period1_param="[null]" period1_date="[null]" + period2_mode="[null]" period2_param="[null]" period2_date="[null]" + period3_mode="[null]" period3_param="[null]" period3_date="[null]" + period4_mode="[null]" period4_param="[null]" period4_date="[null]" + period5_mode="[null]" period5_param="[null]" period5_date="[null]" + depth="[null]" scope="DIR" qualifier="PAC" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00" + version="[null]" path="[null]"/> + + <!-- file --> + <projects long_name="org.struts.RequestContext" id="4" scope="FIL" qualifier="CLA" kee="org.struts:struts:org.struts.RequestContext" + name="RequestContext" root_id="1" + description="[null]" + enabled="[true]" language="java" copy_resource_id="[null]"/> + + <snapshots id="4" project_id="4" parent_snapshot_id="3" root_project_id="1" root_snapshot_id="1" + status="P" islast="[false]" purge_status="[null]" + period1_mode="[null]" period1_param="[null]" period1_date="[null]" + period2_mode="[null]" period2_param="[null]" period2_date="[null]" + period3_mode="[null]" period3_param="[null]" period3_date="[null]" + period4_mode="[null]" period4_param="[null]" period4_date="[null]" + period5_mode="[null]" period5_param="[null]" period5_date="[null]" + depth="[null]" scope="FIL" qualifier="CLA" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00" + version="[null]" path="[null]"/> +</dataset> |