diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2015-01-28 22:41:25 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2015-02-10 17:16:01 +0100 |
commit | e9b5effe30cf68820a3dfb00bf736a325313206b (patch) | |
tree | 74323c9b304a2b5f76d2f77ca6c4b86d03fa4296 /sonar-batch/src | |
parent | 46b6c6f692964e6202aa269245e579badb856e94 (diff) | |
download | sonarqube-e9b5effe30cf68820a3dfb00bf736a325313206b.tar.gz sonarqube-e9b5effe30cf68820a3dfb00bf736a325313206b.zip |
SONAR-6134, SONAR-6048 Improve performance of FS indexation
Diffstat (limited to 'sonar-batch/src')
144 files changed, 1016 insertions, 1041 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/ProjectConfigurator.java b/sonar-batch/src/main/java/org/sonar/batch/ProjectConfigurator.java index b345f2e1b0a..21dd83e51ab 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/ProjectConfigurator.java +++ b/sonar-batch/src/main/java/org/sonar/batch/ProjectConfigurator.java @@ -41,6 +41,10 @@ import java.util.Date; import static org.sonar.api.utils.DateUtils.formatDateTime; import static org.sonar.api.utils.DateUtils.longToDate; +/** + * Used by views !! + * + */ public class ProjectConfigurator implements BatchComponent { private static final Logger LOG = LoggerFactory.getLogger(ProjectConfigurator.class); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java index f55308c348c..80525697653 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java @@ -19,6 +19,14 @@ */ package org.sonar.batch.bootstrap; +import org.sonar.batch.components.PastSnapshotFinder; + +import org.sonar.batch.deprecated.components.PastSnapshotFinderByDate; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByDays; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByPreviousAnalysis; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByPreviousVersion; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByVersion; +import org.sonar.batch.repository.user.UserRepository; import org.sonar.api.Plugin; import org.sonar.api.config.EmailSettings; import org.sonar.api.platform.ComponentContainer; @@ -28,12 +36,6 @@ import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.System2; import org.sonar.api.utils.UriReader; import org.sonar.api.utils.internal.TempFolderCleaner; -import org.sonar.batch.components.PastSnapshotFinder; -import org.sonar.batch.components.PastSnapshotFinderByDate; -import org.sonar.batch.components.PastSnapshotFinderByDays; -import org.sonar.batch.components.PastSnapshotFinderByPreviousAnalysis; -import org.sonar.batch.components.PastSnapshotFinderByPreviousVersion; -import org.sonar.batch.components.PastSnapshotFinderByVersion; import org.sonar.batch.platform.DefaultServer; import org.sonar.batch.repository.DefaultGlobalRepositoriesLoader; import org.sonar.batch.repository.DefaultPreviousIssuesLoader; @@ -42,7 +44,6 @@ import org.sonar.batch.repository.GlobalRepositoriesLoader; import org.sonar.batch.repository.GlobalRepositoriesProvider; import org.sonar.batch.repository.PreviousIssuesLoader; import org.sonar.batch.repository.ProjectRepositoriesLoader; -import org.sonar.batch.user.UserRepository; import org.sonar.core.cluster.NullQueue; import org.sonar.core.config.Logback; import org.sonar.core.i18n.DefaultI18n; diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java index d301f1aa476..b3fd7e46392 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.bootstrap; +import org.sonar.batch.components.PastMeasuresLoader; + import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; import org.sonar.api.platform.ComponentContainer; @@ -28,19 +30,21 @@ import org.sonar.api.task.TaskComponent; import org.sonar.api.task.TaskDefinition; import org.sonar.api.utils.MessageException; import org.sonar.batch.bootstrapper.EnvironmentInformation; -import org.sonar.batch.components.PastMeasuresLoader; +import org.sonar.batch.deprecated.tasks.ListTask; +import org.sonar.batch.deprecated.tasks.Tasks; import org.sonar.batch.scan.DeprecatedProjectReactorBuilder; import org.sonar.batch.scan.ProjectReactorBuilder; import org.sonar.batch.scan.ScanTask; import org.sonar.batch.scan.measure.DefaultMetricFinder; import org.sonar.batch.scan.measure.DeprecatedMetricFinder; -import org.sonar.batch.tasks.ListTask; -import org.sonar.batch.tasks.Tasks; import org.sonar.core.permission.PermissionFacade; import org.sonar.core.resource.DefaultResourcePermissions; import java.util.Map; +/** + * Used by views !! + */ public class TaskContainer extends ComponentContainer { private final Map<String, String> taskProperties; diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java index 6cfd61dcabe..d969e172338 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java @@ -38,6 +38,9 @@ import java.util.Collections; import java.util.List; import java.util.Map; +/** + * Can't be moved. Used by devcockpit. + */ public class PastMeasuresLoader implements BatchComponent { private Map<Integer, Metric> metricByIds; diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java index 48955f9738f..a85eb172cf7 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java @@ -26,11 +26,15 @@ import org.sonar.api.database.model.Snapshot; import org.sonar.api.utils.DateUtils; import javax.annotation.Nullable; + import java.util.Calendar; import java.util.Date; import static org.sonar.api.utils.DateUtils.longToDate; +/** + * Used by devcockpit + */ public class PastSnapshot { private int index; @@ -96,7 +100,7 @@ public class PastSnapshot { return this; } - Integer getProjectSnapshotId() { + public Integer getProjectSnapshotId() { return projectSnapshot != null ? projectSnapshot.getId() : null; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java index 56263637b14..745dcbf817b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java @@ -27,6 +27,11 @@ import org.sonar.api.BatchExtension; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; import org.sonar.api.database.model.Snapshot; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByDate; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByDays; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByPreviousAnalysis; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByPreviousVersion; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByVersion; import javax.annotation.Nullable; @@ -34,6 +39,9 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; +/** + * Can't be moved since it is used by devcockpit. + */ public class PastSnapshotFinder implements BatchExtension { private static final Logger LOG = LoggerFactory.getLogger(PastSnapshotFinder.class); diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java index 2d4c71bc9c1..ec346aa4dcc 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java @@ -27,8 +27,11 @@ import org.sonar.api.batch.RequiresDB; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Qualifiers; +import org.sonar.batch.deprecated.components.Period; +import org.sonar.batch.deprecated.components.PeriodsDefinition; import javax.annotation.CheckForNull; + import java.util.List; import static com.google.common.collect.Lists.newLinkedList; diff --git a/sonar-batch/src/main/java/org/sonar/batch/debt/NewDebtDecorator.java b/sonar-batch/src/main/java/org/sonar/batch/debt/NewDebtDecorator.java index 43046d3763b..452fe02551c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/debt/NewDebtDecorator.java +++ b/sonar-batch/src/main/java/org/sonar/batch/debt/NewDebtDecorator.java @@ -20,6 +20,9 @@ package org.sonar.batch.debt; +import org.sonar.batch.components.TimeMachineConfiguration; + +import org.sonar.batch.deprecated.components.Period; import com.google.common.collect.ImmutableList; import org.sonar.api.batch.Decorator; import org.sonar.api.batch.DecoratorBarriers; @@ -35,8 +38,6 @@ import org.sonar.api.measures.MeasureUtils; import org.sonar.api.measures.Metric; import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; -import org.sonar.batch.components.Period; -import org.sonar.batch.components.TimeMachineConfiguration; import javax.annotation.Nullable; diff --git a/sonar-batch/src/main/java/org/sonar/batch/DeprecatedSensorContext.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/DeprecatedSensorContext.java index 49998b1d2f7..e02cadce624 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/DeprecatedSensorContext.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/DeprecatedSensorContext.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.batch.deprecated; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; diff --git a/sonar-batch/src/main/java/org/sonar/batch/ResourceFilters.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/ResourceFilters.java index 8680300a412..f60713506d7 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/ResourceFilters.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/ResourceFilters.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.batch.deprecated; import com.google.common.base.Joiner; import org.slf4j.Logger; diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultProjectClasspath.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/DefaultProjectClasspath.java index d497906439f..f5a07c9889c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/DefaultProjectClasspath.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/DefaultProjectClasspath.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.batch.deprecated.components; import com.google.common.collect.Lists; import org.apache.maven.project.MavenProject; diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultResourceCreationLock.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/DefaultResourceCreationLock.java index 1adb5074e64..52ca67c348e 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/DefaultResourceCreationLock.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/DefaultResourceCreationLock.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.batch.deprecated.components; import org.sonar.api.batch.ResourceCreationLock; diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/DefaultTimeMachine.java index 79d61dd2f74..460f3a647c4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/DefaultTimeMachine.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.batch.deprecated.components; import com.google.common.collect.Lists; import com.google.common.collect.Maps; diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByDate.java index fbae4382e43..5a5fff138a1 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByDate.java @@ -17,7 +17,7 @@ * 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.components; +package org.sonar.batch.deprecated.components; import org.sonar.api.BatchExtension; import org.sonar.api.CoreProperties; @@ -25,8 +25,10 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.DateUtils; +import org.sonar.batch.components.PastSnapshot; import javax.annotation.CheckForNull; + import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @@ -41,7 +43,7 @@ public class PastSnapshotFinderByDate implements BatchExtension { this.session = session; } - PastSnapshot findByDate(Snapshot projectSnapshot, Date date) { + public PastSnapshot findByDate(Snapshot projectSnapshot, Date date) { Integer projectId = projectSnapshot != null ? projectSnapshot.getResourceId() : null; return findByDate(projectId, date); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDays.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByDays.java index 0f802682c8a..5183236cc05 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDays.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByDays.java @@ -17,7 +17,7 @@ * 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.components; +package org.sonar.batch.deprecated.components; import org.apache.commons.lang.time.DateUtils; import org.sonar.api.BatchExtension; @@ -25,8 +25,10 @@ import org.sonar.api.CoreProperties; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Qualifiers; +import org.sonar.batch.components.PastSnapshot; import javax.annotation.CheckForNull; + import java.util.Date; import java.util.List; @@ -40,7 +42,7 @@ public class PastSnapshotFinderByDays implements BatchExtension { this.session = session; } - PastSnapshot findFromDays(Snapshot projectSnapshot, int days) { + public PastSnapshot findFromDays(Snapshot projectSnapshot, int days) { Date targetDate = DateUtils.addDays(longToDate(projectSnapshot.getCreatedAtMs()), -days); String hql = "from " + Snapshot.class.getSimpleName() + " where resourceId=:resourceId AND status=:status AND createdAt<:date AND qualifier<>:lib order by createdAt asc"; List<Snapshot> snapshots = session.createQuery(hql) diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysis.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousAnalysis.java index 506295775ef..e770975d461 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysis.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousAnalysis.java @@ -17,7 +17,7 @@ * 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.components; +package org.sonar.batch.deprecated.components; import org.sonar.api.BatchExtension; import org.sonar.api.CoreProperties; @@ -25,6 +25,7 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.DateUtils; +import org.sonar.batch.components.PastSnapshot; import java.text.SimpleDateFormat; import java.util.Date; @@ -40,17 +41,17 @@ public class PastSnapshotFinderByPreviousAnalysis implements BatchExtension { this.session = session; } - PastSnapshot findByPreviousAnalysis(Snapshot projectSnapshot) { + public PastSnapshot findByPreviousAnalysis(Snapshot projectSnapshot) { String hql = "from " + Snapshot.class.getSimpleName() - + " where createdAt<:date AND resourceId=:resourceId AND status=:status and last=:last and qualifier<>:lib order by createdAt desc"; + + " where createdAt<:date AND resourceId=:resourceId AND status=:status and last=:last and qualifier<>:lib order by createdAt desc"; List<Snapshot> snapshots = session.createQuery(hql) - .setParameter("date", projectSnapshot.getCreatedAtMs()) - .setParameter("resourceId", projectSnapshot.getResourceId()) - .setParameter("status", Snapshot.STATUS_PROCESSED) - .setParameter("last", true) - .setParameter("lib", Qualifiers.LIBRARY) - .setMaxResults(1) - .getResultList(); + .setParameter("date", projectSnapshot.getCreatedAtMs()) + .setParameter("resourceId", projectSnapshot.getResourceId()) + .setParameter("status", Snapshot.STATUS_PROCESSED) + .setParameter("last", true) + .setParameter("lib", Qualifiers.LIBRARY) + .setMaxResults(1) + .getResultList(); if (snapshots.isEmpty()) { return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousVersion.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousVersion.java index 6437688bf68..0125fa7d4d7 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByPreviousVersion.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousVersion.java @@ -17,7 +17,9 @@ * 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.components; +package org.sonar.batch.deprecated.components; + +import org.sonar.batch.components.PastSnapshot; import org.sonar.api.BatchExtension; import org.sonar.api.CoreProperties; @@ -37,7 +39,7 @@ public class PastSnapshotFinderByPreviousVersion implements BatchExtension { this.session = session; } - PastSnapshot findByPreviousVersion(Snapshot projectSnapshot) { + public PastSnapshot findByPreviousVersion(Snapshot projectSnapshot) { String currentVersion = projectSnapshot.getVersion(); Integer resourceId = projectSnapshot.getResourceId(); @@ -45,10 +47,10 @@ public class PastSnapshotFinderByPreviousVersion implements BatchExtension { " where name<>:version AND category='Version' AND resourceId=:resourceId ORDER BY date DESC"; List<Event> events = session.createQuery(hql) - .setParameter("version", currentVersion) - .setParameter("resourceId", resourceId) - .setMaxResults(1) - .getResultList(); + .setParameter("version", currentVersion) + .setParameter("resourceId", resourceId) + .setMaxResults(1) + .getResultList(); if (events.isEmpty()) { return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByVersion.java index 161bd87b2ee..c6f5423ffab 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByVersion.java @@ -17,7 +17,9 @@ * 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.components; +package org.sonar.batch.deprecated.components; + +import org.sonar.batch.components.PastSnapshot; import org.sonar.api.BatchExtension; import org.sonar.api.CoreProperties; @@ -38,15 +40,15 @@ public class PastSnapshotFinderByVersion implements BatchExtension { this.session = session; } - PastSnapshot findByVersion(Snapshot projectSnapshot, String version) { + public PastSnapshot findByVersion(Snapshot projectSnapshot, String version) { String hql = "from " + Snapshot.class.getSimpleName() + " where version=:version AND resourceId=:resourceId AND status=:status AND qualifier<>:lib order by createdAt desc"; List<Snapshot> snapshots = session.createQuery(hql) - .setParameter("version", version) - .setParameter("resourceId", projectSnapshot.getResourceId()) - .setParameter("status", Snapshot.STATUS_PROCESSED) - .setParameter("lib", Qualifiers.LIBRARY) - .setMaxResults(1) - .getResultList(); + .setParameter("version", version) + .setParameter("resourceId", projectSnapshot.getResourceId()) + .setParameter("status", Snapshot.STATUS_PROCESSED) + .setParameter("lib", Qualifiers.LIBRARY) + .setMaxResults(1) + .getResultList(); PastSnapshot result; if (snapshots.isEmpty()) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/Period.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/Period.java index 4e2f15e88b9..45ca282dd42 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/Period.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/Period.java @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.batch.components; +package org.sonar.batch.deprecated.components; import javax.annotation.CheckForNull; import javax.annotation.Nullable; diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PeriodsDefinition.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/PeriodsDefinition.java index 6f1eb16a5bc..b235129693f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PeriodsDefinition.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/PeriodsDefinition.java @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.batch.components; +package org.sonar.batch.deprecated.components; import org.sonar.api.BatchComponent; import org.sonar.api.config.Settings; @@ -26,6 +26,8 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Qualifiers; import org.sonar.batch.ProjectTree; +import org.sonar.batch.components.PastSnapshot; +import org.sonar.batch.components.PastSnapshotFinder; import javax.persistence.Query; import java.util.List; @@ -46,7 +48,7 @@ public class PeriodsDefinition implements BatchComponent { private List<PastSnapshot> projectPastSnapshots; public PeriodsDefinition(DatabaseSession session, ProjectTree projectTree, Settings settings, - PastSnapshotFinder pastSnapshotFinder) { + PastSnapshotFinder pastSnapshotFinder) { this.session = session; this.projectTree = projectTree; this.settings = settings; @@ -89,7 +91,7 @@ public class PeriodsDefinition implements BatchComponent { /** * @return past snapshots of root project */ - List<PastSnapshot> getRootProjectPastSnapshots() { + public List<PastSnapshot> getRootProjectPastSnapshots() { return projectPastSnapshots; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/package-info.java new file mode 100644 index 00000000000..65baa1e8409 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/components/package-info.java @@ -0,0 +1,23 @@ +/* + * 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.batch.deprecated.components; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/DecoratorsSelector.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/decorator/DecoratorsSelector.java index 7ce661aaa79..c50574fb637 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/DecoratorsSelector.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/decorator/DecoratorsSelector.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.batch.deprecated.decorator; import com.google.common.collect.HashMultimap; import com.google.common.collect.SetMultimap; diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultDecoratorContext.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/decorator/DefaultDecoratorContext.java index 3d18e06c5a9..1740a1aebd9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/DefaultDecoratorContext.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/decorator/DefaultDecoratorContext.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.batch.deprecated.decorator; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; diff --git a/sonar-batch/src/main/java/org/sonar/batch/FormulaDecorator.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/decorator/FormulaDecorator.java index 75de88257d8..8b60efcaf19 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/FormulaDecorator.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/decorator/FormulaDecorator.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.batch.deprecated.decorator; import org.sonar.api.batch.*; import org.sonar.api.measures.FormulaData; diff --git a/sonar-batch/src/main/java/org/sonar/batch/tasks/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/package-info.java index 0f2b4e035bb..ca1dcada4ba 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/tasks/package-info.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/package-info.java @@ -18,6 +18,6 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ @ParametersAreNonnullByDefault -package org.sonar.batch.tasks; +package org.sonar.batch.deprecated; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/tasks/ListTask.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/tasks/ListTask.java index 93a0419b32f..bcd02a00e3b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/tasks/ListTask.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/tasks/ListTask.java @@ -17,7 +17,7 @@ * 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.tasks; +package org.sonar.batch.deprecated.tasks; import org.sonar.api.task.Task; import org.sonar.api.task.TaskDefinition; diff --git a/sonar-batch/src/main/java/org/sonar/batch/tasks/Tasks.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/tasks/Tasks.java index f68fb691269..c97bdea7d6a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/tasks/Tasks.java +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/tasks/Tasks.java @@ -17,7 +17,7 @@ * 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.tasks; +package org.sonar.batch.deprecated.tasks; import com.google.common.collect.ImmutableSortedMap; import com.google.common.collect.Maps; diff --git a/sonar-batch/src/main/java/org/sonar/batch/deprecated/tasks/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/tasks/package-info.java new file mode 100644 index 00000000000..f346de52eb4 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/deprecated/tasks/package-info.java @@ -0,0 +1,23 @@ +/* + * 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.batch.deprecated.tasks; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index eeb37de63ed..6cf8ae826f4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -38,7 +38,6 @@ import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; import org.sonar.api.measures.MeasuresFilter; import org.sonar.api.measures.MeasuresFilters; -import org.sonar.api.resources.Directory; import org.sonar.api.resources.File; import org.sonar.api.resources.Project; import org.sonar.api.resources.ProjectLink; @@ -107,7 +106,6 @@ public class DefaultIndex extends SonarIndex { // caches private Project currentProject; private Map<Resource, Bucket> buckets = Maps.newLinkedHashMap(); - private Map<String, Bucket> bucketsByDeprecatedKey = Maps.newLinkedHashMap(); private Set<Dependency> dependencies = Sets.newLinkedHashSet(); private Map<Resource, Map<Resource, Dependency>> outgoingDependenciesByResource = Maps.newLinkedHashMap(); private Map<Resource, Map<Resource, Dependency>> incomingDependenciesByResource = Maps.newLinkedHashMap(); @@ -166,9 +164,6 @@ public class DefaultIndex extends SonarIndex { private void addBucket(Resource resource, Bucket bucket) { buckets.put(resource, bucket); - if (StringUtils.isNotBlank(resource.getDeprecatedKey())) { - bucketsByDeprecatedKey.put(resource.getDeprecatedKey(), bucket); - } } private void addModule(Project parent, Project module) { @@ -613,12 +608,6 @@ public class DefaultIndex extends SonarIndex { return getBucket(reference) != null; } - /** - * Should support 2 situations - * 1) key = new key and deprecatedKey = old key : this is the standard use case in a perfect world - * 2) key = null and deprecatedKey = oldKey : this is for plugins that are using deprecated constructors of - * {@link File} and {@link Directory} - */ private Bucket getBucket(@Nullable Resource reference) { if (reference == null) { return null; @@ -626,17 +615,6 @@ public class DefaultIndex extends SonarIndex { if (StringUtils.isNotBlank(reference.getKey())) { return buckets.get(reference); } - if (StringUtils.isNotBlank(reference.getDeprecatedKey())) { - // Fallback to use deprecated key - Bucket bucket = bucketsByDeprecatedKey.get(reference.getDeprecatedKey()); - if (bucket != null) { - // Fix reference resource - reference.setKey(bucket.getResource().getKey()); - reference.setPath(bucket.getResource().getPath()); - LOG.debug("Resource {} was found using deprecated key. Please update your plugin.", reference); - return bucket; - } - } return null; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java index 0cb5a0b182d..69b7dd98d85 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java @@ -24,20 +24,21 @@ import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.BatchComponent; -import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.ResourceModel; import org.sonar.api.resources.Directory; -import org.sonar.api.resources.File; import org.sonar.api.resources.Project; import org.sonar.api.resources.Qualifiers; -import org.sonar.api.resources.Resource; import org.sonar.api.resources.Scopes; +import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.api.utils.PathUtils; +import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; import org.sonar.batch.util.DeprecatedKeyUtils; +import javax.annotation.CheckForNull; + import java.util.HashMap; import java.util.List; import java.util.Map; @@ -48,17 +49,19 @@ public class ResourceKeyMigration implements BatchComponent { private static final String COMPONENT_CHANGED_TO = "Component {} changed to {}"; private final Logger logger; private final DatabaseSession session; + private final PathResolver pathResolver; private boolean migrationNeeded = false; - public ResourceKeyMigration(DatabaseSession session) { - this(session, LoggerFactory.getLogger(ResourceKeyMigration.class)); + public ResourceKeyMigration(DatabaseSession session, PathResolver pathResolver) { + this(session, pathResolver, LoggerFactory.getLogger(ResourceKeyMigration.class)); } @VisibleForTesting - ResourceKeyMigration(DatabaseSession session, Logger logger) { + ResourceKeyMigration(DatabaseSession session, PathResolver pathResolver, Logger logger) { this.session = session; this.logger = logger; + this.pathResolver = pathResolver; } public void checkIfMigrationNeeded(Project rootProject) { @@ -68,19 +71,19 @@ public class ResourceKeyMigration implements BatchComponent { } } - public void migrateIfNeeded(Project module, FileSystem fs) { + public void migrateIfNeeded(Project module, DefaultModuleFileSystem fs) { if (migrationNeeded) { - migrateIfNeeded(module, fs.inputFiles(fs.predicates().all())); + migrateIfNeeded(module, fs.inputFiles(fs.predicates().all()), fs); } } - void migrateIfNeeded(Project module, Iterable<InputFile> inputFiles) { + void migrateIfNeeded(Project module, Iterable<InputFile> inputFiles, DefaultModuleFileSystem fs) { logger.info("Update component keys"); Map<String, InputFile> deprecatedFileKeyMapper = new HashMap<String, InputFile>(); Map<String, InputFile> deprecatedTestKeyMapper = new HashMap<String, InputFile>(); Map<String, String> deprecatedDirectoryKeyMapper = new HashMap<String, String>(); for (InputFile inputFile : inputFiles) { - String deprecatedKey = ((DeprecatedDefaultInputFile) inputFile).deprecatedKey(); + String deprecatedKey = computeDeprecatedKey(module.getKey(), (DeprecatedDefaultInputFile) inputFile, fs); if (deprecatedKey != null) { if (InputFile.Type.TEST == inputFile.type() && !deprecatedTestKeyMapper.containsKey(deprecatedKey)) { deprecatedTestKeyMapper.put(deprecatedKey, inputFile); @@ -97,6 +100,23 @@ public class ResourceKeyMigration implements BatchComponent { session.commit(); } + @CheckForNull + private String computeDeprecatedKey(String moduleKey, DeprecatedDefaultInputFile inputFile, DefaultModuleFileSystem fs) { + List<java.io.File> sourceDirs = InputFile.Type.MAIN == inputFile.type() ? fs.sourceDirs() : fs.testDirs(); + for (java.io.File sourceDir : sourceDirs) { + String sourceRelativePath = pathResolver.relativePath(sourceDir, inputFile.file()); + if (sourceRelativePath != null) { + if ("java".equals(inputFile.language())) { + return new StringBuilder() + .append(moduleKey).append(":").append(DeprecatedKeyUtils.getJavaFileDeprecatedKey(sourceRelativePath)).toString(); + } else { + return new StringBuilder().append(moduleKey).append(":").append(sourceRelativePath).toString(); + } + } + } + return null; + } + private void migrateFiles(Project module, Map<String, InputFile> deprecatedFileKeyMapper, Map<String, InputFile> deprecatedTestKeyMapper, Map<String, String> deprecatedDirectoryKeyMapper, int moduleId) { @@ -113,13 +133,11 @@ public class ResourceKeyMigration implements BatchComponent { String newEffectiveKey = ((DeprecatedDefaultInputFile) matchedFile).key(); // Now compute migration of the parent dir String oldKey = StringUtils.substringAfterLast(oldEffectiveKey, ":"); - Resource sonarFile; String parentOldKey; if ("java".equals(resourceModel.getLanguageKey())) { parentOldKey = String.format("%s:%s", module.getEffectiveKey(), DeprecatedKeyUtils.getJavaFileParentDeprecatedKey(oldKey)); } else { - sonarFile = new File(oldKey); - parentOldKey = String.format("%s:%s", module.getEffectiveKey(), sonarFile.getParent().getDeprecatedKey()); + parentOldKey = String.format("%s:%s", module.getEffectiveKey(), oldParentKey(oldKey)); } String parentNewKey = String.format("%s:%s", module.getEffectiveKey(), getParentKey(matchedFile)); if (!deprecatedDirectoryKeyMapper.containsKey(parentOldKey)) { @@ -137,6 +155,18 @@ public class ResourceKeyMigration implements BatchComponent { } } + private String oldParentKey(String oldKey) { + String cleanKey = StringUtils.trim(oldKey.replace('\\', '/')); + if (cleanKey.indexOf(Directory.SEPARATOR) >= 0) { + String oldParentKey = Directory.parseKey(StringUtils.substringBeforeLast(oldKey, Directory.SEPARATOR)); + oldParentKey = StringUtils.removeStart(oldParentKey, Directory.SEPARATOR); + oldParentKey = StringUtils.removeEnd(oldParentKey, Directory.SEPARATOR); + return oldParentKey; + } else { + return Directory.ROOT; + } + } + private void updateKey(ResourceModel resourceModel, String newEffectiveKey, Map<String, ResourceModel> disabledResourceByKey) { // Look for disabled resource with conflicting key if (disabledResourceByKey.containsKey(newEffectiveKey)) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/SourceDataFactory.java b/sonar-batch/src/main/java/org/sonar/batch/index/SourceDataFactory.java index e3e9f33bc79..9a089eaef97 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/SourceDataFactory.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/SourceDataFactory.java @@ -35,6 +35,7 @@ import org.sonar.api.utils.KeyValueFormat; import org.sonar.batch.duplication.DuplicationCache; import org.sonar.batch.highlighting.SyntaxHighlightingData; import org.sonar.batch.highlighting.SyntaxHighlightingRule; +import org.sonar.batch.scan.filesystem.InputFileMetadata; import org.sonar.batch.scan.measure.MeasureCache; import org.sonar.batch.source.CodeColorizers; import org.sonar.batch.symbol.SymbolData; @@ -72,18 +73,18 @@ public class SourceDataFactory implements BatchComponent { this.codeColorizers = codeColorizers; } - public byte[] consolidateData(DefaultInputFile inputFile) throws IOException { + public byte[] consolidateData(DefaultInputFile inputFile, InputFileMetadata metadata) throws IOException { FileSourceDb.Data.Builder dataBuilder = createForSource(inputFile); applyLineMeasures(inputFile, dataBuilder); applyDuplications(inputFile.key(), dataBuilder); - applyHighlighting(inputFile, dataBuilder); - applySymbolReferences(inputFile, dataBuilder); + applyHighlighting(inputFile, metadata, dataBuilder); + applySymbolReferences(inputFile, metadata, dataBuilder); return FileSourceDto.encodeData(dataBuilder.build()); } FileSourceDb.Data.Builder createForSource(DefaultInputFile inputFile) throws IOException { FileSourceDb.Data.Builder result = FileSourceDb.Data.newBuilder(); - List<String> lines = FileUtils.readLines(inputFile.file(), inputFile.encoding()); + List<String> lines = FileUtils.readLines(inputFile.file(), inputFile.charset()); // Missing empty last line if (lines.size() == inputFile.lines() - 1) { lines.add(""); @@ -191,11 +192,11 @@ public class SourceDataFactory implements BatchComponent { void apply(String value, FileSourceDb.Line.Builder lineBuilder); } - void applyHighlighting(DefaultInputFile inputFile, FileSourceDb.Data.Builder to) { + void applyHighlighting(DefaultInputFile inputFile, InputFileMetadata metadata, FileSourceDb.Data.Builder to) { SyntaxHighlightingData highlighting = componentDataCache.getData(inputFile.key(), SnapshotDataTypes.SYNTAX_HIGHLIGHTING); String language = inputFile.language(); if (highlighting == null && language != null) { - highlighting = codeColorizers.toSyntaxHighlighting(inputFile.file(), inputFile.encoding(), language); + highlighting = codeColorizers.toSyntaxHighlighting(inputFile.file(), inputFile.charset(), language); } if (highlighting == null) { return; @@ -204,12 +205,12 @@ public class SourceDataFactory implements BatchComponent { RuleItemWriter ruleItemWriter = new RuleItemWriter(); int currentLineIdx = 1; for (SyntaxHighlightingRule rule : highlighting.syntaxHighlightingRuleSet()) { - while (currentLineIdx < inputFile.lines() && rule.getStartPosition() >= inputFile.originalLineOffsets()[currentLineIdx]) { + while (currentLineIdx < inputFile.lines() && rule.getStartPosition() >= metadata.originalLineOffsets()[currentLineIdx]) { // This rule starts on another line so advance currentLineIdx++; } // Now we know current rule starts on current line - writeDataPerLine(inputFile.originalLineOffsets(), rule, rule.getStartPosition(), rule.getEndPosition(), highlightingPerLine, currentLineIdx, ruleItemWriter); + writeDataPerLine(metadata.originalLineOffsets(), rule, rule.getStartPosition(), rule.getEndPosition(), highlightingPerLine, currentLineIdx, ruleItemWriter); } for (int i = 0; i < highlightingPerLine.length; i++) { StringBuilder sb = highlightingPerLine[i]; @@ -219,7 +220,7 @@ public class SourceDataFactory implements BatchComponent { } } - void applySymbolReferences(DefaultInputFile file, FileSourceDb.Data.Builder to) { + void applySymbolReferences(DefaultInputFile file, InputFileMetadata metadata, FileSourceDb.Data.Builder to) { SymbolData symbolRefs = componentDataCache.getData(file.key(), SnapshotDataTypes.SYMBOL_HIGHLIGHTING); if (symbolRefs != null) { StringBuilder[] refsPerLine = new StringBuilder[file.lines()]; @@ -236,13 +237,13 @@ public class SourceDataFactory implements BatchComponent { int declarationStartOffset = symbol.getDeclarationStartOffset(); int declarationEndOffset = symbol.getDeclarationEndOffset(); int length = declarationEndOffset - declarationStartOffset; - addSymbol(symbolId, declarationStartOffset, declarationEndOffset, file.originalLineOffsets(), refsPerLine); + addSymbol(symbolId, declarationStartOffset, declarationEndOffset, metadata.originalLineOffsets(), refsPerLine); for (Integer referenceStartOffset : symbolRefs.referencesBySymbol().get(symbol)) { if (referenceStartOffset == declarationStartOffset) { // Ignore old API that used to store reference as first declaration continue; } - addSymbol(symbolId, referenceStartOffset, referenceStartOffset + length, file.originalLineOffsets(), refsPerLine); + addSymbol(symbolId, referenceStartOffset, referenceStartOffset + length, metadata.originalLineOffsets(), refsPerLine); } symbolId++; } @@ -255,12 +256,12 @@ public class SourceDataFactory implements BatchComponent { } } - private void addSymbol(int symbolId, int startOffset, int endOffset, long[] originalLineOffsets, StringBuilder[] result) { + private void addSymbol(int symbolId, int startOffset, int endOffset, int[] originalLineOffsets, StringBuilder[] result) { int startLine = binarySearchLine(startOffset, originalLineOffsets); writeDataPerLine(originalLineOffsets, symbolId, startOffset, endOffset, result, startLine, new SymbolItemWriter()); } - private int binarySearchLine(int declarationStartOffset, long[] originalLineOffsets) { + private int binarySearchLine(int declarationStartOffset, int[] originalLineOffsets) { int begin = 0; int end = originalLineOffsets.length - 1; while (begin < end) { @@ -274,7 +275,7 @@ public class SourceDataFactory implements BatchComponent { return begin + 1; } - private <G> void writeDataPerLine(long[] originalLineOffsets, G item, int globalStartOffset, int globalEndOffset, StringBuilder[] dataPerLine, int startLine, + private <G> void writeDataPerLine(int[] originalLineOffsets, G item, int globalStartOffset, int globalEndOffset, StringBuilder[] dataPerLine, int startLine, RangeItemWriter<G> writer) { int currentLineIdx = startLine; // We know current item starts on current line diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java index a7a18548418..14cd85e371a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java @@ -19,14 +19,14 @@ */ package org.sonar.batch.index; -import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.digest.DigestUtils; import org.apache.ibatis.session.ResultContext; import org.apache.ibatis.session.ResultHandler; -import org.sonar.api.batch.fs.InputPath; +import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.utils.System2; import org.sonar.batch.ProjectTree; +import org.sonar.batch.scan.filesystem.InputFileMetadata; import org.sonar.batch.scan.filesystem.InputPathCache; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; @@ -35,7 +35,9 @@ import org.sonar.core.source.db.FileSourceMapper; import javax.annotation.CheckForNull; +import java.io.BufferedReader; import java.io.IOException; +import java.nio.file.Files; import java.util.HashMap; import java.util.Map; @@ -73,10 +75,8 @@ public class SourcePersister implements ScanPersister { }); FileSourceMapper mapper = session.getMapper(FileSourceMapper.class); - for (InputPath inputPath : inputPathCache.all()) { - if (inputPath instanceof DefaultInputFile) { - persist(session, mapper, (DefaultInputFile) inputPath, previousDtosByUuid); - } + for (InputFile inputFile : inputPathCache.allFiles()) { + persist(session, mapper, (DefaultInputFile) inputFile, previousDtosByUuid); } } catch (Exception e) { throw new IllegalStateException("Unable to save file sources", e); @@ -87,7 +87,8 @@ public class SourcePersister implements ScanPersister { private void persist(DbSession session, FileSourceMapper mapper, DefaultInputFile inputFile, Map<String, FileSourceDto> previousDtosByUuid) { String fileUuid = resourceCache.get(inputFile.key()).resource().getUuid(); - byte[] data = computeData(inputFile); + InputFileMetadata metadata = inputPathCache.getFileMetadata(inputFile.moduleKey(), inputFile.relativePath()); + byte[] data = computeData(inputFile, metadata); String dataHash = DigestUtils.md5Hex(data); FileSourceDto previousDto = previousDtosByUuid.get(fileUuid); if (previousDto == null) { @@ -96,20 +97,20 @@ public class SourcePersister implements ScanPersister { .setFileUuid(fileUuid) .setBinaryData(data) .setDataHash(dataHash) - .setSrcHash(inputFile.hash()) - .setLineHashes(lineHashesAsMd5Hex(inputFile)) + .setSrcHash(metadata.hash()) + .setLineHashes(lineHashesAsMd5Hex(inputFile, metadata)) .setCreatedAt(system2.now()) .setUpdatedAt(system2.now()); mapper.insert(dto); session.commit(); } else { // Update only if data_hash has changed or if src_hash is missing (progressive migration) - if (!dataHash.equals(previousDto.getDataHash()) || !inputFile.hash().equals(previousDto.getSrcHash())) { + if (!dataHash.equals(previousDto.getDataHash()) || !metadata.hash().equals(previousDto.getSrcHash())) { previousDto .setBinaryData(data) .setDataHash(dataHash) - .setSrcHash(inputFile.hash()) - .setLineHashes(lineHashesAsMd5Hex(inputFile)) + .setSrcHash(metadata.hash()) + .setLineHashes(lineHashesAsMd5Hex(inputFile, metadata)) .setUpdatedAt(system2.now()); mapper.update(previousDto); session.commit(); @@ -118,24 +119,41 @@ public class SourcePersister implements ScanPersister { } @CheckForNull - private String lineHashesAsMd5Hex(DefaultInputFile inputFile) { - if (inputFile.lines() == 0) { + private String lineHashesAsMd5Hex(DefaultInputFile f, InputFileMetadata metadata) { + if (f.lines() == 0) { return null; } // A md5 string is 32 char long + '\n' = 33 - StringBuilder result = new StringBuilder(inputFile.lines() * (32 + 1)); - for (byte[] lineHash : inputFile.lineHashes()) { - if (result.length() > 0) { - result.append("\n"); + StringBuilder result = new StringBuilder(f.lines() * (32 + 1)); + + try { + BufferedReader reader = Files.newBufferedReader(f.path(), f.charset()); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < f.lines(); i++) { + String lineStr = reader.readLine(); + lineStr = lineStr == null ? "" : lineStr; + for (int j = 0; j < lineStr.length(); j++) { + char c = lineStr.charAt(j); + if (!Character.isWhitespace(c)) { + sb.append(c); + } + } + if (i > 0) { + result.append("\n"); + } + result.append(sb.length() > 0 ? DigestUtils.md5Hex(sb.toString()) : ""); + sb.setLength(0); } - result.append(lineHash != null ? Hex.encodeHexString(lineHash) : ""); + } catch (Exception e) { + throw new IllegalStateException("Unable to compute line hashes of file " + f, e); } + return result.toString(); } - private byte[] computeData(DefaultInputFile inputFile) { + private byte[] computeData(DefaultInputFile inputFile, InputFileMetadata metadata) { try { - return dataFactory.consolidateData(inputFile); + return dataFactory.consolidateData(inputFile, metadata); } catch (IOException e) { throw new IllegalStateException("Fail to read file " + inputFile, e); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/FileHashes.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/FileHashes.java index 49b0405787b..fbc293e8004 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/FileHashes.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/FileHashes.java @@ -19,11 +19,17 @@ */ package org.sonar.batch.issue.tracking; +import com.google.common.base.Charsets; import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.Multimap; import org.apache.commons.codec.binary.Hex; +import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang.ObjectUtils; +import org.sonar.api.batch.fs.internal.DefaultInputFile; +import java.io.BufferedReader; +import java.nio.file.Files; +import java.security.MessageDigest; import java.util.Collection; /** @@ -49,7 +55,29 @@ public final class FileHashes { return new FileHashes(hashes, linesByHash); } - public static FileHashes create(byte[][] hashes) { + public static FileHashes create(DefaultInputFile f) { + byte[][] hashes = new byte[f.lines()][]; + try { + BufferedReader reader = Files.newBufferedReader(f.path(), f.charset()); + MessageDigest lineMd5Digest = DigestUtils.getMd5Digest(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < f.lines(); i++) { + String lineStr = reader.readLine(); + if (lineStr != null) { + for (int j = 0; j < lineStr.length(); j++) { + char c = lineStr.charAt(j); + if (!Character.isWhitespace(c)) { + sb.append(c); + } + } + } + hashes[i] = sb.length() > 0 ? lineMd5Digest.digest(sb.toString().getBytes(Charsets.UTF_8)) : null; + sb.setLength(0); + } + } catch (Exception e) { + throw new IllegalStateException("Unable to compute line hashes of file " + f, e); + } + int size = hashes.length; Multimap<String, Integer> linesByHash = LinkedHashMultimap.create(); String[] hexHashes = new String[size]; diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueHandlers.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueHandlers.java index f8a98c29b00..b501e18ad66 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueHandlers.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueHandlers.java @@ -19,7 +19,7 @@ */ package org.sonar.batch.issue.tracking; -import org.sonar.api.BatchExtension; +import org.sonar.api.BatchComponent; import org.sonar.api.issue.Issue; import org.sonar.api.issue.IssueHandler; import org.sonar.api.issue.internal.DefaultIssue; @@ -30,7 +30,7 @@ import org.sonar.core.user.DefaultUser; import javax.annotation.Nullable; -public class IssueHandlers implements BatchExtension { +public class IssueHandlers implements BatchComponent { private final IssueHandler[] handlers; private final DefaultContext context; @@ -117,7 +117,7 @@ public class IssueHandlers implements BatchExtension { @Override public IssueHandler.Context assign(@Nullable String assignee) { User user = null; - if(assignee != null) { + if (assignee != null) { user = new DefaultUser().setLogin(assignee).setName(assignee); } updater.assign(issue, user, changeContext); diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueTrackingDecorator.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueTrackingDecorator.java index c3826cbd391..c1ae052034c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueTrackingDecorator.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueTrackingDecorator.java @@ -19,8 +19,6 @@ */ package org.sonar.batch.issue.tracking; -import org.sonar.api.batch.RequiresDB; - import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; import com.google.common.collect.Lists; @@ -32,6 +30,7 @@ import org.sonar.api.batch.DecoratorBarriers; import org.sonar.api.batch.DecoratorContext; import org.sonar.api.batch.DependedUpon; import org.sonar.api.batch.DependsUpon; +import org.sonar.api.batch.RequiresDB; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.component.ResourcePerspectives; @@ -133,7 +132,7 @@ public class IssueTrackingDecorator implements Decorator { File sonarFile = (File) resource; InputFile file = inputPathCache.getFile(project.getEffectiveKey(), sonarFile.getPath()); if (file == null) { - throw new IllegalStateException("Resource " + resource + " was not found in InputPath cache"); + throw new IllegalStateException("File " + resource + " was not found in InputPath cache"); } sourceHashHolder = new SourceHashHolder((DefaultInputFile) file, lastLineHashes); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/LocalIssueTracking.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/LocalIssueTracking.java index f0c22e52c68..c4d9a65769f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/LocalIssueTracking.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/LocalIssueTracking.java @@ -24,7 +24,6 @@ import com.google.common.collect.Lists; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.BatchComponent; -import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.rule.ActiveRule; import org.sonar.api.batch.rule.ActiveRules; @@ -108,7 +107,7 @@ public class LocalIssueTracking implements BatchComponent { SourceHashHolder sourceHashHolder = null; if (component.isFile()) { - InputFile file = (InputFile) inputPathCache.getInputPath(component); + DefaultInputFile file = (DefaultInputFile) inputPathCache.getInputPath(component); if (file == null) { throw new IllegalStateException("Resource " + component.resource() + " was not found in InputPath cache"); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/SourceHashHolder.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/SourceHashHolder.java index 38f0af7745a..8151f4184f4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/SourceHashHolder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/SourceHashHolder.java @@ -43,7 +43,7 @@ public class SourceHashHolder { private void initHashes() { if (hashedSource == null) { - hashedSource = FileHashes.create(inputFile.lineHashes()); + hashedSource = FileHashes.create(inputFile); Status status = inputFile.status(); if (status == Status.ADDED) { hashedReference = null; diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/TaskResult.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/TaskResult.java index 7bc120b37d6..4d89b9e4b3e 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/TaskResult.java +++ b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/TaskResult.java @@ -24,7 +24,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputPath; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.sensor.dependency.Dependency; import org.sonar.api.batch.sensor.duplication.DuplicationGroup; @@ -165,12 +164,11 @@ public class TaskResult implements org.sonar.batch.mediumtest.ScanTaskObserver { private void storeFs(ProjectScanContainer container) { InputPathCache inputFileCache = container.getComponentByType(InputPathCache.class); - for (InputPath inputPath : inputFileCache.all()) { - if (inputPath instanceof InputFile) { - inputFiles.put(inputPath.relativePath(), (InputFile) inputPath); - } else { - inputDirs.put(inputPath.relativePath(), (InputDir) inputPath); - } + for (InputFile inputPath : inputFileCache.allFiles()) { + inputFiles.put(inputPath.relativePath(), inputPath); + } + for (InputDir inputPath : inputFileCache.allDirs()) { + inputDirs.put(inputPath.relativePath(), inputPath); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java index 6da48d12bb8..b555f76ded9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java @@ -19,6 +19,9 @@ */ package org.sonar.batch.phases; +import org.sonar.batch.deprecated.decorator.DefaultDecoratorContext; + +import org.sonar.batch.deprecated.decorator.DecoratorsSelector; import com.google.common.collect.Lists; import org.sonar.api.BatchComponent; import org.sonar.api.batch.Decorator; @@ -29,8 +32,6 @@ import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.api.utils.MessageException; import org.sonar.api.utils.SonarException; -import org.sonar.batch.DecoratorsSelector; -import org.sonar.batch.DefaultDecoratorContext; import org.sonar.batch.bootstrap.BatchExtensionDictionnary; import org.sonar.batch.duplication.DuplicationCache; import org.sonar.batch.events.EventBus; diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java b/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java index 6c84c6dff15..2a2e08056ad 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java @@ -24,6 +24,9 @@ import com.google.common.collect.Sets; import java.util.Arrays; import java.util.Set; +/** + * Used by views + */ public class Phases { public static enum Phase { diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/SensorMatcher.java b/sonar-batch/src/main/java/org/sonar/batch/phases/SensorMatcher.java index c50d3727184..38502235cff 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/SensorMatcher.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/SensorMatcher.java @@ -27,6 +27,7 @@ import org.sonar.batch.bootstrap.ExtensionMatcher; /** * Allow to filter sensors that will be executed. + * Used by views !! * @since 3.6 * */ diff --git a/sonar-batch/src/main/java/org/sonar/batch/languages/DefaultLanguagesReferential.java b/sonar-batch/src/main/java/org/sonar/batch/repository/language/DefaultLanguagesRepository.java index 4d43ea2d8ce..fe5f50ba9ea 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/languages/DefaultLanguagesReferential.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/language/DefaultLanguagesRepository.java @@ -17,7 +17,7 @@ * 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.languages; +package org.sonar.batch.repository.language; import org.sonar.api.resources.Languages; @@ -27,14 +27,14 @@ import java.util.ArrayList; import java.util.Collection; /** - * Languages referential using {@link Languages} + * Languages repository using {@link Languages} * @since 4.4 */ -public class DefaultLanguagesReferential implements LanguagesReferential { +public class DefaultLanguagesRepository implements LanguagesRepository { private Languages languages; - public DefaultLanguagesReferential(Languages languages) { + public DefaultLanguagesRepository(Languages languages) { this.languages = languages; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/languages/Language.java b/sonar-batch/src/main/java/org/sonar/batch/repository/language/Language.java index daeb7d50626..67629ae50ad 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/languages/Language.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/language/Language.java @@ -17,7 +17,7 @@ * 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.languages; +package org.sonar.batch.repository.language; import java.util.Arrays; import java.util.Collection; diff --git a/sonar-batch/src/main/java/org/sonar/batch/languages/LanguagesReferential.java b/sonar-batch/src/main/java/org/sonar/batch/repository/language/LanguagesRepository.java index da1596c2c10..dd80fee1dcd 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/languages/LanguagesReferential.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/language/LanguagesRepository.java @@ -17,7 +17,7 @@ * 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.languages; +package org.sonar.batch.repository.language; import org.sonar.api.BatchComponent; @@ -26,10 +26,10 @@ import javax.annotation.CheckForNull; import java.util.Collection; /** - * Languages referential + * Languages repository * @since 4.4 */ -public interface LanguagesReferential extends BatchComponent { +public interface LanguagesRepository extends BatchComponent { /** * Get language. diff --git a/sonar-batch/src/main/java/org/sonar/batch/languages/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/repository/language/package-info.java index 7a5d7a96ff6..03e334e21a0 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/languages/package-info.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/language/package-info.java @@ -18,4 +18,4 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ @javax.annotation.ParametersAreNonnullByDefault -package org.sonar.batch.languages; +package org.sonar.batch.repository.language; diff --git a/sonar-batch/src/main/java/org/sonar/batch/user/User.java b/sonar-batch/src/main/java/org/sonar/batch/repository/user/User.java index bd96d273f8c..5323e18b943 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/user/User.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/user/User.java @@ -17,7 +17,7 @@ * 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.user; +package org.sonar.batch.repository.user; import org.apache.commons.lang.builder.EqualsBuilder; diff --git a/sonar-batch/src/main/java/org/sonar/batch/user/UserRepository.java b/sonar-batch/src/main/java/org/sonar/batch/repository/user/UserRepository.java index c129bb704df..e7185b98645 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/user/UserRepository.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/user/UserRepository.java @@ -17,7 +17,7 @@ * 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.user; +package org.sonar.batch.repository.user; import com.google.common.base.Joiner; import org.sonar.batch.bootstrap.ServerClient; diff --git a/sonar-batch/src/main/java/org/sonar/batch/user/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/repository/user/package-info.java index 6415ba162e8..a86ee26f141 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/user/package-info.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/user/package-info.java @@ -18,6 +18,6 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ @ParametersAreNonnullByDefault -package org.sonar.batch.user; +package org.sonar.batch.repository.user; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java index 13ab4448d78..37455f5b58a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java @@ -19,6 +19,7 @@ */ package org.sonar.batch.rule; +import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; import org.sonar.api.batch.fs.FileSystem; @@ -33,16 +34,20 @@ public class QProfileSensor implements Sensor { private final ModuleQProfiles moduleQProfiles; private final FileSystem fs; + private final AnalysisMode analysisMode; - public QProfileSensor(ModuleQProfiles moduleQProfiles, FileSystem fs) { + public QProfileSensor(ModuleQProfiles moduleQProfiles, FileSystem fs, AnalysisMode analysisMode) { this.moduleQProfiles = moduleQProfiles; this.fs = fs; + this.analysisMode = analysisMode; } @Override public boolean shouldExecuteOnProject(Project project) { // Should be only executed on leaf modules - return project.getModules().isEmpty(); + return project.getModules().isEmpty() + // Useless in preview mode + && !analysisMode.isPreview(); } @Override diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/LanguageVerifier.java b/sonar-batch/src/main/java/org/sonar/batch/scan/LanguageVerifier.java index 801fadddd44..f813c050681 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/LanguageVerifier.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/LanguageVerifier.java @@ -19,7 +19,8 @@ */ package org.sonar.batch.scan; -import org.sonar.batch.languages.Language; +import org.sonar.batch.repository.language.Language; +import org.sonar.batch.repository.language.LanguagesRepository; import org.picocontainer.Startable; import org.slf4j.Logger; @@ -28,7 +29,6 @@ import org.sonar.api.CoreProperties; import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.config.Settings; import org.sonar.api.utils.MessageException; -import org.sonar.batch.languages.LanguagesReferential; /** * Verifies that the property sonar.language is valid @@ -38,10 +38,10 @@ public class LanguageVerifier implements Startable { private static final Logger LOG = LoggerFactory.getLogger(LanguageVerifier.class); private final Settings settings; - private final LanguagesReferential languages; + private final LanguagesRepository languages; private final DefaultFileSystem fs; - public LanguageVerifier(Settings settings, LanguagesReferential languages, DefaultFileSystem fs) { + public LanguageVerifier(Settings settings, LanguagesRepository languages, DefaultFileSystem fs) { this.settings = settings; this.languages = languages; this.fs = fs; diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java index e350fd28992..0f457c735d9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java @@ -29,11 +29,7 @@ import org.sonar.api.checks.NoSonarFilter; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.resources.Project; import org.sonar.api.scan.filesystem.FileExclusions; -import org.sonar.batch.DefaultProjectClasspath; -import org.sonar.batch.DefaultTimeMachine; -import org.sonar.batch.DeprecatedSensorContext; import org.sonar.batch.ProjectTree; -import org.sonar.batch.ResourceFilters; import org.sonar.batch.bootstrap.BatchExtensionDictionnary; import org.sonar.batch.bootstrap.DefaultAnalysisMode; import org.sonar.batch.bootstrap.ExtensionInstaller; @@ -45,6 +41,10 @@ import org.sonar.batch.debt.IssueChangelogDebtCalculator; import org.sonar.batch.debt.NewDebtDecorator; import org.sonar.batch.debt.SqaleRatingDecorator; import org.sonar.batch.debt.SqaleRatingSettings; +import org.sonar.batch.deprecated.DeprecatedSensorContext; +import org.sonar.batch.deprecated.ResourceFilters; +import org.sonar.batch.deprecated.components.DefaultProjectClasspath; +import org.sonar.batch.deprecated.components.DefaultTimeMachine; import org.sonar.batch.events.EventBus; import org.sonar.batch.index.DefaultIndex; import org.sonar.batch.issue.IssuableFactory; @@ -87,6 +87,7 @@ import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; import org.sonar.batch.scan.filesystem.DeprecatedFileFilters; import org.sonar.batch.scan.filesystem.ExclusionFilters; import org.sonar.batch.scan.filesystem.FileIndexer; +import org.sonar.batch.scan.filesystem.FileMetadata; import org.sonar.batch.scan.filesystem.FileSystemLogger; import org.sonar.batch.scan.filesystem.InputFileBuilderFactory; import org.sonar.batch.scan.filesystem.LanguageDetectionFactory; @@ -158,6 +159,7 @@ public class ModuleScanContainer extends ComponentContainer { ExclusionFilters.class, DeprecatedFileFilters.class, InputFileBuilderFactory.class, + FileMetadata.class, StatusDetectionFactory.class, LanguageDetectionFactory.class, FileIndexer.class, diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java index 30ef5615488..98c46431093 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java @@ -19,6 +19,10 @@ */ package org.sonar.batch.scan; +import org.sonar.batch.deprecated.components.DefaultResourceCreationLock; + +import org.sonar.batch.deprecated.components.PeriodsDefinition; +import org.sonar.batch.repository.language.DefaultLanguagesRepository; import com.google.common.annotations.VisibleForTesting; import org.sonar.api.BatchComponent; import org.sonar.api.CoreProperties; @@ -32,7 +36,6 @@ import org.sonar.api.resources.Project; import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.api.utils.SonarException; import org.sonar.batch.DefaultFileLinesContextFactory; -import org.sonar.batch.DefaultResourceCreationLock; import org.sonar.batch.ProjectConfigurator; import org.sonar.batch.ProjectTree; import org.sonar.batch.bootstrap.DefaultAnalysisMode; @@ -40,7 +43,6 @@ import org.sonar.batch.bootstrap.ExtensionInstaller; import org.sonar.batch.bootstrap.ExtensionMatcher; import org.sonar.batch.bootstrap.ExtensionUtils; import org.sonar.batch.bootstrap.MetricProvider; -import org.sonar.batch.components.PeriodsDefinition; import org.sonar.batch.debt.DebtModelProvider; import org.sonar.batch.debt.IssueChangelogDebtCalculator; import org.sonar.batch.duplication.BlockCache; @@ -63,7 +65,6 @@ import org.sonar.batch.issue.IssueCache; import org.sonar.batch.issue.tracking.InitialOpenIssuesStack; import org.sonar.batch.issue.tracking.LocalIssueTracking; import org.sonar.batch.issue.tracking.PreviousIssueRepository; -import org.sonar.batch.languages.DefaultLanguagesReferential; import org.sonar.batch.mediumtest.ScanTaskObservers; import org.sonar.batch.phases.GraphPersister; import org.sonar.batch.profiling.PhasesSumUpTimeProfiler; @@ -177,7 +178,7 @@ public class ProjectScanContainer extends ComponentContainer { // lang Languages.class, - DefaultLanguagesReferential.class, + DefaultLanguagesRepository.class, HighlightableBuilder.class, SymbolizableBuilder.class, diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/AdditionalFilePredicates.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/AdditionalFilePredicates.java index 4d4967ec1f3..83fbd745426 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/AdditionalFilePredicates.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/AdditionalFilePredicates.java @@ -19,7 +19,6 @@ */ package org.sonar.batch.scan.filesystem; -import org.apache.commons.io.FilenameUtils; import org.sonar.api.batch.fs.AbstractFilePredicate; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; @@ -47,42 +46,4 @@ class AdditionalFilePredicates { } } - static class DeprecatedKeyPredicate extends AbstractFilePredicate { - private final String key; - - DeprecatedKeyPredicate(String key) { - this.key = key; - } - - @Override - public boolean apply(InputFile f) { - return key.equals(((DeprecatedDefaultInputFile) f).deprecatedKey()); - } - } - - static class SourceRelativePathPredicate extends AbstractFilePredicate { - private final String path; - - SourceRelativePathPredicate(String s) { - this.path = FilenameUtils.normalize(s, true); - } - - @Override - public boolean apply(InputFile f) { - return path.equals(((DeprecatedDefaultInputFile) f).pathRelativeToSourceDir()); - } - } - - static class SourceDirPredicate extends AbstractFilePredicate { - private final String path; - - SourceDirPredicate(String s) { - this.path = FilenameUtils.normalize(s, true); - } - - @Override - public boolean apply(InputFile f) { - return path.equals(((DeprecatedDefaultInputFile) f).sourceDirAbsolutePath()); - } - } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java index 69438106d8f..460a08831b3 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java @@ -21,16 +21,13 @@ package org.sonar.batch.scan.filesystem; import org.sonar.api.BatchComponent; import org.sonar.api.batch.SonarIndex; -import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; import org.sonar.api.resources.File; import org.sonar.api.resources.Languages; import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.batch.index.ResourceKeyMigration; import org.sonar.batch.index.ResourcePersister; -import org.sonar.batch.util.DeprecatedKeyUtils; import javax.annotation.Nullable; @@ -60,7 +57,9 @@ public class ComponentIndexer implements BatchComponent { this(module, languages, sonarIndex, null, null); } - public void execute(FileSystem fs) { + public void execute(DefaultModuleFileSystem fs) { + module.setBaseDir(fs.baseDir()); + if (resourcePersister != null) { // Force persistence of module structure in order to know if project should be migrated resourcePersister.persist(); @@ -73,16 +72,7 @@ public class ComponentIndexer implements BatchComponent { for (InputFile inputFile : fs.inputFiles(fs.predicates().all())) { String languageKey = inputFile.language(); boolean unitTest = InputFile.Type.TEST == inputFile.type(); - String pathFromSourceDir = ((DeprecatedDefaultInputFile) inputFile).pathRelativeToSourceDir(); - if (pathFromSourceDir == null) { - pathFromSourceDir = inputFile.relativePath(); - } - Resource sonarFile = File.create(inputFile.relativePath(), pathFromSourceDir, languages.get(languageKey), unitTest); - if ("java".equals(languageKey)) { - sonarFile.setDeprecatedKey(DeprecatedKeyUtils.getJavaFileDeprecatedKey(pathFromSourceDir)); - } else { - sonarFile.setDeprecatedKey(pathFromSourceDir); - } + Resource sonarFile = File.create(inputFile.relativePath(), languages.get(languageKey), unitTest); sonarIndex.index(sonarFile); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultInputFileValueCoder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultInputFileValueCoder.java deleted file mode 100644 index 31e0880a8fe..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultInputFileValueCoder.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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.scan.filesystem; - -import com.persistit.Value; -import com.persistit.encoding.CoderContext; -import com.persistit.encoding.ValueCoder; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; - -import javax.annotation.Nullable; - -import java.io.File; - -class DefaultInputFileValueCoder implements ValueCoder { - - @Override - public void put(Value value, Object object, CoderContext context) { - DeprecatedDefaultInputFile f = (DeprecatedDefaultInputFile) object; - putUTFOrNull(value, f.moduleKey()); - putUTFOrNull(value, f.relativePath()); - value.putString(f.getFileBaseDir().toString()); - putUTFOrNull(value, f.deprecatedKey()); - value.putString(f.sourceDirAbsolutePath()); - putUTFOrNull(value, f.pathRelativeToSourceDir()); - putUTFOrNull(value, f.absolutePath()); - value.putString(f.language()); - value.putString(f.type().name()); - value.putString(f.status().name()); - putUTFOrNull(value, f.hash()); - value.put(f.lines()); - value.put(f.nonBlankLines()); - putUTFOrNull(value, f.encoding()); - value.put(f.isEmpty()); - value.putLongArray(f.originalLineOffsets()); - for (int i = 0; i < f.lines(); i++) { - value.putByteArray(f.lineHashes()[i]); - } - } - - private void putUTFOrNull(Value value, @Nullable String utfOrNull) { - if (utfOrNull != null) { - value.putUTF(utfOrNull); - } else { - value.putNull(); - } - } - - @Override - public Object get(Value value, Class clazz, CoderContext context) { - String moduleKey = value.getString(); - DeprecatedDefaultInputFile file = new DeprecatedDefaultInputFile(moduleKey, value.getString()); - file.setBasedir(new File(value.getString())); - file.setDeprecatedKey(value.getString()); - file.setSourceDirAbsolutePath(value.getString()); - file.setPathRelativeToSourceDir(value.getString()); - file.setAbsolutePath(value.getString()); - file.setLanguage(value.getString()); - file.setType(InputFile.Type.valueOf(value.getString())); - file.setStatus(InputFile.Status.valueOf(value.getString())); - file.setHash(value.getString()); - file.setLines(value.getInt()); - file.setNonBlankLines(value.getInt()); - file.setEncoding(value.getString()); - file.setEmpty(value.getBoolean()); - file.setOriginalLineOffsets(value.getLongArray()); - byte[][] lineHashes = new byte[file.lines()][]; - for (int i = 0; i < file.lines(); i++) { - lineHashes[i] = value.getByteArray(); - } - file.setLineHashes(lineHashes); - return file; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java index 43006d48ef6..5f16313e853 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java @@ -19,13 +19,13 @@ */ package org.sonar.batch.scan.filesystem; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; -import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.FilePredicate; import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.config.Settings; @@ -45,8 +45,6 @@ import java.util.List; import java.util.Map; /** - * This class can't be immutable because of execution of maven plugins that can change the project structure (see MavenPluginHandler and sonar.phase) - * * @since 3.5 */ public class DefaultModuleFileSystem extends DefaultFileSystem implements ModuleFileSystem { @@ -62,27 +60,26 @@ public class DefaultModuleFileSystem extends DefaultFileSystem implements Module private ComponentIndexer componentIndexer; private boolean initialized; - /** - * Used by scan2 - */ - public DefaultModuleFileSystem(ModuleInputFileCache moduleInputFileCache, ProjectDefinition def, Settings settings, - FileIndexer indexer, ModuleFileSystemInitializer initializer) { - this(moduleInputFileCache, def.getKey(), settings, indexer, initializer, null); - } - public DefaultModuleFileSystem(ModuleInputFileCache moduleInputFileCache, Project project, - Settings settings, FileIndexer indexer, - ModuleFileSystemInitializer initializer, - ComponentIndexer componentIndexer) { - this(moduleInputFileCache, project.getKey(), settings, indexer, initializer, componentIndexer); + Settings settings, FileIndexer indexer, ModuleFileSystemInitializer initializer, ComponentIndexer componentIndexer) { + super(initializer.baseDir().toPath(), moduleInputFileCache); + this.componentIndexer = componentIndexer; + this.moduleKey = project.getKey(); + this.settings = settings; + this.indexer = indexer; + setWorkDir(initializer.workingDir()); + this.buildDir = initializer.buildDir(); + this.sourceDirsOrFiles = initializer.sources(); + this.testDirsOrFiles = initializer.tests(); + this.binaryDirs = initializer.binaryDirs(); } - private DefaultModuleFileSystem(ModuleInputFileCache moduleInputFileCache, String moduleKey, Settings settings, - FileIndexer indexer, ModuleFileSystemInitializer initializer, - @Nullable ComponentIndexer componentIndexer) { - super(initializer.baseDir(), moduleInputFileCache); + @VisibleForTesting + public DefaultModuleFileSystem(Project project, + Settings settings, FileIndexer indexer, ModuleFileSystemInitializer initializer, ComponentIndexer componentIndexer) { + super(initializer.baseDir().toPath()); this.componentIndexer = componentIndexer; - this.moduleKey = moduleKey; + this.moduleKey = project.getKey(); this.settings = settings; this.indexer = indexer; setWorkDir(initializer.workingDir()); @@ -260,30 +257,6 @@ public class DefaultModuleFileSystem extends DefaultFileSystem implements Module } })); } - if ("CMP_DEPRECATED_KEY".equals(key)) { - return predicates().or(Collections2.transform(value, new Function<String, FilePredicate>() { - @Override - public FilePredicate apply(@Nullable String s) { - return s == null ? predicates().all() : new AdditionalFilePredicates.DeprecatedKeyPredicate(s); - } - })); - } - if ("SRC_REL_PATH".equals(key)) { - return predicates().or(Collections2.transform(value, new Function<String, FilePredicate>() { - @Override - public FilePredicate apply(@Nullable String s) { - return s == null ? predicates().all() : new AdditionalFilePredicates.SourceRelativePathPredicate(s); - } - })); - } - if ("SRC_DIR_PATH".equals(key)) { - return predicates().or(Collections2.transform(value, new Function<String, FilePredicate>() { - @Override - public FilePredicate apply(@Nullable String s) { - return s == null ? predicates().all() : new AdditionalFilePredicates.SourceDirPredicate(s); - } - })); - } throw new IllegalArgumentException("Unsupported file attribute: " + key); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileFilters.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileFilters.java index d52e19b0b0b..9fa6ea98a23 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileFilters.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileFilters.java @@ -21,13 +21,10 @@ package org.sonar.batch.scan.filesystem; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFileFilter; -import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; import org.sonar.api.scan.filesystem.FileSystemFilter; import org.sonar.api.scan.filesystem.FileType; import org.sonar.api.scan.filesystem.ModuleFileSystem; -import java.io.File; - public class DeprecatedFileFilters implements InputFileFilter { private final FileSystemFilter[] filters; @@ -71,13 +68,8 @@ public class DeprecatedFileFilters implements InputFileFilter { } @Override - public File relativeDir() { - return new File(((DeprecatedDefaultInputFile)inputFile).sourceDirAbsolutePath()); - } - - @Override public String relativePath() { - return ((DeprecatedDefaultInputFile)inputFile).pathRelativeToSourceDir(); + return inputFile.relativePath(); } @Override diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java index e78ae260ca3..d4626209aa2 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java @@ -23,20 +23,18 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.commons.io.filefilter.HiddenFileFilter; import org.apache.commons.io.filefilter.IOFileFilter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.sonar.api.BatchComponent; import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFileFilter; import org.sonar.api.batch.fs.internal.DefaultInputDir; -import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.api.utils.MessageException; +import org.sonar.batch.util.ProgressReport; import java.io.File; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -47,14 +45,13 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; /** * Index input files into {@link InputPathCache}. */ public class FileIndexer implements BatchComponent { - private static final Logger LOG = LoggerFactory.getLogger(FileIndexer.class); - private static final IOFileFilter DIR_FILTER = FileFilterUtils.and(HiddenFileFilter.VISIBLE, FileFilterUtils.notFileFilter(FileFilterUtils.prefixFileFilter("."))); private static final IOFileFilter FILE_FILTER = HiddenFileFilter.VISIBLE; @@ -62,18 +59,19 @@ public class FileIndexer implements BatchComponent { private final boolean isAggregator; private final ExclusionFilters exclusionFilters; private final InputFileBuilderFactory inputFileBuilderFactory; + private final InputPathCache inputPathCache; - public FileIndexer(List<InputFileFilter> filters, ExclusionFilters exclusionFilters, InputFileBuilderFactory inputFileBuilderFactory, - ProjectDefinition def) { - this(filters, exclusionFilters, inputFileBuilderFactory, !def.getSubProjects().isEmpty()); - } + private ProgressReport progressReport; + private ExecutorService executorService; + private List<Future<Void>> tasks; - private FileIndexer(List<InputFileFilter> filters, ExclusionFilters exclusionFilters, InputFileBuilderFactory inputFileBuilderFactory, - boolean isAggregator) { + public FileIndexer(List<InputFileFilter> filters, ExclusionFilters exclusionFilters, InputFileBuilderFactory inputFileBuilderFactory, + ProjectDefinition def, InputPathCache inputPathCache) { + this.inputPathCache = inputPathCache; this.filters = filters; this.exclusionFilters = exclusionFilters; this.inputFileBuilderFactory = inputFileBuilderFactory; - this.isAggregator = isAggregator; + this.isAggregator = !def.getSubProjects().isEmpty(); } void index(DefaultModuleFileSystem fileSystem) { @@ -81,47 +79,34 @@ public class FileIndexer implements BatchComponent { // No indexing for an aggregator module return; } - LOG.info("Index files"); + progressReport = new ProgressReport("Report about progress of file indexation", TimeUnit.SECONDS.toMillis(10)); + progressReport.start("Index files"); exclusionFilters.prepare(); Progress progress = new Progress(); InputFileBuilder inputFileBuilder = inputFileBuilderFactory.create(fileSystem); + executorService = Executors.newFixedThreadPool(Math.max(1, Runtime.getRuntime().availableProcessors() - 1)); + tasks = new ArrayList<Future<Void>>(); indexFiles(fileSystem, progress, inputFileBuilder, fileSystem.sources(), InputFile.Type.MAIN); indexFiles(fileSystem, progress, inputFileBuilder, fileSystem.tests(), InputFile.Type.TEST); - indexAllConcurrently(progress); - - // Populate FS in a synchronous way because PersistIt Exchange is not concurrent - for (InputFile indexed : progress.indexed) { - fileSystem.add(indexed); - } - for (InputDir indexed : progress.indexedDir) { - fileSystem.add(indexed); - } - - LOG.info(String.format("%d files indexed", progress.count())); + waitForTasksToComplete(); + progressReport.stop(progress.count() + " files indexed"); } - private void indexAllConcurrently(Progress progress) { - ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1); - try { - List<Future<Void>> all = executor.invokeAll(progress.indexingTasks); - for (Future<Void> future : all) { - future.get(); - } - } catch (InterruptedException e) { - throw new IllegalStateException("FileIndexer was interrupted", e); - } catch (ExecutionException e) { - Throwable cause = e.getCause(); - if (cause instanceof RuntimeException) { - throw (RuntimeException) cause; - } else { - throw new IllegalStateException("Error during file indexing", e); + private void waitForTasksToComplete() { + for (Future<Void> task : tasks) { + try { + task.get(); + } catch (ExecutionException e) { + // Unwrap ExecutionException + throw e.getCause() instanceof RuntimeException ? (RuntimeException) e.getCause() : new IllegalStateException(e.getCause()); + } catch (InterruptedException e) { + throw new IllegalStateException(e); } } - executor.shutdown(); } private void indexFiles(DefaultModuleFileSystem fileSystem, Progress progress, InputFileBuilder inputFileBuilder, List<File> sources, InputFile.Type type) { @@ -151,25 +136,25 @@ public class FileIndexer implements BatchComponent { private void indexFile(final InputFileBuilder inputFileBuilder, final DefaultModuleFileSystem fs, final Progress status, final DeprecatedDefaultInputFile inputFile, final InputFile.Type type) { - Callable<Void> task = new Callable<Void>() { - + tasks.add(executorService.submit(new Callable<Void>() { @Override - public Void call() throws Exception { - DefaultInputFile completedFile = inputFileBuilder.complete(inputFile, type); - if (completedFile != null && accept(completedFile)) { + public Void call() { + InputFileMetadata metadata = inputFileBuilder.completeAndComputeMetadata(inputFile, type); + if (metadata != null && accept(inputFile)) { + fs.add(inputFile); status.markAsIndexed(inputFile); + inputPathCache.put(inputFile.moduleKey(), inputFile.relativePath(), metadata); File parentDir = inputFile.file().getParentFile(); String relativePath = new PathResolver().relativePath(fs.baseDir(), parentDir); if (relativePath != null) { DefaultInputDir inputDir = new DefaultInputDir(fs.moduleKey(), relativePath); - inputDir.setFile(parentDir); - status.markAsIndexed(inputDir); + fs.add(inputDir); } } return null; } - }; - status.planForIndexing(task); + })); + } private boolean accept(InputFile inputFile) { @@ -182,31 +167,16 @@ public class FileIndexer implements BatchComponent { return true; } - private static class Progress { - private final Set<InputFile> indexed; - private final Set<InputDir> indexedDir; - private final List<Callable<Void>> indexingTasks; - - Progress() { - this.indexed = new HashSet<InputFile>(); - this.indexedDir = new HashSet<InputDir>(); - this.indexingTasks = new ArrayList<Callable<Void>>(); - } - - void planForIndexing(Callable<Void> indexingTask) { - this.indexingTasks.add(indexingTask); - } + private class Progress { + private final Set<Path> indexed = new HashSet<>(); synchronized void markAsIndexed(InputFile inputFile) { - if (indexed.contains(inputFile)) { + if (indexed.contains(inputFile.path())) { throw MessageException.of("File " + inputFile + " can't be indexed twice. Please check that inclusion/exclusion patterns produce " + "disjoint sets for main and test files"); } - indexed.add(inputFile); - } - - synchronized void markAsIndexed(InputDir inputDir) { - indexedDir.add(inputDir); + indexed.add(inputFile.path()); + progressReport.message(indexed.size() + " files indexed... (last one was " + inputFile.relativePath() + ")"); } int count() { diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileMetadata.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileMetadata.java index b3b4d1469fd..0f91e4ae3cc 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileMetadata.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileMetadata.java @@ -20,17 +20,22 @@ package org.sonar.batch.scan.filesystem; import com.google.common.base.Charsets; -import com.google.common.primitives.Longs; +import com.google.common.primitives.Ints; import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.ByteOrderMark; import org.apache.commons.io.input.BOMInputStream; +import org.sonar.api.BatchComponent; +import org.sonar.api.batch.AnalysisMode; import javax.annotation.CheckForNull; -import java.io.*; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; import java.nio.charset.Charset; import java.security.MessageDigest; import java.util.ArrayList; @@ -40,10 +45,15 @@ import java.util.List; * Computes hash of files. Ends of Lines are ignored, so files with * same content but different EOL encoding have the same hash. */ -class FileMetadata { +public class FileMetadata implements BatchComponent { private static final char LINE_FEED = '\n'; private static final char CARRIAGE_RETURN = '\r'; + private final AnalysisMode analysisMode; + + public FileMetadata(AnalysisMode analysisMode) { + this.analysisMode = analysisMode; + } private abstract class CharHandler { @@ -110,14 +120,25 @@ class FileMetadata { private class FileHashComputer extends CharHandler { private MessageDigest globalMd5Digest = DigestUtils.getMd5Digest(); + StringBuffer sb = new StringBuffer(); + @Override void handleIgnoreEoL(char c) { - updateDigestUTF8Char(c, globalMd5Digest); + sb.append(c); } @Override void newLine() { - updateDigestUTF8Char(LINE_FEED, globalMd5Digest); + sb.append(LINE_FEED); + globalMd5Digest.update(sb.toString().getBytes(Charsets.UTF_8)); + sb.setLength(0); + } + + @Override + void eof() { + if (sb.length() > 0) { + globalMd5Digest.update(sb.toString().getBytes(Charsets.UTF_8)); + } } @CheckForNull @@ -127,11 +148,11 @@ class FileMetadata { } private class LineOffsetCounter extends CharHandler { - private long currentOriginalOffset = 0; - private List<Long> originalLineOffsets = new ArrayList<Long>(); + private int currentOriginalOffset = 0; + private List<Integer> originalLineOffsets = new ArrayList<Integer>(); public LineOffsetCounter() { - originalLineOffsets.add(0L); + originalLineOffsets.add(0); } @Override @@ -144,41 +165,12 @@ class FileMetadata { originalLineOffsets.add(currentOriginalOffset); } - public List<Long> getOriginalLineOffsets() { + public List<Integer> getOriginalLineOffsets() { return originalLineOffsets; } } - private class LineHashesComputer extends CharHandler { - private List<Object> lineHashes = new ArrayList<Object>(); - private MessageDigest lineMd5Digest = DigestUtils.getMd5Digest(); - private boolean blankLine = true; - - @Override - void handleIgnoreEoL(char c) { - if (!Character.isWhitespace(c)) { - blankLine = false; - updateDigestUTF8Char(c, lineMd5Digest); - } - } - - @Override - void newLine() { - lineHashes.add(blankLine ? null : lineMd5Digest.digest()); - blankLine = true; - } - - @Override - void eof() { - lineHashes.add(blankLine ? null : lineMd5Digest.digest()); - } - - public byte[][] lineHashes() { - return lineHashes.toArray(new byte[0][]); - } - } - /** * Compute hash of a file ignoring line ends differences. * Maximum performance is needed. @@ -188,8 +180,13 @@ class FileMetadata { LineCounter lineCounter = new LineCounter(); FileHashComputer fileHashComputer = new FileHashComputer(); LineOffsetCounter lineOffsetCounter = new LineOffsetCounter(); - LineHashesComputer lineHashesComputer = new LineHashesComputer(); - CharHandler[] handlers = new CharHandler[] {lineCounter, fileHashComputer, lineOffsetCounter, lineHashesComputer}; + CharHandler[] handlers; + if (analysisMode.isPreview()) { + // No need to compute line offsets in preview mode since there is no syntax highlighting + handlers = new CharHandler[] {lineCounter, fileHashComputer}; + } else { + handlers = new CharHandler[] {lineCounter, fileHashComputer, lineOffsetCounter}; + } try (BOMInputStream bomIn = new BOMInputStream(new FileInputStream(file), ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE); Reader reader = new BufferedReader(new InputStreamReader(bomIn, encoding))) { @@ -228,41 +225,26 @@ class FileMetadata { handler.eof(); } return new Metadata(lineCounter.lines(), lineCounter.nonBlankLines(), fileHashComputer.getHash(), lineOffsetCounter.getOriginalLineOffsets(), - lineHashesComputer.lineHashes(), lineCounter.isEmpty()); + lineCounter.isEmpty()); } catch (IOException e) { throw new IllegalStateException(String.format("Fail to read file '%s' with encoding '%s'", file.getAbsolutePath(), encoding), e); } } - private void updateDigestUTF8Char(char c, MessageDigest md5Digest) { - CharBuffer cb = CharBuffer.allocate(1); - cb.put(c); - cb.flip(); - ByteBuffer bb = Charsets.UTF_8.encode(cb); - byte[] array = bb.array(); - for (int i = 0; i < array.length; i++) { - if (array[i] != 0) { - md5Digest.update(array[i]); - } - } - } - static class Metadata { final int lines; final int nonBlankLines; final String hash; - final long[] originalLineOffsets; - final byte[][] lineHashes; + final int[] originalLineOffsets; final boolean empty; - private Metadata(int lines, int nonBlankLines, String hash, List<Long> originalLineOffsets, byte[][] lineHashes, boolean empty) { + private Metadata(int lines, int nonBlankLines, String hash, List<Integer> originalLineOffsets, boolean empty) { this.lines = lines; this.nonBlankLines = nonBlankLines; this.hash = hash; this.empty = empty; - this.originalLineOffsets = Longs.toArray(originalLineOffsets); - this.lineHashes = lineHashes; + this.originalLineOffsets = Ints.toArray(originalLineOffsets); } } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilder.java index 748410fea36..56b8319ac44 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilder.java @@ -19,7 +19,6 @@ */ package org.sonar.batch.scan.filesystem; -import org.apache.commons.io.FilenameUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; @@ -29,12 +28,10 @@ import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; import org.sonar.api.config.Settings; import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.batch.bootstrap.DefaultAnalysisMode; -import org.sonar.batch.util.DeprecatedKeyUtils; import javax.annotation.CheckForNull; import java.io.File; -import java.util.List; class InputFileBuilder { @@ -47,9 +44,10 @@ class InputFileBuilder { private final DefaultModuleFileSystem fs; private final DefaultAnalysisMode analysisMode; private final Settings settings; + private final FileMetadata fileMetadata; InputFileBuilder(String moduleKey, PathResolver pathResolver, LanguageDetection langDetection, - StatusDetection statusDetection, DefaultModuleFileSystem fs, DefaultAnalysisMode analysisMode, Settings settings) { + StatusDetection statusDetection, DefaultModuleFileSystem fs, DefaultAnalysisMode analysisMode, Settings settings, FileMetadata fileMetadata) { this.moduleKey = moduleKey; this.pathResolver = pathResolver; this.langDetection = langDetection; @@ -57,6 +55,7 @@ class InputFileBuilder { this.fs = fs; this.analysisMode = analysisMode; this.settings = settings; + this.fileMetadata = fileMetadata; } String moduleKey() { @@ -86,20 +85,17 @@ class InputFileBuilder { LOG.warn("File '{}' is ignored. It is not located in module basedir '{}'.", file.getAbsolutePath(), fs.baseDir()); return null; } - DeprecatedDefaultInputFile inputFile = new DeprecatedDefaultInputFile(moduleKey, relativePath); - inputFile.setBasedir(fs.baseDir()); - inputFile.setFile(file); - return inputFile; + return new DeprecatedDefaultInputFile(moduleKey, relativePath); } /** - * Optimization to not set all InputFile data if the file is excluded from analysis. + * Optimization to not compute InputFile metadata if the file is excluded from analysis. */ @CheckForNull - DeprecatedDefaultInputFile complete(DeprecatedDefaultInputFile inputFile, InputFile.Type type) { + InputFileMetadata completeAndComputeMetadata(DeprecatedDefaultInputFile inputFile, InputFile.Type type) { inputFile.setType(type); - inputFile.setBasedir(fs.baseDir()); - inputFile.setEncoding(fs.encoding().name()); + inputFile.setModuleBaseDir(fs.baseDir().toPath()); + inputFile.setCharset(fs.encoding()); String lang = langDetection.language(inputFile); if (lang == null && !settings.getBoolean(CoreProperties.IMPORT_UNKNOWN_FILES_KEY)) { @@ -107,36 +103,21 @@ class InputFileBuilder { } inputFile.setLanguage(lang); - FileMetadata.Metadata metadata = new FileMetadata().read(inputFile.file(), fs.encoding()); + InputFileMetadata result = new InputFileMetadata(); + + FileMetadata.Metadata metadata = fileMetadata.read(inputFile.file(), fs.encoding()); inputFile.setLines(metadata.lines); - inputFile.setNonBlankLines(metadata.nonBlankLines); - inputFile.setHash(metadata.hash); - inputFile.setOriginalLineOffsets(metadata.originalLineOffsets); - inputFile.setLineHashes(metadata.lineHashes); - inputFile.setEmpty(metadata.empty); + + result.setNonBlankLines(metadata.nonBlankLines); + result.setHash(metadata.hash); + result.setOriginalLineOffsets(metadata.originalLineOffsets); + result.setEmpty(metadata.empty); + inputFile.setStatus(statusDetection.status(inputFile.moduleKey(), inputFile.relativePath(), metadata.hash)); if (analysisMode.isIncremental() && inputFile.status() == InputFile.Status.SAME) { return null; } - fillDeprecatedData(inputFile); - return inputFile; + return result; } - private void fillDeprecatedData(DeprecatedDefaultInputFile inputFile) { - List<File> sourceDirs = InputFile.Type.MAIN == inputFile.type() ? fs.sourceDirs() : fs.testDirs(); - for (File sourceDir : sourceDirs) { - String sourceRelativePath = pathResolver.relativePath(sourceDir, inputFile.file()); - if (sourceRelativePath != null) { - inputFile.setPathRelativeToSourceDir(sourceRelativePath); - inputFile.setSourceDirAbsolutePath(FilenameUtils.normalize(sourceDir.getAbsolutePath(), true)); - - if ("java".equals(inputFile.language())) { - inputFile.setDeprecatedKey(new StringBuilder() - .append(moduleKey).append(":").append(DeprecatedKeyUtils.getJavaFileDeprecatedKey(sourceRelativePath)).toString()); - } else { - inputFile.setDeprecatedKey(new StringBuilder().append(moduleKey).append(":").append(sourceRelativePath).toString()); - } - } - } - } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactory.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactory.java index c3dde447e01..c173975fc53 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactory.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactory.java @@ -33,15 +33,12 @@ public class InputFileBuilderFactory implements BatchComponent { private final StatusDetectionFactory statusDetectionFactory; private final DefaultAnalysisMode analysisMode; private final Settings settings; + private final FileMetadata fileMetadata; public InputFileBuilderFactory(ProjectDefinition def, PathResolver pathResolver, LanguageDetectionFactory langDetectionFactory, - StatusDetectionFactory statusDetectionFactory, DefaultAnalysisMode analysisMode, Settings settings) { - this(def.getKeyWithBranch(), pathResolver, langDetectionFactory, statusDetectionFactory, analysisMode, settings); - } - - private InputFileBuilderFactory(String effectiveKey, PathResolver pathResolver, LanguageDetectionFactory langDetectionFactory, - StatusDetectionFactory statusDetectionFactory, DefaultAnalysisMode analysisMode, Settings settings) { - this.moduleKey = effectiveKey; + StatusDetectionFactory statusDetectionFactory, DefaultAnalysisMode analysisMode, Settings settings, FileMetadata fileMetadata) { + this.fileMetadata = fileMetadata; + this.moduleKey = def.getKeyWithBranch(); this.pathResolver = pathResolver; this.langDetectionFactory = langDetectionFactory; this.statusDetectionFactory = statusDetectionFactory; @@ -50,6 +47,6 @@ public class InputFileBuilderFactory implements BatchComponent { } InputFileBuilder create(DefaultModuleFileSystem fs) { - return new InputFileBuilder(moduleKey, pathResolver, langDetectionFactory.create(), statusDetectionFactory.create(), fs, analysisMode, settings); + return new InputFileBuilder(moduleKey, pathResolver, langDetectionFactory.create(), statusDetectionFactory.create(), fs, analysisMode, settings, fileMetadata); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileMetadata.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileMetadata.java new file mode 100644 index 00000000000..f37c672d101 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileMetadata.java @@ -0,0 +1,73 @@ +/* + * 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.scan.filesystem; + +import java.io.Serializable; + +/** + * Additional input file metadata that are stored in a disk storage to save memory + */ +public class InputFileMetadata implements Serializable { + + private String hash; + private int nonBlankLines; + private int[] originalLineOffsets; + private boolean empty; + + /** + * Digest hash of the file. + */ + public String hash() { + return hash; + } + + public int nonBlankLines() { + return nonBlankLines; + } + + public int[] originalLineOffsets() { + return originalLineOffsets; + } + + public InputFileMetadata setHash(String hash) { + this.hash = hash; + return this; + } + + public InputFileMetadata setNonBlankLines(int nonBlankLines) { + this.nonBlankLines = nonBlankLines; + return this; + } + + public InputFileMetadata setOriginalLineOffsets(int[] originalLineOffsets) { + this.originalLineOffsets = originalLineOffsets; + return this; + } + + public boolean isEmpty() { + return this.empty; + } + + public InputFileMetadata setEmpty(boolean empty) { + this.empty = empty; + return this; + } + +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputPathCache.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputPathCache.java index adc1a16150c..6270eda9cf2 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputPathCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputPathCache.java @@ -19,82 +19,133 @@ */ package org.sonar.batch.scan.filesystem; +import com.google.common.base.Function; +import com.google.common.collect.Iterables; import org.sonar.api.BatchComponent; import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputPath; -import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; import org.sonar.batch.index.BatchResource; -import org.sonar.batch.index.Cache; -import org.sonar.batch.index.Caches; import javax.annotation.CheckForNull; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + /** - * Cache of all files. This cache is shared amongst all project modules. Inclusion and + * Cache of all files and dirs. This cache is shared amongst all project modules. Inclusion and * exclusion patterns are already applied. */ public class InputPathCache implements BatchComponent { - private static final String DIR = "DIR"; - private static final String FILE = "FILE"; - // [module key | type | path] -> InputPath - // For example: - // [struts-core | FILE | src/main/java/Action.java] -> InputFile - // [struts-core | FILE | src/main/java/Filter.java] -> InputFile - // [struts-core | DIR | src/main/java] -> InputDir - private final Cache<InputPath> cache; - - public InputPathCache(Caches caches) { - caches.registerValueCoder(DeprecatedDefaultInputFile.class, new DefaultInputFileValueCoder()); - cache = caches.createCache("inputFiles"); + private final Map<String, Map<String, InputFile>> inputFileCache = new HashMap<>(); + private final Map<String, Map<String, InputDir>> inputDirCache = new HashMap<>(); + private final Map<String, Map<String, InputFileMetadata>> inputFileMetadataCache = new HashMap<>(); + + public Iterable<InputFile> allFiles() { + return Iterables.concat(Iterables.transform(inputFileCache.values(), new Function<Map<String, InputFile>, Collection<InputFile>>() { + @Override + public Collection<InputFile> apply(Map<String, InputFile> input) { + return input.values(); + } + })); } - public Iterable<InputPath> all() { - return cache.values(); + public Iterable<InputDir> allDirs() { + return Iterables.concat(Iterables.transform(inputDirCache.values(), new Function<Map<String, InputDir>, Collection<InputDir>>() { + @Override + public Collection<InputDir> apply(Map<String, InputDir> input) { + return input.values(); + } + })); } public Iterable<InputFile> filesByModule(String moduleKey) { - return (Iterable) cache.values(moduleKey, FILE); + if (inputFileCache.containsKey(moduleKey)) { + return inputFileCache.get(moduleKey).values(); + } + return Collections.emptyList(); } public Iterable<InputDir> dirsByModule(String moduleKey) { - return (Iterable) cache.values(moduleKey, DIR); + if (inputDirCache.containsKey(moduleKey)) { + return inputDirCache.get(moduleKey).values(); + } + return Collections.emptyList(); } public InputPathCache removeModule(String moduleKey) { - cache.clear(moduleKey); + inputFileCache.remove(moduleKey); + inputDirCache.remove(moduleKey); + inputFileMetadataCache.remove(moduleKey); return this; } public InputPathCache remove(String moduleKey, InputFile inputFile) { - cache.remove(moduleKey, FILE, inputFile.relativePath()); + if (inputFileCache.containsKey(moduleKey)) { + inputFileCache.get(moduleKey).remove(inputFile.relativePath()); + } + if (inputFileMetadataCache.containsKey(moduleKey)) { + inputFileMetadataCache.get(moduleKey).remove(inputFile.relativePath()); + } return this; } public InputPathCache remove(String moduleKey, InputDir inputDir) { - cache.remove(moduleKey, DIR, inputDir.relativePath()); + if (inputDirCache.containsKey(moduleKey)) { + inputDirCache.get(moduleKey).remove(inputDir.relativePath()); + } return this; } public InputPathCache put(String moduleKey, InputFile inputFile) { - cache.put(moduleKey, FILE, inputFile.relativePath(), inputFile); + if (!inputFileCache.containsKey(moduleKey)) { + inputFileCache.put(moduleKey, new HashMap<String, InputFile>()); + } + inputFileCache.get(moduleKey).put(inputFile.relativePath(), inputFile); + return this; + } + + public synchronized InputPathCache put(String moduleKey, String relativePath, InputFileMetadata metadata) { + if (!inputFileMetadataCache.containsKey(moduleKey)) { + inputFileMetadataCache.put(moduleKey, new HashMap<String, InputFileMetadata>()); + } + inputFileMetadataCache.get(moduleKey).put(relativePath, metadata); return this; } public InputPathCache put(String moduleKey, InputDir inputDir) { - cache.put(moduleKey, DIR, inputDir.relativePath(), inputDir); + if (!inputDirCache.containsKey(moduleKey)) { + inputDirCache.put(moduleKey, new HashMap<String, InputDir>()); + } + inputDirCache.get(moduleKey).put(inputDir.relativePath(), inputDir); return this; } @CheckForNull public InputFile getFile(String moduleKey, String relativePath) { - return (InputFile) cache.get(moduleKey, FILE, relativePath); + if (inputFileCache.containsKey(moduleKey)) { + return inputFileCache.get(moduleKey).get(relativePath); + } + return null; + } + + @CheckForNull + public InputFileMetadata getFileMetadata(String moduleKey, String relativePath) { + if (inputFileMetadataCache.containsKey(moduleKey)) { + return inputFileMetadataCache.get(moduleKey).get(relativePath); + } + return null; } @CheckForNull public InputDir getDir(String moduleKey, String relativePath) { - return (InputDir) cache.get(moduleKey, DIR, relativePath); + if (inputDirCache.containsKey(moduleKey)) { + return inputDirCache.get(moduleKey).get(relativePath); + } + return null; } @CheckForNull diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetection.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetection.java index 1ee5379f579..131ac8a4ca4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetection.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetection.java @@ -19,7 +19,8 @@ */ package org.sonar.batch.scan.filesystem; -import org.sonar.batch.languages.Language; +import org.sonar.batch.repository.language.Language; +import org.sonar.batch.repository.language.LanguagesRepository; import com.google.common.base.Joiner; import com.google.common.collect.Lists; @@ -32,7 +33,6 @@ import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.PathPattern; import org.sonar.api.config.Settings; import org.sonar.api.utils.MessageException; -import org.sonar.batch.languages.LanguagesReferential; import javax.annotation.CheckForNull; @@ -54,7 +54,7 @@ class LanguageDetection { private final List<String> languagesToConsider = Lists.newArrayList(); private final String forcedLanguage; - LanguageDetection(Settings settings, LanguagesReferential languages) { + LanguageDetection(Settings settings, LanguagesRepository languages) { for (Language language : languages.all()) { String[] filePatterns = settings.getStringArray(getFileLangPatternPropKey(language.key())); PathPattern[] pathPatterns = PathPattern.create(filePatterns); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactory.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactory.java index 6ff579fd769..c52118612c3 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactory.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactory.java @@ -19,15 +19,16 @@ */ package org.sonar.batch.scan.filesystem; +import org.sonar.batch.repository.language.LanguagesRepository; + import org.sonar.api.BatchComponent; import org.sonar.api.config.Settings; -import org.sonar.batch.languages.LanguagesReferential; public class LanguageDetectionFactory implements BatchComponent { private final Settings settings; - private final LanguagesReferential languages; + private final LanguagesRepository languages; - public LanguageDetectionFactory(Settings settings, LanguagesReferential languages) { + public LanguageDetectionFactory(Settings settings, LanguagesRepository languages) { this.settings = settings; this.languages = languages; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleInputFileCache.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleInputFileCache.java index b932295dc40..17420fc2dc8 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleInputFileCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleInputFileCache.java @@ -28,35 +28,35 @@ import org.sonar.api.batch.fs.internal.DefaultFileSystem; public class ModuleInputFileCache extends DefaultFileSystem.Cache implements BatchComponent { private final String moduleKey; - private final InputPathCache projectCache; + private final InputPathCache inputPathCache; public ModuleInputFileCache(ProjectDefinition projectDef, InputPathCache projectCache) { this.moduleKey = projectDef.getKeyWithBranch(); - this.projectCache = projectCache; + this.inputPathCache = projectCache; } @Override public Iterable<InputFile> inputFiles() { - return projectCache.filesByModule(moduleKey); + return inputPathCache.filesByModule(moduleKey); } @Override public InputFile inputFile(String relativePath) { - return projectCache.getFile(moduleKey, relativePath); + return inputPathCache.getFile(moduleKey, relativePath); } @Override public InputDir inputDir(String relativePath) { - return projectCache.getDir(moduleKey, relativePath); + return inputPathCache.getDir(moduleKey, relativePath); } @Override protected void doAdd(InputFile inputFile) { - projectCache.put(moduleKey, inputFile); + inputPathCache.put(moduleKey, inputFile); } @Override protected void doAdd(InputDir inputDir) { - projectCache.put(moduleKey, inputDir); + inputPathCache.put(moduleKey, inputDir); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/JSONReport.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/JSONReport.java index d4fd9b69231..19523999ab0 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/JSONReport.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/report/JSONReport.java @@ -30,7 +30,6 @@ import org.sonar.api.PropertyType; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputPath; import org.sonar.api.batch.fs.internal.DefaultInputDir; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.rule.ActiveRules; @@ -43,9 +42,9 @@ import org.sonar.api.rule.RuleKey; import org.sonar.api.utils.SonarException; import org.sonar.api.utils.text.JsonWriter; import org.sonar.batch.issue.IssueCache; +import org.sonar.batch.repository.user.User; +import org.sonar.batch.repository.user.UserRepository; import org.sonar.batch.scan.filesystem.InputPathCache; -import org.sonar.batch.user.User; -import org.sonar.batch.user.UserRepository; import java.io.BufferedWriter; import java.io.File; @@ -167,27 +166,24 @@ public class JSONReport implements Reporter { json.name("components").beginArray(); // Dump modules writeJsonModuleComponents(json, rootModule); - for (InputPath inputPath : fileCache.all()) { - if (inputPath instanceof InputFile) { - InputFile inputFile = (InputFile) inputPath; - String key = ((DefaultInputFile) inputFile).key(); - json - .beginObject() - .prop("key", key) - .prop("path", inputFile.relativePath()) - .prop("moduleKey", StringUtils.substringBeforeLast(key, ":")) - .prop("status", inputFile.status().name()) - .endObject(); - } else { - InputDir inputDir = (InputDir) inputPath; - String key = ((DefaultInputDir) inputDir).key(); - json - .beginObject() - .prop("key", key) - .prop("path", inputDir.relativePath()) - .prop("moduleKey", StringUtils.substringBeforeLast(key, ":")) - .endObject(); - } + for (InputFile inputFile : fileCache.allFiles()) { + String key = ((DefaultInputFile) inputFile).key(); + json + .beginObject() + .prop("key", key) + .prop("path", inputFile.relativePath()) + .prop("moduleKey", StringUtils.substringBeforeLast(key, ":")) + .prop("status", inputFile.status().name()) + .endObject(); + } + for (InputDir inputDir : fileCache.allDirs()) { + String key = ((DefaultInputDir) inputDir).key(); + json + .beginObject() + .prop("key", key) + .prop("path", inputDir.relativePath()) + .prop("moduleKey", StringUtils.substringBeforeLast(key, ":")) + .endObject(); } json.endArray(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scm/ScmSensor.java b/sonar-batch/src/main/java/org/sonar/batch/scm/ScmSensor.java index 2a33ed3b3fb..74fccaa446a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scm/ScmSensor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scm/ScmSensor.java @@ -35,6 +35,8 @@ import org.sonar.api.measures.CoreMetrics; import org.sonar.api.utils.TimeProfiler; import org.sonar.batch.protocol.input.FileData; import org.sonar.batch.protocol.input.ProjectRepositories; +import org.sonar.batch.scan.filesystem.InputFileMetadata; +import org.sonar.batch.scan.filesystem.InputPathCache; import java.util.LinkedList; import java.util.List; @@ -47,13 +49,15 @@ public final class ScmSensor implements Sensor { private final ScmConfiguration configuration; private final FileSystem fs; private final ProjectRepositories projectReferentials; + private final InputPathCache inputPathCache; public ScmSensor(ProjectDefinition projectDefinition, ScmConfiguration configuration, - ProjectRepositories projectReferentials, FileSystem fs) { + ProjectRepositories projectReferentials, FileSystem fs, InputPathCache inputPathCache) { this.projectDefinition = projectDefinition; this.configuration = configuration; this.projectReferentials = projectReferentials; this.fs = fs; + this.inputPathCache = inputPathCache; } @Override @@ -104,7 +108,7 @@ public final class ScmSensor implements Sensor { if (f.status() == Status.SAME && fileData != null) { if (fileData.needBlame()) { - addIfNotEmpty(filesToBlame, f); + addIfNotEmpty(filesToBlame, (DefaultInputFile) f); } else { // Copy previous measures String scmAuthorsByLine = fileData.scmAuthorsByLine(); @@ -117,12 +121,13 @@ public final class ScmSensor implements Sensor { } } } else { - addIfNotEmpty(filesToBlame, f); + addIfNotEmpty(filesToBlame, (DefaultInputFile) f); } } - private void addIfNotEmpty(List<InputFile> filesToBlame, InputFile f) { - if (!((DefaultInputFile) f).isEmpty()) { + private void addIfNotEmpty(List<InputFile> filesToBlame, DefaultInputFile f) { + InputFileMetadata metadata = inputPathCache.getFileMetadata(f.moduleKey(), f.relativePath()); + if (!metadata.isEmpty()) { filesToBlame.add(f); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizers.java b/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizers.java index f0d4789f7bc..4b99191538a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizers.java +++ b/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizers.java @@ -37,6 +37,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.nio.charset.Charset; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -67,7 +68,7 @@ public class CodeColorizers implements BatchComponent { } @CheckForNull - public SyntaxHighlightingData toSyntaxHighlighting(File file, String encoding, String language) { + public SyntaxHighlightingData toSyntaxHighlighting(File file, Charset charset, String language) { CodeColorizerFormat format = byLang.get(language); List<Tokenizer> tokenizers; if (format == null) { @@ -81,7 +82,7 @@ public class CodeColorizers implements BatchComponent { } else { tokenizers = format.getTokenizers(); } - try (Reader reader = new BufferedReader(new InputStreamReader(new BOMInputStream(new FileInputStream(file)), encoding))) { + try (Reader reader = new BufferedReader(new InputStreamReader(new BOMInputStream(new FileInputStream(file)), charset))) { return new HighlightingRenderer().render(reader, tokenizers); } catch (Exception e) { throw new IllegalStateException("Unable to read source file for colorization", e); diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/LinesSensor.java b/sonar-batch/src/main/java/org/sonar/batch/source/LinesSensor.java index 4829066972a..b2f159d0997 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/source/LinesSensor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/source/LinesSensor.java @@ -29,14 +29,16 @@ import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.SensorDescriptor; import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; import org.sonar.api.measures.CoreMetrics; +import org.sonar.batch.scan.filesystem.InputFileMetadata; +import org.sonar.batch.scan.filesystem.InputPathCache; @Phase(name = Phase.Name.PRE) public final class LinesSensor implements Sensor { - private final FileSystem fs; + private final InputPathCache inputPathCache; - public LinesSensor(FileSystem fs) { - this.fs = fs; + public LinesSensor(InputPathCache inputPathCache) { + this.inputPathCache = inputPathCache; } @Override @@ -46,6 +48,7 @@ public final class LinesSensor implements Sensor { @Override public void execute(final SensorContext context) { + FileSystem fs = context.fileSystem(); for (InputFile f : fs.inputFiles(fs.predicates().hasType(Type.MAIN))) { ((DefaultMeasure<Integer>) context.<Integer>newMeasure() .onFile(f) @@ -55,10 +58,11 @@ public final class LinesSensor implements Sensor { .save(); if (f.language() == null) { // As an approximation for files with no language plugin we consider every non blank line as ncloc + InputFileMetadata metadata = inputPathCache.getFileMetadata(((DefaultInputFile) f).moduleKey(), f.relativePath()); ((DefaultMeasure<Integer>) context.<Integer>newMeasure() .onFile(f) .forMetric(CoreMetrics.NCLOC) - .withValue(((DefaultInputFile) f).nonBlankLines())) + .withValue(metadata.nonBlankLines())) .save(); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/DefaultFileLinesContextTest.java b/sonar-batch/src/test/java/org/sonar/batch/DefaultFileLinesContextTest.java index 9bf9dc201d4..e85e6003c2f 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/DefaultFileLinesContextTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/DefaultFileLinesContextTest.java @@ -57,7 +57,7 @@ public class DefaultFileLinesContextTest { @Test(expected = IllegalArgumentException.class) public void shouldNotAllowCreationForDirectory() { - new DefaultFileLinesContext(index, new Directory("key")); + new DefaultFileLinesContext(index, Directory.create("key")); } @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastMeasuresLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastMeasuresLoaderTest.java index 4be6293c27a..d032e2b1cd1 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastMeasuresLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastMeasuresLoaderTest.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.components; +import org.sonar.batch.components.PastMeasuresLoader; + import org.junit.Test; import org.sonar.api.database.model.Snapshot; import org.sonar.api.measures.Metric; diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java index c335d3b87d6..f712b04dbc4 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderTest.java @@ -19,6 +19,12 @@ */ package org.sonar.batch.components; +import org.sonar.batch.components.PastSnapshotFinder; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByDate; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByDays; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByPreviousAnalysis; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByPreviousVersion; +import org.sonar.batch.deprecated.components.PastSnapshotFinderByVersion; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentMatcher; diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java index 3d38431f780..201155b1188 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.components; +import org.sonar.batch.components.PastSnapshot; + import org.junit.Test; import org.sonar.api.CoreProperties; import org.sonar.api.database.model.Snapshot; diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java index 023690acdbd..eae7b0f8377 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java @@ -19,6 +19,10 @@ */ package org.sonar.batch.components; +import org.sonar.batch.components.TimeMachineConfiguration; + +import org.sonar.batch.components.PastSnapshot; +import org.sonar.batch.deprecated.components.PeriodsDefinition; import org.junit.Before; import org.junit.Test; import org.sonar.api.database.model.Snapshot; diff --git a/sonar-batch/src/test/java/org/sonar/batch/debt/DebtDecoratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/debt/DebtDecoratorTest.java index 945a3d7614b..7cb4b47c1c5 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/debt/DebtDecoratorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/debt/DebtDecoratorTest.java @@ -247,7 +247,7 @@ public class DebtDecoratorTest { // or for a file context = mock(DecoratorContext.class); - when(context.getResource()).thenReturn(new File("foo")); + when(context.getResource()).thenReturn(File.create("foo")); decorator.saveCharacteristicMeasure(context, (Characteristic) null, 12.0, false); verify(context, times(1)).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT)); } @@ -282,7 +282,7 @@ public class DebtDecoratorTest { @Test public void not_save_technical_debt_for_file_if_zero() throws Exception { DecoratorContext context = mock(DecoratorContext.class); - when(context.getResource()).thenReturn(new File("foo")); + when(context.getResource()).thenReturn(File.create("foo")); decorator.saveCharacteristicMeasure(context, null, 0.0, true); verify(context, never()).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT)); diff --git a/sonar-batch/src/test/java/org/sonar/batch/debt/NewDebtDecoratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/debt/NewDebtDecoratorTest.java index 0bb25ad1d82..c930eb1075e 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/debt/NewDebtDecoratorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/debt/NewDebtDecoratorTest.java @@ -20,6 +20,9 @@ package org.sonar.batch.debt; +import org.sonar.batch.components.TimeMachineConfiguration; + +import org.sonar.batch.deprecated.components.Period; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.time.DateUtils; import org.junit.Before; @@ -42,8 +45,6 @@ import org.sonar.api.measures.Metric; import org.sonar.api.resources.Resource; import org.sonar.api.test.IsMeasure; import org.sonar.api.utils.Duration; -import org.sonar.batch.components.Period; -import org.sonar.batch.components.TimeMachineConfiguration; import org.sonar.batch.debt.IssueChangelogDebtCalculator; import org.sonar.batch.debt.NewDebtDecorator; diff --git a/sonar-batch/src/test/java/org/sonar/batch/debt/SqaleRatingDecoratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/debt/SqaleRatingDecoratorTest.java index 0caa8cf219e..79ce075a097 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/debt/SqaleRatingDecoratorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/debt/SqaleRatingDecoratorTest.java @@ -73,10 +73,9 @@ public class SqaleRatingDecoratorTest { public void setUp() throws Exception { settings = new Settings(); - fs = new DefaultFileSystem(temp.newFolder()); + fs = new DefaultFileSystem(temp.newFolder().toPath()); fs.add(new DefaultInputFile("foo", file.getPath()) - .setLanguage("java") - .setFile(temp.newFile("Foo.java"))); + .setLanguage("java")); decorator = new SqaleRatingDecorator(new SqaleRatingSettings(settings), metrics, fs); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/ResourceFiltersTest.java b/sonar-batch/src/test/java/org/sonar/batch/deprecated/ResourceFiltersTest.java index 602b6c7dd42..c93334474b2 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/ResourceFiltersTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/deprecated/ResourceFiltersTest.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.batch.deprecated; import org.junit.Test; import org.slf4j.Logger; diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByDateTest.java b/sonar-batch/src/test/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByDateTest.java index 7abd043f62b..2cf8b317531 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByDateTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByDateTest.java @@ -17,8 +17,11 @@ * 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.components; +package org.sonar.batch.deprecated.components; +import org.sonar.batch.components.PastSnapshot; + +import org.sonar.batch.deprecated.components.PastSnapshotFinderByDate; import org.junit.Test; import org.sonar.api.database.model.Snapshot; import org.sonar.jpa.test.AbstractDbUnitTestCase; diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByDaysTest.java b/sonar-batch/src/test/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByDaysTest.java index 1b136e9ae72..0515b30673d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByDaysTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByDaysTest.java @@ -17,7 +17,9 @@ * 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.components; +package org.sonar.batch.deprecated.components; + +import org.sonar.batch.deprecated.components.PastSnapshotFinderByDays; import org.hamcrest.core.IsNull; import org.junit.Test; diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest.java b/sonar-batch/src/test/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousAnalysisTest.java index a2c0d82e301..9ec2024b7d4 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousAnalysisTest.java @@ -17,12 +17,14 @@ * 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.components; +package org.sonar.batch.deprecated.components; +import org.sonar.batch.components.PastSnapshot; + +import org.sonar.batch.deprecated.components.PastSnapshotFinderByPreviousAnalysis; import org.junit.Test; import org.sonar.api.database.model.Snapshot; import org.sonar.jpa.test.AbstractDbUnitTestCase; - import static org.hamcrest.Matchers.is; import static org.hamcrest.core.IsNull.nullValue; import static org.junit.Assert.assertThat; diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest.java b/sonar-batch/src/test/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousVersionTest.java index da160c87a4f..d932060914a 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousVersionTest.java @@ -17,13 +17,15 @@ * 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.components; +package org.sonar.batch.deprecated.components; +import org.sonar.batch.components.PastSnapshot; + +import org.sonar.batch.deprecated.components.PastSnapshotFinderByPreviousVersion; import org.junit.Test; import org.sonar.api.CoreProperties; import org.sonar.api.database.model.Snapshot; import org.sonar.jpa.test.AbstractDbUnitTestCase; - import static org.assertj.core.api.Assertions.assertThat; public class PastSnapshotFinderByPreviousVersionTest extends AbstractDbUnitTestCase { diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByVersionTest.java b/sonar-batch/src/test/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByVersionTest.java index b35f4b8990b..5d8c87ad7f0 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotFinderByVersionTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/deprecated/components/PastSnapshotFinderByVersionTest.java @@ -17,13 +17,15 @@ * 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.components; +package org.sonar.batch.deprecated.components; +import org.sonar.batch.components.PastSnapshot; + +import org.sonar.batch.deprecated.components.PastSnapshotFinderByVersion; import org.junit.Test; import org.sonar.api.CoreProperties; import org.sonar.api.database.model.Snapshot; import org.sonar.jpa.test.AbstractDbUnitTestCase; - import static org.assertj.core.api.Assertions.assertThat; public class PastSnapshotFinderByVersionTest extends AbstractDbUnitTestCase { diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PeriodsDefinitionTest.java b/sonar-batch/src/test/java/org/sonar/batch/deprecated/components/PeriodsDefinitionTest.java index 7483d54cf23..425ce9e4e89 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PeriodsDefinitionTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/deprecated/components/PeriodsDefinitionTest.java @@ -18,8 +18,11 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.batch.components; +package org.sonar.batch.deprecated.components; +import org.sonar.batch.components.PastSnapshotFinder; + +import org.sonar.batch.deprecated.components.PeriodsDefinition; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentMatcher; @@ -28,7 +31,6 @@ import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Project; import org.sonar.batch.ProjectTree; import org.sonar.jpa.test.AbstractDbUnitTestCase; - import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.argThat; import static org.mockito.Matchers.eq; diff --git a/sonar-batch/src/test/java/org/sonar/batch/DecoratorsSelectorTest.java b/sonar-batch/src/test/java/org/sonar/batch/deprecated/decorator/DecoratorsSelectorTest.java index 8a0e9a24764..f469d6b52e9 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/DecoratorsSelectorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/deprecated/decorator/DecoratorsSelectorTest.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.batch.deprecated.decorator; import com.google.common.collect.Iterables; import org.junit.Test; diff --git a/sonar-batch/src/test/java/org/sonar/batch/FormulaDecoratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/deprecated/decorator/FormulaDecoratorTest.java index ff5fdc5d159..4507ab8cbc0 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/FormulaDecoratorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/deprecated/decorator/FormulaDecoratorTest.java @@ -17,7 +17,9 @@ * 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; +package org.sonar.batch.deprecated.decorator; + +import org.sonar.batch.deprecated.decorator.FormulaDecorator; import org.junit.Test; import org.sonar.api.batch.DecoratorContext; diff --git a/sonar-batch/src/test/java/org/sonar/batch/tasks/ListTaskTest.java b/sonar-batch/src/test/java/org/sonar/batch/deprecated/tasks/ListTaskTest.java index 923af5177e5..2812b4d28cf 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/tasks/ListTaskTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/deprecated/tasks/ListTaskTest.java @@ -17,7 +17,10 @@ * 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.tasks; +package org.sonar.batch.deprecated.tasks; + +import org.sonar.batch.deprecated.tasks.ListTask; +import org.sonar.batch.deprecated.tasks.Tasks; import org.junit.Test; import org.sonar.api.task.Task; diff --git a/sonar-batch/src/test/java/org/sonar/batch/tasks/TasksTest.java b/sonar-batch/src/test/java/org/sonar/batch/deprecated/tasks/TasksTest.java index 5c4315890c2..51f6eb1c53b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/tasks/TasksTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/deprecated/tasks/TasksTest.java @@ -17,7 +17,7 @@ * 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.tasks; +package org.sonar.batch.deprecated.tasks; import org.junit.Rule; import org.junit.Test; @@ -36,20 +36,20 @@ public class TasksTest { @Test public void should_get_definitions() { - Tasks tasks = new Tasks(new TaskDefinition[]{ScanTask.DEFINITION, ListTask.DEFINITION}); + Tasks tasks = new Tasks(new TaskDefinition[] {ScanTask.DEFINITION, ListTask.DEFINITION}); assertThat(tasks.definitions()).hasSize(2); } @Test public void should_get_definition_by_key() { - Tasks tasks = new Tasks(new TaskDefinition[]{ScanTask.DEFINITION, ListTask.DEFINITION}); + Tasks tasks = new Tasks(new TaskDefinition[] {ScanTask.DEFINITION, ListTask.DEFINITION}); tasks.start(); assertThat(tasks.definition(ListTask.DEFINITION.key())).isEqualTo(ListTask.DEFINITION); } @Test public void should_return_null_if_task_not_found() { - Tasks tasks = new Tasks(new TaskDefinition[]{ScanTask.DEFINITION, ListTask.DEFINITION}); + Tasks tasks = new Tasks(new TaskDefinition[] {ScanTask.DEFINITION, ListTask.DEFINITION}); assertThat(tasks.definition("not-exists")).isNull(); } @@ -59,7 +59,7 @@ public class TasksTest { thrown.expect(SonarException.class); thrown.expectMessage("Task 'foo' is declared twice"); - new Tasks(new TaskDefinition[]{ + new Tasks(new TaskDefinition[] { TaskDefinition.builder().key("foo").taskClass(FakeTask1.class).description("foo1").build(), TaskDefinition.builder().key("foo").taskClass(FakeTask2.class).description("foo2").build() }); @@ -67,13 +67,13 @@ public class TasksTest { @Test public void should_fail_on_duplicated_class() { - Tasks tasks = new Tasks(new TaskDefinition[]{ + Tasks tasks = new Tasks(new TaskDefinition[] { TaskDefinition.builder().key("foo1").taskClass(FakeTask1.class).description("foo1").build(), TaskDefinition.builder().key("foo2").taskClass(FakeTask1.class).description("foo1").build() }); thrown.expect(SonarException.class); - thrown.expectMessage("Task 'org.sonar.batch.tasks.TasksTest$FakeTask1' is defined twice: first by 'foo1' and then by 'foo2'"); + thrown.expectMessage("Task 'org.sonar.batch.deprecated.tasks.TasksTest$FakeTask1' is defined twice: first by 'foo1' and then by 'foo2'"); tasks.start(); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/design/DirectoryDsmDecoratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/design/DirectoryDsmDecoratorTest.java index 598bf4d6f88..fa3ceecc3a5 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/design/DirectoryDsmDecoratorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/design/DirectoryDsmDecoratorTest.java @@ -65,9 +65,9 @@ public class DirectoryDsmDecoratorTest { dir = Directory.create("src"); dirContext = mock(DecoratorContext.class); - file1 = File.create("src/Foo1.java", "Foo1.java", null, false); + file1 = File.create("src/Foo1.java", null, false); file1.setId(1); - file2 = File.create("src/Foo2.java", "Foo2.java", null, false); + file2 = File.create("src/Foo2.java", null, false); file2.setId(2); file1Context = mock(DecoratorContext.class); diff --git a/sonar-batch/src/test/java/org/sonar/batch/design/DsmSerializerTest.java b/sonar-batch/src/test/java/org/sonar/batch/design/DsmSerializerTest.java index 24d949d000c..5d9b0b508ef 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/design/DsmSerializerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/design/DsmSerializerTest.java @@ -43,8 +43,8 @@ public class DsmSerializerTest { @Test public void serialize() throws IOException { - Resource foo = Directory.create("src/org/foo", "org/foo").setId(7); - Resource bar = Directory.create("src/org/bar", "org/bar").setId(8); + Resource foo = Directory.create("src/org/foo").setId(7); + Resource bar = Directory.create("src/org/bar").setId(8); Dependency dep = new Dependency(foo, bar).setId(30l).setWeight(1); DirectedGraph<Resource, Dependency> graph = new DirectedGraph<Resource, Dependency>(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/design/SubProjectDsmDecoratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/design/SubProjectDsmDecoratorTest.java index 94d7d621108..a9a41264adf 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/design/SubProjectDsmDecoratorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/design/SubProjectDsmDecoratorTest.java @@ -60,9 +60,9 @@ public class SubProjectDsmDecoratorTest { module = new Project("foo"); moduleContext = mock(DecoratorContext.class); - dir1 = Directory.create("src/foo1", "foo1"); + dir1 = Directory.create("src/foo1"); dir1.setId(1); - dir2 = Directory.create("src/foo2", "foo2"); + dir2 = Directory.create("src/foo2"); dir2.setId(2); DecoratorContext dir1Context = mock(DecoratorContext.class); diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/BucketTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/BucketTest.java index 51a9c0269c9..97bdb61d75e 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/BucketTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/BucketTest.java @@ -24,17 +24,17 @@ import org.sonar.api.measures.Metric; import org.sonar.api.resources.Directory; import org.sonar.api.resources.File; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; import static org.hamcrest.core.IsNot.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; -import static org.hamcrest.Matchers.hasItem; public class BucketTest { - Directory directory = new Directory("org/foo"); - File javaFile = new File("org/foo/Bar.java"); + Directory directory = Directory.create("org/foo"); + File javaFile = File.create("org/foo/Bar.java"); Metric ncloc = new Metric("ncloc"); @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java index ca7a86e791c..fe541281e05 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java @@ -95,10 +95,10 @@ public class DefaultIndexTest { @Test public void shouldIndexParentOfDeprecatedFiles() { - File file = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false); + File file = File.create("src/org/foo/Bar.java", null, false); assertThat(index.index(file)).isTrue(); - Directory reference = Directory.create("src/org/foo", "org/foo"); + Directory reference = Directory.create("src/org/foo"); assertThat(index.getResource(reference).getName()).isEqualTo("src/org/foo"); assertThat(index.isIndexed(reference, true)).isTrue(); assertThat(index.isExcluded(reference)).isFalse(); @@ -108,15 +108,14 @@ public class DefaultIndexTest { @Test public void shouldIndexTreeOfResources() { - Directory directory = Directory.create("src/org/foo", "org/foo"); - File file = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", Java.INSTANCE, false); + Directory directory = Directory.create("src/org/foo"); + File file = File.create("src/org/foo/Bar.java", Java.INSTANCE, false); assertThat(index.index(directory)).isTrue(); assertThat(index.index(file, directory)).isTrue(); - File fileRef = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false); + File fileRef = File.create("src/org/foo/Bar.java", null, false); assertThat(index.getResource(fileRef).getKey()).isEqualTo("src/org/foo/Bar.java"); - assertThat(index.getResource(fileRef).getDeprecatedKey()).isEqualTo("org/foo/Bar.java"); assertThat(index.getResource(fileRef).getLanguage().getKey()).isEqualTo("java"); assertThat(index.isIndexed(fileRef, true)).isTrue(); assertThat(index.isExcluded(fileRef)).isFalse(); @@ -126,14 +125,14 @@ public class DefaultIndexTest { @Test public void shouldGetSource() throws Exception { - Directory directory = Directory.create("src/org/foo", "org/foo"); - File file = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", Java.INSTANCE, false); + Directory directory = Directory.create("src/org/foo"); + File file = File.create("src/org/foo/Bar.java", Java.INSTANCE, false); FileUtils.write(new java.io.File(baseDir, "src/org/foo/Bar.java"), "Foo bar"); assertThat(index.index(directory)).isTrue(); assertThat(index.index(file, directory)).isTrue(); - File fileRef = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false); + File fileRef = File.create("src/org/foo/Bar.java", null, false); assertThat(index.getSource(fileRef)).isEqualTo("Foo bar"); } @@ -150,12 +149,12 @@ public class DefaultIndexTest { @Test public void shouldNotIndexResourceIfParentNotIndexed() { - Directory directory = Directory.create("src/org/other", "org/other"); - File file = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false); + Directory directory = Directory.create("src/org/other"); + File file = File.create("src/org/foo/Bar.java", null, false); assertThat(index.index(file, directory)).isFalse(); - File fileRef = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false); + File fileRef = File.create("src/org/foo/Bar.java", null, false); assertThat(index.isIndexed(directory, true)).isFalse(); assertThat(index.isIndexed(fileRef, true)).isFalse(); assertThat(index.isExcluded(fileRef)).isFalse(); @@ -165,7 +164,7 @@ public class DefaultIndexTest { @Test public void shouldNotIndexResourceWhenAddingMeasure() { - Resource dir = Directory.create("src/org/foo", "org/foo"); + Resource dir = Directory.create("src/org/foo"); index.addMeasure(dir, new Measure("ncloc").setValue(50.0)); assertThat(index.isIndexed(dir, true)).isFalse(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DuplicationPersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DuplicationPersisterTest.java index 3517b28789f..a62dd075bb5 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/DuplicationPersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/DuplicationPersisterTest.java @@ -46,7 +46,7 @@ public class DuplicationPersisterTest extends AbstractDaoTestCase { DuplicationPersister duplicationPersister; RuleFinder ruleFinder = mock(RuleFinder.class); - File aFile = new File("org/foo/Bar.java"); + File aFile = File.create("org/foo/Bar.java"); Snapshot fileSnapshot = snapshot(FILE_SNAPSHOT_ID); private DuplicationCache duplicationCache; diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java index b5cfb38c063..fb59aa4d0a5 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java @@ -61,8 +61,8 @@ public class MeasurePersisterTest extends AbstractDaoTestCase { MeasurePersister measurePersister; RuleFinder ruleFinder = mock(RuleFinder.class); Project project = new Project("foo"); - Directory aDirectory = new Directory("org/foo"); - File aFile = new File("org/foo/Bar.java"); + Directory aDirectory = Directory.create("org/foo"); + File aFile = File.create("org/foo/Bar.java"); BatchResource projectResource = batchResource(project, PROJECT_SNAPSHOT_ID); BatchResource dirResource = batchResource(aDirectory, PACKAGE_SNAPSHOT_ID); BatchResource fileResource = batchResource(aFile, FILE_SNAPSHOT_ID); diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/ResourceCacheTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/ResourceCacheTest.java index 33eb06086d0..bd250b544dd 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/ResourceCacheTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/ResourceCacheTest.java @@ -31,7 +31,7 @@ public class ResourceCacheTest { public void should_cache_resource() throws Exception { ResourceCache cache = new ResourceCache(); String componentKey = "struts:src/org/struts/Action.java"; - Resource resource = new File("org/struts/Action.java").setEffectiveKey(componentKey); + Resource resource = File.create("org/struts/Action.java").setEffectiveKey(componentKey); cache.add(resource, null); assertThat(cache.get(componentKey).resource()).isSameAs(resource); @@ -41,7 +41,7 @@ public class ResourceCacheTest { @Test public void should_fail_if_missing_component_key() throws Exception { ResourceCache cache = new ResourceCache(); - Resource resource = new File("org/struts/Action.java").setEffectiveKey(null); + Resource resource = File.create("org/struts/Action.java").setEffectiveKey(null); try { cache.add(resource, null); fail(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/ResourceKeyMigrationTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/ResourceKeyMigrationTest.java index e91b5596101..1d3d227a4d4 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/ResourceKeyMigrationTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/ResourceKeyMigrationTest.java @@ -31,6 +31,8 @@ import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; +import org.sonar.api.scan.filesystem.PathResolver; +import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; import org.sonar.jpa.test.AbstractDbUnitTestCase; import java.io.File; @@ -39,6 +41,7 @@ import java.util.Arrays; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class ResourceKeyMigrationTest extends AbstractDbUnitTestCase { @@ -73,23 +76,21 @@ public class ResourceKeyMigrationTest extends AbstractDbUnitTestCase { baseDir = temp.newFolder(); javaInputFiles = (Iterable) Arrays.asList( - newInputFile(javaModule, "src/main/java/org/foo/Bar.java", "org.foo.Bar", false), - newInputFile(javaModule, "src/main/java/RootBar.java", "[default].RootBar", false), - newInputFile(javaModule, "src/test/java/org/foo/BarTest.java", "org.foo.BarTest", true)); + newInputFile(javaModule, "src/main/java/org/foo/Bar.java", false, "java"), + newInputFile(javaModule, "src/main/java/RootBar.java", false, "java"), + newInputFile(javaModule, "src/test/java/org/foo/BarTest.java", true, "java")); phpInputFiles = (Iterable) Arrays.asList( - newInputFile(phpModule, "org/foo/Bar.php", "org/foo/Bar.php", false), - newInputFile(phpModule, "RootBar.php", "RootBar.php", false), - newInputFile(phpModule, "test/org/foo/BarTest.php", "org/foo/BarTest.php", true)); + newInputFile(phpModule, "org/foo/Bar.php", false, "php"), + newInputFile(phpModule, "RootBar.php", false, "php"), + newInputFile(phpModule, "test/org/foo/BarTest.php", true, "php")); } - private DefaultInputFile newInputFile(Project module, String path, String deprecatedKey, boolean isTest) { - File file = new File(baseDir, path); - String deprecatedEffectiveKey = module.getKey() + ":" + deprecatedKey; + private DefaultInputFile newInputFile(Project module, String path, boolean isTest, String language) { return new DeprecatedDefaultInputFile(module.getKey(), path) - .setDeprecatedKey(deprecatedEffectiveKey) - .setFile(file) + .setModuleBaseDir(baseDir.toPath()) + .setLanguage(language) .setType(isTest ? InputFile.Type.TEST : InputFile.Type.MAIN); } @@ -98,11 +99,17 @@ public class ResourceKeyMigrationTest extends AbstractDbUnitTestCase { setupData("shouldMigrateResourceKeys"); Logger logger = mock(Logger.class); - ResourceKeyMigration migration = new ResourceKeyMigration(getSession(), logger); + ResourceKeyMigration migration = new ResourceKeyMigration(getSession(), new PathResolver(), logger); migration.checkIfMigrationNeeded(multiModuleProject); - migration.migrateIfNeeded(javaModule, javaInputFiles); - migration.migrateIfNeeded(phpModule, phpInputFiles); + DefaultModuleFileSystem fs = mock(DefaultModuleFileSystem.class); + when(fs.sourceDirs()).thenReturn(Arrays.asList(new File(baseDir, "src/main/java"))); + when(fs.testDirs()).thenReturn(Arrays.asList(new File(baseDir, "src/test/java"))); + migration.migrateIfNeeded(javaModule, javaInputFiles, fs); + + when(fs.sourceDirs()).thenReturn(Arrays.asList(new File(baseDir, "."))); + when(fs.testDirs()).thenReturn(Arrays.asList(new File(baseDir, "test"))); + migration.migrateIfNeeded(phpModule, phpInputFiles, fs); verify(logger).info("Component {} changed to {}", "b:org.foo.Bar", "b:src/main/java/org/foo/Bar.java"); verify(logger).warn("Directory with key b:org/foo matches both b:src/main/java/org/foo and b:src/test/java/org/foo. First match is arbitrary chosen."); diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/ResourcePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/ResourcePersisterTest.java index 6ec57439787..6a4506273b4 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/ResourcePersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/ResourcePersisterTest.java @@ -290,7 +290,7 @@ public class ResourcePersisterTest extends AbstractDbUnitTestCase { setupData("shared"); resourceCache.add(singleProject, null).setSnapshot(persister.persist(null, singleProject, null)); - persister.persist(singleProject, Directory.create("src/main/java/org/foo", "org.foo").setEffectiveKey("foo:src/main/java/org/foo"), null); + persister.persist(singleProject, Directory.create("src/main/java/org/foo").setEffectiveKey("foo:src/main/java/org/foo"), null); // check that the directory is attached to the project checkTables("shouldSaveNewDirectory", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid", "project_uuid", "module_uuid", "module_uuid_path"}, "projects", "snapshots"); diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/SourceDataFactoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/SourceDataFactoryTest.java index 821cb735756..2c510d62aac 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/SourceDataFactoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/SourceDataFactoryTest.java @@ -26,6 +26,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.sensor.duplication.DuplicationGroup; import org.sonar.api.batch.sensor.highlighting.TypeOfText; @@ -35,6 +36,7 @@ import org.sonar.api.measures.Metric; import org.sonar.batch.duplication.DuplicationCache; import org.sonar.batch.highlighting.SyntaxHighlightingData; import org.sonar.batch.highlighting.SyntaxHighlightingDataBuilder; +import org.sonar.batch.scan.filesystem.InputFileMetadata; import org.sonar.batch.scan.measure.MeasureCache; import org.sonar.batch.source.CodeColorizers; import org.sonar.batch.symbol.DefaultSymbolTableBuilder; @@ -58,18 +60,21 @@ public class SourceDataFactoryTest { DuplicationCache duplicationCache = mock(DuplicationCache.class); CodeColorizers colorizers = mock(CodeColorizers.class); DefaultInputFile inputFile; + InputFileMetadata metadata; SourceDataFactory sut = new SourceDataFactory(measureCache, componentDataCache, duplicationCache, colorizers); FileSourceDb.Data.Builder output; @Before public void setUp() throws Exception { // generate a file with 3 lines - File file = temp.newFile(); + File baseDir = temp.newFolder(); + DefaultFileSystem fs = new DefaultFileSystem(baseDir.toPath()); inputFile = new DefaultInputFile("module_key", "src/Foo.java") .setLines(3) - .setEncoding(Charsets.UTF_8.name()) - .setFile(file); - FileUtils.write(file, "one\ntwo\nthree\n"); + .setCharset(Charsets.UTF_8); + fs.add(inputFile); + metadata = new InputFileMetadata(); + FileUtils.write(inputFile.file(), "one\ntwo\nthree\n"); output = sut.createForSource(inputFile); } @@ -84,7 +89,7 @@ public class SourceDataFactoryTest { @Test public void consolidateData() throws Exception { - byte[] bytes = sut.consolidateData(inputFile); + byte[] bytes = sut.consolidateData(inputFile, metadata); assertThat(bytes).isNotEmpty(); } @@ -145,7 +150,6 @@ public class SourceDataFactoryTest { Arrays.asList(new Measure().setData(dataPerLine).setMetric(metric))); } - @Test public void applyDuplications() throws Exception { DuplicationGroup group1 = new DuplicationGroup(new DuplicationGroup.Block(inputFile.key(), 1, 1)) @@ -169,7 +173,7 @@ public class SourceDataFactoryTest { public void applyHighlighting_missing() throws Exception { when(componentDataCache.getData(inputFile.key(), SnapshotDataTypes.SYNTAX_HIGHLIGHTING)).thenReturn(null); - sut.applyHighlighting(inputFile, output); + sut.applyHighlighting(inputFile, metadata, output); FileSourceDb.Data data = output.build(); assertThat(data.getLines(0).hasHighlighting()).isFalse(); @@ -185,9 +189,9 @@ public class SourceDataFactoryTest { .registerHighlightingRule(7, 16, TypeOfText.CONSTANT) .build(); when(componentDataCache.getData(inputFile.key(), SnapshotDataTypes.SYNTAX_HIGHLIGHTING)).thenReturn(highlighting); - inputFile.setOriginalLineOffsets(new long[] {0, 4, 7}); + metadata.setOriginalLineOffsets(new int[] {0, 4, 7}); - sut.applyHighlighting(inputFile, output); + sut.applyHighlighting(inputFile, metadata, output); FileSourceDb.Data data = output.build(); assertThat(data.getLines(0).getHighlighting()).isEqualTo("0,4,a"); @@ -203,9 +207,9 @@ public class SourceDataFactoryTest { .registerHighlightingRule(10, 16, TypeOfText.CONSTANT) .build(); when(componentDataCache.getData(inputFile.key(), SnapshotDataTypes.SYNTAX_HIGHLIGHTING)).thenReturn(highlighting); - inputFile.setOriginalLineOffsets(new long[] {0, 4, 7}); + metadata.setOriginalLineOffsets(new int[] {0, 4, 7}); - sut.applyHighlighting(inputFile, output); + sut.applyHighlighting(inputFile, metadata, output); FileSourceDb.Data data = output.build(); assertThat(data.getLines(0).getHighlighting()).isEqualTo("0,3,a"); @@ -222,9 +226,9 @@ public class SourceDataFactoryTest { .registerHighlightingRule(8, 15, TypeOfText.KEYWORD) .build(); when(componentDataCache.getData(inputFile.key(), SnapshotDataTypes.SYNTAX_HIGHLIGHTING)).thenReturn(highlighting); - inputFile.setOriginalLineOffsets(new long[] {0, 4, 7}); + metadata.setOriginalLineOffsets(new int[] {0, 4, 7}); - sut.applyHighlighting(inputFile, output); + sut.applyHighlighting(inputFile, metadata, output); FileSourceDb.Data data = output.build(); assertThat(data.getLines(0).getHighlighting()).isEqualTo("0,3,a"); @@ -241,9 +245,9 @@ public class SourceDataFactoryTest { .registerHighlightingRule(8, 15, TypeOfText.KEYWORD) .build(); when(componentDataCache.getData(inputFile.key(), SnapshotDataTypes.SYNTAX_HIGHLIGHTING)).thenReturn(highlighting); - inputFile.setOriginalLineOffsets(new long[] {0, 4, 7}); + metadata.setOriginalLineOffsets(new int[] {0, 4, 7}); - sut.applyHighlighting(inputFile, output); + sut.applyHighlighting(inputFile, metadata, output); FileSourceDb.Data data = output.build(); assertThat(data.getLines(0).getHighlighting()).isEqualTo("0,3,a"); @@ -255,7 +259,7 @@ public class SourceDataFactoryTest { public void applySymbolReferences_missing() throws Exception { when(componentDataCache.getData(inputFile.key(), SnapshotDataTypes.SYMBOL_HIGHLIGHTING)).thenReturn(null); - sut.applySymbolReferences(inputFile, output); + sut.applySymbolReferences(inputFile, metadata, output); FileSourceDb.Data data = output.build(); assertThat(data.getLines(0).hasSymbols()).isFalse(); @@ -273,9 +277,9 @@ public class SourceDataFactoryTest { symbolBuilder.newReference(s2, 0); symbolBuilder.newReference(s2, 7); when(componentDataCache.getData(inputFile.key(), SnapshotDataTypes.SYMBOL_HIGHLIGHTING)).thenReturn(symbolBuilder.build()); - inputFile.setOriginalLineOffsets(new long[] {0, 4, 7}); + metadata.setOriginalLineOffsets(new int[] {0, 4, 7}); - sut.applySymbolReferences(inputFile, output); + sut.applySymbolReferences(inputFile, metadata, output); FileSourceDb.Data data = output.build(); assertThat(data.getLines(0).getSymbols()).isEqualTo("1,2,1;0,2,2"); @@ -293,9 +297,9 @@ public class SourceDataFactoryTest { symbolBuilder.newReference(s1, 11); symbolBuilder.newReference(s1, 4); when(componentDataCache.getData(inputFile.key(), SnapshotDataTypes.SYMBOL_HIGHLIGHTING)).thenReturn(symbolBuilder.build()); - inputFile.setOriginalLineOffsets(new long[] {0, 4, 7}); + metadata.setOriginalLineOffsets(new int[] {0, 4, 7}); - sut.applySymbolReferences(inputFile, output); + sut.applySymbolReferences(inputFile, metadata, output); FileSourceDb.Data data = output.build(); assertThat(data.getLines(0).getSymbols()).isEqualTo("1,2,1;0,2,2"); diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/IssuableFactoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/IssuableFactoryTest.java index c24e04a1ded..514a6d05a1b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/IssuableFactoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/IssuableFactoryTest.java @@ -40,7 +40,7 @@ public class IssuableFactoryTest { @Test public void file_should_be_issuable() throws Exception { IssuableFactory factory = new IssuableFactory(moduleIssues, cache, projectTree); - Component component = new ResourceComponent(new File("foo/bar.c").setEffectiveKey("foo/bar.c")); + Component component = new ResourceComponent(File.create("foo/bar.c").setEffectiveKey("foo/bar.c")); Issuable issuable = factory.loadPerspective(Issuable.class, component); assertThat(issuable).isNotNull(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java index 0594cb16234..7ffa9905721 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java @@ -207,7 +207,7 @@ public class ModuleIssuesTest { initModuleIssues(); org.sonar.api.rules.Rule rule = org.sonar.api.rules.Rule.create("squid", "AvoidCycle", "Avoid Cycle"); - Resource resource = new File("org/struts/Action.java").setEffectiveKey("struts:src/org/struts/Action.java"); + Resource resource = File.create("org/struts/Action.java").setEffectiveKey("struts:src/org/struts/Action.java"); Violation violation = new Violation(rule, resource); violation.setLineId(42); violation.setSeverity(RulePriority.CRITICAL); diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsLoaderTest.java index 9fd36ece0b7..3cca58ac2b2 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsLoaderTest.java @@ -72,7 +72,7 @@ public class IssueExclusionsLoaderTest { @Before public void before() throws Exception { baseDir = temp.newFolder(); - fs = new DefaultFileSystem(baseDir).setEncoding(UTF_8); + fs = new DefaultFileSystem(baseDir.toPath()).setEncoding(UTF_8); MockitoAnnotations.initMocks(this); scanner = new IssueExclusionsLoader(regexpScanner, exclusionPatternInitializer, inclusionPatternInitializer, fs); } @@ -106,11 +106,9 @@ public class IssueExclusionsLoaderTest { public void shouldAnalyzeProject() throws IOException { File javaFile1 = new File(baseDir, "src/main/java/Foo.java"); fs.add(new DeprecatedDefaultInputFile("polop", "src/main/java/Foo.java") - .setFile(javaFile1) .setType(InputFile.Type.MAIN)); File javaTestFile1 = new File(baseDir, "src/test/java/FooTest.java"); fs.add(new DeprecatedDefaultInputFile("polop", "src/test/java/FooTest.java") - .setFile(javaTestFile1) .setType(InputFile.Type.TEST)); when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(true); @@ -129,11 +127,9 @@ public class IssueExclusionsLoaderTest { public void shouldAnalyseFilesOnlyWhenRegexConfigured() throws IOException { File javaFile1 = new File(baseDir, "src/main/java/Foo.java"); fs.add(new DeprecatedDefaultInputFile("polop", "src/main/java/Foo.java") - .setFile(javaFile1) .setType(InputFile.Type.MAIN)); File javaTestFile1 = new File(baseDir, "src/test/java/FooTest.java"); fs.add(new DeprecatedDefaultInputFile("polop", "src/test/java/FooTest.java") - .setFile(javaTestFile1) .setType(InputFile.Type.TEST)); when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(false); @@ -150,7 +146,6 @@ public class IssueExclusionsLoaderTest { public void shouldReportFailure() throws IOException { File phpFile1 = new File(baseDir, "src/Foo.php"); fs.add(new DeprecatedDefaultInputFile("polop", "src/Foo.php") - .setFile(phpFile1) .setType(InputFile.Type.MAIN)); when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(true); diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/IssueTrackingDecoratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/IssueTrackingDecoratorTest.java index 7691dbf6538..1c5e080cd06 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/IssueTrackingDecoratorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/IssueTrackingDecoratorTest.java @@ -19,9 +19,12 @@ */ package org.sonar.batch.issue.tracking; +import com.google.common.base.Charsets; import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.mockito.ArgumentCaptor; import org.mockito.ArgumentMatcher; import org.sonar.api.batch.DecoratorContext; @@ -41,6 +44,7 @@ import org.sonar.api.utils.Duration; import org.sonar.api.utils.System2; import org.sonar.batch.issue.IssueCache; import org.sonar.batch.scan.LastLineHashes; +import org.sonar.batch.scan.filesystem.InputFileMetadata; import org.sonar.batch.scan.filesystem.InputPathCache; import org.sonar.core.issue.IssueUpdater; import org.sonar.core.issue.db.IssueChangeDto; @@ -48,6 +52,7 @@ import org.sonar.core.issue.db.IssueDto; import org.sonar.core.issue.workflow.IssueWorkflow; import org.sonar.java.api.JavaClass; +import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -71,6 +76,9 @@ import static org.mockito.Mockito.when; public class IssueTrackingDecoratorTest { + @org.junit.Rule + public TemporaryFolder temp = new TemporaryFolder(); + IssueTrackingDecorator decorator; IssueCache issueCache = mock(IssueCache.class, RETURNS_MOCKS); InitialOpenIssuesStack initialOpenIssues = mock(InitialOpenIssuesStack.class); @@ -124,6 +132,7 @@ public class IssueTrackingDecoratorTest { List<PreviousIssue> dbIssues = Collections.emptyList(); when(initialOpenIssues.selectAndRemoveIssues("struts:Action.java")).thenReturn(dbIssues); when(inputPathCache.getFile("foo", "Action.java")).thenReturn(mock(DefaultInputFile.class)); + when(inputPathCache.getFileMetadata("foo", "Action.java")).thenReturn(new InputFileMetadata()); decorator.doDecorate(file); // Apply filters, track, apply transitions, notify extensions then update cache @@ -152,6 +161,7 @@ public class IssueTrackingDecoratorTest { when(tracking.track(isA(SourceHashHolder.class), anyCollection(), anyCollection())).thenReturn(trackingResult); when(inputPathCache.getFile("foo", "Action.java")).thenReturn(mock(DefaultInputFile.class)); + when(inputPathCache.getFileMetadata("foo", "Action.java")).thenReturn(new InputFileMetadata()); decorator.doDecorate(file); @@ -210,10 +220,13 @@ public class IssueTrackingDecoratorTest { assertThat(issue.isOnDisabledRule()).isFalse(); } - private Resource mockHashes(String originalSource, String newSource) { + private Resource mockHashes(String originalSource, String newSource) throws IOException { DefaultInputFile inputFile = mock(DefaultInputFile.class); - byte[][] hashes = computeHashes(newSource); - when(inputFile.lineHashes()).thenReturn(hashes); + java.io.File f = temp.newFile(); + when(inputFile.path()).thenReturn(f.toPath()); + when(inputFile.charset()).thenReturn(Charsets.UTF_8); + when(inputFile.lines()).thenReturn(newSource.split("\n").length); + FileUtils.write(f, newSource, Charsets.UTF_8); when(inputFile.key()).thenReturn("foo:Action.java"); when(inputPathCache.getFile("foo", "Action.java")).thenReturn(inputFile); when(lastSnapshots.getLineHashes("foo:Action.java")).thenReturn(computeHexHashes(originalSource)); @@ -561,15 +574,6 @@ public class IssueTrackingDecoratorTest { assertThat(issue.changes()).hasSize(1); } - private byte[][] computeHashes(String source) { - String[] lines = source.split("\n"); - byte[][] hashes = new byte[lines.length][]; - for (int i = 0; i < lines.length; i++) { - hashes[i] = DigestUtils.md5(lines[i].replaceAll("[\t ]", "")); - } - return hashes; - } - private String[] computeHexHashes(String source) { String[] lines = source.split("\n"); String[] hashes = new String[lines.length]; diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/IssueTrackingTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/IssueTrackingTest.java index 2efb24c13eb..bcc25ac598c 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/IssueTrackingTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/IssueTrackingTest.java @@ -24,8 +24,11 @@ import com.google.common.base.Charsets; import com.google.common.collect.Lists; import com.google.common.io.Resources; import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.io.FileUtils; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.issue.Issue; import org.sonar.api.issue.internal.DefaultIssue; @@ -35,6 +38,7 @@ import org.sonar.api.rule.RuleKey; import org.sonar.batch.scan.LastLineHashes; import org.sonar.core.issue.db.IssueDto; +import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.Collections; @@ -47,6 +51,9 @@ import static org.mockito.Mockito.when; public class IssueTrackingTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + IssueTracking tracking; Resource project; SourceHashHolder sourceHashHolder; @@ -345,22 +352,17 @@ public class IssueTrackingTest { private void initLastHashes(String reference, String newSource) throws IOException { DefaultInputFile inputFile = mock(DefaultInputFile.class); - byte[][] hashes = computeHashes(load(newSource)); - when(inputFile.lineHashes()).thenReturn(hashes); + File f = temp.newFile(); + when(inputFile.path()).thenReturn(f.toPath()); + when(inputFile.charset()).thenReturn(Charsets.UTF_8); + String data = load(newSource); + when(inputFile.lines()).thenReturn(data.split("\n").length); + FileUtils.write(f, data, Charsets.UTF_8); when(inputFile.key()).thenReturn("foo:Action.java"); when(lastSnapshots.getLineHashes("foo:Action.java")).thenReturn(computeHexHashes(load(reference))); sourceHashHolder = new SourceHashHolder(inputFile, lastSnapshots); } - private byte[][] computeHashes(String source) { - String[] lines = source.split("\n"); - byte[][] hashes = new byte[lines.length][]; - for (int i = 0; i < lines.length; i++) { - hashes[i] = DigestUtils.md5(lines[i].replaceAll("[\t ]", "")); - } - return hashes; - } - private String[] computeHexHashes(String source) { String[] lines = source.split("\n"); String[] hashes = new String[lines.length]; diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/SourceHashHolderTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/SourceHashHolderTest.java index 6ac6645bbae..59371ca4947 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/SourceHashHolderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/SourceHashHolderTest.java @@ -19,14 +19,19 @@ */ package org.sonar.batch.issue.tracking; +import com.google.common.base.Charsets; +import org.apache.commons.io.FileUtils; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.mockito.Mockito; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.batch.scan.LastLineHashes; -import static org.apache.commons.codec.digest.DigestUtils.md5; +import java.io.File; + import static org.apache.commons.codec.digest.DigestUtils.md5Hex; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -35,39 +40,48 @@ import static org.mockito.Mockito.when; public class SourceHashHolderTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + SourceHashHolder sourceHashHolder; LastLineHashes lastSnapshots; DefaultInputFile file; + private File ioFile; + @Before - public void setUp() { + public void setUp() throws Exception { lastSnapshots = mock(LastLineHashes.class); file = mock(DefaultInputFile.class); + ioFile = temp.newFile(); + when(file.file()).thenReturn(ioFile); + when(file.path()).thenReturn(ioFile.toPath()); + when(file.lines()).thenReturn(1); + when(file.charset()).thenReturn(Charsets.UTF_8); sourceHashHolder = new SourceHashHolder(file, lastSnapshots); } @Test - public void should_lazy_load_line_hashes() { + public void should_lazy_load_line_hashes() throws Exception { final String source = "source"; - when(file.lineHashes()).thenReturn(new byte[][] {md5(source), null}); + FileUtils.write(ioFile, source + "\n", Charsets.UTF_8); + when(file.lines()).thenReturn(2); assertThat(sourceHashHolder.getHashedSource().getHash(1)).isEqualTo(md5Hex(source)); assertThat(sourceHashHolder.getHashedSource().getHash(2)).isEqualTo(""); - verify(file).lineHashes(); verify(file).key(); verify(file).status(); assertThat(sourceHashHolder.getHashedSource().getHash(1)).isEqualTo(md5Hex(source)); - Mockito.verifyNoMoreInteractions(file); } @Test - public void should_lazy_load_reference_hashes_when_status_changed() { + public void should_lazy_load_reference_hashes_when_status_changed() throws Exception { final String source = "source"; String key = "foo:src/Foo.java"; - when(file.lineHashes()).thenReturn(new byte[][] {md5(source)}); + FileUtils.write(ioFile, source, Charsets.UTF_8); when(file.key()).thenReturn(key); when(file.status()).thenReturn(InputFile.Status.CHANGED); when(lastSnapshots.getLineHashes(key)).thenReturn(new String[] {md5Hex(source)}); @@ -80,23 +94,22 @@ public class SourceHashHolderTest { } @Test - public void should_not_load_reference_hashes_when_status_same() { + public void should_not_load_reference_hashes_when_status_same() throws Exception { final String source = "source"; String key = "foo:src/Foo.java"; - when(file.lineHashes()).thenReturn(new byte[][] {md5(source)}); + FileUtils.write(ioFile, source, Charsets.UTF_8); when(file.key()).thenReturn(key); when(file.status()).thenReturn(InputFile.Status.SAME); assertThat(sourceHashHolder.getHashedReference().getHash(1)).isEqualTo(md5Hex(source)); - assertThat(sourceHashHolder.getHashedReference().getHash(1)).isEqualTo(md5Hex(source)); Mockito.verifyNoMoreInteractions(lastSnapshots); } @Test - public void no_reference_hashes_when_status_added() { + public void no_reference_hashes_when_status_added() throws Exception { final String source = "source"; String key = "foo:src/Foo.java"; - when(file.lineHashes()).thenReturn(new byte[][] {md5(source)}); + FileUtils.write(ioFile, source, Charsets.UTF_8); when(file.key()).thenReturn(key); when(file.status()).thenReturn(InputFile.Status.ADDED); diff --git a/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java b/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java index 45774d11f95..7b644d27530 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java @@ -28,8 +28,8 @@ import org.sonar.api.resources.File; import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.api.utils.SonarException; -import org.sonar.batch.DefaultDecoratorContext; import org.sonar.batch.bootstrap.BatchExtensionDictionnary; +import org.sonar.batch.deprecated.decorator.DefaultDecoratorContext; import org.sonar.batch.duplication.DuplicationCache; import org.sonar.batch.events.EventBus; import org.sonar.batch.scan.measure.MeasureCache; @@ -69,7 +69,7 @@ public class DecoratorsExecutorTest { DecoratorsExecutor executor = new DecoratorsExecutor(mock(BatchExtensionDictionnary.class), new Project("key"), mock(SonarIndex.class), mock(EventBus.class), mock(CoverageExclusions.class), mock(MeasureCache.class), mock(MetricFinder.class), mock(DuplicationCache.class)); try { - executor.executeDecorator(decorator, mock(DefaultDecoratorContext.class), File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false)); + executor.executeDecorator(decorator, mock(DefaultDecoratorContext.class), File.create("src/org/foo/Bar.java", null, false)); fail("Exception has not been thrown"); } catch (SonarException e) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/qualitygate/GenerateQualityGateEventsTest.java b/sonar-batch/src/test/java/org/sonar/batch/qualitygate/GenerateQualityGateEventsTest.java index 83cab61793e..e7e2c332838 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/qualitygate/GenerateQualityGateEventsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/qualitygate/GenerateQualityGateEventsTest.java @@ -19,8 +19,6 @@ */ package org.sonar.batch.qualitygate; -import org.sonar.batch.qualitygate.GenerateQualityGateEvents; - import org.junit.Before; import org.junit.Test; import org.sonar.api.batch.DecoratorContext; @@ -35,14 +33,20 @@ import org.sonar.api.notifications.NotificationManager; import org.sonar.api.resources.File; import org.sonar.api.resources.Project; import org.sonar.api.test.ProjectTestBuilder; -import org.sonar.batch.qualitygate.QualityGate; import java.util.Arrays; import java.util.Date; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Matchers.*; -import static org.mockito.Mockito.*; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.isNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class GenerateQualityGateEventsTest { private GenerateQualityGateEvents decorator; @@ -80,7 +84,7 @@ public class GenerateQualityGateEventsTest { @Test public void shouldNotDecorateIfNotRootProject() { - decorator.decorate(new File("Foo"), context); + decorator.decorate(File.create("Foo"), context); verify(context, never()).createEvent(anyString(), anyString(), anyString(), (Date) isNull()); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java index 87a72c6c057..30f56d3a46e 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java @@ -62,17 +62,17 @@ public class ComponentsPublisherTest { dir.setId(3).setUuid("DIR_UUID"); resourceCache.add(dir, module1).setSnapshot(new Snapshot().setId(13)); - org.sonar.api.resources.File file = org.sonar.api.resources.File.create("src/Foo.java", "Foo.java", Java.INSTANCE, false); + org.sonar.api.resources.File file = org.sonar.api.resources.File.create("src/Foo.java", Java.INSTANCE, false); file.setEffectiveKey("foo:src/Foo.java"); file.setId(4).setUuid("FILE_UUID"); resourceCache.add(file, dir).setSnapshot(new Snapshot().setId(14)); - org.sonar.api.resources.File fileWithoutLang = org.sonar.api.resources.File.create("src/make", "make", null, false); + org.sonar.api.resources.File fileWithoutLang = org.sonar.api.resources.File.create("src/make", null, false); fileWithoutLang.setEffectiveKey("foo:src/make"); fileWithoutLang.setId(5).setUuid("FILE_WITHOUT_LANG_UUID"); resourceCache.add(fileWithoutLang, dir).setSnapshot(new Snapshot().setId(15)); - org.sonar.api.resources.File testFile = org.sonar.api.resources.File.create("test/FooTest.java", "FooTest.java", Java.INSTANCE, true); + org.sonar.api.resources.File testFile = org.sonar.api.resources.File.create("test/FooTest.java", Java.INSTANCE, true); testFile.setEffectiveKey("foo:test/FooTest.java"); testFile.setId(6).setUuid("TEST_FILE_UUID"); resourceCache.add(testFile, dir).setSnapshot(new Snapshot().setId(16)); diff --git a/sonar-batch/src/test/java/org/sonar/batch/user/UserRepositoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryTest.java index 7191d185fa3..ad8c954e98f 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/user/UserRepositoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryTest.java @@ -17,7 +17,7 @@ * 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.user; +package org.sonar.batch.repository.user; import org.junit.Test; import org.sonar.batch.bootstrap.ServerClient; diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileSensorTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileSensorTest.java index c9e6deabcaf..7c83baad914 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileSensorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileSensorTest.java @@ -23,6 +23,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.SensorContext; import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.measures.CoreMetrics; @@ -57,20 +58,29 @@ public class QProfileSensorTest { @Before public void prepare() throws Exception { - fs = new DefaultFileSystem(temp.newFolder()); + fs = new DefaultFileSystem(temp.newFolder().toPath()); } @Test public void to_string() throws Exception { - QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs); + QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs, mock(AnalysisMode.class)); assertThat(sensor.toString()).isEqualTo("QProfileSensor"); } @Test + public void no_execution_in_preview() throws Exception { + AnalysisMode analysisMode = mock(AnalysisMode.class); + when(analysisMode.isPreview()).thenReturn(true); + QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs, analysisMode); + assertThat(sensor.shouldExecuteOnProject(project)).isFalse(); + + } + + @Test public void no_qprofiles() throws Exception { when(moduleQProfiles.findAll()).thenReturn(Collections.<QProfile>emptyList()); - QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs); + QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs, mock(AnalysisMode.class)); assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); sensor.analyse(project, sensorContext); @@ -85,7 +95,7 @@ public class QProfileSensorTest { when(moduleQProfiles.findByLanguage("abap")).thenReturn(null); fs.addLanguages("java", "php", "abap"); - QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs); + QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs, mock(AnalysisMode.class)); assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); sensor.analyse(project, sensorContext); } @@ -97,7 +107,7 @@ public class QProfileSensorTest { when(moduleQProfiles.findByLanguage("abap")).thenReturn(null); fs.addLanguages("java"); - QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs); + QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs, mock(AnalysisMode.class)); assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); sensor.analyse(project, sensorContext); @@ -113,7 +123,7 @@ public class QProfileSensorTest { when(moduleQProfiles.findByLanguage("abap")).thenReturn(null); fs.addLanguages("java", "php"); - QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs); + QProfileSensor sensor = new QProfileSensor(moduleQProfiles, fs, mock(AnalysisMode.class)); assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); sensor.analyse(project, sensorContext); diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileVerifierTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileVerifierTest.java index 9c5db0f6d6a..61463ae5742 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileVerifierTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/QProfileVerifierTest.java @@ -47,7 +47,7 @@ public class QProfileVerifierTest { @Before public void before() throws Exception { - fs = new DefaultFileSystem(temp.newFolder()); + fs = new DefaultFileSystem(temp.newFolder().toPath()); profiles = mock(ModuleQProfiles.class); QProfile javaProfile = new QProfile().setKey("p1").setName("My Java profile").setLanguage("java"); when(profiles.findByLanguage("java")).thenReturn(javaProfile); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/LanguageVerifierTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/LanguageVerifierTest.java index a60fd596e53..420d0641c36 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/LanguageVerifierTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/LanguageVerifierTest.java @@ -29,8 +29,8 @@ import org.sonar.api.config.Settings; import org.sonar.api.resources.Java; import org.sonar.api.resources.Languages; import org.sonar.api.utils.MessageException; -import org.sonar.batch.languages.DefaultLanguagesReferential; -import org.sonar.batch.languages.LanguagesReferential; +import org.sonar.batch.repository.language.DefaultLanguagesRepository; +import org.sonar.batch.repository.language.LanguagesRepository; import static org.assertj.core.api.Assertions.assertThat; @@ -43,12 +43,12 @@ public class LanguageVerifierTest { public ExpectedException thrown = ExpectedException.none(); private Settings settings = new Settings(); - private LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(Java.INSTANCE)); + private LanguagesRepository languages = new DefaultLanguagesRepository(new Languages(Java.INSTANCE)); private DefaultFileSystem fs; @Before public void prepare() throws Exception { - fs = new DefaultFileSystem(temp.newFolder()); + fs = new DefaultFileSystem(temp.newFolder().toPath()); } @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/AdditionalFilePredicatesTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/AdditionalFilePredicatesTest.java index ff6e76e0857..ad3dfffc491 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/AdditionalFilePredicatesTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/AdditionalFilePredicatesTest.java @@ -26,8 +26,6 @@ import org.sonar.api.batch.fs.FilePredicate; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; -import java.io.File; - import static org.assertj.core.api.Assertions.assertThat; public class AdditionalFilePredicatesTest { @@ -45,38 +43,4 @@ public class AdditionalFilePredicatesTest { inputFile = new DeprecatedDefaultInputFile("struts", "Filter.java"); assertThat(predicate.apply(inputFile)).isFalse(); } - - @Test - public void deprecated_key() throws Exception { - FilePredicate predicate = new AdditionalFilePredicates.DeprecatedKeyPredicate("struts:Action.java"); - - DeprecatedDefaultInputFile inputFile = new DeprecatedDefaultInputFile("struts", "Action.java").setDeprecatedKey("struts:Action.java"); - assertThat(predicate.apply(inputFile)).isTrue(); - - inputFile = new DeprecatedDefaultInputFile("struts", "Filter.java").setDeprecatedKey("struts:Filter.java"); - assertThat(predicate.apply(inputFile)).isFalse(); - } - - @Test - public void absolute_path_of_source_dir() throws Exception { - File dir = temp.newFolder(); - FilePredicate predicate = new AdditionalFilePredicates.SourceDirPredicate(dir.getAbsolutePath()); - - DeprecatedDefaultInputFile inputFile = new DeprecatedDefaultInputFile("struts", "Action.java").setSourceDirAbsolutePath(dir.getAbsolutePath()); - assertThat(predicate.apply(inputFile)).isTrue(); - - inputFile = new DeprecatedDefaultInputFile("struts", "Filter.java").setSourceDirAbsolutePath(temp.newFolder().getAbsolutePath()); - assertThat(predicate.apply(inputFile)).isFalse(); - } - - @Test - public void path_relative_to_source_dir() throws Exception { - FilePredicate predicate = new AdditionalFilePredicates.SourceRelativePathPredicate("foo/Bar.php"); - - DeprecatedDefaultInputFile inputFile = new DeprecatedDefaultInputFile("foo", "src/php/foo/Bar.php").setPathRelativeToSourceDir("foo/Bar.php"); - assertThat(predicate.apply(inputFile)).isTrue(); - - inputFile = new DeprecatedDefaultInputFile("foo", "foo/Bar.php").setPathRelativeToSourceDir("Bar.php"); - assertThat(predicate.apply(inputFile)).isFalse(); - } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ComponentIndexerTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ComponentIndexerTest.java index 702aa2fb068..5960fab5c2d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ComponentIndexerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ComponentIndexerTest.java @@ -42,6 +42,7 @@ import java.io.IOException; import static org.mockito.Matchers.argThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class ComponentIndexerTest { @@ -52,13 +53,17 @@ public class ComponentIndexerTest { private SonarIndex sonarIndex; private AbstractLanguage cobolLanguage; private Project project; + private ModuleFileSystemInitializer initializer; @Before public void prepare() throws IOException { baseDir = temp.newFolder(); - fs = new DefaultFileSystem(baseDir); + fs = new DefaultFileSystem(baseDir.toPath()); sonarIndex = mock(SonarIndex.class); project = new Project("myProject"); + initializer = mock(ModuleFileSystemInitializer.class); + when(initializer.baseDir()).thenReturn(baseDir); + when(initializer.workingDir()).thenReturn(temp.newFolder()); cobolLanguage = new AbstractLanguage("cobol") { @Override public String[] getFileSuffixes() { @@ -69,15 +74,17 @@ public class ComponentIndexerTest { @Test public void should_index_java_files() throws IOException { + Languages languages = new Languages(Java.INSTANCE); + ComponentIndexer indexer = createIndexer(languages); + DefaultModuleFileSystem fs = new DefaultModuleFileSystem(project, null, mock(FileIndexer.class), initializer, indexer); fs.add(newInputFile("src/main/java/foo/bar/Foo.java", "", "foo/bar/Foo.java", "java", false)); fs.add(newInputFile("src/main/java2/foo/bar/Foo.java", "", "foo/bar/Foo.java", "java", false)); fs.add(newInputFile("src/test/java/foo/bar/FooTest.java", "", "foo/bar/FooTest.java", "java", true)); - Languages languages = new Languages(Java.INSTANCE); - ComponentIndexer indexer = createIndexer(languages); - indexer.execute(fs); - verify(sonarIndex).index(org.sonar.api.resources.File.create("src/main/java/foo/bar/Foo.java", "foo/bar/Foo.java", Java.INSTANCE, false)); - verify(sonarIndex).index(org.sonar.api.resources.File.create("src/main/java2/foo/bar/Foo.java", "foo/bar/Foo.java", Java.INSTANCE, false)); + fs.index(); + + verify(sonarIndex).index(org.sonar.api.resources.File.create("src/main/java/foo/bar/Foo.java", Java.INSTANCE, false)); + verify(sonarIndex).index(org.sonar.api.resources.File.create("src/main/java2/foo/bar/Foo.java", Java.INSTANCE, false)); verify(sonarIndex).index(argThat(new ArgumentMatcher<org.sonar.api.resources.File>() { @Override public boolean matches(Object arg0) { @@ -95,25 +102,24 @@ public class ComponentIndexerTest { @Test public void should_index_cobol_files() throws IOException { + Languages languages = new Languages(cobolLanguage); + ComponentIndexer indexer = createIndexer(languages); + DefaultModuleFileSystem fs = new DefaultModuleFileSystem(project, null, mock(FileIndexer.class), initializer, indexer); fs.add(newInputFile("src/foo/bar/Foo.cbl", "", "foo/bar/Foo.cbl", "cobol", false)); fs.add(newInputFile("src2/foo/bar/Foo.cbl", "", "foo/bar/Foo.cbl", "cobol", false)); fs.add(newInputFile("src/test/foo/bar/FooTest.cbl", "", "foo/bar/FooTest.cbl", "cobol", true)); - Languages languages = new Languages(cobolLanguage); - ComponentIndexer indexer = createIndexer(languages); - indexer.execute(fs); + fs.index(); - verify(sonarIndex).index(org.sonar.api.resources.File.create("/src/foo/bar/Foo.cbl", "foo/bar/Foo.cbl", cobolLanguage, false)); - verify(sonarIndex).index(org.sonar.api.resources.File.create("/src2/foo/bar/Foo.cbl", "foo/bar/Foo.cbl", cobolLanguage, false)); - verify(sonarIndex).index(org.sonar.api.resources.File.create("/src/test/foo/bar/FooTest.cbl", "foo/bar/FooTest.cbl", cobolLanguage, true)); + verify(sonarIndex).index(org.sonar.api.resources.File.create("/src/foo/bar/Foo.cbl", cobolLanguage, false)); + verify(sonarIndex).index(org.sonar.api.resources.File.create("/src2/foo/bar/Foo.cbl", cobolLanguage, false)); + verify(sonarIndex).index(org.sonar.api.resources.File.create("/src/test/foo/bar/FooTest.cbl", cobolLanguage, true)); } private DefaultInputFile newInputFile(String path, String content, String sourceRelativePath, String languageKey, boolean unitTest) throws IOException { File file = new File(baseDir, path); FileUtils.write(file, content); return new DeprecatedDefaultInputFile("foo", path) - .setPathRelativeToSourceDir(sourceRelativePath) - .setFile(file) .setLanguage(languageKey) .setType(unitTest ? InputFile.Type.TEST : InputFile.Type.MAIN); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java index 12be0438530..7f954c405a2 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java @@ -135,9 +135,9 @@ public class DefaultModuleFileSystemTest { DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache, new Project("foo"), settings, fileIndexer, initializer, componentIndexer); - File mainFile = temp.newFile(); - InputFile mainInput = new DeprecatedDefaultInputFile("foo", "Main.java").setFile(mainFile).setType(InputFile.Type.MAIN); - InputFile testInput = new DeprecatedDefaultInputFile("foo", "Test.java").setFile(temp.newFile()).setType(InputFile.Type.TEST); + File baseDir = temp.newFile(); + InputFile mainInput = new DeprecatedDefaultInputFile("foo", "Main.java").setModuleBaseDir(baseDir.toPath()).setType(InputFile.Type.MAIN); + InputFile testInput = new DeprecatedDefaultInputFile("foo", "Test.java").setModuleBaseDir(baseDir.toPath()).setType(InputFile.Type.TEST); when(moduleInputFileCache.inputFiles()).thenReturn(Lists.newArrayList(mainInput, testInput)); fs.index(); @@ -145,7 +145,7 @@ public class DefaultModuleFileSystemTest { assertThat(inputFiles).containsOnly(mainInput); Iterable<File> files = fs.files(fs.predicates().hasType(InputFile.Type.MAIN)); - assertThat(files).containsOnly(mainFile); + assertThat(files).containsOnly(new File(baseDir, "Main.java")); } @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DeprecatedFileFiltersTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DeprecatedFileFiltersTest.java index 4066080360b..3235301f031 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DeprecatedFileFiltersTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DeprecatedFileFiltersTest.java @@ -49,7 +49,7 @@ public class DeprecatedFileFiltersTest { public void no_filters() throws Exception { DeprecatedFileFilters filters = new DeprecatedFileFilters(); - InputFile inputFile = new DeprecatedDefaultInputFile("foo", "src/main/java/Foo.java").setFile(temp.newFile()); + InputFile inputFile = new DeprecatedDefaultInputFile("foo", "src/main/java/Foo.java"); assertThat(filters.accept(inputFile)).isTrue(); } @@ -58,11 +58,9 @@ public class DeprecatedFileFiltersTest { DeprecatedFileFilters filters = new DeprecatedFileFilters(new FileSystemFilter[] {filter}); File basedir = temp.newFolder(); - File file = temp.newFile(); + File file = new File(basedir, "src/main/java/Foo.java"); InputFile inputFile = new DeprecatedDefaultInputFile("foo", "src/main/java/Foo.java") - .setSourceDirAbsolutePath(new File(basedir, "src/main/java").getAbsolutePath()) - .setPathRelativeToSourceDir("Foo.java") - .setFile(file) + .setModuleBaseDir(basedir.toPath()) .setType(InputFile.Type.MAIN); when(filter.accept(eq(file), any(DeprecatedFileFilters.DeprecatedContext.class))).thenReturn(false); @@ -73,8 +71,7 @@ public class DeprecatedFileFiltersTest { DeprecatedFileFilters.DeprecatedContext context = argument.getValue(); assertThat(context.canonicalPath()).isEqualTo(FilenameUtils.separatorsToUnix(file.getAbsolutePath())); - assertThat(context.relativeDir()).isEqualTo(new File(basedir, "src/main/java")); - assertThat(context.relativePath()).isEqualTo("Foo.java"); + assertThat(context.relativePath()).isEqualTo("src/main/java/Foo.java"); assertThat(context.type()).isEqualTo(FileType.MAIN); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ExclusionFiltersTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ExclusionFiltersTest.java index 02478075c9c..c5bd36cddf5 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ExclusionFiltersTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ExclusionFiltersTest.java @@ -28,6 +28,7 @@ import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.config.Settings; import org.sonar.api.scan.filesystem.FileExclusions; +import java.io.File; import java.io.IOException; import static org.assertj.core.api.Assertions.assertThat; @@ -43,7 +44,7 @@ public class ExclusionFiltersTest { filter.prepare(); java.io.File file = temp.newFile(); - DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/FooDao.java").setFile(file); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/FooDao.java").setModuleBaseDir(temp.newFolder().toPath()); assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue(); assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isTrue(); } @@ -56,10 +57,10 @@ public class ExclusionFiltersTest { filter.prepare(); java.io.File file = temp.newFile(); - DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/FooDao.java").setFile(file); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/FooDao.java").setModuleBaseDir(temp.newFolder().toPath()); assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue(); - inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/Foo.java").setFile(file); + inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/Foo.java").setModuleBaseDir(temp.newFolder().toPath()); assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isFalse(); } @@ -73,10 +74,10 @@ public class ExclusionFiltersTest { java.io.File file = temp.newFile(); - DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/Foo.java").setFile(file); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/Foo.java").setModuleBaseDir(temp.newFolder().toPath()); assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isFalse(); - inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/FooDto.java").setFile(file); + inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/FooDto.java").setModuleBaseDir(temp.newFolder().toPath()); assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue(); } @@ -91,21 +92,21 @@ public class ExclusionFiltersTest { filter.prepare(); java.io.File file = temp.newFile(); - DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/FooDao.java").setFile(file); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/FooDao.java").setModuleBaseDir(temp.newFolder().toPath()); assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isFalse(); - inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/Foo.java").setFile(file); + inputFile = new DefaultInputFile("foo", "src/main/java/com/mycompany/Foo.java").setModuleBaseDir(temp.newFolder().toPath()); assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue(); // source exclusions do not apply to tests - inputFile = new DefaultInputFile("foo", "src/test/java/com/mycompany/FooDao.java").setFile(file); + inputFile = new DefaultInputFile("foo", "src/test/java/com/mycompany/FooDao.java").setModuleBaseDir(temp.newFolder().toPath()); assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isTrue(); } @Test public void match_exclusion_by_absolute_path() throws IOException { - java.io.File includedFile = temp.newFile("Foo.java"); - java.io.File excludedFile = temp.newFile("Bar.java"); + File baseDir = temp.newFile(); + File excludedFile = new File(baseDir, "src/main/java/org/bar/Bar.java"); Settings settings = new Settings(); settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "src/main/java/**/*"); @@ -114,10 +115,10 @@ public class ExclusionFiltersTest { filter.prepare(); - DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/org/bar/Foo.java").setFile(includedFile); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/main/java/org/bar/Foo.java").setModuleBaseDir(baseDir.toPath()); assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue(); - inputFile = new DefaultInputFile("foo", "src/main/java/org/bar/Bar.java").setFile(excludedFile); + inputFile = new DefaultInputFile("foo", "src/main/java/org/bar/Bar.java").setModuleBaseDir(baseDir.toPath()); assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isFalse(); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java index cbfcb03242b..b34c5d50271 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java @@ -25,12 +25,13 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.AnalysisMode; import java.io.File; -import static org.apache.commons.codec.digest.DigestUtils.md5; import static org.apache.commons.codec.digest.DigestUtils.md5Hex; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; public class FileMetadataTest { @@ -40,17 +41,18 @@ public class FileMetadataTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); + private AnalysisMode mode = mock(AnalysisMode.class); + @Test public void empty_file() throws Exception { File tempFile = temp.newFile(); FileUtils.touch(tempFile); - FileMetadata.Metadata metadata = new FileMetadata().read(tempFile, Charsets.UTF_8); + FileMetadata.Metadata metadata = new FileMetadata(mode).read(tempFile, Charsets.UTF_8); assertThat(metadata.lines).isEqualTo(1); assertThat(metadata.nonBlankLines).isEqualTo(0); assertThat(metadata.hash).isNotEmpty(); assertThat(metadata.originalLineOffsets).containsOnly(0); - assertThat(metadata.lineHashes[0]).isNull(); assertThat(metadata.empty).isTrue(); } @@ -59,14 +61,11 @@ public class FileMetadataTest { File tempFile = temp.newFile(); FileUtils.write(tempFile, "foo\r\nbar\r\nbaz", Charsets.UTF_8, true); - FileMetadata.Metadata metadata = new FileMetadata().read(tempFile, Charsets.UTF_8); + FileMetadata.Metadata metadata = new FileMetadata(mode).read(tempFile, Charsets.UTF_8); assertThat(metadata.lines).isEqualTo(3); assertThat(metadata.nonBlankLines).isEqualTo(3); assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz")); assertThat(metadata.originalLineOffsets).containsOnly(0, 5, 10); - assertThat(metadata.lineHashes[0]).containsOnly(md5("foo")); - assertThat(metadata.lineHashes[1]).containsOnly(md5("bar")); - assertThat(metadata.lineHashes[2]).containsOnly(md5("baz")); assertThat(metadata.empty).isFalse(); } @@ -75,15 +74,11 @@ public class FileMetadataTest { File tempFile = temp.newFile(); FileUtils.write(tempFile, "föo\r\nbà r\r\n\u1D11Ebaßz\r\n", Charsets.UTF_8, true); - FileMetadata.Metadata metadata = new FileMetadata().read(tempFile, Charsets.UTF_8); + FileMetadata.Metadata metadata = new FileMetadata(mode).read(tempFile, Charsets.UTF_8); assertThat(metadata.lines).isEqualTo(4); assertThat(metadata.nonBlankLines).isEqualTo(3); assertThat(metadata.hash).isEqualTo(md5Hex("föo\nbà r\n\u1D11Ebaßz\n")); assertThat(metadata.originalLineOffsets).containsOnly(0, 5, 10, 18); - assertThat(metadata.lineHashes[0]).containsExactly(md5("föo")); - assertThat(metadata.lineHashes[1]).containsExactly(md5("bà r")); - assertThat(metadata.lineHashes[2]).containsExactly(md5("\u1D11Ebaßz")); - assertThat(metadata.lineHashes[3]).isNull(); } @Test @@ -91,15 +86,11 @@ public class FileMetadataTest { File tempFile = temp.newFile(); FileUtils.write(tempFile, "föo\r\nbà r\r\n\u1D11Ebaßz\r\n", Charsets.UTF_16, true); - FileMetadata.Metadata metadata = new FileMetadata().read(tempFile, Charsets.UTF_16); + FileMetadata.Metadata metadata = new FileMetadata(mode).read(tempFile, Charsets.UTF_16); assertThat(metadata.lines).isEqualTo(4); assertThat(metadata.nonBlankLines).isEqualTo(3); assertThat(metadata.hash).isEqualTo(md5Hex("föo\nbà r\n\u1D11Ebaßz\n")); assertThat(metadata.originalLineOffsets).containsOnly(0, 5, 10, 18); - assertThat(metadata.lineHashes[0]).containsExactly(md5("föo")); - assertThat(metadata.lineHashes[1]).containsExactly(md5("bà r")); - assertThat(metadata.lineHashes[2]).containsExactly(md5("\u1D11Ebaßz")); - assertThat(metadata.lineHashes[3]).isNull(); } @Test @@ -107,14 +98,11 @@ public class FileMetadataTest { File tempFile = temp.newFile(); FileUtils.write(tempFile, "foo\nbar\nbaz", Charsets.UTF_8, true); - FileMetadata.Metadata metadata = new FileMetadata().read(tempFile, Charsets.UTF_8); + FileMetadata.Metadata metadata = new FileMetadata(mode).read(tempFile, Charsets.UTF_8); assertThat(metadata.lines).isEqualTo(3); assertThat(metadata.nonBlankLines).isEqualTo(3); assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz")); assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 8); - assertThat(metadata.lineHashes[0]).containsOnly(md5("foo")); - assertThat(metadata.lineHashes[1]).containsOnly(md5("bar")); - assertThat(metadata.lineHashes[2]).containsOnly(md5("baz")); } @Test @@ -122,15 +110,11 @@ public class FileMetadataTest { File tempFile = temp.newFile(); FileUtils.write(tempFile, "foo\nbar\nbaz\n", Charsets.UTF_8, true); - FileMetadata.Metadata metadata = new FileMetadata().read(tempFile, Charsets.UTF_8); + FileMetadata.Metadata metadata = new FileMetadata(mode).read(tempFile, Charsets.UTF_8); assertThat(metadata.lines).isEqualTo(4); assertThat(metadata.nonBlankLines).isEqualTo(3); assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz\n")); assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 8, 12); - assertThat(metadata.lineHashes[0]).containsOnly(md5("foo")); - assertThat(metadata.lineHashes[1]).containsOnly(md5("bar")); - assertThat(metadata.lineHashes[2]).containsOnly(md5("baz")); - assertThat(metadata.lineHashes[3]).isNull(); } @Test @@ -138,15 +122,11 @@ public class FileMetadataTest { File tempFile = temp.newFile(); FileUtils.write(tempFile, "foo\nbar\r\nbaz\n", Charsets.UTF_8, true); - FileMetadata.Metadata metadata = new FileMetadata().read(tempFile, Charsets.UTF_8); + FileMetadata.Metadata metadata = new FileMetadata(mode).read(tempFile, Charsets.UTF_8); assertThat(metadata.lines).isEqualTo(4); assertThat(metadata.nonBlankLines).isEqualTo(3); assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz\n")); assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 9, 13); - assertThat(metadata.lineHashes[0]).containsOnly(md5("foo")); - assertThat(metadata.lineHashes[1]).containsOnly(md5("bar")); - assertThat(metadata.lineHashes[2]).containsOnly(md5("baz")); - assertThat(metadata.lineHashes[3]).isNull(); } @Test @@ -154,15 +134,11 @@ public class FileMetadataTest { File tempFile = temp.newFile(); FileUtils.write(tempFile, "foo\n\n\nbar", Charsets.UTF_8, true); - FileMetadata.Metadata metadata = new FileMetadata().read(tempFile, Charsets.UTF_8); + FileMetadata.Metadata metadata = new FileMetadata(mode).read(tempFile, Charsets.UTF_8); assertThat(metadata.lines).isEqualTo(4); assertThat(metadata.nonBlankLines).isEqualTo(2); assertThat(metadata.hash).isEqualTo(md5Hex("foo\n\n\nbar")); assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 5, 6); - assertThat(metadata.lineHashes[0]).containsOnly(md5("foo")); - assertThat(metadata.lineHashes[1]).isNull(); - assertThat(metadata.lineHashes[2]).isNull(); - assertThat(metadata.lineHashes[3]).containsOnly(md5("bar")); } @Test @@ -170,14 +146,11 @@ public class FileMetadataTest { File tempFile = temp.newFile(); FileUtils.write(tempFile, "foo\nbar\r\nbaz", Charsets.UTF_8, true); - FileMetadata.Metadata metadata = new FileMetadata().read(tempFile, Charsets.UTF_8); + FileMetadata.Metadata metadata = new FileMetadata(mode).read(tempFile, Charsets.UTF_8); assertThat(metadata.lines).isEqualTo(3); assertThat(metadata.nonBlankLines).isEqualTo(3); assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz")); assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 9); - assertThat(metadata.lineHashes[0]).containsOnly(md5("foo")); - assertThat(metadata.lineHashes[1]).containsOnly(md5("bar")); - assertThat(metadata.lineHashes[2]).containsOnly(md5("baz")); } @Test @@ -185,15 +158,11 @@ public class FileMetadataTest { File tempFile = temp.newFile(); FileUtils.write(tempFile, "\nfoo\nbar\r\nbaz", Charsets.UTF_8, true); - FileMetadata.Metadata metadata = new FileMetadata().read(tempFile, Charsets.UTF_8); + FileMetadata.Metadata metadata = new FileMetadata(mode).read(tempFile, Charsets.UTF_8); assertThat(metadata.lines).isEqualTo(4); assertThat(metadata.nonBlankLines).isEqualTo(3); assertThat(metadata.hash).isEqualTo(md5Hex("\nfoo\nbar\nbaz")); assertThat(metadata.originalLineOffsets).containsOnly(0, 1, 5, 10); - assertThat(metadata.lineHashes[0]).isNull(); - assertThat(metadata.lineHashes[1]).containsOnly(md5("foo")); - assertThat(metadata.lineHashes[2]).containsOnly(md5("bar")); - assertThat(metadata.lineHashes[3]).containsOnly(md5("baz")); } @Test @@ -201,14 +170,11 @@ public class FileMetadataTest { File tempFile = temp.newFile(); FileUtils.write(tempFile, "\uFEFFfoo\nbar\r\nbaz", Charsets.UTF_8, true); - FileMetadata.Metadata metadata = new FileMetadata().read(tempFile, Charsets.UTF_8); + FileMetadata.Metadata metadata = new FileMetadata(mode).read(tempFile, Charsets.UTF_8); assertThat(metadata.lines).isEqualTo(3); assertThat(metadata.nonBlankLines).isEqualTo(3); assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz")); assertThat(metadata.originalLineOffsets).containsOnly(0, 4, 9); - assertThat(metadata.lineHashes[0]).containsOnly(md5("foo")); - assertThat(metadata.lineHashes[1]).containsOnly(md5("bar")); - assertThat(metadata.lineHashes[2]).containsOnly(md5("baz")); } @Test @@ -216,13 +182,10 @@ public class FileMetadataTest { File tempFile = temp.newFile(); FileUtils.write(tempFile, " foo\nb ar\r\nbaz \t", Charsets.UTF_8, true); - FileMetadata.Metadata metadata = new FileMetadata().read(tempFile, Charsets.UTF_8); + FileMetadata.Metadata metadata = new FileMetadata(mode).read(tempFile, Charsets.UTF_8); assertThat(metadata.lines).isEqualTo(3); assertThat(metadata.nonBlankLines).isEqualTo(3); assertThat(metadata.hash).isEqualTo(md5Hex(" foo\nb ar\nbaz \t")); - assertThat(metadata.lineHashes[0]).containsOnly(md5("foo")); - assertThat(metadata.lineHashes[1]).containsOnly(md5("bar")); - assertThat(metadata.lineHashes[2]).containsOnly(md5("baz")); } @Test @@ -233,7 +196,7 @@ public class FileMetadataTest { thrown.expect(IllegalStateException.class); thrown.expectMessage("Fail to read file '" + file.getAbsolutePath() + "' with encoding 'UTF-8'"); - new FileMetadata().read(file, Charsets.UTF_8); + new FileMetadata(mode).read(file, Charsets.UTF_8); } @Test @@ -248,9 +211,9 @@ public class FileMetadataTest { File file2 = temp.newFile(); FileUtils.write(file2, "foo\nbar", Charsets.UTF_8, true); - String hash1 = new FileMetadata().read(file1, Charsets.UTF_8).hash; - String hash1a = new FileMetadata().read(file1a, Charsets.UTF_8).hash; - String hash2 = new FileMetadata().read(file2, Charsets.UTF_8).hash; + String hash1 = new FileMetadata(mode).read(file1, Charsets.UTF_8).hash; + String hash1a = new FileMetadata(mode).read(file1a, Charsets.UTF_8).hash; + String hash2 = new FileMetadata(mode).read(file2, Charsets.UTF_8).hash; assertThat(hash1).isEqualTo(hash1a); assertThat(hash1).isNotEqualTo(hash2); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactoryTest.java index e702dbbfa81..2b5b1f4bcca 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactoryTest.java @@ -39,7 +39,7 @@ public class InputFileBuilderFactoryTest { DefaultAnalysisMode analysisMode = mock(DefaultAnalysisMode.class); InputFileBuilderFactory factory = new InputFileBuilderFactory(ProjectDefinition.create().setKey("struts"), pathResolver, langDetectionFactory, - statusDetectionFactory, analysisMode, new Settings()); + statusDetectionFactory, analysisMode, new Settings(), new FileMetadata(analysisMode)); InputFileBuilder builder = factory.create(fs); assertThat(builder.langDetection()).isNotNull(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderTest.java index 36682184b37..df058f13c8f 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderTest.java @@ -32,7 +32,6 @@ import org.sonar.api.utils.PathUtils; import org.sonar.batch.bootstrap.DefaultAnalysisMode; import java.io.File; -import java.util.Arrays; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.any; @@ -67,9 +66,9 @@ public class InputFileBuilderTest { .thenReturn(InputFile.Status.ADDED); InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(), - langDetection, statusDetection, fs, analysisMode, new Settings()); + langDetection, statusDetection, fs, analysisMode, new Settings(), new FileMetadata(analysisMode)); DeprecatedDefaultInputFile inputFile = builder.create(srcFile); - inputFile = builder.complete(inputFile, InputFile.Type.MAIN); + builder.completeAndComputeMetadata(inputFile, InputFile.Type.MAIN); assertThat(inputFile.type()).isEqualTo(InputFile.Type.MAIN); assertThat(inputFile.file()).isEqualTo(srcFile.getAbsoluteFile()); @@ -78,9 +77,6 @@ public class InputFileBuilderTest { assertThat(inputFile.key()).isEqualTo("struts:src/main/java/foo/Bar.java"); assertThat(inputFile.relativePath()).isEqualTo("src/main/java/foo/Bar.java"); assertThat(inputFile.lines()).isEqualTo(1); - assertThat(inputFile.sourceDirAbsolutePath()).isNull(); - assertThat(inputFile.pathRelativeToSourceDir()).isNull(); - assertThat(inputFile.deprecatedKey()).isNull(); } @Test @@ -93,7 +89,7 @@ public class InputFileBuilderTest { when(fs.baseDir()).thenReturn(basedir); InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(), - langDetection, statusDetection, fs, analysisMode, new Settings()); + langDetection, statusDetection, fs, analysisMode, new Settings(), new FileMetadata(analysisMode)); DeprecatedDefaultInputFile inputFile = builder.create(srcFile); assertThat(inputFile).isNull(); @@ -113,69 +109,11 @@ public class InputFileBuilderTest { when(langDetection.language(any(InputFile.class))).thenReturn(null); InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(), - langDetection, statusDetection, fs, analysisMode, new Settings()); + langDetection, statusDetection, fs, analysisMode, new Settings(), new FileMetadata(analysisMode)); DeprecatedDefaultInputFile inputFile = builder.create(srcFile); - inputFile = builder.complete(inputFile, InputFile.Type.MAIN); + InputFileMetadata metadata = builder.completeAndComputeMetadata(inputFile, InputFile.Type.MAIN); - assertThat(inputFile).isNull(); - } - - @Test - public void fill_deprecated_data_of_java_file() throws Exception { - // file system - File basedir = temp.newFolder(); - File srcFile = new File(basedir, "src/main/java/foo/Bar.java"); - FileUtils.touch(srcFile); - FileUtils.write(srcFile, "single line"); - when(fs.baseDir()).thenReturn(basedir); - when(fs.encoding()).thenReturn(Charsets.UTF_8); - File sourceDir = new File(basedir, "src/main/java"); - when(fs.sourceDirs()).thenReturn(Arrays.asList(sourceDir)); - - // lang - when(langDetection.language(any(InputFile.class))).thenReturn("java"); - - // status - when(statusDetection.status("foo", "src/main/java/foo/Bar.java", "6c1d64c0b3555892fe7273e954f6fb5a")) - .thenReturn(InputFile.Status.ADDED); - - InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(), - langDetection, statusDetection, fs, analysisMode, new Settings()); - DeprecatedDefaultInputFile inputFile = builder.create(srcFile); - inputFile = builder.complete(inputFile, InputFile.Type.MAIN); - - assertThat(inputFile.pathRelativeToSourceDir()).isEqualTo("foo/Bar.java"); - assertThat(inputFile.sourceDirAbsolutePath()).isEqualTo(PathUtils.sanitize(sourceDir.getAbsolutePath())); - assertThat(inputFile.deprecatedKey()).isEqualTo("struts:foo.Bar"); + assertThat(metadata).isNull(); } - @Test - public void fill_deprecated_data_of_non_java_file() throws Exception { - // file system - File basedir = temp.newFolder(); - File srcFile = new File(basedir, "src/foo/Bar.php"); - FileUtils.touch(srcFile); - FileUtils.write(srcFile, "single line"); - when(fs.baseDir()).thenReturn(basedir); - when(fs.encoding()).thenReturn(Charsets.UTF_8); - File sourceDir = new File(basedir, "src"); - when(fs.sourceDirs()).thenReturn(Arrays.asList(sourceDir)); - - // lang - when(langDetection.language(any(InputFile.class))).thenReturn("php"); - - // status - when(statusDetection.status("foo", "src/Bar.php", "6c1d64c0b3555892fe7273e954f6fb5a")) - .thenReturn(InputFile.Status.ADDED); - - InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(), - langDetection, statusDetection, fs, analysisMode, new Settings()); - DeprecatedDefaultInputFile inputFile = builder.create(srcFile); - inputFile = builder.complete(inputFile, InputFile.Type.MAIN); - - assertThat(inputFile.pathRelativeToSourceDir()).isEqualTo("foo/Bar.php"); - assertThat(inputFile.sourceDirAbsolutePath()).isEqualTo(PathUtils.sanitize(sourceDir.getAbsolutePath())); - assertThat(inputFile.deprecatedKey()).isEqualTo("struts:foo/Bar.php"); - - } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputPathCacheTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputPathCacheTest.java index 1a38d6e998c..7c943e06f66 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputPathCacheTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputPathCacheTest.java @@ -19,7 +19,7 @@ */ package org.sonar.batch.scan.filesystem; -import org.apache.commons.codec.digest.DigestUtils; +import com.google.common.base.Charsets; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -30,8 +30,6 @@ import org.sonar.api.batch.fs.InputFile.Type; import org.sonar.api.batch.fs.InputPath; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; -import org.sonar.batch.index.Caches; -import org.sonar.batch.index.CachesTest; import static org.assertj.core.api.Assertions.assertThat; @@ -40,59 +38,61 @@ public class InputPathCacheTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - Caches caches; - @Before public void start() throws Exception { - caches = CachesTest.createCacheOnTemp(temp); - caches.start(); } @After public void stop() { - caches.stop(); } @Test public void should_add_input_file() throws Exception { - InputPathCache cache = new InputPathCache(caches); - DefaultInputFile fooFile = new DefaultInputFile("foo", "src/main/java/Foo.java").setFile(temp.newFile("Foo.java")); + InputPathCache cache = new InputPathCache(); + DefaultInputFile fooFile = new DefaultInputFile("foo", "src/main/java/Foo.java").setModuleBaseDir(temp.newFolder().toPath()); cache.put("struts", fooFile); cache.put("struts-core", new DeprecatedDefaultInputFile("foo", "src/main/java/Bar.java") - .setBasedir(temp.newFolder()) - .setDeprecatedKey("foo") - .setSourceDirAbsolutePath("foo") - .setPathRelativeToSourceDir("foo") .setLanguage("bla") .setType(Type.MAIN) .setStatus(Status.ADDED) - .setHash("xyz") .setLines(2) - .setEncoding("UTF-8") - .setOriginalLineOffsets(new long[] {0, 4}) - .setLineHashes(new byte[][] {DigestUtils.md5("foo"), DigestUtils.md5("bar")}) - .setFile(temp.newFile("Bar.java"))); + .setCharset(Charsets.UTF_8) + .setModuleBaseDir(temp.newFolder().toPath())); DefaultInputFile loadedFile = (DefaultInputFile) cache.getFile("struts-core", "src/main/java/Bar.java"); assertThat(loadedFile.relativePath()).isEqualTo("src/main/java/Bar.java"); - assertThat(loadedFile.encoding()).isEqualTo("UTF-8"); - assertThat(loadedFile.originalLineOffsets()).containsOnly(0, 4); - assertThat(loadedFile.lineHashes()[0]).containsOnly(DigestUtils.md5("foo")); - assertThat(loadedFile.lineHashes()[1]).containsOnly(DigestUtils.md5("bar")); + assertThat(loadedFile.charset()).isEqualTo(Charsets.UTF_8); assertThat(cache.filesByModule("struts")).hasSize(1); assertThat(cache.filesByModule("struts-core")).hasSize(1); - assertThat(cache.all()).hasSize(2); - for (InputPath inputPath : cache.all()) { + assertThat(cache.allFiles()).hasSize(2); + for (InputPath inputPath : cache.allFiles()) { assertThat(inputPath.relativePath()).startsWith("src/main/java/"); } cache.remove("struts", fooFile); - assertThat(cache.all()).hasSize(1); + assertThat(cache.allFiles()).hasSize(1); cache.removeModule("struts"); assertThat(cache.filesByModule("struts")).hasSize(0); assertThat(cache.filesByModule("struts-core")).hasSize(1); - assertThat(cache.all()).hasSize(1); + assertThat(cache.allFiles()).hasSize(1); + } + + @Test + public void should_add_input_file_metadata() throws Exception { + InputPathCache cache = new InputPathCache(); + cache.put("struts-core", "src/main/java/Bar.java", new InputFileMetadata() + .setHash("xyz") + .setNonBlankLines(2) + .setEmpty(true) + .setOriginalLineOffsets(new int[] {0, 4})); + + InputFileMetadata loadedFileMetadata = cache.getFileMetadata("struts-core", "src/main/java/Bar.java"); + assertThat(loadedFileMetadata.originalLineOffsets()).containsOnly(0, 4); + assertThat(loadedFileMetadata.hash()).isEqualTo("xyz"); + assertThat(loadedFileMetadata.nonBlankLines()).isEqualTo(2); + assertThat(loadedFileMetadata.isEmpty()).isTrue(); + } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java index af30dcfc706..93b4d05145a 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java @@ -19,19 +19,19 @@ */ package org.sonar.batch.scan.filesystem; +import org.sonar.batch.repository.language.DefaultLanguagesRepository; +import org.sonar.batch.repository.language.LanguagesRepository; + import org.junit.Test; import org.sonar.api.config.Settings; import org.sonar.api.resources.Java; import org.sonar.api.resources.Languages; -import org.sonar.batch.languages.DefaultLanguagesReferential; -import org.sonar.batch.languages.LanguagesReferential; - import static org.assertj.core.api.Assertions.assertThat; public class LanguageDetectionFactoryTest { @Test public void testCreate() throws Exception { - LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(Java.INSTANCE)); + LanguagesRepository languages = new DefaultLanguagesRepository(new Languages(Java.INSTANCE)); LanguageDetectionFactory factory = new LanguageDetectionFactory(new Settings(), languages); LanguageDetection languageDetection = factory.create(); assertThat(languageDetection).isNotNull(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionTest.java index e9155fd9c8a..95d3631eaa5 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionTest.java @@ -30,8 +30,8 @@ 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.languages.DefaultLanguagesReferential; -import org.sonar.batch.languages.LanguagesReferential; +import org.sonar.batch.repository.language.DefaultLanguagesRepository; +import org.sonar.batch.repository.language.LanguagesRepository; import java.io.File; import java.io.IOException; @@ -58,7 +58,7 @@ public class LanguageDetectionTest { @Test public void search_by_file_extension() throws Exception { - LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("java", "java", "jav"), new MockLanguage("cobol", "cbl", "cob"))); + LanguagesRepository languages = new DefaultLanguagesRepository(new Languages(new MockLanguage("java", "java", "jav"), new MockLanguage("cobol", "cbl", "cob"))); LanguageDetection detection = new LanguageDetection(new Settings(), languages); assertThat(detection.language(newInputFile("Foo.java"))).isEqualTo("java"); @@ -76,13 +76,13 @@ public class LanguageDetectionTest { @Test public void should_not_fail_if_no_language() throws Exception { - LanguageDetection detection = spy(new LanguageDetection(new Settings(), new DefaultLanguagesReferential(new Languages()))); + LanguageDetection detection = spy(new LanguageDetection(new Settings(), new DefaultLanguagesRepository(new Languages()))); assertThat(detection.language(newInputFile("Foo.java"))).isNull(); } @Test public void plugin_can_declare_a_file_extension_twice_for_case_sensitivity() throws Exception { - LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("abap", "abap", "ABAP"))); + LanguagesRepository languages = new DefaultLanguagesRepository(new Languages(new MockLanguage("abap", "abap", "ABAP"))); LanguageDetection detection = new LanguageDetection(new Settings(), languages); assertThat(detection.language(newInputFile("abc.abap"))).isEqualTo("abap"); @@ -92,7 +92,7 @@ public class LanguageDetectionTest { public void language_with_no_extension() throws Exception { // abap does not declare any file extensions. // When analyzing an ABAP project, then all source files must be parsed. - LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("abap"))); + LanguagesRepository languages = new DefaultLanguagesRepository(new Languages(new MockLanguage("java", "java"), new MockLanguage("abap"))); // No side-effect on non-ABAP projects LanguageDetection detection = new LanguageDetection(new Settings(), languages); @@ -110,7 +110,7 @@ public class LanguageDetectionTest { @Test public void force_language_using_deprecated_property() throws Exception { - LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php"))); + LanguagesRepository languages = new DefaultLanguagesRepository(new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php"))); Settings settings = new Settings(); settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "java"); @@ -126,7 +126,7 @@ public class LanguageDetectionTest { thrown.expect(MessageException.class); thrown.expectMessage("No language is installed with key 'unknown'. Please update property 'sonar.language'"); - LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php"))); + LanguagesRepository languages = new DefaultLanguagesRepository(new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php"))); Settings settings = new Settings(); settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "unknown"); new LanguageDetection(settings, languages); @@ -134,7 +134,7 @@ public class LanguageDetectionTest { @Test public void fail_if_conflicting_language_suffix() throws Exception { - LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"))); + LanguagesRepository languages = new DefaultLanguagesRepository(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"))); LanguageDetection detection = new LanguageDetection(new Settings(), languages); try { detection.language(newInputFile("abc.xhtml")); @@ -149,7 +149,7 @@ public class LanguageDetectionTest { @Test public void solve_conflict_using_filepattern() throws Exception { - LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"))); + LanguagesRepository languages = new DefaultLanguagesRepository(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"))); Settings settings = new Settings(); settings.setProperty("sonar.lang.patterns.xml", "xml/**"); @@ -161,7 +161,7 @@ public class LanguageDetectionTest { @Test public void fail_if_conflicting_filepattern() throws Exception { - LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("abap", "abap"), new MockLanguage("cobol", "cobol"))); + LanguagesRepository languages = new DefaultLanguagesRepository(new Languages(new MockLanguage("abap", "abap"), new MockLanguage("cobol", "cobol"))); Settings settings = new Settings(); settings.setProperty("sonar.lang.patterns.abap", "*.abap,*.txt"); settings.setProperty("sonar.lang.patterns.cobol", "*.cobol,*.txt"); @@ -183,7 +183,7 @@ public class LanguageDetectionTest { private InputFile newInputFile(String path) throws IOException { File basedir = temp.newFolder(); - return new DefaultInputFile("foo", path).setFile(new File(basedir, path)); + return new DefaultInputFile("foo", path).setModuleBaseDir(basedir.toPath()); } static class MockLanguage implements Language { diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/measure/MeasureCacheTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/measure/MeasureCacheTest.java index ebb97af6492..7aa5c49856d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/measure/MeasureCacheTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/measure/MeasureCacheTest.java @@ -233,9 +233,9 @@ public class MeasureCacheTest { @Test public void should_get_measures() throws Exception { Project p = new Project("struts"); - Resource dir = new Directory("foo/bar").setEffectiveKey("struts:foo/bar"); - Resource file1 = new File("foo/bar/File1.txt").setEffectiveKey("struts:foo/bar/File1.txt"); - Resource file2 = new File("foo/bar/File2.txt").setEffectiveKey("struts:foo/bar/File2.txt"); + Resource dir = Directory.create("foo/bar").setEffectiveKey("struts:foo/bar"); + Resource file1 = Directory.create("foo/bar/File1.txt").setEffectiveKey("struts:foo/bar/File1.txt"); + Resource file2 = Directory.create("foo/bar/File2.txt").setEffectiveKey("struts:foo/bar/File2.txt"); assertThat(cache.entries()).hasSize(0); @@ -271,7 +271,7 @@ public class MeasureCacheTest { @Test public void test_measure_coder() throws Exception { - Resource file1 = new File("foo/bar/File1.txt").setEffectiveKey("struts:foo/bar/File1.txt"); + Resource file1 = File.create("foo/bar/File1.txt").setEffectiveKey("struts:foo/bar/File1.txt"); Measure measure = new Measure(CoreMetrics.NCLOC, 1.786, 5); cache.put(file1, measure); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java index 786d5a7f040..1e61491a8c1 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java @@ -27,8 +27,8 @@ import org.junit.Before; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.skyscreamer.jsonassert.JSONAssert; +import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputPath; import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.batch.fs.internal.DefaultInputDir; import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; @@ -42,9 +42,9 @@ import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.api.rule.RuleKey; import org.sonar.batch.issue.IssueCache; +import org.sonar.batch.repository.user.User; +import org.sonar.batch.repository.user.UserRepository; import org.sonar.batch.scan.filesystem.InputPathCache; -import org.sonar.batch.user.User; -import org.sonar.batch.user.UserRepository; import java.io.File; import java.io.IOException; @@ -77,7 +77,7 @@ public class JSONReportTest { @Before public void before() throws Exception { - fs = new DefaultFileSystem(temp.newFolder()); + fs = new DefaultFileSystem(temp.newFolder().toPath()); SIMPLE_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+02:00")); when(resource.getEffectiveKey()).thenReturn("Action.java"); when(server.getVersion()).thenReturn("3.6"); @@ -86,7 +86,8 @@ public class JSONReportTest { DeprecatedDefaultInputFile inputFile = new DeprecatedDefaultInputFile("struts", "src/main/java/org/apache/struts/Action.java"); inputFile.setStatus(InputFile.Status.CHANGED); InputPathCache fileCache = mock(InputPathCache.class); - when(fileCache.all()).thenReturn(Arrays.<InputPath>asList(inputDir, inputFile)); + when(fileCache.allFiles()).thenReturn(Arrays.<InputFile>asList(inputFile)); + when(fileCache.allDirs()).thenReturn(Arrays.<InputDir>asList(inputDir)); Project rootModule = new Project("struts"); Project moduleA = new Project("struts-core"); moduleA.setParent(rootModule).setPath("core"); diff --git a/sonar-batch/src/test/java/org/sonar/batch/sensor/AnalyzerOptimizerTest.java b/sonar-batch/src/test/java/org/sonar/batch/sensor/AnalyzerOptimizerTest.java index d0cf02f65a3..1850fe2b001 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/sensor/AnalyzerOptimizerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/sensor/AnalyzerOptimizerTest.java @@ -53,7 +53,7 @@ public class AnalyzerOptimizerTest { @Before public void prepare() throws Exception { - fs = new DefaultFileSystem(temp.newFolder()); + fs = new DefaultFileSystem(temp.newFolder().toPath()); settings = new Settings(); analysisMode = mock(AnalysisMode.class); optimizer = new AnalyzerOptimizer(fs, new ActiveRulesBuilder().build(), settings, analysisMode); diff --git a/sonar-batch/src/test/java/org/sonar/batch/sensor/DefaultSensorContextTest.java b/sonar-batch/src/test/java/org/sonar/batch/sensor/DefaultSensorContextTest.java index e6d24b0643f..1812fde5cf4 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/sensor/DefaultSensorContextTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/sensor/DefaultSensorContextTest.java @@ -58,7 +58,7 @@ public class DefaultSensorContextTest { @Before public void prepare() throws Exception { activeRules = new ActiveRulesBuilder().build(); - fs = new DefaultFileSystem(temp.newFolder()); + fs = new DefaultFileSystem(temp.newFolder().toPath()); MetricFinder metricFinder = mock(MetricFinder.class); when(metricFinder.findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC); when(metricFinder.findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION); diff --git a/sonar-batch/src/test/java/org/sonar/batch/sensor/DefaultSensorStorageTest.java b/sonar-batch/src/test/java/org/sonar/batch/sensor/DefaultSensorStorageTest.java index 658f48e41fd..506bcd328e1 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/sensor/DefaultSensorStorageTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/sensor/DefaultSensorStorageTest.java @@ -84,7 +84,7 @@ public class DefaultSensorStorageTest { @Before public void prepare() throws Exception { activeRules = new ActiveRulesBuilder().build(); - fs = new DefaultFileSystem(temp.newFolder()); + fs = new DefaultFileSystem(temp.newFolder().toPath()); MetricFinder metricFinder = mock(MetricFinder.class); when(metricFinder.findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC); when(metricFinder.findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION); @@ -279,7 +279,7 @@ public class DefaultSensorStorageTest { when(sonarIndex.getEdge(foo, bar)).thenReturn(new Dependency(foo, bar)); thrown.expect(IllegalStateException.class); - thrown.expectMessage("Dependency between [moduleKey=foo, relative=src/Foo.java, abs=null] and [moduleKey=foo, relative=src/Bar.java, abs=null] was already saved."); + thrown.expectMessage("Dependency between [moduleKey=foo, relative=src/Foo.java, basedir=null] and [moduleKey=foo, relative=src/Bar.java, basedir=null] was already saved."); sensorStorage.store(new DefaultDependency() .from(new DefaultInputFile("foo", "src/Foo.java").setType(Type.MAIN)) diff --git a/sonar-batch/src/test/java/org/sonar/batch/sensor/coverage/CoverageExclusionsTest.java b/sonar-batch/src/test/java/org/sonar/batch/sensor/coverage/CoverageExclusionsTest.java index ec592a7962e..0e2efc2782a 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/sensor/coverage/CoverageExclusionsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/sensor/coverage/CoverageExclusionsTest.java @@ -54,7 +54,7 @@ public class CoverageExclusionsTest { @Test public void shouldFilterFileBasedOnPattern() { - Resource resource = File.create("src/org/polop/File.php", "org/polop/File.php", null, false); + Resource resource = File.create("src/org/polop/File.php", null, false); Measure coverageMeasure = mock(Measure.class); when(coverageMeasure.getMetric()).thenReturn(CoreMetrics.LINES_TO_COVER); @@ -65,7 +65,7 @@ public class CoverageExclusionsTest { @Test public void shouldNotFilterFileBasedOnPattern() { - Resource resource = File.create("src/org/polop/File.php", "org/polop/File.php", null, false); + Resource resource = File.create("src/org/polop/File.php", null, false); Measure coverageMeasure = mock(Measure.class); when(coverageMeasure.getMetric()).thenReturn(CoreMetrics.COVERAGE); diff --git a/sonar-batch/src/test/java/org/sonar/batch/source/CodeColorizersTest.java b/sonar-batch/src/test/java/org/sonar/batch/source/CodeColorizersTest.java index 58f1d3f86c8..a7486ad376b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/source/CodeColorizersTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/source/CodeColorizersTest.java @@ -19,6 +19,7 @@ */ package org.sonar.batch.source; +import com.google.common.base.Charsets; import com.google.common.collect.ImmutableList; import org.apache.commons.io.FileUtils; import org.junit.Rule; @@ -52,7 +53,7 @@ public class CodeColorizersTest { File jsFile = new File(this.getClass().getResource("CodeColorizersTest/Person.js").toURI()); - SyntaxHighlightingData syntaxHighlighting = codeColorizers.toSyntaxHighlighting(jsFile, "UTF-8", "js"); + SyntaxHighlightingData syntaxHighlighting = codeColorizers.toSyntaxHighlighting(jsFile, Charsets.UTF_8, "js"); assertThat(syntaxHighlighting.writeString()).isEqualTo(HIGHLIGHTING_JS); @@ -67,7 +68,7 @@ public class CodeColorizersTest { File jsFile = new File(this.getClass().getResource("CodeColorizersTest/Person.js").toURI()); FileUtils.write(fileWithBom, FileUtils.readFileToString(jsFile), "UTF-8", true); - SyntaxHighlightingData syntaxHighlighting = codeColorizers.toSyntaxHighlighting(fileWithBom, "UTF-8", "js"); + SyntaxHighlightingData syntaxHighlighting = codeColorizers.toSyntaxHighlighting(fileWithBom, Charsets.UTF_8, "js"); assertThat(syntaxHighlighting.writeString()).isEqualTo(HIGHLIGHTING_JS); } @@ -78,7 +79,7 @@ public class CodeColorizersTest { File javaFile = new File(this.getClass().getResource("CodeColorizersTest/Person.java").toURI()); - SyntaxHighlightingData syntaxHighlighting = codeColorizers.toSyntaxHighlighting(javaFile, "UTF-8", "java"); + SyntaxHighlightingData syntaxHighlighting = codeColorizers.toSyntaxHighlighting(javaFile, Charsets.UTF_8, "java"); assertThat(syntaxHighlighting.writeString()).isEqualTo(HIGHLIGHTING_JAVA); diff --git a/sonar-batch/src/test/java/org/sonar/batch/source/HighlightableBuilderTest.java b/sonar-batch/src/test/java/org/sonar/batch/source/HighlightableBuilderTest.java index 4a26c3b4bbf..e2adf2dd0db 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/source/HighlightableBuilderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/source/HighlightableBuilderTest.java @@ -37,7 +37,7 @@ public class HighlightableBuilderTest { @Test public void should_load_default_perspective() throws Exception { - Resource file = new File("foo.c").setEffectiveKey("myproject:path/to/foo.c"); + Resource file = File.create("foo.c").setEffectiveKey("myproject:path/to/foo.c"); Component component = new ResourceComponent(file); HighlightableBuilder builder = new HighlightableBuilder(cache); diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByDateTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByDateTest/shared.xml index d794c3a2422..d794c3a2422 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByDateTest/shared.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByDateTest/shared.xml diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByDaysTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByDaysTest/shared.xml index 119f7399b4d..119f7399b4d 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByDaysTest/shared.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByDaysTest/shared.xml diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByDaysTest/shouldNotFindSelf.xml b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByDaysTest/shouldNotFindSelf.xml index 166f7c18ea8..166f7c18ea8 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByDaysTest/shouldNotFindSelf.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByDaysTest/shouldNotFindSelf.xml diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest/shouldFindPreviousAnalysis.xml b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousAnalysisTest/shouldFindPreviousAnalysis.xml index eb5136dc808..eb5136dc808 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest/shouldFindPreviousAnalysis.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousAnalysisTest/shouldFindPreviousAnalysis.xml diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest/shouldNotFindPreviousAnalysis.xml b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousAnalysisTest/shouldNotFindPreviousAnalysis.xml index 5809598e5d7..5809598e5d7 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousAnalysisTest/shouldNotFindPreviousAnalysis.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousAnalysisTest/shouldNotFindPreviousAnalysis.xml diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/no-previous-version.xml b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousVersionTest/no-previous-version.xml index 2f01f86b989..2f01f86b989 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/no-previous-version.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousVersionTest/no-previous-version.xml diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version-deleted.xml b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version-deleted.xml index d990e648414..d990e648414 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version-deleted.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version-deleted.xml diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version.xml b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version.xml index 5ecbc2fa959..5ecbc2fa959 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByPreviousVersionTest/with-previous-version.xml diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByVersionTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByVersionTest/shared.xml index 289a362f781..289a362f781 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/components/PastSnapshotFinderByVersionTest/shared.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PastSnapshotFinderByVersionTest/shared.xml diff --git a/sonar-batch/src/test/resources/org/sonar/batch/components/PeriodsDefinitionTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PeriodsDefinitionTest/shared.xml index c8796e2bb80..c8796e2bb80 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/components/PeriodsDefinitionTest/shared.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/deprecated/components/PeriodsDefinitionTest/shared.xml diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml index bed6ddbad3d..e168b6bc85d 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml @@ -31,7 +31,7 @@ enabled="true" language="[null]" copy_resource_id="[null]" person_id="[null]" path="src/main/java/org" deprecated_kee="[null]" /> <projects id="1006" scope="FIL" qualifier="FIL" kee="b1:src/main/java/org/Foo.java" root_id="1004" - name="src/main/java/org/Foo.java" long_name="src/main/java/org/Foo.java" description="[null]" + name="Foo.java" long_name="src/main/java/org/Foo.java" description="[null]" enabled="true" language="[null]" copy_resource_id="[null]" person_id="[null]" path="src/main/java/org/Foo.java" deprecated_kee="[null]" /> <snapshots purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]" period2_mode="[null]" period2_param="[null]" period2_date="[null]" period3_mode="[null]" period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]" period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]" id="3001" project_id="1001" root_project_id="1001" parent_snapshot_id="[null]" root_snapshot_id="[null]" diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProjectAndLibrary-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProjectAndLibrary-result.xml index 86b94e27622..f475284edcb 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProjectAndLibrary-result.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProjectAndLibrary-result.xml @@ -31,7 +31,7 @@ enabled="true" language="[null]" copy_resource_id="[null]" person_id="[null]" path="src/main/java/org" deprecated_kee="[null]" /> <projects id="1006" scope="FIL" qualifier="FIL" kee="b1:src/main/java/org/Foo.java" root_id="1004" - name="src/main/java/org/Foo.java" long_name="src/main/java/org/Foo.java" description="[null]" + name="Foo.java" long_name="src/main/java/org/Foo.java" description="[null]" enabled="true" language="[null]" copy_resource_id="[null]" person_id="[null]" path="src/main/java/org/Foo.java" deprecated_kee="[null]" /> <snapshots purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]" period2_mode="[null]" period2_param="[null]" period2_date="[null]" period3_mode="[null]" period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]" period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]" id="3001" project_id="1001" root_project_id="1001" parent_snapshot_id="[null]" root_snapshot_id="[null]" |