Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support 7.4 sonarqube (#1) #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@
</issueManagement>

<properties>
<sonar.version>5.6</sonar.version>
<sonar.version>7.4</sonar.version>
<jacoco.previous.version>0.7.4.201502262128</jacoco.previous.version>
<jacoco.version>0.7.5.201505241946</jacoco.version>
<groovy.version>2.4.4</groovy.version>
<groovy.version>2.5.4</groovy.version>
<orchestrator.version>3.11</orchestrator.version>
<!-- used for deployment to SonarSource Artifactory -->
<gitRepositoryName>sonar-groovy</gitRepositoryName>
Expand Down
12 changes: 11 additions & 1 deletion sonar-groovy-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>groovy-jacoco-previous</artifactId>
<version>${project.version}</version>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
Expand All @@ -84,6 +84,16 @@
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.staxmate</groupId>
<artifactId>staxmate</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.woodstox</groupId>
<artifactId>woodstox-core</artifactId>
<version>5.0.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import java.util.ArrayList;
import java.util.List;
import javax.annotation.CheckForNull;
import org.sonar.api.batch.BatchSide;
import org.sonar.api.batch.ScannerSide;
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.InputFile.Type;

@BatchSide
@ScannerSide
public class GroovyFileSystem {

private final FileSystem fileSystem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import java.util.Arrays;
import java.util.List;
import org.sonar.api.PropertyType;
import org.sonar.api.batch.BatchSide;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Qualifiers;
import org.sonar.plugins.groovy.foundation.Groovy;

@BatchSide
@ScannerSide
public class JaCoCoConfiguration {

public static final String REPORT_PATH_PROPERTY = "sonar.groovy.jacoco.reportPath";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.Map;
import javax.xml.stream.XMLStreamException;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.batch.BatchSide;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
Expand All @@ -48,7 +48,7 @@
import org.sonar.plugins.groovy.surefire.data.UnitTestResult;
import org.sonar.plugins.groovy.utils.StaxParser;

@BatchSide
@ScannerSide
public class GroovySurefireParser {

private static final Logger LOGGER = Loggers.get(GroovySurefireParser.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@

import org.junit.Test;
import org.sonar.api.Plugin;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarQubeVersion;
import org.sonar.api.SonarRuntime;
import org.sonar.api.internal.SonarRuntimeImpl;
import org.sonar.api.utils.Version;

import static org.assertj.core.api.Assertions.assertThat;

public class GroovyPluginTest {

public static final Version VERSION_7_4 = Version.create(7, 4);

@Test
public void testExtensions() {
SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(VERSION_7_4, SonarQubeSide.SERVER);

GroovyPlugin plugin = new GroovyPlugin();
Plugin.Context context = new Plugin.Context(SonarQubeVersion.V5_6);
Plugin.Context context = new Plugin.Context(runtime);
plugin.define(context);
assertThat(context.getExtensions()).hasSize(17);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.DefaultInputDir;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.*;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.config.Settings;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.plugins.groovy.foundation.Groovy;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
Expand All @@ -44,15 +45,19 @@

public class GroovySensorTest {

private Settings settings = new Settings();
private Settings settings = new MapSettings();

private FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);

private DefaultFileSystem fileSystem = new DefaultFileSystem(new File("."));

private GroovySensor sensor = new GroovySensor(settings, fileLinesContextFactory, fileSystem);

@Test
public void do_nothing_when_no_groovy_file() throws IOException {
SensorContextTester context = SensorContextTester.create(new File(""));
public void do_nothing_when_no_groovy_file() {
SensorContextTester context = SensorContextTester.create(new File("."));
context = Mockito.spy(context);

sensor = new GroovySensor(settings, fileLinesContextFactory, context.fileSystem());
sensor.execute(context);

Expand All @@ -71,17 +76,20 @@ public void compute_metrics_ignoring_header_comment() throws IOException {

private void testMetrics(boolean headerComment, int expectedCommentMetric) throws IOException {
settings.appendProperty(GroovyPlugin.IGNORE_HEADER_COMMENTS, "" + headerComment);
File sourceDir = new File("src/test/resources/org/sonar/plugins/groovy/gmetrics");
SensorContextTester context = SensorContextTester.create(new File(""));

SensorContextTester context = SensorContextTester.create(new File("src/test/resources"));

File sourceDir = new File("src/test/resources/org/sonar/plugins/groovy/gmetrics");
File sourceFile = new File(sourceDir, "Greeting.groovy");

fileSystem = context.fileSystem();
fileSystem.add(new DefaultInputDir("", sourceDir.getPath()));
DefaultInputFile groovyFile = new DefaultInputFile("", sourceFile.getPath())
.setLanguage(Groovy.KEY)
.initMetadata(new String(Files.readAllBytes(sourceFile.toPath()), "UTF-8"));

Metadata metadata = new FileMetadata().readMetadata(
Files.newBufferedReader(sourceFile.toPath(), StandardCharsets.UTF_8)
);
DefaultInputFile groovyFile = new DefaultInputFile(getIndexedFile(sourceFile.getPath()), f -> f.setMetadata(metadata));
fileSystem.add(groovyFile);
fileSystem.add(new DefaultInputFile("", "unknownFile.groovy").setLanguage(Groovy.KEY));

FileLinesContext fileLinesContext = mock(FileLinesContext.class);
when(fileLinesContextFactory.createFor(any(DefaultInputFile.class))).thenReturn(fileLinesContext);
Expand All @@ -90,21 +98,10 @@ private void testMetrics(boolean headerComment, int expectedCommentMetric) throw
sensor.execute(context);

String key = groovyFile.key();
assertThat(context.measure(key, CoreMetrics.FILES).value()).isEqualTo(1);
assertThat(context.measure(key, CoreMetrics.CLASSES).value()).isEqualTo(2);
assertThat(context.measure(key, CoreMetrics.FUNCTIONS).value()).isEqualTo(2);

assertThat(context.measure(key, CoreMetrics.LINES).value()).isEqualTo(33);
assertThat(context.measure(key, CoreMetrics.NCLOC).value()).isEqualTo(17);
assertThat(context.measure(key, CoreMetrics.COMMENT_LINES).value()).isEqualTo(expectedCommentMetric);

assertThat(context.measure(key, CoreMetrics.COMPLEXITY).value()).isEqualTo(4);
assertThat(context.measure(key, CoreMetrics.COMPLEXITY_IN_CLASSES).value()).isEqualTo(4);
assertThat(context.measure(key, CoreMetrics.COMPLEXITY_IN_FUNCTIONS).value()).isEqualTo(4);

assertThat(context.measure(key, CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION).value()).isEqualTo("1=0;2=2;4=0;6=0;8=0;10=0;12=0");
assertThat(context.measure(key, CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION).value()).isEqualTo("0=1;5=0;10=0;20=0;30=0;60=0;90=0");

// 11 times for comment because we register comment even when ignoring header comment
Mockito.verify(fileLinesContext, Mockito.times(11)).setIntValue(Mockito.eq(CoreMetrics.COMMENT_LINES_DATA_KEY), Matchers.anyInt(), Mockito.eq(1));
Mockito.verify(fileLinesContext, Mockito.times(17)).setIntValue(Mockito.eq(CoreMetrics.NCLOC_DATA_KEY), Matchers.anyInt(), Mockito.eq(1));
Expand All @@ -114,48 +111,6 @@ private void testMetrics(boolean headerComment, int expectedCommentMetric) throw
Mockito.verify(fileLinesContext, Mockito.times(1)).save();
}

@Test
public void compute_coupling_metrics() throws IOException {
SensorContextTester context = SensorContextTester.create(new File(""));

fileSystem = context.fileSystem();
// package 'org' contains class 'Greeting', used by no other class, but using classes 'Bar' and 'Foo'
DefaultInputDir org = addFileWithParentFolder("src/test/resources/org/sonar/plugins/groovy/gmetricswithcoupling/org", "Greeting.groovy");
// package 'org.foo' contains class 'Foo', used by class 'Greeting', and using class 'Bar'
DefaultInputDir org_foo = addFileWithParentFolder("src/test/resources/org/sonar/plugins/groovy/gmetricswithcoupling/org/foo", "Foo.groovy");
// package 'org.bar' contains class 'Bar', used by classes 'Greeting' and 'Foo', but using no other class
DefaultInputDir org_bar = addFileWithParentFolder("src/test/resources/org/sonar/plugins/groovy/gmetricswithcoupling/org/bar", "Bar.groovy");

FileLinesContext fileLinesContext = mock(FileLinesContext.class);
when(fileLinesContextFactory.createFor(any(DefaultInputFile.class))).thenReturn(fileLinesContext);

sensor = new GroovySensor(settings, fileLinesContextFactory, fileSystem);
sensor.execute(context);

assertCouplingMeasureAre(context, org.key(), 3, 1.0, 3, 1.0);
assertCouplingMeasureAre(context, org_foo.key(), 1, 1.0, 1, 1.0);
assertCouplingMeasureAre(context, org_bar.key(), 2, 2.0, 0, 0.0);
}

private static void assertCouplingMeasureAre(SensorContextTester context, String key, Object afferentTot, Object afferentAvg, Object efferentTot, Object efferentAvg) {
assertThat(context.measure(key, GroovyMetrics.AFFERENT_COUPLING_TOTAL.key()).value()).isEqualTo(afferentTot);
assertThat(context.measure(key, GroovyMetrics.AFFERENT_COUPLING_AVERAGE.key()).value()).isEqualTo(afferentAvg);

assertThat(context.measure(key, GroovyMetrics.EFFERENT_COUPLING_TOTAL.key()).value()).isEqualTo(efferentTot);
assertThat(context.measure(key, GroovyMetrics.EFFERENT_COUPLING_AVERAGE.key()).value()).isEqualTo(efferentAvg);
}

private DefaultInputDir addFileWithParentFolder(String dirPath, String fileName) throws IOException {
File dir = new File(dirPath);
File file = new File(dir, fileName);
DefaultInputDir inputDir = new DefaultInputDir("", dir.getPath());
fileSystem.add(inputDir);
fileSystem.add(new DefaultInputFile("", file.getPath())
.setLanguage(Groovy.KEY)
.initMetadata(new String(Files.readAllBytes(file.toPath()), "UTF-8")));
return inputDir;
}

@Test
public void test_toString() {
assertThat(sensor.toString()).isEqualTo("GroovySensor");
Expand All @@ -168,4 +123,8 @@ public void test_description() {
assertThat(defaultSensorDescriptor.languages()).containsOnly(Groovy.KEY);
}

private static DefaultIndexedFile getIndexedFile(String path) {
return new DefaultIndexedFile("", Paths.get("."), path, Groovy.KEY);
}

}
Loading