From: Julien Lancelot Date: Tue, 1 Jul 2014 16:33:57 +0000 (+0200) Subject: Fix quality flaws X-Git-Tag: 4.4-RC1~42 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=313fe9a7fb0ed72ba098265c0b45d4ea44340000;p=sonarqube.git Fix quality flaws --- diff --git a/sonar-core/src/main/java/org/sonar/core/resource/SnapshotDto.java b/sonar-core/src/main/java/org/sonar/core/resource/SnapshotDto.java index aa5207fb190..54e18053919 100644 --- a/sonar-core/src/main/java/org/sonar/core/resource/SnapshotDto.java +++ b/sonar-core/src/main/java/org/sonar/core/resource/SnapshotDto.java @@ -19,15 +19,16 @@ */ package org.sonar.core.resource; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - import java.util.Date; public final class SnapshotDto { + + private static final String INDEX_SHOULD_BE_IN_RANGE_FROM_1_TO_5 = "Index should be in range from 1 to 5"; + private Long id; private Long parentId; private Long rootId; + private Long rootProjectId; private Date date; private Date buildDate; @@ -40,7 +41,6 @@ public final class SnapshotDto { private String version; private String path; private Integer depth; - private Long rootProjectId; private String period1Mode; private String period2Mode; @@ -195,53 +195,26 @@ public final class SnapshotDto { return this; } - @CheckForNull - public String getPeriod1Mode() { - return period1Mode; - } - - public SnapshotDto setPeriod1Mode(@Nullable String period1Mode) { - this.period1Mode = period1Mode; - return this; - } - - @CheckForNull - public String getPeriod2Mode() { - return period2Mode; - } - - public SnapshotDto setPeriod2Mode(@Nullable String period2Mode) { - this.period2Mode = period2Mode; - return this; - } - - @CheckForNull - public String getPeriod3Mode() { - return period3Mode; - } - - public SnapshotDto setPeriod3Mode(@Nullable String period3Mode) { - this.period3Mode = period3Mode; - return this; - } - - @CheckForNull - public String getPeriod4Mode() { - return period4Mode; - } - - public SnapshotDto setPeriod4Mode(@Nullable String period4Mode) { - this.period4Mode = period4Mode; - return this; - } - - @CheckForNull - public String getPeriod5Mode() { - return period5Mode; - } - - public SnapshotDto setPeriod5Mode(@Nullable String period5Mode) { - this.period5Mode = period5Mode; + public SnapshotDto setPeriodMode(int index, String p) { + switch (index) { + case 1: + period1Mode = p; + break; + case 2: + period2Mode = p; + break; + case 3: + period3Mode = p; + break; + case 4: + period4Mode = p; + break; + case 5: + period5Mode = p; + break; + default: + throw new IndexOutOfBoundsException(INDEX_SHOULD_BE_IN_RANGE_FROM_1_TO_5); + } return this; } @@ -258,57 +231,30 @@ public final class SnapshotDto { case 5: return period5Mode; default: - throw new IndexOutOfBoundsException("Index of periodMode is between 1 and 5"); + throw new IndexOutOfBoundsException(INDEX_SHOULD_BE_IN_RANGE_FROM_1_TO_5); } } - @CheckForNull - public String getPeriod1Param() { - return period1Param; - } - - public SnapshotDto setPeriod1Param(@Nullable String period1Param) { - this.period1Param = period1Param; - return this; - } - - @CheckForNull - public String getPeriod2Param() { - return period2Param; - } - - public SnapshotDto setPeriod2Param(@Nullable String period2Param) { - this.period2Param = period2Param; - return this; - } - - @CheckForNull - public String getPeriod3Param() { - return period3Param; - } - - public SnapshotDto setPeriod3Param(@Nullable String period3Param) { - this.period3Param = period3Param; - return this; - } - - @CheckForNull - public String getPeriod4Param() { - return period4Param; - } - - public SnapshotDto setPeriod4Param(@Nullable String period4Param) { - this.period4Param = period4Param; - return this; - } - - @CheckForNull - public String getPeriod5Param() { - return period5Param; - } - - public SnapshotDto setPeriod5Param(@Nullable String period5Param) { - this.period5Param = period5Param; + public SnapshotDto setPeriodParam(int index, String p) { + switch (index) { + case 1: + period1Param = p; + break; + case 2: + period2Param = p; + break; + case 3: + period3Param = p; + break; + case 4: + period4Param = p; + break; + case 5: + period5Param = p; + break; + default: + throw new IndexOutOfBoundsException(INDEX_SHOULD_BE_IN_RANGE_FROM_1_TO_5); + } return this; } @@ -325,57 +271,30 @@ public final class SnapshotDto { case 5: return period5Param; default: - throw new IndexOutOfBoundsException("Index of periodModeParameter is between 1 and 5"); + throw new IndexOutOfBoundsException(INDEX_SHOULD_BE_IN_RANGE_FROM_1_TO_5); } } - @CheckForNull - public Date getPeriod1Date() { - return period1Date; - } - - public SnapshotDto setPeriod1Date(@Nullable Date period1Date) { - this.period1Date = period1Date; - return this; - } - - @CheckForNull - public Date getPeriod2Date() { - return period2Date; - } - - public SnapshotDto setPeriod2Date(@Nullable Date period2Date) { - this.period2Date = period2Date; - return this; - } - - @CheckForNull - public Date getPeriod3Date() { - return period3Date; - } - - public SnapshotDto setPeriod3Date(@Nullable Date period3Date) { - this.period3Date = period3Date; - return this; - } - - @CheckForNull - public Date getPeriod4Date() { - return period4Date; - } - - public SnapshotDto setPeriod4Date(@Nullable Date period4Date) { - this.period4Date = period4Date; - return this; - } - - @CheckForNull - public Date getPeriod5Date() { - return period5Date; - } - - public SnapshotDto setPeriod5Date(@Nullable Date period5Date) { - this.period5Date = period5Date; + public SnapshotDto setPeriodDate(int index, Date d) { + switch (index) { + case 1: + period1Date = d; + break; + case 2: + period2Date = d; + break; + case 3: + period3Date = d; + break; + case 4: + period4Date = d; + break; + case 5: + period5Date = d; + break; + default: + throw new IndexOutOfBoundsException(INDEX_SHOULD_BE_IN_RANGE_FROM_1_TO_5); + } return this; } @@ -392,7 +311,8 @@ public final class SnapshotDto { case 5: return period5Date; default: - throw new IndexOutOfBoundsException("Index of periodDate is between 1 and 5"); + throw new IndexOutOfBoundsException(INDEX_SHOULD_BE_IN_RANGE_FROM_1_TO_5); } } + } 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 index 41f3fa4c59b..0c5b71fa4bd 100644 --- a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java @@ -28,6 +28,7 @@ import org.junit.Test; import org.sonar.api.component.Component; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Scopes; +import org.sonar.api.utils.DateUtils; import org.sonar.core.component.ComponentDto; import org.sonar.core.persistence.AbstractDaoTestCase; import org.sonar.core.persistence.DbSession; @@ -443,11 +444,31 @@ public class ResourceDaoTest extends AbstractDaoTestCase { @Test public void get_last_snapshot_by_resource_id() { - setupData("fixture"); + setupData("get_last_snapshot_by_resource_id"); SnapshotDto snapshotDto = dao.getLastSnapshotByResourceId(1L, session); assertThat(snapshotDto.getId()).isEqualTo(1); + assertThat(snapshotDto.getPeriodMode(1)).isEqualTo("previous_analysis"); + assertThat(snapshotDto.getPeriodModeParameter(1)).isNull(); + assertThat(snapshotDto.getPeriodDate(1)).isNull(); + + assertThat(snapshotDto.getPeriodMode(2)).isEqualTo("days"); + assertThat(snapshotDto.getPeriodModeParameter(2)).isEqualTo("30"); + assertThat(snapshotDto.getPeriodDate(2)).isEqualTo(DateUtils.parseDate("2011-09-24")); + + assertThat(snapshotDto.getPeriodMode(3)).isEqualTo("days"); + assertThat(snapshotDto.getPeriodModeParameter(3)).isEqualTo("90"); + assertThat(snapshotDto.getPeriodDate(3)).isEqualTo(DateUtils.parseDate("2011-07-26")); + + assertThat(snapshotDto.getPeriodMode(4)).isEqualTo("previous_analysis"); + assertThat(snapshotDto.getPeriodModeParameter(4)).isNull(); + assertThat(snapshotDto.getPeriodDate(4)).isNull(); + + assertThat(snapshotDto.getPeriodMode(5)).isEqualTo("previous_version"); + assertThat(snapshotDto.getPeriodModeParameter(5)).isNull(); + assertThat(snapshotDto.getPeriodDate(5)).isNull(); + snapshotDto = dao.getLastSnapshotByResourceId(2L, session); assertThat(snapshotDto.getId()).isEqualTo(2L); diff --git a/sonar-core/src/test/java/org/sonar/core/resource/SnapshotDtoTest.java b/sonar-core/src/test/java/org/sonar/core/resource/SnapshotDtoTest.java new file mode 100644 index 00000000000..94ce88008c7 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/resource/SnapshotDtoTest.java @@ -0,0 +1,92 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.core.resource; + +import org.junit.Test; +import org.sonar.api.utils.DateUtils; + +import static org.fest.assertions.Assertions.assertThat; + +public class SnapshotDtoTest { + + @Test + public void test_getter_and_setter() throws Exception { + SnapshotDto snapshotDto = new SnapshotDto() + .setId(10L) + .setParentId(2L) + .setRootId(3L) + .setRootProjectId(20L) + .setDate(DateUtils.parseDate("2014-07-01")) + .setBuildDate(DateUtils.parseDate("2014-07-02")) + .setResourceId(21L) + .setLast(true) + .setScope("FIL") + .setQualifier("FIL") + .setVersion("1.0") + .setPath("3.2.") + .setDepth(1) + .setPeriodMode(1, "mode1") + .setPeriodMode(2, "mode2") + .setPeriodMode(3, "mode3") + .setPeriodMode(4, "mode4") + .setPeriodMode(5, "mode5") + .setPeriodParam(1, "param1") + .setPeriodParam(2, "param2") + .setPeriodParam(3, "param3") + .setPeriodParam(4, "param4") + .setPeriodParam(5, "param5") + .setPeriodDate(1, DateUtils.parseDate("2014-06-01")) + .setPeriodDate(2, DateUtils.parseDate("2014-06-02")) + .setPeriodDate(3, DateUtils.parseDate("2014-06-03")) + .setPeriodDate(4, DateUtils.parseDate("2014-06-04")) + .setPeriodDate(5, DateUtils.parseDate("2014-06-05")); + + assertThat(snapshotDto.getId()).isEqualTo(10L); + assertThat(snapshotDto.getParentId()).isEqualTo(2L); + assertThat(snapshotDto.getRootId()).isEqualTo(3L); + assertThat(snapshotDto.getRootProjectId()).isEqualTo(20L); + assertThat(snapshotDto.getDate()).isEqualTo(DateUtils.parseDate("2014-07-01")); + assertThat(snapshotDto.getBuildDate()).isEqualTo(DateUtils.parseDate("2014-07-02")); + assertThat(snapshotDto.getResourceId()).isEqualTo(21L); + assertThat(snapshotDto.getLast()).isTrue(); + assertThat(snapshotDto.getScope()).isEqualTo("FIL"); + assertThat(snapshotDto.getQualifier()).isEqualTo("FIL"); + assertThat(snapshotDto.getVersion()).isEqualTo("1.0"); + assertThat(snapshotDto.getPath()).isEqualTo("3.2."); + assertThat(snapshotDto.getDepth()).isEqualTo(1); + assertThat(snapshotDto.getPeriodMode(1)).isEqualTo("mode1"); + assertThat(snapshotDto.getPeriodMode(2)).isEqualTo("mode2"); + assertThat(snapshotDto.getPeriodMode(3)).isEqualTo("mode3"); + assertThat(snapshotDto.getPeriodMode(4)).isEqualTo("mode4"); + assertThat(snapshotDto.getPeriodMode(5)).isEqualTo("mode5"); + assertThat(snapshotDto.getPeriodModeParameter(1)).isEqualTo("param1"); + assertThat(snapshotDto.getPeriodModeParameter(2)).isEqualTo("param2"); + assertThat(snapshotDto.getPeriodModeParameter(3)).isEqualTo("param3"); + assertThat(snapshotDto.getPeriodModeParameter(4)).isEqualTo("param4"); + assertThat(snapshotDto.getPeriodModeParameter(5)).isEqualTo("param5"); + assertThat(snapshotDto.getPeriodDate(1)).isEqualTo(DateUtils.parseDate("2014-06-01")); + assertThat(snapshotDto.getPeriodDate(2)).isEqualTo(DateUtils.parseDate("2014-06-02")); + assertThat(snapshotDto.getPeriodDate(3)).isEqualTo(DateUtils.parseDate("2014-06-03")); + assertThat(snapshotDto.getPeriodDate(4)).isEqualTo(DateUtils.parseDate("2014-06-04")); + assertThat(snapshotDto.getPeriodDate(5)).isEqualTo(DateUtils.parseDate("2014-06-05")); + } + +} diff --git a/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/get_last_snapshot_by_resource_id.xml b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/get_last_snapshot_by_resource_id.xml new file mode 100644 index 00000000000..2b948c6aedc --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/resource/ResourceDaoTest/get_last_snapshot_by_resource_id.xml @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java b/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java index 13ebf05e6d5..9a0f822eef3 100644 --- a/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java +++ b/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java @@ -324,7 +324,7 @@ public class ComponentAppActionTest { addComponent(); when(resourceDao.getLastSnapshotByResourceId(eq(1L), eq(session))).thenReturn( - new SnapshotDto().setPeriod1Mode("previous_analysis").setPeriod1Date(DateUtils.parseDate("2014-05-08")) + new SnapshotDto().setPeriodMode(1, "previous_analysis").setPeriodDate(1, DateUtils.parseDate("2014-05-08")) ); when(periods.label(anyString(), anyString(), any(Date.class))).thenReturn("since previous analysis (May 08 2014)"); @@ -383,7 +383,7 @@ public class ComponentAppActionTest { Date periodDate = DateUtils.parseDate("2014-05-08"); when(resourceDao.getLastSnapshotByResourceId(eq(1L), eq(session))).thenReturn( - new SnapshotDto().setPeriod1Mode("previous_analysis").setPeriod1Date(periodDate) + new SnapshotDto().setPeriodMode(1, "previous_analysis").setPeriodDate(1, periodDate) ); when(periods.label(anyString(), anyString(), any(Date.class))).thenReturn("since previous analysis (May 08 2014)"); @@ -448,7 +448,7 @@ public class ComponentAppActionTest { private void addPeriod(){ Date periodDate = DateUtils.parseDate("2014-05-08"); when(resourceDao.getLastSnapshotByResourceId(eq(1L), eq(session))).thenReturn( - new SnapshotDto().setPeriod1Mode("previous_analysis").setPeriod1Date(periodDate) + new SnapshotDto().setPeriodMode(1, "previous_analysis").setPeriodDate(1, periodDate) ); when(periods.label(anyString(), anyString(), any(Date.class))).thenReturn("since previous analysis (May 08 2014)"); }