From 6f3f992a81ea8b4c91a88dfd668284f0f428f476 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 22 May 2013 09:16:05 +0200 Subject: [PATCH] SONAR-3755 fix quality flaws --- .../core/issue/db/ChangeDtoConverter.java | 79 ------------------- .../sonar/core/issue/db/IssueChangeDao.java | 4 +- .../sonar/core/issue/db/IssueChangeDto.java | 48 +++++++++++ .../org/sonar/core/issue/db/IssueStorage.java | 8 +- ...erterTest.java => IssueChangeDtoTest.java} | 10 +-- .../java/org/sonar/api/issue/IssueQuery.java | 23 ++++-- .../server/issue/IssueCommentService.java | 3 +- 7 files changed, 76 insertions(+), 99 deletions(-) delete mode 100644 sonar-core/src/main/java/org/sonar/core/issue/db/ChangeDtoConverter.java rename sonar-core/src/test/java/org/sonar/core/issue/db/{ChangeDtoConverterTest.java => IssueChangeDtoTest.java} (87%) diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/ChangeDtoConverter.java b/sonar-core/src/main/java/org/sonar/core/issue/db/ChangeDtoConverter.java deleted file mode 100644 index f1b0af58f17..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/ChangeDtoConverter.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 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.issue.db; - -import org.sonar.core.issue.DefaultIssueComment; -import org.sonar.core.issue.FieldDiffs; - -import java.util.Date; - -public class ChangeDtoConverter { - - private ChangeDtoConverter() { - // only static methods - } - - public static IssueChangeDto commentToDto(DefaultIssueComment comment) { - IssueChangeDto dto = newDto(comment.issueKey()); - dto.setKey(comment.key()); - dto.setChangeType(IssueChangeDto.TYPE_COMMENT); - dto.setChangeData(comment.markdownText()); - dto.setUserLogin(comment.userLogin()); - return dto; - } - - public static IssueChangeDto changeToDto(String issueKey, FieldDiffs diffs) { - IssueChangeDto dto = newDto(issueKey); - dto.setChangeType(IssueChangeDto.TYPE_FIELD_CHANGE); - dto.setChangeData(diffs.toString()); - dto.setUserLogin(diffs.userLogin()); - return dto; - } - - private static IssueChangeDto newDto(String issueKey) { - IssueChangeDto dto = new IssueChangeDto(); - dto.setIssueKey(issueKey); - - // technical dates - do not use the context date - Date now = new Date(); - dto.setCreatedAt(now); - dto.setUpdatedAt(new Date()); - return dto; - } - - public static DefaultIssueComment dtoToComment(IssueChangeDto dto) { - return new DefaultIssueComment() - .setMarkdownText(dto.getChangeData()) - .setKey(dto.getKey()) - .setCreatedAt(dto.getCreatedAt()) - .setUpdatedAt(dto.getUpdatedAt()) - .setUserLogin(dto.getUserLogin()) - .setIssueKey(dto.getIssueKey()) - .setNew(false); - } - - public static FieldDiffs dtoToChange(IssueChangeDto dto) { - FieldDiffs diffs = FieldDiffs.parse(dto.getChangeData()); - diffs.setUserLogin(dto.getUserLogin()); - diffs.setCreatedAt(dto.getCreatedAt()); - diffs.setUpdatedAt(dto.getUpdatedAt()); - return diffs; - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueChangeDao.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueChangeDao.java index e4dfa410554..1223f83d989 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueChangeDao.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueChangeDao.java @@ -53,7 +53,7 @@ public class IssueChangeDao implements BatchComponent, ServerComponent { try { IssueChangeMapper mapper = session.getMapper(IssueChangeMapper.class); IssueChangeDto dto = mapper.selectByKeyAndType(commentKey, IssueChangeDto.TYPE_COMMENT); - return dto != null ? ChangeDtoConverter.dtoToComment(dto) : null; + return dto != null ? dto.toComment() : null; } finally { MyBatis.closeQuietly(session); @@ -67,7 +67,7 @@ public class IssueChangeDao implements BatchComponent, ServerComponent { IssueChangeMapper mapper = session.getMapper(IssueChangeMapper.class); List dtos = mapper.selectByIssuesAndType(issueKeys, changeType); for (IssueChangeDto dto : dtos) { - result.add(ChangeDtoConverter.dtoToComment(dto)); + result.add(dto.toComment()); } } return result; diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueChangeDto.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueChangeDto.java index 1f9069d8c44..b1a6321394e 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueChangeDto.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueChangeDto.java @@ -21,6 +21,8 @@ package org.sonar.core.issue.db; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; +import org.sonar.core.issue.DefaultIssueComment; +import org.sonar.core.issue.FieldDiffs; import javax.annotation.CheckForNull; import javax.annotation.Nullable; @@ -121,4 +123,50 @@ public final class IssueChangeDto { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); } + public static IssueChangeDto of(DefaultIssueComment comment) { + IssueChangeDto dto = newDto(comment.issueKey()); + dto.setKey(comment.key()); + dto.setChangeType(IssueChangeDto.TYPE_COMMENT); + dto.setChangeData(comment.markdownText()); + dto.setUserLogin(comment.userLogin()); + return dto; + } + + public static IssueChangeDto of(String issueKey, FieldDiffs diffs) { + IssueChangeDto dto = newDto(issueKey); + dto.setChangeType(IssueChangeDto.TYPE_FIELD_CHANGE); + dto.setChangeData(diffs.toString()); + dto.setUserLogin(diffs.userLogin()); + return dto; + } + + private static IssueChangeDto newDto(String issueKey) { + IssueChangeDto dto = new IssueChangeDto(); + dto.setIssueKey(issueKey); + + // technical dates - do not use the context date + Date now = new Date(); + dto.setCreatedAt(now); + dto.setUpdatedAt(new Date()); + return dto; + } + + public DefaultIssueComment toComment() { + return new DefaultIssueComment() + .setMarkdownText(changeData) + .setKey(kee) + .setCreatedAt(createdAt) + .setUpdatedAt(updatedAt) + .setUserLogin(userLogin) + .setIssueKey(issueKey) + .setNew(false); + } + + public FieldDiffs toFieldDiffs() { + FieldDiffs diffs = FieldDiffs.parse(changeData); + diffs.setUserLogin(userLogin); + diffs.setCreatedAt(createdAt); + diffs.setUpdatedAt(updatedAt); + return diffs; + } } diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueStorage.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueStorage.java index 1b2af6e383e..e38c4198eec 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueStorage.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueStorage.java @@ -27,6 +27,7 @@ import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; import org.sonar.core.issue.DefaultIssue; import org.sonar.core.issue.DefaultIssueComment; +import org.sonar.core.issue.FieldDiffs; import org.sonar.core.persistence.MyBatis; import java.util.Arrays; @@ -82,12 +83,13 @@ public abstract class IssueStorage { for (IssueComment comment : issue.comments()) { DefaultIssueComment c = (DefaultIssueComment) comment; if (c.isNew()) { - IssueChangeDto changeDto = ChangeDtoConverter.commentToDto(c); + IssueChangeDto changeDto = IssueChangeDto.of(c); mapper.insert(changeDto); } } - if (issue.diffs() != null) { - IssueChangeDto changeDto = ChangeDtoConverter.changeToDto(issue.key(), issue.diffs()); + FieldDiffs diffs = issue.diffs(); + if (diffs != null) { + IssueChangeDto changeDto = IssueChangeDto.of(issue.key(), diffs); mapper.insert(changeDto); } } diff --git a/sonar-core/src/test/java/org/sonar/core/issue/db/ChangeDtoConverterTest.java b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueChangeDtoTest.java similarity index 87% rename from sonar-core/src/test/java/org/sonar/core/issue/db/ChangeDtoConverterTest.java rename to sonar-core/src/test/java/org/sonar/core/issue/db/IssueChangeDtoTest.java index 8b0e67347d9..c146bf22195 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/db/ChangeDtoConverterTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueChangeDtoTest.java @@ -25,12 +25,12 @@ import org.sonar.core.issue.FieldDiffs; import static org.fest.assertions.Assertions.assertThat; -public class ChangeDtoConverterTest { +public class IssueChangeDtoTest { @Test - public void testToChangeDtos() throws Exception { + public void create_from_comment() throws Exception { DefaultIssueComment comment = DefaultIssueComment.create("ABCDE", "emmerik", "the comment"); - IssueChangeDto dto = ChangeDtoConverter.commentToDto(comment); + IssueChangeDto dto = IssueChangeDto.of(comment); assertThat(dto.getChangeData()).isEqualTo("the comment"); assertThat(dto.getChangeType()).isEqualTo("comment"); @@ -41,12 +41,12 @@ public class ChangeDtoConverterTest { } @Test - public void testToDiffsDtos() throws Exception { + public void create_from_diff() throws Exception { FieldDiffs diffs = new FieldDiffs(); diffs.setDiff("severity", "INFO", "BLOCKER"); diffs.setUserLogin("emmerik"); - IssueChangeDto dto = ChangeDtoConverter.changeToDto("ABCDE", diffs); + IssueChangeDto dto = IssueChangeDto.of("ABCDE", diffs); assertThat(dto.getChangeData()).isEqualTo("severity=INFO|BLOCKER"); assertThat(dto.getChangeType()).isEqualTo("diff"); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java index 4a74dedf0fb..158cba4c329 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java @@ -131,30 +131,37 @@ public class IssueQuery { return assignees; } + @CheckForNull public Boolean assigned() { return assigned; } + @CheckForNull public Boolean planned() { return planned; } + @CheckForNull public Boolean resolved() { return resolved; } + @CheckForNull public Date createdAfter() { return createdAfter; } + @CheckForNull public Date createdBefore() { return createdBefore; } + @CheckForNull public Sort sort() { return sort; } + @CheckForNull public Boolean asc() { return asc; } @@ -260,8 +267,8 @@ public class IssueQuery { * If true, it will return all issues assigned to someone * If false, it will return all issues not assigned to someone */ - public Builder assigned(Boolean assigned) { - this.assigned = assigned; + public Builder assigned(@Nullable Boolean b) { + this.assigned = b; return this; } @@ -278,18 +285,18 @@ public class IssueQuery { * If true, it will return all resolved issues * If false, it will return all none resolved issues */ - public Builder resolved(Boolean resolved) { + public Builder resolved(@Nullable Boolean resolved) { this.resolved = resolved; return this; } - public Builder createdAfter(Date createdAfter) { - this.createdAfter = createdAfter; + public Builder createdAfter(@Nullable Date d) { + this.createdAfter = d; return this; } - public Builder createdBefore(Date createdBefore) { - this.createdBefore = createdBefore; + public Builder createdBefore(@Nullable Date d) { + this.createdBefore = d; return this; } @@ -298,7 +305,7 @@ public class IssueQuery { return this; } - public Builder asc(Boolean asc) { + public Builder asc(@Nullable Boolean asc) { this.asc = asc; return this; } diff --git a/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java b/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java index ad68816b633..5095ce59d37 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java @@ -28,7 +28,6 @@ import org.sonar.core.issue.DefaultIssue; import org.sonar.core.issue.DefaultIssueComment; import org.sonar.core.issue.IssueChangeContext; import org.sonar.core.issue.IssueUpdater; -import org.sonar.core.issue.db.ChangeDtoConverter; import org.sonar.core.issue.db.IssueChangeDao; import org.sonar.core.issue.db.IssueChangeDto; import org.sonar.core.issue.db.IssueStorage; @@ -96,7 +95,7 @@ public class IssueCommentService implements ServerComponent { // check authorization finder.findByKey(comment.issueKey(), UserRole.USER); - IssueChangeDto dto = ChangeDtoConverter.commentToDto(comment); + IssueChangeDto dto = IssueChangeDto.of(comment); dto.setUpdatedAt(new Date()); dto.setChangeData(text); changeDao.update(dto); -- 2.39.5