From 992d3a563a55db13040d8dbffd147dfb2ce0f4e9 Mon Sep 17 00:00:00 2001 From: Linda Martin Date: Tue, 3 Feb 2015 18:17:23 +0100 Subject: [PATCH] SONARJS-332 Upgrade SonarQube dependency from 3.7 to 4.5.1 --- javascript-checks/pom.xml | 5 +- .../javascript/checks/CheckListTest.java | 3 + .../javascript/metrics/FileLinesVisitor.java | 51 +++++--- .../java/org/sonar/javascript/TestUtils.java | 33 ++++++ .../metrics/FileLinesVisitorTest.java | 52 ++++---- pom.xml | 9 +- sonar-javascript-plugin/pom.xml | 4 + .../javascript/JavaScriptSquidSensor.java | 63 ++++++---- .../javascript/lcov/CoverageSensor.java | 35 +++--- .../unittest/jstest/JsTestSensor.java | 6 +- .../jstestdriver/JsTestDriverSensor.java | 50 ++++---- .../javascript/JavaScriptSquidSensorTest.java | 62 +++++----- .../javascript/lcov/CoverageSensorTest.java | 112 +++++++++--------- .../unittest/jstest/JsTestSensorTest.java | 55 +++------ .../jstestdriver/JsTestDriverSensorTest.java | 82 +++++-------- .../jstestdriver/sensortests/main/Another.js | 1 + 16 files changed, 328 insertions(+), 295 deletions(-) create mode 100644 javascript-squid/src/test/java/org/sonar/javascript/TestUtils.java create mode 100644 sonar-javascript-plugin/src/test/resources/org/sonar/plugins/javascript/unittest/jstestdriver/sensortests/main/Another.js diff --git a/javascript-checks/pom.xml b/javascript-checks/pom.xml index 7f0ca637c9b..3787a883b17 100644 --- a/javascript-checks/pom.xml +++ b/javascript-checks/pom.xml @@ -16,7 +16,10 @@ ${project.groupId} javascript-squid - ${project.version} + + + org.codehaus.sonar + sonar-deprecated diff --git a/javascript-checks/src/test/java/org/sonar/javascript/checks/CheckListTest.java b/javascript-checks/src/test/java/org/sonar/javascript/checks/CheckListTest.java index 296562cf557..2e1690c864d 100644 --- a/javascript-checks/src/test/java/org/sonar/javascript/checks/CheckListTest.java +++ b/javascript-checks/src/test/java/org/sonar/javascript/checks/CheckListTest.java @@ -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; diff --git a/javascript-squid/src/main/java/org/sonar/javascript/metrics/FileLinesVisitor.java b/javascript-squid/src/main/java/org/sonar/javascript/metrics/FileLinesVisitor.java index 65c104561cc..928c52bf6ca 100644 --- a/javascript-squid/src/main/java/org/sonar/javascript/metrics/FileLinesVisitor.java +++ b/javascript-squid/src/main/java/org/sonar/javascript/metrics/FileLinesVisitor.java @@ -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 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 linesOfCode = Sets.newHashSet(); private final Set 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 @@ -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(); diff --git a/javascript-squid/src/test/java/org/sonar/javascript/TestUtils.java b/javascript-squid/src/test/java/org/sonar/javascript/TestUtils.java new file mode 100644 index 00000000000..427d6f652f7 --- /dev/null +++ b/javascript-squid/src/test/java/org/sonar/javascript/TestUtils.java @@ -0,0 +1,33 @@ +/* + * SonarQube JavaScript Plugin + * Copyright (C) 2011 SonarSource and Eriks Nukis + * dev@sonar.codehaus.org + * + * 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()); + } +} diff --git a/javascript-squid/src/test/java/org/sonar/javascript/metrics/FileLinesVisitorTest.java b/javascript-squid/src/test/java/org/sonar/javascript/metrics/FileLinesVisitorTest.java index 90760cf3c0d..9149771f932 100644 --- a/javascript-squid/src/test/java/org/sonar/javascript/metrics/FileLinesVisitorTest.java +++ b/javascript-squid/src/test/java/org/sonar/javascript/metrics/FileLinesVisitorTest.java @@ -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; @@ -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; + } + } diff --git a/pom.xml b/pom.xml index 9e19984a8ee..4b805f07076 100644 --- a/pom.xml +++ b/pom.xml @@ -67,12 +67,17 @@ true - 3.7 + 4.5.1 1.20 + + ${project.groupId} + javascript-squid + ${project.version} + org.codehaus.sonar sonar-plugin-api @@ -80,7 +85,7 @@ org.codehaus.sonar - sonar-squid + sonar-deprecated ${sonar.version} diff --git a/sonar-javascript-plugin/pom.xml b/sonar-javascript-plugin/pom.xml index 821e38db795..b2b8bf3e9c0 100644 --- a/sonar-javascript-plugin/pom.xml +++ b/sonar-javascript-plugin/pom.xml @@ -34,6 +34,10 @@ sonar-plugin-api provided + + org.codehaus.sonar + sonar-deprecated + ${project.groupId} diff --git a/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/JavaScriptSquidSensor.java b/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/JavaScriptSquidSensor.java index 2e1872a4df2..51f14d8f39c 100644 --- a/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/JavaScriptSquidSensor.java +++ b/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/JavaScriptSquidSensor.java @@ -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; import org.sonar.api.checks.NoSonarFilter; import org.sonar.api.component.ResourcePerspectives; @@ -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; @@ -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 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 astNodeVisitors = Lists.newArrayList(); @@ -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 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 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()); + } } } diff --git a/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/lcov/CoverageSensor.java b/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/lcov/CoverageSensor.java index b8a9c1c29ad..d7a727f7a7f 100644 --- a/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/lcov/CoverageSensor.java +++ b/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/lcov/CoverageSensor.java @@ -24,6 +24,9 @@ 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.config.Settings; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.CoverageMeasuresBuilder; @@ -31,7 +34,6 @@ import org.sonar.api.measures.PropertiesBuilder; import org.sonar.api.resources.Project; import org.sonar.api.scan.filesystem.FileQuery; -import org.sonar.api.scan.filesystem.ModuleFileSystem; import org.sonar.plugins.javascript.JavaScriptPlugin; import org.sonar.plugins.javascript.core.JavaScript; @@ -42,17 +44,21 @@ public class CoverageSensor implements Sensor { private static final Logger LOG = LoggerFactory.getLogger(CoverageSensor.class); - private final ModuleFileSystem moduleFileSystem; + private final FileSystem fileSystem; private final Settings settings; + private final FilePredicate mainFilePredicate; - public CoverageSensor(ModuleFileSystem moduleFileSystem, Settings settings) { - this.moduleFileSystem = moduleFileSystem; + public CoverageSensor(FileSystem fileSystem, Settings settings) { + this.fileSystem = fileSystem; this.settings = settings; + 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 @@ -68,14 +74,14 @@ public void analyse(Project project, SensorContext context) { } protected void saveZeroValueForAllFiles(Project project, SensorContext context) { - for (File file : moduleFileSystem.files(FileQuery.onSource().onLanguage(JavaScript.KEY))) { - saveZeroValueForResource(org.sonar.api.resources.File.fromIOFile(file, project), context); + for (InputFile inputFile : fileSystem.inputFiles(mainFilePredicate)) { + saveZeroValueForResource(org.sonar.api.resources.File.create(inputFile.relativePath()), context); } } protected void saveMeasureFromLCOVFile(Project project, SensorContext context) { String providedPath = settings.getString(JavaScriptPlugin.LCOV_REPORT_PATH); - File lcovFile = getIOFile(moduleFileSystem.baseDir(), providedPath); + File lcovFile = getIOFile(fileSystem.baseDir(), providedPath); if (!lcovFile.isFile()) { LOG.warn("No coverage information will be saved because LCOV file cannot be analysed. Provided LCOV file path: {}", providedPath); @@ -84,13 +90,13 @@ protected void saveMeasureFromLCOVFile(Project project, SensorContext context) { LOG.info("Analysing {}", lcovFile); - LCOVParser parser = new LCOVParser(moduleFileSystem.baseDir()); + LCOVParser parser = new LCOVParser(fileSystem.baseDir()); Map coveredFiles = parser.parseFile(lcovFile); - for (File file : moduleFileSystem.files(FileQuery.onSource().onLanguage(JavaScript.KEY))) { + for (InputFile inputFile : fileSystem.inputFiles(mainFilePredicate)) { try { - CoverageMeasuresBuilder fileCoverage = coveredFiles.get(file.getAbsolutePath()); - org.sonar.api.resources.File resource = org.sonar.api.resources.File.fromIOFile(file, project); + CoverageMeasuresBuilder fileCoverage = coveredFiles.get(inputFile.absolutePath()); + org.sonar.api.resources.File resource = org.sonar.api.resources.File.create(inputFile.relativePath()); if (fileCoverage != null) { for (Measure measure : fileCoverage.createMeasures()) { @@ -101,9 +107,9 @@ protected void saveMeasureFromLCOVFile(Project project, SensorContext context) { saveZeroValueForResource(resource, context); } } catch (Exception e) { - LOG.error("Problem while calculating coverage for " + file.getAbsolutePath(), e); + LOG.error("Problem while calculating coverage for " + inputFile.absolutePath(), e); } - } + } } private void saveZeroValueForResource(org.sonar.api.resources.File resource, SensorContext context) { @@ -143,7 +149,6 @@ public static File getIOFile(File baseDir, String path) { file = new File(baseDir, path); } - return file; } diff --git a/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/unittest/jstest/JsTestSensor.java b/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/unittest/jstest/JsTestSensor.java index 39da87170cf..2d17aef3a40 100644 --- a/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/unittest/jstest/JsTestSensor.java +++ b/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/unittest/jstest/JsTestSensor.java @@ -20,22 +20,22 @@ package org.sonar.plugins.javascript.unittest.jstest; import org.sonar.api.batch.SensorContext; +import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; -import org.sonar.api.scan.filesystem.ModuleFileSystem; import org.sonar.plugins.javascript.JavaScriptPlugin; import org.sonar.plugins.javascript.unittest.jstestdriver.JsTestDriverSensor; public class JsTestSensor extends JsTestDriverSensor { - public JsTestSensor(ModuleFileSystem fileSystem, Settings settings) { + public JsTestSensor(FileSystem fileSystem, Settings settings) { super(fileSystem, settings); } @Override public void analyse(Project project, SensorContext context) { String jsTestDriverFolder = getReportsDirectoryPath(); - collect(project, context, getIOFile(jsTestDriverFolder)); + collect(context, getIOFile(jsTestDriverFolder)); } @Override diff --git a/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/unittest/jstestdriver/JsTestDriverSensor.java b/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/unittest/jstestdriver/JsTestDriverSensor.java index 07d73651c67..e969f8a9cee 100644 --- a/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/unittest/jstestdriver/JsTestDriverSensor.java +++ b/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/unittest/jstestdriver/JsTestDriverSensor.java @@ -24,61 +24,62 @@ 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.config.Settings; import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; -import org.sonar.api.scan.filesystem.FileQuery; -import org.sonar.api.scan.filesystem.ModuleFileSystem; import org.sonar.plugins.javascript.JavaScriptPlugin; import org.sonar.plugins.javascript.core.JavaScript; import org.sonar.plugins.javascript.unittest.surefireparser.AbstractSurefireParser; import java.io.File; -import java.util.List; public class JsTestDriverSensor implements Sensor { - protected ModuleFileSystem fileSystem; + protected FileSystem fileSystem; protected Settings settings; + private final FilePredicate mainFilePredicate; + private final FilePredicate testFilePredicate; - public JsTestDriverSensor(ModuleFileSystem fileSystem, Settings settings) { + public JsTestDriverSensor(FileSystem fileSystem, Settings settings) { this.fileSystem = fileSystem; this.settings = settings; + this.mainFilePredicate = fileSystem.predicates().and( + fileSystem.predicates().hasType(InputFile.Type.MAIN), + fileSystem.predicates().hasLanguage(JavaScript.KEY)); + + this.testFilePredicate = fileSystem.predicates().and( + fileSystem.predicates().hasType(InputFile.Type.TEST), + fileSystem.predicates().hasLanguage(JavaScript.KEY)); } private static final Logger LOG = LoggerFactory.getLogger(JsTestDriverSensor.class); @Override public boolean shouldExecuteOnProject(Project project) { - return StringUtils.isNotBlank(getReportsDirectoryPath()) && - // Required for compatibility with SonarQube 3.7 - (JavaScript.KEY.equals(project.getLanguageKey()) - || StringUtils.isBlank(project.getLanguageKey()) && !fileSystem.files(FileQuery.onSource().onLanguage(JavaScript.KEY)).isEmpty()); + return StringUtils.isNotBlank(getReportsDirectoryPath()) && fileSystem.hasFiles(mainFilePredicate); } @Override public void analyse(Project project, SensorContext context) { - collect(project, context, getIOFile(getReportsDirectoryPath())); + collect(context, getIOFile(getReportsDirectoryPath())); } - protected void collect(final Project project, final SensorContext context, File reportsDir) { + protected void collect(final SensorContext context, File reportsDir) { LOG.info("Parsing Unit Test run results in Surefire format from folder {}", reportsDir); new AbstractSurefireParser() { @Override protected Resource getUnitTestResource(String classKey) { - File unitTestFile = getUnitTestFile(fileSystem.testDirs(), getUnitTestFileName(classKey)); - org.sonar.api.resources.File sonarFile = org.sonar.api.resources.File.fromIOFile(unitTestFile, project); + fileSystem.predicates().hasType(InputFile.Type.MAIN); + org.sonar.api.resources.File sonarFile = org.sonar.api.resources.File.create(getTestFileRelativePathToBaseDir(getUnitTestFileName(classKey))); - if (sonarFile == null) { - // support SQ < 4.2 - sonarFile = org.sonar.api.resources.File.fromIOFile(unitTestFile, fileSystem.testDirs()); - } return context.getResource(sonarFile); } }.collect(context, reportsDir); - } protected String getUnitTestFileName(String className) { @@ -89,16 +90,13 @@ protected String getUnitTestFileName(String className) { return fileName; } - protected File getUnitTestFile(List testDirectories, String name) { - File unitTestFile = new File(""); - for (File dir : testDirectories) { - unitTestFile = new File(dir, name); - - if (unitTestFile.exists()) { - break; + protected String getTestFileRelativePathToBaseDir(String name) { + for (InputFile inputFile : fileSystem.inputFiles(testFilePredicate)) { + if (inputFile.file().getAbsolutePath().endsWith(name)) { + return inputFile.relativePath(); } } - return unitTestFile; + return name; } /** diff --git a/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/JavaScriptSquidSensorTest.java b/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/JavaScriptSquidSensorTest.java index 7b4446dd5ed..a2aea9854db 100644 --- a/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/JavaScriptSquidSensorTest.java +++ b/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/JavaScriptSquidSensorTest.java @@ -19,10 +19,19 @@ */ package org.sonar.plugins.javascript; -import com.google.common.collect.ImmutableList; +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import org.junit.Before; import org.junit.Test; import org.sonar.api.batch.SensorContext; +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.checks.NoSonarFilter; import org.sonar.api.component.ResourcePerspectives; import org.sonar.api.measures.CoreMetrics; @@ -30,67 +39,49 @@ import org.sonar.api.measures.FileLinesContextFactory; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Project; -import org.sonar.api.resources.ProjectFileSystem; import org.sonar.api.resources.Resource; -import org.sonar.api.scan.filesystem.FileQuery; -import org.sonar.api.scan.filesystem.ModuleFileSystem; - -import java.io.File; -import java.nio.charset.Charset; -import java.util.Arrays; -import java.util.Collections; - -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import org.sonar.plugins.javascript.core.JavaScript; +import org.sonar.test.TestUtils; public class JavaScriptSquidSensorTest { + private final DefaultFileSystem fileSystem = new DefaultFileSystem(); private FileLinesContextFactory fileLinesContextFactory; - private Project project; + private final Project project = new Project("project"); @Before public void setUp() { fileLinesContextFactory = mock(FileLinesContextFactory.class); FileLinesContext fileLinesContext = mock(FileLinesContext.class); - when(fileLinesContextFactory.createFor(any(Resource.class))).thenReturn(fileLinesContext); - project = new Project("key"); - ProjectFileSystem pfs = mock(ProjectFileSystem.class); - when(pfs.getSourceDirs()).thenReturn(Arrays.asList(new File("src/test/resources/cpd/"))); + when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext); - project.setFileSystem(pfs); } @Test public void should_execute_if_js_files() { - ModuleFileSystem fs = mock(ModuleFileSystem.class); - project = new Project("key"); - - JavaScriptSquidSensor sensor = new JavaScriptSquidSensor(mock(RulesProfile.class), fileLinesContextFactory, mock(ResourcePerspectives.class), fs, new NoSonarFilter()); + DefaultFileSystem localFS = new DefaultFileSystem(); + JavaScriptSquidSensor sensor = new JavaScriptSquidSensor(mock(RulesProfile.class), fileLinesContextFactory, mock(ResourcePerspectives.class), localFS, new NoSonarFilter( + mock(SensorContext.class))); // no JS files -> do not execute - when(fs.files(any(FileQuery.class))).thenReturn(Collections.emptyList()); assertThat(sensor.shouldExecuteOnProject(project)).isFalse(); // at least one JS file -> do execute - when(fs.files(any(FileQuery.class))).thenReturn(Arrays.asList(mock(File.class))); + localFS.add(new DefaultInputFile("file.js").setType(InputFile.Type.MAIN).setLanguage(JavaScript.KEY)); assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); } @Test public void should_analyse() { - ModuleFileSystem fs = mock(ModuleFileSystem.class); - when(fs.sourceCharset()).thenReturn(Charset.forName("UTF-8")); - ImmutableList files = ImmutableList.of(new File("src/test/resources/cpd/Person.js")); - when(fs.files(any(FileQuery.class))).thenReturn(files); - + fileSystem.add(new DefaultInputFile("Person.js") + .setAbsolutePath(TestUtils.getResource("/cpd/Person.js").getAbsolutePath()) + .setType(InputFile.Type.MAIN) + .setLanguage(JavaScript.KEY)); SensorContext context = mock(SensorContext.class); - JavaScriptSquidSensor sensor = new JavaScriptSquidSensor(mock(RulesProfile.class), fileLinesContextFactory, mock(ResourcePerspectives.class), fs, new NoSonarFilter()); + JavaScriptSquidSensor sensor = new JavaScriptSquidSensor(mock(RulesProfile.class), fileLinesContextFactory, mock(ResourcePerspectives.class), fileSystem, new NoSonarFilter( + mock(SensorContext.class))); sensor.analyse(project, context); @@ -111,8 +102,9 @@ public void test_to_string() { mock(RulesProfile.class), fileLinesContextFactory, mock(ResourcePerspectives.class), - mock(ModuleFileSystem.class), new NoSonarFilter()); + new DefaultFileSystem(), new NoSonarFilter(mock(SensorContext.class))); assertThat(sensor.toString()).isNotNull(); } + } diff --git a/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/lcov/CoverageSensorTest.java b/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/lcov/CoverageSensorTest.java index 1bdadf0763b..a27be56a555 100644 --- a/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/lcov/CoverageSensorTest.java +++ b/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/lcov/CoverageSensorTest.java @@ -19,97 +19,103 @@ */ package org.sonar.plugins.javascript.lcov; -import com.google.common.collect.ImmutableList; +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.anyObject; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + import org.junit.Before; import org.junit.Test; import org.sonar.api.batch.SensorContext; +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.config.Settings; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; import org.sonar.api.measures.Metric; import org.sonar.api.resources.Project; -import org.sonar.api.resources.ProjectFileSystem; import org.sonar.api.resources.Resource; -import org.sonar.api.scan.filesystem.FileQuery; -import org.sonar.api.scan.filesystem.ModuleFileSystem; import org.sonar.plugins.javascript.JavaScriptPlugin; - -import java.io.File; -import java.util.ArrayList; -import java.util.Collections; - -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.*; +import org.sonar.plugins.javascript.core.JavaScript; +import org.sonar.test.TestUtils; public class CoverageSensorTest { - private static final File baseDir = new File("src/test/resources/org/sonar/plugins/javascript/unittest/jstestdriver/"); - private CoverageSensor sensor; private SensorContext context; private Settings settings; - private ModuleFileSystem fileSystem = mock(ModuleFileSystem.class); + private DefaultFileSystem fileSystem = new DefaultFileSystem(); private Project project; @Before public void init() { + fileSystem.setBaseDir(TestUtils.getResource("org/sonar/plugins/javascript/unittest/jstestdriver/")); + settings = new Settings(); settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATH, "jsTestDriver.conf-coverage.dat"); sensor = new CoverageSensor(fileSystem, settings); context = mock(SensorContext.class); - project = mockProject(); + project = new Project("project"); + } @Test public void test_should_execute() { + DefaultFileSystem localFS = new DefaultFileSystem(); + Settings localSettings = new Settings(); + localSettings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATH, "jsTestDriver.conf-coverage.dat"); + CoverageSensor localSensor = new CoverageSensor(localFS, localSettings); + // no JS files -> do not execute - when(fileSystem.files(any(FileQuery.class))).thenReturn(new ArrayList()); - assertThat(sensor.shouldExecuteOnProject(project)).isFalse(); + assertThat(localSensor.shouldExecuteOnProject(project)).isFalse(); // at least one JS file -> do execute - when(fileSystem.files(any(FileQuery.class))).thenReturn(Collections.singletonList(mock(File.class))); - assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); + localFS.add(new DefaultInputFile("file.js").setType(InputFile.Type.MAIN).setLanguage(JavaScript.KEY)); + assertThat(localSensor.shouldExecuteOnProject(project)).isTrue(); // no path to report -> do execute - settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATH, ""); - assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); + localSettings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATH, ""); + assertThat(localSensor.shouldExecuteOnProject(project)).isTrue(); } @Test public void report_not_found() throws Exception { - Project project = new Project("key"); - when(fileSystem.baseDir()).thenReturn((new File("bad/base/module/dir"))); + // Setting with bad report path + Settings localSettings = new Settings(); + localSettings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATH, "/fake/path/jsTestDriver.conf-coverage.dat"); - sensor.analyse(project, context); + // sensor with local settings + CoverageSensor localSensor = new CoverageSensor(fileSystem, localSettings); + localSensor.analyse(project, context); verifyZeroInteractions(context); } @Test - public void testFileInJsTestDriverCoverageReport() { - when(fileSystem.baseDir()).thenReturn((baseDir)); - when(fileSystem.files(any(FileQuery.class))).thenReturn(ImmutableList.of( - new File(baseDir, "sensortests/main/Person.js"), - new File(baseDir, "sensortests/main/Person.js"), - new File(baseDir, "sensortests/test/PersonTest.js"))); - + public void test_file_in_coverage_report() { + fileSystem.add(newSourceInputFile("Another.js", "org/sonar/plugins/javascript/unittest/jstestdriver/sensortests/main/Another.js")); + fileSystem.add(newSourceInputFile("Person.js", "org/sonar/plugins/javascript/unittest/jstestdriver/sensortests/main/Person.js")); sensor.analyse(project, context); - verify(context, atLeast(3)).saveMeasure((Resource) anyObject(), (Measure) anyObject()); + + verify(context, atLeast(3)).saveMeasure(any(Resource.class), (Measure) anyObject()); } @Test - public void testFileNotInJsTestDriverCoverageReport() { - File fileToCheck = new File(baseDir, "another.js"); - when(fileSystem.baseDir()).thenReturn((baseDir)); - when(fileSystem.files(any(FileQuery.class))).thenReturn(ImmutableList.of( - fileToCheck, - new File(baseDir, "sensortests/main/Person.js"), - new File(baseDir, "sensortests/test/PersonTest.js"))); - - when(context.getMeasure(org.sonar.api.resources.File.fromIOFile(fileToCheck, project), CoreMetrics.LINES)).thenReturn( + public void test_file_not_in_coverage_report() { + fileSystem.add(newSourceInputFile("Another.js", "org/sonar/plugins/javascript/unittest/jstestdriver/sensortests/main/Another.js")); + + when(context.getMeasure(any(org.sonar.api.resources.File.class), eq(CoreMetrics.LINES))).thenReturn( new Measure(CoreMetrics.LINES, (double) 20)); - when(context.getMeasure(org.sonar.api.resources.File.fromIOFile(fileToCheck, project), CoreMetrics.NCLOC)).thenReturn( - new Measure(CoreMetrics.LINES, (double) 22)); + when(context.getMeasure(any(org.sonar.api.resources.File.class), eq(CoreMetrics.NCLOC))) + .thenReturn( + new Measure(CoreMetrics.LINES, (double) 22)); sensor.analyse(project, context); @@ -118,10 +124,8 @@ public void testFileNotInJsTestDriverCoverageReport() { } @Test - public void testSaveZeroValueForAllFiles() throws Exception { - when(fileSystem.baseDir()).thenReturn((baseDir)); - when(fileSystem.files(any(FileQuery.class))).thenReturn(ImmutableList.of( - new File(baseDir, "sensortests/main/Person.js"))); + public void test_save_zero_value_for_all_files() throws Exception { + fileSystem.add(newSourceInputFile("Person.js", "org/sonar/plugins/javascript/unittest/jstestdriver/sensortests/main/Person.js")); settings.setProperty(JavaScriptPlugin.FORCE_ZERO_COVERAGE_KEY, "true"); settings.setProperty(JavaScriptPlugin.LCOV_REPORT_PATH, ""); @@ -138,13 +142,11 @@ public void test_toString() { assertThat(sensor.toString()).isEqualTo("CoverageSensor"); } - public static Project mockProject() { - ProjectFileSystem pfs = mock(ProjectFileSystem.class); - when(pfs.getSourceDirs()).thenReturn(ImmutableList.of(new File(baseDir, "main"))); - - Project project = new Project("key"); - project.setFileSystem(pfs); - - return project; + public DefaultInputFile newSourceInputFile(String name, String path) { + return new DefaultInputFile(name) + .setAbsolutePath(TestUtils.getResource(path).getAbsolutePath()) + .setType(InputFile.Type.MAIN) + .setLanguage(JavaScript.KEY); } + } diff --git a/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/unittest/jstest/JsTestSensorTest.java b/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/unittest/jstest/JsTestSensorTest.java index 4a35e44f0ad..e2a53fef0c3 100644 --- a/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/unittest/jstest/JsTestSensorTest.java +++ b/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/unittest/jstest/JsTestSensorTest.java @@ -19,45 +19,43 @@ */ package org.sonar.plugins.javascript.unittest.jstest; -import com.google.common.collect.ImmutableList; +import static org.fest.assertions.Assertions.assertThat; + import org.junit.Before; import org.junit.Test; +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.config.Settings; import org.sonar.api.resources.Project; -import org.sonar.api.scan.filesystem.FileQuery; -import org.sonar.api.scan.filesystem.ModuleFileSystem; import org.sonar.plugins.javascript.JavaScriptPlugin; - -import java.io.File; - -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import org.sonar.plugins.javascript.core.JavaScript; public class JsTestSensorTest { + private final Project project = new Project("project"); private Settings settings; private JsTestSensor sensor; + private DefaultFileSystem fileSystem; @Before public void setUp() { settings = new Settings(); - sensor = new JsTestSensor(mockFileSystem(), settings); + fileSystem = new DefaultFileSystem(); + sensor = new JsTestSensor(fileSystem, settings); } @Test public void test_shouldExecuteOnProject() { - // Not a JavaScript project - assertThat(sensor.shouldExecuteOnProject(mockProject("java"))).isFalse(); + // No JavaScipt files in fileSystem & no report + assertThat(sensor.shouldExecuteOnProject(project)).isFalse(); // No report path provided - assertThat(sensor.shouldExecuteOnProject(mockProject("js"))).isFalse(); - assertThat(mock_sensor_for_SQ_over_4_0().shouldExecuteOnProject(mockProject(""))).isFalse(); + fileSystem.add(new DefaultInputFile("File.js").setLanguage(JavaScript.KEY).setType(InputFile.Type.MAIN)); + assertThat(sensor.shouldExecuteOnProject(project)).isFalse(); settings.setProperty(JavaScriptPlugin.JSTEST_REPORTS_PATH, "jstest"); - assertThat(sensor.shouldExecuteOnProject(mockProject("js"))).isTrue(); - assertThat(mock_sensor_for_SQ_over_4_0().shouldExecuteOnProject(mockProject(""))).isTrue(); + assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); } @Test @@ -65,28 +63,5 @@ public void test_toString() { assertThat(sensor.toString()).isEqualTo("JsTestSensor"); } - private Project mockProject(final String language) { - return new Project("mock") { - @Override - public String getLanguageKey() { - return language; - } - }; - } - - private ModuleFileSystem mockFileSystem() { - ModuleFileSystem fs = mock(ModuleFileSystem.class); - when(fs.files(any(FileQuery.class))).thenReturn(ImmutableList.of(new File("mock"))); - - return fs; - } - - private JsTestSensor mock_sensor_for_SQ_over_4_0() { - ModuleFileSystem fs = mock(ModuleFileSystem.class); - when(fs.files(any(FileQuery.class))).thenReturn(ImmutableList.of(new File("mock"))); - - return new JsTestSensor(fs, settings); - } - } diff --git a/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/unittest/jstestdriver/JsTestDriverSensorTest.java b/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/unittest/jstestdriver/JsTestDriverSensorTest.java index 93517c8cd0c..634775daa27 100644 --- a/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/unittest/jstestdriver/JsTestDriverSensorTest.java +++ b/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/unittest/jstestdriver/JsTestDriverSensorTest.java @@ -19,42 +19,38 @@ */ package org.sonar.plugins.javascript.unittest.jstestdriver; -import com.google.common.collect.ImmutableList; +import static org.fest.assertions.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.io.File; + import org.junit.Before; import org.junit.Test; import org.sonar.api.batch.SensorContext; +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.config.Settings; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.resources.Project; -import org.sonar.api.resources.ProjectFileSystem; import org.sonar.api.resources.Resource; -import org.sonar.api.scan.filesystem.FileQuery; -import org.sonar.api.scan.filesystem.ModuleFileSystem; import org.sonar.plugins.javascript.JavaScriptPlugin; +import org.sonar.plugins.javascript.core.JavaScript; import org.sonar.test.TestUtils; -import java.io.File; -import java.nio.charset.Charset; -import java.util.Arrays; - -import static org.fest.assertions.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - public class JsTestDriverSensorTest { private static final File PROJECT_BASE_DIR = TestUtils.getResource("org/sonar/plugins/javascript/unittest/jstestdriver/sensortests"); - private static final File TEST_DIR = new File(PROJECT_BASE_DIR, "test"); private JsTestDriverSensor sensor; private SensorContext context; private Settings settings; - private ModuleFileSystem fileSystem = mock(ModuleFileSystem.class); + private DefaultFileSystem fileSystem = new DefaultFileSystem(); + private final Project project = new Project("project"); @Before public void init() { @@ -66,27 +62,21 @@ public void init() { @Test public void test_shouldExecuteOnProject() { // Not a JavaScript project - assertThat(sensor.shouldExecuteOnProject(mockProject("java"))).isFalse(); + assertThat(sensor.shouldExecuteOnProject(project)).isFalse(); // No report path provided - assertThat(sensor.shouldExecuteOnProject(mockProject("js"))).isFalse(); - assertThat(mock_sensor_for_SQ_over_4_0().shouldExecuteOnProject(mockProject(""))).isFalse(); + assertThat(sensor.shouldExecuteOnProject(project)).isFalse(); settings.setProperty(JavaScriptPlugin.JSTESTDRIVER_REPORTS_PATH, "jstestdriver"); - assertThat(sensor.shouldExecuteOnProject(mockProject("js"))).isTrue(); - assertThat(mock_sensor_for_SQ_over_4_0().shouldExecuteOnProject(mockProject(""))).isTrue(); + fileSystem.add(new DefaultInputFile("File.jsp").setLanguage(JavaScript.KEY).setType(InputFile.Type.MAIN)); + assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); } @Test public void testAnalyseUnitTests() { settings.setProperty(JavaScriptPlugin.JSTESTDRIVER_REPORTS_PATH, "reports/jstestdriver"); - - when(fileSystem.sourceCharset()).thenReturn(Charset.defaultCharset()); - - when(fileSystem.baseDir()).thenReturn(PROJECT_BASE_DIR); - when(fileSystem.testDirs()).thenReturn(Arrays.asList(TEST_DIR)); - - Project project = mockProject("js"); + fileSystem.setBaseDir(PROJECT_BASE_DIR); + fileSystem.add(newTestInputFile("PersonTest.js", "org/sonar/plugins/javascript/unittest/jstestdriver/sensortests/test/PersonTest.js")); sensor.analyse(project, context); @@ -109,28 +99,18 @@ public void test_toString() { assertThat(sensor.toString()).isEqualTo("JsTestDriverSensor"); } - private Project mockProject(final String language) { - return new Project("mock") { - @Override - public String getLanguageKey() { - return language; - } - - @Override - public ProjectFileSystem getFileSystem() { - ProjectFileSystem pfs = mock(ProjectFileSystem.class); - when(pfs.getSourceDirs()).thenReturn(ImmutableList.of(TEST_DIR)); - - return pfs; - } - }; + public DefaultInputFile newSourceInputFile(String name, String path) { + return new DefaultInputFile(name) + .setAbsolutePath(TestUtils.getResource(path).getAbsolutePath()) + .setType(InputFile.Type.MAIN) + .setLanguage(JavaScript.KEY); } - private JsTestDriverSensor mock_sensor_for_SQ_over_4_0() { - ModuleFileSystem fs = mock(ModuleFileSystem.class); - when(fs.files(any(FileQuery.class))).thenReturn(ImmutableList.of(new File("mock"))); - - return new JsTestDriverSensor(fs, settings); + public DefaultInputFile newTestInputFile(String name, String path) { + return new DefaultInputFile(name) + .setAbsolutePath(TestUtils.getResource(path).getAbsolutePath()) + .setType(InputFile.Type.TEST) + .setLanguage(JavaScript.KEY); } } diff --git a/sonar-javascript-plugin/src/test/resources/org/sonar/plugins/javascript/unittest/jstestdriver/sensortests/main/Another.js b/sonar-javascript-plugin/src/test/resources/org/sonar/plugins/javascript/unittest/jstestdriver/sensortests/main/Another.js new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/sonar-javascript-plugin/src/test/resources/org/sonar/plugins/javascript/unittest/jstestdriver/sensortests/main/Another.js @@ -0,0 +1 @@ +