From 421ca0d61503b7286175866608ec43a60acc7fc3 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 4 Dec 2014 09:08:46 +0100 Subject: [PATCH] Fix some quality flaws --- .../plugins/core/issue/IssueTracking.java | 18 ++++++++++-------- .../core/issue/IssueTrackingDecorator.java | 3 +++ .../core/issue/IssueTrackingDecoratorTest.java | 7 ++++--- .../org/sonar/batch/scan/LastLineHashes.java | 2 +- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTracking.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTracking.java index b470b424f84..884e298bd68 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTracking.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTracking.java @@ -30,6 +30,7 @@ import org.sonar.api.BatchExtension; import org.sonar.api.issue.internal.DefaultIssue; import org.sonar.api.rule.RuleKey; import org.sonar.core.issue.db.IssueDto; +import org.sonar.plugins.core.issue.tracking.FileHashes; import org.sonar.plugins.core.issue.tracking.IssueTrackingBlocksRecognizer; import org.sonar.plugins.core.issue.tracking.RollingFileHashes; @@ -63,14 +64,15 @@ public class IssueTracking implements BatchExtension { return; } for (DefaultIssue issue : issues) { - if (issue.line() != null) { - issue.setChecksum(sourceHashHolder.getHashedSource().getHash(issue.line())); + Integer line = issue.line(); + if (line != null) { + issue.setChecksum(sourceHashHolder.getHashedSource().getHash(line)); } } } @VisibleForTesting - void mapIssues(Collection newIssues, @Nullable Collection lastIssues, SourceHashHolder sourceHashHolder, IssueTrackingResult result) { + void mapIssues(Collection newIssues, @Nullable Collection lastIssues, @Nullable SourceHashHolder sourceHashHolder, IssueTrackingResult result) { boolean hasLastScan = false; if (lastIssues != null) { @@ -81,7 +83,7 @@ public class IssueTracking implements BatchExtension { // If each new issue matches an old one we can stop the matching mechanism if (result.matched().size() != newIssues.size()) { if (sourceHashHolder != null && sourceHashHolder.getHashedReference() != null && hasLastScan) { - mapNewissues(sourceHashHolder, newIssues, result); + mapNewissues(sourceHashHolder.getHashedReference(), sourceHashHolder.getHashedSource(), newIssues, result); } mapIssuesOnSameRule(newIssues, result); } @@ -108,12 +110,12 @@ public class IssueTracking implements BatchExtension { } } - private void mapNewissues(SourceHashHolder sourceHashHolder, Collection newIssues, IssueTrackingResult result) { + private void mapNewissues(FileHashes hashedReference, FileHashes hashedSource, Collection newIssues, IssueTrackingResult result) { - IssueTrackingBlocksRecognizer rec = new IssueTrackingBlocksRecognizer(sourceHashHolder.getHashedReference(), sourceHashHolder.getHashedSource()); + IssueTrackingBlocksRecognizer rec = new IssueTrackingBlocksRecognizer(hashedReference, hashedSource); - RollingFileHashes a = RollingFileHashes.create(sourceHashHolder.getHashedReference(), 5); - RollingFileHashes b = RollingFileHashes.create(sourceHashHolder.getHashedSource(), 5); + RollingFileHashes a = RollingFileHashes.create(hashedReference, 5); + RollingFileHashes b = RollingFileHashes.create(hashedSource, 5); Multimap newIssuesByLines = newIssuesByLines(newIssues, rec, result); Multimap lastIssuesByLines = lastIssuesByLines(result.unmatched(), rec); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingDecorator.java index 7c39348de1f..a465c1b860c 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingDecorator.java @@ -129,6 +129,9 @@ public class IssueTrackingDecorator implements Decorator { if (ResourceUtils.isFile(resource)) { File sonarFile = (File) resource; InputFile file = inputPathCache.getFile(project.getEffectiveKey(), sonarFile.getPath()); + if (file == null) { + throw new IllegalStateException("Resource " + resource + " was not found in InputPath cache"); + } sourceHashHolder = new SourceHashHolder((DefaultInputFile) file, lastLineHashes); } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/IssueTrackingDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/IssueTrackingDecoratorTest.java index 844b2a8dfd8..99d3cdcdd63 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/IssueTrackingDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/IssueTrackingDecoratorTest.java @@ -116,14 +116,14 @@ public class IssueTrackingDecoratorTest extends AbstractDaoTestCase { @Test public void should_process_open_issues() throws Exception { - Resource file = new File("Action.java").setEffectiveKey("struts:Action.java").setId(123); + Resource file = File.create("Action.java").setEffectiveKey("struts:Action.java").setId(123); final DefaultIssue issue = new DefaultIssue(); // INPUT : one issue, no open issues during previous scan, no filtering when(issueCache.byComponent("struts:Action.java")).thenReturn(Arrays.asList(issue)); List dbIssues = Collections.emptyList(); when(initialOpenIssues.selectAndRemoveIssues("struts:Action.java")).thenReturn(dbIssues); - + when(inputPathCache.getFile("foo", "Action.java")).thenReturn(mock(DefaultInputFile.class)); decorator.doDecorate(file); // Apply filters, track, apply transitions, notify extensions then update cache @@ -142,7 +142,7 @@ public class IssueTrackingDecoratorTest extends AbstractDaoTestCase { @Test public void should_register_unmatched_issues_as_end_of_life() throws Exception { // "Unmatched" issues existed in previous scan but not in current one -> they have to be closed - Resource file = new File("Action.java").setEffectiveKey("struts:Action.java").setId(123); + Resource file = File.create("Action.java").setEffectiveKey("struts:Action.java").setId(123); // INPUT : one issue existing during previous scan IssueDto unmatchedIssue = new IssueDto().setKee("ABCDE").setResolution(null).setStatus("OPEN").setRuleKey("squid", "AvoidCycle"); @@ -151,6 +151,7 @@ public class IssueTrackingDecoratorTest extends AbstractDaoTestCase { trackingResult.addUnmatched(unmatchedIssue); when(tracking.track(isA(SourceHashHolder.class), anyCollection(), anyCollection())).thenReturn(trackingResult); + when(inputPathCache.getFile("foo", "Action.java")).thenReturn(mock(DefaultInputFile.class)); decorator.doDecorate(file); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/LastLineHashes.java b/sonar-batch/src/main/java/org/sonar/batch/scan/LastLineHashes.java index ed0e3adce3b..7fde8b45295 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/LastLineHashes.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/LastLineHashes.java @@ -39,7 +39,7 @@ public class LastLineHashes implements BatchComponent { public String[] getLineHashes(String fileKey) { String hashesFromWs = loadHashesFromWs(fileKey); - return hashesFromWs != null ? Iterators.toArray(Splitter.on('\n').split(hashesFromWs).iterator(), String.class) : null; + return Iterators.toArray(Splitter.on('\n').split(hashesFromWs).iterator(), String.class); } private String loadHashesFromWs(String fileKey) { -- 2.39.5