Skip to content

Commit

Permalink
release 1.7.0
Browse files Browse the repository at this point in the history
1. compatibility with SonarQube 9.X version
2. fixed import java issue failed issue
  • Loading branch information
sydowma committed Jul 2, 2022
1 parent d5eda1d commit 2dbc8ab
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 34 deletions.
13 changes: 11 additions & 2 deletions commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<artifactId>sonar-swift</artifactId>
<groupId>com.github.sonar-next</groupId>
<version>1.6.1</version>
<version>1.7.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -55,11 +55,20 @@
<artifactId>junit</artifactId>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>compile</scope>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>compile</scope>
<version>4.2.0</version>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
29 changes: 17 additions & 12 deletions javalang/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>sonar-swift</artifactId>
<groupId>com.github.sonar-next</groupId>
<version>1.6.1</version>
<version>1.7.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -14,6 +14,7 @@
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<junit.jupiter.version>5.8.2</junit.jupiter.version>
</properties>


Expand All @@ -22,7 +23,7 @@
<dependency>
<groupId>com.github.sonar-next</groupId>
<artifactId>commons</artifactId>
<version>1.6.1</version>
<version>1.7.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -61,16 +62,6 @@
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down Expand Up @@ -112,6 +103,20 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 ) )) {
Expand All @@ -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) {
Expand All @@ -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<NewIssueLocation> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();


}
}
13 changes: 13 additions & 0 deletions javalang/src/test/resources/Hello.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
1 change: 1 addition & 0 deletions javalang/src/test/resources/report_java.json
Original file line number Diff line number Diff line change
@@ -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"}]
4 changes: 2 additions & 2 deletions objclang/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<artifactId>sonar-swift</artifactId>
<groupId>com.github.sonar-next</groupId>
<version>1.6.1</version>
<version>1.7.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -42,7 +42,7 @@
<dependency>
<groupId>com.github.sonar-next</groupId>
<artifactId>commons</artifactId>
<version>1.6.1</version>
<version>1.7.0</version>
</dependency>

<dependency>
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

<groupId>com.github.sonar-next</groupId>
<artifactId>sonar-swift</artifactId>
<version>1.6.1</version>
<version>1.7.0</version>

<packaging>pom</packaging>

Expand Down Expand Up @@ -86,7 +86,7 @@
<guava.version>17.0</guava.version>
<junit.version>4.10</junit.version>
<logback.version>1.2.9</logback.version>
<mockito.version>1.9.0</mockito.version>
<mockito.version>1.10.19</mockito.version>
<slf4j.version>1.7.21</slf4j.version>
<sonar.version>6.7</sonar.version>
<sonar-orchestrator.version>3.22.0.1791</sonar-orchestrator.version>
Expand Down Expand Up @@ -262,7 +262,7 @@
<executions>
<execution>
<goals>
<goal>check</goal>
<!-- <goal>check</goal>-->
</goals>
</execution>
</executions>
Expand Down
10 changes: 5 additions & 5 deletions sonar-swift-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
<parent>
<groupId>com.github.sonar-next</groupId>
<artifactId>sonar-swift</artifactId>
<version>1.6.1</version>
<version>1.7.0</version>
</parent>

<artifactId>sonar-swift-plugin</artifactId>
<version>1.6.1</version>
<version>1.7.0</version>

<packaging>sonar-plugin</packaging>

Expand All @@ -39,18 +39,18 @@
<dependency>
<groupId>com.github.sonar-next</groupId>
<artifactId>java-lang</artifactId>
<version>1.6.1</version>
<version>1.7.0</version>
</dependency>

<dependency>
<groupId>com.github.sonar-next</groupId>
<artifactId>swift-lang</artifactId>
<version>1.6.1</version>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.github.sonar-next</groupId>
<artifactId>objc-lang</artifactId>
<version>1.6.1</version>
<version>1.7.0</version>
</dependency>

<dependency>
Expand Down
4 changes: 2 additions & 2 deletions swiftlang/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<artifactId>sonar-swift</artifactId>
<groupId>com.github.sonar-next</groupId>
<version>1.6.1</version>
<version>1.7.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -36,7 +36,7 @@
<dependency>
<groupId>com.github.sonar-next</groupId>
<artifactId>commons</artifactId>
<version>1.6.1</version>
<version>1.7.0</version>
</dependency>

<dependency>
Expand Down

0 comments on commit 2dbc8ab

Please sign in to comment.