From f627842b8d37623bbf2f4bb79c28905630a0b25a Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Fri, 27 Nov 2015 10:31:57 +0100 Subject: [PATCH] Improve UT coverage --- .../batch/issue/TrackedIssueAdapter.java | 12 +- .../batch/issue/tracking/TrackedIssue.java | 62 ++++--- .../DeprecatedIssueAdapterForFilterTest.java | 157 ++++++++++++++++++ .../batch/issue/TrackedIssueAdapterTest.java | 84 ++++++++++ 4 files changed, 283 insertions(+), 32 deletions(-) create mode 100644 sonar-batch/src/test/java/org/sonar/batch/issue/DeprecatedIssueAdapterForFilterTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/issue/TrackedIssueAdapterTest.java diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/TrackedIssueAdapter.java b/sonar-batch/src/main/java/org/sonar/batch/issue/TrackedIssueAdapter.java index 575df94cd5f..5e528a6c892 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/TrackedIssueAdapter.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/TrackedIssueAdapter.java @@ -19,19 +19,17 @@ */ package org.sonar.batch.issue; -import org.sonar.api.issue.IssueComment; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.Duration; - import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; - -import org.sonar.batch.issue.tracking.TrackedIssue; import org.sonar.api.issue.Issue; +import org.sonar.api.issue.IssueComment; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.utils.Duration; +import org.sonar.batch.issue.tracking.TrackedIssue; public class TrackedIssueAdapter implements Issue { private TrackedIssue issue; @@ -127,7 +125,7 @@ public class TrackedIssueAdapter implements Issue { @Override public String attribute(String key) { - return null; + return attributes().get(key); } @Override diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/TrackedIssue.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/TrackedIssue.java index c7938b6359a..4620336f636 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/TrackedIssue.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/TrackedIssue.java @@ -19,19 +19,15 @@ */ package org.sonar.batch.issue.tracking; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; import com.google.common.base.Preconditions; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - -import org.sonar.core.issue.tracking.Trackable; - import java.io.Serializable; import java.util.Date; - +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.rule.RuleKey; +import org.sonar.core.issue.tracking.Trackable; public class TrackedIssue implements Trackable, Serializable { private static final long serialVersionUID = -1755017079070964287L; @@ -81,16 +77,18 @@ public class TrackedIssue implements Trackable, Serializable { return message; } - public void setMessage(String message) { + public TrackedIssue setMessage(String message) { this.message = message; + return this; } public String componentKey() { return componentKey; } - public void setComponentKey(String componentKey) { + public TrackedIssue setComponentKey(String componentKey) { this.componentKey = componentKey; + return this; } public String key() { @@ -106,68 +104,77 @@ public class TrackedIssue implements Trackable, Serializable { return startLine; } - public void setStartLine(Integer startLine) { + public TrackedIssue setStartLine(Integer startLine) { this.startLine = startLine; + return this; } public Integer startLineOffset() { return startLineOffset; } - public void setStartLineOffset(Integer startLineOffset) { + public TrackedIssue setStartLineOffset(Integer startLineOffset) { this.startLineOffset = startLineOffset; + return this; } public Integer endLine() { return endLine; } - public void setEndLine(Integer endLine) { + public TrackedIssue setEndLine(Integer endLine) { this.endLine = endLine; + return this; } public Integer endLineOffset() { return endLineOffset; } - public void setEndLineOffset(Integer endLineOffset) { + public TrackedIssue setEndLineOffset(Integer endLineOffset) { this.endLineOffset = endLineOffset; + return this; } - public void setKey(String key) { + public TrackedIssue setKey(String key) { this.key = key; + return this; } public String assignee() { return assignee; } - public void setAssignee(String assignee) { + public TrackedIssue setAssignee(String assignee) { this.assignee = assignee; + return this; } public String reporter() { return reporter; } - public void setReporter(String reporter) { + public TrackedIssue setReporter(String reporter) { this.reporter = reporter; + return this; } public String resolution() { return resolution; } - public void setResolution(String resolution) { + public TrackedIssue setResolution(String resolution) { this.resolution = resolution; + return this; } public String status() { return status; } - public void setStatus(String status) { + public TrackedIssue setStatus(String status) { this.status = status; + return this; } @Override @@ -191,28 +198,33 @@ public class TrackedIssue implements Trackable, Serializable { return isNew; } - public void setNew(boolean isNew) { + public TrackedIssue setNew(boolean isNew) { this.isNew = isNew; + return this; } public Date creationDate() { return creationDate; } - public void setCreationDate(Date creationDate) { + public TrackedIssue setCreationDate(Date creationDate) { this.creationDate = creationDate; + return this; } - public void setRuleKey(RuleKey ruleKey) { + public TrackedIssue setRuleKey(RuleKey ruleKey) { this.ruleKey = ruleKey; + return this; } - public void setSeverity(String severity) { + public TrackedIssue setSeverity(String severity) { this.severity = severity; + return this; } - public void setEffortToFix(Double effortToFix) { + public TrackedIssue setEffortToFix(Double effortToFix) { this.effortToFix = effortToFix; + return this; } @Override diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/DeprecatedIssueAdapterForFilterTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/DeprecatedIssueAdapterForFilterTest.java new file mode 100644 index 00000000000..169f504d1b2 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/DeprecatedIssueAdapterForFilterTest.java @@ -0,0 +1,157 @@ +/* + * 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.batch.issue; + +import java.util.Date; +import org.junit.Test; +import org.sonar.api.issue.Issue; +import org.sonar.api.resources.Project; +import org.sonar.api.rule.RuleKey; +import org.sonar.batch.protocol.Constants.Severity; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; + +public class DeprecatedIssueAdapterForFilterTest { + + private static final String PROJECT_KEY = "foo"; + private static final Date ANALYSIS_DATE = new Date(); + private static final String COMPONENT_KEY = "foo:src/Foo.java"; + + @Test + public void improve_coverage() { + DeprecatedIssueAdapterForFilter issue = new DeprecatedIssueAdapterForFilter(new Project(PROJECT_KEY).setAnalysisDate(ANALYSIS_DATE), + org.sonar.batch.protocol.output.BatchReport.Issue.newBuilder() + .setRuleRepository("repo") + .setRuleKey("key") + .setSeverity(Severity.BLOCKER) + .setMsg("msg") + .build(), + COMPONENT_KEY); + DeprecatedIssueAdapterForFilter issue2 = new DeprecatedIssueAdapterForFilter(new Project(PROJECT_KEY).setAnalysisDate(ANALYSIS_DATE), + org.sonar.batch.protocol.output.BatchReport.Issue.newBuilder() + .setRuleRepository("repo") + .setRuleKey("key") + .setSeverity(Severity.BLOCKER) + .setMsg("msg") + .setLine(1) + .setEffortToFix(2.0) + .build(), + COMPONENT_KEY); + + try { + issue.key(); + fail("Should be unsupported"); + } catch (Exception e) { + assertThat(e).isExactlyInstanceOf(UnsupportedOperationException.class).hasMessage("Not available for issues filters"); + } + + assertThat(issue.componentKey()).isEqualTo(COMPONENT_KEY); + assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "key")); + + try { + issue.language(); + fail("Should be unsupported"); + } catch (Exception e) { + assertThat(e).isExactlyInstanceOf(UnsupportedOperationException.class).hasMessage("Not available for issues filters"); + } + + assertThat(issue.severity()).isEqualTo("BLOCKER"); + assertThat(issue.message()).isEqualTo("msg"); + assertThat(issue.line()).isNull(); + assertThat(issue2.line()).isEqualTo(1); + assertThat(issue.effortToFix()).isNull(); + assertThat(issue2.effortToFix()).isEqualTo(2.0); + assertThat(issue.status()).isEqualTo(Issue.STATUS_OPEN); + assertThat(issue.resolution()).isNull(); + + try { + issue.reporter(); + fail("Should be unsupported"); + } catch (Exception e) { + assertThat(e).isExactlyInstanceOf(UnsupportedOperationException.class).hasMessage("Not available for issues filters"); + } + + assertThat(issue.assignee()).isNull(); + assertThat(issue.creationDate()).isEqualTo(ANALYSIS_DATE); + assertThat(issue.updateDate()).isNull(); + assertThat(issue.closeDate()).isNull(); + assertThat(issue.attribute(PROJECT_KEY)).isNull(); + + try { + issue.authorLogin(); + fail("Should be unsupported"); + } catch (Exception e) { + assertThat(e).isExactlyInstanceOf(UnsupportedOperationException.class).hasMessage("Not available for issues filters"); + } + + try { + issue.actionPlanKey(); + fail("Should be unsupported"); + } catch (Exception e) { + assertThat(e).isExactlyInstanceOf(UnsupportedOperationException.class).hasMessage("Not available for issues filters"); + } + + try { + issue.comments(); + fail("Should be unsupported"); + } catch (Exception e) { + assertThat(e).isExactlyInstanceOf(UnsupportedOperationException.class).hasMessage("Not available for issues filters"); + } + + try { + issue.isNew(); + fail("Should be unsupported"); + } catch (Exception e) { + assertThat(e).isExactlyInstanceOf(UnsupportedOperationException.class).hasMessage("Not available for issues filters"); + } + + try { + issue.debt(); + fail("Should be unsupported"); + } catch (Exception e) { + assertThat(e).isExactlyInstanceOf(UnsupportedOperationException.class).hasMessage("Not available for issues filters"); + } + + assertThat(issue.projectKey()).isEqualTo(PROJECT_KEY); + + try { + issue.projectUuid(); + fail("Should be unsupported"); + } catch (Exception e) { + assertThat(e).isExactlyInstanceOf(UnsupportedOperationException.class).hasMessage("Not available for issues filters"); + } + + try { + issue.componentUuid(); + fail("Should be unsupported"); + } catch (Exception e) { + assertThat(e).isExactlyInstanceOf(UnsupportedOperationException.class).hasMessage("Not available for issues filters"); + } + + try { + issue.tags(); + fail("Should be unsupported"); + } catch (Exception e) { + assertThat(e).isExactlyInstanceOf(UnsupportedOperationException.class).hasMessage("Not available for issues filters"); + } + } + +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/TrackedIssueAdapterTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/TrackedIssueAdapterTest.java new file mode 100644 index 00000000000..eead6eb0944 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/TrackedIssueAdapterTest.java @@ -0,0 +1,84 @@ +/* + * 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.batch.issue; + +import java.util.Date; +import org.junit.Test; +import org.sonar.api.issue.Issue; +import org.sonar.api.rule.RuleKey; +import org.sonar.batch.issue.tracking.TrackedIssue; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TrackedIssueAdapterTest { + + @Test + public void improve_coverage() { + Date creationDate = new Date(); + TrackedIssue trackedIssue = new TrackedIssue() + .setKey("XYZ123") + .setComponentKey("foo") + .setRuleKey(RuleKey.of("repo", "rule")) + .setSeverity("MAJOR") + .setMessage("msg") + .setStartLine(1) + .setEffortToFix(2.0) + .setStatus("RESOLVED") + .setResolution("FIXED") + .setReporter("toto") + .setAssignee("tata") + .setNew(true) + .setCreationDate(creationDate); + Issue issue = new TrackedIssueAdapter(trackedIssue); + assertThat(issue.key()).isEqualTo("XYZ123"); + assertThat(issue.componentKey()).isEqualTo("foo"); + assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule")); + assertThat(issue.severity()).isEqualTo("MAJOR"); + assertThat(issue.message()).isEqualTo("msg"); + assertThat(issue.line()).isEqualTo(1); + assertThat(issue.effortToFix()).isEqualTo(2.0); + assertThat(issue.status()).isEqualTo("RESOLVED"); + assertThat(issue.resolution()).isEqualTo("FIXED"); + assertThat(issue.reporter()).isEqualTo("toto"); + assertThat(issue.assignee()).isEqualTo("tata"); + assertThat(issue.isNew()).isTrue(); + assertThat(issue.attribute("foo")).isNull(); + assertThat(issue.creationDate()).isEqualTo(creationDate); + assertThat(issue.language()).isNull(); + assertThat(issue.updateDate()).isNull(); + assertThat(issue.closeDate()).isNull(); + assertThat(issue.authorLogin()).isNull(); + assertThat(issue.actionPlanKey()).isNull(); + assertThat(issue.comments()).isEmpty(); + assertThat(issue.debt()).isNull(); + assertThat(issue.projectKey()).isNull(); + assertThat(issue.projectUuid()).isNull(); + assertThat(issue.componentUuid()).isNull(); + assertThat(issue.tags()).isEmpty(); + + assertThat(issue).isNotEqualTo(null); + assertThat(issue).isNotEqualTo("Foo"); + assertThat(issue).isEqualTo(new TrackedIssueAdapter(trackedIssue)); + assertThat(issue.hashCode()).isEqualTo(trackedIssue.key().hashCode()); + assertThat(issue).isNotEqualTo(new TrackedIssueAdapter(new TrackedIssue() + .setKey("another"))); + } + +} -- 2.39.5