diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2019-06-05 11:01:49 -0500 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-07-12 20:21:14 +0200 |
commit | 97e15208790028ed50187e58cd4580e6cef8e6b3 (patch) | |
tree | b6232897a8e69eb219b0479599b29962a804338b /sonar-plugin-api | |
parent | e020f1425d815ae48e9b5c39bc0eaf92382c3c06 (diff) | |
download | sonarqube-97e15208790028ed50187e58cd4580e6cef8e6b3.tar.gz sonarqube-97e15208790028ed50187e58cd4580e6cef8e6b3.zip |
Extract implementation from plugin API - Scanner FS
Diffstat (limited to 'sonar-plugin-api')
33 files changed, 14 insertions, 3116 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/AbstractProjectOrModule.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/AbstractProjectOrModule.java deleted file mode 100644 index 2f45fb0f488..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/AbstractProjectOrModule.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.LinkOption; -import java.nio.file.Path; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import javax.annotation.CheckForNull; -import javax.annotation.concurrent.Immutable; -import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang.SystemUtils; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; - -@Immutable -public abstract class AbstractProjectOrModule extends DefaultInputComponent { - private static final Logger LOGGER = Loggers.get(AbstractProjectOrModule.class); - private final Path baseDir; - private final Path workDir; - private final String name; - private final String originalName; - private final String description; - private final String keyWithBranch; - private final String branch; - private final Map<String, String> properties; - - private final String key; - private final ProjectDefinition definition; - private final Charset encoding; - - public AbstractProjectOrModule(ProjectDefinition definition, int scannerComponentId) { - super(scannerComponentId); - this.baseDir = initBaseDir(definition); - this.workDir = initWorkingDir(definition); - this.name = definition.getName(); - this.originalName = definition.getOriginalName(); - this.description = definition.getDescription(); - this.keyWithBranch = definition.getKeyWithBranch(); - this.branch = definition.getBranch(); - this.properties = Collections.unmodifiableMap(new HashMap<>(definition.properties())); - - this.definition = definition; - this.key = definition.getKey(); - this.encoding = initEncoding(definition); - } - - private static Charset initEncoding(ProjectDefinition module) { - String encodingStr = module.properties().get(CoreProperties.ENCODING_PROPERTY); - Charset result; - if (StringUtils.isNotEmpty(encodingStr)) { - result = Charset.forName(StringUtils.trim(encodingStr)); - } else { - result = Charset.defaultCharset(); - } - return result; - } - - private static Path initBaseDir(ProjectDefinition module) { - Path result; - try { - result = module.getBaseDir().toPath().toRealPath(LinkOption.NOFOLLOW_LINKS); - } catch (IOException e) { - throw new IllegalStateException("Unable to resolve module baseDir", e); - } - return result; - } - - private static Path initWorkingDir(ProjectDefinition module) { - File workingDirAsFile = module.getWorkDir(); - Path workingDir = workingDirAsFile.getAbsoluteFile().toPath().normalize(); - if (SystemUtils.IS_OS_WINDOWS) { - try { - Files.createDirectories(workingDir); - Files.setAttribute(workingDir, "dos:hidden", true, LinkOption.NOFOLLOW_LINKS); - } catch (IOException e) { - LOGGER.warn("Failed to set working directory hidden: {}", e.getMessage()); - } - } - return workingDir; - } - - /** - * Module key without branch - */ - @Override - public String key() { - return key; - } - - @Override - public boolean isFile() { - return false; - } - - public ProjectDefinition definition() { - return definition; - } - - public Path getBaseDir() { - return baseDir; - } - - public Path getWorkDir() { - return workDir; - } - - public String getKeyWithBranch() { - return keyWithBranch; - } - - @CheckForNull - public String getBranch() { - return branch; - } - - public Map<String, String> properties() { - return properties; - } - - @CheckForNull - public String getOriginalName() { - return originalName; - } - - public String getName() { - return name; - } - - public String getDescription() { - return description; - } - - public Charset getEncoding() { - return encoding; - } -} 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 deleted file mode 100644 index 229280fca5b..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.concurrent.atomic.AtomicInteger; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; -import org.sonar.api.batch.fs.IndexedFile; -import org.sonar.api.batch.fs.InputFile.Type; -import org.sonar.api.utils.PathUtils; - -/** - * @since 6.3 - */ -@Immutable -public class DefaultIndexedFile extends DefaultInputComponent implements IndexedFile { - private static AtomicInteger intGenerator = new AtomicInteger(0); - - private final String projectRelativePath; - private final String moduleRelativePath; - private final String projectKey; - private final String language; - private final Type type; - private final Path absolutePath; - private final SensorStrategy sensorStrategy; - - /** - * Testing purposes only! - */ - public DefaultIndexedFile(String projectKey, Path baseDir, String relativePath, @Nullable String language) { - this(baseDir.resolve(relativePath), projectKey, relativePath, relativePath, Type.MAIN, language, intGenerator.getAndIncrement(), - new SensorStrategy()); - } - - public DefaultIndexedFile(Path absolutePath, String projectKey, String projectRelativePath, String moduleRelativePath, Type type, @Nullable String language, int batchId, - SensorStrategy sensorStrategy) { - super(batchId); - this.projectKey = projectKey; - this.projectRelativePath = PathUtils.sanitize(projectRelativePath); - this.moduleRelativePath = PathUtils.sanitize(moduleRelativePath); - this.type = type; - this.language = language; - this.sensorStrategy = sensorStrategy; - this.absolutePath = absolutePath; - } - - @Override - public String relativePath() { - return sensorStrategy.isGlobal() ? projectRelativePath : moduleRelativePath; - } - - public String getModuleRelativePath() { - return moduleRelativePath; - } - - public String getProjectRelativePath() { - return projectRelativePath; - } - - @Override - public String absolutePath() { - return PathUtils.sanitize(path().toString()); - } - - @Override - public File file() { - return path().toFile(); - } - - @Override - public Path path() { - return absolutePath; - } - - @Override - public InputStream inputStream() throws IOException { - return Files.newInputStream(path()); - } - - @CheckForNull - @Override - public String language() { - return language; - } - - @Override - public Type type() { - return type; - } - - /** - * Component key (without branch). - */ - @Override - public String key() { - return new StringBuilder().append(projectKey).append(":").append(projectRelativePath).toString(); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - - if (!(o instanceof DefaultIndexedFile)) { - return false; - } - - DefaultIndexedFile that = (DefaultIndexedFile) o; - return projectRelativePath.equals(that.projectRelativePath); - } - - @Override - public int hashCode() { - return projectRelativePath.hashCode(); - } - - @Override - public String toString() { - return projectRelativePath; - } - - @Override - public boolean isFile() { - return true; - } - - @Override - public String filename() { - return path().getFileName().toString(); - } - - @Override - public URI uri() { - return path().toUri(); - } -} 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 deleted file mode 100644 index 19a5cce029b..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputComponent.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.HashSet; -import java.util.Set; -import org.sonar.api.batch.fs.InputComponent; -import org.sonar.api.batch.measure.Metric; - -/** - * @since 5.2 - */ -public abstract class DefaultInputComponent implements InputComponent { - private int id; - private Set<String> storedMetricKeys = new HashSet<>(); - - public DefaultInputComponent(int scannerId) { - this.id = scannerId; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || this.getClass() != o.getClass()) { - return false; - } - - DefaultInputComponent that = (DefaultInputComponent) o; - return key().equals(that.key()); - } - - public int scannerId() { - return id; - } - - @Override - public int hashCode() { - return key().hashCode(); - } - - @Override - public String toString() { - return "[key=" + key() + "]"; - } - - public void setHasMeasureFor(Metric metric) { - storedMetricKeys.add(metric.key()); - } - - public boolean hasMeasureFor(Metric metric) { - return storedMetricKeys.contains(metric.key()); - } -} 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 deleted file mode 100644 index 14a39ee1b32..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.io.File; -import java.net.URI; -import java.nio.file.Path; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.batch.fs.InputDir; -import org.sonar.api.utils.PathUtils; - -/** - * @since 4.5 - */ -public class DefaultInputDir extends DefaultInputComponent implements InputDir { - - private final String relativePath; - private final String moduleKey; - private Path moduleBaseDir; - - public DefaultInputDir(String moduleKey, String relativePath) { - super(-1); - this.moduleKey = moduleKey; - this.relativePath = PathUtils.sanitize(relativePath); - } - - @Override - public String relativePath() { - return relativePath; - } - - @Override - public String absolutePath() { - return PathUtils.sanitize(path().toString()); - } - - @Override - public File file() { - return path().toFile(); - } - - @Override - public Path path() { - if (moduleBaseDir == null) { - throw new IllegalStateException("Can not return the java.nio.file.Path because module baseDir is not set (see method setModuleBaseDir(java.io.File))"); - } - return moduleBaseDir.resolve(relativePath); - } - - public String moduleKey() { - return moduleKey; - } - - @Override - public String key() { - StringBuilder sb = new StringBuilder().append(moduleKey).append(":"); - if (StringUtils.isEmpty(relativePath)) { - sb.append("/"); - } else { - sb.append(relativePath); - } - return sb.toString(); - } - - /** - * For testing purpose. Will be automatically set when dir is added to {@link DefaultFileSystem} - */ - public DefaultInputDir setModuleBaseDir(Path moduleBaseDir) { - this.moduleBaseDir = moduleBaseDir.normalize(); - return this; - } - - @Override - public boolean isFile() { - return false; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || this.getClass() != o.getClass()) { - return false; - } - - DefaultInputDir that = (DefaultInputDir) o; - return moduleKey.equals(that.moduleKey) && relativePath.equals(that.relativePath); - } - - @Override - public int hashCode() { - return moduleKey.hashCode() + relativePath.hashCode() * 13; - } - - @Override - public String toString() { - return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]"; - } - - @Override - public URI uri() { - return path().toUri(); - } -} 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 deleted file mode 100644 index 19381e4c7ea..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java +++ /dev/null @@ -1,439 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.BitSet; -import java.util.Collection; -import java.util.Optional; -import java.util.Set; -import java.util.function.Consumer; -import java.util.stream.Collectors; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.apache.commons.io.ByteOrderMark; -import org.apache.commons.io.input.BOMInputStream; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextPointer; -import org.sonar.api.batch.fs.TextRange; - -import static org.sonar.api.utils.Preconditions.checkArgument; -import static org.sonar.api.utils.Preconditions.checkState; - -/** - * @since 4.2 - * To create {@link InputFile} in tests, use TestInputFileBuilder. - */ -public class DefaultInputFile extends DefaultInputComponent implements InputFile { - - private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; - - private final DefaultIndexedFile indexedFile; - private final String contents; - private final Consumer<DefaultInputFile> metadataGenerator; - - private boolean published; - private boolean excludedForCoverage; - private boolean excludedForDuplication; - private boolean ignoreAllIssues; - // Lazy init to save memory - private BitSet noSonarLines; - private Status status; - private Charset charset; - private Metadata metadata; - private Collection<int[]> ignoreIssuesOnlineRanges; - private BitSet executableLines; - - public DefaultInputFile(DefaultIndexedFile indexedFile, Consumer<DefaultInputFile> metadataGenerator) { - this(indexedFile, metadataGenerator, null); - } - - // For testing - public DefaultInputFile(DefaultIndexedFile indexedFile, Consumer<DefaultInputFile> metadataGenerator, @Nullable String contents) { - super(indexedFile.scannerId()); - this.indexedFile = indexedFile; - this.metadataGenerator = metadataGenerator; - this.metadata = null; - this.published = false; - this.excludedForCoverage = false; - this.contents = contents; - } - - public void checkMetadata() { - if (metadata == null) { - metadataGenerator.accept(this); - } - } - - @Override - public InputStream inputStream() throws IOException { - return contents != null ? new ByteArrayInputStream(contents.getBytes(charset())) - : new BOMInputStream(Files.newInputStream(path()), - ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE); - } - - @Override - public String contents() throws IOException { - if (contents != null) { - return contents; - } else { - ByteArrayOutputStream result = new ByteArrayOutputStream(); - try (InputStream inputStream = inputStream()) { - byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; - int length; - while ((length = inputStream.read(buffer)) != -1) { - result.write(buffer, 0, length); - } - } - return result.toString(charset().name()); - } - } - - public DefaultInputFile setPublished(boolean published) { - this.published = published; - return this; - } - - public boolean isPublished() { - return published; - } - - public DefaultInputFile setExcludedForCoverage(boolean excludedForCoverage) { - this.excludedForCoverage = excludedForCoverage; - return this; - } - - public boolean isExcludedForCoverage() { - return excludedForCoverage; - } - - public DefaultInputFile setExcludedForDuplication(boolean excludedForDuplication) { - this.excludedForDuplication = excludedForDuplication; - return this; - } - - public boolean isExcludedForDuplication() { - return excludedForDuplication; - } - - /** - * @deprecated since 6.6 - */ - @Deprecated - @Override - public String relativePath() { - return indexedFile.relativePath(); - } - - public String getModuleRelativePath() { - return indexedFile.getModuleRelativePath(); - } - - public String getProjectRelativePath() { - return indexedFile.getProjectRelativePath(); - } - - @Override - public String absolutePath() { - return indexedFile.absolutePath(); - } - - @Override - public File file() { - return indexedFile.file(); - } - - @Override - public Path path() { - return indexedFile.path(); - } - - @CheckForNull - @Override - public String language() { - return indexedFile.language(); - } - - @Override - public Type type() { - return indexedFile.type(); - } - - /** - * Component key (without branch). - */ - @Override - public String key() { - return indexedFile.key(); - } - - @Override - public int hashCode() { - return indexedFile.hashCode(); - } - - @Override - public String toString() { - return indexedFile.toString(); - } - - /** - * {@link #setStatus(org.sonar.api.batch.fs.InputFile.Status)} - */ - @Override - public Status status() { - checkMetadata(); - return status; - } - - @Override - public int lines() { - checkMetadata(); - return metadata.lines(); - } - - @Override - public boolean isEmpty() { - checkMetadata(); - return metadata.isEmpty(); - } - - @Override - public Charset charset() { - checkMetadata(); - return charset; - } - - public int lastValidOffset() { - checkMetadata(); - return metadata.lastValidOffset(); - } - - /** - * Digest hash of the file. - */ - public String hash() { - checkMetadata(); - return metadata.hash(); - } - - public int nonBlankLines() { - checkMetadata(); - return metadata.nonBlankLines(); - } - - public int[] originalLineStartOffsets() { - checkMetadata(); - checkState(metadata.originalLineStartOffsets() != null, "InputFile is not properly initialized."); - checkState(metadata.originalLineStartOffsets().length == metadata.lines(), - "InputFile is not properly initialized. 'originalLineStartOffsets' property length should be equal to 'lines'"); - return metadata.originalLineStartOffsets(); - } - - public int[] originalLineEndOffsets() { - checkMetadata(); - checkState(metadata.originalLineEndOffsets() != null, "InputFile is not properly initialized."); - checkState(metadata.originalLineEndOffsets().length == metadata.lines(), - "InputFile is not properly initialized. 'originalLineEndOffsets' property length should be equal to 'lines'"); - return metadata.originalLineEndOffsets(); - } - - @Override - public TextPointer newPointer(int line, int lineOffset) { - checkMetadata(); - DefaultTextPointer textPointer = new DefaultTextPointer(line, lineOffset); - checkValid(textPointer, "pointer"); - return textPointer; - } - - @Override - public TextRange newRange(TextPointer start, TextPointer end) { - checkMetadata(); - checkValid(start, "start pointer"); - checkValid(end, "end pointer"); - return newRangeValidPointers(start, end, false); - } - - @Override - public TextRange newRange(int startLine, int startLineOffset, int endLine, int endLineOffset) { - checkMetadata(); - TextPointer start = newPointer(startLine, startLineOffset); - TextPointer end = newPointer(endLine, endLineOffset); - return newRangeValidPointers(start, end, false); - } - - @Override - public TextRange selectLine(int line) { - checkMetadata(); - TextPointer startPointer = newPointer(line, 0); - TextPointer endPointer = newPointer(line, lineLength(line)); - return newRangeValidPointers(startPointer, endPointer, true); - } - - public void validate(TextRange range) { - checkMetadata(); - checkValid(range.start(), "start pointer"); - checkValid(range.end(), "end pointer"); - } - - /** - * Create Range from global offsets. Used for backward compatibility with older API. - */ - public TextRange newRange(int startOffset, int endOffset) { - checkMetadata(); - return newRangeValidPointers(newPointer(startOffset), newPointer(endOffset), false); - } - - public TextPointer newPointer(int globalOffset) { - checkMetadata(); - checkArgument(globalOffset >= 0, "%s is not a valid offset for a file", globalOffset); - checkArgument(globalOffset <= lastValidOffset(), "%s is not a valid offset for file %s. Max offset is %s", globalOffset, this, lastValidOffset()); - int line = findLine(globalOffset); - int startLineOffset = originalLineStartOffsets()[line - 1]; - // In case the global offset is between \r and \n, move the pointer to a valid location - return new DefaultTextPointer(line, Math.min(globalOffset, originalLineEndOffsets()[line - 1]) - startLineOffset); - } - - public DefaultInputFile setStatus(Status status) { - this.status = status; - return this; - } - - public DefaultInputFile setCharset(Charset charset) { - this.charset = charset; - return this; - } - - private void checkValid(TextPointer pointer, String owner) { - checkArgument(pointer.line() >= 1, "%s is not a valid line for a file", pointer.line()); - checkArgument(pointer.line() <= this.metadata.lines(), "%s is not a valid line for %s. File %s has %s line(s)", pointer.line(), owner, this, metadata.lines()); - checkArgument(pointer.lineOffset() >= 0, "%s is not a valid line offset for a file", pointer.lineOffset()); - int lineLength = lineLength(pointer.line()); - checkArgument(pointer.lineOffset() <= lineLength, - "%s is not a valid line offset for %s. File %s has %s character(s) at line %s", pointer.lineOffset(), owner, this, lineLength, pointer.line()); - } - - private int lineLength(int line) { - return originalLineEndOffsets()[line - 1] - originalLineStartOffsets()[line - 1]; - } - - private static TextRange newRangeValidPointers(TextPointer start, TextPointer end, boolean acceptEmptyRange) { - checkArgument(acceptEmptyRange ? (start.compareTo(end) <= 0) : (start.compareTo(end) < 0), - "Start pointer %s should be before end pointer %s", start, end); - return new DefaultTextRange(start, end); - } - - private int findLine(int globalOffset) { - return Math.abs(Arrays.binarySearch(originalLineStartOffsets(), globalOffset) + 1); - } - - public DefaultInputFile setMetadata(Metadata metadata) { - this.metadata = metadata; - return this; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - - if (this.getClass() != obj.getClass()) { - return false; - } - - DefaultInputFile that = (DefaultInputFile) obj; - return this.getProjectRelativePath().equals(that.getProjectRelativePath()); - } - - @Override - public boolean isFile() { - return true; - } - - @Override - public String filename() { - return indexedFile.filename(); - } - - @Override - public URI uri() { - return indexedFile.uri(); - } - - public void noSonarAt(Set<Integer> noSonarLines) { - if (this.noSonarLines == null) { - this.noSonarLines = new BitSet(lines()); - } - noSonarLines.forEach(l -> this.noSonarLines.set(l - 1)); - } - - public boolean hasNoSonarAt(int line) { - if (this.noSonarLines == null) { - return false; - } - return this.noSonarLines.get(line - 1); - } - - public boolean isIgnoreAllIssues() { - return ignoreAllIssues; - } - - public void setIgnoreAllIssues(boolean ignoreAllIssues) { - this.ignoreAllIssues = ignoreAllIssues; - } - - public void addIgnoreIssuesOnLineRanges(Collection<int[]> lineRanges) { - if (this.ignoreIssuesOnlineRanges == null) { - this.ignoreIssuesOnlineRanges = new ArrayList<>(); - } - this.ignoreIssuesOnlineRanges.addAll(lineRanges); - } - - public boolean isIgnoreAllIssuesOnLine(@Nullable Integer line) { - if (line == null || ignoreIssuesOnlineRanges == null) { - return false; - } - return ignoreIssuesOnlineRanges.stream().anyMatch(r -> r[0] <= line && line <= r[1]); - } - - public void setExecutableLines(Set<Integer> executableLines) { - checkState(this.executableLines == null, "Executable lines have already been saved for file: {}", this.toString()); - this.executableLines = new BitSet(lines()); - executableLines.forEach(l -> this.executableLines.set(l - 1)); - } - - public Optional<Set<Integer>> getExecutableLines() { - if (this.executableLines == null) { - return Optional.empty(); - } - return Optional.of(this.executableLines.stream().map(i -> i + 1).boxed().collect(Collectors.toSet())); - } -} 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 deleted file mode 100644 index 73fa8dd7629..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputModule.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.io.File; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import javax.annotation.CheckForNull; -import javax.annotation.concurrent.Immutable; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.fs.InputModule; -import org.sonar.api.scan.filesystem.PathResolver; - -import static org.sonar.api.config.internal.MultivalueProperty.parseAsCsv; - -@Immutable -public class DefaultInputModule extends AbstractProjectOrModule implements InputModule { - - private final List<Path> sourceDirsOrFiles; - private final List<Path> testDirsOrFiles; - - /** - * For testing only! - */ - public DefaultInputModule(ProjectDefinition definition) { - this(definition, 0); - } - - public DefaultInputModule(ProjectDefinition definition, int scannerComponentId) { - super(definition, scannerComponentId); - - this.sourceDirsOrFiles = initSources(definition, ProjectDefinition.SOURCES_PROPERTY); - this.testDirsOrFiles = initSources(definition, ProjectDefinition.TESTS_PROPERTY); - } - - @CheckForNull - private List<Path> initSources(ProjectDefinition module, String propertyKey) { - if (!module.properties().containsKey(propertyKey)) { - return null; - } - List<Path> result = new ArrayList<>(); - PathResolver pathResolver = new PathResolver(); - String srcPropValue = module.properties().get(propertyKey); - if (srcPropValue != null) { - for (String sourcePath : parseAsCsv(propertyKey, srcPropValue)) { - File dirOrFile = pathResolver.relativeFile(getBaseDir().toFile(), sourcePath); - if (dirOrFile.exists()) { - result.add(dirOrFile.toPath()); - } - } - } - return result; - } - - public Optional<List<Path>> getSourceDirsOrFiles() { - return Optional.ofNullable(sourceDirsOrFiles); - } - - public Optional<List<Path>> getTestDirsOrFiles() { - return Optional.ofNullable(testDirsOrFiles); - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputProject.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputProject.java deleted file mode 100644 index b6f07c852f2..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputProject.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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 javax.annotation.concurrent.Immutable; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.scanner.fs.InputProject; - -@Immutable -public class DefaultInputProject extends AbstractProjectOrModule implements InputProject { - - /** - * For testing only! - */ - public DefaultInputProject(ProjectDefinition definition) { - super(definition, 0); - } - - public DefaultInputProject(ProjectDefinition definition, int scannerComponentId) { - super(definition, scannerComponentId); - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultTextPointer.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultTextPointer.java deleted file mode 100644 index c10efd01de7..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultTextPointer.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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 org.sonar.api.batch.fs.TextPointer; - -/** - * @since 5.2 - */ -public class DefaultTextPointer implements TextPointer { - - private final int line; - private final int lineOffset; - - public DefaultTextPointer(int line, int lineOffset) { - this.line = line; - this.lineOffset = lineOffset; - } - - @Override - public int line() { - return line; - } - - @Override - public int lineOffset() { - return lineOffset; - } - - @Override - public String toString() { - return "[line=" + line + ", lineOffset=" + lineOffset + "]"; - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof DefaultTextPointer)) { - return false; - } - DefaultTextPointer other = (DefaultTextPointer) obj; - return other.line == this.line && other.lineOffset == this.lineOffset; - } - - @Override - public int hashCode() { - return 37 * this.line + lineOffset; - } - - @Override - public int compareTo(TextPointer o) { - if (this.line == o.line()) { - return Integer.compare(this.lineOffset, o.lineOffset()); - } - return Integer.compare(this.line, o.line()); - } - -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultTextRange.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultTextRange.java deleted file mode 100644 index efef79cf367..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultTextRange.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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 org.sonar.api.batch.fs.TextPointer; -import org.sonar.api.batch.fs.TextRange; - -/** - * @since 5.2 - */ -public class DefaultTextRange implements TextRange { - - private final TextPointer start; - private final TextPointer end; - - public DefaultTextRange(TextPointer start, TextPointer end) { - this.start = start; - this.end = end; - } - - @Override - public TextPointer start() { - return start; - } - - @Override - public TextPointer end() { - return end; - } - - @Override - public boolean overlap(TextRange another) { - // [A,B] and [C,D] - // B > C && D > A - return this.end.compareTo(another.start()) > 0 && another.end().compareTo(this.start) > 0; - } - - @Override - public String toString() { - return "Range[from " + start + " to " + end + "]"; - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof DefaultTextRange)) { - return false; - } - DefaultTextRange other = (DefaultTextRange) obj; - return start.equals(other.start) && end.equals(other.end); - } - - @Override - public int hashCode() { - return start.hashCode() * 17 + end.hashCode(); - } - -} 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 deleted file mode 100644 index b3304cb4a34..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/InputModuleHierarchy.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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 javax.annotation.concurrent.Immutable; - -@Immutable -public interface InputModuleHierarchy { - DefaultInputModule root(); - - boolean isRoot(DefaultInputModule module); - - Collection<DefaultInputModule> children(DefaultInputModule module); - - @CheckForNull - DefaultInputModule parent(DefaultInputModule module); - - @CheckForNull - String relativePath(DefaultInputModule module); - - @CheckForNull - String relativePathToRoot(DefaultInputModule module); -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/code/internal/DefaultSignificantCode.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/code/internal/DefaultSignificantCode.java deleted file mode 100644 index 40ea16bd42b..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/code/internal/DefaultSignificantCode.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.sensor.code.internal; - -import java.util.SortedMap; -import java.util.TreeMap; -import javax.annotation.Nullable; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.sensor.code.NewSignificantCode; -import org.sonar.api.batch.sensor.internal.DefaultStorable; -import org.sonar.api.batch.sensor.internal.SensorStorage; -import org.sonar.api.utils.Preconditions; - -public class DefaultSignificantCode extends DefaultStorable implements NewSignificantCode { - private SortedMap<Integer, TextRange> significantCodePerLine = new TreeMap<>(); - private InputFile inputFile; - - public DefaultSignificantCode() { - super(); - } - - public DefaultSignificantCode(@Nullable SensorStorage storage) { - super(storage); - } - - @Override - public DefaultSignificantCode onFile(InputFile inputFile) { - this.inputFile = inputFile; - return this; - } - - @Override - public DefaultSignificantCode addRange(TextRange range) { - Preconditions.checkState(this.inputFile != null, "addRange() should be called after on()"); - - int line = range.start().line(); - - Preconditions.checkArgument(line == range.end().line(), "Ranges of significant code must be located in a single line"); - Preconditions.checkState(!significantCodePerLine.containsKey(line), "Significant code was already reported for line '%s'. Can only report once per line.", line); - - significantCodePerLine.put(line, range); - return this; - } - - @Override - protected void doSave() { - Preconditions.checkState(inputFile != null, "Call onFile() first"); - storage.store(this); - } - - public InputFile inputFile() { - return inputFile; - } - - public SortedMap<Integer, TextRange> significantCodePerLine() { - return significantCodePerLine; - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/code/internal/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/code/internal/package-info.java deleted file mode 100644 index 95f388572a9..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/code/internal/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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. - */ -@javax.annotation.ParametersAreNonnullByDefault -package org.sonar.api.batch.sensor.code.internal; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/coverage/internal/DefaultCoverage.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/coverage/internal/DefaultCoverage.java deleted file mode 100644 index 94fc1887361..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/coverage/internal/DefaultCoverage.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.sensor.coverage.internal; - -import java.util.Collections; -import java.util.SortedMap; -import java.util.TreeMap; -import javax.annotation.Nullable; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.sensor.coverage.CoverageType; -import org.sonar.api.batch.sensor.coverage.NewCoverage; -import org.sonar.api.batch.sensor.internal.DefaultStorable; -import org.sonar.api.batch.sensor.internal.SensorStorage; - -import static java.util.Objects.requireNonNull; -import static org.sonar.api.utils.Preconditions.checkState; - -public class DefaultCoverage extends DefaultStorable implements NewCoverage { - - private InputFile inputFile; - private CoverageType type; - private int totalCoveredLines = 0; - private int totalConditions = 0; - private int totalCoveredConditions = 0; - private SortedMap<Integer, Integer> hitsByLine = new TreeMap<>(); - private SortedMap<Integer, Integer> conditionsByLine = new TreeMap<>(); - private SortedMap<Integer, Integer> coveredConditionsByLine = new TreeMap<>(); - - public DefaultCoverage() { - super(); - } - - public DefaultCoverage(@Nullable SensorStorage storage) { - super(storage); - } - - @Override - public DefaultCoverage onFile(InputFile inputFile) { - this.inputFile = inputFile; - return this; - } - - public InputFile inputFile() { - return inputFile; - } - - @Override - public NewCoverage ofType(CoverageType type) { - this.type = requireNonNull(type, "type can't be null"); - return this; - } - - public CoverageType type() { - return type; - } - - @Override - public NewCoverage lineHits(int line, int hits) { - validateFile(); - if (isExcluded()) { - return this; - } - validateLine(line); - - if (!hitsByLine.containsKey(line)) { - hitsByLine.put(line, hits); - if (hits > 0) { - totalCoveredLines += 1; - } - } - return this; - } - - private void validateLine(int line) { - checkState(line <= inputFile.lines(), "Line %s is out of range in the file %s (lines: %s)", line, inputFile, inputFile.lines()); - checkState(line > 0, "Line number must be strictly positive: %s", line); - } - - private void validateFile() { - requireNonNull(inputFile, "Call onFile() first"); - } - - @Override - public NewCoverage conditions(int line, int conditions, int coveredConditions) { - validateFile(); - if (isExcluded()) { - return this; - } - validateLine(line); - - if (conditions > 0 && !conditionsByLine.containsKey(line)) { - totalConditions += conditions; - totalCoveredConditions += coveredConditions; - conditionsByLine.put(line, conditions); - coveredConditionsByLine.put(line, coveredConditions); - } - return this; - } - - public int coveredLines() { - return totalCoveredLines; - } - - public int linesToCover() { - return hitsByLine.size(); - } - - public int conditions() { - return totalConditions; - } - - public int coveredConditions() { - return totalCoveredConditions; - } - - public SortedMap<Integer, Integer> hitsByLine() { - return Collections.unmodifiableSortedMap(hitsByLine); - } - - public SortedMap<Integer, Integer> conditionsByLine() { - return Collections.unmodifiableSortedMap(conditionsByLine); - } - - public SortedMap<Integer, Integer> coveredConditionsByLine() { - return Collections.unmodifiableSortedMap(coveredConditionsByLine); - } - - @Override - public void doSave() { - validateFile(); - if (!isExcluded()) { - storage.store(this); - } - } - - private boolean isExcluded() { - return ((DefaultInputFile) inputFile).isExcludedForCoverage(); - } - -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/coverage/internal/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/coverage/internal/package-info.java deleted file mode 100644 index a0b5e87657d..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/coverage/internal/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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. - */ -@javax.annotation.ParametersAreNonnullByDefault -package org.sonar.api.batch.sensor.coverage.internal; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokens.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokens.java deleted file mode 100644 index a2c592c37b5..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokens.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.sensor.cpd.internal; - -import java.util.ArrayList; -import java.util.List; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.sensor.cpd.NewCpdTokens; -import org.sonar.api.batch.sensor.internal.DefaultStorable; -import org.sonar.api.batch.sensor.internal.SensorStorage; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; - -import static java.util.Collections.unmodifiableList; -import static java.util.Objects.requireNonNull; -import static org.sonar.api.utils.Preconditions.checkState; - -public class DefaultCpdTokens extends DefaultStorable implements NewCpdTokens { - private static final Logger LOG = Loggers.get(DefaultCpdTokens.class); - private final List<TokensLine> result = new ArrayList<>(); - private DefaultInputFile inputFile; - private int startLine = Integer.MIN_VALUE; - private int startIndex = 0; - private int currentIndex = 0; - private StringBuilder sb = new StringBuilder(); - private TextRange lastRange; - private boolean loggedTestCpdWarning = false; - - public DefaultCpdTokens(SensorStorage storage) { - super(storage); - } - - @Override - public DefaultCpdTokens onFile(InputFile inputFile) { - this.inputFile = (DefaultInputFile) requireNonNull(inputFile, "file can't be null"); - return this; - } - - public InputFile inputFile() { - return inputFile; - } - - @Override - public NewCpdTokens addToken(int startLine, int startLineOffset, int endLine, int endLineOffset, String image) { - checkInputFileNotNull(); - TextRange newRange; - try { - newRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset); - } catch (Exception e) { - throw new IllegalArgumentException("Unable to register token in file " + inputFile, e); - } - return addToken(newRange, image); - } - - @Override - public DefaultCpdTokens addToken(TextRange range, String image) { - requireNonNull(range, "Range should not be null"); - requireNonNull(image, "Image should not be null"); - checkInputFileNotNull(); - if (isExcludedForDuplication()) { - return this; - } - checkState(lastRange == null || lastRange.end().compareTo(range.start()) <= 0, - "Tokens of file %s should be provided in order.\nPrevious token: %s\nLast token: %s", inputFile, lastRange, range); - - int line = range.start().line(); - if (line != startLine) { - addNewTokensLine(result, startIndex, currentIndex, startLine, sb); - startIndex = currentIndex + 1; - startLine = line; - } - currentIndex++; - sb.append(image); - lastRange = range; - - return this; - } - - private boolean isExcludedForDuplication() { - if (inputFile.isExcludedForDuplication()) { - return true; - } - if (inputFile.type() == InputFile.Type.TEST) { - if (!loggedTestCpdWarning) { - LOG.warn("Duplication reported for '{}' will be ignored because it's a test file.", inputFile); - loggedTestCpdWarning = true; - } - return true; - } - return false; - } - - public List<TokensLine> getTokenLines() { - return unmodifiableList(new ArrayList<>(result)); - } - - private static void addNewTokensLine(List<TokensLine> result, int startUnit, int endUnit, int startLine, StringBuilder sb) { - if (sb.length() != 0) { - result.add(new TokensLine(startUnit, endUnit, startLine, sb.toString())); - sb.setLength(0); - } - } - - @Override - protected void doSave() { - checkState(inputFile != null, "Call onFile() first"); - if (isExcludedForDuplication()) { - return; - } - addNewTokensLine(result, startIndex, currentIndex, startLine, sb); - storage.store(this); - } - - private void checkInputFileNotNull() { - checkState(inputFile != null, "Call onFile() first"); - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/error/internal/DefaultAnalysisError.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/error/internal/DefaultAnalysisError.java deleted file mode 100644 index 3a9073a5095..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/error/internal/DefaultAnalysisError.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.sensor.error.internal; - -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextPointer; -import org.sonar.api.batch.sensor.error.AnalysisError; -import org.sonar.api.batch.sensor.error.NewAnalysisError; -import org.sonar.api.batch.sensor.internal.DefaultStorable; -import org.sonar.api.batch.sensor.internal.SensorStorage; - -import static java.util.Objects.requireNonNull; -import static org.sonar.api.utils.Preconditions.checkArgument; -import static org.sonar.api.utils.Preconditions.checkState; - -public class DefaultAnalysisError extends DefaultStorable implements NewAnalysisError, AnalysisError { - private InputFile inputFile; - private String message; - private TextPointer location; - - public DefaultAnalysisError() { - super(null); - } - - public DefaultAnalysisError(SensorStorage storage) { - super(storage); - } - - @Override - public InputFile inputFile() { - return inputFile; - } - - @Override - public String message() { - return message; - } - - @Override - public TextPointer location() { - return location; - } - - @Override - public NewAnalysisError onFile(InputFile inputFile) { - checkArgument(inputFile != null, "Cannot use a inputFile that is null"); - checkState(this.inputFile == null, "onFile() already called"); - this.inputFile = inputFile; - return this; - } - - @Override - public NewAnalysisError message(String message) { - this.message = message; - return this; - } - - @Override - public NewAnalysisError at(TextPointer location) { - checkState(this.location == null, "at() already called"); - this.location = location; - return this; - } - - @Override - protected void doSave() { - requireNonNull(this.inputFile, "inputFile is mandatory on AnalysisError"); - storage.store(this); - } - -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/error/internal/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/error/internal/package-info.java deleted file mode 100644 index 1abae586b1e..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/error/internal/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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. - */ -@javax.annotation.ParametersAreNonnullByDefault -package org.sonar.api.batch.sensor.error.internal; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/DefaultStorable.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/DefaultStorable.java deleted file mode 100644 index 822ffbc2a40..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/DefaultStorable.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.sensor.internal; - -import javax.annotation.Nullable; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; - -import static java.util.Objects.requireNonNull; -import static org.sonar.api.utils.Preconditions.checkState; - -public abstract class DefaultStorable { - - protected final transient SensorStorage storage; - private transient boolean saved = false; - - public DefaultStorable() { - this.storage = null; - } - - public DefaultStorable(@Nullable SensorStorage storage) { - this.storage = storage; - } - - public final void save() { - requireNonNull(this.storage, "No persister on this object"); - checkState(!saved, "This object was already saved"); - doSave(); - this.saved = true; - } - - protected abstract void doSave(); - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); - } - -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorStorage.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorStorage.java index fde8e22e5ac..1e0e5580de4 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorStorage.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorStorage.java @@ -19,16 +19,16 @@ */ package org.sonar.api.batch.sensor.internal; -import org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode; -import org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage; -import org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens; +import org.sonar.api.batch.sensor.code.NewSignificantCode; +import org.sonar.api.batch.sensor.coverage.NewCoverage; +import org.sonar.api.batch.sensor.cpd.NewCpdTokens; import org.sonar.api.batch.sensor.error.AnalysisError; import org.sonar.api.batch.sensor.highlighting.NewHighlighting; import org.sonar.api.batch.sensor.issue.ExternalIssue; import org.sonar.api.batch.sensor.issue.Issue; import org.sonar.api.batch.sensor.measure.Measure; import org.sonar.api.batch.sensor.rule.AdHocRule; -import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable; +import org.sonar.api.batch.sensor.symbol.NewSymbolTable; import org.sonar.api.scanner.ScannerSide; /** @@ -52,17 +52,17 @@ public interface SensorStorage { /** * @since 5.2 */ - void store(DefaultCoverage defaultCoverage); + void store(NewCoverage defaultCoverage); /** * @since 5.5 */ - void store(DefaultCpdTokens defaultCpdTokens); + void store(NewCpdTokens cpdTokens); /** * @since 5.6 */ - void store(DefaultSymbolTable symbolTable); + void store(NewSymbolTable symbolTable); /** * @since 6.0 @@ -81,5 +81,5 @@ public interface SensorStorage { /** * @since 7.2 */ - void store(DefaultSignificantCode significantCode); + void store(NewSignificantCode significantCode); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTable.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTable.java deleted file mode 100644 index 087681a5aa8..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTable.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.sensor.symbol.internal; - -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.sensor.internal.DefaultStorable; -import org.sonar.api.batch.sensor.internal.SensorStorage; -import org.sonar.api.batch.sensor.symbol.NewSymbol; -import org.sonar.api.batch.sensor.symbol.NewSymbolTable; - -import static java.util.Objects.requireNonNull; -import static org.sonar.api.utils.Preconditions.checkArgument; -import static org.sonar.api.utils.Preconditions.checkState; - -public class DefaultSymbolTable extends DefaultStorable implements NewSymbolTable { - - private final Map<TextRange, Set<TextRange>> referencesBySymbol; - private DefaultInputFile inputFile; - - public DefaultSymbolTable(SensorStorage storage) { - super(storage); - referencesBySymbol = new LinkedHashMap<>(); - } - - public Map<TextRange, Set<TextRange>> getReferencesBySymbol() { - return referencesBySymbol; - } - - @Override - public DefaultSymbolTable onFile(InputFile inputFile) { - requireNonNull(inputFile, "file can't be null"); - this.inputFile = (DefaultInputFile) inputFile; - return this; - } - - public InputFile inputFile() { - return inputFile; - } - - @Override - public NewSymbol newSymbol(int startLine, int startLineOffset, int endLine, int endLineOffset) { - checkInputFileNotNull(); - TextRange declarationRange; - try { - declarationRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset); - } catch (Exception e) { - throw new IllegalArgumentException("Unable to create symbol on file " + inputFile, e); - } - return newSymbol(declarationRange); - } - - @Override - public NewSymbol newSymbol(int startOffset, int endOffset) { - checkInputFileNotNull(); - TextRange declarationRange; - try { - declarationRange = inputFile.newRange(startOffset, endOffset); - } catch (Exception e) { - throw new IllegalArgumentException("Unable to create symbol on file " + inputFile, e); - } - return newSymbol(declarationRange); - } - - @Override - public NewSymbol newSymbol(TextRange range) { - checkInputFileNotNull(); - TreeSet<TextRange> references = new TreeSet<>((o1, o2) -> o1.start().compareTo(o2.start())); - referencesBySymbol.put(range, references); - return new DefaultSymbol(inputFile, range, references); - } - - private static class DefaultSymbol implements NewSymbol { - - private final Collection<TextRange> references; - private final DefaultInputFile inputFile; - private final TextRange declaration; - - public DefaultSymbol(DefaultInputFile inputFile, TextRange declaration, Collection<TextRange> references) { - this.inputFile = inputFile; - this.declaration = declaration; - this.references = references; - } - - @Override - public NewSymbol newReference(int startOffset, int endOffset) { - TextRange referenceRange; - try { - referenceRange = inputFile.newRange(startOffset, endOffset); - } catch (Exception e) { - throw new IllegalArgumentException("Unable to create symbol reference on file " + inputFile, e); - } - return newReference(referenceRange); - } - - @Override - public NewSymbol newReference(int startLine, int startLineOffset, int endLine, int endLineOffset) { - TextRange referenceRange; - try { - referenceRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset); - } catch (Exception e) { - throw new IllegalArgumentException("Unable to create symbol reference on file " + inputFile, e); - } - return newReference(referenceRange); - } - - @Override - public NewSymbol newReference(TextRange range) { - requireNonNull(range, "Provided range is null"); - checkArgument(!declaration.overlap(range), "Overlapping symbol declaration and reference for symbol at %s", declaration); - references.add(range); - return this; - } - - } - - @Override - protected void doSave() { - checkInputFileNotNull(); - storage.store(this); - } - - private void checkInputFileNotNull() { - checkState(inputFile != null, "Call onFile() first"); - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/symbol/internal/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/symbol/internal/package-info.java deleted file mode 100644 index e8dcd39cf6c..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/symbol/internal/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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. - */ -@javax.annotation.ParametersAreNonnullByDefault -package org.sonar.api.batch.sensor.symbol.internal; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/NoSonarFilter.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/NoSonarFilter.java index a58e5f02e4f..a94fd4f2123 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/NoSonarFilter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/NoSonarFilter.java @@ -21,7 +21,6 @@ package org.sonar.api.issue; import java.util.Set; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.scanner.ScannerSide; /** @@ -34,7 +33,7 @@ import org.sonar.api.scanner.ScannerSide; * @since 3.6 */ @ScannerSide -public class NoSonarFilter { +public abstract class NoSonarFilter { /** * Register lines in a file that contains the NOSONAR flag. @@ -44,9 +43,5 @@ public class NoSonarFilter { * @since 5.0 * @since 7.6 the method can be called multiple times by different sensors, and NOSONAR lines are merged */ - public NoSonarFilter noSonarInFile(InputFile inputFile, Set<Integer> noSonarLines) { - ((DefaultInputFile) inputFile).noSonarAt(noSonarLines); - return this; - } - + public abstract NoSonarFilter noSonarInFile(InputFile inputFile, Set<Integer> noSonarLines); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputDirTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputDirTest.java deleted file mode 100644 index ed2c2b36a64..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputDirTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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 org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - -import java.io.File; - -import static org.assertj.core.api.Assertions.assertThat; - -public class DefaultInputDirTest { - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - @Test - public void test() throws Exception { - File baseDir = temp.newFolder(); - DefaultInputDir inputDir = new DefaultInputDir("ABCDE", "src") - .setModuleBaseDir(baseDir.toPath()); - - assertThat(inputDir.key()).isEqualTo("ABCDE:src"); - assertThat(inputDir.file().getAbsolutePath()).isEqualTo(new File(baseDir, "src").getAbsolutePath()); - assertThat(inputDir.relativePath()).isEqualTo("src"); - assertThat(new File(inputDir.relativePath())).isRelative(); - assertThat(inputDir.absolutePath()).endsWith("src"); - assertThat(new File(inputDir.absolutePath())).isAbsolute(); - } - - @Test - public void testEqualsAndHashCode() throws Exception { - DefaultInputDir inputDir1 = new DefaultInputDir("ABCDE", "src"); - - DefaultInputDir inputDir2 = new DefaultInputDir("ABCDE", "src"); - - assertThat(inputDir1.equals(inputDir1)).isTrue(); - assertThat(inputDir1.equals(inputDir2)).isTrue(); - assertThat(inputDir1.equals("foo")).isFalse(); - - assertThat(inputDir1.hashCode()).isEqualTo(63545559); - - assertThat(inputDir1.toString()).contains("[moduleKey=ABCDE, relative=src, basedir=null"); - - } - -} 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 deleted file mode 100644 index 85feccc8581..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java +++ /dev/null @@ -1,314 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.StringReader; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; -import java.util.stream.Collectors; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.scanner.fs.FileMetadata; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; - -public class DefaultInputFileTest { - - private static final String PROJECT_RELATIVE_PATH = "module1/src/Foo.php"; - private static final String MODULE_RELATIVE_PATH = "src/Foo.php"; - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - private DefaultIndexedFile indexedFile; - - private Path baseDir; - private SensorStrategy sensorStrategy; - - @Before - public void prepare() throws IOException { - baseDir = temp.newFolder().toPath(); - sensorStrategy = new SensorStrategy(); - indexedFile = new DefaultIndexedFile(baseDir.resolve(PROJECT_RELATIVE_PATH), "ABCDE", PROJECT_RELATIVE_PATH, MODULE_RELATIVE_PATH, InputFile.Type.TEST, "php", 0, - sensorStrategy); - } - - @Test - public void test() throws Exception { - - Metadata metadata = new Metadata(42, 42, "", new int[0], new int[0], 10); - DefaultInputFile inputFile = new DefaultInputFile(indexedFile, (f) -> f.setMetadata(metadata)) - .setStatus(InputFile.Status.ADDED) - .setCharset(StandardCharsets.ISO_8859_1); - - assertThat(inputFile.absolutePath()).endsWith("Foo.php"); - assertThat(inputFile.filename()).isEqualTo("Foo.php"); - assertThat(inputFile.uri()).hasPath(baseDir.resolve(PROJECT_RELATIVE_PATH).toUri().getPath()); - assertThat(new File(inputFile.absolutePath())).isAbsolute(); - assertThat(inputFile.language()).isEqualTo("php"); - assertThat(inputFile.status()).isEqualTo(InputFile.Status.ADDED); - assertThat(inputFile.type()).isEqualTo(InputFile.Type.TEST); - assertThat(inputFile.lines()).isEqualTo(42); - assertThat(inputFile.charset()).isEqualTo(StandardCharsets.ISO_8859_1); - - assertThat(inputFile.getModuleRelativePath()).isEqualTo(MODULE_RELATIVE_PATH); - assertThat(inputFile.getProjectRelativePath()).isEqualTo(PROJECT_RELATIVE_PATH); - - sensorStrategy.setGlobal(false); - assertThat(inputFile.relativePath()).isEqualTo(MODULE_RELATIVE_PATH); - assertThat(new File(inputFile.relativePath())).isRelative(); - sensorStrategy.setGlobal(true); - assertThat(inputFile.relativePath()).isEqualTo(PROJECT_RELATIVE_PATH); - assertThat(new File(inputFile.relativePath())).isRelative(); - } - - @Test - public void test_content() throws IOException { - Path testFile = baseDir.resolve(PROJECT_RELATIVE_PATH); - Files.createDirectories(testFile.getParent()); - String content = "test é string"; - Files.write(testFile, content.getBytes(StandardCharsets.ISO_8859_1)); - - assertThat(Files.readAllLines(testFile, StandardCharsets.ISO_8859_1).get(0)).hasSize(content.length()); - - Metadata metadata = new Metadata(42, 30, "", new int[0], new int[0], 10); - - DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> f.setMetadata(metadata)) - .setStatus(InputFile.Status.ADDED) - .setCharset(StandardCharsets.ISO_8859_1); - - assertThat(inputFile.contents()).isEqualTo(content); - try (InputStream inputStream = inputFile.inputStream()) { - String result = new BufferedReader(new InputStreamReader(inputStream, inputFile.charset())).lines().collect(Collectors.joining()); - assertThat(result).isEqualTo(content); - } - - } - - @Test - public void test_content_exclude_bom() throws IOException { - Path testFile = baseDir.resolve(PROJECT_RELATIVE_PATH); - Files.createDirectories(testFile.getParent()); - try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(testFile.toFile()), StandardCharsets.UTF_8))) { - out.write('\ufeff'); - } - String content = "test é string €"; - Files.write(testFile, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND); - - assertThat(Files.readAllLines(testFile, StandardCharsets.UTF_8).get(0)).hasSize(content.length() + 1); - - Metadata metadata = new Metadata(42, 30, "", new int[0], new int[0], 10); - - DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> f.setMetadata(metadata)) - .setStatus(InputFile.Status.ADDED) - .setCharset(StandardCharsets.UTF_8); - - assertThat(inputFile.contents()).isEqualTo(content); - try (InputStream inputStream = inputFile.inputStream()) { - String result = new BufferedReader(new InputStreamReader(inputStream, inputFile.charset())).lines().collect(Collectors.joining()); - assertThat(result).isEqualTo(content); - } - - } - - @Test - public void test_equals_and_hashcode() throws Exception { - DefaultInputFile f1 = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), (f) -> mock(Metadata.class)); - DefaultInputFile f1a = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), (f) -> mock(Metadata.class)); - DefaultInputFile f2 = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Bar.php", null), (f) -> mock(Metadata.class)); - - assertThat(f1).isEqualTo(f1); - assertThat(f1).isEqualTo(f1a); - assertThat(f1).isNotEqualTo(f2); - assertThat(f1.equals("foo")).isFalse(); - assertThat(f1.equals(null)).isFalse(); - - assertThat(f1.hashCode()).isEqualTo(f1.hashCode()); - assertThat(f1.hashCode()).isEqualTo(f1a.hashCode()); - } - - @Test - public void test_toString() throws Exception { - DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), (f) -> mock(Metadata.class)); - assertThat(file.toString()).isEqualTo(MODULE_RELATIVE_PATH); - } - - @Test - public void checkValidPointer() { - Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, new int[] {9, 15}, 16); - DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); - assertThat(file.newPointer(1, 0).line()).isEqualTo(1); - assertThat(file.newPointer(1, 0).lineOffset()).isEqualTo(0); - // Don't fail - file.newPointer(1, 9); - file.newPointer(2, 0); - file.newPointer(2, 5); - - try { - file.newPointer(0, 1); - fail(); - } catch (Exception e) { - assertThat(e).hasMessage("0 is not a valid line for a file"); - } - try { - file.newPointer(3, 1); - fail(); - } catch (Exception e) { - assertThat(e).hasMessage("3 is not a valid line for pointer. File src/Foo.php has 2 line(s)"); - } - try { - file.newPointer(1, -1); - fail(); - } catch (Exception e) { - assertThat(e).hasMessage("-1 is not a valid line offset for a file"); - } - try { - file.newPointer(1, 10); - fail(); - } catch (Exception e) { - assertThat(e).hasMessage("10 is not a valid line offset for pointer. File src/Foo.php has 9 character(s) at line 1"); - } - } - - @Test - public void checkValidPointerUsingGlobalOffset() { - Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, new int[] {8, 15}, 16); - DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); - assertThat(file.newPointer(0).line()).isEqualTo(1); - assertThat(file.newPointer(0).lineOffset()).isEqualTo(0); - - assertThat(file.newPointer(9).line()).isEqualTo(1); - // Ignore eol characters - assertThat(file.newPointer(9).lineOffset()).isEqualTo(8); - - assertThat(file.newPointer(10).line()).isEqualTo(2); - assertThat(file.newPointer(10).lineOffset()).isEqualTo(0); - - assertThat(file.newPointer(15).line()).isEqualTo(2); - assertThat(file.newPointer(15).lineOffset()).isEqualTo(5); - - assertThat(file.newPointer(16).line()).isEqualTo(2); - // Ignore eol characters - assertThat(file.newPointer(16).lineOffset()).isEqualTo(5); - - try { - file.newPointer(-1); - fail(); - } catch (Exception e) { - assertThat(e).hasMessage("-1 is not a valid offset for a file"); - } - - try { - file.newPointer(17); - fail(); - } catch (Exception e) { - assertThat(e).hasMessage("17 is not a valid offset for file src/Foo.php. Max offset is 16"); - } - } - - @Test - public void checkValidRange() { - Metadata metadata = new FileMetadata().readMetadata(new StringReader("bla bla a\nabcde")); - DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); - - assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 1)).start().line()).isEqualTo(1); - // Don't fail - file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)); - file.newRange(file.newPointer(1, 0), file.newPointer(1, 9)); - file.newRange(file.newPointer(1, 0), file.newPointer(2, 0)); - assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 5))).isEqualTo(file.newRange(0, 15)); - - try { - file.newRange(file.newPointer(1, 0), file.newPointer(1, 0)); - fail(); - } catch (Exception e) { - assertThat(e).hasMessage("Start pointer [line=1, lineOffset=0] should be before end pointer [line=1, lineOffset=0]"); - } - try { - file.newRange(file.newPointer(1, 0), file.newPointer(1, 10)); - fail(); - } catch (Exception e) { - assertThat(e).hasMessage("10 is not a valid line offset for pointer. File src/Foo.php has 9 character(s) at line 1"); - } - } - - @Test - public void selectLine() { - Metadata metadata = new FileMetadata().readMetadata(new StringReader("bla bla a\nabcde\n\nabc")); - DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); - - assertThat(file.selectLine(1).start().line()).isEqualTo(1); - assertThat(file.selectLine(1).start().lineOffset()).isEqualTo(0); - assertThat(file.selectLine(1).end().line()).isEqualTo(1); - assertThat(file.selectLine(1).end().lineOffset()).isEqualTo(9); - - // Don't fail when selecting empty line - assertThat(file.selectLine(3).start().line()).isEqualTo(3); - assertThat(file.selectLine(3).start().lineOffset()).isEqualTo(0); - assertThat(file.selectLine(3).end().line()).isEqualTo(3); - assertThat(file.selectLine(3).end().lineOffset()).isEqualTo(0); - - try { - file.selectLine(5); - fail(); - } catch (Exception e) { - assertThat(e).hasMessage("5 is not a valid line for pointer. File src/Foo.php has 4 line(s)"); - } - } - - @Test - public void checkValidRangeUsingGlobalOffset() { - Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, new int[] {9, 15}, 16); - DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); - TextRange newRange = file.newRange(10, 13); - assertThat(newRange.start().line()).isEqualTo(2); - assertThat(newRange.start().lineOffset()).isEqualTo(0); - assertThat(newRange.end().line()).isEqualTo(2); - assertThat(newRange.end().lineOffset()).isEqualTo(3); - } - - @Test - public void testRangeOverlap() { - Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, new int[] {9, 15}, 16); - DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); - // Don't fail - assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)))).isTrue(); - assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 2)))).isTrue(); - assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 1), file.newPointer(1, 2)))).isFalse(); - assertThat(file.newRange(file.newPointer(1, 2), file.newPointer(1, 3)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 2)))).isFalse(); - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputModuleTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputModuleTest.java deleted file mode 100644 index 42394d7f81e..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputModuleTest.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Path; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.bootstrap.ProjectDefinition; - -import static org.assertj.core.api.Assertions.assertThat; - -public class DefaultInputModuleTest { - - private static final String FILE_1 = "file1"; - private static final String TEST_1 = "test1"; - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - @Test - public void check_getters() throws IOException { - ProjectDefinition def = ProjectDefinition.create(); - def.setKey("moduleKey"); - File baseDir = temp.newFolder(); - Path src = baseDir.toPath().resolve(FILE_1); - Files.createFile(src); - Path test = baseDir.toPath().resolve(TEST_1); - Files.createFile(test); - def.setBaseDir(baseDir); - File workDir = temp.newFolder(); - def.setWorkDir(workDir); - def.setSources(FILE_1); - def.setTests(TEST_1); - DefaultInputModule module = new DefaultInputModule(def); - - assertThat(module.key()).isEqualTo("moduleKey"); - assertThat(module.definition()).isEqualTo(def); - assertThat(module.getBranch()).isNull(); - assertThat(module.getBaseDir()).isEqualTo(baseDir.toPath()); - assertThat(module.getKeyWithBranch()).isEqualTo("moduleKey"); - assertThat(module.getWorkDir()).isEqualTo(workDir.toPath()); - assertThat(module.getEncoding()).isEqualTo(Charset.defaultCharset()); - assertThat(module.getSourceDirsOrFiles().get()).containsExactlyInAnyOrder(src); - assertThat(module.getTestDirsOrFiles().get()).containsExactlyInAnyOrder(test); - assertThat(module.getEncoding()).isEqualTo(Charset.defaultCharset()); - - assertThat(module.isFile()).isFalse(); - } - - @Test - public void no_sources() throws IOException { - ProjectDefinition def = ProjectDefinition.create(); - def.setKey("moduleKey"); - File baseDir = temp.newFolder(); - Path src = baseDir.toPath().resolve(FILE_1); - Files.createFile(src); - Path test = baseDir.toPath().resolve(TEST_1); - Files.createFile(test); - def.setBaseDir(baseDir); - File workDir = temp.newFolder(); - def.setWorkDir(workDir); - DefaultInputModule module = new DefaultInputModule(def); - - assertThat(module.key()).isEqualTo("moduleKey"); - assertThat(module.definition()).isEqualTo(def); - assertThat(module.getBranch()).isNull(); - assertThat(module.getBaseDir()).isEqualTo(baseDir.toPath()); - assertThat(module.getKeyWithBranch()).isEqualTo("moduleKey"); - assertThat(module.getWorkDir()).isEqualTo(workDir.toPath()); - assertThat(module.getEncoding()).isEqualTo(Charset.defaultCharset()); - assertThat(module.getSourceDirsOrFiles()).isNotPresent(); - assertThat(module.getTestDirsOrFiles()).isNotPresent(); - assertThat(module.getEncoding()).isEqualTo(Charset.defaultCharset()); - - assertThat(module.isFile()).isFalse(); - } - - @Test - public void working_directory_should_be_hidden() throws IOException { - ProjectDefinition def = ProjectDefinition.create(); - File workDir = temp.newFolder(".sonar"); - def.setWorkDir(workDir); - File baseDir = temp.newFolder(); - def.setBaseDir(baseDir); - DefaultInputModule module = new DefaultInputModule(def); - assertThat(workDir.isHidden()).isTrue(); - } - -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputProjectTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputProjectTest.java deleted file mode 100644 index 8a4a33bdce3..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputProjectTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.bootstrap.ProjectDefinition; - -import static org.assertj.core.api.Assertions.assertThat; - -public class DefaultInputProjectTest { - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - @Test - public void testGetters() throws IOException { - ProjectDefinition def = ProjectDefinition.create(); - def.setKey("projectKey"); - def.setName("projectName"); - File baseDir = temp.newFolder(); - def.setBaseDir(baseDir); - def.setDescription("desc"); - File workDir = temp.newFolder(); - def.setWorkDir(workDir); - def.setSources("file1"); - def.setTests("test1"); - AbstractProjectOrModule project = new DefaultInputProject(def); - - assertThat(project.key()).isEqualTo("projectKey"); - assertThat(project.getName()).isEqualTo("projectName"); - assertThat(project.getOriginalName()).isEqualTo("projectName"); - assertThat(project.definition()).isEqualTo(def); - assertThat(project.getBranch()).isNull(); - assertThat(project.getBaseDir()).isEqualTo(baseDir.toPath()); - assertThat(project.getKeyWithBranch()).isEqualTo("projectKey"); - assertThat(project.getDescription()).isEqualTo("desc"); - assertThat(project.getWorkDir()).isEqualTo(workDir.toPath()); - assertThat(project.getEncoding()).isEqualTo(Charset.defaultCharset()); - - assertThat(project.properties()).hasSize(5); - - assertThat(project.isFile()).isFalse(); - } - - @Test - public void testEncoding() throws IOException { - ProjectDefinition def = ProjectDefinition.create(); - def.setKey("projectKey"); - def.setName("projectName"); - File baseDir = temp.newFolder(); - def.setBaseDir(baseDir); - def.setProjectVersion("version"); - def.setDescription("desc"); - File workDir = temp.newFolder(); - def.setWorkDir(workDir); - def.setSources("file1"); - def.setProperty("sonar.sourceEncoding", "UTF-16"); - AbstractProjectOrModule project = new DefaultInputProject(def); - - assertThat(project.getEncoding()).isEqualTo(StandardCharsets.UTF_16); - } - -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/PathPatternTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/PathPatternTest.java index cc2d186da44..8296da8465e 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/PathPatternTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/PathPatternTest.java @@ -27,6 +27,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.batch.fs.IndexedFile; +import org.sonar.scanner.fs.DefaultIndexedFile; import static org.assertj.core.api.Assertions.assertThat; diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/TestInputFileBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/TestInputFileBuilderTest.java deleted file mode 100644 index 3055be6a419..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/TestInputFileBuilderTest.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import org.apache.commons.io.IOUtils; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.fs.InputFile.Status; -import org.sonar.api.batch.fs.InputFile.Type; -import org.sonar.scanner.fs.TestInputFileBuilder; - -import static org.assertj.core.api.Assertions.assertThat; - -public class TestInputFileBuilderTest { - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - @Test - public void setContent() throws IOException { - DefaultInputFile file = TestInputFileBuilder.create("module", "invalidPath") - .setContents("my content") - .setCharset(StandardCharsets.UTF_8) - .build(); - assertThat(file.contents()).isEqualTo("my content"); - assertThat(IOUtils.toString(file.inputStream())).isEqualTo("my content"); - } - - @Test - public void testGetters() { - DefaultInputFile file = TestInputFileBuilder.create("module", new File("baseDir"), new File("baseDir", "path")) - .setStatus(Status.SAME) - .setType(Type.MAIN) - .build(); - - assertThat(file.type()).isEqualTo(Type.MAIN); - assertThat(file.status()).isEqualTo(Status.SAME); - assertThat(file.isPublished()).isTrue(); - assertThat(file.type()).isEqualTo(Type.MAIN); - assertThat(file.relativePath()).isEqualTo("path"); - assertThat(file.absolutePath()).isEqualTo("baseDir/path"); - - } - - @Test - public void testCreateInputModule() throws IOException { - File baseDir = temp.newFolder(); - AbstractProjectOrModule module = TestInputFileBuilder.newDefaultInputModule("key", baseDir); - assertThat(module.key()).isEqualTo("key"); - assertThat(module.getBaseDir()).isEqualTo(baseDir.toPath()); - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/code/internal/DefaultSignificantCodeTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/code/internal/DefaultSignificantCodeTest.java deleted file mode 100644 index 5d853e456ea..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/code/internal/DefaultSignificantCodeTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.sensor.code.internal; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.sensor.internal.SensorStorage; -import org.sonar.scanner.fs.TestInputFileBuilder; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -public class DefaultSignificantCodeTest { - private SensorStorage sensorStorage = mock(SensorStorage.class); - private DefaultSignificantCode underTest = new DefaultSignificantCode(sensorStorage); - private InputFile inputFile = TestInputFileBuilder.create("module", "file1.xoo") - .setContents("this is\na file\n with some code") - .build(); - - @Rule - public ExpectedException exception = ExpectedException.none(); - - @Test - public void should_save_ranges() { - underTest.onFile(inputFile) - .addRange(inputFile.selectLine(1)) - .save(); - verify(sensorStorage).store(underTest); - } - - @Test - public void fail_if_save_without_file() { - exception.expect(IllegalStateException.class); - exception.expectMessage("Call onFile() first"); - underTest.save(); - } - - @Test - public void fail_if_add_range_to_same_line_twice() { - underTest.onFile(inputFile); - underTest.addRange(inputFile.selectLine(1)); - - exception.expect(IllegalStateException.class); - exception.expectMessage("Significant code was already reported for line '1'."); - underTest.addRange(inputFile.selectLine(1)); - } - - @Test - public void fail_if_range_includes_many_lines() { - underTest.onFile(inputFile); - - exception.expect(IllegalArgumentException.class); - exception.expectMessage("Ranges of significant code must be located in a single line"); - underTest.addRange(inputFile.newRange(1, 1, 2, 1)); - } - - @Test - public void fail_if_add_range_before_setting_file() { - exception.expect(IllegalStateException.class); - exception.expectMessage("addRange() should be called after on()"); - underTest.addRange(inputFile.selectLine(1)); - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokensTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokensTest.java deleted file mode 100644 index 1adb0c71e54..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokensTest.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.sensor.cpd.internal; - -import org.junit.Test; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.sensor.internal.SensorStorage; -import org.sonar.scanner.fs.TestInputFileBuilder; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; -import static org.assertj.core.api.Assertions.tuple; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; - -public class DefaultCpdTokensTest { - private final SensorStorage sensorStorage = mock(SensorStorage.class); - - private final DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java") - .setLines(2) - .setOriginalLineStartOffsets(new int[] {0, 50}) - .setOriginalLineEndOffsets(new int[] {49, 100}) - .setLastValidOffset(101) - .build(); - - @Test - public void save_no_tokens() { - DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage) - .onFile(inputFile); - - tokens.save(); - - verify(sensorStorage).store(tokens); - - assertThat(tokens.inputFile()).isEqualTo(inputFile); - } - - @Test - public void save_one_token() { - DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage) - .onFile(inputFile) - .addToken(inputFile.newRange(1, 2, 1, 5), "foo"); - - tokens.save(); - - verify(sensorStorage).store(tokens); - - assertThat(tokens.getTokenLines()).extracting("value", "startLine", "hashCode", "startUnit", "endUnit").containsExactly(tuple("foo", 1, "foo".hashCode(), 1, 1)); - } - - @Test - public void handle_exclusions() { - inputFile.setExcludedForDuplication(true); - DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage) - .onFile(inputFile) - .addToken(inputFile.newRange(1, 2, 1, 5), "foo"); - - tokens.save(); - - verifyZeroInteractions(sensorStorage); - - assertThat(tokens.getTokenLines()).isEmpty(); - } - - @Test - public void dont_save_for_test_files() { - DefaultInputFile testInputFile = new TestInputFileBuilder("foo", "src/Foo.java") - .setLines(2) - .setOriginalLineStartOffsets(new int[] {0, 50}) - .setOriginalLineEndOffsets(new int[] {49, 100}) - .setLastValidOffset(101) - .setType(InputFile.Type.TEST) - .build(); - - DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage) - .onFile(testInputFile) - .addToken(testInputFile.newRange(1, 2, 1, 5), "foo"); - - tokens.save(); - verifyZeroInteractions(sensorStorage); - assertThat(tokens.getTokenLines()).isEmpty(); - } - - @Test - public void save_many_tokens() { - DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage) - .onFile(inputFile) - .addToken(inputFile.newRange(1, 2, 1, 5), "foo") - .addToken(inputFile.newRange(1, 6, 1, 10), "bar") - .addToken(inputFile.newRange(1, 20, 1, 25), "biz") - .addToken(inputFile.newRange(2, 1, 2, 10), "next"); - - tokens.save(); - - verify(sensorStorage).store(tokens); - - assertThat(tokens.getTokenLines()) - .extracting("value", "startLine", "hashCode", "startUnit", "endUnit") - .containsExactly( - tuple("foobarbiz", 1, "foobarbiz".hashCode(), 1, 3), - tuple("next", 2, "next".hashCode(), 4, 4)); - } - - @Test - public void basic_validation() { - SensorStorage sensorStorage = mock(SensorStorage.class); - DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage); - try { - tokens.save(); - fail("Expected exception"); - } catch (Exception e) { - assertThat(e).hasMessage("Call onFile() first"); - } - try { - tokens.addToken(inputFile.newRange(1, 2, 1, 5), "foo"); - fail("Expected exception"); - } catch (Exception e) { - assertThat(e).hasMessage("Call onFile() first"); - } - try { - tokens.addToken(null, "foo"); - fail("Expected exception"); - } catch (Exception e) { - assertThat(e).hasMessage("Range should not be null"); - } - try { - tokens.addToken(inputFile.newRange(1, 2, 1, 5), null); - fail("Expected exception"); - } catch (Exception e) { - assertThat(e).hasMessage("Image should not be null"); - } - } - - @Test - public void validate_tokens_order() { - SensorStorage sensorStorage = mock(SensorStorage.class); - DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage) - .onFile(inputFile) - .addToken(inputFile.newRange(1, 6, 1, 10), "bar"); - - try { - tokens.addToken(inputFile.newRange(1, 2, 1, 5), "foo"); - fail("Expected exception"); - } catch (Exception e) { - assertThat(e).hasMessage("Tokens of file src/Foo.java should be provided in order.\n" + - "Previous token: Range[from [line=1, lineOffset=6] to [line=1, lineOffset=10]]\n" + - "Last token: Range[from [line=1, lineOffset=2] to [line=1, lineOffset=5]]"); - } - } - -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/error/internal/DefaultAnalysisErrorTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/error/internal/DefaultAnalysisErrorTest.java deleted file mode 100644 index ae920c967fe..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/error/internal/DefaultAnalysisErrorTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.sensor.error.internal; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextPointer; -import org.sonar.api.batch.fs.internal.DefaultTextPointer; -import org.sonar.api.batch.sensor.error.NewAnalysisError; -import org.sonar.api.batch.sensor.internal.SensorStorage; -import org.sonar.scanner.fs.TestInputFileBuilder; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.*; - -public class DefaultAnalysisErrorTest { - private InputFile inputFile; - private SensorStorage storage; - private TextPointer textPointer; - - @Rule - public ExpectedException exception = ExpectedException.none(); - - @Before - public void setUp() { - inputFile = new TestInputFileBuilder("module1", "src/File.java").build(); - textPointer = new DefaultTextPointer(5, 2); - storage = mock(SensorStorage.class); - } - - @Test - public void test_analysis_error() { - DefaultAnalysisError analysisError = new DefaultAnalysisError(storage); - analysisError.onFile(inputFile) - .at(textPointer) - .message("msg"); - - assertThat(analysisError.location()).isEqualTo(textPointer); - assertThat(analysisError.message()).isEqualTo("msg"); - assertThat(analysisError.inputFile()).isEqualTo(inputFile); - } - - @Test - public void test_save() { - DefaultAnalysisError analysisError = new DefaultAnalysisError(storage); - analysisError.onFile(inputFile).save(); - - verify(storage).store(analysisError); - verifyNoMoreInteractions(storage); - } - - @Test - public void test_no_storage() { - exception.expect(NullPointerException.class); - DefaultAnalysisError analysisError = new DefaultAnalysisError(); - analysisError.onFile(inputFile).save(); - } - - @Test - public void test_validation() { - try { - new DefaultAnalysisError(storage).onFile(null); - fail("Expected exception"); - } catch (IllegalArgumentException e) { - // expected - } - - NewAnalysisError error = new DefaultAnalysisError(storage).onFile(inputFile); - try { - error.onFile(inputFile); - fail("Expected exception"); - } catch (IllegalStateException e) { - // expected - } - - error = new DefaultAnalysisError(storage).at(textPointer); - try { - error.at(textPointer); - fail("Expected exception"); - } catch (IllegalStateException e) { - // expected - } - - try { - new DefaultAnalysisError(storage).save(); - fail("Expected exception"); - } catch (NullPointerException e) { - // expected - } - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTableTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTableTest.java deleted file mode 100644 index c880f7f0bd9..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTableTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 SonarSource SA - * mailto:info 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.sensor.symbol.internal; - -import java.util.Map; -import java.util.Set; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.sensor.internal.SensorStorage; -import org.sonar.scanner.fs.TestInputFileBuilder; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; - -public class DefaultSymbolTableTest { - - private static final InputFile INPUT_FILE = new TestInputFileBuilder("foo", "src/Foo.java") - .setLines(2) - .setOriginalLineStartOffsets(new int[] {0, 50}) - .setOriginalLineEndOffsets(new int[] {49, 100}) - .setLastValidOffset(101) - .build(); - - private Map<TextRange, Set<TextRange>> referencesPerSymbol; - - @Rule - public ExpectedException throwable = ExpectedException.none(); - - @Before - public void setUpSampleSymbols() { - - DefaultSymbolTable symbolTableBuilder = new DefaultSymbolTable(mock(SensorStorage.class)) - .onFile(INPUT_FILE); - symbolTableBuilder - .newSymbol(0, 10) - .newReference(12, 15) - .newReference(2, 10, 2, 15); - - symbolTableBuilder.newSymbol(1, 12, 1, 15).newReference(52, 55); - - symbolTableBuilder.save(); - - referencesPerSymbol = symbolTableBuilder.getReferencesBySymbol(); - } - - @Test - public void should_register_symbols() { - assertThat(referencesPerSymbol).hasSize(2); - } - -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/issue/NoSonarFilterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/issue/NoSonarFilterTest.java index 76ac2f6758a..20427354f25 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/issue/NoSonarFilterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/issue/NoSonarFilterTest.java @@ -22,8 +22,9 @@ package org.sonar.api.issue; import java.util.Arrays; import java.util.HashSet; import org.junit.Test; -import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.scanner.fs.DefaultInputFile; import org.sonar.scanner.fs.TestInputFileBuilder; +import org.sonar.scanner.issue.DefaultNoSonarFilter; import static org.assertj.core.api.Assertions.assertThat; @@ -32,7 +33,7 @@ public class NoSonarFilterTest { @Test public void should_store_nosonar_lines_on_inputfile() { DefaultInputFile f = TestInputFileBuilder.create("module1", "myfile.java").setLines(8).build(); - new NoSonarFilter().noSonarInFile(f, new HashSet<>(Arrays.asList(1, 4))); + new DefaultNoSonarFilter().noSonarInFile(f, new HashSet<>(Arrays.asList(1, 4))); assertThat(f.hasNoSonarAt(1)).isTrue(); assertThat(f.hasNoSonarAt(2)).isFalse(); |