Skip to content

Commit

Permalink
SONARJS-332 Upgrade SonarQube dependency from 3.7 to 4.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
lindamartin committed Feb 3, 2015
1 parent 5c4729c commit 992d3a5
Show file tree
Hide file tree
Showing 16 changed files with 328 additions and 295 deletions.
5 changes: 4 additions & 1 deletion javascript-checks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>javascript-squid</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-deprecated</artifactId>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.CheckFactory;
import org.sonar.api.batch.rule.Checks;
import org.sonar.api.rules.AnnotationRuleParser;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleParam;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,45 @@
*/
package org.sonar.javascript.metrics;

import com.google.common.collect.Sets;
import com.sonar.sslr.api.AstAndTokenVisitor;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.GenericTokenType;
import com.sonar.sslr.api.Token;
import com.sonar.sslr.api.Trivia;
import org.sonar.squidbridge.SquidAstVisitor;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.resources.File;
import org.sonar.api.resources.Project;
import org.sonar.javascript.api.EcmaScriptMetric;
import org.sonar.squidbridge.SquidAstVisitor;
import org.sonar.sslr.parser.LexerlessGrammar;

import java.util.List;
import java.util.Set;
import com.google.common.collect.Sets;
import com.sonar.sslr.api.AstAndTokenVisitor;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.GenericTokenType;
import com.sonar.sslr.api.Token;
import com.sonar.sslr.api.Trivia;

