From 1f41283751271d5fc4eeac4149830800437431dd Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 3 Jun 2013 00:46:58 +0200 Subject: [PATCH] Fix some quality flaws --- .../issue/notification/NewIssuesEmailTemplate.java | 10 ++++++---- .../issue/notification/NewIssuesEmailTemplateTest.java | 10 ++++++++++ .../java/org/sonar/core/issue/ActionPlanStats.java | 3 ++- .../java/org/sonar/core/issue/DefaultIssueBuilder.java | 4 ++-- .../org/sonar/server/issue/IssueChangeService.java | 7 ++++++- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplate.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplate.java index b3e883b273e..c07e6adaa29 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplate.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplate.java @@ -65,10 +65,12 @@ public class NewIssuesEmailTemplate extends EmailTemplate { private void appendFooter(StringBuilder sb, Notification notification) { String projectKey = notification.getFieldValue("projectKey"); String dateString = notification.getFieldValue("projectDate"); - Date date = DateUtils.parseDateTime(dateString); - String url = String.format("%s/issues/search?componentRoots=%s&createdAfter=%s", - settings.getServerBaseURL(), URLEncoder.encode(projectKey), DateUtils.formatDate(date)); - sb.append("\n").append("See it in SonarQube: ").append(url).append("\n"); + if (projectKey != null && dateString != null) { + Date date = DateUtils.parseDateTime(dateString); + String url = String.format("%s/issues/search?componentRoots=%s&createdAfter=%s", + settings.getServerBaseURL(), URLEncoder.encode(projectKey), DateUtils.formatDate(date)); + sb.append("\n").append("See it in SonarQube: ").append(url).append("\n"); + } } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplateTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplateTest.java index 238b6810901..f3f8b1cf42c 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplateTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplateTest.java @@ -75,4 +75,14 @@ public class NewIssuesEmailTemplateTest { "\n" + "See it in SonarQube: http://nemo.sonarsource.org/issues/search?componentRoots=org.apache%3Astruts&createdAfter=2010-05-18\n"); } + + @Test + public void shouldNotAddFooterIfMissingProperties() { + Notification notification = new Notification("new-issues") + .setFieldValue("count", "32") + .setFieldValue("projectName", "Struts"); + + EmailMessage message = template.format(notification); + assertThat(message.getMessage()).doesNotContain("See it"); + } } diff --git a/sonar-core/src/main/java/org/sonar/core/issue/ActionPlanStats.java b/sonar-core/src/main/java/org/sonar/core/issue/ActionPlanStats.java index 371125642c5..30f7c66c072 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/ActionPlanStats.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/ActionPlanStats.java @@ -71,6 +71,7 @@ public class ActionPlanStats extends DefaultActionPlan { } public boolean overDue(){ - return isOpen() && deadLine() != null && new Date().after(deadLine()); + Date deadline = deadLine(); + return isOpen() && deadline != null && new Date().after(deadline); } } diff --git a/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueBuilder.java b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueBuilder.java index be3bb84b722..a8a8c303c2f 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueBuilder.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueBuilder.java @@ -71,7 +71,7 @@ public class DefaultIssueBuilder implements Issuable.IssueBuilder { } @Override - public Issuable.IssueBuilder severity(String severity) { + public Issuable.IssueBuilder severity(@Nullable String severity) { this.severity = severity; return this; } @@ -89,7 +89,7 @@ public class DefaultIssueBuilder implements Issuable.IssueBuilder { } @Override - public Issuable.IssueBuilder attribute(String key, String value) { + public Issuable.IssueBuilder attribute(String key, @Nullable String value) { if (attributes == null) { attributes = Maps.newLinkedHashMap(); } diff --git a/sonar-server/src/main/java/org/sonar/server/issue/IssueChangeService.java b/sonar-server/src/main/java/org/sonar/server/issue/IssueChangeService.java index 450f35c44d0..3d73cbdea8c 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/IssueChangeService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/IssueChangeService.java @@ -125,8 +125,13 @@ public class IssueChangeService implements ServerComponent { } } + // TODO remove this duplication from IssueService public IssueQueryResult loadIssue(String issueKey) { IssueQuery query = IssueQuery.builder().issueKeys(Arrays.asList(issueKey)).requiredRole(UserRole.USER).build(); - return finder.find(query); + IssueQueryResult result = finder.find(query); + if (result.issues().size()!=1) { + throw new IllegalStateException("Issue not found: " + issueKey); + } + return result; } } -- 2.39.5