From 2dbc8ab691675d604db36d455d2c832386c6d2ea Mon Sep 17 00:00:00 2001 From: magaofei Date: Sat, 2 Jul 2022 21:08:53 +0800 Subject: [PATCH] release 1.7.0 1. compatibility with SonarQube 9.X version 2. fixed import java issue failed issue --- commons/pom.xml | 13 ++- javalang/pom.xml | 29 +++--- .../java/issues/infer/InferReportParser.java | 18 ++-- .../issues/infer/InferReportParserTest.java | 99 +++++++++++++++++++ javalang/src/test/resources/Hello.java | 13 +++ javalang/src/test/resources/report_java.json | 1 + objclang/pom.xml | 4 +- pom.xml | 6 +- sonar-swift-plugin/pom.xml | 10 +- swiftlang/pom.xml | 4 +- 10 files changed, 163 insertions(+), 34 deletions(-) create mode 100644 javalang/src/test/java/com/github/sonar/next/sonarqube/java/issues/infer/InferReportParserTest.java create mode 100644 javalang/src/test/resources/Hello.java create mode 100644 javalang/src/test/resources/report_java.json diff --git a/commons/pom.xml b/commons/pom.xml index a314469..3a0adae 100644 --- a/commons/pom.xml +++ b/commons/pom.xml @@ -24,7 +24,7 @@ sonar-swift com.github.sonar-next - 1.6.1 + 1.7.0 4.0.0 @@ -55,11 +55,20 @@ junit compile + + + org.mockito + mockito-core + compile + 4.2.0 + org.mockito - mockito-all + mockito-inline compile + 4.2.0 + org.assertj assertj-core diff --git a/javalang/pom.xml b/javalang/pom.xml index d8242be..106a34b 100644 --- a/javalang/pom.xml +++ b/javalang/pom.xml @@ -5,7 +5,7 @@ sonar-swift com.github.sonar-next - 1.6.1 + 1.7.0 4.0.0 @@ -14,6 +14,7 @@ 8 8 + 5.8.2 @@ -22,7 +23,7 @@ com.github.sonar-next commons - 1.6.1 + 1.7.0 @@ -61,16 +62,6 @@ com.googlecode.json-simple json-simple - - junit - junit - compile - - - org.mockito - mockito-all - compile - org.assertj assertj-core @@ -112,6 +103,20 @@ + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + + org.junit.jupiter + junit-jupiter-api + ${junit.jupiter.version} + test + + \ No newline at end of file diff --git a/javalang/src/main/java/com/github/sonar/next/sonarqube/java/issues/infer/InferReportParser.java b/javalang/src/main/java/com/github/sonar/next/sonarqube/java/issues/infer/InferReportParser.java index cb53245..a3508d9 100644 --- a/javalang/src/main/java/com/github/sonar/next/sonarqube/java/issues/infer/InferReportParser.java +++ b/javalang/src/main/java/com/github/sonar/next/sonarqube/java/issues/infer/InferReportParser.java @@ -56,7 +56,7 @@ public void parseReport(File jsonFile) { } } - private void recordIssue(final JSONObject jsonObject) { + protected void recordIssue(final JSONObject jsonObject) { String filePath = (String) jsonObject.get("file"); logger.debug("record issue for java, path = {}", filePath); @@ -81,12 +81,13 @@ private void recordIssue(final JSONObject jsonObject) { } - File file = new File( filePath ); FilePredicates predicates = context.fileSystem().predicates(); - FilePredicate fp = predicates.or( predicates.hasAbsolutePath( filePath ), predicates.hasRelativePath( filePath ) ); + FilePredicate fp = predicates.or( predicates.hasAbsolutePath( filePath ), + predicates.hasRelativePath( filePath ) ); InputFile inputFile = null; if (!context.fileSystem().hasFiles( fp )) { + logger.debug("fileSystem hasFiles filePath:{}", filePath); FileSystem fs = context.fileSystem(); //Search for path _ending_ with the filename for (InputFile f : fs.inputFiles( fs.predicates().hasType( InputFile.Type.MAIN ) )) { @@ -96,6 +97,7 @@ private void recordIssue(final JSONObject jsonObject) { } } } else { + logger.debug("fileSystem inputFile filePath:{}", filePath); inputFile = context.fileSystem().inputFile( fp ); } if (inputFile == null) { @@ -104,17 +106,17 @@ private void recordIssue(final JSONObject jsonObject) { } String info = (String) jsonObject.get("qualifier"); - // 规则名为了保持一致,增加 JAVA 前缀 - String rule = "JAVA:" + jsonObject.get("bug_type"); - assert inputFile != null; + String rule = jsonObject.get("bug_type").toString(); try { NewIssueLocation dil = new DefaultIssueLocation() .on(inputFile) .at(inputFile.selectLine(lineNum)) .message(info); + RuleKey ruleKey = RuleKey.of(InferRulesDefinition.REPOSITORY_KEY, rule); + List newIssueLocations = this.composeLocationList(filePath, bugTraceJsonArray); context.newIssue() - .forRule(RuleKey.of(InferRulesDefinition.REPOSITORY_KEY, rule)) - .addFlow(this.composeLocationList(filePath, bugTraceJsonArray)) + .forRule(ruleKey) + .addFlow(newIssueLocations) .at(dil) .save(); } catch (IllegalArgumentException e) { diff --git a/javalang/src/test/java/com/github/sonar/next/sonarqube/java/issues/infer/InferReportParserTest.java b/javalang/src/test/java/com/github/sonar/next/sonarqube/java/issues/infer/InferReportParserTest.java new file mode 100644 index 0000000..266a2c8 --- /dev/null +++ b/javalang/src/test/java/com/github/sonar/next/sonarqube/java/issues/infer/InferReportParserTest.java @@ -0,0 +1,99 @@ +package com.github.sonar.next.sonarqube.java.issues.infer; + +import static org.junit.jupiter.api.Assertions.*; + +import org.json.simple.parser.ParseException; +import org.junit.Rule; +import org.junit.jupiter.api.Test; +import org.junit.rules.TemporaryFolder; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.sonar.api.batch.fs.FilePredicate; +import org.sonar.api.batch.fs.FilePredicates; +import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.DefaultFilePredicates; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; +import org.sonar.api.batch.fs.internal.TestInputFileBuilder; +import org.sonar.api.batch.sensor.SensorContext; +import org.sonar.api.batch.sensor.issue.NewIssueLocation; +import org.sonar.api.batch.sensor.issue.internal.DefaultIssue; +import org.sonar.api.rule.RuleKey; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Objects; + +import static org.mockito.Mockito.*; + +class InferReportParserTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + private DefaultFileSystem fs; + private Path moduleBasePath; + + @Mock + SensorContext sensorContext; + + @Mock + FileSystem fileSystem; + private InferReportParser self; + + + @org.junit.jupiter.api.BeforeEach + void setUp() throws IOException { + moduleBasePath = temp.newFolder().toPath(); + fs = new DefaultFileSystem(moduleBasePath); + + MockitoAnnotations.openMocks(this); + self = new InferReportParser(sensorContext); + } + + @org.junit.jupiter.api.AfterEach + void tearDown() { + } + + + @Test + void parseReport() throws IOException, ParseException { + ClassLoader classLoader = getClass().getClassLoader(); + File reportFile = new File(Objects.requireNonNull(classLoader.getResource("report_java.json")).getFile()); + File file = new File(Objects.requireNonNull(classLoader.getResource("Hello.java")).getFile()); + + FilePredicates predicates = new DefaultFilePredicates(temp.newFolder().toPath()); + InputFile javaFile = new TestInputFileBuilder("foo", "Hello.java") + .setModuleBaseDir(moduleBasePath) + .setLanguage("java") + .setContents(new String(Files.readAllBytes(file.toPath()))) + .setStatus(InputFile.Status.SAME) + .build(); + + fs.add(javaFile); + + + when(sensorContext.fileSystem()).thenReturn(fs); + when(fileSystem.predicates()).thenReturn(predicates); + + DefaultIssue defaultIssue = mock(DefaultIssue.class); + when(defaultIssue.addFlow(anyIterable())).thenReturn(defaultIssue); + when(defaultIssue.forRule(any(RuleKey.class))).thenReturn(defaultIssue); + when(defaultIssue.at(any(NewIssueLocation.class))).thenReturn(defaultIssue); + when(sensorContext.newIssue()).thenReturn(defaultIssue); + doNothing().when(defaultIssue).save(); + + self.parseReport(reportFile); + + verify(defaultIssue, times(1)).addFlow(anyIterable()); + verify(defaultIssue, times(1)).forRule(any(RuleKey.class)); + verify(defaultIssue, times(1)).at(any(NewIssueLocation.class)); + verify(defaultIssue, times(1)).save(); + + + } +} \ No newline at end of file diff --git a/javalang/src/test/resources/Hello.java b/javalang/src/test/resources/Hello.java new file mode 100644 index 0000000..3f6e577 --- /dev/null +++ b/javalang/src/test/resources/Hello.java @@ -0,0 +1,13 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +class Hello { + int test() { + String s = null; + return s.length(); + } +} diff --git a/javalang/src/test/resources/report_java.json b/javalang/src/test/resources/report_java.json new file mode 100644 index 0000000..ba8c806 --- /dev/null +++ b/javalang/src/test/resources/report_java.json @@ -0,0 +1 @@ +[{"bug_type":"NULL_DEREFERENCE","qualifier":"object `s` last assigned on line 10 could be null and is dereferenced at line 11.","severity":"ERROR","line":11,"column":-1,"procedure":"Hello.test():int","procedure_start_line":9,"file":"Hello.java","bug_trace":[{"level":0,"filename":"Hello.java","line_number":9,"column_number":-1,"description":"start of procedure test()"},{"level":0,"filename":"Hello.java","line_number":10,"column_number":-1,"description":""},{"level":0,"filename":"Hello.java","line_number":11,"column_number":-1,"description":""}],"key":"Hello.java|test|NULL_DEREFERENCE","node_key":"c8c6b1f0f8892a67c957e59c27c08c9f","hash":"f21e4baf23b546897421c6f1f10c2e78","bug_type_hum":"Null Dereference"}] diff --git a/objclang/pom.xml b/objclang/pom.xml index f656351..fd77f6a 100644 --- a/objclang/pom.xml +++ b/objclang/pom.xml @@ -24,7 +24,7 @@ sonar-swift com.github.sonar-next - 1.6.1 + 1.7.0 4.0.0 @@ -42,7 +42,7 @@ com.github.sonar-next commons - 1.6.1 + 1.7.0 diff --git a/pom.xml b/pom.xml index 6cc0f14..73bfae6 100644 --- a/pom.xml +++ b/pom.xml @@ -39,7 +39,7 @@ com.github.sonar-next sonar-swift - 1.6.1 + 1.7.0 pom @@ -86,7 +86,7 @@ 17.0 4.10 1.2.9 - 1.9.0 + 1.10.19 1.7.21 6.7 3.22.0.1791 @@ -262,7 +262,7 @@ - check + diff --git a/sonar-swift-plugin/pom.xml b/sonar-swift-plugin/pom.xml index 35b732a..912ab18 100644 --- a/sonar-swift-plugin/pom.xml +++ b/sonar-swift-plugin/pom.xml @@ -26,11 +26,11 @@ com.github.sonar-next sonar-swift - 1.6.1 + 1.7.0 sonar-swift-plugin - 1.6.1 + 1.7.0 sonar-plugin @@ -39,18 +39,18 @@ com.github.sonar-next java-lang - 1.6.1 + 1.7.0 com.github.sonar-next swift-lang - 1.6.1 + 1.7.0 com.github.sonar-next objc-lang - 1.6.1 + 1.7.0 diff --git a/swiftlang/pom.xml b/swiftlang/pom.xml index 47ee4fc..d889313 100644 --- a/swiftlang/pom.xml +++ b/swiftlang/pom.xml @@ -24,7 +24,7 @@ sonar-swift com.github.sonar-next - 1.6.1 + 1.7.0 4.0.0 @@ -36,7 +36,7 @@ com.github.sonar-next commons - 1.6.1 + 1.7.0