Skip to content

Commit

Permalink
Fix missing coverage before bug fix release 6.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
alban-auzeill authored and Wohops committed Jul 16, 2020
1 parent d1b9121 commit 5bbf311
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
40 changes: 31 additions & 9 deletions java-frontend/src/test/java/org/sonar/java/JavaSquidTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.rule.CheckFactory;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
Expand All @@ -35,6 +38,7 @@
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.utils.Version;
import org.sonar.java.filters.SonarJavaIssueFilter;
import org.sonar.java.model.JavaVersionImpl;
import org.sonar.plugins.java.api.JavaResourceLocator;

Expand Down Expand Up @@ -63,7 +67,7 @@ public void number_of_visitors_in_sonarLint_context_LTS() throws Exception {

String code = "/***/\nclass A {\n String foo() {\n return foo();\n }\n}";

InputFile defaultFile = scanForErrors(code);
InputFile defaultFile = scanForErrorsInSonarLint(code);

// No symbol table : check reference to foo is empty.
assertThat(context.referencesForSymbolAt(defaultFile.key(), 3, 8)).isNull();
Expand All @@ -78,9 +82,25 @@ public void number_of_visitors_in_sonarLint_context_LTS() throws Exception {
verify(javaTestClasspath, times(1)).getElements();
}

@Test
public void metrics_and_highlighting_in_sonarqube() throws Exception {

String code = "/***/\nclass A {\n String foo() {\n return foo();\n }\n}";

SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.create(7, 9), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
InputFile defaultFile = scanForErrors(code, runtime);

// Metrics on lines
verify(fileLinesContext, times(1)).save();
// Highlighting
assertThat(context.highlightingTypeAt(defaultFile.key(), 1, 0)).isNotEmpty();
// Measures
assertThat(context.measures(defaultFile.key())).isNotEmpty();
}

@Test
public void parsing_errors_should_be_reported_to_sonarlint() throws Exception {
scanForErrors("class A {");
scanForErrorsInSonarLint("class A {");

assertThat(context.allAnalysisErrors()).hasSize(1);
assertThat(context.allAnalysisErrors().iterator().next().message()).startsWith("Parse error at line 1 column 8");
Expand All @@ -89,19 +109,21 @@ public void parsing_errors_should_be_reported_to_sonarlint() throws Exception {
@org.junit.Ignore("new semantic analysis does not throw exception in this case")
@Test
public void semantic_errors_should_be_reported_to_sonarlint() throws Exception {
scanForErrors("class A {} class A {}");
scanForErrorsInSonarLint("class A {} class A {}");

assertThat(context.allAnalysisErrors()).hasSize(1);
assertThat(context.allAnalysisErrors().iterator().next().message()).isEqualTo("Registering class 2 times : A");
}


private InputFile scanForErrors(String code) throws IOException {
private InputFile scanForErrorsInSonarLint(String code) throws IOException {
return scanForErrors(code, SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
}

private InputFile scanForErrors(String code, SonarRuntime runtime) throws IOException {
File baseDir = temp.getRoot().getAbsoluteFile();
context = SensorContextTester.create(baseDir);

// Set sonarLint runtime
context.setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
context.setRuntime(runtime);

InputFile inputFile = addFile(code, context);

Expand All @@ -114,9 +136,9 @@ private InputFile scanForErrors(String code) throws IOException {
javaTestClasspath = mock(JavaTestClasspath.class);
sonarComponents = new SonarComponents(fileLinesContextFactory, context.fileSystem(), javaClasspath, javaTestClasspath, mock(CheckFactory.class));
sonarComponents.setSensorContext(context);
JavaSquid javaSquid = new JavaSquid(new JavaVersionImpl(), sonarComponents, new Measurer(context, mock(NoSonarFilter.class)), mock(JavaResourceLocator.class), null);
SonarJavaIssueFilter passThroughFilter = (issue, chain) -> chain.accept(issue);
JavaSquid javaSquid = new JavaSquid(new JavaVersionImpl(), sonarComponents, new Measurer(context, mock(NoSonarFilter.class)), mock(JavaResourceLocator.class), passThroughFilter);
javaSquid.scan(Collections.singletonList(inputFile), Collections.emptyList(), Collections.emptyList());

return inputFile;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.eclipse.core.internal.resources.ProjectDescription;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
Expand All @@ -49,6 +53,8 @@
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.issue.Issue;
import org.sonar.api.batch.sensor.symbol.NewSymbolTable;
import org.sonar.api.ce.posttask.Project;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.internal.SonarRuntimeImpl;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
Expand Down Expand Up @@ -197,6 +203,40 @@ public void creation_of_custom_test_checks() {
postTestExecutionChecks();
}

@Test
public void get_work_directory_from_parent() {
ProjectDefinition grandParentProjectDefinition = ProjectDefinition.create();
ProjectDefinition parentProjectDefinition = ProjectDefinition.create();
ProjectDefinition childProjectDefinition = ProjectDefinition.create();

grandParentProjectDefinition.setWorkDir(new File("grandParentWorkDir"));

grandParentProjectDefinition.addSubProject(parentProjectDefinition);
parentProjectDefinition.addSubProject(childProjectDefinition);

SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory,
null, null, null, checkFactory, childProjectDefinition);
sonarComponents.setSensorContext(context);

assertThat(sonarComponents.workDir()).isEqualTo(new File("grandParentWorkDir"));
assertThat(sonarComponents.project()).isNull();
}

@Test
public void should_fail_analysis() {
SonarComponents sonarComponents = new SonarComponents(null, null, null, null, null);
SensorContextTester context = SensorContextTester.create(new File(""));
MapSettings mapSettings = new MapSettings();
mapSettings.setProperty("sonar.internal.analysis.failFast", "false");
context.setSettings(mapSettings);
sonarComponents.setSensorContext(context);

assertThat(sonarComponents.shouldFailAnalysisOnException()).isFalse();

mapSettings.setProperty("sonar.internal.analysis.failFast", "true");
assertThat(sonarComponents.shouldFailAnalysisOnException()).isTrue();
}

@Test
public void creation_of_both_types_test_checks() {
JavaCheck expectedCheck = new CustomCheck();
Expand Down Expand Up @@ -396,7 +436,6 @@ public void jsp_classpath_should_include_plugin() throws Exception {
assertThat(jspClassPath).containsExactly(plugin.getAbsolutePath(), someJar.getAbsolutePath());
}


private static CheckRegistrar getRegistrar(final JavaCheck expectedCheck) {
return registrarContext -> registrarContext.registerClassesForRepository(REPOSITORY_NAME,
Lists.<Class<? extends JavaCheck>>newArrayList(expectedCheck.getClass()), null);
Expand Down

0 comments on commit 5bbf311

Please sign in to comment.