From 93a7bc8f307a431f2a1cb82b8401c4fd48042e79 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 6 Feb 2012 13:44:20 +0100 Subject: [PATCH] Fix some quality flaws --- .../core/sensors/ViolationsDecorator.java | 2 +- .../timemachine/NewViolationsDecorator.java | 2 +- .../sonar/batch/index/MeasurePersister.java | 2 +- .../java/org/sonar/core/purge/PurgeDao.java | 3 +- .../sonar/core/purge/PurgeSnapshotQuery.java | 4 +- .../org/sonar/core/resource/ResourceDao.java | 7 +-- .../core/purge/PurgeableSnapshotDtoTest.java | 56 +++++++++++++++++ .../sonar/core/resource/ResourceDaoTest.java | 63 +++++++++++++++++++ .../core/resource/ResourceDaoTest/fixture.xml | 61 ++++++++++++++++++ 9 files changed, 190 insertions(+), 10 deletions(-) create mode 100644 sonar-core/src/test/java/org/sonar/core/purge/PurgeableSnapshotDtoTest.java create mode 100644 sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java create mode 100644 sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/fixture.xml diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ViolationsDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ViolationsDecorator.java index c739a57e287..eb4ade51a18 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ViolationsDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ViolationsDecorator.java @@ -113,7 +113,7 @@ public class ViolationsDecorator implements Decorator { if (rulesBag != null) { for (Multiset.Entry entry : rulesBag.entrySet()) { RuleMeasure measure = RuleMeasure.createForRule(metric, entry.getElement(), (double) entry.getCount()); - measure.setRulePriority(severity); + measure.setSeverity(severity); context.saveMeasure(measure); } } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java index 48b97f0a60e..96c4e6aa589 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java @@ -156,7 +156,7 @@ public class NewViolationsDecorator implements Decorator { for (Rule rule : rules) { RuleMeasure measure = RuleMeasure.createForRule(metric, rule, null); - measure.setRulePriority(severity); + measure.setSeverity(severity); for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) { int variationIndex = pastSnapshot.getIndex(); int count = countViolations(violationsPerRule.get(rule), pastSnapshot.getTargetDate()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java index 620519345f0..8552118e54c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java @@ -158,7 +158,7 @@ public final class MeasurePersister { } if (measure instanceof RuleMeasure) { RuleMeasure ruleMeasure = (RuleMeasure) measure; - merge.setRulePriority(ruleMeasure.getRulePriority()); + merge.setRulePriority(ruleMeasure.getSeverity()); if (ruleMeasure.getRule() != null) { Rule ruleWithId = ruleFinder.findByKey(ruleMeasure.getRule().getRepositoryKey(), ruleMeasure.getRule().getKey()); if (ruleWithId != null) { 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 projectIds = resourceDao.getDescendantProjectIdsAndSelf(rootProjectId, session); + List 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 getDescendantProjectIdsAndSelf(long projectId) { + public List getDescendantProjectIds(long projectId) { SqlSession session = mybatis.openSession(); try { - return getDescendantProjectIdsAndSelf(projectId, session); + return getDescendantProjectIds(projectId, session); } finally { MyBatis.closeQuietly(session); } } - public List getDescendantProjectIdsAndSelf(long projectId, SqlSession session) { + public List getDescendantProjectIds(long projectId, SqlSession session) { ResourceMapper mapper = session.getMapper(ResourceMapper.class); List ids = Lists.newArrayList(); appendChildProjectIds(projectId, mapper, ids); @@ -49,7 +49,6 @@ public class ResourceDao { } private void appendChildProjectIds(long projectId, ResourceMapper mapper, List ids) { - ids.add(projectId); List 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 ids = dao.getDescendantProjectIds(1L); + + assertThat(ids, hasItems(2L)); + assertThat(ids.size(), Is.is(1)); + } + + @Test + public void testDescendantProjectIdsAndSelf_id_not_found() { + setupData("fixture"); + + List 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 @@ + + + + + + + + + + + + + + + + + + + -- 2.39.5