<modules>
<module>sonar-application</module>
<module>sonar-batch</module>
- <module>sonar-batch-plugin-api</module>
<module>sonar-batch-maven-compat</module>
<module>sonar-check-api</module>
<module>sonar-colorizer</module>
<artifactId>sonar-plugin-api</artifactId>
<version>${project.version}</version>
</dependency>
- <dependency>
- <groupId>org.codehaus.sonar</groupId>
- <artifactId>sonar-batch-plugin-api</artifactId>
- <version>${project.version}</version>
- </dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-update-center-common</artifactId>
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <parent>
- <groupId>org.codehaus.sonar</groupId>
- <artifactId>sonar</artifactId>
- <version>4.4-SNAPSHOT</version>
- </parent>
-
- <artifactId>sonar-batch-plugin-api</artifactId>
- <packaging>jar</packaging>
-
- <name>SonarQube :: Batch Plugin API</name>
-
- <dependencies>
- <dependency>
- <groupId>com.google.code.findbugs</groupId>
- <artifactId>jsr305</artifactId>
- </dependency>
-
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- </dependency>
- <dependency>
- <groupId>org.easytesting</groupId>
- <artifactId>fest-assert</artifactId>
- </dependency>
- </dependencies>
-
-
-</project>
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.api.analyzer;
-
-import org.sonar.batch.api.BatchExtension;
-
-/**
- * <p>
- * An Analyzer is invoked once during the analysis of a project. The analyzer can parse a flat file, connect to a web server... Analyzers are
- * used to add measure and issues at file level.
- * </p>
- *
- * <p>
- * For example the Cobertura Analyzer parses Cobertura report and saves the first-level of measures on files.
- * </p>
- *
- * @since 4.4
- */
-public interface Analyzer extends BatchExtension {
-
- /**
- * Describe what this analyzer is doing.
- * @return
- */
- AnalyzerDescriptor describe();
-
- /**
- * The method that is going to be run when the analyzer is called
- *
- * @param context the context
- */
- void analyse(AnalyzerContext context);
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.api.analyzer;
-
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.batch.api.analyzer.issue.AnalyzerIssue;
-import org.sonar.batch.api.analyzer.measure.AnalyzerMeasure;
-import org.sonar.batch.api.measures.Metric;
-
-import javax.annotation.CheckForNull;
-
-import java.io.Serializable;
-import java.util.Collection;
-
-/**
- * @since 4.4
- */
-public interface AnalyzerContext {
-
- // ----------- MEASURES --------------
-
- /**
- * Find a project measure.
- */
- @CheckForNull
- AnalyzerMeasure<?> getMeasure(String metricKey);
-
- /**
- * Find a project measure.
- */
- @CheckForNull
- <G extends Serializable> AnalyzerMeasure<G> getMeasure(Metric<G> metric);
-
- /**
- * Find a file measure.
- */
- @CheckForNull
- AnalyzerMeasure<?> getMeasure(InputFile file, String metricKey);
-
- /**
- * Find a file measure.
- */
- @CheckForNull
- <G extends Serializable> AnalyzerMeasure<G> getMeasure(InputFile file, Metric<G> metric);
-
- /**
- * Add a measure.
- */
- void addMeasure(AnalyzerMeasure<?> measure);
-
- // ----------- ISSUES --------------
-
- /**
- * Add an issue.
- */
- void addIssue(AnalyzerIssue issue);
-
- /**
- * Add a list of issues.
- */
- void addIssues(Collection<AnalyzerIssue> issues);
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.api.analyzer;
-
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.batch.api.measures.Metric;
-
-import java.util.Arrays;
-import java.util.Collection;
-
-public class AnalyzerDescriptor {
-
- private final String name;
- private final Metric<?>[] dependsOn;
- private final Metric<?>[] provides;
- private final String[] languages;
- private final InputFile.Type[] types;
-
- private AnalyzerDescriptor(Builder builder) {
- this.name = builder.name;
- this.dependsOn = builder.dependsOn != null ? builder.dependsOn : new Metric<?>[0];
- this.provides = builder.provides != null ? builder.provides : new Metric<?>[0];
- this.languages = builder.languages != null ? builder.languages : new String[0];
- this.types = builder.types;
- }
-
- public String name() {
- return name;
- }
-
- public Metric<?>[] dependsOn() {
- return dependsOn;
- }
-
- public Metric<?>[] provides() {
- return provides;
- }
-
- public Collection<String> languages() {
- return Arrays.asList(languages);
- }
-
- public InputFile.Type[] types() {
- return types;
- }
-
- public static Builder builder() {
- return new Builder();
- }
-
- public static class Builder {
-
- private String name;
- private Metric<?>[] dependsOn;
- private Metric<?>[] provides;
- private String[] languages;
- private InputFile.Type[] types;
-
- public Builder name(String name) {
- this.name = name;
- return this;
- }
-
- public Builder dependsOn(Metric<?>... metrics) {
- this.dependsOn = metrics;
- return this;
- }
-
- public Builder provides(Metric<?>... metrics) {
- this.provides = metrics;
- return this;
- }
-
- public Builder runOnLanguages(String... languageKeys) {
- this.languages = languageKeys;
- return this;
- }
-
- public Builder runOnTypes(InputFile.Type... types) {
- this.types = types;
- return this;
- }
-
- public AnalyzerDescriptor build() {
- return new AnalyzerDescriptor(this);
- }
-
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.api.analyzer.issue;
-
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.batch.api.analyzer.Analyzer;
-import org.sonar.batch.api.internal.Preconditions;
-import org.sonar.batch.api.rules.RuleKey;
-
-import javax.annotation.Nullable;
-
-/**
- * Issue reported by an {@link Analyzer}
- *
- * @since 4.4
- */
-public class AnalyzerIssue {
-
- private final InputFile inputFile;
- private final RuleKey ruleKey;
- private final String message;
- private final Integer line;
- private final Double effortToFix;
-
- private AnalyzerIssue(Builder builder) {
- this.inputFile = builder.file;
- this.ruleKey = builder.ruleKey;
- this.message = builder.message;
- this.line = builder.line;
- this.effortToFix = builder.effortToFix;
- }
-
- public static Builder builder() {
- return new Builder();
- }
-
- @Nullable
- public InputFile inputFile() {
- return inputFile;
- }
-
- public RuleKey ruleKey() {
- return ruleKey;
- }
-
- public String message() {
- return message;
- }
-
- public Integer line() {
- return line;
- }
-
- @Nullable
- public Double effortToFix() {
- return effortToFix;
- }
-
- public static class Builder {
-
- private Boolean onProject = null;
- private InputFile file;
- private RuleKey ruleKey;
- private String message;
- private Integer line;
- private Double effortToFix;
-
- public AnalyzerIssue build() {
- return new AnalyzerIssue(this);
- }
-
- public Builder ruleKey(RuleKey ruleKey) {
- this.ruleKey = ruleKey;
- return this;
- }
-
- public Builder onFile(InputFile file) {
- Preconditions.checkState(onProject == null, "onFile or onProject can be called only once");
- Preconditions.checkNotNull(file, "InputFile should be non null");
- this.file = file;
- this.onProject = false;
- return this;
- }
-
- public Builder onProject() {
- Preconditions.checkState(onProject == null, "onFile or onProject can be called only once");
- this.file = null;
- this.onProject = true;
- return this;
- }
-
- public Builder atLine(int line) {
- this.line = line;
- return this;
- }
-
- public Builder effortToFix(@Nullable Double effortToFix) {
- this.effortToFix = effortToFix;
- return this;
- }
-
- public Builder message(String message) {
- this.message = message;
- return this;
- }
-
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.batch.api.analyzer.issue;
\ No newline at end of file
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.api.analyzer.measure;
-
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.batch.api.internal.Preconditions;
-import org.sonar.batch.api.measures.Metric;
-
-import javax.annotation.Nullable;
-
-import java.io.Serializable;
-
-public class AnalyzerMeasure<G extends Serializable> implements Serializable {
-
- private final InputFile inputFile;
- private final String metricKey;
- private final G value;
-
- private AnalyzerMeasure(Builder<G> builder) {
- Preconditions.checkNotNull(builder.value, "Measure value can't be null");
- Preconditions.checkNotNull(builder.metricKey, "Measure metricKey can't be null");
- this.inputFile = builder.file;
- this.metricKey = builder.metricKey;
- this.value = builder.value;
- }
-
- @Nullable
- public InputFile inputFile() {
- return inputFile;
- }
-
- public String metricKey() {
- return metricKey;
- }
-
- public Serializable value() {
- return value;
- }
-
- public static <G extends Serializable> Builder<G> builder() {
- return new Builder<G>();
- }
-
- public static class Builder<G extends Serializable> {
-
- private Boolean onProject = null;
- private InputFile file;
- private String metricKey;
- private G value;
-
- public Builder<G> onFile(InputFile file) {
- Preconditions.checkState(onProject == null, "onFile or onProject can be called only once");
- Preconditions.checkNotNull(file, "InputFile should be non null");
- this.file = file;
- this.onProject = false;
- return this;
- }
-
- public Builder<G> onProject() {
- Preconditions.checkState(onProject == null, "onFile or onProject can be called only once");
- this.file = null;
- this.onProject = true;
- return this;
- }
-
- private Builder<G> metricKey(String metricKey) {
- Preconditions.checkState(metricKey != null, "Metric already defined");
- this.metricKey = metricKey;
- return this;
- }
-
- public Builder<G> forMetric(Metric<G> metric) {
- return metricKey(metric.key());
- }
-
- public Builder<G> withValue(G value) {
- Preconditions.checkState(value != null, "Measure value already defined");
- Preconditions.checkNotNull(value, "Measure value can't be null");
- this.value = value;
- return this;
- }
-
- public AnalyzerMeasure<G> build() {
- return new AnalyzerMeasure<G>(this);
- }
- }
-
- @Override
- public boolean equals(Object obj) {
- if (!(obj instanceof AnalyzerMeasure)) {
- return false;
- }
- AnalyzerMeasure<?> other = (AnalyzerMeasure<?>) obj;
- return metricKey.equals(other.metricKey)
- && value.equals(other.value)
- && (inputFile == null ? other.inputFile == null : inputFile.equals(other.inputFile));
- }
-
- @Override
- public int hashCode() {
- return metricKey.hashCode()
- + value.hashCode()
- + (inputFile != null ? inputFile.hashCode() : 0);
- }
-
- @Override
- public String toString() {
- return "AnalyzerMeasure[" + (inputFile != null ? "inputFile=" + inputFile.toString() : "onProject")
- + ",metricKey=" + metricKey + ",value=" + value + "]";
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.batch.api.analyzer.measure;
\ No newline at end of file
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.batch.api.analyzer;
\ No newline at end of file
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs;
-
-/**
- * Determines if a file must be kept in search results. See {@link org.sonar.api.batch.fs.FileSystem}
- * and {@link org.sonar.api.batch.fs.FilePredicates}.
- * @since 4.2
- */
-public interface FilePredicate {
- boolean apply(InputFile inputFile);
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs;
-
-import java.io.File;
-import java.util.Collection;
-
-/**
- * Factory of {@link org.sonar.api.batch.fs.FilePredicate}
- *
- * @since 4.2
- */
-public interface FilePredicates {
- /**
- * Returns a predicate that always evaluates to true
- */
- FilePredicate all();
-
- /**
- * Returns a predicate that always evaluates to false
- */
- FilePredicate none();
-
- /**
- * Warning - not efficient because absolute path is not indexed yet.
- */
- FilePredicate hasAbsolutePath(String s);
-
- /**
- * TODO document that non-normalized path and Windows-style path are supported
- */
- FilePredicate hasRelativePath(String s);
-
- FilePredicate matchesPathPattern(String inclusionPattern);
-
- FilePredicate matchesPathPatterns(String[] inclusionPatterns);
-
- FilePredicate doesNotMatchPathPattern(String exclusionPattern);
-
- FilePredicate doesNotMatchPathPatterns(String[] exclusionPatterns);
-
- FilePredicate hasPath(String s);
-
- FilePredicate is(File ioFile);
-
- FilePredicate hasLanguage(String language);
-
- FilePredicate hasLanguages(Collection<String> languages);
-
- FilePredicate hasLanguages(String... languages);
-
- FilePredicate hasStatus(InputFile.Status status);
-
- FilePredicate hasType(InputFile.Type type);
-
- FilePredicate not(FilePredicate p);
-
- FilePredicate or(Collection<FilePredicate> or);
-
- FilePredicate or(FilePredicate... or);
-
- FilePredicate or(FilePredicate first, FilePredicate second);
-
- FilePredicate and(Collection<FilePredicate> and);
-
- FilePredicate and(FilePredicate... and);
-
- FilePredicate and(FilePredicate first, FilePredicate second);
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs;
-
-import org.sonar.batch.api.BatchComponent;
-
-import javax.annotation.CheckForNull;
-
-import java.io.File;
-import java.nio.charset.Charset;
-import java.util.SortedSet;
-
-/**
- * The {@link FileSystem} manages all the source files to be analyzed.
- * <p/>
- * This is not an extension point so it must not be implemented by plugins. It must be injected as a
- * constructor parameter :
- * <pre>
- * public class MySensor implements Sensor {
- * private final FileSystem fs;
- *
- * public MySensor(FileSystem fs) {
- * this.fs = fs;
- * }
- * }
- * </pre>
- *
- * <h2>How to use in unit tests</h2>
- * The unit tests needing an instance of FileSystem can use the implementation
- * {@link org.sonar.api.batch.fs.internal.DefaultFileSystem} and the related {@link org.sonar.api.batch.fs.internal.DefaultInputFile},
- * for example :
- * <pre>
- * DefaultFileSystem fs = new DefaultFileSystem();
- * fs.add(new DefaultInputFile("src/foo/bar.php"));
- * </pre>
- *
- * @since 4.2
- */
-public interface FileSystem extends BatchComponent {
-
- /**
- * Absolute base directory of module
- */
- File baseDir();
-
- /**
- * Default encoding of input files. If it's not defined, then
- * the platform default encoding is returned
- */
- Charset encoding();
-
- /**
- * Absolute work directory. It can be used to
- * store third-party analysis reports.
- * <p/>
- * The work directory can be located outside {@link #baseDir()}.
- */
- File workDir();
-
- /**
- * Factory of {@link FilePredicate}
- */
- FilePredicates predicates();
-
- /**
- * Returns the single element matching the predicate. If more than one elements match
- * the predicate, then {@link IllegalArgumentException} is thrown. Returns {@code null}
- * if no files match.
- *
- * <p/>
- * How to use :
- * <pre>
- * InputFile file = fs.inputFile(fs.predicates().hasRelativePath("src/Foo.php"));
- * </pre>
- *
- * @see #predicates()
- */
- @CheckForNull
- InputFile inputFile(FilePredicate predicate);
-
- /**
- * Input files matching the given attributes. Return all the files if the parameter
- * <code>attributes</code> is empty.
- * <p/>
- * <b>Important</b> - result is an {@link java.lang.Iterable} to benefit from streaming and decreasing
- * memory consumption. It should be iterated only once, else copy it into a list :
- * {@code com.google.common.collect.Lists.newArrayList(inputFiles(predicate))}
- * <p/>
- * How to use :
- * <pre>
- * FilePredicates p = fs.predicates();
- * Iterable<InputFile> files = fs.inputFiles(p.and(p.hasLanguage("java"), p.hasType(InputFile.Type.MAIN)));
- * </pre>
- *
- * @see #predicates()
- */
- Iterable<InputFile> inputFiles(FilePredicate predicate);
-
- /**
- * Returns true if at least one {@link org.sonar.api.batch.fs.InputFile} matches
- * the given predicate. This method can be faster than checking if {@link #inputFiles(org.sonar.api.batch.fs.FilePredicate)}
- * has elements.
- * @see #predicates()
- */
- boolean hasFiles(FilePredicate predicate);
-
- /**
- * Files matching the given predicate.
- * @see #predicates()
- */
- Iterable<File> files(FilePredicate predicate);
-
- /**
- * Languages detected in all files, whatever their type (main or test)
- */
- SortedSet<String> languages();
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs;
-
-import java.io.File;
-import java.io.Serializable;
-
-/**
- * This layer over {@link java.io.File} adds information for code analyzers.
- *
- * @since 4.2
- */
-public interface InputFile extends Serializable {
-
- enum Type {
- MAIN, TEST
- }
-
- /**
- * Status regarding previous analysis
- */
- enum Status {
- SAME, CHANGED, ADDED
- }
-
- /**
- * Path relative to module base directory. Path is unique and identifies file
- * within given <code>{@link FileSystem}</code>. File separator is the forward
- * slash ('/'), even on Microsoft Windows.
- * <p/>
- * Returns <code>src/main/java/com/Foo.java</code> if module base dir is
- * <code>/path/to/module</code> and if file is
- * <code>/path/to/module/src/main/java/com/Foo.java</code>.
- * <p/>
- * Relative path is not null and is normalized ('foo/../foo' is replaced by 'foo').
- */
- String relativePath();
-
- /**
- * Normalized absolute path. File separator is forward slash ('/'), even on Microsoft Windows.
- * <p/>
- * This is not canonical path. Symbolic links are not resolved. For example if /project/src links
- * to /tmp/src and basedir is /project, then this method returns /project/src/index.php. Use
- * {@code file().getCanonicalPath()} to resolve symbolic link.
- */
- String absolutePath();
-
- /**
- * The underlying absolute {@link java.io.File}
- */
- File file();
-
- /**
- * Language, for example "java" or "php". It's automatically guessed if it is not
- * set in project settings.
- */
- String language();
-
- /**
- * Does it contain main or test code ?
- */
- Type type();
-
- /**
- * Status regarding previous analysis
- */
- Status status();
-
- /**
- * Number of physical lines. This method supports all end-of-line characters. Returns
- * zero if the file is empty.
- */
- int lines();
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs;
-
-import org.sonar.batch.api.BatchExtension;
-
-/**
- * Extension point to exclude some files from inspection
- * @since 4.2
- */
-public interface InputFileFilter extends BatchExtension {
-
- // TODO requires a context (FileSystem) ?
- boolean accept(InputFile f);
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.batch.api.internal.FilenameUtils;
-
-/**
- * @since 4.2
- */
-class AbsolutePathPredicate implements FilePredicate {
-
- private final String path;
-
- AbsolutePathPredicate(String path) {
- this.path = FilenameUtils.normalize(path);
- }
-
- @Override
- public boolean apply(InputFile f) {
- return path.equals(f.absolutePath());
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
-
-import java.util.Collection;
-
-/**
- * @since 4.2
- */
-class AndPredicate implements FilePredicate {
-
- private final Collection<FilePredicate> predicates;
-
- AndPredicate(Collection<FilePredicate> predicates) {
- this.predicates = predicates;
- }
-
- @Override
- public boolean apply(InputFile f) {
- for (FilePredicate predicate : predicates) {
- if (!predicate.apply(f)) {
- return false;
- }
- }
- return true;
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.FilePredicates;
-import org.sonar.api.batch.fs.InputFile;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-
-/**
- * Factory of {@link org.sonar.api.batch.fs.FilePredicate}
- *
- * @since 4.2
- */
-public class DefaultFilePredicates implements FilePredicates {
- /**
- * Client code should use {@link org.sonar.api.batch.fs.FileSystem#predicates()} to get an instance
- */
- DefaultFilePredicates() {
- }
-
- /**
- * Returns a predicate that always evaluates to true
- */
- public FilePredicate all() {
- return TruePredicate.TRUE;
- }
-
- /**
- * Returns a predicate that always evaluates to false
- */
- public FilePredicate none() {
- return FalsePredicate.FALSE;
- }
-
- /**
- * Warning - not efficient because absolute path is not indexed yet.
- */
- public FilePredicate hasAbsolutePath(String s) {
- return new AbsolutePathPredicate(s);
- }
-
- /**
- * TODO document that non-normalized path and Windows-style path are supported
- */
- public FilePredicate hasRelativePath(String s) {
- return new RelativePathPredicate(s);
- }
-
- public FilePredicate matchesPathPattern(String inclusionPattern) {
- return new PathPatternPredicate(PathPattern.create(inclusionPattern));
- }
-
- public FilePredicate matchesPathPatterns(String[] inclusionPatterns) {
- if (inclusionPatterns.length == 0) {
- return TruePredicate.TRUE;
- }
- FilePredicate[] predicates = new FilePredicate[inclusionPatterns.length];
- for (int i = 0; i < inclusionPatterns.length; i++) {
- predicates[i] = new PathPatternPredicate(PathPattern.create(inclusionPatterns[i]));
- }
- return or(predicates);
- }
-
- public FilePredicate doesNotMatchPathPattern(String exclusionPattern) {
- return not(matchesPathPattern(exclusionPattern));
- }
-
- public FilePredicate doesNotMatchPathPatterns(String[] exclusionPatterns) {
- if (exclusionPatterns.length == 0) {
- return TruePredicate.TRUE;
- }
- return not(matchesPathPatterns(exclusionPatterns));
- }
-
- public FilePredicate hasPath(String s) {
- File file = new File(s);
- if (file.isAbsolute()) {
- return hasAbsolutePath(s);
- }
- return hasRelativePath(s);
- }
-
- public FilePredicate is(File ioFile) {
- if (ioFile.isAbsolute()) {
- return hasAbsolutePath(ioFile.getAbsolutePath());
- }
- return hasRelativePath(ioFile.getPath());
- }
-
- public FilePredicate hasLanguage(String language) {
- return new LanguagePredicate(language);
- }
-
- public FilePredicate hasLanguages(Collection<String> languages) {
- List<FilePredicate> list = new ArrayList<FilePredicate>();
- for (String language : languages) {
- list.add(hasLanguage(language));
- }
- return or(list);
- }
-
- public FilePredicate hasLanguages(String... languages) {
- List<FilePredicate> list = new ArrayList<FilePredicate>();
- for (String language : languages) {
- list.add(hasLanguage(language));
- }
- return or(list);
- }
-
- public FilePredicate hasStatus(InputFile.Status status) {
- return new StatusPredicate(status);
- }
-
- public FilePredicate hasType(InputFile.Type type) {
- return new TypePredicate(type);
- }
-
- public FilePredicate not(FilePredicate p) {
- return new NotPredicate(p);
- }
-
- public FilePredicate or(Collection<FilePredicate> or) {
- return new OrPredicate(or);
- }
-
- public FilePredicate or(FilePredicate... or) {
- return new OrPredicate(Arrays.asList(or));
- }
-
- public FilePredicate or(FilePredicate first, FilePredicate second) {
- return new OrPredicate(Arrays.asList(first, second));
- }
-
- public FilePredicate and(Collection<FilePredicate> and) {
- return new AndPredicate(and);
- }
-
- public FilePredicate and(FilePredicate... and) {
- return new AndPredicate(Arrays.asList(and));
- }
-
- public FilePredicate and(FilePredicate first, FilePredicate second) {
- return new AndPredicate(Arrays.asList(first, second));
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.FilePredicates;
-import org.sonar.api.batch.fs.FileSystem;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.batch.api.internal.Preconditions;
-
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-
-import java.io.File;
-import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.SortedSet;
-import java.util.TreeSet;
-
-/**
- * @since 4.2
- */
-public class DefaultFileSystem implements FileSystem {
-
- private final Cache cache;
- private final SortedSet<String> languages = new TreeSet<String>();
- private File baseDir, workDir;
- private Charset encoding;
- private final FilePredicates predicates = new DefaultFilePredicates();
-
- /**
- * Only for testing
- */
- public DefaultFileSystem() {
- this.cache = new MapCache();
- }
-
- protected DefaultFileSystem(Cache cache) {
- this.cache = cache;
- }
-
- public DefaultFileSystem setBaseDir(File d) {
- Preconditions.checkNotNull(d, "Base directory can't be null");
- this.baseDir = d.getAbsoluteFile();
- return this;
- }
-
- @Override
- public File baseDir() {
- return baseDir;
- }
-
- public DefaultFileSystem setEncoding(@Nullable Charset e) {
- this.encoding = e;
- return this;
- }
-
- @Override
- public Charset encoding() {
- return encoding == null ? Charset.defaultCharset() : encoding;
- }
-
- public boolean isDefaultJvmEncoding() {
- return encoding == null;
- }
-
- public DefaultFileSystem setWorkDir(File d) {
- this.workDir = d.getAbsoluteFile();
- return this;
- }
-
- @Override
- public File workDir() {
- return workDir;
- }
-
- @Override
- public InputFile inputFile(FilePredicate predicate) {
- doPreloadFiles();
- if (predicate instanceof RelativePathPredicate) {
- return cache.inputFile((RelativePathPredicate) predicate);
- }
- Iterable<InputFile> files = inputFiles(predicate);
- Iterator<InputFile> iterator = files.iterator();
- if (!iterator.hasNext()) {
- return null;
- }
- InputFile first = iterator.next();
- if (!iterator.hasNext()) {
- return first;
- }
-
- StringBuilder sb = new StringBuilder();
- sb.append("expected one element but was: <" + first);
- for (int i = 0; i < 4 && iterator.hasNext(); i++) {
- sb.append(", " + iterator.next());
- }
- if (iterator.hasNext()) {
- sb.append(", ...");
- }
- sb.append('>');
-
- throw new IllegalArgumentException(sb.toString());
-
- }
-
- @Override
- public Iterable<InputFile> inputFiles(FilePredicate predicate) {
- doPreloadFiles();
- return filter(cache.inputFiles(), predicate);
- }
-
- @Override
- public boolean hasFiles(FilePredicate predicate) {
- doPreloadFiles();
- for (InputFile element : cache.inputFiles()) {
- if (predicate.apply(element)) {
- return true;
- }
- }
- return false;
- }
-
- @Override
- public Iterable<File> files(FilePredicate predicate) {
- doPreloadFiles();
- Collection<File> result = new ArrayList<File>();
- for (InputFile element : inputFiles(predicate)) {
- if (predicate.apply(element)) {
- result.add(element.file());
- }
- }
- return result;
- }
-
- public static Collection<InputFile> filter(Iterable<InputFile> target, FilePredicate predicate) {
- Collection<InputFile> result = new ArrayList<InputFile>();
- for (InputFile element : target) {
- if (predicate.apply(element)) {
- result.add(element);
- }
- }
- return result;
- }
-
- /**
- * Adds InputFile to the list and registers its language, if present.
- */
- public DefaultFileSystem add(InputFile inputFile) {
- cache.add(inputFile);
- if (inputFile.language() != null) {
- languages.add(inputFile.language());
- }
- return this;
- }
-
- /**
- * Adds a language to the list. To be used only for unit tests that need to use {@link #languages()} without
- * using {@link #add(org.sonar.api.batch.fs.InputFile)}.
- */
- public DefaultFileSystem addLanguages(String language, String... others) {
- languages.add(language);
- Collections.addAll(languages, others);
- return this;
- }
-
- @Override
- public SortedSet<String> languages() {
- doPreloadFiles();
- return languages;
- }
-
- @Override
- public FilePredicates predicates() {
- return predicates;
- }
-
- /**
- * This method is called before each search of files.
- */
- protected void doPreloadFiles() {
- // nothing to do by default
- }
-
- public static abstract class Cache {
- protected abstract Iterable<InputFile> inputFiles();
-
- @CheckForNull
- protected abstract InputFile inputFile(RelativePathPredicate predicate);
-
- protected abstract void doAdd(InputFile inputFile);
-
- final void add(InputFile inputFile) {
- doAdd(inputFile);
- }
- }
-
- /**
- * Used only for testing
- */
- private static class MapCache extends Cache {
- private final Map<String, InputFile> fileMap = new HashMap<String, InputFile>();
-
- @Override
- public Iterable<InputFile> inputFiles() {
- return new ArrayList<InputFile>(fileMap.values());
- }
-
- @Override
- public InputFile inputFile(RelativePathPredicate predicate) {
- return fileMap.get(predicate.path());
- }
-
- @Override
- protected void doAdd(InputFile inputFile) {
- fileMap.put(inputFile.relativePath(), inputFile);
- }
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.batch.api.internal.FilenameUtils;
-
-import javax.annotation.CheckForNull;
-
-import java.io.File;
-import java.io.Serializable;
-
-/**
- * @since 4.2
- */
-public class DefaultInputFile implements InputFile, Serializable {
-
- private final String relativePath;
- private String absolutePath;
- private String language;
- private Type type = Type.MAIN;
- private Status status;
- private String hash;
- private int lines;
- private String key;
-
- public DefaultInputFile(String relativePath) {
- this.relativePath = FilenameUtils.normalize(relativePath);
- }
-
- @Override
- public String relativePath() {
- return relativePath;
- }
-
- /**
- * Marked as nullable just for the unit tests that do not call {@link #setFile(java.io.File)}
- * previously.
- */
- @Override
- @CheckForNull
- public String absolutePath() {
- return absolutePath;
- }
-
- @Override
- public File file() {
- if (absolutePath == null) {
- throw new IllegalStateException("Can not return the java.io.File because absolute path is not set (see method setFile(java.io.File))");
- }
- return new File(absolutePath);
- }
-
- /**
- * Marked as nullable just for the unit tests that do not call {@link #setLanguage(String)}
- * previously.
- */
- @CheckForNull
- @Override
- public String language() {
- return language;
- }
-
- @Override
- public Type type() {
- return type;
- }
-
- /**
- * Marked as nullable just for the unit tests that do not previously call
- * {@link #setStatus(org.sonar.api.batch.fs.InputFile.Status)}
- */
- @CheckForNull
- @Override
- public Status status() {
- return status;
- }
-
- /**
- * Digest hash of the file. Marked as nullable just for the unit tests
- * that do not previously call {@link #setHash(String)}
- */
- @CheckForNull
- public String hash() {
- return hash;
- }
-
- @Override
- public int lines() {
- return lines;
- }
-
- /**
- * Component key. It's marked as nullable just for the unit tests that
- * do not previously call {@link #setKey(String)}.
- */
- @CheckForNull
- public String key() {
- return key;
- }
-
- public DefaultInputFile setAbsolutePath(String s) {
- this.absolutePath = FilenameUtils.normalize(s);
- return this;
- }
-
- public DefaultInputFile setLanguage(String language) {
- this.language = language;
- return this;
- }
-
- public DefaultInputFile setFile(File file) {
- setAbsolutePath(file.getAbsolutePath());
- return this;
- }
-
- public DefaultInputFile setType(Type type) {
- this.type = type;
- return this;
- }
-
- public DefaultInputFile setStatus(Status status) {
- this.status = status;
- return this;
- }
-
- public DefaultInputFile setHash(String hash) {
- this.hash = hash;
- return this;
- }
-
- public DefaultInputFile setLines(int lines) {
- this.lines = lines;
- return this;
- }
-
- public DefaultInputFile setKey(String s) {
- this.key = s;
- return this;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (!(o instanceof DefaultInputFile)) {
- return false;
- }
-
- DefaultInputFile that = (DefaultInputFile) o;
- return relativePath.equals(that.relativePath);
- }
-
- @Override
- public int hashCode() {
- return relativePath.hashCode();
- }
-
- @Override
- public String toString() {
- return "[relative=" + relativePath + ", abs=" + absolutePath + "]";
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
-
-class FalsePredicate implements FilePredicate {
-
- static final FilePredicate FALSE = new FalsePredicate();
-
- @Override
- public boolean apply(InputFile inputFile) {
- return false;
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
-
-/**
- * @since 4.2
- */
-class LanguagePredicate implements FilePredicate {
- private final String language;
-
- LanguagePredicate(String language) {
- this.language = language;
- }
-
- @Override
- public boolean apply(InputFile f) {
- return language.equals(f.language());
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
-
-/**
- * @since 4.2
- */
-class NotPredicate implements FilePredicate {
-
- private final FilePredicate predicate;
-
- NotPredicate(FilePredicate predicate) {
- this.predicate = predicate;
- }
-
- @Override
- public boolean apply(InputFile f) {
- return !predicate.apply(f);
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
-
-import java.util.Arrays;
-import java.util.Collection;
-
-/**
- * @since 4.2
- */
-class OrPredicate implements FilePredicate {
-
- private final Collection<FilePredicate> predicates;
-
- OrPredicate(Collection<FilePredicate> predicates) {
- if (predicates.isEmpty()) {
- this.predicates = Arrays.asList(TruePredicate.TRUE);
- } else {
- this.predicates = predicates;
- }
- }
-
- @Override
- public boolean apply(InputFile f) {
- for (FilePredicate predicate : predicates) {
- if (predicate.apply(f)) {
- return true;
- }
- }
- return false;
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.batch.api.internal.FilenameUtils;
-import org.sonar.batch.api.internal.StringUtils;
-import org.sonar.batch.api.internal.WildcardPattern;
-
-public abstract class PathPattern {
-
- final WildcardPattern pattern;
-
- PathPattern(String pattern) {
- this.pattern = WildcardPattern.create(pattern);
- }
-
- public abstract boolean match(InputFile inputFile);
-
- public abstract boolean match(InputFile inputFile, boolean caseSensitiveFileExtension);
-
- public static PathPattern create(String s) {
- String trimmed = StringUtils.trim(s);
- if (StringUtils.startsWithIgnoreCase(trimmed, "file:")) {
- return new AbsolutePathPattern(StringUtils.substring(trimmed, "file:".length()));
- }
- return new RelativePathPattern(trimmed);
- }
-
- public static PathPattern[] create(String[] s) {
- PathPattern[] result = new PathPattern[s.length];
- for (int i = 0; i < s.length; i++) {
- result[i] = create(s[i]);
- }
- return result;
- }
-
- private static class AbsolutePathPattern extends PathPattern {
- private AbsolutePathPattern(String pattern) {
- super(pattern);
- }
-
- @Override
- public boolean match(InputFile inputFile) {
- return match(inputFile, true);
- }
-
- @Override
- public boolean match(InputFile inputFile, boolean caseSensitiveFileExtension) {
- String path = inputFile.absolutePath();
- if (!caseSensitiveFileExtension) {
- String extension = sanitizeExtension(FilenameUtils.getExtension(inputFile.file().getName()));
- if (StringUtils.isNotBlank(extension)) {
- path = StringUtils.removeEndIgnoreCase(path, extension);
- path = path + extension;
- }
- }
- return pattern.match(path);
- }
-
- @Override
- public String toString() {
- return "file:" + pattern.toString();
- }
- }
-
- /**
- * Path relative to module basedir
- */
- private static class RelativePathPattern extends PathPattern {
- private RelativePathPattern(String pattern) {
- super(pattern);
- }
-
- @Override
- public boolean match(InputFile inputFile) {
- return match(inputFile, true);
- }
-
- @Override
- public boolean match(InputFile inputFile, boolean caseSensitiveFileExtension) {
- String path = inputFile.relativePath();
- if (!caseSensitiveFileExtension) {
- String extension = sanitizeExtension(FilenameUtils.getExtension(inputFile.file().getName()));
- if (StringUtils.isNotBlank(extension)) {
- path = StringUtils.removeEndIgnoreCase(path, extension);
- path = path + extension;
- }
- }
- return path != null && pattern.match(path);
- }
-
- @Override
- public String toString() {
- return pattern.toString();
- }
- }
-
- static String sanitizeExtension(String suffix) {
- return StringUtils.lowerCase(StringUtils.removeStart(suffix, "."));
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
-
-/**
- * @since 4.2
- */
-class PathPatternPredicate implements FilePredicate {
-
- private final PathPattern pattern;
-
- PathPatternPredicate(PathPattern pattern) {
- this.pattern = pattern;
- }
-
- @Override
- public boolean apply(InputFile f) {
- return pattern.match(f);
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.batch.api.internal.FilenameUtils;
-
-/**
- * @since 4.2
- */
-public class RelativePathPredicate implements FilePredicate {
-
- private final String path;
-
- RelativePathPredicate(String path) {
- this.path = FilenameUtils.normalize(path);
- }
-
- public String path() {
- return path;
- }
-
- @Override
- public boolean apply(InputFile f) {
- return path.equals(f.relativePath());
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
-
-/**
- * @since 4.2
- */
-class StatusPredicate implements FilePredicate {
-
- private final InputFile.Status status;
-
- StatusPredicate(InputFile.Status status) {
- this.status = status;
- }
-
- @Override
- public boolean apply(InputFile f) {
- return status == f.status();
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
-
-class TruePredicate implements FilePredicate {
-
- static final FilePredicate TRUE = new TruePredicate();
-
- @Override
- public boolean apply(InputFile inputFile) {
- return true;
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.InputFile;
-
-/**
- * @since 4.2
- */
-class TypePredicate implements FilePredicate {
-
- private final InputFile.Type type;
-
- TypePredicate(InputFile.Type type) {
- this.type = type;
- }
-
- @Override
- public boolean apply(InputFile f) {
- return type == f.type();
- }
-
-}
-
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-@ParametersAreNonnullByDefault
-package org.sonar.api.batch.fs.internal;
-
-import javax.annotation.ParametersAreNonnullByDefault;
-
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-@ParametersAreNonnullByDefault
-package org.sonar.api.batch.fs;
-
-import javax.annotation.ParametersAreNonnullByDefault;
-
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.api.internal;
-
-import java.io.File;
-
-/**
- * Copied from commons io
- *
- */
-public class FilenameUtils {
-
- public static final char EXTENSION_SEPARATOR = '.';
-
- private static final char UNIX_SEPARATOR = '/';
-
- /**
- * The Windows separator character.
- */
- private static final char WINDOWS_SEPARATOR = '\\';
-
- /**
- * The system separator character.
- */
- private static final char SYSTEM_SEPARATOR = File.separatorChar;
-
- /**
- * The separator character that is the opposite of the system separator.
- */
- private static final char OTHER_SEPARATOR;
- static {
- if (isSystemWindows()) {
- OTHER_SEPARATOR = UNIX_SEPARATOR;
- } else {
- OTHER_SEPARATOR = WINDOWS_SEPARATOR;
- }
- }
-
- static boolean isSystemWindows() {
- return SYSTEM_SEPARATOR == WINDOWS_SEPARATOR;
- }
-
- public static String normalize(String filename) {
- return doNormalize(filename, UNIX_SEPARATOR, true);
- }
-
- private static String doNormalize(String filename, char separator, boolean keepSeparator) {
- if (filename == null) {
- return null;
- }
- int size = filename.length();
- if (size == 0) {
- return filename;
- }
- int prefix = getPrefixLength(filename);
- if (prefix < 0) {
- return null;
- }
-
- char[] array = new char[size + 2]; // +1 for possible extra slash, +2 for arraycopy
- filename.getChars(0, filename.length(), array, 0);
-
- // fix separators throughout
- char otherSeparator = separator == SYSTEM_SEPARATOR ? OTHER_SEPARATOR : SYSTEM_SEPARATOR;
- for (int i = 0; i < array.length; i++) {
- if (array[i] == otherSeparator) {
- array[i] = separator;
- }
- }
-
- // add extra separator on the end to simplify code below
- boolean lastIsDirectory = true;
- if (array[size - 1] != separator) {
- array[size++] = separator;
- lastIsDirectory = false;
- }
-
- // adjoining slashes
- for (int i = prefix + 1; i < size; i++) {
- if (array[i] == separator && array[i - 1] == separator) {
- System.arraycopy(array, i, array, i - 1, size - i);
- size--;
- i--;
- }
- }
-
- // dot slash
- for (int i = prefix + 1; i < size; i++) {
- if (array[i] == separator && array[i - 1] == '.' &&
- (i == prefix + 1 || array[i - 2] == separator)) {
- if (i == size - 1) {
- lastIsDirectory = true;
- }
- System.arraycopy(array, i + 1, array, i - 1, size - i);
- size -= 2;
- i--;
- }
- }
-
- // double dot slash
- outer: for (int i = prefix + 2; i < size; i++) {
- if (array[i] == separator && array[i - 1] == '.' && array[i - 2] == '.' &&
- (i == prefix + 2 || array[i - 3] == separator)) {
- if (i == prefix + 2) {
- return null;
- }
- if (i == size - 1) {
- lastIsDirectory = true;
- }
- int j;
- for (j = i - 4; j >= prefix; j--) {
- if (array[j] == separator) {
- // remove b/../ from a/b/../c
- System.arraycopy(array, i + 1, array, j + 1, size - i);
- size -= i - j;
- i = j + 1;
- continue outer;
- }
- }
- // remove a/../ from a/../c
- System.arraycopy(array, i + 1, array, prefix, size - i);
- size -= i + 1 - prefix;
- i = prefix + 1;
- }
- }
-
- if (size <= 0) { // should never be less than 0
- return "";
- }
- if (size <= prefix) { // should never be less than prefix
- return new String(array, 0, size);
- }
- if (lastIsDirectory && keepSeparator) {
- return new String(array, 0, size); // keep trailing separator
- }
- return new String(array, 0, size - 1); // lose trailing separator
- }
-
- public static int getPrefixLength(String filename) {
- if (filename == null) {
- return -1;
- }
- int len = filename.length();
- if (len == 0) {
- return 0;
- }
- char ch0 = filename.charAt(0);
- if (ch0 == ':') {
- return -1;
- }
- if (len == 1) {
- if (ch0 == '~') {
- return 2; // return a length greater than the input
- }
- return isSeparator(ch0) ? 1 : 0;
- } else {
- if (ch0 == '~') {
- int posUnix = filename.indexOf(UNIX_SEPARATOR, 1);
- int posWin = filename.indexOf(WINDOWS_SEPARATOR, 1);
- if (posUnix == -1 && posWin == -1) {
- return len + 1; // return a length greater than the input
- }
- posUnix = posUnix == -1 ? posWin : posUnix;
- posWin = posWin == -1 ? posUnix : posWin;
- return Math.min(posUnix, posWin) + 1;
- }
- char ch1 = filename.charAt(1);
- if (ch1 == ':') {
- ch0 = Character.toUpperCase(ch0);
- if (ch0 >= 'A' && ch0 <= 'Z') {
- if (len == 2 || isSeparator(filename.charAt(2)) == false) {
- return 2;
- }
- return 3;
- }
- return -1;
-
- } else if (isSeparator(ch0) && isSeparator(ch1)) {
- int posUnix = filename.indexOf(UNIX_SEPARATOR, 2);
- int posWin = filename.indexOf(WINDOWS_SEPARATOR, 2);
- if (posUnix == -1 && posWin == -1 || posUnix == 2 || posWin == 2) {
- return -1;
- }
- posUnix = posUnix == -1 ? posWin : posUnix;
- posWin = posWin == -1 ? posUnix : posWin;
- return Math.min(posUnix, posWin) + 1;
- } else {
- return isSeparator(ch0) ? 1 : 0;
- }
- }
- }
-
- private static boolean isSeparator(char ch) {
- return ch == UNIX_SEPARATOR || ch == WINDOWS_SEPARATOR;
- }
-
- public static String getExtension(String filename) {
- if (filename == null) {
- return null;
- }
- int index = indexOfExtension(filename);
- if (index == -1) {
- return "";
- } else {
- return filename.substring(index + 1);
- }
- }
-
- public static int indexOfExtension(String filename) {
- if (filename == null) {
- return -1;
- }
- int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
- int lastSeparator = indexOfLastSeparator(filename);
- return lastSeparator > extensionPos ? -1 : extensionPos;
- }
-
- public static int indexOfLastSeparator(String filename) {
- if (filename == null) {
- return -1;
- }
- int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR);
- int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR);
- return Math.max(lastUnixPos, lastWindowsPos);
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.api.internal;
-
-import javax.annotation.Nullable;
-
-/**
- * Copied from Guava
- */
-public final class Preconditions {
- private Preconditions() {
- }
-
- /**
- * Ensures the truth of an expression involving one or more parameters to the
- * calling method.
- *
- * @param expression a boolean expression
- * @throws IllegalArgumentException if {@code expression} is false
- */
- public static void checkArgument(boolean expression) {
- if (!expression) {
- throw new IllegalArgumentException();
- }
- }
-
- /**
- * Ensures the truth of an expression involving one or more parameters to the
- * calling method.
- *
- * @param expression a boolean expression
- * @param errorMessage the exception message to use if the check fails; will
- * be converted to a string using {@link String#valueOf(Object)}
- * @throws IllegalArgumentException if {@code expression} is false
- */
- public static void checkArgument(
- boolean expression, @Nullable Object errorMessage) {
- if (!expression) {
- throw new IllegalArgumentException(String.valueOf(errorMessage));
- }
- }
-
- /**
- * Ensures the truth of an expression involving one or more parameters to the
- * calling method.
- *
- * @param expression a boolean expression
- * @param errorMessageTemplate a template for the exception message should the
- * check fail. The message is formed by replacing each {@code %s}
- * placeholder in the template with an argument. These are matched by
- * position - the first {@code %s} gets {@code errorMessageArgs[0]}, etc.
- * Unmatched arguments will be appended to the formatted message in square
- * braces. Unmatched placeholders will be left as-is.
- * @param errorMessageArgs the arguments to be substituted into the message
- * template. Arguments are converted to strings using
- * {@link String#valueOf(Object)}.
- * @throws IllegalArgumentException if {@code expression} is false
- * @throws NullPointerException if the check fails and either {@code
- * errorMessageTemplate} or {@code errorMessageArgs} is null (don't let
- * this happen)
- */
- public static void checkArgument(boolean expression,
- @Nullable String errorMessageTemplate,
- @Nullable Object... errorMessageArgs) {
- if (!expression) {
- throw new IllegalArgumentException(
- format(errorMessageTemplate, errorMessageArgs));
- }
- }
-
- /**
- * Ensures the truth of an expression involving the state of the calling
- * instance, but not involving any parameters to the calling method.
- *
- * @param expression a boolean expression
- * @throws IllegalStateException if {@code expression} is false
- */
- public static void checkState(boolean expression) {
- if (!expression) {
- throw new IllegalStateException();
- }
- }
-
- /**
- * Ensures the truth of an expression involving the state of the calling
- * instance, but not involving any parameters to the calling method.
- *
- * @param expression a boolean expression
- * @param errorMessage the exception message to use if the check fails; will
- * be converted to a string using {@link String#valueOf(Object)}
- * @throws IllegalStateException if {@code expression} is false
- */
- public static void checkState(
- boolean expression, @Nullable Object errorMessage) {
- if (!expression) {
- throw new IllegalStateException(String.valueOf(errorMessage));
- }
- }
-
- /**
- * Ensures the truth of an expression involving the state of the calling
- * instance, but not involving any parameters to the calling method.
- *
- * @param expression a boolean expression
- * @param errorMessageTemplate a template for the exception message should the
- * check fail. The message is formed by replacing each {@code %s}
- * placeholder in the template with an argument. These are matched by
- * position - the first {@code %s} gets {@code errorMessageArgs[0]}, etc.
- * Unmatched arguments will be appended to the formatted message in square
- * braces. Unmatched placeholders will be left as-is.
- * @param errorMessageArgs the arguments to be substituted into the message
- * template. Arguments are converted to strings using
- * {@link String#valueOf(Object)}.
- * @throws IllegalStateException if {@code expression} is false
- * @throws NullPointerException if the check fails and either {@code
- * errorMessageTemplate} or {@code errorMessageArgs} is null (don't let
- * this happen)
- */
- public static void checkState(boolean expression,
- @Nullable String errorMessageTemplate,
- @Nullable Object... errorMessageArgs) {
- if (!expression) {
- throw new IllegalStateException(
- format(errorMessageTemplate, errorMessageArgs));
- }
- }
-
- /**
- * Ensures that an object reference passed as a parameter to the calling
- * method is not null.
- *
- * @param reference an object reference
- * @return the non-null reference that was validated
- * @throws NullPointerException if {@code reference} is null
- */
- public static <T> T checkNotNull(T reference) {
- if (reference == null) {
- throw new NullPointerException();
- }
- return reference;
- }
-
- /**
- * Ensures that an object reference passed as a parameter to the calling
- * method is not null.
- *
- * @param reference an object reference
- * @param errorMessage the exception message to use if the check fails; will
- * be converted to a string using {@link String#valueOf(Object)}
- * @return the non-null reference that was validated
- * @throws NullPointerException if {@code reference} is null
- */
- public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) {
- if (reference == null) {
- throw new NullPointerException(String.valueOf(errorMessage));
- }
- return reference;
- }
-
- /**
- * Ensures that an object reference passed as a parameter to the calling
- * method is not null.
- *
- * @param reference an object reference
- * @param errorMessageTemplate a template for the exception message should the
- * check fail. The message is formed by replacing each {@code %s}
- * placeholder in the template with an argument. These are matched by
- * position - the first {@code %s} gets {@code errorMessageArgs[0]}, etc.
- * Unmatched arguments will be appended to the formatted message in square
- * braces. Unmatched placeholders will be left as-is.
- * @param errorMessageArgs the arguments to be substituted into the message
- * template. Arguments are converted to strings using
- * {@link String#valueOf(Object)}.
- * @return the non-null reference that was validated
- * @throws NullPointerException if {@code reference} is null
- */
- public static <T> T checkNotNull(T reference,
- @Nullable String errorMessageTemplate,
- @Nullable Object... errorMessageArgs) {
- if (reference == null) {
- // If either of these parameters is null, the right thing happens anyway
- throw new NullPointerException(
- format(errorMessageTemplate, errorMessageArgs));
- }
- return reference;
- }
-
- /*
- * All recent hotspots (as of 2009) *really* like to have the natural code
- *
- * if (guardExpression) {
- * throw new BadException(messageExpression);
- * }
- *
- * refactored so that messageExpression is moved to a separate
- * String-returning method.
- *
- * if (guardExpression) {
- * throw new BadException(badMsg(...));
- * }
- *
- * The alternative natural refactorings into void or Exception-returning
- * methods are much slower. This is a big deal - we're talking factors of
- * 2-8 in microbenchmarks, not just 10-20%. (This is a hotspot optimizer
- * bug, which should be fixed, but that's a separate, big project).
- *
- * The coding pattern above is heavily used in java.util, e.g. in ArrayList.
- * There is a RangeCheckMicroBenchmark in the JDK that was used to test this.
- *
- * But the methods in this class want to throw different exceptions,
- * depending on the args, so it appears that this pattern is not directly
- * applicable. But we can use the ridiculous, devious trick of throwing an
- * exception in the middle of the construction of another exception.
- * Hotspot is fine with that.
- */
-
- /**
- * Ensures that {@code index} specifies a valid <i>element</i> in an array,
- * list or string of size {@code size}. An element index may range from zero,
- * inclusive, to {@code size}, exclusive.
- *
- * @param index a user-supplied index identifying an element of an array, list
- * or string
- * @param size the size of that array, list or string
- * @return the value of {@code index}
- * @throws IndexOutOfBoundsException if {@code index} is negative or is not
- * less than {@code size}
- * @throws IllegalArgumentException if {@code size} is negative
- */
- public static int checkElementIndex(int index, int size) {
- return checkElementIndex(index, size, "index");
- }
-
- /**
- * Ensures that {@code index} specifies a valid <i>element</i> in an array,
- * list or string of size {@code size}. An element index may range from zero,
- * inclusive, to {@code size}, exclusive.
- *
- * @param index a user-supplied index identifying an element of an array, list
- * or string
- * @param size the size of that array, list or string
- * @param desc the text to use to describe this index in an error message
- * @return the value of {@code index}
- * @throws IndexOutOfBoundsException if {@code index} is negative or is not
- * less than {@code size}
- * @throws IllegalArgumentException if {@code size} is negative
- */
- public static int checkElementIndex(
- int index, int size, @Nullable String desc) {
- // Carefully optimized for execution by hotspot (explanatory comment above)
- if (index < 0 || index >= size) {
- throw new IndexOutOfBoundsException(badElementIndex(index, size, desc));
- }
- return index;
- }
-
- private static String badElementIndex(int index, int size, String desc) {
- if (index < 0) {
- return format("%s (%s) must not be negative", desc, index);
- } else if (size < 0) {
- throw new IllegalArgumentException("negative size: " + size);
- } else { // index >= size
- return format("%s (%s) must be less than size (%s)", desc, index, size);
- }
- }
-
- /**
- * Ensures that {@code index} specifies a valid <i>position</i> in an array,
- * list or string of size {@code size}. A position index may range from zero
- * to {@code size}, inclusive.
- *
- * @param index a user-supplied index identifying a position in an array, list
- * or string
- * @param size the size of that array, list or string
- * @return the value of {@code index}
- * @throws IndexOutOfBoundsException if {@code index} is negative or is
- * greater than {@code size}
- * @throws IllegalArgumentException if {@code size} is negative
- */
- public static int checkPositionIndex(int index, int size) {
- return checkPositionIndex(index, size, "index");
- }
-
- /**
- * Ensures that {@code index} specifies a valid <i>position</i> in an array,
- * list or string of size {@code size}. A position index may range from zero
- * to {@code size}, inclusive.
- *
- * @param index a user-supplied index identifying a position in an array, list
- * or string
- * @param size the size of that array, list or string
- * @param desc the text to use to describe this index in an error message
- * @return the value of {@code index}
- * @throws IndexOutOfBoundsException if {@code index} is negative or is
- * greater than {@code size}
- * @throws IllegalArgumentException if {@code size} is negative
- */
- public static int checkPositionIndex(
- int index, int size, @Nullable String desc) {
- // Carefully optimized for execution by hotspot (explanatory comment above)
- if (index < 0 || index > size) {
- throw new IndexOutOfBoundsException(badPositionIndex(index, size, desc));
- }
- return index;
- }
-
- private static String badPositionIndex(int index, int size, String desc) {
- if (index < 0) {
- return format("%s (%s) must not be negative", desc, index);
- } else if (size < 0) {
- throw new IllegalArgumentException("negative size: " + size);
- } else { // index > size
- return format("%s (%s) must not be greater than size (%s)",
- desc, index, size);
- }
- }
-
- /**
- * Ensures that {@code start} and {@code end} specify a valid <i>positions</i>
- * in an array, list or string of size {@code size}, and are in order. A
- * position index may range from zero to {@code size}, inclusive.
- *
- * @param start a user-supplied index identifying a starting position in an
- * array, list or string
- * @param end a user-supplied index identifying a ending position in an array,
- * list or string
- * @param size the size of that array, list or string
- * @throws IndexOutOfBoundsException if either index is negative or is
- * greater than {@code size}, or if {@code end} is less than {@code start}
- * @throws IllegalArgumentException if {@code size} is negative
- */
- public static void checkPositionIndexes(int start, int end, int size) {
- // Carefully optimized for execution by hotspot (explanatory comment above)
- if (start < 0 || end < start || end > size) {
- throw new IndexOutOfBoundsException(badPositionIndexes(start, end, size));
- }
- }
-
- private static String badPositionIndexes(int start, int end, int size) {
- if (start < 0 || start > size) {
- return badPositionIndex(start, size, "start index");
- }
- if (end < 0 || end > size) {
- return badPositionIndex(end, size, "end index");
- }
- // end < start
- return format("end index (%s) must not be less than start index (%s)",
- end, start);
- }
-
- /**
- * Substitutes each {@code %s} in {@code template} with an argument. These
- * are matched by position - the first {@code %s} gets {@code args[0]}, etc.
- * If there are more arguments than placeholders, the unmatched arguments will
- * be appended to the end of the formatted message in square braces.
- *
- * @param template a non-null string containing 0 or more {@code %s}
- * placeholders.
- * @param args the arguments to be substituted into the message
- * template. Arguments are converted to strings using
- * {@link String#valueOf(Object)}. Arguments can be null.
- */
- static String format(String template,
- @Nullable Object... args) {
- template = String.valueOf(template); // null -> "null"
-
- // start substituting the arguments into the '%s' placeholders
- StringBuilder builder = new StringBuilder(
- template.length() + 16 * args.length);
- int templateStart = 0;
- int i = 0;
- while (i < args.length) {
- int placeholderStart = template.indexOf("%s", templateStart);
- if (placeholderStart == -1) {
- break;
- }
- builder.append(template.substring(templateStart, placeholderStart));
- builder.append(args[i++]);
- templateStart = placeholderStart + 2;
- }
- builder.append(template.substring(templateStart));
-
- // if we run out of placeholders, append the extra args in square braces
- if (i < args.length) {
- builder.append(" [");
- builder.append(args[i++]);
- while (i < args.length) {
- builder.append(", ");
- builder.append(args[i++]);
- }
- builder.append(']');
- }
-
- return builder.toString();
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.api.internal;
-
-/**
- * Copied from commons lang
- */
-public class StringUtils {
-
- public static final String EMPTY = "";
-
- public static String trim(String str) {
- return str == null ? null : str.trim();
- }
-
- public static String removeStart(String str, String remove) {
- if (isEmpty(str) || isEmpty(remove)) {
- return str;
- }
- if (str.startsWith(remove)) {
- return str.substring(remove.length());
- }
- return str;
- }
-
- public static String removeEnd(String str, String remove) {
- if (isEmpty(str) || isEmpty(remove)) {
- return str;
- }
- if (str.endsWith(remove)) {
- return str.substring(0, str.length() - remove.length());
- }
- return str;
- }
-
- public static boolean isEmpty(String str) {
- return str == null || str.length() == 0;
- }
-
- public static boolean startsWithIgnoreCase(String str, String prefix) {
- return startsWith(str, prefix, true);
- }
-
- private static boolean startsWith(String str, String prefix, boolean ignoreCase) {
- if (str == null || prefix == null) {
- return (str == null && prefix == null);
- }
- if (prefix.length() > str.length()) {
- return false;
- }
- return str.regionMatches(ignoreCase, 0, prefix, 0, prefix.length());
- }
-
- public static String substring(String str, int start) {
- if (str == null) {
- return null;
- }
-
- // handle negatives, which means last n characters
- if (start < 0) {
- start = str.length() + start; // remember start is negative
- }
-
- if (start < 0) {
- start = 0;
- }
- if (start > str.length()) {
- return EMPTY;
- }
-
- return str.substring(start);
- }
-
- public static boolean isBlank(String str) {
- int strLen;
- if (str == null || (strLen = str.length()) == 0) {
- return true;
- }
- for (int i = 0; i < strLen; i++) {
- if ((Character.isWhitespace(str.charAt(i)) == false)) {
- return false;
- }
- }
- return true;
- }
-
- public static boolean isNotBlank(String str) {
- return !StringUtils.isBlank(str);
- }
-
- public static String removeEndIgnoreCase(String str, String remove) {
- if (isEmpty(str) || isEmpty(remove)) {
- return str;
- }
- if (endsWithIgnoreCase(str, remove)) {
- return str.substring(0, str.length() - remove.length());
- }
- return str;
- }
-
- public static boolean endsWithIgnoreCase(String str, String suffix) {
- return endsWith(str, suffix, true);
- }
-
- private static boolean endsWith(String str, String suffix, boolean ignoreCase) {
- if (str == null || suffix == null) {
- return (str == null && suffix == null);
- }
- if (suffix.length() > str.length()) {
- return false;
- }
- int strOffset = str.length() - suffix.length();
- return str.regionMatches(ignoreCase, strOffset, suffix, 0, suffix.length());
- }
-
- public static String lowerCase(String str) {
- if (str == null) {
- return null;
- }
- return str.toLowerCase();
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.api.internal;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.regex.Pattern;
-
-/**
- * Implementation of Ant-style matching patterns.
- * Contrary to other implementations (like AntPathMatcher from Spring Framework) it is based on {@link Pattern Java Regular Expressions}.
- * To increase performance it holds an internal cache of all processed patterns.
- * <p>
- * Following rules are applied:
- * <ul>
- * <li>? matches single character</li>
- * <li>* matches zero or more characters</li>
- * <li>** matches zero or more 'directories'</li>
- * </ul>
- * </p>
- * <p>
- * Some examples of patterns:
- * <ul>
- * <li><code>org/T?st.java</code> - matches <code>org/Test.java</code> and also <code>org/Tost.java</code></li>
- * <li><code>org/*.java</code> - matches all <code>.java</code> files in the <code>org</code> directory,
- * e.g. <code>org/Foo.java</code> or <code>org/Bar.java</code></li>
- * <li><code>org/**</code> - matches all files underneath the <code>org</code> directory,
- * e.g. <code>org/Foo.java</code> or <code>org/foo/bar.jsp</code></li>
- * <li><code>org/**/Test.java</code> - matches all <code>Test.java</code> files underneath the <code>org</code> directory,
- * e.g. <code>org/Test.java</code> or <code>org/foo/Test.java</code> or <code>org/foo/bar/Test.java</code></li>
- * <li><code>org/**/*.java</code> - matches all <code>.java</code> files underneath the <code>org</code> directory,
- * e.g. <code>org/Foo.java</code> or <code>org/foo/Bar.java</code> or <code>org/foo/bar/Baz.java</code></li>
- * </ul>
- * </p>
- * <p>
- * Another implementation, which is also based on Java Regular Expressions, can be found in
- * <a href="https://github.com/JetBrains/intellij-community/blob/idea/107.743/platform/util/src/com/intellij/openapi/util/io/FileUtil.java#L847">FileUtil</a>
- * from IntelliJ OpenAPI.
- * </p>
- *
- */
-public class WildcardPattern {
-
- private static final Map<String, WildcardPattern> CACHE = new HashMap<String, WildcardPattern>();
- private static final String SPECIAL_CHARS = "()[]^$.{}+|";
-
- private Pattern pattern;
- private String stringRepresentation;
-
- protected WildcardPattern(String pattern, String directorySeparator) {
- this.stringRepresentation = pattern;
- this.pattern = Pattern.compile(toRegexp(pattern, directorySeparator));
- }
-
- private static String toRegexp(String antPattern, String directorySeparator) {
- final String escapedDirectorySeparator = '\\' + directorySeparator;
-
- final StringBuilder sb = new StringBuilder(antPattern.length());
-
- sb.append('^');
-
- int i = antPattern.startsWith("/") || antPattern.startsWith("\\") ? 1 : 0;
- while (i < antPattern.length()) {
- final char ch = antPattern.charAt(i);
-
- if (SPECIAL_CHARS.indexOf(ch) != -1) {
- // Escape regexp-specific characters
- sb.append('\\').append(ch);
- } else if (ch == '*') {
- if (i + 1 < antPattern.length() && antPattern.charAt(i + 1) == '*') {
- // Double asterisk
- // Zero or more directories
- if (i + 2 < antPattern.length() && isSlash(antPattern.charAt(i + 2))) {
- sb.append("(?:.*").append(escapedDirectorySeparator).append("|)");
- i += 2;
- } else {
- sb.append(".*");
- i += 1;
- }
- } else {
- // Single asterisk
- // Zero or more characters excluding directory separator
- sb.append("[^").append(escapedDirectorySeparator).append("]*?");
- }
- } else if (ch == '?') {
- // Any single character excluding directory separator
- sb.append("[^").append(escapedDirectorySeparator).append("]");
- } else if (isSlash(ch)) {
- // Directory separator
- sb.append(escapedDirectorySeparator);
- } else {
- // Single character
- sb.append(ch);
- }
-
- i++;
- }
-
- sb.append('$');
-
- return sb.toString();
- }
-
- private static boolean isSlash(char ch) {
- return ch == '/' || ch == '\\';
- }
-
- /**
- * Returns string representation of this pattern.
- *
- * @since 2.5
- */
- @Override
- public String toString() {
- return stringRepresentation;
- }
-
- /**
- * Returns true if specified value matches this pattern.
- */
- public boolean match(String value) {
- value = StringUtils.removeStart(value, "/");
- value = StringUtils.removeEnd(value, "/");
- return pattern.matcher(value).matches();
- }
-
- /**
- * Returns true if specified value matches one of specified patterns.
- *
- * @since 2.4
- */
- public static boolean match(WildcardPattern[] patterns, String value) {
- for (WildcardPattern pattern : patterns) {
- if (pattern.match(value)) {
- return true;
- }
- }
- return false;
- }
-
- /**
- * Creates pattern with "/" as a directory separator.
- *
- * @see #create(String, String)
- */
- public static WildcardPattern create(String pattern) {
- return create(pattern, "/");
- }
-
- /**
- * Creates array of patterns with "/" as a directory separator.
- *
- * @see #create(String, String)
- */
- public static WildcardPattern[] create(String[] patterns) {
- if (patterns == null) {
- return new WildcardPattern[0];
- }
- WildcardPattern[] exclusionPAtterns = new WildcardPattern[patterns.length];
- for (int i = 0; i < patterns.length; i++) {
- exclusionPAtterns[i] = create(patterns[i]);
- }
- return exclusionPAtterns;
- }
-
- /**
- * Creates pattern with specified separator for directories.
- * <p>
- * This is used to match Java-classes, i.e. <code>org.foo.Bar</code> against <code>org/**</code>.
- * <b>However usage of character other than "/" as a directory separator is misleading and should be avoided,
- * so method {@link #create(String)} is preferred over this one.</b>
- * </p>
- * <p>
- * Also note that no matter whether forward or backward slashes were used in the <code>antPattern</code>
- * the returned pattern will use <code>directorySeparator</code>.
- * Thus to match Windows-style path "dir\file.ext" against pattern "dir/file.ext" normalization should be performed.
- * </p>
- */
- public static WildcardPattern create(String pattern, String directorySeparator) {
- String key = pattern + directorySeparator;
- WildcardPattern wildcardPattern = CACHE.get(key);
- if (wildcardPattern == null) {
- wildcardPattern = new WildcardPattern(pattern, directorySeparator);
- CACHE.put(key, wildcardPattern);
- }
- return wildcardPattern;
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.batch.api.internal;
\ No newline at end of file
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.api.languages;
-
-import java.util.Arrays;
-import java.util.Collection;
-
-public final class Language {
-
- private final String key, name;
- private final String[] fileSuffixes;
-
- public Language(String key, String name, String... fileSuffixes) {
- this.key = key;
- this.name = name;
- this.fileSuffixes = fileSuffixes;
- }
-
- /**
- * For example "java".
- */
- public String key() {
- return key;
- }
-
- /**
- * For example "Java"
- */
- public String name() {
- return name;
- }
-
- /**
- * For example ["jav", "java"].
- */
- public Collection<String> fileSuffixes() {
- return Arrays.asList(fileSuffixes);
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.batch.api.languages;
\ No newline at end of file
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.api.measures;
-
-import java.io.Serializable;
-
-public interface Metric<G extends Serializable> {
-
- Class<G> type();
-
- String key();
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.batch.api.measures;
\ No newline at end of file
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.api.rules;
-
-public class QProfile {
- private final String name, language;
- private final Integer version;
-
- public QProfile(String name, String language, Integer version) {
- this.name = name;
- this.language = language;
- this.version = version;
- }
-
- public String name() {
- return name;
- }
-
- public String language() {
- return language;
- }
-
- public Integer version() {
- return version;
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.api.rules;
-
-import java.io.Serializable;
-
-/**
- * Key of a rule. Unique among all the rule repositories.
- *
- * @since 3.6
- */
-public class RuleKey implements Serializable {
- private final String repository, rule;
-
- protected RuleKey(String repositoryKey, String ruleKey) {
- this.repository = repositoryKey;
- this.rule = ruleKey;
- }
-
- /**
- * Create a key. Parameters are NOT null.
- */
- public static RuleKey of(String repository, String rule) {
- return new RuleKey(repository, rule);
- }
-
- /**
- * Never null
- */
- public String repository() {
- return repository;
- }
-
- /**
- * Never null
- */
- public String rule() {
- return rule;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
- RuleKey ruleKey = (RuleKey) o;
- if (!repository.equals(ruleKey.repository)) {
- return false;
- }
- if (!rule.equals(ruleKey.rule)) {
- return false;
- }
- return true;
- }
-
- @Override
- public int hashCode() {
- int result = repository.hashCode();
- result = 31 * result + rule.hashCode();
- return result;
- }
-
- /**
- * Format is "repository:rule", for example "squid:AvoidCycle"
- */
- @Override
- public String toString() {
- return String.format("%s:%s", repository, rule);
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.batch.api.rules;
\ No newline at end of file
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.fest.assertions.Assertions;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.FilePredicates;
-import org.sonar.api.batch.fs.InputFile;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.Arrays;
-import java.util.Collections;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class DefaultFilePredicatesTest {
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- DefaultInputFile javaFile;
- FilePredicates predicates = new DefaultFilePredicates();
-
- @Before
- public void before() throws IOException {
- javaFile = new DefaultInputFile("src/main/java/struts/Action.java")
- .setFile(temp.newFile("Action.java"))
- .setLanguage("java")
- .setStatus(InputFile.Status.ADDED);
- }
-
- @Test
- public void all() throws Exception {
- Assertions.assertThat(predicates.all().apply(javaFile)).isTrue();
- }
-
- @Test
- public void none() throws Exception {
- assertThat(predicates.none().apply(javaFile)).isFalse();
- }
-
- @Test
- public void matches_inclusion_pattern() throws Exception {
- assertThat(predicates.matchesPathPattern("src/main/**/Action.java").apply(javaFile)).isTrue();
- assertThat(predicates.matchesPathPattern("Action.java").apply(javaFile)).isFalse();
- assertThat(predicates.matchesPathPattern("src/**/*.php").apply(javaFile)).isFalse();
- }
-
- @Test
- public void matches_inclusion_patterns() throws Exception {
- assertThat(predicates.matchesPathPatterns(new String[] {"src/other/**.java", "src/main/**/Action.java"}).apply(javaFile)).isTrue();
- assertThat(predicates.matchesPathPatterns(new String[] {}).apply(javaFile)).isTrue();
- assertThat(predicates.matchesPathPatterns(new String[] {"src/other/**.java", "src/**/*.php"}).apply(javaFile)).isFalse();
- }
-
- @Test
- public void does_not_match_exclusion_pattern() throws Exception {
- assertThat(predicates.doesNotMatchPathPattern("src/main/**/Action.java").apply(javaFile)).isFalse();
- assertThat(predicates.doesNotMatchPathPattern("Action.java").apply(javaFile)).isTrue();
- assertThat(predicates.doesNotMatchPathPattern("src/**/*.php").apply(javaFile)).isTrue();
- }
-
- @Test
- public void does_not_match_exclusion_patterns() throws Exception {
- assertThat(predicates.doesNotMatchPathPatterns(new String[] {}).apply(javaFile)).isTrue();
- assertThat(predicates.doesNotMatchPathPatterns(new String[] {"src/other/**.java", "src/**/*.php"}).apply(javaFile)).isTrue();
- assertThat(predicates.doesNotMatchPathPatterns(new String[] {"src/other/**.java", "src/main/**/Action.java"}).apply(javaFile)).isFalse();
- }
-
- @Test
- public void has_relative_path() throws Exception {
- assertThat(predicates.hasRelativePath("src/main/java/struts/Action.java").apply(javaFile)).isTrue();
- assertThat(predicates.hasRelativePath("src/main/java/struts/Other.java").apply(javaFile)).isFalse();
-
- // path is normalized
- assertThat(predicates.hasRelativePath("src/main/java/../java/struts/Action.java").apply(javaFile)).isTrue();
-
- assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\Action.java").apply(javaFile)).isTrue();
- assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\Other.java").apply(javaFile)).isFalse();
- assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\..\\struts\\Action.java").apply(javaFile)).isTrue();
- }
-
- @Test
- public void has_absolute_path() throws Exception {
- String path = javaFile.file().getAbsolutePath();
- assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue();
- assertThat(predicates.hasAbsolutePath(path.replaceAll("/", "\\\\")).apply(javaFile)).isTrue();
-
- assertThat(predicates.hasAbsolutePath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse();
- assertThat(predicates.hasAbsolutePath("src/main/java/struts/Action.java").apply(javaFile)).isFalse();
- }
-
- @Test
- public void has_path() throws Exception {
- // is relative path
- assertThat(predicates.hasPath("src/main/java/struts/Action.java").apply(javaFile)).isTrue();
- assertThat(predicates.hasPath("src/main/java/struts/Other.java").apply(javaFile)).isFalse();
-
- // is absolute path
- String path = javaFile.file().getAbsolutePath();
- assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue();
- assertThat(predicates.hasPath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse();
- }
-
- @Test
- public void is_file() throws Exception {
- // relative file
- assertThat(predicates.is(new File(javaFile.relativePath())).apply(javaFile)).isTrue();
-
- // absolute file
- assertThat(predicates.is(javaFile.file()).apply(javaFile)).isTrue();
- assertThat(predicates.is(javaFile.file().getAbsoluteFile()).apply(javaFile)).isTrue();
- assertThat(predicates.is(javaFile.file().getCanonicalFile()).apply(javaFile)).isTrue();
- assertThat(predicates.is(new File(javaFile.file().toURI())).apply(javaFile)).isTrue();
- assertThat(predicates.is(temp.newFile()).apply(javaFile)).isFalse();
- }
-
- @Test
- public void has_language() throws Exception {
- assertThat(predicates.hasLanguage("java").apply(javaFile)).isTrue();
- assertThat(predicates.hasLanguage("php").apply(javaFile)).isFalse();
- }
-
- @Test
- public void has_languages() throws Exception {
- assertThat(predicates.hasLanguages(Arrays.asList("java", "php")).apply(javaFile)).isTrue();
- assertThat(predicates.hasLanguages(Arrays.asList("cobol", "php")).apply(javaFile)).isFalse();
- assertThat(predicates.hasLanguages(Collections.<String>emptyList()).apply(javaFile)).isTrue();
- }
-
- @Test
- public void has_status() throws Exception {
- assertThat(predicates.hasStatus(InputFile.Status.ADDED).apply(javaFile)).isTrue();
- assertThat(predicates.hasStatus(InputFile.Status.CHANGED).apply(javaFile)).isFalse();
- }
-
- @Test
- public void has_type() throws Exception {
- assertThat(predicates.hasType(InputFile.Type.MAIN).apply(javaFile)).isTrue();
- assertThat(predicates.hasType(InputFile.Type.TEST).apply(javaFile)).isFalse();
- }
-
- @Test
- public void not() throws Exception {
- assertThat(predicates.not(predicates.hasType(InputFile.Type.MAIN)).apply(javaFile)).isFalse();
- assertThat(predicates.not(predicates.hasType(InputFile.Type.TEST)).apply(javaFile)).isTrue();
- }
-
- @Test
- public void and() throws Exception {
- // empty
- assertThat(predicates.and().apply(javaFile)).isTrue();
- assertThat(predicates.and(new FilePredicate[0]).apply(javaFile)).isTrue();
- assertThat(predicates.and(Collections.<FilePredicate>emptyList()).apply(javaFile)).isTrue();
-
- // two arguments
- assertThat(predicates.and(predicates.all(), predicates.all()).apply(javaFile)).isTrue();
- assertThat(predicates.and(predicates.all(), predicates.none()).apply(javaFile)).isFalse();
- assertThat(predicates.and(predicates.none(), predicates.all()).apply(javaFile)).isFalse();
-
- // collection
- assertThat(predicates.and(Arrays.asList(predicates.all(), predicates.all())).apply(javaFile)).isTrue();
- assertThat(predicates.and(Arrays.asList(predicates.all(), predicates.none())).apply(javaFile)).isFalse();
-
- // array
- assertThat(predicates.and(new FilePredicate[] {predicates.all(), predicates.all()}).apply(javaFile)).isTrue();
- assertThat(predicates.and(new FilePredicate[] {predicates.all(), predicates.none()}).apply(javaFile)).isFalse();
- }
-
- @Test
- public void or() throws Exception {
- // empty
- assertThat(predicates.or().apply(javaFile)).isTrue();
- assertThat(predicates.or(new FilePredicate[0]).apply(javaFile)).isTrue();
- assertThat(predicates.or(Collections.<FilePredicate>emptyList()).apply(javaFile)).isTrue();
-
- // two arguments
- assertThat(predicates.or(predicates.all(), predicates.all()).apply(javaFile)).isTrue();
- assertThat(predicates.or(predicates.all(), predicates.none()).apply(javaFile)).isTrue();
- assertThat(predicates.or(predicates.none(), predicates.all()).apply(javaFile)).isTrue();
- assertThat(predicates.or(predicates.none(), predicates.none()).apply(javaFile)).isFalse();
-
- // collection
- assertThat(predicates.or(Arrays.asList(predicates.all(), predicates.all())).apply(javaFile)).isTrue();
- assertThat(predicates.or(Arrays.asList(predicates.all(), predicates.none())).apply(javaFile)).isTrue();
- assertThat(predicates.or(Arrays.asList(predicates.none(), predicates.none())).apply(javaFile)).isFalse();
-
- // array
- assertThat(predicates.or(new FilePredicate[] {predicates.all(), predicates.all()}).apply(javaFile)).isTrue();
- assertThat(predicates.or(new FilePredicate[] {predicates.all(), predicates.none()}).apply(javaFile)).isTrue();
- assertThat(predicates.or(new FilePredicate[] {predicates.none(), predicates.none()}).apply(javaFile)).isFalse();
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.rules.TemporaryFolder;
-
-import java.io.File;
-import java.nio.charset.Charset;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class DefaultFileSystemTest {
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- @Test
- public void test_directories() throws Exception {
- DefaultFileSystem fs = new DefaultFileSystem();
-
- File basedir = temp.newFolder();
- fs.setBaseDir(basedir);
- assertThat(fs.baseDir()).isAbsolute().isDirectory().exists();
- assertThat(fs.baseDir().getCanonicalPath()).isEqualTo(basedir.getCanonicalPath());
-
- File workdir = temp.newFolder();
- fs.setWorkDir(workdir);
- assertThat(fs.workDir()).isAbsolute().isDirectory().exists();
- assertThat(fs.workDir().getCanonicalPath()).isEqualTo(workdir.getCanonicalPath());
- }
-
- @Test
- public void test_encoding() throws Exception {
- DefaultFileSystem fs = new DefaultFileSystem();
-
- assertThat(fs.isDefaultJvmEncoding()).isTrue();
- assertThat(fs.encoding()).isEqualTo(Charset.defaultCharset());
-
- fs.setEncoding(Charset.forName("ISO-8859-1"));
- assertThat(fs.encoding()).isEqualTo(Charset.forName("ISO-8859-1"));
- assertThat(fs.isDefaultJvmEncoding()).isFalse();
- }
-
- @Test
- public void add_languages() throws Exception {
- DefaultFileSystem fs = new DefaultFileSystem();
-
- assertThat(fs.languages()).isEmpty();
-
- fs.addLanguages("java", "php", "cobol");
- assertThat(fs.languages()).containsOnly("cobol", "java", "php");
- }
-
- @Test
- public void files() throws Exception {
- DefaultFileSystem fs = new DefaultFileSystem();
-
- assertThat(fs.inputFiles(fs.predicates().all())).isEmpty();
-
- fs.add(new DefaultInputFile("src/Foo.php").setLanguage("php").setFile(temp.newFile()));
- fs.add(new DefaultInputFile("src/Bar.java").setLanguage("java").setFile(temp.newFile()));
- fs.add(new DefaultInputFile("src/Baz.java").setLanguage("java").setFile(temp.newFile()));
-
- // no language
- fs.add(new DefaultInputFile("src/readme.txt").setFile(temp.newFile()));
-
- assertThat(fs.inputFile(fs.predicates().hasRelativePath("src/Bar.java"))).isNotNull();
- assertThat(fs.inputFile(fs.predicates().hasRelativePath("does/not/exist"))).isNull();
-
- assertThat(fs.files(fs.predicates().all())).hasSize(4);
- assertThat(fs.files(fs.predicates().hasLanguage("java"))).hasSize(2);
- assertThat(fs.files(fs.predicates().hasLanguage("cobol"))).isEmpty();
-
- assertThat(fs.hasFiles(fs.predicates().all())).isTrue();
- assertThat(fs.hasFiles(fs.predicates().hasLanguage("java"))).isTrue();
- assertThat(fs.hasFiles(fs.predicates().hasLanguage("cobol"))).isFalse();
-
- assertThat(fs.inputFiles(fs.predicates().all())).hasSize(4);
- assertThat(fs.inputFiles(fs.predicates().hasLanguage("php"))).hasSize(1);
- assertThat(fs.inputFiles(fs.predicates().hasLanguage("java"))).hasSize(2);
- assertThat(fs.inputFiles(fs.predicates().hasLanguage("cobol"))).isEmpty();
-
- assertThat(fs.languages()).containsOnly("java", "php");
- }
-
- @Test
- public void input_file_returns_null_if_file_not_found() throws Exception {
- DefaultFileSystem fs = new DefaultFileSystem();
- assertThat(fs.inputFile(fs.predicates().hasRelativePath("src/Bar.java"))).isNull();
- assertThat(fs.inputFile(fs.predicates().hasLanguage("cobol"))).isNull();
- }
-
- @Test
- public void input_file_fails_if_too_many_results() throws Exception {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("expected one element");
-
- DefaultFileSystem fs = new DefaultFileSystem();
- fs.add(new DefaultInputFile("src/Bar.java").setLanguage("java").setFile(temp.newFile()));
- fs.add(new DefaultInputFile("src/Baz.java").setLanguage("java").setFile(temp.newFile()));
-
- fs.inputFile(fs.predicates().all());
- }
-
- @Test
- public void input_file_supports_non_indexed_predicates() throws Exception {
- DefaultFileSystem fs = new DefaultFileSystem();
- fs.add(new DefaultInputFile("src/Bar.java").setLanguage("java").setFile(temp.newFile()));
-
- // it would fail if more than one java file
- assertThat(fs.inputFile(fs.predicates().hasLanguage("java"))).isNotNull();
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.InputFile;
-
-import java.io.File;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class DefaultInputFileTest {
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Test
- public void test() throws Exception {
- DefaultInputFile inputFile = new DefaultInputFile("src/Foo.php")
- .setFile(temp.newFile("Foo.php"))
- .setKey("ABCDE")
- .setHash("1234")
- .setLines(42)
- .setLanguage("php")
- .setStatus(InputFile.Status.ADDED)
- .setType(InputFile.Type.TEST);
-
- assertThat(inputFile.relativePath()).isEqualTo("src/Foo.php");
- assertThat(new File(inputFile.relativePath())).isRelative();
- assertThat(inputFile.absolutePath()).endsWith("Foo.php");
- 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.hash()).isEqualTo("1234");
- }
-
- @Test
- public void test_equals_and_hashcode() throws Exception {
- DefaultInputFile f1 = new DefaultInputFile("src/Foo.php");
- DefaultInputFile f1a = new DefaultInputFile("src/Foo.php");
- DefaultInputFile f2 = new DefaultInputFile("src/Bar.php");
-
- 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("src/Foo.php").setAbsolutePath("/path/to/src/Foo.php");
- assertThat(file.toString()).isEqualTo("[relative=src/Foo.php, abs=/path/to/src/Foo.php]");
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.fs.internal;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.fs.InputFile;
-
-import java.io.File;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class PathPatternTest {
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Test
- public void match_relative_path() throws Exception {
- PathPattern pattern = PathPattern.create("**/*Foo.java");
- assertThat(pattern.toString()).isEqualTo("**/*Foo.java");
-
- File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.java");
- InputFile inputFile = new DefaultInputFile("src/main/java/org/MyFoo.java").setFile(file);
- assertThat(pattern.match(inputFile)).isTrue();
-
- // case sensitive by default
- file = new File(temp.newFolder(), "src/main/java/org/MyFoo.JAVA");
- inputFile = new DefaultInputFile("src/main/java/org/MyFoo.JAVA").setFile(file);
- assertThat(pattern.match(inputFile)).isFalse();
-
- file = new File(temp.newFolder(), "src/main/java/org/Other.java");
- inputFile = new DefaultInputFile("src/main/java/org/Other.java").setFile(file);
- assertThat(pattern.match(inputFile)).isFalse();
- }
-
- @Test
- public void match_relative_path_and_insensitive_file_extension() throws Exception {
- PathPattern pattern = PathPattern.create("**/*Foo.java");
-
- File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.JAVA");
- InputFile inputFile = new DefaultInputFile("src/main/java/org/MyFoo.JAVA").setFile(file);
- assertThat(pattern.match(inputFile, false)).isTrue();
-
- file = new File(temp.newFolder(), "src/main/java/org/Other.java");
- inputFile = new DefaultInputFile("src/main/java/org/Other.java").setFile(file);
- assertThat(pattern.match(inputFile, false)).isFalse();
- }
-
- @Test
- public void match_absolute_path() throws Exception {
- PathPattern pattern = PathPattern.create("file:**/src/main/**Foo.java");
- assertThat(pattern.toString()).isEqualTo("file:**/src/main/**Foo.java");
-
- File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.java");
- InputFile inputFile = new DefaultInputFile("src/main/java/org/MyFoo.java").setFile(file);
- assertThat(pattern.match(inputFile)).isTrue();
-
- // case sensitive by default
- file = new File(temp.newFolder(), "src/main/java/org/MyFoo.JAVA");
- inputFile = new DefaultInputFile("src/main/java/org/MyFoo.JAVA").setFile(file);
- assertThat(pattern.match(inputFile)).isFalse();
-
- file = new File(temp.newFolder(), "src/main/java/org/Other.java");
- inputFile = new DefaultInputFile("src/main/java/org/Other.java").setFile(file);
- assertThat(pattern.match(inputFile)).isFalse();
- }
-
- @Test
- public void match_absolute_path_and_insensitive_file_extension() throws Exception {
- PathPattern pattern = PathPattern.create("file:**/src/main/**Foo.java");
- assertThat(pattern.toString()).isEqualTo("file:**/src/main/**Foo.java");
-
- File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.JAVA");
- InputFile inputFile = new DefaultInputFile("src/main/java/org/MyFoo.JAVA").setFile(file);
- assertThat(pattern.match(inputFile, false)).isTrue();
-
- file = new File(temp.newFolder(), "src/main/java/org/Other.JAVA");
- inputFile = new DefaultInputFile("src/main/java/org/Other.JAVA").setFile(file);
- assertThat(pattern.match(inputFile, false)).isFalse();
- }
-
- @Test
- public void create_array_of_patterns() throws Exception {
- PathPattern[] patterns = PathPattern.create(new String[]{
- "**/src/main/**Foo.java",
- "file:**/src/main/**Bar.java"
- });
- assertThat(patterns).hasSize(2);
- assertThat(patterns[0].toString()).isEqualTo("**/src/main/**Foo.java");
- assertThat(patterns[1].toString()).isEqualTo("file:**/src/main/**Bar.java");
- }
-}
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-java-api</artifactId>
</dependency>
- <dependency>
- <groupId>org.codehaus.sonar</groupId>
- <artifactId>sonar-batch-plugin-api</artifactId>
- </dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-plugin-api</artifactId>
*/
package org.sonar.batch.bootstrap;
+import org.sonar.api.batch.analyzer.Analyzer;
+import org.sonar.api.batch.analyzer.AnalyzerContext;
+
import com.google.common.collect.Lists;
import org.apache.commons.lang.ClassUtils;
import org.sonar.api.batch.CheckProject;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.resources.Project;
-import org.sonar.batch.api.analyzer.Analyzer;
-import org.sonar.batch.api.analyzer.AnalyzerContext;
import org.sonar.batch.scan.SensorWrapper;
import java.util.Collection;
}
@Override
- public Measure getMeasure(Resource resource, org.sonar.batch.api.measures.Metric<?> metric) {
+ public Measure getMeasure(Resource resource, org.sonar.api.batch.measures.Metric<?> metric) {
return getMeasures(resource, MeasuresFilters.metric(metric));
}
*/
package org.sonar.batch.languages;
+import org.sonar.api.batch.languages.Language;
+
import org.sonar.api.resources.Languages;
-import org.sonar.batch.api.languages.Language;
import javax.annotation.CheckForNull;
*/
package org.sonar.batch.languages;
+import org.sonar.api.batch.languages.Language;
+
import org.sonar.api.BatchComponent;
-import org.sonar.batch.api.languages.Language;
import javax.annotation.CheckForNull;
*/
package org.sonar.batch.phases;
+import org.sonar.api.batch.analyzer.Analyzer;
+
import org.apache.commons.lang.ClassUtils;
import org.sonar.api.BatchExtension;
import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.batch.Sensor;
-import org.sonar.batch.api.analyzer.Analyzer;
import org.sonar.batch.bootstrap.ExtensionMatcher;
/**
*/
package org.sonar.batch.rule;
+import org.sonar.api.batch.rules.QProfile;
+
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import org.picocontainer.injectors.ProviderAdapter;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.RuleParam;
-import org.sonar.batch.api.rules.QProfile;
import org.sonar.batch.rules.QProfileWithId;
import org.sonar.core.qualityprofile.db.ActiveRuleDao;
import org.sonar.core.qualityprofile.db.ActiveRuleDto;
*/
package org.sonar.batch.rule;
+import org.sonar.api.batch.languages.Language;
+import org.sonar.api.batch.rules.QProfile;
+
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.BatchComponent;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.MessageException;
-import org.sonar.batch.api.languages.Language;
-import org.sonar.batch.api.rules.QProfile;
import org.sonar.batch.languages.LanguagesReferential;
import org.sonar.batch.rules.QProfilesReferential;
*/
package org.sonar.batch.rule;
+import org.sonar.api.batch.rules.QProfile;
+
import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.MessageException;
-import org.sonar.batch.api.rules.QProfile;
public class QProfileVerifier implements BatchComponent {
*/
package org.sonar.batch.rule;
+import org.sonar.api.batch.rules.QProfile;
+
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.picocontainer.injectors.ProviderAdapter;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.RulePriority;
-import org.sonar.batch.api.rules.QProfile;
import java.util.Collection;
import java.util.Map;
*/
package org.sonar.batch.rules;
-import org.sonar.batch.api.rules.QProfile;
+import org.sonar.api.batch.rules.QProfile;
+
import org.sonar.core.qualityprofile.db.QualityProfileDao;
import org.sonar.core.qualityprofile.db.QualityProfileDto;
*/
package org.sonar.batch.rules;
-import org.sonar.batch.api.rules.QProfile;
+import org.sonar.api.batch.rules.QProfile;
public class QProfileWithId extends QProfile {
private final int id;
*/
package org.sonar.batch.rules;
+import org.sonar.api.batch.rules.QProfile;
+
import org.sonar.api.BatchComponent;
-import org.sonar.batch.api.rules.QProfile;
import javax.annotation.CheckForNull;
package org.sonar.batch.scan;
import org.sonar.api.batch.SensorContext;
+import org.sonar.api.batch.analyzer.AnalyzerContext;
+import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
+import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
+import org.sonar.api.batch.analyzer.measure.AnalyzerMeasureBuilder;
+import org.sonar.api.batch.analyzer.measure.internal.DefaultAnalyzerMeasureBuilder;
import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measures.Metric;
import org.sonar.api.component.ResourcePerspectives;
import org.sonar.api.issue.Issuable;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.rule.RuleKey;
-import org.sonar.batch.api.analyzer.AnalyzerContext;
-import org.sonar.batch.api.analyzer.issue.AnalyzerIssue;
-import org.sonar.batch.api.analyzer.measure.AnalyzerMeasure;
-import org.sonar.batch.api.measures.Metric;
import java.io.Serializable;
import java.util.Collection;
this.perspectives = perspectives;
}
+ @Override
+ public <G extends Serializable> AnalyzerMeasureBuilder<G> measureBuilder() {
+ return new DefaultAnalyzerMeasureBuilder<G>();
+ }
+
@Override
public AnalyzerMeasure<?> getMeasure(String metricKey) {
Metric<?> m = metricFinder.findByKey(metricKey);
if (measure == null) {
return null;
}
- return AnalyzerMeasure.<G>builder()
+ return this.<G>measureBuilder()
.onProject()
.forMetric(metric)
.withValue(measure.value())
if (measure == null) {
return null;
}
- return AnalyzerMeasure.<G>builder()
+ return this.<G>measureBuilder()
.onFile(file)
.forMetric(metric)
.withValue(measure.value())
*/
package org.sonar.batch.scan;
+import org.sonar.api.batch.languages.Language;
+
import org.picocontainer.Startable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.MessageException;
-import org.sonar.batch.api.languages.Language;
import org.sonar.batch.languages.LanguagesReferential;
/**
// Example : C# plugin adds sub-projects at runtime, even if they are not defined in root pom.
return !ExtensionUtils.isMavenExtensionOnly(extension) || module.getPom() != null;
}
- return ExtensionUtils.isType(extension, org.sonar.batch.api.BatchComponent.class)
- && ExtensionUtils.isInstantiationStrategy(extension, org.sonar.batch.api.InstantiationStrategy.PER_PROJECT);
+ return false;
}
});
}
}
private void projectBootstrap() {
- ProjectReactor reactor;
- // OK, not present, so look for a custom ProjectBootstrapper
- ProjectBootstrapper bootstrapper = getComponentByType(ProjectBootstrapper.class);
- Settings settings = getComponentByType(Settings.class);
- if (bootstrapper == null
- // Starting from Maven plugin 2.3 then only DefaultProjectBootstrapper should be used.
- || "true".equals(settings.getString("sonar.mojoUseRunner"))) {
- // Use default SonarRunner project bootstrapper
- ProjectReactorBuilder builder = getComponentByType(ProjectReactorBuilder.class);
- reactor = builder.execute();
- } else {
- reactor = bootstrapper.bootstrap();
- }
+ // Views pass a custom ProjectReactor
+ ProjectReactor reactor = getComponentByType(ProjectReactor.class);
if (reactor == null) {
- throw new SonarException(bootstrapper + " has returned null as ProjectReactor");
+ // OK, not present, so look for a deprecated custom ProjectBootstrapper for old versions of SQ Runner
+ ProjectBootstrapper bootstrapper = getComponentByType(ProjectBootstrapper.class);
+ Settings settings = getComponentByType(Settings.class);
+ if (bootstrapper == null
+ // Starting from Maven plugin 2.3 then only DefaultProjectBootstrapper should be used.
+ || "true".equals(settings.getString("sonar.mojoUseRunner"))) {
+ // Use default SonarRunner project bootstrapper
+ ProjectReactorBuilder builder = getComponentByType(ProjectReactorBuilder.class);
+ reactor = builder.execute();
+ } else {
+ reactor = bootstrapper.bootstrap();
+ }
+ if (reactor == null) {
+ throw new SonarException(bootstrapper + " has returned null as ProjectReactor");
+ }
+ add(reactor);
}
- add(reactor);
}
private void addBatchComponents() {
*/
package org.sonar.batch.scan;
+import org.sonar.batch.scan2.ProjectScanContainer;
+
import org.sonar.api.CoreProperties;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.task.Task;
import org.sonar.batch.bootstrap.BootstrapProperties;
import org.sonar.batch.bootstrap.TaskContainer;
import org.sonar.batch.phases.Phases;
-import org.sonar.batch.scan2.ProjectScanContainer;
public class ScanTask implements Task {
public static final TaskDefinition DEFINITION = TaskDefinition.builder()
import org.sonar.api.batch.DependsUpon;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
+import org.sonar.api.batch.analyzer.Analyzer;
+import org.sonar.api.batch.analyzer.AnalyzerContext;
+import org.sonar.api.batch.analyzer.internal.DefaultAnalyzerDescriptor;
import org.sonar.api.batch.fs.FileSystem;
+import org.sonar.api.batch.measures.Metric;
import org.sonar.api.resources.Project;
-import org.sonar.batch.api.analyzer.Analyzer;
-import org.sonar.batch.api.analyzer.AnalyzerContext;
-import org.sonar.batch.api.measures.Metric;
import java.util.Arrays;
import java.util.List;
private Analyzer analyzer;
private AnalyzerContext adaptor;
private FileSystem fs;
+ private DefaultAnalyzerDescriptor descriptor;
public SensorWrapper(Analyzer analyzer, AnalyzerContext adaptor, FileSystem fs) {
this.analyzer = analyzer;
+ descriptor = new DefaultAnalyzerDescriptor();
+ analyzer.describe(descriptor);
this.adaptor = adaptor;
this.fs = fs;
}
@DependedUpon
public List<Metric<?>> provides() {
- return Arrays.asList(analyzer.describe().provides());
+ return Arrays.asList(descriptor.provides());
}
@DependsUpon
public List<Metric<?>> depends() {
- return Arrays.asList(analyzer.describe().dependsOn());
+ return Arrays.asList(descriptor.dependsOn());
}
@Override
public boolean shouldExecuteOnProject(Project project) {
- if (!analyzer.describe().languages().isEmpty()) {
- if (project.getLanguageKey() != null && !analyzer.describe().languages().contains(project.getLanguageKey())) {
+ if (!descriptor.languages().isEmpty()) {
+ if (project.getLanguageKey() != null && !descriptor.languages().contains(project.getLanguageKey())) {
return false;
}
boolean hasFile = false;
- for (String languageKey : analyzer.describe().languages()) {
+ for (String languageKey : descriptor.languages()) {
hasFile |= fs.hasFiles(fs.predicates().hasLanguage(languageKey));
}
if (!hasFile) {
*/
package org.sonar.batch.scan.filesystem;
+import org.sonar.api.batch.languages.Language;
+
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.sonar.api.batch.fs.internal.PathPattern;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.MessageException;
-import org.sonar.batch.api.languages.Language;
import org.sonar.batch.languages.LanguagesReferential;
import javax.annotation.CheckForNull;
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.scan2;
+
+import com.google.common.base.Preconditions;
+import org.sonar.api.BatchComponent;
+import org.sonar.api.batch.analyzer.measure.internal.DefaultAnalyzerMeasure;
+import org.sonar.batch.index.Cache;
+import org.sonar.batch.index.Cache.Entry;
+import org.sonar.batch.index.Caches;
+
+/**
+ * Cache of all measures. This cache is shared amongst all project modules.
+ */
+public class AnalyzerMeasureCache implements BatchComponent {
+
+ private final Cache<DefaultAnalyzerMeasure> cache;
+
+ public AnalyzerMeasureCache(Caches caches) {
+ cache = caches.createCache("measures");
+ }
+
+ public Iterable<Entry<DefaultAnalyzerMeasure>> entries() {
+ return cache.entries();
+ }
+
+ public DefaultAnalyzerMeasure<?> byMetric(String resourceKey, String metricKey) {
+ return cache.get(resourceKey, metricKey);
+ }
+
+ public AnalyzerMeasureCache put(String resourceKey, DefaultAnalyzerMeasure<?> measure) {
+ Preconditions.checkNotNull(resourceKey);
+ Preconditions.checkNotNull(measure.metricKey());
+ cache.put(resourceKey, measure.metricKey(), measure);
+ return this;
+ }
+
+ public boolean contains(String resourceKey, DefaultAnalyzerMeasure<?> measure) {
+ Preconditions.checkNotNull(resourceKey);
+ Preconditions.checkNotNull(measure);
+ return cache.containsKey(resourceKey, measure.metricKey());
+ }
+
+ public Iterable<DefaultAnalyzerMeasure> all() {
+ return cache.values();
+ }
+
+}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.BatchComponent;
+import org.sonar.api.batch.analyzer.Analyzer;
+import org.sonar.api.batch.analyzer.AnalyzerContext;
+import org.sonar.api.batch.analyzer.internal.DefaultAnalyzerDescriptor;
import org.sonar.api.platform.ComponentContainer;
-import org.sonar.batch.api.analyzer.Analyzer;
-import org.sonar.batch.api.analyzer.AnalyzerContext;
-import org.sonar.batch.api.analyzer.AnalyzerDescriptor;
import java.util.Collection;
for (Analyzer analyzer : analyzers) {
- AnalyzerDescriptor descriptor = analyzer.describe();
+ DefaultAnalyzerDescriptor descriptor = new DefaultAnalyzerDescriptor();
+ analyzer.describe(descriptor);
LOG.info("Execute analyzer: " + descriptor.name());
*/
package org.sonar.batch.scan2;
+import org.sonar.api.batch.analyzer.AnalyzerContext;
+import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
+import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
+import org.sonar.api.batch.analyzer.measure.AnalyzerMeasureBuilder;
+import org.sonar.api.batch.analyzer.measure.internal.DefaultAnalyzerMeasure;
+import org.sonar.api.batch.analyzer.measure.internal.DefaultAnalyzerMeasureBuilder;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measures.Metric;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.rule.RuleKey;
-import org.sonar.batch.api.analyzer.AnalyzerContext;
-import org.sonar.batch.api.analyzer.issue.AnalyzerIssue;
-import org.sonar.batch.api.analyzer.measure.AnalyzerMeasure;
-import org.sonar.batch.api.measures.Metric;
import org.sonar.batch.issue.ModuleIssues;
import org.sonar.core.component.ComponentKeys;
import org.sonar.core.issue.DefaultIssueBuilder;
public class DefaultAnalyzerContext implements AnalyzerContext {
- private final MeasureCache measureCache;
+ private final AnalyzerMeasureCache measureCache;
private ProjectDefinition def;
private ModuleIssues moduleIssues;
- public DefaultAnalyzerContext(ProjectDefinition def, MeasureCache measureCache, ModuleIssues moduleIssues) {
+ public DefaultAnalyzerContext(ProjectDefinition def, AnalyzerMeasureCache measureCache, ModuleIssues moduleIssues) {
this.def = def;
this.measureCache = measureCache;
this.moduleIssues = moduleIssues;
}
+ @Override
+ public <G extends Serializable> AnalyzerMeasureBuilder<G> measureBuilder() {
+ return new DefaultAnalyzerMeasureBuilder<G>();
+ }
+
@Override
public AnalyzerMeasure getMeasure(String metricKey) {
return measureCache.byMetric(def.getKey(), metricKey);
}
@Override
- public void addMeasure(org.sonar.batch.api.analyzer.measure.AnalyzerMeasure<?> measure) {
+ public void addMeasure(AnalyzerMeasure<?> measure) {
if (measure.inputFile() != null) {
- measureCache.put(ComponentKeys.createEffectiveKey(def.getKey(), measure.inputFile()), measure);
+ measureCache.put(ComponentKeys.createEffectiveKey(def.getKey(), measure.inputFile()), (DefaultAnalyzerMeasure) measure);
} else {
- measureCache.put(def.getKey(), measure);
+ measureCache.put(def.getKey(), (DefaultAnalyzerMeasure) measure);
}
}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.scan2;
-
-import com.google.common.base.Preconditions;
-import org.sonar.api.BatchComponent;
-import org.sonar.batch.api.analyzer.measure.AnalyzerMeasure;
-import org.sonar.batch.index.Cache;
-import org.sonar.batch.index.Cache.Entry;
-import org.sonar.batch.index.Caches;
-
-/**
- * Cache of all measures. This cache is shared amongst all project modules.
- */
-public class MeasureCache implements BatchComponent {
-
- private final Cache<AnalyzerMeasure<?>> cache;
-
- public MeasureCache(Caches caches) {
- cache = caches.createCache("measures");
- }
-
- public Iterable<Entry<AnalyzerMeasure<?>>> entries() {
- return cache.entries();
- }
-
- public AnalyzerMeasure<?> byMetric(String resourceKey, String metricKey) {
- return cache.get(resourceKey, metricKey);
- }
-
- public MeasureCache put(String resourceKey, AnalyzerMeasure<?> measure) {
- Preconditions.checkNotNull(resourceKey);
- Preconditions.checkNotNull(measure.metricKey());
- cache.put(resourceKey, measure.metricKey(), measure);
- return this;
- }
-
- public boolean contains(String resourceKey, AnalyzerMeasure<?> measure) {
- Preconditions.checkNotNull(resourceKey);
- Preconditions.checkNotNull(measure);
- return cache.containsKey(resourceKey, measure.metricKey());
- }
-
- public Iterable<AnalyzerMeasure<?>> all() {
- return cache.values();
- }
-
-}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.sonar.api.BatchComponent;
+import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.rule.CheckFactory;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.scan.filesystem.FileExclusions;
-import org.sonar.batch.api.BatchComponent;
-import org.sonar.batch.api.InstantiationStrategy;
import org.sonar.batch.bootstrap.BatchExtensionDictionnary;
import org.sonar.batch.bootstrap.ExtensionInstaller;
import org.sonar.batch.bootstrap.ExtensionMatcher;
*/
package org.sonar.batch.scan2;
+import org.sonar.api.batch.analyzer.AnalyzerContext;
+
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
-import org.sonar.batch.api.analyzer.AnalyzerContext;
import org.sonar.batch.issue.ignore.scanner.IssueExclusionsLoader;
import org.sonar.batch.phases.SensorsExecutor;
import org.sonar.batch.rule.QProfileVerifier;
package org.sonar.batch.scan2;
import com.google.common.annotations.VisibleForTesting;
+import org.sonar.api.BatchComponent;
import org.sonar.api.CoreProperties;
+import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.batch.bootstrap.ProjectBootstrapper;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.SonarException;
-import org.sonar.batch.api.BatchComponent;
-import org.sonar.batch.api.InstantiationStrategy;
import org.sonar.batch.bootstrap.ExtensionInstaller;
import org.sonar.batch.bootstrap.ExtensionMatcher;
import org.sonar.batch.bootstrap.ExtensionUtils;
ResourceCache.class,
ComponentDataCache.class,
ComponentDataPersister.class,
- MeasureCache.class,
+ AnalyzerMeasureCache.class,
// file system
InputFileCache.class,
*/
package org.sonar.batch.scan2;
-import org.sonar.batch.api.BatchExtension;
+import org.sonar.api.BatchExtension;
public interface ScanTaskObserver extends BatchExtension {
*/
package org.sonar.batch.bootstrap;
+import org.sonar.api.batch.analyzer.AnalyzerContext;
+
import org.junit.Test;
import org.sonar.api.BatchExtension;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.resources.Project;
-import org.sonar.batch.api.analyzer.AnalyzerContext;
import java.util.Collection;
*/
package org.sonar.batch.rule;
+import org.sonar.api.batch.rules.QProfile;
+
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.rule.ActiveRule;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
-import org.sonar.batch.api.rules.QProfile;
import org.sonar.batch.rules.QProfileWithId;
import org.sonar.core.persistence.AbstractDaoTestCase;
import org.sonar.core.qualityprofile.db.ActiveRuleDao;
*/
package org.sonar.batch.rule;
+import org.sonar.api.batch.rules.QProfile;
+
import com.google.common.collect.Lists;
import org.junit.Test;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.utils.MessageException;
-import org.sonar.batch.api.rules.QProfile;
import org.sonar.batch.languages.DeprecatedLanguagesReferential;
import org.sonar.batch.languages.LanguagesReferential;
import org.sonar.batch.rules.DefaultQProfileReferential;
*/
package org.sonar.batch.rule;
+import org.sonar.api.batch.rules.QProfile;
+
import org.junit.Test;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.resources.Project;
import org.sonar.api.test.IsMeasure;
-import org.sonar.batch.api.rules.QProfile;
import org.sonar.batch.rules.QProfileWithId;
import org.sonar.core.persistence.AbstractDaoTestCase;
import org.sonar.core.qualityprofile.db.QualityProfileDao;
*/
package org.sonar.batch.rule;
+import org.sonar.api.batch.rules.QProfile;
+
import org.junit.Test;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.config.Settings;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rules.RuleFinder;
-import org.sonar.batch.api.rules.QProfile;
import org.sonar.batch.rules.QProfileWithId;
import java.util.Arrays;
import com.google.common.collect.Lists;
import org.apache.commons.lang.ClassUtils;
import org.sonar.api.BatchExtension;
+import org.sonar.api.batch.analyzer.Analyzer;
+import org.sonar.api.batch.analyzer.internal.DefaultAnalyzerDescriptor;
import org.sonar.api.batch.maven.DependsUponMavenPlugin;
import org.sonar.api.batch.maven.MavenPluginHandler;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.AnnotationUtils;
import org.sonar.api.utils.dag.DirectAcyclicGraph;
-import org.sonar.batch.api.analyzer.Analyzer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
private static void completeBatchExtensions(ComponentContainer container, List<Object> extensions) {
if (container != null) {
extensions.addAll(container.getComponentsByType(BatchExtension.class));
- extensions.addAll(container.getComponentsByType(org.sonar.batch.api.BatchExtension.class));
completeBatchExtensions(container.getParent(), extensions);
}
}
List<Object> result = new ArrayList<Object>();
result.addAll(evaluateAnnotatedClasses(extension, DependsUpon.class));
if (ClassUtils.isAssignable(extension.getClass(), Analyzer.class)) {
- result.addAll(Arrays.asList(((Analyzer) extension).describe().dependsOn()));
+ DefaultAnalyzerDescriptor descriptor = new DefaultAnalyzerDescriptor();
+ ((Analyzer) extension).describe(descriptor);
+ result.addAll(Arrays.asList(descriptor.dependsOn()));
}
return result;
}
List<Object> result = new ArrayList<Object>();
result.addAll(evaluateAnnotatedClasses(extension, DependedUpon.class));
if (ClassUtils.isAssignable(extension.getClass(), Analyzer.class)) {
- result.addAll(Arrays.asList(((Analyzer) extension).describe().provides()));
+ DefaultAnalyzerDescriptor descriptor = new DefaultAnalyzerDescriptor();
+ ((Analyzer) extension).describe(descriptor);
+ result.addAll(Arrays.asList(descriptor.provides()));
}
return result;
}
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-check-api</artifactId>
</dependency>
- <dependency>
- <groupId>org.codehaus.sonar</groupId>
- <artifactId>sonar-batch-plugin-api</artifactId>
- </dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-colorizer</artifactId>
public abstract Resource addResource(Resource resource);
@CheckForNull
- public abstract Measure getMeasure(Resource resource, org.sonar.batch.api.measures.Metric<?> metric);
+ public abstract Measure getMeasure(Resource resource, org.sonar.api.batch.measures.Metric<?> metric);
@CheckForNull
public abstract <M> M getMeasures(Resource resource, MeasuresFilter<M> filter);
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.analyzer;
+
+import org.sonar.api.BatchExtension;
+
+/**
+ * <p>
+ * An Analyzer is invoked once during the analysis of a project. The analyzer can parse a flat file, connect to a web server... Analyzers are
+ * used to add measure and issues at file level.
+ * </p>
+ *
+ * <p>
+ * For example the Cobertura Analyzer parses Cobertura report and saves the first-level of measures on files.
+ * </p>
+ *
+ * @since 4.4
+ */
+public interface Analyzer extends BatchExtension {
+
+ /**
+ * Populate {@link AnalyzerDescriptor} of this analyzer.
+ */
+ void describe(AnalyzerDescriptor descriptor);
+
+ /**
+ * The actual analyzer code.
+ */
+ void analyse(AnalyzerContext context);
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.analyzer;
+
+import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
+import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
+import org.sonar.api.batch.analyzer.measure.AnalyzerMeasureBuilder;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measures.Metric;
+
+import javax.annotation.CheckForNull;
+
+import java.io.Serializable;
+import java.util.Collection;
+
+/**
+ * @since 4.4
+ */
+public interface AnalyzerContext {
+
+ // ----------- MEASURES --------------
+
+ /**
+ * Builder to create a new {@link AnalyzerMeasure}.
+ */
+ <G extends Serializable> AnalyzerMeasureBuilder<G> measureBuilder();
+
+ /**
+ * Find a project measure.
+ */
+ @CheckForNull
+ AnalyzerMeasure getMeasure(String metricKey);
+
+ /**
+ * Find a project measure.
+ */
+ @CheckForNull
+ <G extends Serializable> AnalyzerMeasure<G> getMeasure(Metric<G> metric);
+
+ /**
+ * Find a file measure.
+ */
+ @CheckForNull
+ AnalyzerMeasure getMeasure(InputFile file, String metricKey);
+
+ /**
+ * Find a file measure.
+ */
+ @CheckForNull
+ <G extends Serializable> AnalyzerMeasure<G> getMeasure(InputFile file, Metric<G> metric);
+
+ /**
+ * Add a measure. Use {@link #measureBuilder()} to create the new measure.
+ */
+ void addMeasure(AnalyzerMeasure<?> measure);
+
+ // ----------- ISSUES --------------
+
+ /**
+ * Add an issue.
+ */
+ void addIssue(AnalyzerIssue issue);
+
+ /**
+ * Add a list of issues.
+ */
+ void addIssues(Collection<AnalyzerIssue> issues);
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.analyzer;
+
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measures.Metric;
+
+/**
+ * @since 4.4
+ */
+public interface AnalyzerDescriptor {
+
+ AnalyzerDescriptor name(String name);
+
+ AnalyzerDescriptor dependsOn(Metric<?>... metrics);
+
+ AnalyzerDescriptor provides(Metric<?>... metrics);
+
+ AnalyzerDescriptor runOnLanguages(String... languageKeys);
+
+ AnalyzerDescriptor runOnTypes(InputFile.Type... types);
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.analyzer.internal;
+
+import org.sonar.api.batch.analyzer.AnalyzerDescriptor;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measures.Metric;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+public class DefaultAnalyzerDescriptor implements AnalyzerDescriptor {
+
+ private String name;
+ private Metric<?>[] dependsOn;
+ private Metric<?>[] provides;
+ private String[] languages;
+ private InputFile.Type[] types;
+
+ public String name() {
+ return name;
+ }
+
+ public Metric<?>[] dependsOn() {
+ return dependsOn;
+ }
+
+ public Metric<?>[] provides() {
+ return provides;
+ }
+
+ public Collection<String> languages() {
+ return Arrays.asList(languages);
+ }
+
+ public InputFile.Type[] types() {
+ return types;
+ }
+
+ @Override
+ public DefaultAnalyzerDescriptor name(String name) {
+ this.name = name;
+ return this;
+ }
+
+ @Override
+ public DefaultAnalyzerDescriptor dependsOn(Metric<?>... metrics) {
+ this.dependsOn = metrics;
+ return this;
+ }
+
+ @Override
+ public DefaultAnalyzerDescriptor provides(Metric<?>... metrics) {
+ this.provides = metrics;
+ return this;
+ }
+
+ @Override
+ public DefaultAnalyzerDescriptor runOnLanguages(String... languageKeys) {
+ this.languages = languageKeys;
+ return this;
+ }
+
+ @Override
+ public DefaultAnalyzerDescriptor runOnTypes(InputFile.Type... types) {
+ this.types = types;
+ return this;
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.analyzer.internal;
\ No newline at end of file
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.analyzer.issue;
+
+import com.google.common.base.Preconditions;
+import org.sonar.api.batch.analyzer.Analyzer;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.rule.RuleKey;
+
+import javax.annotation.Nullable;
+
+/**
+ * Issue reported by an {@link Analyzer}
+ *
+ * @since 4.4
+ */
+public class AnalyzerIssue {
+
+ private final InputFile inputFile;
+ private final RuleKey ruleKey;
+ private final String message;
+ private final Integer line;
+ private final Double effortToFix;
+
+ private AnalyzerIssue(Builder builder) {
+ this.inputFile = builder.file;
+ this.ruleKey = builder.ruleKey;
+ this.message = builder.message;
+ this.line = builder.line;
+ this.effortToFix = builder.effortToFix;
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ @Nullable
+ public InputFile inputFile() {
+ return inputFile;
+ }
+
+ public RuleKey ruleKey() {
+ return ruleKey;
+ }
+
+ public String message() {
+ return message;
+ }
+
+ public Integer line() {
+ return line;
+ }
+
+ @Nullable
+ public Double effortToFix() {
+ return effortToFix;
+ }
+
+ public static class Builder {
+
+ private Boolean onProject = null;
+ private InputFile file;
+ private RuleKey ruleKey;
+ private String message;
+ private Integer line;
+ private Double effortToFix;
+
+ public AnalyzerIssue build() {
+ return new AnalyzerIssue(this);
+ }
+
+ public Builder ruleKey(RuleKey ruleKey) {
+ this.ruleKey = ruleKey;
+ return this;
+ }
+
+ public Builder onFile(InputFile file) {
+ Preconditions.checkState(onProject == null, "onFile or onProject can be called only once");
+ Preconditions.checkNotNull(file, "InputFile should be non null");
+ this.file = file;
+ this.onProject = false;
+ return this;
+ }
+
+ public Builder onProject() {
+ Preconditions.checkState(onProject == null, "onFile or onProject can be called only once");
+ this.file = null;
+ this.onProject = true;
+ return this;
+ }
+
+ public Builder atLine(int line) {
+ this.line = line;
+ return this;
+ }
+
+ public Builder effortToFix(@Nullable Double effortToFix) {
+ this.effortToFix = effortToFix;
+ return this;
+ }
+
+ public Builder message(String message) {
+ this.message = message;
+ return this;
+ }
+
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+/**
+ *
+ */
+/**
+ * @author julien
+ *
+ */
+package org.sonar.api.batch.analyzer.issue.internal;
\ No newline at end of file
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.analyzer.issue;
\ No newline at end of file
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.analyzer.measure;
+
+import org.sonar.api.batch.analyzer.Analyzer;
+import org.sonar.api.batch.fs.InputFile;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+
+/**
+ * A measure produced by {@link Analyzer}.
+ * @since 4.4
+ */
+public interface AnalyzerMeasure<G extends Serializable> {
+
+ /**
+ * The {@link InputFile} this measure belongs to. Returns null if measure is global to the project.
+ */
+ @Nullable
+ InputFile inputFile();
+
+ String metricKey();
+
+ G value();
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.analyzer.measure;
+
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measures.Metric;
+
+import java.io.Serializable;
+
+/**
+ * Builder to create new {@link AnalyzerMeasure}
+ * @since 4.4
+ */
+public interface AnalyzerMeasureBuilder<G extends Serializable> {
+
+ /**
+ * The file the measure belongs to.
+ */
+ AnalyzerMeasureBuilder<G> onFile(InputFile file);
+
+ /**
+ * Tell that the measure is global to the project.
+ */
+ AnalyzerMeasureBuilder<G> onProject();
+
+ /**
+ * The metric this measure belong to.
+ */
+ AnalyzerMeasureBuilder<G> forMetric(Metric<G> metric);
+
+ /**
+ * Value of the measure.
+ */
+ AnalyzerMeasureBuilder<G> withValue(G value);
+
+ /**
+ * Build the measure.
+ */
+ AnalyzerMeasure<G> build();
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.analyzer.measure.internal;
+
+import com.google.common.base.Preconditions;
+import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
+import org.sonar.api.batch.fs.InputFile;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+
+public class DefaultAnalyzerMeasure<G extends Serializable> implements AnalyzerMeasure<G>, Serializable {
+
+ private final InputFile inputFile;
+ private final String metricKey;
+ private final G value;
+
+ DefaultAnalyzerMeasure(DefaultAnalyzerMeasureBuilder<G> builder) {
+ Preconditions.checkNotNull(builder.value, "Measure value can't be null");
+ Preconditions.checkNotNull(builder.metricKey, "Measure metricKey can't be null");
+ this.inputFile = builder.file;
+ this.metricKey = builder.metricKey;
+ this.value = builder.value;
+ }
+
+ @Nullable
+ @Override
+ public InputFile inputFile() {
+ return inputFile;
+ }
+
+ @Override
+ public String metricKey() {
+ return metricKey;
+ }
+
+ @Override
+ public G value() {
+ return value;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (!(obj instanceof DefaultAnalyzerMeasure)) {
+ return false;
+ }
+ DefaultAnalyzerMeasure<?> other = (DefaultAnalyzerMeasure<?>) obj;
+ return metricKey.equals(other.metricKey)
+ && value.equals(other.value)
+ && (inputFile == null ? other.inputFile == null : inputFile.equals(other.inputFile));
+ }
+
+ @Override
+ public int hashCode() {
+ return metricKey.hashCode()
+ + value.hashCode()
+ + (inputFile != null ? inputFile.hashCode() : 0);
+ }
+
+ @Override
+ public String toString() {
+ return "AnalyzerMeasure[" + (inputFile != null ? "inputFile=" + inputFile.toString() : "onProject")
+ + ",metricKey=" + metricKey + ",value=" + value + "]";
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.analyzer.measure.internal;
+
+import com.google.common.base.Preconditions;
+import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
+import org.sonar.api.batch.analyzer.measure.AnalyzerMeasureBuilder;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measures.Metric;
+
+import java.io.Serializable;
+
+public class DefaultAnalyzerMeasureBuilder<G extends Serializable> implements AnalyzerMeasureBuilder<G> {
+
+ Boolean onProject = null;
+ InputFile file;
+ String metricKey;
+ G value;
+
+ @Override
+ public AnalyzerMeasureBuilder<G> onFile(InputFile file) {
+ Preconditions.checkState(onProject == null, "onFile or onProject can be called only once");
+ Preconditions.checkNotNull(file, "InputFile should be non null");
+ this.file = file;
+ this.onProject = false;
+ return this;
+ }
+
+ @Override
+ public AnalyzerMeasureBuilder<G> onProject() {
+ Preconditions.checkState(onProject == null, "onFile or onProject can be called only once");
+ this.file = null;
+ this.onProject = true;
+ return this;
+ }
+
+ private AnalyzerMeasureBuilder<G> metricKey(String metricKey) {
+ Preconditions.checkState(metricKey != null, "Metric already defined");
+ this.metricKey = metricKey;
+ return this;
+ }
+
+ @Override
+ public AnalyzerMeasureBuilder<G> forMetric(Metric<G> metric) {
+ return metricKey(metric.key());
+ }
+
+ @Override
+ public AnalyzerMeasureBuilder<G> withValue(G value) {
+ Preconditions.checkState(value != null, "Measure value already defined");
+ Preconditions.checkNotNull(value, "Measure value can't be null");
+ this.value = value;
+ return this;
+ }
+
+ @Override
+ public AnalyzerMeasure<G> build() {
+ return new DefaultAnalyzerMeasure<G>(this);
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.analyzer.measure.internal;
\ No newline at end of file
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.analyzer.measure;
\ No newline at end of file
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.analyzer;
\ No newline at end of file
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs;
+
+/**
+ * Determines if a file must be kept in search results. See {@link org.sonar.api.batch.fs.FileSystem}
+ * and {@link org.sonar.api.batch.fs.FilePredicates}.
+ * @since 4.2
+ */
+public interface FilePredicate {
+ boolean apply(InputFile inputFile);
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs;
+
+import java.io.File;
+import java.util.Collection;
+
+/**
+ * Factory of {@link org.sonar.api.batch.fs.FilePredicate}
+ *
+ * @since 4.2
+ */
+public interface FilePredicates {
+ /**
+ * Returns a predicate that always evaluates to true
+ */
+ FilePredicate all();
+
+ /**
+ * Returns a predicate that always evaluates to false
+ */
+ FilePredicate none();
+
+ /**
+ * Warning - not efficient because absolute path is not indexed yet.
+ */
+ FilePredicate hasAbsolutePath(String s);
+
+ /**
+ * TODO document that non-normalized path and Windows-style path are supported
+ */
+ FilePredicate hasRelativePath(String s);
+
+ FilePredicate matchesPathPattern(String inclusionPattern);
+
+ FilePredicate matchesPathPatterns(String[] inclusionPatterns);
+
+ FilePredicate doesNotMatchPathPattern(String exclusionPattern);
+
+ FilePredicate doesNotMatchPathPatterns(String[] exclusionPatterns);
+
+ FilePredicate hasPath(String s);
+
+ FilePredicate is(File ioFile);
+
+ FilePredicate hasLanguage(String language);
+
+ FilePredicate hasLanguages(Collection<String> languages);
+
+ FilePredicate hasLanguages(String... languages);
+
+ FilePredicate hasStatus(InputFile.Status status);
+
+ FilePredicate hasType(InputFile.Type type);
+
+ FilePredicate not(FilePredicate p);
+
+ FilePredicate or(Collection<FilePredicate> or);
+
+ FilePredicate or(FilePredicate... or);
+
+ FilePredicate or(FilePredicate first, FilePredicate second);
+
+ FilePredicate and(Collection<FilePredicate> and);
+
+ FilePredicate and(FilePredicate... and);
+
+ FilePredicate and(FilePredicate first, FilePredicate second);
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs;
+
+import org.sonar.api.BatchComponent;
+
+import javax.annotation.CheckForNull;
+
+import java.io.File;
+import java.nio.charset.Charset;
+import java.util.SortedSet;
+
+/**
+ * The {@link FileSystem} manages all the source files to be analyzed.
+ * <p/>
+ * This is not an extension point so it must not be implemented by plugins. It must be injected as a
+ * constructor parameter :
+ * <pre>
+ * public class MySensor implements Sensor {
+ * private final FileSystem fs;
+ *
+ * public MySensor(FileSystem fs) {
+ * this.fs = fs;
+ * }
+ * }
+ * </pre>
+ *
+ * <h2>How to use in unit tests</h2>
+ * The unit tests needing an instance of FileSystem can use the implementation
+ * {@link org.sonar.api.batch.fs.internal.DefaultFileSystem} and the related {@link org.sonar.api.batch.fs.internal.DefaultInputFile},
+ * for example :
+ * <pre>
+ * DefaultFileSystem fs = new DefaultFileSystem();
+ * fs.add(new DefaultInputFile("src/foo/bar.php"));
+ * </pre>
+ *
+ * @since 4.2
+ */
+public interface FileSystem extends BatchComponent {
+
+ /**
+ * Absolute base directory of module
+ */
+ File baseDir();
+
+ /**
+ * Default encoding of input files. If it's not defined, then
+ * the platform default encoding is returned
+ */
+ Charset encoding();
+
+ /**
+ * Absolute work directory. It can be used to
+ * store third-party analysis reports.
+ * <p/>
+ * The work directory can be located outside {@link #baseDir()}.
+ */
+ File workDir();
+
+ /**
+ * Factory of {@link FilePredicate}
+ */
+ FilePredicates predicates();
+
+ /**
+ * Returns the single element matching the predicate. If more than one elements match
+ * the predicate, then {@link IllegalArgumentException} is thrown. Returns {@code null}
+ * if no files match.
+ *
+ * <p/>
+ * How to use :
+ * <pre>
+ * InputFile file = fs.inputFile(fs.predicates().hasRelativePath("src/Foo.php"));
+ * </pre>
+ *
+ * @see #predicates()
+ */
+ @CheckForNull
+ InputFile inputFile(FilePredicate predicate);
+
+ /**
+ * Input files matching the given attributes. Return all the files if the parameter
+ * <code>attributes</code> is empty.
+ * <p/>
+ * <b>Important</b> - result is an {@link java.lang.Iterable} to benefit from streaming and decreasing
+ * memory consumption. It should be iterated only once, else copy it into a list :
+ * {@code com.google.common.collect.Lists.newArrayList(inputFiles(predicate))}
+ * <p/>
+ * How to use :
+ * <pre>
+ * FilePredicates p = fs.predicates();
+ * Iterable<InputFile> files = fs.inputFiles(p.and(p.hasLanguage("java"), p.hasType(InputFile.Type.MAIN)));
+ * </pre>
+ *
+ * @see #predicates()
+ */
+ Iterable<InputFile> inputFiles(FilePredicate predicate);
+
+ /**
+ * Returns true if at least one {@link org.sonar.api.batch.fs.InputFile} matches
+ * the given predicate. This method can be faster than checking if {@link #inputFiles(org.sonar.api.batch.fs.FilePredicate)}
+ * has elements.
+ * @see #predicates()
+ */
+ boolean hasFiles(FilePredicate predicate);
+
+ /**
+ * Files matching the given predicate.
+ * @see #predicates()
+ */
+ Iterable<File> files(FilePredicate predicate);
+
+ /**
+ * Languages detected in all files, whatever their type (main or test)
+ */
+ SortedSet<String> languages();
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs;
+
+import java.io.File;
+import java.io.Serializable;
+
+/**
+ * This layer over {@link java.io.File} adds information for code analyzers.
+ *
+ * @since 4.2
+ */
+public interface InputFile extends Serializable {
+
+ enum Type {
+ MAIN, TEST
+ }
+
+ /**
+ * Status regarding previous analysis
+ */
+ enum Status {
+ SAME, CHANGED, ADDED
+ }
+
+ /**
+ * Path relative to module base directory. Path is unique and identifies file
+ * within given <code>{@link FileSystem}</code>. File separator is the forward
+ * slash ('/'), even on Microsoft Windows.
+ * <p/>
+ * Returns <code>src/main/java/com/Foo.java</code> if module base dir is
+ * <code>/path/to/module</code> and if file is
+ * <code>/path/to/module/src/main/java/com/Foo.java</code>.
+ * <p/>
+ * Relative path is not null and is normalized ('foo/../foo' is replaced by 'foo').
+ */
+ String relativePath();
+
+ /**
+ * Normalized absolute path. File separator is forward slash ('/'), even on Microsoft Windows.
+ * <p/>
+ * This is not canonical path. Symbolic links are not resolved. For example if /project/src links
+ * to /tmp/src and basedir is /project, then this method returns /project/src/index.php. Use
+ * {@code file().getCanonicalPath()} to resolve symbolic link.
+ */
+ String absolutePath();
+
+ /**
+ * The underlying absolute {@link java.io.File}
+ */
+ File file();
+
+ /**
+ * Language, for example "java" or "php". It's automatically guessed if it is not
+ * set in project settings.
+ */
+ String language();
+
+ /**
+ * Does it contain main or test code ?
+ */
+ Type type();
+
+ /**
+ * Status regarding previous analysis
+ */
+ Status status();
+
+ /**
+ * Number of physical lines. This method supports all end-of-line characters. Returns
+ * zero if the file is empty.
+ */
+ int lines();
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs;
+
+import org.sonar.api.BatchExtension;
+
+/**
+ * Extension point to exclude some files from inspection
+ * @since 4.2
+ */
+public interface InputFileFilter extends BatchExtension {
+
+ // TODO requires a context (FileSystem) ?
+ boolean accept(InputFile f);
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.utils.PathUtils;
+
+/**
+ * @since 4.2
+ */
+class AbsolutePathPredicate implements FilePredicate {
+
+ private final String path;
+
+ AbsolutePathPredicate(String path) {
+ this.path = PathUtils.sanitize(path);
+ }
+
+ @Override
+ public boolean apply(InputFile f) {
+ return path.equals(f.absolutePath());
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.InputFile;
+
+import java.util.Collection;
+
+/**
+ * @since 4.2
+ */
+class AndPredicate implements FilePredicate {
+
+ private final Collection<FilePredicate> predicates;
+
+ AndPredicate(Collection<FilePredicate> predicates) {
+ this.predicates = predicates;
+ }
+
+ @Override
+ public boolean apply(InputFile f) {
+ for (FilePredicate predicate : predicates) {
+ if (!predicate.apply(f)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.FilePredicates;
+import org.sonar.api.batch.fs.InputFile;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Factory of {@link org.sonar.api.batch.fs.FilePredicate}
+ *
+ * @since 4.2
+ */
+public class DefaultFilePredicates implements FilePredicates {
+ /**
+ * Client code should use {@link org.sonar.api.batch.fs.FileSystem#predicates()} to get an instance
+ */
+ DefaultFilePredicates() {
+ }
+
+ /**
+ * Returns a predicate that always evaluates to true
+ */
+ public FilePredicate all() {
+ return TruePredicate.TRUE;
+ }
+
+ /**
+ * Returns a predicate that always evaluates to false
+ */
+ public FilePredicate none() {
+ return FalsePredicate.FALSE;
+ }
+
+ /**
+ * Warning - not efficient because absolute path is not indexed yet.
+ */
+ public FilePredicate hasAbsolutePath(String s) {
+ return new AbsolutePathPredicate(s);
+ }
+
+ /**
+ * TODO document that non-normalized path and Windows-style path are supported
+ */
+ public FilePredicate hasRelativePath(String s) {
+ return new RelativePathPredicate(s);
+ }
+
+ public FilePredicate matchesPathPattern(String inclusionPattern) {
+ return new PathPatternPredicate(PathPattern.create(inclusionPattern));
+ }
+
+ public FilePredicate matchesPathPatterns(String[] inclusionPatterns) {
+ if (inclusionPatterns.length == 0) {
+ return TruePredicate.TRUE;
+ }
+ FilePredicate[] predicates = new FilePredicate[inclusionPatterns.length];
+ for (int i = 0; i < inclusionPatterns.length; i++) {
+ predicates[i] = new PathPatternPredicate(PathPattern.create(inclusionPatterns[i]));
+ }
+ return or(predicates);
+ }
+
+ public FilePredicate doesNotMatchPathPattern(String exclusionPattern) {
+ return not(matchesPathPattern(exclusionPattern));
+ }
+
+ public FilePredicate doesNotMatchPathPatterns(String[] exclusionPatterns) {
+ if (exclusionPatterns.length == 0) {
+ return TruePredicate.TRUE;
+ }
+ return not(matchesPathPatterns(exclusionPatterns));
+ }
+
+ public FilePredicate hasPath(String s) {
+ File file = new File(s);
+ if (file.isAbsolute()) {
+ return hasAbsolutePath(s);
+ }
+ return hasRelativePath(s);
+ }
+
+ public FilePredicate is(File ioFile) {
+ if (ioFile.isAbsolute()) {
+ return hasAbsolutePath(ioFile.getAbsolutePath());
+ }
+ return hasRelativePath(ioFile.getPath());
+ }
+
+ public FilePredicate hasLanguage(String language) {
+ return new LanguagePredicate(language);
+ }
+
+ public FilePredicate hasLanguages(Collection<String> languages) {
+ List<FilePredicate> list = new ArrayList<FilePredicate>();
+ for (String language : languages) {
+ list.add(hasLanguage(language));
+ }
+ return or(list);
+ }
+
+ public FilePredicate hasLanguages(String... languages) {
+ List<FilePredicate> list = new ArrayList<FilePredicate>();
+ for (String language : languages) {
+ list.add(hasLanguage(language));
+ }
+ return or(list);
+ }
+
+ public FilePredicate hasStatus(InputFile.Status status) {
+ return new StatusPredicate(status);
+ }
+
+ public FilePredicate hasType(InputFile.Type type) {
+ return new TypePredicate(type);
+ }
+
+ public FilePredicate not(FilePredicate p) {
+ return new NotPredicate(p);
+ }
+
+ public FilePredicate or(Collection<FilePredicate> or) {
+ return new OrPredicate(or);
+ }
+
+ public FilePredicate or(FilePredicate... or) {
+ return new OrPredicate(Arrays.asList(or));
+ }
+
+ public FilePredicate or(FilePredicate first, FilePredicate second) {
+ return new OrPredicate(Arrays.asList(first, second));
+ }
+
+ public FilePredicate and(Collection<FilePredicate> and) {
+ return new AndPredicate(and);
+ }
+
+ public FilePredicate and(FilePredicate... and) {
+ return new AndPredicate(Arrays.asList(and));
+ }
+
+ public FilePredicate and(FilePredicate first, FilePredicate second) {
+ return new AndPredicate(Arrays.asList(first, second));
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import com.google.common.base.Preconditions;
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.FilePredicates;
+import org.sonar.api.batch.fs.FileSystem;
+import org.sonar.api.batch.fs.InputFile;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+import java.io.File;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+/**
+ * @since 4.2
+ */
+public class DefaultFileSystem implements FileSystem {
+
+ private final Cache cache;
+ private final SortedSet<String> languages = new TreeSet<String>();
+ private File baseDir, workDir;
+ private Charset encoding;
+ private final FilePredicates predicates = new DefaultFilePredicates();
+
+ /**
+ * Only for testing
+ */
+ public DefaultFileSystem() {
+ this.cache = new MapCache();
+ }
+
+ protected DefaultFileSystem(Cache cache) {
+ this.cache = cache;
+ }
+
+ public DefaultFileSystem setBaseDir(File d) {
+ Preconditions.checkNotNull(d, "Base directory can't be null");
+ this.baseDir = d.getAbsoluteFile();
+ return this;
+ }
+
+ @Override
+ public File baseDir() {
+ return baseDir;
+ }
+
+ public DefaultFileSystem setEncoding(@Nullable Charset e) {
+ this.encoding = e;
+ return this;
+ }
+
+ @Override
+ public Charset encoding() {
+ return encoding == null ? Charset.defaultCharset() : encoding;
+ }
+
+ public boolean isDefaultJvmEncoding() {
+ return encoding == null;
+ }
+
+ public DefaultFileSystem setWorkDir(File d) {
+ this.workDir = d.getAbsoluteFile();
+ return this;
+ }
+
+ @Override
+ public File workDir() {
+ return workDir;
+ }
+
+ @Override
+ public InputFile inputFile(FilePredicate predicate) {
+ doPreloadFiles();
+ if (predicate instanceof RelativePathPredicate) {
+ return cache.inputFile((RelativePathPredicate) predicate);
+ }
+ Iterable<InputFile> files = inputFiles(predicate);
+ Iterator<InputFile> iterator = files.iterator();
+ if (!iterator.hasNext()) {
+ return null;
+ }
+ InputFile first = iterator.next();
+ if (!iterator.hasNext()) {
+ return first;
+ }
+
+ StringBuilder sb = new StringBuilder();
+ sb.append("expected one element but was: <" + first);
+ for (int i = 0; i < 4 && iterator.hasNext(); i++) {
+ sb.append(", " + iterator.next());
+ }
+ if (iterator.hasNext()) {
+ sb.append(", ...");
+ }
+ sb.append('>');
+
+ throw new IllegalArgumentException(sb.toString());
+
+ }
+
+ @Override
+ public Iterable<InputFile> inputFiles(FilePredicate predicate) {
+ doPreloadFiles();
+ return filter(cache.inputFiles(), predicate);
+ }
+
+ @Override
+ public boolean hasFiles(FilePredicate predicate) {
+ doPreloadFiles();
+ for (InputFile element : cache.inputFiles()) {
+ if (predicate.apply(element)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public Iterable<File> files(FilePredicate predicate) {
+ doPreloadFiles();
+ Collection<File> result = new ArrayList<File>();
+ for (InputFile element : inputFiles(predicate)) {
+ if (predicate.apply(element)) {
+ result.add(element.file());
+ }
+ }
+ return result;
+ }
+
+ public static Collection<InputFile> filter(Iterable<InputFile> target, FilePredicate predicate) {
+ Collection<InputFile> result = new ArrayList<InputFile>();
+ for (InputFile element : target) {
+ if (predicate.apply(element)) {
+ result.add(element);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Adds InputFile to the list and registers its language, if present.
+ */
+ public DefaultFileSystem add(InputFile inputFile) {
+ cache.add(inputFile);
+ if (inputFile.language() != null) {
+ languages.add(inputFile.language());
+ }
+ return this;
+ }
+
+ /**
+ * Adds a language to the list. To be used only for unit tests that need to use {@link #languages()} without
+ * using {@link #add(org.sonar.api.batch.fs.InputFile)}.
+ */
+ public DefaultFileSystem addLanguages(String language, String... others) {
+ languages.add(language);
+ Collections.addAll(languages, others);
+ return this;
+ }
+
+ @Override
+ public SortedSet<String> languages() {
+ doPreloadFiles();
+ return languages;
+ }
+
+ @Override
+ public FilePredicates predicates() {
+ return predicates;
+ }
+
+ /**
+ * This method is called before each search of files.
+ */
+ protected void doPreloadFiles() {
+ // nothing to do by default
+ }
+
+ public static abstract class Cache {
+ protected abstract Iterable<InputFile> inputFiles();
+
+ @CheckForNull
+ protected abstract InputFile inputFile(RelativePathPredicate predicate);
+
+ protected abstract void doAdd(InputFile inputFile);
+
+ final void add(InputFile inputFile) {
+ doAdd(inputFile);
+ }
+ }
+
+ /**
+ * Used only for testing
+ */
+ private static class MapCache extends Cache {
+ private final Map<String, InputFile> fileMap = new HashMap<String, InputFile>();
+
+ @Override
+ public Iterable<InputFile> inputFiles() {
+ return new ArrayList<InputFile>(fileMap.values());
+ }
+
+ @Override
+ public InputFile inputFile(RelativePathPredicate predicate) {
+ return fileMap.get(predicate.path());
+ }
+
+ @Override
+ protected void doAdd(InputFile inputFile) {
+ fileMap.put(inputFile.relativePath(), inputFile);
+ }
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.utils.PathUtils;
+
+import javax.annotation.CheckForNull;
+
+import java.io.File;
+import java.io.Serializable;
+
+/**
+ * @since 4.2
+ */
+public class DefaultInputFile implements InputFile, Serializable {
+
+ private final String relativePath;
+ private String absolutePath;
+ private String language;
+ private Type type = Type.MAIN;
+ private Status status;
+ private String hash;
+ private int lines;
+ private String key;
+
+ public DefaultInputFile(String relativePath) {
+ this.relativePath = PathUtils.sanitize(relativePath);
+ }
+
+ @Override
+ public String relativePath() {
+ return relativePath;
+ }
+
+ /**
+ * Marked as nullable just for the unit tests that do not call {@link #setFile(java.io.File)}
+ * previously.
+ */
+ @Override
+ @CheckForNull
+ public String absolutePath() {
+ return absolutePath;
+ }
+
+ @Override
+ public File file() {
+ if (absolutePath == null) {
+ throw new IllegalStateException("Can not return the java.io.File because absolute path is not set (see method setFile(java.io.File))");
+ }
+ return new File(absolutePath);
+ }
+
+ /**
+ * Marked as nullable just for the unit tests that do not call {@link #setLanguage(String)}
+ * previously.
+ */
+ @CheckForNull
+ @Override
+ public String language() {
+ return language;
+ }
+
+ @Override
+ public Type type() {
+ return type;
+ }
+
+ /**
+ * Marked as nullable just for the unit tests that do not previously call
+ * {@link #setStatus(org.sonar.api.batch.fs.InputFile.Status)}
+ */
+ @CheckForNull
+ @Override
+ public Status status() {
+ return status;
+ }
+
+ /**
+ * Digest hash of the file. Marked as nullable just for the unit tests
+ * that do not previously call {@link #setHash(String)}
+ */
+ @CheckForNull
+ public String hash() {
+ return hash;
+ }
+
+ @Override
+ public int lines() {
+ return lines;
+ }
+
+ /**
+ * Component key. It's marked as nullable just for the unit tests that
+ * do not previously call {@link #setKey(String)}.
+ */
+ @CheckForNull
+ public String key() {
+ return key;
+ }
+
+ public DefaultInputFile setAbsolutePath(String s) {
+ this.absolutePath = PathUtils.sanitize(s);
+ return this;
+ }
+
+ public DefaultInputFile setLanguage(String language) {
+ this.language = language;
+ return this;
+ }
+
+ public DefaultInputFile setFile(File file) {
+ setAbsolutePath(file.getAbsolutePath());
+ return this;
+ }
+
+ public DefaultInputFile setType(Type type) {
+ this.type = type;
+ return this;
+ }
+
+ public DefaultInputFile setStatus(Status status) {
+ this.status = status;
+ return this;
+ }
+
+ public DefaultInputFile setHash(String hash) {
+ this.hash = hash;
+ return this;
+ }
+
+ public DefaultInputFile setLines(int lines) {
+ this.lines = lines;
+ return this;
+ }
+
+ public DefaultInputFile setKey(String s) {
+ this.key = s;
+ return this;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (!(o instanceof DefaultInputFile)) {
+ return false;
+ }
+
+ DefaultInputFile that = (DefaultInputFile) o;
+ return relativePath.equals(that.relativePath);
+ }
+
+ @Override
+ public int hashCode() {
+ return relativePath.hashCode();
+ }
+
+ @Override
+ public String toString() {
+ return "[relative=" + relativePath + ", abs=" + absolutePath + "]";
+ }
+}
*/
package org.sonar.api.batch.fs.internal;
-import org.sonar.batch.api.internal.FilenameUtils;
+import org.sonar.api.utils.PathUtils;
import java.io.BufferedInputStream;
import java.io.File;
}
public void setBasedir(File basedir) {
- this.basedir = FilenameUtils.normalize(basedir.getAbsolutePath());
+ this.basedir = PathUtils.sanitize(basedir.getAbsolutePath());
}
/**
}
public DeprecatedDefaultInputFile setSourceDirAbsolutePath(String s) {
- this.sourceDirAbsolutePath = FilenameUtils.normalize(s);
+ this.sourceDirAbsolutePath = PathUtils.sanitize(s);
return this;
}
}
public DeprecatedDefaultInputFile setPathRelativeToSourceDir(String s) {
- this.pathRelativeToSourceDir = FilenameUtils.normalize(s);
+ this.pathRelativeToSourceDir = PathUtils.sanitize(s);
return this;
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.InputFile;
+
+class FalsePredicate implements FilePredicate {
+
+ static final FilePredicate FALSE = new FalsePredicate();
+
+ @Override
+ public boolean apply(InputFile inputFile) {
+ return false;
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.InputFile;
+
+/**
+ * @since 4.2
+ */
+class LanguagePredicate implements FilePredicate {
+ private final String language;
+
+ LanguagePredicate(String language) {
+ this.language = language;
+ }
+
+ @Override
+ public boolean apply(InputFile f) {
+ return language.equals(f.language());
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.InputFile;
+
+/**
+ * @since 4.2
+ */
+class NotPredicate implements FilePredicate {
+
+ private final FilePredicate predicate;
+
+ NotPredicate(FilePredicate predicate) {
+ this.predicate = predicate;
+ }
+
+ @Override
+ public boolean apply(InputFile f) {
+ return !predicate.apply(f);
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.InputFile;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+/**
+ * @since 4.2
+ */
+class OrPredicate implements FilePredicate {
+
+ private final Collection<FilePredicate> predicates;
+
+ OrPredicate(Collection<FilePredicate> predicates) {
+ if (predicates.isEmpty()) {
+ this.predicates = Arrays.asList(TruePredicate.TRUE);
+ } else {
+ this.predicates = predicates;
+ }
+ }
+
+ @Override
+ public boolean apply(InputFile f) {
+ for (FilePredicate predicate : predicates) {
+ if (predicate.apply(f)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.utils.WildcardPattern;
+
+public abstract class PathPattern {
+
+ final WildcardPattern pattern;
+
+ PathPattern(String pattern) {
+ this.pattern = WildcardPattern.create(pattern);
+ }
+
+ public abstract boolean match(InputFile inputFile);
+
+ public abstract boolean match(InputFile inputFile, boolean caseSensitiveFileExtension);
+
+ public static PathPattern create(String s) {
+ String trimmed = StringUtils.trim(s);
+ if (StringUtils.startsWithIgnoreCase(trimmed, "file:")) {
+ return new AbsolutePathPattern(StringUtils.substring(trimmed, "file:".length()));
+ }
+ return new RelativePathPattern(trimmed);
+ }
+
+ public static PathPattern[] create(String[] s) {
+ PathPattern[] result = new PathPattern[s.length];
+ for (int i = 0; i < s.length; i++) {
+ result[i] = create(s[i]);
+ }
+ return result;
+ }
+
+ private static class AbsolutePathPattern extends PathPattern {
+ private AbsolutePathPattern(String pattern) {
+ super(pattern);
+ }
+
+ @Override
+ public boolean match(InputFile inputFile) {
+ return match(inputFile, true);
+ }
+
+ @Override
+ public boolean match(InputFile inputFile, boolean caseSensitiveFileExtension) {
+ String path = inputFile.absolutePath();
+ if (!caseSensitiveFileExtension) {
+ String extension = sanitizeExtension(FilenameUtils.getExtension(inputFile.file().getName()));
+ if (StringUtils.isNotBlank(extension)) {
+ path = StringUtils.removeEndIgnoreCase(path, extension);
+ path = path + extension;
+ }
+ }
+ return pattern.match(path);
+ }
+
+ @Override
+ public String toString() {
+ return "file:" + pattern.toString();
+ }
+ }
+
+ /**
+ * Path relative to module basedir
+ */
+ private static class RelativePathPattern extends PathPattern {
+ private RelativePathPattern(String pattern) {
+ super(pattern);
+ }
+
+ @Override
+ public boolean match(InputFile inputFile) {
+ return match(inputFile, true);
+ }
+
+ @Override
+ public boolean match(InputFile inputFile, boolean caseSensitiveFileExtension) {
+ String path = inputFile.relativePath();
+ if (!caseSensitiveFileExtension) {
+ String extension = sanitizeExtension(FilenameUtils.getExtension(inputFile.file().getName()));
+ if (StringUtils.isNotBlank(extension)) {
+ path = StringUtils.removeEndIgnoreCase(path, extension);
+ path = path + extension;
+ }
+ }
+ return path != null && pattern.match(path);
+ }
+
+ @Override
+ public String toString() {
+ return pattern.toString();
+ }
+ }
+
+ static String sanitizeExtension(String suffix) {
+ return StringUtils.lowerCase(StringUtils.removeStart(suffix, "."));
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.InputFile;
+
+/**
+ * @since 4.2
+ */
+class PathPatternPredicate implements FilePredicate {
+
+ private final PathPattern pattern;
+
+ PathPatternPredicate(PathPattern pattern) {
+ this.pattern = pattern;
+ }
+
+ @Override
+ public boolean apply(InputFile f) {
+ return pattern.match(f);
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.utils.PathUtils;
+
+/**
+ * @since 4.2
+ */
+public class RelativePathPredicate implements FilePredicate {
+
+ private final String path;
+
+ RelativePathPredicate(String path) {
+ this.path = PathUtils.sanitize(path);
+ }
+
+ public String path() {
+ return path;
+ }
+
+ @Override
+ public boolean apply(InputFile f) {
+ return path.equals(f.relativePath());
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.InputFile;
+
+/**
+ * @since 4.2
+ */
+class StatusPredicate implements FilePredicate {
+
+ private final InputFile.Status status;
+
+ StatusPredicate(InputFile.Status status) {
+ this.status = status;
+ }
+
+ @Override
+ public boolean apply(InputFile f) {
+ return status == f.status();
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.InputFile;
+
+class TruePredicate implements FilePredicate {
+
+ static final FilePredicate TRUE = new TruePredicate();
+
+ @Override
+ public boolean apply(InputFile inputFile) {
+ return true;
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.InputFile;
+
+/**
+ * @since 4.2
+ */
+class TypePredicate implements FilePredicate {
+
+ private final InputFile.Type type;
+
+ TypePredicate(InputFile.Type type) {
+ this.type = type;
+ }
+
+ @Override
+ public boolean apply(InputFile f) {
+ return type == f.type();
+ }
+
+}
+
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.api.batch.fs.internal;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.api.batch.fs;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.languages;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+public final class Language {
+
+ private final String key, name;
+ private final String[] fileSuffixes;
+
+ public Language(String key, String name, String... fileSuffixes) {
+ this.key = key;
+ this.name = name;
+ this.fileSuffixes = fileSuffixes;
+ }
+
+ /**
+ * For example "java".
+ */
+ public String key() {
+ return key;
+ }
+
+ /**
+ * For example "Java"
+ */
+ public String name() {
+ return name;
+ }
+
+ /**
+ * For example ["jav", "java"].
+ */
+ public Collection<String> fileSuffixes() {
+ return Arrays.asList(fileSuffixes);
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.languages;
\ No newline at end of file
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.measures;
+
+import java.io.Serializable;
+
+public interface Metric<G extends Serializable> {
+
+ Class<G> type();
+
+ String key();
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.measures;
\ No newline at end of file
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.rules;
+
+public class QProfile {
+ private final String name, language;
+ private final Integer version;
+
+ public QProfile(String name, String language, Integer version) {
+ this.name = name;
+ this.language = language;
+ this.version = version;
+ }
+
+ public String name() {
+ return name;
+ }
+
+ public String language() {
+ return language;
+ }
+
+ public Integer version() {
+ return version;
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.api.batch.rules;
\ No newline at end of file
};
}
- public static MeasuresFilter<Measure> metric(final org.sonar.batch.api.measures.Metric<?> metric) {
+ public static MeasuresFilter<Measure> metric(final org.sonar.api.batch.measures.Metric<?> metric) {
return metric(metric.key());
}
@Table(name = "metrics")
@Entity(name = "Metric")
@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
-public class Metric<G extends Serializable> implements ServerExtension, BatchExtension, Serializable, org.sonar.batch.api.measures.Metric<G> {
+public class Metric<G extends Serializable> implements ServerExtension, BatchExtension, Serializable, org.sonar.api.batch.measures.Metric<G> {
/**
* A metric bigger value means a degradation
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.fest.assertions.Assertions;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.FilePredicates;
+import org.sonar.api.batch.fs.InputFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DefaultFilePredicatesTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ DefaultInputFile javaFile;
+ FilePredicates predicates = new DefaultFilePredicates();
+
+ @Before
+ public void before() throws IOException {
+ javaFile = new DefaultInputFile("src/main/java/struts/Action.java")
+ .setFile(temp.newFile("Action.java"))
+ .setLanguage("java")
+ .setStatus(InputFile.Status.ADDED);
+ }
+
+ @Test
+ public void all() throws Exception {
+ Assertions.assertThat(predicates.all().apply(javaFile)).isTrue();
+ }
+
+ @Test
+ public void none() throws Exception {
+ assertThat(predicates.none().apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void matches_inclusion_pattern() throws Exception {
+ assertThat(predicates.matchesPathPattern("src/main/**/Action.java").apply(javaFile)).isTrue();
+ assertThat(predicates.matchesPathPattern("Action.java").apply(javaFile)).isFalse();
+ assertThat(predicates.matchesPathPattern("src/**/*.php").apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void matches_inclusion_patterns() throws Exception {
+ assertThat(predicates.matchesPathPatterns(new String[] {"src/other/**.java", "src/main/**/Action.java"}).apply(javaFile)).isTrue();
+ assertThat(predicates.matchesPathPatterns(new String[] {}).apply(javaFile)).isTrue();
+ assertThat(predicates.matchesPathPatterns(new String[] {"src/other/**.java", "src/**/*.php"}).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void does_not_match_exclusion_pattern() throws Exception {
+ assertThat(predicates.doesNotMatchPathPattern("src/main/**/Action.java").apply(javaFile)).isFalse();
+ assertThat(predicates.doesNotMatchPathPattern("Action.java").apply(javaFile)).isTrue();
+ assertThat(predicates.doesNotMatchPathPattern("src/**/*.php").apply(javaFile)).isTrue();
+ }
+
+ @Test
+ public void does_not_match_exclusion_patterns() throws Exception {
+ assertThat(predicates.doesNotMatchPathPatterns(new String[] {}).apply(javaFile)).isTrue();
+ assertThat(predicates.doesNotMatchPathPatterns(new String[] {"src/other/**.java", "src/**/*.php"}).apply(javaFile)).isTrue();
+ assertThat(predicates.doesNotMatchPathPatterns(new String[] {"src/other/**.java", "src/main/**/Action.java"}).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void has_relative_path() throws Exception {
+ assertThat(predicates.hasRelativePath("src/main/java/struts/Action.java").apply(javaFile)).isTrue();
+ assertThat(predicates.hasRelativePath("src/main/java/struts/Other.java").apply(javaFile)).isFalse();
+
+ // path is normalized
+ assertThat(predicates.hasRelativePath("src/main/java/../java/struts/Action.java").apply(javaFile)).isTrue();
+
+ assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\Action.java").apply(javaFile)).isTrue();
+ assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\Other.java").apply(javaFile)).isFalse();
+ assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\..\\struts\\Action.java").apply(javaFile)).isTrue();
+ }
+
+ @Test
+ public void has_absolute_path() throws Exception {
+ String path = javaFile.file().getAbsolutePath();
+ assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue();
+ assertThat(predicates.hasAbsolutePath(path.replaceAll("/", "\\\\")).apply(javaFile)).isTrue();
+
+ assertThat(predicates.hasAbsolutePath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse();
+ assertThat(predicates.hasAbsolutePath("src/main/java/struts/Action.java").apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void has_path() throws Exception {
+ // is relative path
+ assertThat(predicates.hasPath("src/main/java/struts/Action.java").apply(javaFile)).isTrue();
+ assertThat(predicates.hasPath("src/main/java/struts/Other.java").apply(javaFile)).isFalse();
+
+ // is absolute path
+ String path = javaFile.file().getAbsolutePath();
+ assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue();
+ assertThat(predicates.hasPath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void is_file() throws Exception {
+ // relative file
+ assertThat(predicates.is(new File(javaFile.relativePath())).apply(javaFile)).isTrue();
+
+ // absolute file
+ assertThat(predicates.is(javaFile.file()).apply(javaFile)).isTrue();
+ assertThat(predicates.is(javaFile.file().getAbsoluteFile()).apply(javaFile)).isTrue();
+ assertThat(predicates.is(javaFile.file().getCanonicalFile()).apply(javaFile)).isTrue();
+ assertThat(predicates.is(new File(javaFile.file().toURI())).apply(javaFile)).isTrue();
+ assertThat(predicates.is(temp.newFile()).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void has_language() throws Exception {
+ assertThat(predicates.hasLanguage("java").apply(javaFile)).isTrue();
+ assertThat(predicates.hasLanguage("php").apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void has_languages() throws Exception {
+ assertThat(predicates.hasLanguages(Arrays.asList("java", "php")).apply(javaFile)).isTrue();
+ assertThat(predicates.hasLanguages(Arrays.asList("cobol", "php")).apply(javaFile)).isFalse();
+ assertThat(predicates.hasLanguages(Collections.<String>emptyList()).apply(javaFile)).isTrue();
+ }
+
+ @Test
+ public void has_status() throws Exception {
+ assertThat(predicates.hasStatus(InputFile.Status.ADDED).apply(javaFile)).isTrue();
+ assertThat(predicates.hasStatus(InputFile.Status.CHANGED).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void has_type() throws Exception {
+ assertThat(predicates.hasType(InputFile.Type.MAIN).apply(javaFile)).isTrue();
+ assertThat(predicates.hasType(InputFile.Type.TEST).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void not() throws Exception {
+ assertThat(predicates.not(predicates.hasType(InputFile.Type.MAIN)).apply(javaFile)).isFalse();
+ assertThat(predicates.not(predicates.hasType(InputFile.Type.TEST)).apply(javaFile)).isTrue();
+ }
+
+ @Test
+ public void and() throws Exception {
+ // empty
+ assertThat(predicates.and().apply(javaFile)).isTrue();
+ assertThat(predicates.and(new FilePredicate[0]).apply(javaFile)).isTrue();
+ assertThat(predicates.and(Collections.<FilePredicate>emptyList()).apply(javaFile)).isTrue();
+
+ // two arguments
+ assertThat(predicates.and(predicates.all(), predicates.all()).apply(javaFile)).isTrue();
+ assertThat(predicates.and(predicates.all(), predicates.none()).apply(javaFile)).isFalse();
+ assertThat(predicates.and(predicates.none(), predicates.all()).apply(javaFile)).isFalse();
+
+ // collection
+ assertThat(predicates.and(Arrays.asList(predicates.all(), predicates.all())).apply(javaFile)).isTrue();
+ assertThat(predicates.and(Arrays.asList(predicates.all(), predicates.none())).apply(javaFile)).isFalse();
+
+ // array
+ assertThat(predicates.and(new FilePredicate[] {predicates.all(), predicates.all()}).apply(javaFile)).isTrue();
+ assertThat(predicates.and(new FilePredicate[] {predicates.all(), predicates.none()}).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void or() throws Exception {
+ // empty
+ assertThat(predicates.or().apply(javaFile)).isTrue();
+ assertThat(predicates.or(new FilePredicate[0]).apply(javaFile)).isTrue();
+ assertThat(predicates.or(Collections.<FilePredicate>emptyList()).apply(javaFile)).isTrue();
+
+ // two arguments
+ assertThat(predicates.or(predicates.all(), predicates.all()).apply(javaFile)).isTrue();
+ assertThat(predicates.or(predicates.all(), predicates.none()).apply(javaFile)).isTrue();
+ assertThat(predicates.or(predicates.none(), predicates.all()).apply(javaFile)).isTrue();
+ assertThat(predicates.or(predicates.none(), predicates.none()).apply(javaFile)).isFalse();
+
+ // collection
+ assertThat(predicates.or(Arrays.asList(predicates.all(), predicates.all())).apply(javaFile)).isTrue();
+ assertThat(predicates.or(Arrays.asList(predicates.all(), predicates.none())).apply(javaFile)).isTrue();
+ assertThat(predicates.or(Arrays.asList(predicates.none(), predicates.none())).apply(javaFile)).isFalse();
+
+ // array
+ assertThat(predicates.or(new FilePredicate[] {predicates.all(), predicates.all()}).apply(javaFile)).isTrue();
+ assertThat(predicates.or(new FilePredicate[] {predicates.all(), predicates.none()}).apply(javaFile)).isTrue();
+ assertThat(predicates.or(new FilePredicate[] {predicates.none(), predicates.none()}).apply(javaFile)).isFalse();
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.nio.charset.Charset;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DefaultFileSystemTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void test_directories() throws Exception {
+ DefaultFileSystem fs = new DefaultFileSystem();
+
+ File basedir = temp.newFolder();
+ fs.setBaseDir(basedir);
+ assertThat(fs.baseDir()).isAbsolute().isDirectory().exists();
+ assertThat(fs.baseDir().getCanonicalPath()).isEqualTo(basedir.getCanonicalPath());
+
+ File workdir = temp.newFolder();
+ fs.setWorkDir(workdir);
+ assertThat(fs.workDir()).isAbsolute().isDirectory().exists();
+ assertThat(fs.workDir().getCanonicalPath()).isEqualTo(workdir.getCanonicalPath());
+ }
+
+ @Test
+ public void test_encoding() throws Exception {
+ DefaultFileSystem fs = new DefaultFileSystem();
+
+ assertThat(fs.isDefaultJvmEncoding()).isTrue();
+ assertThat(fs.encoding()).isEqualTo(Charset.defaultCharset());
+
+ fs.setEncoding(Charset.forName("ISO-8859-1"));
+ assertThat(fs.encoding()).isEqualTo(Charset.forName("ISO-8859-1"));
+ assertThat(fs.isDefaultJvmEncoding()).isFalse();
+ }
+
+ @Test
+ public void add_languages() throws Exception {
+ DefaultFileSystem fs = new DefaultFileSystem();
+
+ assertThat(fs.languages()).isEmpty();
+
+ fs.addLanguages("java", "php", "cobol");
+ assertThat(fs.languages()).containsOnly("cobol", "java", "php");
+ }
+
+ @Test
+ public void files() throws Exception {
+ DefaultFileSystem fs = new DefaultFileSystem();
+
+ assertThat(fs.inputFiles(fs.predicates().all())).isEmpty();
+
+ fs.add(new DefaultInputFile("src/Foo.php").setLanguage("php").setFile(temp.newFile()));
+ fs.add(new DefaultInputFile("src/Bar.java").setLanguage("java").setFile(temp.newFile()));
+ fs.add(new DefaultInputFile("src/Baz.java").setLanguage("java").setFile(temp.newFile()));
+
+ // no language
+ fs.add(new DefaultInputFile("src/readme.txt").setFile(temp.newFile()));
+
+ assertThat(fs.inputFile(fs.predicates().hasRelativePath("src/Bar.java"))).isNotNull();
+ assertThat(fs.inputFile(fs.predicates().hasRelativePath("does/not/exist"))).isNull();
+
+ assertThat(fs.files(fs.predicates().all())).hasSize(4);
+ assertThat(fs.files(fs.predicates().hasLanguage("java"))).hasSize(2);
+ assertThat(fs.files(fs.predicates().hasLanguage("cobol"))).isEmpty();
+
+ assertThat(fs.hasFiles(fs.predicates().all())).isTrue();
+ assertThat(fs.hasFiles(fs.predicates().hasLanguage("java"))).isTrue();
+ assertThat(fs.hasFiles(fs.predicates().hasLanguage("cobol"))).isFalse();
+
+ assertThat(fs.inputFiles(fs.predicates().all())).hasSize(4);
+ assertThat(fs.inputFiles(fs.predicates().hasLanguage("php"))).hasSize(1);
+ assertThat(fs.inputFiles(fs.predicates().hasLanguage("java"))).hasSize(2);
+ assertThat(fs.inputFiles(fs.predicates().hasLanguage("cobol"))).isEmpty();
+
+ assertThat(fs.languages()).containsOnly("java", "php");
+ }
+
+ @Test
+ public void input_file_returns_null_if_file_not_found() throws Exception {
+ DefaultFileSystem fs = new DefaultFileSystem();
+ assertThat(fs.inputFile(fs.predicates().hasRelativePath("src/Bar.java"))).isNull();
+ assertThat(fs.inputFile(fs.predicates().hasLanguage("cobol"))).isNull();
+ }
+
+ @Test
+ public void input_file_fails_if_too_many_results() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("expected one element");
+
+ DefaultFileSystem fs = new DefaultFileSystem();
+ fs.add(new DefaultInputFile("src/Bar.java").setLanguage("java").setFile(temp.newFile()));
+ fs.add(new DefaultInputFile("src/Baz.java").setLanguage("java").setFile(temp.newFile()));
+
+ fs.inputFile(fs.predicates().all());
+ }
+
+ @Test
+ public void input_file_supports_non_indexed_predicates() throws Exception {
+ DefaultFileSystem fs = new DefaultFileSystem();
+ fs.add(new DefaultInputFile("src/Bar.java").setLanguage("java").setFile(temp.newFile()));
+
+ // it would fail if more than one java file
+ assertThat(fs.inputFile(fs.predicates().hasLanguage("java"))).isNotNull();
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.InputFile;
+
+import java.io.File;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DefaultInputFileTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Test
+ public void test() throws Exception {
+ DefaultInputFile inputFile = new DefaultInputFile("src/Foo.php")
+ .setFile(temp.newFile("Foo.php"))
+ .setKey("ABCDE")
+ .setHash("1234")
+ .setLines(42)
+ .setLanguage("php")
+ .setStatus(InputFile.Status.ADDED)
+ .setType(InputFile.Type.TEST);
+
+ assertThat(inputFile.relativePath()).isEqualTo("src/Foo.php");
+ assertThat(new File(inputFile.relativePath())).isRelative();
+ assertThat(inputFile.absolutePath()).endsWith("Foo.php");
+ 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.hash()).isEqualTo("1234");
+ }
+
+ @Test
+ public void test_equals_and_hashcode() throws Exception {
+ DefaultInputFile f1 = new DefaultInputFile("src/Foo.php");
+ DefaultInputFile f1a = new DefaultInputFile("src/Foo.php");
+ DefaultInputFile f2 = new DefaultInputFile("src/Bar.php");
+
+ 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("src/Foo.php").setAbsolutePath("/path/to/src/Foo.php");
+ assertThat(file.toString()).isEqualTo("[relative=src/Foo.php, abs=/path/to/src/Foo.php]");
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.fs.internal;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.InputFile;
+
+import java.io.File;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class PathPatternTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Test
+ public void match_relative_path() throws Exception {
+ PathPattern pattern = PathPattern.create("**/*Foo.java");
+ assertThat(pattern.toString()).isEqualTo("**/*Foo.java");
+
+ File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.java");
+ InputFile inputFile = new DefaultInputFile("src/main/java/org/MyFoo.java").setFile(file);
+ assertThat(pattern.match(inputFile)).isTrue();
+
+ // case sensitive by default
+ file = new File(temp.newFolder(), "src/main/java/org/MyFoo.JAVA");
+ inputFile = new DefaultInputFile("src/main/java/org/MyFoo.JAVA").setFile(file);
+ assertThat(pattern.match(inputFile)).isFalse();
+
+ file = new File(temp.newFolder(), "src/main/java/org/Other.java");
+ inputFile = new DefaultInputFile("src/main/java/org/Other.java").setFile(file);
+ assertThat(pattern.match(inputFile)).isFalse();
+ }
+
+ @Test
+ public void match_relative_path_and_insensitive_file_extension() throws Exception {
+ PathPattern pattern = PathPattern.create("**/*Foo.java");
+
+ File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.JAVA");
+ InputFile inputFile = new DefaultInputFile("src/main/java/org/MyFoo.JAVA").setFile(file);
+ assertThat(pattern.match(inputFile, false)).isTrue();
+
+ file = new File(temp.newFolder(), "src/main/java/org/Other.java");
+ inputFile = new DefaultInputFile("src/main/java/org/Other.java").setFile(file);
+ assertThat(pattern.match(inputFile, false)).isFalse();
+ }
+
+ @Test
+ public void match_absolute_path() throws Exception {
+ PathPattern pattern = PathPattern.create("file:**/src/main/**Foo.java");
+ assertThat(pattern.toString()).isEqualTo("file:**/src/main/**Foo.java");
+
+ File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.java");
+ InputFile inputFile = new DefaultInputFile("src/main/java/org/MyFoo.java").setFile(file);
+ assertThat(pattern.match(inputFile)).isTrue();
+
+ // case sensitive by default
+ file = new File(temp.newFolder(), "src/main/java/org/MyFoo.JAVA");
+ inputFile = new DefaultInputFile("src/main/java/org/MyFoo.JAVA").setFile(file);
+ assertThat(pattern.match(inputFile)).isFalse();
+
+ file = new File(temp.newFolder(), "src/main/java/org/Other.java");
+ inputFile = new DefaultInputFile("src/main/java/org/Other.java").setFile(file);
+ assertThat(pattern.match(inputFile)).isFalse();
+ }
+
+ @Test
+ public void match_absolute_path_and_insensitive_file_extension() throws Exception {
+ PathPattern pattern = PathPattern.create("file:**/src/main/**Foo.java");
+ assertThat(pattern.toString()).isEqualTo("file:**/src/main/**Foo.java");
+
+ File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.JAVA");
+ InputFile inputFile = new DefaultInputFile("src/main/java/org/MyFoo.JAVA").setFile(file);
+ assertThat(pattern.match(inputFile, false)).isTrue();
+
+ file = new File(temp.newFolder(), "src/main/java/org/Other.JAVA");
+ inputFile = new DefaultInputFile("src/main/java/org/Other.JAVA").setFile(file);
+ assertThat(pattern.match(inputFile, false)).isFalse();
+ }
+
+ @Test
+ public void create_array_of_patterns() throws Exception {
+ PathPattern[] patterns = PathPattern.create(new String[]{
+ "**/src/main/**Foo.java",
+ "file:**/src/main/**Bar.java"
+ });
+ assertThat(patterns).hasSize(2);
+ assertThat(patterns[0].toString()).isEqualTo("**/src/main/**Foo.java");
+ assertThat(patterns[1].toString()).isEqualTo("file:**/src/main/**Bar.java");
+ }
+}