import javax.annotation.Nullable;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.ObjectUtils;
-import org.apache.ibatis.session.ResultContext;
-import org.apache.ibatis.session.ResultHandler;
import org.sonar.api.utils.System2;
import org.sonar.core.util.CloseableIterator;
import org.sonar.db.DbClient;
public void visitProject(Component project) {
this.projectUuid = project.getUuid();
session.select("org.sonar.db.source.FileSourceMapper.selectHashesForProject", ImmutableMap.of("projectUuid", projectUuid, "dataType", Type.SOURCE),
- new ResultHandler() {
- @Override
- public void handleResult(ResultContext context) {
- FileSourceDto dto = (FileSourceDto) context.getResultObject();
- previousFileSourcesByUuid.put(dto.getFileUuid(), dto);
- }
+ context -> {
+ FileSourceDto dto = (FileSourceDto) context.getResultObject();
+ previousFileSourcesByUuid.put(dto.getFileUuid(), dto);
});
}
}
private boolean checkIfThereAreUnprocessedCoverageDetails(Multimap<String, DbFileSources.Test.Builder> testsByName,
- Table<String, String, DbFileSources.Test.CoveredFile.Builder> coveredFilesByName, String componentKey) {
+ Table<String, String, DbFileSources.Test.CoveredFile.Builder> coveredFilesByName, String componentKey) {
Set<String> unprocessedCoverageDetailNames = new HashSet<>(coveredFilesByName.rowKeySet());
unprocessedCoverageDetailNames.removeAll(testsByName.keySet());
boolean hasUnprocessedCoverage = !unprocessedCoverageDetailNames.isEmpty();
}
private List<DbFileSources.Test> addCoveredFilesToTests(Multimap<String, DbFileSources.Test.Builder> testsByName,
- Table<String, String, DbFileSources.Test.CoveredFile.Builder> coveredFilesByName) {
+ Table<String, String, DbFileSources.Test.CoveredFile.Builder> coveredFilesByName) {
List<DbFileSources.Test> tests = new ArrayList<>();
for (DbFileSources.Test.Builder test : testsByName.values()) {
Collection<DbFileSources.Test.CoveredFile.Builder> coveredFiles = coveredFilesByName.row(test.getName()).values();
try (CloseableIterator<ScannerReport.CoverageDetail> coverageIterator = reportReader.readCoverageDetails(testFileRef)) {
while (coverageIterator.hasNext()) {
ScannerReport.CoverageDetail batchCoverageDetail = coverageIterator.next();
+ String testName = batchCoverageDetail.getTestName();
for (ScannerReport.CoverageDetail.CoveredFile batchCoveredFile : batchCoverageDetail.getCoveredFileList()) {
- String testName = batchCoverageDetail.getTestName();
- String mainFileUuid = getUuid(batchCoveredFile.getFileRef());
- DbFileSources.Test.CoveredFile.Builder existingDbCoveredFile = nameToCoveredFiles.get(testName, mainFileUuid);
- List<Integer> batchCoveredLines = batchCoveredFile.getCoveredLineList();
- if (existingDbCoveredFile == null) {
- DbFileSources.Test.CoveredFile.Builder dbCoveredFile = DbFileSources.Test.CoveredFile.newBuilder()
- .setFileUuid(getUuid(batchCoveredFile.getFileRef()))
- .addAllCoveredLine(batchCoveredLines);
- nameToCoveredFiles.put(testName, mainFileUuid, dbCoveredFile);
- } else {
- List<Integer> remainingBatchCoveredLines = new ArrayList<>(batchCoveredLines);
- remainingBatchCoveredLines.removeAll(existingDbCoveredFile.getCoveredLineList());
- existingDbCoveredFile.addAllCoveredLine(batchCoveredLines);
- }
+ loadCoverageFile(batchCoveredFile, testName, nameToCoveredFiles);
}
}
}
return nameToCoveredFiles;
}
+ private void loadCoverageFile(ScannerReport.CoverageDetail.CoveredFile batchCoveredFile, String testName, Table<String, String,
+ DbFileSources.Test.CoveredFile.Builder> nameToCoveredFiles) {
+ String mainFileUuid = getUuid(batchCoveredFile.getFileRef());
+ DbFileSources.Test.CoveredFile.Builder existingDbCoveredFile = nameToCoveredFiles.get(testName, mainFileUuid);
+ List<Integer> batchCoveredLines = batchCoveredFile.getCoveredLineList();
+ if (existingDbCoveredFile == null) {
+ DbFileSources.Test.CoveredFile.Builder dbCoveredFile = DbFileSources.Test.CoveredFile.newBuilder()
+ .setFileUuid(getUuid(batchCoveredFile.getFileRef()))
+ .addAllCoveredLine(batchCoveredLines);
+ nameToCoveredFiles.put(testName, mainFileUuid, dbCoveredFile);
+ } else {
+ List<Integer> remainingBatchCoveredLines = new ArrayList<>(batchCoveredLines);
+ remainingBatchCoveredLines.removeAll(existingDbCoveredFile.getCoveredLineList());
+ existingDbCoveredFile.addAllCoveredLine(batchCoveredLines);
+ }
+ }
+
private String getUuid(int fileRef) {
return treeRootHolder.getComponentByRef(fileRef).getUuid();
}
*/
package org.sonar.duplications.statement;
+import java.util.List;
+import javax.annotation.Nullable;
import org.sonar.duplications.CodeFragment;
import org.sonar.duplications.token.Token;
-import java.util.List;
-
public class Statement implements CodeFragment {
private final int startLine;
this.value = value;
}
- public Statement(List<Token> tokens) {
+ public Statement(@Nullable List<Token> tokens) {
if (tokens == null || tokens.isEmpty()) {
throw new IllegalArgumentException("A statement can't be initialized with an empty list of tokens");
}
import java.util.ArrayList;
import java.util.List;
-
+import javax.annotation.Nullable;
import org.sonar.duplications.statement.matcher.TokenMatcher;
import org.sonar.duplications.token.Token;
import org.sonar.duplications.token.TokenQueue;
private final TokenMatcher[] tokenMatchers;
private final boolean blackHole;
+ private StatementChannel(boolean blackHole, @Nullable TokenMatcher... tokenMatchers) {
+ if (tokenMatchers == null || tokenMatchers.length == 0) {
+ throw new IllegalArgumentException();
+ }
+ this.blackHole = blackHole;
+ this.tokenMatchers = tokenMatchers;
+ }
+
public static StatementChannel create(TokenMatcher... tokenMatchers) {
return new StatementChannel(false, tokenMatchers);
}
return new StatementChannel(true, tokenMatchers);
}
- private StatementChannel(boolean blackHole, TokenMatcher... tokenMatchers) {
- if (tokenMatchers == null || tokenMatchers.length == 0) {
- throw new IllegalArgumentException();
- }
- this.blackHole = blackHole;
- this.tokenMatchers = tokenMatchers;
- }
-
public boolean consume(TokenQueue tokenQueue, List<Statement> output) {
List<Token> matchedTokenList = new ArrayList<>();
for (TokenMatcher tokenMatcher : tokenMatchers) {
import java.util.ArrayList;
import java.util.List;
-
+import javax.annotation.Nullable;
import org.sonar.duplications.DuplicationsException;
import org.sonar.duplications.statement.matcher.TokenMatcher;
import org.sonar.duplications.token.TokenQueue;
private final StatementChannelDisptacher channelDispatcher;
- public static Builder builder() {
- return new Builder();
- }
-
private StatementChunker(Builder builder) {
this.channelDispatcher = builder.getChannelDispatcher();
}
- public List<Statement> chunk(TokenQueue tokenQueue) {
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public List<Statement> chunk(@Nullable TokenQueue tokenQueue) {
if (tokenQueue == null) {
throw new IllegalArgumentException();
}
import java.util.Arrays;
import java.util.EnumMap;
import java.util.List;
+import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.annotation.Nullable;
return Result.SUCCESS;
}
- EnumMap<PropertyType, Validation> validations = createValidations(options);
- return validations.getOrDefault(type, aValue -> Result.SUCCESS).validate(value);
+ EnumMap<PropertyType, Function<String, Result>> validations = createValidations(options);
+ return validations.getOrDefault(type, aValue -> Result.SUCCESS).apply(value);
}
- private static EnumMap<PropertyType, Validation> createValidations(List<String> options) {
- return new EnumMap<>(ImmutableMap.<PropertyType, Validation>builder()
+ private static EnumMap<PropertyType, Function<String, Result>> createValidations(List<String> options) {
+ return new EnumMap<>(ImmutableMap.<PropertyType, Function<String, Result>>builder()
.put(BOOLEAN, validateBoolean())
.put(INTEGER, validateInteger())
.put(LONG, validateInteger())
.build());
}
- private static Validation validateBoolean() {
+ private static Function<String, Result> validateBoolean() {
return value -> {
if (!StringUtils.equalsIgnoreCase(value, "true") && !StringUtils.equalsIgnoreCase(value, "false")) {
return Result.newError("notBoolean");
};
}
- private static Validation validateInteger() {
+ private static Function<String, Result> validateInteger() {
return value -> {
if (!NumberUtils.isDigits(value)) {
return Result.newError("notInteger");
};
}
- private static Validation validateFloat() {
+ private static Function<String, Result> validateFloat() {
return value -> {
try {
Double.parseDouble(value);
};
}
- private static Validation validateRegexp() {
+ private static Function<String, Result> validateRegexp() {
return value -> {
try {
Pattern.compile(value);
}
}
}
-
- @FunctionalInterface
- private interface Validation {
- Result validate(String value);
- }
}
*/
package org.sonar.api.measures;
-import com.google.common.base.Function;
-import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
+import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
/**
* Used to define a metric in a plugin. Should be used with {@link Metrics} extension point.
- * Should no more be used on scanner side. Use {@link org.sonar.api.batch.measure.Metric} instead.
+ * Should no more be used on scanner side. Use {@link org.sonar.api.batch.measure.Metric} instead.
*/
@ScannerSide
@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
/**
* The maximum supported value of scale for decimal metrics
+ *
* @since 5.3
*/
public static final int MAX_DECIMAL_SCALE = 5;
public enum Level {
OK("Green"), WARN("Orange"), ERROR("Red");
- private static final List<String> NAMES = Lists.transform(Arrays.asList(values()), new Function<Level, String>() {
- @Nonnull
- @Override
- public String apply(@Nonnull Level level) {
- return level.name();
- }
- });
+ private static final List<String> NAMES = Arrays.stream(values())
+ .map(Level::name)
+ .collect(Collectors.toList());
private String colorName;
/**
* Return the number scale if metric type is {@link ValueType#FLOAT}, else {@code null}
+ *
* @since 5.3
*/
@CheckForNull
* </ul>
* Metric.DIRECTION_NONE is the default value.
*
+ * @param d the direction
+ * @return the builder
* @see Metric#DIRECTION_WORST
* @see Metric#DIRECTION_BETTER
* @see Metric#DIRECTION_NONE
- *
- * @param d the direction
- * @return the builder
*/
public Builder setDirection(Integer d) {
this.direction = d;
*
* @param f the formula
* @return the builder
- *
* @deprecated since 5.2, it's no more possible to define a formula on a metric, please use {@link org.sonar.api.ce.measure.MeasureComputer} instead
*/
@Deprecated
/**
* Specifies whether this metric can be edited online in the "Manual measures" page. Default is false.
*
- * @since 2.10
- *
* @param b true if the metric can be edited online.
* @return the builder
+ * @since 2.10
*/
public Builder setUserManaged(boolean b) {
this.userManaged = b;
* <br>
* By default, historical data are kept.
*
- * @since 2.14
- *
* @param b true if measures from the past can be deleted automatically.
* @return the builder
+ * @since 2.14
*/
public Builder setDeleteHistoricalData(boolean b) {
this.deleteHistoricalData = b;
/**
* Scale to be used if the metric has decimal type ({@link ValueType#FLOAT} or {@link ValueType#PERCENT}).
* Default is 1. It is not set (({@code null}) on non-decimal metrics.
+ *
* @since 5.3
*/
public Builder setDecimalScale(int scale) {
* @return the target directory
*/
public static File unzip(File zip, File toDir) throws IOException {
- return unzip(zip, toDir, (Predicate<ZipEntry>)ze -> true);
+ return unzip(zip, toDir, (Predicate<ZipEntry>) ze -> true);
}
public static File unzip(InputStream zip, File toDir) throws IOException {
- return unzip(zip, toDir, (Predicate<ZipEntry>)ze -> true);
+ return unzip(zip, toDir, (Predicate<ZipEntry>) ze -> true);
}
/**
/**
* Unzip a file to a directory.
+ *
* @param stream the zip input file
- * @param toDir the target directory. It is created if needed.
+ * @param toDir the target directory. It is created if needed.
* @param filter filter zip entries so that only a subset of directories/files can be
* extracted to target directory.
* @return the parameter {@code toDir}
ZipEntry entry;
while ((entry = zipStream.getNextEntry()) != null) {
if (filter.test(entry)) {
- File to = new File(toDir, entry.getName());
- if (entry.isDirectory()) {
- throwExceptionIfDirectoryIsNotCreatable(to);
- } else {
- File parent = to.getParentFile();
- throwExceptionIfDirectoryIsNotCreatable(parent);
- copy(zipStream, to);
- }
+ unzipEntry(entry, zipStream, toDir);
}
}
return toDir;
}
}
+ private static void unzipEntry(ZipEntry entry, ZipInputStream zipStream, File toDir) throws IOException {
+ File to = new File(toDir, entry.getName());
+ if (entry.isDirectory()) {
+ throwExceptionIfDirectoryIsNotCreatable(to);
+ } else {
+ File parent = to.getParentFile();
+ throwExceptionIfDirectoryIsNotCreatable(parent);
+ copy(zipStream, to);
+ }
+ }
+
private static void throwExceptionIfDirectoryIsNotCreatable(File to) throws IOException {
if (!to.exists() && !to.mkdirs()) {
throw new IOException(ERROR_CREATING_DIRECTORY + to);
/**
* Unzip a file to a directory.
- * @param zip the zip file. It must exist.
- * @param toDir the target directory. It is created if needed.
+ *
+ * @param zip the zip file. It must exist.
+ * @param toDir the target directory. It is created if needed.
* @param filter filter zip entries so that only a subset of directories/files can be
* extracted to target directory.
* @return the parameter {@code toDir}
}
/**
- * @deprecated replaced by {@link Predicate<ZipEntry>} in 6.2.
* @see #unzip(File, File, Predicate)
+ * @deprecated replaced by {@link Predicate<ZipEntry>} in 6.2.
*/
@Deprecated
@FunctionalInterface
*/
package org.sonar.api.server.ws.internal;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-
import java.io.InputStream;
import org.junit.Test;
import org.sonar.api.server.ws.Request;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.entry;
+import static org.mockito.Mockito.mock;
+
public class SimpleGetRequestTest {
SimpleGetRequest underTest = new SimpleGetRequest();
assertThat(underTest.paramAsPart("unknown")).isNull();
}
+
+ @Test
+ public void getMediaType() {
+ underTest.setMediaType("JSON");
+
+ assertThat(underTest.getMediaType()).isEqualTo("JSON");
+ }
+
+ @Test
+ public void multiParam_with_one_element() {
+ underTest.setParam("foo", "bar");
+
+ assertThat(underTest.multiParam("foo")).containsExactly("bar");
+ }
+
+ @Test
+ public void multiParam_without_any_element() {
+ assertThat(underTest.multiParam("42")).isEmpty();
+ }
+
+ @Test
+ public void getParams() {
+ underTest
+ .setParam("foo", "bar")
+ .setParam("fee", "beer");
+
+ assertThat(underTest.getParams()).containsOnly(entry("foo", "bar"), entry("fee", "beer"));
+ }
}