/**
* Visitor that computes {@link CoreMetrics#NCLOC_DATA_KEY} and {@link CoreMetrics#COMMENT_LINES_DATA_KEY} metrics used by the DevCockpit.
*/
public class FileLinesVisitor extends SquidAstVisitor<LexerlessGrammar> implements AstAndTokenVisitor {

private final Project project;
private static final Logger LOG = LoggerFactory.getLogger(FileLinesVisitor.class);

private final FileLinesContextFactory fileLinesContextFactory;
private final FileSystem fileSystem;

private final Set<Integer> linesOfCode = Sets.newHashSet();
private final Set<Integer> linesOfComments = Sets.newHashSet();

public FileLinesVisitor(Project project, FileLinesContextFactory fileLinesContextFactory) {
this.project = project;
public FileLinesVisitor(FileLinesContextFactory fileLinesContextFactory, FileSystem fileSystem) {
this.fileLinesContextFactory = fileLinesContextFactory;
this.fileSystem = fileSystem;
}

@Override
Expand All @@ -70,15 +77,21 @@ public void visitToken(Token token) {

@Override
public void leaveFile(AstNode astNode) {
File sonarFile = File.fromIOFile(getContext().getFile(), project);
FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(sonarFile);
InputFile inputFile = fileSystem.inputFile(fileSystem.predicates().is(getContext().getFile()));

if (inputFile != null) {
FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(inputFile);

int fileLength = getContext().peekSourceCode().getInt(EcmaScriptMetric.LINES);
for (int line = 1; line <= fileLength; line++) {
fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, linesOfCode.contains(line) ? 1 : 0);
fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, linesOfComments.contains(line) ? 1 : 0);
}
fileLinesContext.save();

int fileLength = getContext().peekSourceCode().getInt(EcmaScriptMetric.LINES);
for (int line = 1; line <= fileLength; line++) {
fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, linesOfCode.contains(line) ? 1 : 0);
fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, linesOfComments.contains(line) ? 1 : 0);
} else {
LOG.warn("Cannot save measures for DevCockpit for file {}. Unable to retrieve the associated sonar resource.", getContext().getFile().getName());
}
fileLinesContext.save();

linesOfCode.clear();
linesOfComments.clear();
Expand Down
33 changes: 33 additions & 0 deletions javascript-squid/src/test/java/org/sonar/javascript/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011 SonarSource and Eriks Nukis
* [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.javascript;

import java.io.File;
import java.net.URISyntaxException;

public class TestUtils {

private TestUtils() {
}

public static File getResource(String filename) throws URISyntaxException {
return new File(TestUtils.class.getResource(filename).toURI());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@
*/
package org.sonar.javascript.metrics;

import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.resources.Resource;
import org.sonar.javascript.JavaScriptAstScanner;

import java.io.File;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.eq;
Expand All @@ -39,25 +27,43 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.File;

import org.junit.Test;
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.DefaultInputFile;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.javascript.JavaScriptAstScanner;
import org.sonar.javascript.TestUtils;

public class FileLinesVisitorTest {

@Test
public void test() {
public void test() throws Exception {
FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);

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

ProjectFileSystem fs = mock(ProjectFileSystem.class);
when(fs.getSourceDirs()).thenReturn(ImmutableList.of(new File("src/test/resources/")));
when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);

Project project = new Project("key");
project.setFileSystem(fs);

FileLinesVisitor visitor = new FileLinesVisitor(project, fileLinesContextFactory);
JavaScriptAstScanner.scanSingleFile(new File("src/test/resources/metrics/lines.js"), visitor);
File file = TestUtils.getResource("/metrics/lines.js");
FileLinesVisitor visitor = new FileLinesVisitor(fileLinesContextFactory, newFileSystem(file));
JavaScriptAstScanner.scanSingleFile(file, visitor);

verify(fileLinesContext, times(3)).setIntValue(eq(CoreMetrics.NCLOC_DATA_KEY), anyInt(), eq(1));
verify(fileLinesContext, times(1)).setIntValue(eq(CoreMetrics.COMMENT_LINES_DATA_KEY), anyInt(), eq(1));
}

private FileSystem newFileSystem(File file) {
DefaultFileSystem fs = new DefaultFileSystem();

fs.add(new DefaultInputFile(file.getName())
.setAbsolutePath(file.getAbsolutePath())
.setType(InputFile.Type.MAIN));

return fs;
}

}
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,25 @@

<maven.test.redirectTestOutputToFile>true</maven.test.redirectTestOutputToFile>

<sonar.version>3.7</sonar.version>
<sonar.version>4.5.1</sonar.version>
<sslr.version>1.20</sslr.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>javascript-squid</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-plugin-api</artifactId>
<version>${sonar.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-squid</artifactId>
<artifactId>sonar-deprecated</artifactId>
<version>${sonar.version}</version>
</dependency>
<dependency>
Expand Down
4 changes: 4 additions & 0 deletions sonar-javascript-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<artifactId>sonar-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-deprecated</artifactId>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@
*/
package org.sonar.plugins.javascript;

import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.checks.AnnotationCheckFactory;

This comment has been minimized.

Copy link
@pynicolas

pynicolas Feb 4, 2015

Contributor

Deprecated class. Can be replaced by org.sonar.api.batch.rule.CheckFactory (not trivial).

import org.sonar.api.checks.NoSonarFilter;

This comment has been minimized.

Copy link
@pynicolas

pynicolas Feb 4, 2015

Contributor

Deprecated class. Can be replaced by org.sonar.api.issue.NoSonarFilter

import org.sonar.api.component.ResourcePerspectives;
Expand All @@ -36,8 +45,6 @@
import org.sonar.api.resources.Project;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.scan.filesystem.FileQuery;
import org.sonar.api.scan.filesystem.ModuleFileSystem;
import org.sonar.javascript.EcmaScriptConfiguration;
import org.sonar.javascript.JavaScriptAstScanner;
import org.sonar.javascript.JavaScriptFileScanner;
Expand All @@ -58,43 +65,43 @@
import org.sonar.squidbridge.indexer.QueryByType;
import org.sonar.sslr.parser.LexerlessGrammar;

import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import com.google.common.collect.Lists;

public class JavaScriptSquidSensor implements Sensor {

private static final Logger LOG = LoggerFactory.getLogger(JavaScriptSquidSensor.class);
private static final Number[] FUNCTIONS_DISTRIB_BOTTOM_LIMITS = {1, 2, 4, 6, 8, 10, 12, 20, 30};
private static final Number[] FILES_DISTRIB_BOTTOM_LIMITS = {0, 5, 10, 20, 30, 60, 90};

private final AnnotationCheckFactory annotationCheckFactory;
private final FileLinesContextFactory fileLinesContextFactory;
private final ResourcePerspectives resourcePerspectives;
private final ModuleFileSystem moduleFileSystem;
private final FileSystem fileSystem;
private final NoSonarFilter noSonarFilter;
private final FilePredicate mainFilePredicate;

private Project project;
private SensorContext context;
private AstScanner<LexerlessGrammar> scanner;

public JavaScriptSquidSensor(RulesProfile profile, FileLinesContextFactory fileLinesContextFactory,
ResourcePerspectives resourcePerspectives, ModuleFileSystem moduleFileSystem, NoSonarFilter noSonarFilter) {
ResourcePerspectives resourcePerspectives, FileSystem fileSystem, NoSonarFilter noSonarFilter) {
this.annotationCheckFactory = AnnotationCheckFactory.create(profile, CheckList.REPOSITORY_KEY, CheckList.getChecks());
this.fileLinesContextFactory = fileLinesContextFactory;
this.resourcePerspectives = resourcePerspectives;
this.moduleFileSystem = moduleFileSystem;
this.fileSystem = fileSystem;
this.noSonarFilter = noSonarFilter;
this.mainFilePredicate = fileSystem.predicates().and(
fileSystem.predicates().hasType(InputFile.Type.MAIN),
fileSystem.predicates().hasLanguage(JavaScript.KEY));
}

@Override
public boolean shouldExecuteOnProject(Project project) {
return !moduleFileSystem.files(FileQuery.onSource().onLanguage(JavaScript.KEY)).isEmpty();
return fileSystem.hasFiles(mainFilePredicate);
}

@Override
public void analyse(Project project, SensorContext context) {
this.project = project;
this.context = context;

List<CodeVisitor> astNodeVisitors = Lists.newArrayList();
Expand All @@ -110,31 +117,37 @@ public void analyse(Project project, SensorContext context) {
}

astNodeVisitors.add(new VisitorsBridge(treeVisitors));
astNodeVisitors.add(new FileLinesVisitor(project, fileLinesContextFactory));
astNodeVisitors.add(new FileLinesVisitor(fileLinesContextFactory, fileSystem));

scanner = JavaScriptAstScanner.create(createConfiguration(project), astNodeVisitors.toArray(new SquidAstVisitor[astNodeVisitors.size()]));
scanner.scanFiles(moduleFileSystem.files(FileQuery.onSource().onLanguage(JavaScript.KEY)));
scanner = JavaScriptAstScanner.create(createConfiguration(), astNodeVisitors.toArray(new SquidAstVisitor[astNodeVisitors.size()]));
scanner.scanFiles(Lists.newArrayList(fileSystem.files(mainFilePredicate)));

Collection<SourceCode> squidSourceFiles = scanner.getIndex().search(new QueryByType(SourceFile.class));
save(squidSourceFiles);
}

private EcmaScriptConfiguration createConfiguration(Project project) {
return new EcmaScriptConfiguration(moduleFileSystem.sourceCharset());
private EcmaScriptConfiguration createConfiguration() {
return new EcmaScriptConfiguration(fileSystem.encoding());
}

private void save(Collection<SourceCode> squidSourceFiles) {
for (SourceCode squidSourceFile : squidSourceFiles) {
SourceFile squidFile = (SourceFile) squidSourceFile;

File sonarFile = File.fromIOFile(new java.io.File(squidFile.getKey()), project);
InputFile inputFile = fileSystem.inputFile(fileSystem.predicates().hasAbsolutePath(squidFile.getKey()));

noSonarFilter.addResource(sonarFile, squidFile.getNoSonarTagLines());
saveClassComplexity(sonarFile, squidFile);
saveFilesComplexityDistribution(sonarFile, squidFile);
saveFunctionsComplexityAndDistribution(sonarFile, squidFile);
saveMeasures(sonarFile, squidFile);
saveIssues(sonarFile, squidFile);
if (inputFile != null) {
File sonarFile = File.create(inputFile.relativePath());
noSonarFilter.addResource(sonarFile, squidFile.getNoSonarTagLines());
saveClassComplexity(sonarFile, squidFile);
saveFilesComplexityDistribution(sonarFile, squidFile);
saveFunctionsComplexityAndDistribution(sonarFile, squidFile);
saveMeasures(sonarFile, squidFile);
saveIssues(sonarFile, squidFile);

} else {
LOG.warn("Cannot save analysis information for file {}. Unable to retrieve the associated sonar resource.", squidFile.getKey());
}
}
}

Expand Down
Loading

0 comments on commit 992d3a5

Please sign in to comment.