diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2015-02-11 22:42:59 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2015-02-11 22:48:52 +0100 |
commit | 15efbf64da4505fdc70104ee315e617f9bbedb57 (patch) | |
tree | 0eeaa300f6601b4fc5cb845127f702c6390c56c3 /sonar-batch | |
parent | 24de4549b07a6b7610db658551afb613deb28c82 (diff) | |
download | sonarqube-15efbf64da4505fdc70104ee315e617f9bbedb57.tar.gz sonarqube-15efbf64da4505fdc70104ee315e617f9bbedb57.zip |
Restore deprecated methods on Resource API
Diffstat (limited to 'sonar-batch')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java | 33 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java | 88 |
2 files changed, 121 insertions, 0 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index 6cf8ae826f4..4cc275dc4aa 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -38,6 +38,7 @@ import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; import org.sonar.api.measures.MeasuresFilter; import org.sonar.api.measures.MeasuresFilters; +import org.sonar.api.resources.Directory; import org.sonar.api.resources.File; import org.sonar.api.resources.Project; import org.sonar.api.resources.ProjectLink; @@ -615,6 +616,38 @@ public class DefaultIndex extends SonarIndex { if (StringUtils.isNotBlank(reference.getKey())) { return buckets.get(reference); } + String relativePathFromSourceDir = null; + boolean isTest = false; + boolean isDir = false; + if (reference instanceof File) { + File referenceFile = (File) reference; + isTest = Qualifiers.UNIT_TEST_FILE.equals(referenceFile.getQualifier()); + relativePathFromSourceDir = referenceFile.relativePathFromSourceDir(); + } else if (reference instanceof Directory) { + isDir = true; + Directory referenceDir = (Directory) reference; + relativePathFromSourceDir = referenceDir.relativePathFromSourceDir(); + if (Directory.ROOT.equals(relativePathFromSourceDir)) { + relativePathFromSourceDir = ""; + } + } + if (relativePathFromSourceDir != null) { + // Resolve using deprecated key + List<java.io.File> dirs; + if (isTest) { + dirs = getProject().getFileSystem().getTestDirs(); + } else { + dirs = getProject().getFileSystem().getSourceDirs(); + } + for (java.io.File src : dirs) { + java.io.File abs = new java.io.File(src, relativePathFromSourceDir); + Bucket b = getBucket(isDir ? Directory.fromIOFile(abs, getProject()) : File.fromIOFile(abs, getProject())); + if (b != null) { + return b; + } + } + + } return null; } diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java new file mode 100644 index 00000000000..671b5137514 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java @@ -0,0 +1,88 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 02110-1301, USA. + */ +package org.sonar.batch.mediumtest.deprecated; + +import com.google.common.collect.ImmutableMap; +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.batch.mediumtest.BatchMediumTester; +import org.sonar.batch.mediumtest.TaskResult; +import org.sonar.batch.protocol.input.ActiveRule; +import org.sonar.xoo.XooPlugin; + +import java.io.File; +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DeprecatedApiMediumTest { + + @org.junit.Rule + public TemporaryFolder temp = new TemporaryFolder(); + + public BatchMediumTester tester = BatchMediumTester.builder() + .registerPlugin("xoo", new XooPlugin()) + .addDefaultQProfile("xoo", "Sonar Way") + .activateRule(new ActiveRule("xoo", "DeprecatedResourceApi", null, "One issue per line", "MAJOR", null, "xoo")) + .build(); + + @Before + public void prepare() { + tester.start(); + } + + @After + public void stop() { + tester.stop(); + } + + @Test + public void testIssueDetails() throws IOException { + + File baseDir = temp.newFolder(); + File srcDir = new File(baseDir, "src"); + srcDir.mkdir(); + + File xooFileInRootDir = new File(srcDir, "sample.xoo"); + FileUtils.write(xooFileInRootDir, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10"); + + File xooFileInAnotherDir = new File(srcDir, "package/sample.xoo"); + FileUtils.write(xooFileInAnotherDir, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10"); + + TaskResult result = tester.newTask() + .properties(ImmutableMap.<String, String>builder() + .put("sonar.task", "scan") + .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) + .put("sonar.projectKey", "com.foo.project") + .put("sonar.projectName", "Foo Project") + .put("sonar.projectVersion", "1.0-SNAPSHOT") + .put("sonar.projectDescription", "Description of Foo Project") + .put("sonar.sources", "src") + .build()) + .start(); + + // 1 issue on root dir + 1 issue on each file + 1 issue on each file parent dir + assertThat(result.issues()).hasSize(5); + } + +} |