From 3b8306b997e65b995e9974824cd952d746270433 Mon Sep 17 00:00:00 2001 From: Willem Cheizoo Date: Tue, 13 Dec 2016 09:13:08 +0100 Subject: [PATCH] Fix NullPointerException when tslint json is empty. Improve current Class to Java 8. --- pom.xml | 26 ++++---- .../pablissimo/sonar/TsLintParserImpl.java | 61 ++++++++----------- .../pablissimo/sonar/TsLintParserTest.java | 46 ++++++++------ 3 files changed, 63 insertions(+), 70 deletions(-) diff --git a/pom.xml b/pom.xml index 3b53d51..7b7d22a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,19 +5,19 @@ com.pablissimo.sonar sonar-typescript-plugin sonar-plugin - 0.96-SNAPSHOT + 0.96.1-SNAPSHOT TypeScript Analyse TypeScript projects 2013 https://github.com/pablissimo/SonarTsPlugin - - + + Paul O'Neill (pablissimo) and contributors https://github.com/pablissimo/SonarTsPlugin - - + + pablissimo Paul O'Neill @@ -39,11 +39,11 @@ Travis https://travis-ci.org/pablissimo/SonarTsPlugin - - + + UTF-8 - - 5.6 + + 5.6 1.8 1.21 @@ -61,8 +61,8 @@ gson 2.3 - - + + commons-io commons-io 2.4 @@ -135,8 +135,8 @@ ${jdk.min.version} ${jdk.min.version} - - true + + true -Xlint:all -Werror diff --git a/src/main/java/com/pablissimo/sonar/TsLintParserImpl.java b/src/main/java/com/pablissimo/sonar/TsLintParserImpl.java index 864d99e..5e6b7ef 100644 --- a/src/main/java/com/pablissimo/sonar/TsLintParserImpl.java +++ b/src/main/java/com/pablissimo/sonar/TsLintParserImpl.java @@ -1,56 +1,43 @@ package com.pablissimo.sonar; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.google.gson.Gson; import com.google.gson.GsonBuilder; + import com.pablissimo.sonar.model.TsLintIssue; +import org.apache.commons.lang.StringUtils; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + public class TsLintParserImpl implements TsLintParser { - private static final Logger LOG = LoggerFactory.getLogger(TsLintParserImpl.class); - + public Map> parse(List toParse) { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); - - List allIssues = new ArrayList(); - - for (String batch : toParse) { - TsLintIssue[] batchIssues = gson.fromJson(getFixedUpOutput(batch), TsLintIssue[].class); - for (TsLintIssue batchIssue : batchIssues) { - allIssues.add(batchIssue); - } - } - // Remap by filename - Map> toReturn = new HashMap>(); - for (TsLintIssue issue : allIssues) { - List issuesByFile = toReturn.get(issue.getName()); - if (issuesByFile == null) { - issuesByFile = new ArrayList(); - toReturn.put(issue.getName(), issuesByFile); - } - - issuesByFile.add(issue); - } + List allIssues = toParse.stream() + .map(batch -> gson.fromJson(getFixedUpOutput(batch), TsLintIssue[].class)) + .filter(Objects::nonNull) + .flatMap(Arrays::stream) + .collect(Collectors.toList()); - return toReturn; + // Remap by filename + return allIssues.stream() + .collect(Collectors.groupingBy(TsLintIssue::getName)); } - + private String getFixedUpOutput(String toParse) { - if (toParse.contains("][")) { - // Pre 4.0.0-versions of TsLint return nonsense for its JSON output - // when faced with multiple files so we need to fix it up before we + if (StringUtils.contains(toParse, ("]["))) { + // Pre 4.0.0-versions of TsLint return nonsense for its JSON output + // when faced with multiple files so we need to fix it up before we // do anything else return toParse.replaceAll("\\]\\[", ","); } - + return toParse; } -} \ No newline at end of file +} diff --git a/src/test/java/com/pablissimo/sonar/TsLintParserTest.java b/src/test/java/com/pablissimo/sonar/TsLintParserTest.java index ae1ca53..9b6ecfa 100644 --- a/src/test/java/com/pablissimo/sonar/TsLintParserTest.java +++ b/src/test/java/com/pablissimo/sonar/TsLintParserTest.java @@ -1,36 +1,33 @@ package com.pablissimo.sonar; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import com.pablissimo.sonar.model.TsLintIssue; + +import org.junit.Test; -import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.List; import java.util.Map; -import org.junit.Test; -import org.sonar.api.Properties; -import org.sonar.api.Property; - -import com.pablissimo.sonar.model.TsLintIssue; +import static org.junit.Assert.assertEquals; public class TsLintParserTest { - @Test public void parsesValidTsLintRecordIntoObject() { - String parseRow1 = "[{\"endPosition\":{\"character\":44,\"line\":23,\"position\":658},\"failure\":\"for statements must be braced\",\"name\":\"Tools.ts\",\"ruleName\":\"curly\",\"startPosition\":{\"character\":6,\"line\":22,\"position\":587}}]"; + String + parseRow1 = + "[{\"endPosition\":{\"character\":44,\"line\":23,\"position\":658},\"failure\":\"for statements must be braced\",\"name\":\"Tools.ts\",\"ruleName\":\"curly\",\"startPosition\":{\"character\":6,\"line\":22,\"position\":587}}]"; List toParse = new ArrayList(); toParse.add(parseRow1); - + Map> issues = new TsLintParserImpl().parse(toParse); assertEquals(1, issues.size()); List fileIssues = issues.get("Tools.ts"); - + assertEquals(1, fileIssues.size()); - + TsLintIssue issue = fileIssues.get(0); assertEquals(44, issue.getEndPosition().getCharacter()); assertEquals(23, issue.getEndPosition().getLine()); @@ -44,26 +41,35 @@ public void parsesValidTsLintRecordIntoObject() { assertEquals(22, issue.getStartPosition().getLine()); assertEquals(587, issue.getStartPosition().getPosition()); } - + + @Test + public void parsesEmptyJson() { + List toParse = new ArrayList<>(); + toParse.add(""); + + new TsLintParserImpl().parse(toParse); + } + @Test public void parsesIssuesWithSameNameIntoSameBucket() { List toParse = new ArrayList(); toParse.add("[{\"name\":\"Tools.ts\",\"ruleName\":\"tools1\"}]"); toParse.add("[{\"name\":\"Tools.ts\",\"ruleName\":\"tools2\"}]"); - + Map> issues = new TsLintParserImpl().parse(toParse); - + assertEquals(1, issues.size()); assertEquals(2, issues.get("Tools.ts").size()); } - + @Test public void fixesUpBrokenBatchedOutputFromTsLintPriorTo_4_0_0() { List toParse = new ArrayList(); - toParse.add("[{\"name\":\"Tools.ts\",\"ruleName\":\"tools1\"}][{\"name\":\"Tools.ts\",\"ruleName\":\"tools2\"}]"); - + toParse + .add("[{\"name\":\"Tools.ts\",\"ruleName\":\"tools1\"}][{\"name\":\"Tools.ts\",\"ruleName\":\"tools2\"}]"); + Map> issues = new TsLintParserImpl().parse(toParse); - + assertEquals(1, issues.size()); assertEquals(2, issues.get("Tools.ts").size()); }