diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-01-17 17:45:09 +0100 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-01-27 16:26:30 +0100 |
commit | eea589c564a924993e8edba9d8fa9691e756bce4 (patch) | |
tree | c112f6d41d7f4dfeb7ad8d14f895e1b879a68411 /sonar-plugin-api/src | |
parent | 211a993bd85b5d12ace1686b133677381da8c597 (diff) | |
download | sonarqube-eea589c564a924993e8edba9d8fa9691e756bce4.tar.gz sonarqube-eea589c564a924993e8edba9d8fa9691e756bce4.zip |
Refactor resources API
Diffstat (limited to 'sonar-plugin-api/src')
21 files changed, 252 insertions, 268 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java index 0e8bf735df6..3243f8dcab5 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java @@ -163,5 +163,7 @@ public interface FileSystem { @CheckForNull InputDir inputDir(String relativePath); + + InputModule module(); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputComponent.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputComponent.java index 961156d56c0..c6272fb8aee 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputComponent.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputComponent.java @@ -33,10 +33,11 @@ public interface InputComponent { * Component key shared by all part of SonarQube (batch, server, WS...) */ String key(); - + /** * Is the component an {@link InputFile} */ boolean isFile(); + } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputModule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputModule.java index 78902475cd5..71387ae49d5 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputModule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputModule.java @@ -27,5 +27,4 @@ import org.sonar.api.batch.sensor.SensorContext; * @since 5.2 */ public interface InputModule extends InputComponent { - } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java index b6a8d3aaa7e..c58d668515b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java @@ -40,6 +40,7 @@ import org.sonar.api.batch.fs.FilePredicates; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.InputModule; import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.api.utils.PathUtils; @@ -199,6 +200,11 @@ public class DefaultFileSystem implements FileSystem { cache.add(inputDir); return this; } + + public DefaultFileSystem add(InputModule inputModule) { + cache.add(inputModule); + return this; + } /** * Adds a language to the list. To be used only for unit tests that need to use {@link #languages()} without @@ -234,6 +240,8 @@ public class DefaultFileSystem implements FileSystem { protected abstract void doAdd(InputDir inputDir); + protected abstract void doAdd(InputModule inputModule); + final void add(InputFile inputFile) { doAdd(inputFile); } @@ -242,6 +250,10 @@ public class DefaultFileSystem implements FileSystem { doAdd(inputDir); } + public void add(InputModule inputModule) { + doAdd(inputModule); + } + } /** @@ -250,6 +262,7 @@ public class DefaultFileSystem implements FileSystem { private static class MapCache extends Cache { private final Map<String, InputFile> fileMap = new HashMap<>(); private final Map<String, InputDir> dirMap = new HashMap<>(); + private InputModule module; @Override public Iterable<InputFile> inputFiles() { @@ -266,6 +279,10 @@ public class DefaultFileSystem implements FileSystem { return dirMap.get(relativePath); } + public InputModule module() { + return module; + } + @Override protected void doAdd(InputFile inputFile) { fileMap.put(inputFile.relativePath(), inputFile); @@ -275,6 +292,11 @@ public class DefaultFileSystem implements FileSystem { protected void doAdd(InputDir inputDir) { dirMap.put(inputDir.relativePath(), inputDir); } + + @Override + protected void doAdd(InputModule inputModule) { + module = inputModule; + } } @Override diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java index e3a1107e28f..642b6e1176b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java @@ -37,10 +37,15 @@ public class DefaultIndexedFile extends DefaultInputComponent implements Indexed private final Type type; public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath) { - this(moduleKey, moduleBaseDir, relativePath, Type.MAIN); + this(moduleKey, moduleBaseDir, relativePath, TestInputFileBuilder.batchId++); } - public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, Type type) { + public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, int batchId) { + this(moduleKey, moduleBaseDir, relativePath, Type.MAIN, batchId); + } + + public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, Type type, int batchId) { + super(batchId); this.moduleKey = moduleKey; this.relativePath = PathUtils.sanitize(relativePath); this.moduleBaseDir = moduleBaseDir.normalize(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputComponent.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputComponent.java index 94acf6e9dc0..c04104d6b92 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputComponent.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputComponent.java @@ -25,6 +25,11 @@ import org.sonar.api.batch.fs.InputComponent; * @since 5.2 */ public abstract class DefaultInputComponent implements InputComponent { + private int id; + + public DefaultInputComponent(int batchId) { + this.id = batchId; + } @Override public boolean equals(Object o) { @@ -39,6 +44,10 @@ public abstract class DefaultInputComponent implements InputComponent { return key().equals(that.key()); } + public int batchId() { + return id; + } + @Override public int hashCode() { return key().hashCode(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java index 24608b70cd4..b845be0865b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java @@ -35,6 +35,11 @@ public class DefaultInputDir extends DefaultInputComponent implements InputDir { private Path moduleBaseDir; public DefaultInputDir(String moduleKey, String relativePath) { + this(moduleKey, relativePath, TestInputFileBuilder.batchId++); + } + + public DefaultInputDir(String moduleKey, String relativePath, int batchId) { + super(batchId); this.moduleKey = moduleKey; this.relativePath = PathUtils.sanitize(relativePath); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java index b6c644e9cb6..a2c97b8c954 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java @@ -42,6 +42,7 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile private Metadata metadata; public DefaultInputFile(DefaultIndexedFile indexedFile, Function<DefaultInputFile, Metadata> metadataGenerator) { + super(indexedFile.batchId()); this.indexedFile = indexedFile; this.metadataGenerator = metadataGenerator; this.metadata = null; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputModule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputModule.java index 902bd5e10e6..54f315b3427 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputModule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputModule.java @@ -19,6 +19,7 @@ */ package org.sonar.api.batch.fs.internal; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputModule; /** @@ -27,11 +28,21 @@ import org.sonar.api.batch.fs.InputModule; public class DefaultInputModule extends DefaultInputComponent implements InputModule { private final String moduleKey; + private final ProjectDefinition definition; public DefaultInputModule(String moduleKey) { - this.moduleKey = moduleKey; + this(ProjectDefinition.create().setKey(moduleKey), TestInputFileBuilder.batchId++); } + public DefaultInputModule(ProjectDefinition definition, int batchId) { + super(batchId); + this.definition = definition; + this.moduleKey = definition.getKey(); + } + + /** + * Module key without branch + */ @Override public String key() { return moduleKey; @@ -42,4 +53,8 @@ public class DefaultInputModule extends DefaultInputComponent implements InputMo return false; } + public ProjectDefinition definition() { + return definition; + } + } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/InputComponentTree.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/InputComponentTree.java new file mode 100644 index 00000000000..782a379ef72 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/InputComponentTree.java @@ -0,0 +1,30 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * 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 02110-1301, USA. + */ +package org.sonar.api.batch.fs.internal; + +import java.util.Collection; + +import org.sonar.api.batch.fs.InputComponent; + +public interface InputComponentTree { + public Collection<InputComponent> getChildren(InputComponent module); + + public InputComponent getParent(InputComponent module); +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/InputModuleHierarchy.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/InputModuleHierarchy.java new file mode 100644 index 00000000000..05e96dc19b2 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/InputModuleHierarchy.java @@ -0,0 +1,41 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * 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 02110-1301, USA. + */ +package org.sonar.api.batch.fs.internal; + +import java.util.Collection; + +import javax.annotation.CheckForNull; + +import org.sonar.api.batch.fs.InputModule; +import org.sonar.api.batch.fs.internal.DefaultInputModule; + +public interface InputModuleHierarchy { + DefaultInputModule root(); + + boolean isRoot(InputModule module); + + Collection<DefaultInputModule> children(InputModule module); + + @CheckForNull + DefaultInputModule parent(InputModule module); + + @CheckForNull + String relativePath(InputModule module); +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java index fe4bb6d74d3..5da96484421 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java @@ -28,10 +28,10 @@ import javax.annotation.Nullable; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.utils.PathUtils; -/** - * @since 4.2 - */ public class TestInputFileBuilder { + public static int batchId = 1; + + private final int id; private final String relativePath; private final String moduleKey; private Path moduleBaseDir; @@ -46,9 +46,14 @@ public class TestInputFileBuilder { private int[] originalLineOffsets; public TestInputFileBuilder(String moduleKey, String relativePath) { + this(moduleKey, relativePath, batchId++); + } + + public TestInputFileBuilder(String moduleKey, String relativePath, int id) { this.moduleKey = moduleKey; this.moduleBaseDir = Paths.get(moduleKey); this.relativePath = PathUtils.sanitize(relativePath); + this.id = id; } public TestInputFileBuilder setModuleBaseDir(Path moduleBaseDir) { @@ -115,7 +120,7 @@ public class TestInputFileBuilder { } public DefaultInputFile build() { - DefaultIndexedFile indexedFile = new DefaultIndexedFile(moduleKey, moduleBaseDir, relativePath, type); + DefaultIndexedFile indexedFile = new DefaultIndexedFile(moduleKey, moduleBaseDir, relativePath, type, id); indexedFile.setLanguage(language); DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> new Metadata(lines, nonBlankLines, hash, originalLineOffsets, lastValidOffset)); inputFile.setStatus(status); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java index 41b46e8c905..9e7ebd82422 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java @@ -22,6 +22,7 @@ package org.sonar.api.batch.sensor; import java.io.Serializable; import org.sonar.api.SonarRuntime; import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputModule; import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.sensor.coverage.NewCoverage; @@ -156,5 +157,7 @@ public interface SensorContext { * @since 6.1 */ void addContextProperty(String key, String value); + + void markForPublishing(InputFile inputFile); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java index 637d7d883c9..67e1f4f5a04 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java @@ -35,6 +35,7 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.api.SonarQubeSide; import org.sonar.api.SonarRuntime; +import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputModule; import org.sonar.api.batch.fs.TextRange; import org.sonar.api.batch.fs.internal.DefaultFileSystem; @@ -336,4 +337,9 @@ public class SensorContextTester implements SensorContext { public Map<String, String> getContextProperties() { return ImmutableMap.copyOf(sensorStorage.contextProperties); } + + @Override + public void markForPublishing(InputFile inputFile) { + + } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Library.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Library.java index 361659c7cb9..ac4846287dc 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Library.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Library.java @@ -91,7 +91,7 @@ public final class Library extends Resource { } public static Library createFromMavenIds(String groupId, String artifactId, String version) { - return new Library(String.format(Project.MAVEN_KEY_FORMAT, groupId, artifactId), version); + return new Library(String.format("%s:%s", groupId, artifactId), version); } @Override diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java index 490a0a5e6ce..f45d8c85e3b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java @@ -19,17 +19,17 @@ */ package org.sonar.api.resources; -import java.util.ArrayList; -import java.util.Date; import java.util.List; +import java.util.stream.Collectors; + import javax.annotation.CheckForNull; -import javax.annotation.Nullable; + import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.ToStringBuilder; -import org.sonar.api.CoreProperties; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputModule; import org.sonar.api.component.Component; -import org.sonar.api.config.Settings; +import org.sonar.api.scan.filesystem.PathResolver; /** * @since 1.10 @@ -37,125 +37,79 @@ import org.sonar.api.config.Settings; */ @Deprecated public class Project extends Resource implements Component { + private final ProjectDefinition definition; - /** - * Internal use - */ - public static final Language NONE_LANGUAGE = new AbstractLanguage("none", "None") { - @Override - public String[] getFileSuffixes() { - return new String[0]; - } - }; - - static final String MAVEN_KEY_FORMAT = "%s:%s"; - private static final String BRANCH_KEY_FORMAT = "%s:%s"; - - public static final String SCOPE = Scopes.PROJECT; - - private String branch; - private String name; - private String description; - private Date analysisDate; - private String analysisVersion; - private Settings settings; - private String originalName; - - // For internal use - private java.io.File baseDir; + public Project(ProjectDefinition definition) { + this.definition = definition; + this.setKey(definition.getKey()); + this.setEffectiveKey(definition.getKeyWithBranch()); + } - // modules tree - private Project parent; - private List<Project> modules = new ArrayList<>(); + public ProjectDefinition definition() { + return definition; + } - public Project(String key) { - setKey(key); - setEffectiveKey(key); + @Override + public String key() { + return definition.getKey(); } - public Project(String key, String branch, String name) { - if (StringUtils.isNotBlank(branch)) { - setKey(String.format(BRANCH_KEY_FORMAT, key, branch)); - this.name = String.format("%s %s", name, branch); - } else { - setKey(key); - this.name = name; + @Override + public String path() { + ProjectDefinition parent = definition.getParent(); + if (parent == null) { + return null; } - this.originalName = this.name; - setEffectiveKey(getKey()); - this.branch = branch; + return new PathResolver().relativePath(parent.getBaseDir(), definition.getBaseDir()); } public String getBranch() { - return branch; - } - - /** - * For internal use only. - */ - public Project setBranch(String branch) { - this.branch = branch; - return this; + return definition.getBranch(); } @CheckForNull public String getOriginalName() { - return originalName; - } - - public void setOriginalName(String originalName) { - if (StringUtils.isNotBlank(branch)) { - this.originalName = String.format("%s %s", originalName, branch); - } else { - this.originalName = originalName; + String name = definition.getOriginalName(); + if (StringUtils.isNotEmpty(getBranch())) { + name = name + " " + getBranch(); } + return name; } - @Override - public String getName() { - return name; + java.io.File getBaseDir() { + return definition.getBaseDir(); } @Override - public String getLongName() { + public String name() { + String name = definition.getName(); + if (StringUtils.isNotEmpty(getBranch())) { + name = name + " " + getBranch(); + } return name; } @Override - public String getDescription() { - return description; - } - - /** - * For internal use only. - */ - public Project setName(String name) { - this.name = name; - return this; + public String longName() { + return definition.getName(); } @Override - public Language getLanguage() { - return null; + public String qualifier() { + return getParent() == null ? Qualifiers.PROJECT : Qualifiers.MODULE; } - /** - * For internal use only. - */ - public Project setDescription(String description) { - this.description = description; - return this; + @Override + public String getName() { + return name(); } - /** - * @return whether the current project is root project - */ public boolean isRoot() { return getParent() == null; } public Project getRoot() { - return parent == null ? this : parent.getRoot(); + return getParent() == null ? this : getParent().getRoot(); } /** @@ -165,182 +119,64 @@ public class Project extends Resource implements Component { return !isRoot(); } - /** - * For internal use only. - * - * @deprecated in 3.6. It's not possible to analyze a project before the latest known quality snapshot. - * See http://jira.sonarsource.com/browse/SONAR-4334 - */ - @Deprecated - public Project setLatestAnalysis(boolean b) { - if (!b) { - throw new UnsupportedOperationException("The analysis is always the latest one. " + - "Past analysis must be done in a chronological order."); - } - return this; - } - - /** - * @return the language key or empty if no language is specified - * @deprecated since 4.2 use {@link org.sonar.api.batch.fs.FileSystem#languages()} - */ - @Deprecated - public String getLanguageKey() { - if (settings == null) { - throw new IllegalStateException("Project is not yet initialized"); - } - return StringUtils.defaultIfEmpty(settings.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY), ""); - } - - /** - * Internal use - */ - public Project setSettings(Settings settings) { - this.settings = settings; - return this; - } - - /** - * Internal use for backward compatibility. Settings should be retrieved as an IoC dependency. - * @deprecated since 5.0 - */ - @Deprecated - public Settings getSettings() { - return settings; + @Override + public String getLongName() { + return longName(); } - /** - * For internal use only. - */ - public Project setAnalysisDate(Date analysisDate) { - this.analysisDate = analysisDate; - return this; + @Override + public String getDescription() { + return definition.getDescription(); } - /** - * For internal use only. + /** + * @deprecated since 4.2 use {@link org.sonar.api.batch.fs.FileSystem#languages()} */ - public Project setAnalysisVersion(String analysisVersion) { - this.analysisVersion = analysisVersion; - return this; + @Override + public Language getLanguage() { + throw new UnsupportedOperationException(); } - /** - * @return the scope of the current object - */ @Override public String getScope() { return Scopes.PROJECT; } - /** - * @return the qualifier of the current object - */ @Override public String getQualifier() { - return isRoot() ? Qualifiers.PROJECT : Qualifiers.MODULE; - } - - @Override - public boolean matchFilePattern(String antPattern) { - return false; + return qualifier(); } - @CheckForNull @Override public Project getParent() { - return parent; - } - - /** - * For internal use only. - */ - public Project setParent(Project parent) { - this.parent = parent; - if (parent != null) { - parent.modules.add(this); - } - return this; - } - - /** - * For internal use only. - */ - public void removeFromParent() { - if (parent != null) { - parent.modules.remove(this); + ProjectDefinition parent = definition.getParent(); + if (parent == null) { + return null; } + return new Project(parent); } /** * @return the list of modules */ public List<Project> getModules() { - return modules; + return definition.getSubProjects().stream() + .map(Project::new) + .collect(Collectors.toList()); } - /** - * @return the current version of the project - */ - public String getAnalysisVersion() { - return analysisVersion; - } - - /** - * @return the analysis date, i.e. the date that will be used to store the snapshot - */ - public Date getAnalysisDate() { - return analysisDate; - } - - public static Project createFromMavenIds(String groupId, String artifactId) { - return createFromMavenIds(groupId, artifactId, null); - } - - public static Project createFromMavenIds(String groupId, String artifactId, @Nullable String branch) { - return new Project(String.format(MAVEN_KEY_FORMAT, groupId, artifactId), branch, ""); + @Override + public boolean matchFilePattern(String antPattern) { + return false; } @Override public String toString() { return new ToStringBuilder(this) .append("id", getId()) - .append("key", getKey()) + .append("key", key()) .append("qualifier", getQualifier()) .toString(); } - @Override - public String key() { - return getKey(); - } - - @Override - public String name() { - return getName(); - } - - @Override - public String path() { - return getPath(); - } - - @Override - public String longName() { - return getLongName(); - } - - @Override - public String qualifier() { - return getQualifier(); - } - - // For internal use - public void setBaseDir(java.io.File baseDir) { - this.baseDir = baseDir; - } - - java.io.File getBaseDir() { - return baseDir; - } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java index ae1aed402ce..e581552aa76 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java @@ -51,7 +51,7 @@ public class DefaultInputFileTest { Path baseDir = temp.newFolder().toPath(); Metadata metadata = new Metadata(42, 42, "", new int[0], 0); - DefaultIndexedFile indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/Foo.php", InputFile.Type.TEST).setLanguage("php"); + DefaultIndexedFile indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/Foo.php", InputFile.Type.TEST, 0).setLanguage("php"); DefaultInputFile inputFile = new DefaultInputFile(indexedFile, (f) -> metadata) .setStatus(InputFile.Status.ADDED) .setCharset(StandardCharsets.ISO_8859_1); @@ -75,7 +75,7 @@ public class DefaultInputFileTest { Files.write(testFile, "test string".getBytes(StandardCharsets.UTF_8)); Metadata metadata = new Metadata(42, 30, "", new int[0], 0); - DefaultInputFile inputFile = new DefaultInputFile(new DefaultIndexedFile("ABCDE", baseDir, "src/Foo.php", InputFile.Type.TEST) + DefaultInputFile inputFile = new DefaultInputFile(new DefaultIndexedFile("ABCDE", baseDir, "src/Foo.php", InputFile.Type.TEST, 0) .setLanguage("php"), f -> metadata) .setStatus(InputFile.Status.ADDED) .setCharset(StandardCharsets.ISO_8859_1); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java index 7805c2ec453..78a81e2be04 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java @@ -20,41 +20,40 @@ package org.sonar.api.resources; import org.junit.Test; +import org.sonar.api.CoreProperties; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import static org.assertj.core.api.Assertions.assertThat; public class ProjectTest { - @Test - public void effectiveKeyShouldEqualKey() { - assertThat(new Project("my:project").getEffectiveKey()).isEqualTo("my:project"); - } + public void effectiveKeyShouldEqualKeyWithBranch() { - @Test - public void createFromMavenIds() { - Project project = Project.createFromMavenIds("my", "artifact"); - - assertThat(project.getKey()).isEqualTo("my:artifact"); + ProjectDefinition definition = ProjectDefinition.create() + .setKey("mykey") + .setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, "branch"); + assertThat(new Project(definition).getEffectiveKey()).isEqualTo("mykey:branch"); + assertThat(new Project(definition).getKey()).isEqualTo("mykey"); } - + @Test public void setNameWithBranch() { - Project project = new Project("key", "branch", "name"); + ProjectDefinition definition = ProjectDefinition.create() + .setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, "branch") + .setKey("key") + .setName("name"); + Project project = new Project(definition); assertThat(project.getName()).isEqualTo("name branch"); assertThat(project.getOriginalName()).isEqualTo("name branch"); - - project.setOriginalName("Project1"); - assertThat(project.getOriginalName()).isEqualTo("Project1 branch"); } - + @Test public void setNameWithoutBranch() { - Project project = new Project("key", null, "name"); + ProjectDefinition definition = ProjectDefinition.create() + .setKey("key") + .setName("name"); + Project project = new Project(definition); assertThat(project.getName()).isEqualTo("name"); assertThat(project.getOriginalName()).isEqualTo("name"); - - project.setOriginalName("Project1"); - assertThat(project.getOriginalName()).isEqualTo("Project1"); } - } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/QualifiersTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/QualifiersTest.java index 36b82b87df5..acfc2ed17bf 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/QualifiersTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/QualifiersTest.java @@ -20,6 +20,7 @@ package org.sonar.api.resources; import org.junit.Test; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -46,7 +47,10 @@ public class QualifiersTest { @Test public void testProject() { - Resource root = new Project("foo"); + ProjectDefinition rootDef = ProjectDefinition.create(); + ProjectDefinition moduleDef = ProjectDefinition.create(); + rootDef.addSubProject(moduleDef); + Resource root = new Project(rootDef); assertThat(Qualifiers.isView(root, true), is(false)); assertThat(Qualifiers.isView(root, false), is(false)); assertThat(Qualifiers.isProject(root, true), is(true)); @@ -55,7 +59,10 @@ public class QualifiersTest { @Test public void testModule() { - Resource sub = new Project("sub").setParent(new Project("root")); + ProjectDefinition rootDef = ProjectDefinition.create(); + ProjectDefinition moduleDef = ProjectDefinition.create(); + rootDef.addSubProject(moduleDef); + Resource sub = new Project(moduleDef); assertThat(Qualifiers.isView(sub, true), is(false)); assertThat(Qualifiers.isView(sub, false), is(false)); assertThat(Qualifiers.isProject(sub, true), is(true)); @@ -119,5 +126,3 @@ public class QualifiersTest { } } } - - diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceUtilsTest.java index 03d2c618033..edb7799407b 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceUtilsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceUtilsTest.java @@ -65,7 +65,6 @@ public class ResourceUtilsTest { public void shouldBePersistable() { assertThat(ResourceUtils.isPersistable(File.create("Foo.java"))).isTrue(); assertThat(ResourceUtils.isPersistable(Directory.create("bar/Foo.java"))).isTrue(); - assertThat(ResourceUtils.isPersistable(new Project("foo"))).isTrue(); } @Test diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java index cabe8785520..d13918df974 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java @@ -20,6 +20,7 @@ package org.sonar.api.resources; import org.junit.Test; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -28,7 +29,7 @@ public class ScopesTest { @Test public void testProject() { - Project resource = new Project("key"); + Project resource = new Project(ProjectDefinition.create()); assertThat(Scopes.isProject(resource), is(true)); assertThat(Scopes.isDirectory(resource), is(false)); assertThat(Scopes.isFile(resource), is(false)); |