+++ /dev/null
-/*
- * 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.core.config;
-
-import com.google.common.annotations.VisibleForTesting;
-import java.io.IOException;
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.function.Function;
-import org.apache.commons.csv.CSVFormat;
-import org.apache.commons.csv.CSVParser;
-import org.apache.commons.csv.CSVRecord;
-import org.apache.commons.lang.ArrayUtils;
-
-public class MultivalueProperty {
- private MultivalueProperty() {
- // prevents instantiation
- }
-
- public static String[] parseAsCsv(String key, String value) {
- return parseAsCsv(key, value, Function.identity());
- }
-
- public static String[] parseAsCsv(String key, String value, Function<String, String> valueProcessor) {
- String cleanValue = MultivalueProperty.trimFieldsAndRemoveEmptyFields(value);
- List<String> result = new ArrayList<>();
- try (CSVParser csvParser = CSVFormat.RFC4180
- .withHeader((String) null)
- .withIgnoreEmptyLines()
- .withIgnoreSurroundingSpaces()
- .parse(new StringReader(cleanValue))) {
- List<CSVRecord> records = csvParser.getRecords();
- if (records.isEmpty()) {
- return ArrayUtils.EMPTY_STRING_ARRAY;
- }
- processRecords(result, records, valueProcessor);
- return result.toArray(new String[result.size()]);
- } catch (IOException e) {
- throw new IllegalStateException("Property: '" + key + "' doesn't contain a valid CSV value: '" + value + "'", e);
- }
- }
-
- /**
- * In most cases we expect a single record. <br>Having multiple records means the input value was splitted over multiple lines (this is common in Maven).
- * For example:
- * <pre>
- * <sonar.exclusions>
- * src/foo,
- * src/bar,
- * src/biz
- * <sonar.exclusions>
- * </pre>
- * In this case records will be merged to form a single list of items. Last item of a record is appended to first item of next record.
- * <p>
- * This is a very curious case, but we try to preserve line break in the middle of an item:
- * <pre>
- * <sonar.exclusions>
- * a
- * b,
- * c
- * <sonar.exclusions>
- * </pre>
- * will produce ['a\nb', 'c']
- */
- private static void processRecords(List<String> result, List<CSVRecord> records, Function<String, String> valueProcessor) {
- for (CSVRecord csvRecord : records) {
- Iterator<String> it = csvRecord.iterator();
- if (!result.isEmpty()) {
- String next = it.next();
- if (!next.isEmpty()) {
- int lastItemIdx = result.size() - 1;
- String previous = result.get(lastItemIdx);
- if (previous.isEmpty()) {
- result.set(lastItemIdx, valueProcessor.apply(next));
- } else {
- result.set(lastItemIdx, valueProcessor.apply(previous + "\n" + next));
- }
- }
- }
- it.forEachRemaining(s -> {
- String apply = valueProcessor.apply(s);
- result.add(apply);
- });
- }
- }
-
- /**
- * Removes the empty fields from the value of a multi-value property from empty fields, including trimming each field.
- * <p>
- * Quotes can be used to prevent an empty field to be removed (as it is used to preserve empty spaces).
- * <ul>
- * <li>{@code "" => ""}</li>
- * <li>{@code " " => ""}</li>
- * <li>{@code "," => ""}</li>
- * <li>{@code ",," => ""}</li>
- * <li>{@code ",,," => ""}</li>
- * <li>{@code ",a" => "a"}</li>
- * <li>{@code "a," => "a"}</li>
- * <li>{@code ",a," => "a"}</li>
- * <li>{@code "a,,b" => "a,b"}</li>
- * <li>{@code "a, ,b" => "a,b"}</li>
- * <li>{@code "a,\"\",b" => "a,b"}</li>
- * <li>{@code "\"a\",\"b\"" => "\"a\",\"b\""}</li>
- * <li>{@code "\" a \",\"b \"" => "\" a \",\"b \""}</li>
- * <li>{@code "\"a\",\"\",\"b\"" => "\"a\",\"\",\"b\""}</li>
- * <li>{@code "\"a\",\" \",\"b\"" => "\"a\",\" \",\"b\""}</li>
- * <li>{@code "\" a,,b,c \",\"d \"" => "\" a,,b,c \",\"d \""}</li>
- * <li>{@code "a,\" \",b" => "ab"]}</li>
- * </ul>
- */
- @VisibleForTesting
- static String trimFieldsAndRemoveEmptyFields(String str) {
- char[] chars = str.toCharArray();
- char[] res = new char[chars.length];
- /*
- * set when reading the first non trimmable char after a separator char (or the beginning of the string)
- * unset when reading a separator
- */
- boolean inField = false;
- boolean inQuotes = false;
- int i = 0;
- int resI = 0;
- for (; i < chars.length; i++) {
- boolean isSeparator = chars[i] == ',';
- if (!inQuotes && isSeparator) {
- // exiting field (may already be unset)
- inField = false;
- if (resI > 0) {
- resI = retroTrim(res, resI);
- }
- } else {
- boolean isTrimmed = !inQuotes && istrimmable(chars[i]);
- if (isTrimmed && !inField) {
- // we haven't meet any non trimmable char since the last separator yet
- continue;
- }
-
- boolean isEscape = isEscapeChar(chars[i]);
- if (isEscape) {
- inQuotes = !inQuotes;
- }
-
- // add separator as we already had one field
- if (!inField && resI > 0) {
- res[resI] = ',';
- resI++;
- }
-
- // register in field (may already be set)
- inField = true;
- // copy current char
- res[resI] = chars[i];
- resI++;
- }
- }
- // inQuotes can only be true at this point if quotes are unbalanced
- if (!inQuotes) {
- // trim end of str
- resI = retroTrim(res, resI);
- }
- return new String(res, 0, resI);
- }
-
- private static boolean isEscapeChar(char aChar) {
- return aChar == '"';
- }
-
- private static boolean istrimmable(char aChar) {
- return aChar <= ' ';
- }
-
- /**
- * Reads from index {@code resI} to the beginning into {@code res} looking up the location of the trimmable char with
- * the lowest index before encountering a non-trimmable char.
- * <p>
- * This basically trims {@code res} from any trimmable char at its end.
- *
- * @return index of next location to put new char in res
- */
- private static int retroTrim(char[] res, int resI) {
- int i = resI;
- while (i >= 1) {
- if (!istrimmable(res[i - 1])) {
- return i;
- }
- i--;
- }
- return i;
- }
-
-}
+++ /dev/null
-/*
- * 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.core.config;
-
-import com.tngtech.java.junit.dataprovider.DataProvider;
-import com.tngtech.java.junit.dataprovider.DataProviderRunner;
-import com.tngtech.java.junit.dataprovider.UseDataProvider;
-import java.util.Random;
-import java.util.function.Function;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.runner.RunWith;
-
-import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.core.config.MultivalueProperty.parseAsCsv;
-import static org.sonar.core.config.MultivalueProperty.trimFieldsAndRemoveEmptyFields;
-
-@RunWith(DataProviderRunner.class)
-public class MultivaluePropertyTest {
- private static final String[] EMPTY_STRING_ARRAY = {};
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- @Test
- @UseDataProvider("testParseAsCsv")
- public void parseAsCsv_for_coverage(String value, String[] expected) {
- // parseAsCsv is extensively tested in org.sonar.server.config.ConfigurationProviderTest
- assertThat(parseAsCsv("key", value))
- .isEqualTo(parseAsCsv("key", value, Function.identity()))
- .isEqualTo(expected);
- }
-
- @Test
- public void parseAsCsv_fails_with_ISE_if_value_can_not_be_parsed() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Property: 'multi' doesn't contain a valid CSV value: '\"a ,b'");
-
- parseAsCsv("multi", "\"a ,b");
- }
-
- @DataProvider
- public static Object[][] testParseAsCsv() {
- return new Object[][] {
- {"a", arrayOf("a")},
- {" a", arrayOf("a")},
- {"a ", arrayOf("a")},
- {" a, b", arrayOf("a", "b")},
- {"a,b ", arrayOf("a", "b")},
- {"a,,,b,c,,d", arrayOf("a", "b", "c", "d")},
- {" , \n ,, \t", EMPTY_STRING_ARRAY},
- {"\" a\"", arrayOf(" a")},
- {"\",\"", arrayOf(",")},
- // escaped quote in quoted field
- {"\"\"\"\"", arrayOf("\"")}
- };
- }
-
- private static String[] arrayOf(String... strs) {
- return strs;
- }
-
- @Test
- public void trimFieldsAndRemoveEmptyFields_throws_NPE_if_arg_is_null() {
- expectedException.expect(NullPointerException.class);
-
- trimFieldsAndRemoveEmptyFields(null);
- }
-
- @Test
- @UseDataProvider("plains")
- public void trimFieldsAndRemoveEmptyFields_ignores_EmptyFields(String str) {
- assertThat(trimFieldsAndRemoveEmptyFields("")).isEqualTo("");
- assertThat(trimFieldsAndRemoveEmptyFields(str)).isEqualTo(str);
-
- assertThat(trimFieldsAndRemoveEmptyFields(',' + str)).isEqualTo(str);
- assertThat(trimFieldsAndRemoveEmptyFields(str + ',')).isEqualTo(str);
- assertThat(trimFieldsAndRemoveEmptyFields(",,," + str)).isEqualTo(str);
- assertThat(trimFieldsAndRemoveEmptyFields(str + ",,,")).isEqualTo(str);
-
- assertThat(trimFieldsAndRemoveEmptyFields(str + ',' + str)).isEqualTo(str + ',' + str);
- assertThat(trimFieldsAndRemoveEmptyFields(str + ",,," + str)).isEqualTo(str + ',' + str);
- assertThat(trimFieldsAndRemoveEmptyFields(',' + str + ',' + str)).isEqualTo(str + ',' + str);
- assertThat(trimFieldsAndRemoveEmptyFields("," + str + ",,," + str)).isEqualTo(str + ',' + str);
- assertThat(trimFieldsAndRemoveEmptyFields(",,," + str + ",,," + str)).isEqualTo(str + ',' + str);
-
- assertThat(trimFieldsAndRemoveEmptyFields(str + ',' + str + ',')).isEqualTo(str + ',' + str);
- assertThat(trimFieldsAndRemoveEmptyFields(str + ",,," + str + ",")).isEqualTo(str + ',' + str);
- assertThat(trimFieldsAndRemoveEmptyFields(str + ",,," + str + ",,")).isEqualTo(str + ',' + str);
-
- assertThat(trimFieldsAndRemoveEmptyFields(',' + str + ',' + str + ',')).isEqualTo(str + ',' + str);
- assertThat(trimFieldsAndRemoveEmptyFields(",," + str + ',' + str + ',')).isEqualTo(str + ',' + str);
- assertThat(trimFieldsAndRemoveEmptyFields(',' + str + ",," + str + ',')).isEqualTo(str + ',' + str);
- assertThat(trimFieldsAndRemoveEmptyFields(',' + str + ',' + str + ",,")).isEqualTo(str + ',' + str);
- assertThat(trimFieldsAndRemoveEmptyFields(",,," + str + ",,," + str + ",,")).isEqualTo(str + ',' + str);
-
- assertThat(trimFieldsAndRemoveEmptyFields(str + ',' + str + ',' + str)).isEqualTo(str + ',' + str + ',' + str);
- assertThat(trimFieldsAndRemoveEmptyFields(str + ',' + str + ',' + str)).isEqualTo(str + ',' + str + ',' + str);
- }
-
- @DataProvider
- public static Object[][] plains() {
- return new Object[][] {
- {randomAlphanumeric(1)},
- {randomAlphanumeric(2)},
- {randomAlphanumeric(3 + new Random().nextInt(5))}
- };
- }
-
- @Test
- @UseDataProvider("emptyAndtrimmable")
- public void trimFieldsAndRemoveEmptyFields_ignores_empty_fields_and_trims_fields(String empty, String trimmable) {
- String expected = trimmable.trim();
- assertThat(empty.trim()).isEmpty();
-
- assertThat(trimFieldsAndRemoveEmptyFields(trimmable)).isEqualTo(expected);
- assertThat(trimFieldsAndRemoveEmptyFields(trimmable + ',' + empty)).isEqualTo(expected);
- assertThat(trimFieldsAndRemoveEmptyFields(trimmable + ",," + empty)).isEqualTo(expected);
- assertThat(trimFieldsAndRemoveEmptyFields(empty + ',' + trimmable)).isEqualTo(expected);
- assertThat(trimFieldsAndRemoveEmptyFields(empty + ",," + trimmable)).isEqualTo(expected);
- assertThat(trimFieldsAndRemoveEmptyFields(empty + ',' + trimmable + ',' + empty)).isEqualTo(expected);
- assertThat(trimFieldsAndRemoveEmptyFields(empty + ",," + trimmable + ",,," + empty)).isEqualTo(expected);
-
- assertThat(trimFieldsAndRemoveEmptyFields(trimmable + ',' + empty + ',' + empty)).isEqualTo(expected);
- assertThat(trimFieldsAndRemoveEmptyFields(trimmable + ",," + empty + ",,," + empty)).isEqualTo(expected);
-
- assertThat(trimFieldsAndRemoveEmptyFields(empty + ',' + empty + ',' + trimmable)).isEqualTo(expected);
- assertThat(trimFieldsAndRemoveEmptyFields(empty + ",,,," + empty + ",," + trimmable)).isEqualTo(expected);
-
- assertThat(trimFieldsAndRemoveEmptyFields(trimmable + ',' + trimmable)).isEqualTo(expected + ',' + expected);
- assertThat(trimFieldsAndRemoveEmptyFields(trimmable + ',' + trimmable + ',' + trimmable)).isEqualTo(expected + ',' + expected + ',' + expected);
- assertThat(trimFieldsAndRemoveEmptyFields(trimmable + "," + trimmable + ',' + trimmable)).isEqualTo(expected + ',' + expected + ',' + expected);
- }
-
- @Test
- public void trimAccordingToStringTrim() {
- String str = randomAlphanumeric(4);
- for (int i = 0; i <= ' '; i++) {
- String prefixed = (char) i + str;
- String suffixed = (char) i + str;
- String both = (char) i + str + (char) i;
- assertThat(trimFieldsAndRemoveEmptyFields(prefixed)).isEqualTo(prefixed.trim());
- assertThat(trimFieldsAndRemoveEmptyFields(suffixed)).isEqualTo(suffixed.trim());
- assertThat(trimFieldsAndRemoveEmptyFields(both)).isEqualTo(both.trim());
- }
- }
-
- @DataProvider
- public static Object[][] emptyAndtrimmable() {
- Random random = new Random();
- String oneEmpty = randomTrimmedChars(1, random);
- String twoEmpty = randomTrimmedChars(2, random);
- String threePlusEmpty = randomTrimmedChars(3 + random.nextInt(5), random);
- String onePlusEmpty = randomTrimmedChars(1 + random.nextInt(5), random);
-
- String plain = randomAlphanumeric(1);
- String plainWithtrimmable = randomAlphanumeric(2) + onePlusEmpty + randomAlphanumeric(3);
- String quotedWithSeparator = '"' + randomAlphanumeric(3) + ',' + randomAlphanumeric(2) + '"';
- String quotedWithDoubleSeparator = '"' + randomAlphanumeric(3) + ",," + randomAlphanumeric(2) + '"';
- String quotedWithtrimmable = '"' + randomAlphanumeric(3) + onePlusEmpty + randomAlphanumeric(2) + '"';
-
- String[] empties = {oneEmpty, twoEmpty, threePlusEmpty};
- String[] strings = {plain, plainWithtrimmable,
- onePlusEmpty + plain, plain + onePlusEmpty, onePlusEmpty + plain + onePlusEmpty,
- onePlusEmpty + plainWithtrimmable, plainWithtrimmable + onePlusEmpty, onePlusEmpty + plainWithtrimmable + onePlusEmpty,
- onePlusEmpty + quotedWithSeparator, quotedWithSeparator + onePlusEmpty, onePlusEmpty + quotedWithSeparator + onePlusEmpty,
- onePlusEmpty + quotedWithDoubleSeparator, quotedWithDoubleSeparator + onePlusEmpty, onePlusEmpty + quotedWithDoubleSeparator + onePlusEmpty,
- onePlusEmpty + quotedWithtrimmable, quotedWithtrimmable + onePlusEmpty, onePlusEmpty + quotedWithtrimmable + onePlusEmpty
- };
-
- Object[][] res = new Object[empties.length * strings.length][2];
- int i = 0;
- for (String empty : empties) {
- for (String string : strings) {
- res[i][0] = empty;
- res[i][1] = string;
- i++;
- }
- }
- return res;
- }
-
- @Test
- @UseDataProvider("emptys")
- public void trimFieldsAndRemoveEmptyFields_quotes_allow_to_preserve_fields(String empty) {
- String quotedEmpty = '"' + empty + '"';
-
- assertThat(trimFieldsAndRemoveEmptyFields(quotedEmpty)).isEqualTo(quotedEmpty);
- assertThat(trimFieldsAndRemoveEmptyFields(',' + quotedEmpty)).isEqualTo(quotedEmpty);
- assertThat(trimFieldsAndRemoveEmptyFields(quotedEmpty + ',')).isEqualTo(quotedEmpty);
- assertThat(trimFieldsAndRemoveEmptyFields(',' + quotedEmpty + ',')).isEqualTo(quotedEmpty);
-
- assertThat(trimFieldsAndRemoveEmptyFields(quotedEmpty + ',' + quotedEmpty)).isEqualTo(quotedEmpty + ',' + quotedEmpty);
- assertThat(trimFieldsAndRemoveEmptyFields(quotedEmpty + ",," + quotedEmpty)).isEqualTo(quotedEmpty + ',' + quotedEmpty);
-
- assertThat(trimFieldsAndRemoveEmptyFields(quotedEmpty + ',' + quotedEmpty + ',' + quotedEmpty)).isEqualTo(quotedEmpty + ',' + quotedEmpty + ',' + quotedEmpty);
- }
-
- @DataProvider
- public static Object[][] emptys() {
- Random random = new Random();
- return new Object[][] {
- {randomTrimmedChars(1, random)},
- {randomTrimmedChars(2, random)},
- {randomTrimmedChars(3 + random.nextInt(5), random)}
- };
- }
-
- @Test
- public void trimFieldsAndRemoveEmptyFields_supports_escaped_quote_in_quotes() {
- assertThat(trimFieldsAndRemoveEmptyFields("\"f\"\"oo\"")).isEqualTo("\"f\"\"oo\"");
- assertThat(trimFieldsAndRemoveEmptyFields("\"f\"\"oo\",\"bar\"\"\"")).isEqualTo("\"f\"\"oo\",\"bar\"\"\"");
- }
-
- @Test
- public void trimFieldsAndRemoveEmptyFields_does_not_fail_on_unbalanced_quotes() {
- assertThat(trimFieldsAndRemoveEmptyFields("\"")).isEqualTo("\"");
- assertThat(trimFieldsAndRemoveEmptyFields("\"foo")).isEqualTo("\"foo");
- assertThat(trimFieldsAndRemoveEmptyFields("foo\"")).isEqualTo("foo\"");
-
- assertThat(trimFieldsAndRemoveEmptyFields("\"foo\",\"")).isEqualTo("\"foo\",\"");
- assertThat(trimFieldsAndRemoveEmptyFields("\",\"foo\"")).isEqualTo("\",\"foo\"");
-
- assertThat(trimFieldsAndRemoveEmptyFields("\"foo\",\", ")).isEqualTo("\"foo\",\", ");
-
- assertThat(trimFieldsAndRemoveEmptyFields(" a ,,b , c, \"foo\",\" ")).isEqualTo("a,b,c,\"foo\",\" ");
- assertThat(trimFieldsAndRemoveEmptyFields("\" a ,,b , c, ")).isEqualTo("\" a ,,b , c, ");
- }
-
- private static final char[] SOME_PRINTABLE_TRIMMABLE_CHARS = {
- ' ', '\t', '\n', '\r'
- };
-
- /**
- * Result of randomTrimmedChars being used as arguments to JUnit test method through the DataProvider feature, they
- * are printed to surefire report. Some of those chars breaks the parsing of the surefire report during sonar analysis.
- * Therefor, we only use a subset of the trimmable chars.
- */
- private static String randomTrimmedChars(int length, Random random) {
- char[] chars = new char[length];
- for (int i = 0; i < chars.length; i++) {
- chars[i] = SOME_PRINTABLE_TRIMMABLE_CHARS[random.nextInt(SOME_PRINTABLE_TRIMMABLE_CHARS.length)];
- }
- return new String(chars);
- }
-}
compile 'commons-lang:commons-lang'
compile 'com.google.code.gson:gson'
compile 'com.google.guava:guava'
+ compile 'org.apache.commons:commons-csv'
// shaded, but not relocated
compile project(':sonar-check-api')
import java.io.File;
import java.io.IOException;
+import java.nio.charset.Charset;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.concurrent.Immutable;
+import org.apache.commons.lang.StringUtils;
+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 LOG = Loggers.get(AbstractProjectOrModule.class);
+
private final Path baseDir;
private final Path workDir;
private final String name;
private final String key;
private final ProjectDefinition definition;
-
- /**
- * For testing only!
- */
- public AbstractProjectOrModule(ProjectDefinition definition) {
- this(definition, TestInputFileBuilder.nextBatchId());
- }
+ private final Charset encoding;
public AbstractProjectOrModule(ProjectDefinition definition, int scannerComponentId) {
super(scannerComponentId);
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) {
public String getDescription() {
return description;
}
-
+
+ public Charset getEncoding() {
+ return encoding;
+ }
}
*/
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 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 org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
+
+import static org.sonar.api.config.internal.MultivalueProperty.parseAsCsv;
@Immutable
public class DefaultInputModule extends AbstractProjectOrModule implements InputModule {
+ private static final Logger LOG = Loggers.get(DefaultInputModule.class);
+ private final List<Path> sourceDirsOrFiles;
+ private final List<Path> testDirsOrFiles;
+
+ /**
+ * For testing only!
+ */
public DefaultInputModule(ProjectDefinition definition) {
- super(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);
+ }
+
+ private List<Path> initSources(ProjectDefinition module, String propertyKey) {
+ 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 List<Path> getSourceDirsOrFiles() {
+ return sourceDirsOrFiles;
+ }
+
+ public List<Path> getTestDirsOrFiles() {
+ return testDirsOrFiles;
}
}
@Immutable
public class DefaultInputProject extends AbstractProjectOrModule implements InputProject {
+ /**
+ * For testing only!
+ */
public DefaultInputProject(ProjectDefinition definition) {
- super(definition);
+ super(definition, 0);
}
public DefaultInputProject(ProjectDefinition definition, int scannerComponentId) {
--- /dev/null
+/*
+ * 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.config.internal;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.function.Function;
+import org.apache.commons.csv.CSVFormat;
+import org.apache.commons.csv.CSVParser;
+import org.apache.commons.csv.CSVRecord;
+import org.apache.commons.lang.ArrayUtils;
+
+public class MultivalueProperty {
+ private MultivalueProperty() {
+ // prevents instantiation
+ }
+
+ public static String[] parseAsCsv(String key, String value) {
+ return parseAsCsv(key, value, Function.identity());
+ }
+
+ public static String[] parseAsCsv(String key, String value, Function<String, String> valueProcessor) {
+ String cleanValue = MultivalueProperty.trimFieldsAndRemoveEmptyFields(value);
+ List<String> result = new ArrayList<>();
+ try (CSVParser csvParser = CSVFormat.RFC4180
+ .withHeader((String) null)
+ .withIgnoreEmptyLines()
+ .withIgnoreSurroundingSpaces()
+ .parse(new StringReader(cleanValue))) {
+ List<CSVRecord> records = csvParser.getRecords();
+ if (records.isEmpty()) {
+ return ArrayUtils.EMPTY_STRING_ARRAY;
+ }
+ processRecords(result, records, valueProcessor);
+ return result.toArray(new String[result.size()]);
+ } catch (IOException e) {
+ throw new IllegalStateException("Property: '" + key + "' doesn't contain a valid CSV value: '" + value + "'", e);
+ }
+ }
+
+ /**
+ * In most cases we expect a single record. <br>Having multiple records means the input value was splitted over multiple lines (this is common in Maven).
+ * For example:
+ * <pre>
+ * <sonar.exclusions>
+ * src/foo,
+ * src/bar,
+ * src/biz
+ * <sonar.exclusions>
+ * </pre>
+ * In this case records will be merged to form a single list of items. Last item of a record is appended to first item of next record.
+ * <p>
+ * This is a very curious case, but we try to preserve line break in the middle of an item:
+ * <pre>
+ * <sonar.exclusions>
+ * a
+ * b,
+ * c
+ * <sonar.exclusions>
+ * </pre>
+ * will produce ['a\nb', 'c']
+ */
+ private static void processRecords(List<String> result, List<CSVRecord> records, Function<String, String> valueProcessor) {
+ for (CSVRecord csvRecord : records) {
+ Iterator<String> it = csvRecord.iterator();
+ if (!result.isEmpty()) {
+ String next = it.next();
+ if (!next.isEmpty()) {
+ int lastItemIdx = result.size() - 1;
+ String previous = result.get(lastItemIdx);
+ if (previous.isEmpty()) {
+ result.set(lastItemIdx, valueProcessor.apply(next));
+ } else {
+ result.set(lastItemIdx, valueProcessor.apply(previous + "\n" + next));
+ }
+ }
+ }
+ it.forEachRemaining(s -> {
+ String apply = valueProcessor.apply(s);
+ result.add(apply);
+ });
+ }
+ }
+
+ /**
+ * Removes the empty fields from the value of a multi-value property from empty fields, including trimming each field.
+ * <p>
+ * Quotes can be used to prevent an empty field to be removed (as it is used to preserve empty spaces).
+ * <ul>
+ * <li>{@code "" => ""}</li>
+ * <li>{@code " " => ""}</li>
+ * <li>{@code "," => ""}</li>
+ * <li>{@code ",," => ""}</li>
+ * <li>{@code ",,," => ""}</li>
+ * <li>{@code ",a" => "a"}</li>
+ * <li>{@code "a," => "a"}</li>
+ * <li>{@code ",a," => "a"}</li>
+ * <li>{@code "a,,b" => "a,b"}</li>
+ * <li>{@code "a, ,b" => "a,b"}</li>
+ * <li>{@code "a,\"\",b" => "a,b"}</li>
+ * <li>{@code "\"a\",\"b\"" => "\"a\",\"b\""}</li>
+ * <li>{@code "\" a \",\"b \"" => "\" a \",\"b \""}</li>
+ * <li>{@code "\"a\",\"\",\"b\"" => "\"a\",\"\",\"b\""}</li>
+ * <li>{@code "\"a\",\" \",\"b\"" => "\"a\",\" \",\"b\""}</li>
+ * <li>{@code "\" a,,b,c \",\"d \"" => "\" a,,b,c \",\"d \""}</li>
+ * <li>{@code "a,\" \",b" => "ab"]}</li>
+ * </ul>
+ */
+ @VisibleForTesting
+ static String trimFieldsAndRemoveEmptyFields(String str) {
+ char[] chars = str.toCharArray();
+ char[] res = new char[chars.length];
+ /*
+ * set when reading the first non trimmable char after a separator char (or the beginning of the string)
+ * unset when reading a separator
+ */
+ boolean inField = false;
+ boolean inQuotes = false;
+ int i = 0;
+ int resI = 0;
+ for (; i < chars.length; i++) {
+ boolean isSeparator = chars[i] == ',';
+ if (!inQuotes && isSeparator) {
+ // exiting field (may already be unset)
+ inField = false;
+ if (resI > 0) {
+ resI = retroTrim(res, resI);
+ }
+ } else {
+ boolean isTrimmed = !inQuotes && istrimmable(chars[i]);
+ if (isTrimmed && !inField) {
+ // we haven't meet any non trimmable char since the last separator yet
+ continue;
+ }
+
+ boolean isEscape = isEscapeChar(chars[i]);
+ if (isEscape) {
+ inQuotes = !inQuotes;
+ }
+
+ // add separator as we already had one field
+ if (!inField && resI > 0) {
+ res[resI] = ',';
+ resI++;
+ }
+
+ // register in field (may already be set)
+ inField = true;
+ // copy current char
+ res[resI] = chars[i];
+ resI++;
+ }
+ }
+ // inQuotes can only be true at this point if quotes are unbalanced
+ if (!inQuotes) {
+ // trim end of str
+ resI = retroTrim(res, resI);
+ }
+ return new String(res, 0, resI);
+ }
+
+ private static boolean isEscapeChar(char aChar) {
+ return aChar == '"';
+ }
+
+ private static boolean istrimmable(char aChar) {
+ return aChar <= ' ';
+ }
+
+ /**
+ * Reads from index {@code resI} to the beginning into {@code res} looking up the location of the trimmable char with
+ * the lowest index before encountering a non-trimmable char.
+ * <p>
+ * This basically trims {@code res} from any trimmable char at its end.
+ *
+ * @return index of next location to put new char in res
+ */
+ private static int retroTrim(char[] res, int resI) {
+ int i = resI;
+ while (i >= 1) {
+ if (!istrimmable(res[i - 1])) {
+ return i;
+ }
+ i--;
+ }
+ return i;
+ }
+
+}
* runtime.
*
* @since 3.5
+ * @deprecated since 7.6
*/
+@Deprecated
@ScannerSide
public class FileExclusions {
private final Configuration settings;
--- /dev/null
+/*
+ * 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.config.internal;
+
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
+import java.util.Random;
+import java.util.function.Function;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.api.config.internal.MultivalueProperty.parseAsCsv;
+import static org.sonar.api.config.internal.MultivalueProperty.trimFieldsAndRemoveEmptyFields;
+
+@RunWith(DataProviderRunner.class)
+public class MultivaluePropertyTest {
+ private static final String[] EMPTY_STRING_ARRAY = {};
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Test
+ @UseDataProvider("testParseAsCsv")
+ public void parseAsCsv_for_coverage(String value, String[] expected) {
+ // parseAsCsv is extensively tested in org.sonar.server.config.ConfigurationProviderTest
+ assertThat(parseAsCsv("key", value))
+ .isEqualTo(parseAsCsv("key", value, Function.identity()))
+ .isEqualTo(expected);
+ }
+
+ @Test
+ public void parseAsCsv_fails_with_ISE_if_value_can_not_be_parsed() {
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Property: 'multi' doesn't contain a valid CSV value: '\"a ,b'");
+
+ parseAsCsv("multi", "\"a ,b");
+ }
+
+ @DataProvider
+ public static Object[][] testParseAsCsv() {
+ return new Object[][] {
+ {"a", arrayOf("a")},
+ {" a", arrayOf("a")},
+ {"a ", arrayOf("a")},
+ {" a, b", arrayOf("a", "b")},
+ {"a,b ", arrayOf("a", "b")},
+ {"a,,,b,c,,d", arrayOf("a", "b", "c", "d")},
+ {" , \n ,, \t", EMPTY_STRING_ARRAY},
+ {"\" a\"", arrayOf(" a")},
+ {"\",\"", arrayOf(",")},
+ // escaped quote in quoted field
+ {"\"\"\"\"", arrayOf("\"")}
+ };
+ }
+
+ private static String[] arrayOf(String... strs) {
+ return strs;
+ }
+
+ @Test
+ public void trimFieldsAndRemoveEmptyFields_throws_NPE_if_arg_is_null() {
+ expectedException.expect(NullPointerException.class);
+
+ trimFieldsAndRemoveEmptyFields(null);
+ }
+
+ @Test
+ @UseDataProvider("plains")
+ public void trimFieldsAndRemoveEmptyFields_ignores_EmptyFields(String str) {
+ assertThat(trimFieldsAndRemoveEmptyFields("")).isEqualTo("");
+ assertThat(trimFieldsAndRemoveEmptyFields(str)).isEqualTo(str);
+
+ assertThat(trimFieldsAndRemoveEmptyFields(',' + str)).isEqualTo(str);
+ assertThat(trimFieldsAndRemoveEmptyFields(str + ',')).isEqualTo(str);
+ assertThat(trimFieldsAndRemoveEmptyFields(",,," + str)).isEqualTo(str);
+ assertThat(trimFieldsAndRemoveEmptyFields(str + ",,,")).isEqualTo(str);
+
+ assertThat(trimFieldsAndRemoveEmptyFields(str + ',' + str)).isEqualTo(str + ',' + str);
+ assertThat(trimFieldsAndRemoveEmptyFields(str + ",,," + str)).isEqualTo(str + ',' + str);
+ assertThat(trimFieldsAndRemoveEmptyFields(',' + str + ',' + str)).isEqualTo(str + ',' + str);
+ assertThat(trimFieldsAndRemoveEmptyFields("," + str + ",,," + str)).isEqualTo(str + ',' + str);
+ assertThat(trimFieldsAndRemoveEmptyFields(",,," + str + ",,," + str)).isEqualTo(str + ',' + str);
+
+ assertThat(trimFieldsAndRemoveEmptyFields(str + ',' + str + ',')).isEqualTo(str + ',' + str);
+ assertThat(trimFieldsAndRemoveEmptyFields(str + ",,," + str + ",")).isEqualTo(str + ',' + str);
+ assertThat(trimFieldsAndRemoveEmptyFields(str + ",,," + str + ",,")).isEqualTo(str + ',' + str);
+
+ assertThat(trimFieldsAndRemoveEmptyFields(',' + str + ',' + str + ',')).isEqualTo(str + ',' + str);
+ assertThat(trimFieldsAndRemoveEmptyFields(",," + str + ',' + str + ',')).isEqualTo(str + ',' + str);
+ assertThat(trimFieldsAndRemoveEmptyFields(',' + str + ",," + str + ',')).isEqualTo(str + ',' + str);
+ assertThat(trimFieldsAndRemoveEmptyFields(',' + str + ',' + str + ",,")).isEqualTo(str + ',' + str);
+ assertThat(trimFieldsAndRemoveEmptyFields(",,," + str + ",,," + str + ",,")).isEqualTo(str + ',' + str);
+
+ assertThat(trimFieldsAndRemoveEmptyFields(str + ',' + str + ',' + str)).isEqualTo(str + ',' + str + ',' + str);
+ assertThat(trimFieldsAndRemoveEmptyFields(str + ',' + str + ',' + str)).isEqualTo(str + ',' + str + ',' + str);
+ }
+
+ @DataProvider
+ public static Object[][] plains() {
+ return new Object[][] {
+ {randomAlphanumeric(1)},
+ {randomAlphanumeric(2)},
+ {randomAlphanumeric(3 + new Random().nextInt(5))}
+ };
+ }
+
+ @Test
+ @UseDataProvider("emptyAndtrimmable")
+ public void trimFieldsAndRemoveEmptyFields_ignores_empty_fields_and_trims_fields(String empty, String trimmable) {
+ String expected = trimmable.trim();
+ assertThat(empty.trim()).isEmpty();
+
+ assertThat(trimFieldsAndRemoveEmptyFields(trimmable)).isEqualTo(expected);
+ assertThat(trimFieldsAndRemoveEmptyFields(trimmable + ',' + empty)).isEqualTo(expected);
+ assertThat(trimFieldsAndRemoveEmptyFields(trimmable + ",," + empty)).isEqualTo(expected);
+ assertThat(trimFieldsAndRemoveEmptyFields(empty + ',' + trimmable)).isEqualTo(expected);
+ assertThat(trimFieldsAndRemoveEmptyFields(empty + ",," + trimmable)).isEqualTo(expected);
+ assertThat(trimFieldsAndRemoveEmptyFields(empty + ',' + trimmable + ',' + empty)).isEqualTo(expected);
+ assertThat(trimFieldsAndRemoveEmptyFields(empty + ",," + trimmable + ",,," + empty)).isEqualTo(expected);
+
+ assertThat(trimFieldsAndRemoveEmptyFields(trimmable + ',' + empty + ',' + empty)).isEqualTo(expected);
+ assertThat(trimFieldsAndRemoveEmptyFields(trimmable + ",," + empty + ",,," + empty)).isEqualTo(expected);
+
+ assertThat(trimFieldsAndRemoveEmptyFields(empty + ',' + empty + ',' + trimmable)).isEqualTo(expected);
+ assertThat(trimFieldsAndRemoveEmptyFields(empty + ",,,," + empty + ",," + trimmable)).isEqualTo(expected);
+
+ assertThat(trimFieldsAndRemoveEmptyFields(trimmable + ',' + trimmable)).isEqualTo(expected + ',' + expected);
+ assertThat(trimFieldsAndRemoveEmptyFields(trimmable + ',' + trimmable + ',' + trimmable)).isEqualTo(expected + ',' + expected + ',' + expected);
+ assertThat(trimFieldsAndRemoveEmptyFields(trimmable + "," + trimmable + ',' + trimmable)).isEqualTo(expected + ',' + expected + ',' + expected);
+ }
+
+ @Test
+ public void trimAccordingToStringTrim() {
+ String str = randomAlphanumeric(4);
+ for (int i = 0; i <= ' '; i++) {
+ String prefixed = (char) i + str;
+ String suffixed = (char) i + str;
+ String both = (char) i + str + (char) i;
+ assertThat(trimFieldsAndRemoveEmptyFields(prefixed)).isEqualTo(prefixed.trim());
+ assertThat(trimFieldsAndRemoveEmptyFields(suffixed)).isEqualTo(suffixed.trim());
+ assertThat(trimFieldsAndRemoveEmptyFields(both)).isEqualTo(both.trim());
+ }
+ }
+
+ @DataProvider
+ public static Object[][] emptyAndtrimmable() {
+ Random random = new Random();
+ String oneEmpty = randomTrimmedChars(1, random);
+ String twoEmpty = randomTrimmedChars(2, random);
+ String threePlusEmpty = randomTrimmedChars(3 + random.nextInt(5), random);
+ String onePlusEmpty = randomTrimmedChars(1 + random.nextInt(5), random);
+
+ String plain = randomAlphanumeric(1);
+ String plainWithtrimmable = randomAlphanumeric(2) + onePlusEmpty + randomAlphanumeric(3);
+ String quotedWithSeparator = '"' + randomAlphanumeric(3) + ',' + randomAlphanumeric(2) + '"';
+ String quotedWithDoubleSeparator = '"' + randomAlphanumeric(3) + ",," + randomAlphanumeric(2) + '"';
+ String quotedWithtrimmable = '"' + randomAlphanumeric(3) + onePlusEmpty + randomAlphanumeric(2) + '"';
+
+ String[] empties = {oneEmpty, twoEmpty, threePlusEmpty};
+ String[] strings = {plain, plainWithtrimmable,
+ onePlusEmpty + plain, plain + onePlusEmpty, onePlusEmpty + plain + onePlusEmpty,
+ onePlusEmpty + plainWithtrimmable, plainWithtrimmable + onePlusEmpty, onePlusEmpty + plainWithtrimmable + onePlusEmpty,
+ onePlusEmpty + quotedWithSeparator, quotedWithSeparator + onePlusEmpty, onePlusEmpty + quotedWithSeparator + onePlusEmpty,
+ onePlusEmpty + quotedWithDoubleSeparator, quotedWithDoubleSeparator + onePlusEmpty, onePlusEmpty + quotedWithDoubleSeparator + onePlusEmpty,
+ onePlusEmpty + quotedWithtrimmable, quotedWithtrimmable + onePlusEmpty, onePlusEmpty + quotedWithtrimmable + onePlusEmpty
+ };
+
+ Object[][] res = new Object[empties.length * strings.length][2];
+ int i = 0;
+ for (String empty : empties) {
+ for (String string : strings) {
+ res[i][0] = empty;
+ res[i][1] = string;
+ i++;
+ }
+ }
+ return res;
+ }
+
+ @Test
+ @UseDataProvider("emptys")
+ public void trimFieldsAndRemoveEmptyFields_quotes_allow_to_preserve_fields(String empty) {
+ String quotedEmpty = '"' + empty + '"';
+
+ assertThat(trimFieldsAndRemoveEmptyFields(quotedEmpty)).isEqualTo(quotedEmpty);
+ assertThat(trimFieldsAndRemoveEmptyFields(',' + quotedEmpty)).isEqualTo(quotedEmpty);
+ assertThat(trimFieldsAndRemoveEmptyFields(quotedEmpty + ',')).isEqualTo(quotedEmpty);
+ assertThat(trimFieldsAndRemoveEmptyFields(',' + quotedEmpty + ',')).isEqualTo(quotedEmpty);
+
+ assertThat(trimFieldsAndRemoveEmptyFields(quotedEmpty + ',' + quotedEmpty)).isEqualTo(quotedEmpty + ',' + quotedEmpty);
+ assertThat(trimFieldsAndRemoveEmptyFields(quotedEmpty + ",," + quotedEmpty)).isEqualTo(quotedEmpty + ',' + quotedEmpty);
+
+ assertThat(trimFieldsAndRemoveEmptyFields(quotedEmpty + ',' + quotedEmpty + ',' + quotedEmpty)).isEqualTo(quotedEmpty + ',' + quotedEmpty + ',' + quotedEmpty);
+ }
+
+ @DataProvider
+ public static Object[][] emptys() {
+ Random random = new Random();
+ return new Object[][] {
+ {randomTrimmedChars(1, random)},
+ {randomTrimmedChars(2, random)},
+ {randomTrimmedChars(3 + random.nextInt(5), random)}
+ };
+ }
+
+ @Test
+ public void trimFieldsAndRemoveEmptyFields_supports_escaped_quote_in_quotes() {
+ assertThat(trimFieldsAndRemoveEmptyFields("\"f\"\"oo\"")).isEqualTo("\"f\"\"oo\"");
+ assertThat(trimFieldsAndRemoveEmptyFields("\"f\"\"oo\",\"bar\"\"\"")).isEqualTo("\"f\"\"oo\",\"bar\"\"\"");
+ }
+
+ @Test
+ public void trimFieldsAndRemoveEmptyFields_does_not_fail_on_unbalanced_quotes() {
+ assertThat(trimFieldsAndRemoveEmptyFields("\"")).isEqualTo("\"");
+ assertThat(trimFieldsAndRemoveEmptyFields("\"foo")).isEqualTo("\"foo");
+ assertThat(trimFieldsAndRemoveEmptyFields("foo\"")).isEqualTo("foo\"");
+
+ assertThat(trimFieldsAndRemoveEmptyFields("\"foo\",\"")).isEqualTo("\"foo\",\"");
+ assertThat(trimFieldsAndRemoveEmptyFields("\",\"foo\"")).isEqualTo("\",\"foo\"");
+
+ assertThat(trimFieldsAndRemoveEmptyFields("\"foo\",\", ")).isEqualTo("\"foo\",\", ");
+
+ assertThat(trimFieldsAndRemoveEmptyFields(" a ,,b , c, \"foo\",\" ")).isEqualTo("a,b,c,\"foo\",\" ");
+ assertThat(trimFieldsAndRemoveEmptyFields("\" a ,,b , c, ")).isEqualTo("\" a ,,b , c, ");
+ }
+
+ private static final char[] SOME_PRINTABLE_TRIMMABLE_CHARS = {
+ ' ', '\t', '\n', '\r'
+ };
+
+ /**
+ * Result of randomTrimmedChars being used as arguments to JUnit test method through the DataProvider feature, they
+ * are printed to surefire report. Some of those chars breaks the parsing of the surefire report during sonar analysis.
+ * Therefor, we only use a subset of the trimmable chars.
+ */
+ private static String randomTrimmedChars(int length, Random random) {
+ char[] chars = new char[length];
+ for (int i = 0; i < chars.length; i++) {
+ chars[i] = SOME_PRINTABLE_TRIMMABLE_CHARS[random.nextInt(SOME_PRINTABLE_TRIMMABLE_CHARS.length)];
+ }
+ return new String(chars);
+ }
+}
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang.StringUtils.trim;
-import static org.sonar.core.config.MultivalueProperty.parseAsCsv;
+import static org.sonar.api.config.internal.MultivalueProperty.parseAsCsv;
@Immutable
public abstract class DefaultConfiguration implements Configuration {
*/
package org.sonar.scanner.scan;
+import java.nio.file.Path;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
import java.util.Map;
+import java.util.Optional;
+import org.apache.commons.lang.StringUtils;
import org.picocontainer.injectors.ProviderAdapter;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
+import org.sonar.api.scan.filesystem.PathResolver;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.scanner.scan.filesystem.ScannerComponentIdGenerator;
public class InputModuleHierarchyProvider extends ProviderAdapter {
+ private static final Logger LOG = Loggers.get(InputModuleHierarchyProvider.class);
+
private DefaultInputModuleHierarchy hierarchy = null;
public DefaultInputModuleHierarchy provide(ScannerComponentIdGenerator scannerComponentIdGenerator, DefaultInputProject project) {
if (hierarchy == null) {
- DefaultInputModule root = new DefaultInputModule(project.definition(), project.scannerId());
+ LOG.debug("Creating module hierarchy");
+ DefaultInputModule root = createModule(project.definition(), project.scannerId());
Map<DefaultInputModule, DefaultInputModule> parents = createChildren(root, scannerComponentIdGenerator, new HashMap<>());
if (parents.isEmpty()) {
hierarchy = new DefaultInputModuleHierarchy(root);
private static Map<DefaultInputModule, DefaultInputModule> createChildren(DefaultInputModule parent, ScannerComponentIdGenerator scannerComponentIdGenerator,
Map<DefaultInputModule, DefaultInputModule> parents) {
for (ProjectDefinition def : parent.definition().getSubProjects()) {
- DefaultInputModule child = new DefaultInputModule(def, scannerComponentIdGenerator.getAsInt());
+ DefaultInputModule child = createModule(def, scannerComponentIdGenerator.getAsInt());
parents.put(child, parent);
createChildren(child, scannerComponentIdGenerator, parents);
}
return parents;
}
+
+ private static DefaultInputModule createModule(ProjectDefinition def, int scannerComponentId) {
+ LOG.debug(" Init module '{}'", def.getName());
+ DefaultInputModule module = new DefaultInputModule(def, scannerComponentId);
+ LOG.debug(" Base dir: {}", module.getBaseDir().toAbsolutePath().toString());
+ LOG.debug(" Working dir: {}", module.getWorkDir().toAbsolutePath().toString());
+ LOG.debug(" Module global encoding: {}, default locale: {}", module.getEncoding().displayName(), Locale.getDefault());
+ logPaths(" Source paths: ", module.getBaseDir(), module.getSourceDirsOrFiles());
+ logPaths(" Test paths: ", module.getBaseDir(), module.getTestDirsOrFiles());
+ return module;
+ }
+
+ private static void logPaths(String label, Path baseDir, List<Path> paths) {
+ if (!paths.isEmpty()) {
+ StringBuilder sb = new StringBuilder(label);
+ for (Iterator<Path> it = paths.iterator(); it.hasNext(); ) {
+ Path file = it.next();
+ Optional<String> relativePathToBaseDir = PathResolver.relativize(baseDir, file);
+ if (!relativePathToBaseDir.isPresent()) {
+ sb.append(file);
+ } else if (StringUtils.isBlank(relativePathToBaseDir.get())) {
+ sb.append(".");
+ } else {
+ sb.append(relativePathToBaseDir.get());
+ }
+ if (it.hasNext()) {
+ sb.append(", ");
+ }
+ }
+ LOG.info(sb.toString());
+ }
+ }
+
+
}
*/
package org.sonar.scanner.scan;
+import java.util.Locale;
import org.picocontainer.injectors.ProviderAdapter;
import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.scanner.scan.filesystem.ScannerComponentIdGenerator;
public class InputProjectProvider extends ProviderAdapter {
+ private static final Logger LOG = Loggers.get(DefaultInputModule.class);
+
private DefaultInputProject project = null;
public DefaultInputProject provide(ProjectBuildersExecutor projectBuildersExecutor, ProjectReactorValidator validator,
// 2 Validate final reactor
validator.validate(projectReactor);
- // 3 Create modules and the hierarchy
+ // 3 Create project
project = new DefaultInputProject(projectReactor.getRoot(), scannerComponentIdGenerator.getAsInt());
+
+ LOG.info("Base dir: {}", project.getBaseDir().toAbsolutePath().toString());
+ LOG.info("Working dir: {}", project.getWorkDir().toAbsolutePath().toString());
+ LOG.debug("Project global encoding: {}, default locale: {}", project.getEncoding().displayName(), Locale.getDefault());
}
return project;
}
import org.sonar.scanner.scan.filesystem.ExclusionFilters;
import org.sonar.scanner.scan.filesystem.FileIndexer;
import org.sonar.scanner.scan.filesystem.InputFileBuilder;
-import org.sonar.scanner.scan.filesystem.LanguageDetection;
import org.sonar.scanner.scan.filesystem.MetadataGenerator;
-import org.sonar.scanner.scan.filesystem.ModuleFileSystemInitializer;
import org.sonar.scanner.scan.filesystem.ModuleInputComponentStore;
import org.sonar.scanner.scan.report.IssuesReports;
import org.sonar.scanner.sensor.DefaultSensorContext;
ExclusionFilters.class,
MetadataGenerator.class,
FileMetadata.class,
- LanguageDetection.class,
FileIndexer.class,
InputFileBuilder.class,
DefaultModuleFileSystem.class,
- ModuleFileSystemInitializer.class,
QProfileVerifier.class,
SensorOptimizer.class,
import org.sonar.scanner.bootstrap.ScannerProperties;
import org.sonar.scanner.util.ScannerUtils;
-import static org.sonar.core.config.MultivalueProperty.parseAsCsv;
+import static org.sonar.api.config.internal.MultivalueProperty.parseAsCsv;
+
/**
* Class that creates a project definition based on a set of properties.
import org.sonar.scanner.scan.branch.ProjectBranchesProvider;
import org.sonar.scanner.scan.branch.ProjectPullRequestsProvider;
import org.sonar.scanner.scan.filesystem.InputComponentStore;
+import org.sonar.scanner.scan.filesystem.LanguageDetection;
import org.sonar.scanner.scan.filesystem.ScannerComponentIdGenerator;
import org.sonar.scanner.scan.filesystem.StatusDetection;
import org.sonar.scanner.scan.measure.DefaultMetricFinder;
ScannerComponentIdGenerator.class,
new ScmChangedFilesProvider(),
StatusDetection.class,
+ LanguageDetection.class,
// rules
new ActiveRulesProvider(),
import com.google.common.annotations.VisibleForTesting;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
-import org.sonar.api.batch.fs.internal.AbstractProjectOrModule;
+import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.scanner.analysis.DefaultAnalysisMode;
public class DefaultModuleFileSystem extends DefaultFileSystem {
- public DefaultModuleFileSystem(ModuleInputComponentStore moduleInputFileCache, AbstractProjectOrModule module, ModuleFileSystemInitializer initializer, DefaultAnalysisMode mode,
+ public DefaultModuleFileSystem(ModuleInputComponentStore moduleInputFileCache, DefaultInputModule module, DefaultAnalysisMode mode,
StatusDetection statusDetection) {
super(module.getBaseDir(), moduleInputFileCache);
- setFields(module, initializer, mode, statusDetection);
+ setFields(module, mode, statusDetection);
}
@VisibleForTesting
- public DefaultModuleFileSystem(AbstractProjectOrModule module, ModuleFileSystemInitializer initializer, DefaultAnalysisMode mode, StatusDetection statusDetection) {
+ public DefaultModuleFileSystem(DefaultInputModule module, DefaultAnalysisMode mode, StatusDetection statusDetection) {
super(module.getBaseDir());
- setFields(module, initializer, mode, statusDetection);
+ setFields(module, mode, statusDetection);
}
- private void setFields(AbstractProjectOrModule module, ModuleFileSystemInitializer initializer, DefaultAnalysisMode mode, StatusDetection statusDetection) {
+ private void setFields(DefaultInputModule module, DefaultAnalysisMode mode, StatusDetection statusDetection) {
setWorkDir(module.getWorkDir());
- setEncoding(initializer.defaultEncoding());
+ setEncoding(module.getEncoding());
// filter the files sensors have access to
if (!mode.scanAllFiles()) {
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputFile.Type;
import org.sonar.api.batch.fs.InputFileFilter;
-import org.sonar.api.batch.fs.InputModule;
import org.sonar.api.batch.fs.internal.DefaultInputDir;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.AbstractProjectOrModule;
+import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.MessageException;
import org.sonar.scanner.scan.DefaultComponentTree;
private final ExclusionFilters exclusionFilters;
private final InputFileBuilder inputFileBuilder;
private final DefaultComponentTree componentTree;
- private final AbstractProjectOrModule module;
+ private final DefaultInputModule module;
private final ScannerComponentIdGenerator scannerComponentIdGenerator;
private final InputComponentStore componentStore;
- private final ModuleFileSystemInitializer moduleFileSystemInitializer;
private ExecutorService executorService;
private final List<Future<Void>> tasks;
private final DefaultModuleFileSystem defaultModuleFileSystem;
private ProgressReport progressReport;
- public FileIndexer(ScannerComponentIdGenerator scannerComponentIdGenerator, InputComponentStore componentStore, InputModule module, ExclusionFilters exclusionFilters,
- DefaultComponentTree componentTree, InputFileBuilder inputFileBuilder, ModuleFileSystemInitializer initializer, DefaultModuleFileSystem defaultModuleFileSystem,
+ public FileIndexer(ScannerComponentIdGenerator scannerComponentIdGenerator, InputComponentStore componentStore, DefaultInputModule module, ExclusionFilters exclusionFilters,
+ DefaultComponentTree componentTree, InputFileBuilder inputFileBuilder, DefaultModuleFileSystem defaultModuleFileSystem,
LanguageDetection languageDetection,
InputFileFilter[] filters) {
this.scannerComponentIdGenerator = scannerComponentIdGenerator;
this.componentStore = componentStore;
- this.module = (AbstractProjectOrModule) module;
+ this.module = module;
this.componentTree = componentTree;
this.inputFileBuilder = inputFileBuilder;
- this.moduleFileSystemInitializer = initializer;
this.defaultModuleFileSystem = defaultModuleFileSystem;
this.langDetection = languageDetection;
this.filters = filters;
this.tasks = new ArrayList<>();
}
- public FileIndexer(ScannerComponentIdGenerator scannerComponentIdGenerator, InputComponentStore componentStore, InputModule module, ExclusionFilters exclusionFilters,
- DefaultComponentTree componentTree, InputFileBuilder inputFileBuilder, ModuleFileSystemInitializer initializer, DefaultModuleFileSystem defaultModuleFileSystem,
+ public FileIndexer(ScannerComponentIdGenerator scannerComponentIdGenerator, InputComponentStore componentStore, DefaultInputModule module, ExclusionFilters exclusionFilters,
+ DefaultComponentTree componentTree, InputFileBuilder inputFileBuilder, DefaultModuleFileSystem defaultModuleFileSystem,
LanguageDetection languageDetection) {
- this(scannerComponentIdGenerator, componentStore, module, exclusionFilters, componentTree, inputFileBuilder, initializer, defaultModuleFileSystem, languageDetection,
+ this(scannerComponentIdGenerator, componentStore, module, exclusionFilters, componentTree, inputFileBuilder, defaultModuleFileSystem, languageDetection,
new InputFileFilter[0]);
}
Progress progress = new Progress();
- indexFiles(moduleFileSystemInitializer.sources(), InputFile.Type.MAIN, progress);
- indexFiles(moduleFileSystemInitializer.tests(), InputFile.Type.TEST, progress);
+ indexFiles(module.getSourceDirsOrFiles(), InputFile.Type.MAIN, progress);
+ indexFiles(module.getTestDirsOrFiles(), InputFile.Type.TEST, progress);
waitForTasksToComplete(progressReport);
import java.nio.file.Path;
import javax.annotation.Nullable;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.InputModule;
import org.sonar.api.batch.fs.internal.DefaultIndexedFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.AbstractProjectOrModule;
-import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
+import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.api.batch.fs.internal.SensorStrategy;
import org.sonar.scanner.scan.ScanProperties;
public class InputFileBuilder {
- private final String moduleKey;
- private final Path moduleBaseDir;
+ private final DefaultInputModule module;
private final ScannerComponentIdGenerator idGenerator;
private final MetadataGenerator metadataGenerator;
private final boolean preloadMetadata;
- private final ModuleFileSystemInitializer moduleFileSystemInitializer;
private final Path projectBaseDir;
private final SensorStrategy sensorStrategy;
- public InputFileBuilder(InputModule module, MetadataGenerator metadataGenerator,
- ScannerComponentIdGenerator idGenerator, ScanProperties properties, ModuleFileSystemInitializer moduleFileSystemInitializer, InputModuleHierarchy hierarchy,
+ public InputFileBuilder(DefaultInputProject project, DefaultInputModule module, MetadataGenerator metadataGenerator,
+ ScannerComponentIdGenerator idGenerator, ScanProperties properties,
SensorStrategy sensorStrategy) {
this.sensorStrategy = sensorStrategy;
- this.projectBaseDir = hierarchy.root().getBaseDir();
- this.moduleFileSystemInitializer = moduleFileSystemInitializer;
- this.moduleKey = module.key();
- this.moduleBaseDir = ((AbstractProjectOrModule) module).getBaseDir();
+ this.projectBaseDir = project.getBaseDir();
+ this.module = module;
this.metadataGenerator = metadataGenerator;
this.idGenerator = idGenerator;
this.preloadMetadata = properties.preloadFileMetadata();
}
DefaultInputFile create(InputFile.Type type, Path absolutePath, @Nullable String language) {
- DefaultIndexedFile indexedFile = new DefaultIndexedFile(absolutePath, moduleKey,
+ DefaultIndexedFile indexedFile = new DefaultIndexedFile(absolutePath, module.key(),
projectBaseDir.relativize(absolutePath).toString(),
- moduleBaseDir.relativize(absolutePath).toString(),
+ module.getBaseDir().relativize(absolutePath).toString(),
type, language, idGenerator.getAsInt(), sensorStrategy);
- DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(f, moduleFileSystemInitializer.defaultEncoding()));
+ DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(f, module.getEncoding()));
if (language != null) {
inputFile.setPublished(true);
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.fs.internal.PathPattern;
import org.sonar.api.config.Configuration;
import org.sonar.api.utils.MessageException;
/**
* Detect language of a source file based on its suffix and configured patterns.
*/
-@ScannerSide
@ThreadSafe
public class LanguageDetection {
+++ /dev/null
-/*
- * 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.scanner.scan.filesystem;
-
-import java.io.File;
-import java.nio.charset.Charset;
-import java.nio.file.Path;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-import java.util.Optional;
-import javax.annotation.Nullable;
-import javax.annotation.concurrent.Immutable;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.ScannerSide;
-import org.sonar.api.batch.bootstrap.ProjectDefinition;
-import org.sonar.api.batch.fs.InputModule;
-import org.sonar.api.batch.fs.internal.AbstractProjectOrModule;
-import org.sonar.api.scan.filesystem.PathResolver;
-import org.sonar.api.utils.log.Logger;
-import org.sonar.api.utils.log.Loggers;
-
-import static org.sonar.core.config.MultivalueProperty.parseAsCsv;
-
-@ScannerSide
-@Immutable
-public class ModuleFileSystemInitializer {
-
- private static final Logger LOG = Loggers.get(ModuleFileSystemInitializer.class);
-
- private final List<Path> sourceDirsOrFiles;
- private final List<Path> testDirsOrFiles;
- private final Charset encoding;
-
- public ModuleFileSystemInitializer(InputModule inputModule) {
- AbstractProjectOrModule inputModuleCasted = (AbstractProjectOrModule) inputModule;
- logDir("Base dir: ", inputModuleCasted.getBaseDir());
- logDir("Working dir: ", inputModuleCasted.getWorkDir());
- sourceDirsOrFiles = initSources(inputModuleCasted, ProjectDefinition.SOURCES_PROPERTY, "Source paths: ");
- testDirsOrFiles = initSources(inputModuleCasted, ProjectDefinition.TESTS_PROPERTY, "Test paths: ");
- encoding = initEncoding(inputModuleCasted);
- }
-
- private static List<Path> initSources(AbstractProjectOrModule module, String propertyKey, String logLabel) {
- 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(module.getBaseDir().toFile(), sourcePath);
- if (dirOrFile.exists()) {
- result.add(dirOrFile.toPath());
- }
- }
- }
- logPaths(logLabel, module.getBaseDir(), result);
- return result;
- }
-
- private static Charset initEncoding(AbstractProjectOrModule module) {
- String encodingStr = module.properties().get(CoreProperties.ENCODING_PROPERTY);
- Charset result;
- if (StringUtils.isNotEmpty(encodingStr)) {
- result = Charset.forName(StringUtils.trim(encodingStr));
- LOG.info("Source encoding: {}, default locale: {}", result.displayName(), Locale.getDefault());
- } else {
- result = Charset.defaultCharset();
- LOG.warn("Source encoding is platform dependent ({}), default locale: {}", result.displayName(), Locale.getDefault());
- }
- return result;
- }
-
- List<Path> sources() {
- return sourceDirsOrFiles;
- }
-
- List<Path> tests() {
- return testDirsOrFiles;
- }
-
- public Charset defaultEncoding() {
- return encoding;
- }
-
- private static void logPaths(String label, Path baseDir, List<Path> paths) {
- if (!paths.isEmpty()) {
- StringBuilder sb = new StringBuilder(label);
- for (Iterator<Path> it = paths.iterator(); it.hasNext(); ) {
- Path file = it.next();
- Optional<String> relativePathToBaseDir = PathResolver.relativize(baseDir, file);
- if (!relativePathToBaseDir.isPresent()) {
- sb.append(file);
- } else if (StringUtils.isBlank(relativePathToBaseDir.get())) {
- sb.append(".");
- } else {
- sb.append(relativePathToBaseDir.get());
- }
- if (it.hasNext()) {
- sb.append(", ");
- }
- }
- LOG.info(sb.toString());
- }
- }
-
- private static void logDir(String label, @Nullable Path dir) {
- if (dir != null) {
- LOG.info(label + dir.toAbsolutePath().toString());
- }
- }
-
-}
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
+import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.InputFile.Type;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.api.batch.fs.internal.SensorStrategy;
-import org.sonar.scanner.scan.DefaultInputModuleHierarchy;
import org.sonar.scanner.scan.ScanProperties;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public class InputFileBuilderTest {
@Rule
.setKey("root"), 0);
Path moduleBaseDir = baseDir.resolve("module1");
Files.createDirectories(moduleBaseDir);
+ DefaultInputProject project = new DefaultInputProject(ProjectDefinition.create()
+ .setBaseDir(baseDir.toFile())
+ .setWorkDir(workDir.toFile())
+ .setProperty(CoreProperties.ENCODING_PROPERTY, StandardCharsets.UTF_8.name())
+ .setKey("module1"), 0);
DefaultInputModule module = new DefaultInputModule(ProjectDefinition.create()
.setBaseDir(moduleBaseDir.toFile())
.setWorkDir(workDir.toFile())
MetadataGenerator metadataGenerator = mock(MetadataGenerator.class);
ScannerComponentIdGenerator idGenerator = new ScannerComponentIdGenerator();
ScanProperties properties = mock(ScanProperties.class);
- ModuleFileSystemInitializer moduleFileSystemInitializer = mock(ModuleFileSystemInitializer.class);
- when(moduleFileSystemInitializer.defaultEncoding()).thenReturn(StandardCharsets.UTF_8);
sensorStrategy = new SensorStrategy();
- builder = new InputFileBuilder(module, metadataGenerator, idGenerator, properties, moduleFileSystemInitializer, new DefaultInputModuleHierarchy(root),
+ builder = new InputFileBuilder(project, module, metadataGenerator, idGenerator, properties,
sensorStrategy);
}
+++ /dev/null
-/*
- * 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.scanner.scan.filesystem;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.Path;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.FilenameUtils;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.bootstrap.ProjectDefinition;
-import org.sonar.api.batch.fs.internal.DefaultInputModule;
-import org.sonar.api.utils.log.LogTester;
-import org.sonar.api.utils.log.LoggerLevel;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class ModuleFileSystemInitializerTest {
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Rule
- public LogTester logTester = new LogTester();
-
- @Test
- public void test_default_directories() throws Exception {
- File baseDir = temp.newFolder("base");
- File workDir = temp.newFolder("work");
- ProjectDefinition module = ProjectDefinition.create().setBaseDir(baseDir).setWorkDir(workDir);
-
- ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(new DefaultInputModule(module));
-
- assertThat(logTester.logs(LoggerLevel.INFO)).contains("Base dir: " + baseDir.toPath().toAbsolutePath().toString());
- assertThat(logTester.logs(LoggerLevel.INFO)).contains("Working dir: " + workDir.toPath().toAbsolutePath().toString());
- assertThat(initializer.sources()).isEmpty();
- assertThat(initializer.tests()).isEmpty();
- }
-
- @Test
- public void should_init_directories() throws IOException {
- File baseDir = temp.newFolder("base");
- File workDir = temp.newFolder("work");
- File sourceDir = new File(baseDir, "src/main/java");
- FileUtils.forceMkdir(sourceDir);
- File testDir = new File(baseDir, "src/test/java");
- FileUtils.forceMkdir(testDir);
- File binaryDir = new File(baseDir, "target/classes");
- FileUtils.forceMkdir(binaryDir);
-
- ProjectDefinition project = ProjectDefinition.create()
- .setBaseDir(baseDir)
- .setWorkDir(workDir)
- .addSources("src/main/java", "src/main/unknown")
- .addTests("src/test/java", "src/test/unknown");
-
- ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(new DefaultInputModule(project));
-
- assertThat(initializer.sources()).hasSize(1);
- assertThat(path(initializer.sources().get(0))).endsWith("src/main/java");
- assertThat(initializer.tests()).hasSize(1);
- assertThat(path(initializer.tests().get(0))).endsWith("src/test/java");
- }
-
- @Test
- public void supportFilenamesWithComma() throws IOException {
- File baseDir = temp.newFolder("base");
- File workDir = temp.newFolder("work");
- File sourceFile = new File(baseDir, "my,File.cs");
- sourceFile.createNewFile();
- File testFile = new File(baseDir, "my,TestFile.cs");
- testFile.createNewFile();
-
- ProjectDefinition project = ProjectDefinition.create()
- .setBaseDir(baseDir)
- .setWorkDir(workDir)
- .addSources("\"my,File.cs\"")
- .addTests("\"my,TestFile.cs\"");
-
- ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(new DefaultInputModule(project));
-
- assertThat(initializer.sources()).hasSize(1);
- assertThat(initializer.sources().get(0)).isEqualTo(sourceFile.toPath());
- assertThat(initializer.tests()).hasSize(1);
- assertThat(initializer.tests().get(0)).isEqualTo(testFile.toPath());
- }
-
- private String path(Path f) throws IOException {
- return FilenameUtils.separatorsToUnix(f.toString());
- }
-
-}