private InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
@Override
- public void setAnalysisDate(Date date) {
+ public MutableAnalysisMetadataHolder setAnalysisDate(Date date) {
checkNotNull(date, "Date must not be null");
checkState(!analysisDate.isInitialized(), "Analysis date has already been set");
this.analysisDate.setProperty(date.getTime());
+ return this;
}
@Override
}
@Override
- public void setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot) {
+ public MutableAnalysisMetadataHolder setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot) {
checkState(!this.baseProjectSnapshot.isInitialized(), "Base project snapshot has already been set");
this.baseProjectSnapshot.setProperty(baseProjectSnapshot);
+ return this;
}
@Override
}
@Override
- public void setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
+ public MutableAnalysisMetadataHolder setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
checkState(!this.crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has already been set");
this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
+ return this;
}
@Override
}
@Override
- public void setBranch(@Nullable String branch) {
+ public MutableAnalysisMetadataHolder setBranch(@Nullable String branch) {
checkState(!this.branch.isInitialized(), "Branch has already been set");
this.branch.setProperty(branch);
+ return this;
}
@Override
}
@Override
- public void setRootComponentRef(int rootComponentRef) {
+ public MutableAnalysisMetadataHolder setRootComponentRef(int rootComponentRef) {
checkState(!this.rootComponentRef.isInitialized(), "Root component ref has already been set");
this.rootComponentRef.setProperty(rootComponentRef);
+ return this;
}
@Override
* @throws NullPointerException if specified date is {@code null}
* @throws IllegalStateException if the analysis date has already been set
*/
- void setAnalysisDate(Date date);
+ MutableAnalysisMetadataHolder setAnalysisDate(Date date);
/**
* @throws IllegalStateException if baseProjectSnapshot has already been set
*/
- void setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot);
+ MutableAnalysisMetadataHolder setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot);
/**
* @throws IllegalStateException if cross project duplication flag has already been set
*/
- void setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled);
+ MutableAnalysisMetadataHolder setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled);
/**
* @throws IllegalStateException if branch has already been set
*/
- void setBranch(@Nullable String branch);
+ MutableAnalysisMetadataHolder setBranch(@Nullable String branch);
/**
* @throws IllegalStateException if root component ref has already been set
*/
- void setRootComponentRef(int rootComponentRef);
+ MutableAnalysisMetadataHolder setRootComponentRef(int rootComponentRef);
}
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
-import java.util.Date;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@Override
public void execute() {
- analysisMetadataHolder.setAnalysisDate(new Date(reportReader.readMetadata().getAnalysisDate()));
- BatchReport.Metadata reportMetadata = reportReader.readMetadata();
- String branch = reportMetadata.hasBranch() ? reportMetadata.getBranch() : null;
- BatchReport.Component reportProject = reportReader.readComponent(reportMetadata.getRootComponentRef());
+ String branch = analysisMetadataHolder.getBranch();
+ BatchReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
UuidFactory uuidFactory = new UuidFactory(dbClient, moduleKey(reportProject, branch));
Component project = new ComponentRootBuilder(reportProject, uuidFactory, branch).build();
treeRootHolder.setRoot(project);
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.computation.step;
+
+import java.util.Date;
+import org.sonar.batch.protocol.output.BatchReport;
+import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolder;
+import org.sonar.server.computation.batch.BatchReportReader;
+
+/**
+ * Feed analysis metadata holder with metadata from the analysis report.
+ */
+public class LoadReportAnalysisMetadataHolderStep implements ComputationStep {
+
+ private final BatchReportReader reportReader;
+ private final MutableAnalysisMetadataHolder mutableAnalysisMetadataHolder;
+
+ public LoadReportAnalysisMetadataHolderStep(BatchReportReader reportReader, MutableAnalysisMetadataHolder mutableAnalysisMetadataHolder) {
+ this.reportReader = reportReader;
+ this.mutableAnalysisMetadataHolder = mutableAnalysisMetadataHolder;
+ }
+
+ @Override
+ public void execute() {
+ BatchReport.Metadata reportMetadata = reportReader.readMetadata();
+ mutableAnalysisMetadataHolder.setRootComponentRef(reportMetadata.getRootComponentRef());
+ mutableAnalysisMetadataHolder.setBranch(reportMetadata.hasBranch() ? reportMetadata.getBranch() : null);
+ mutableAnalysisMetadataHolder.setAnalysisDate(new Date(reportMetadata.getAnalysisDate()));
+ mutableAnalysisMetadataHolder.setCrossProjectDuplicationEnabled(reportMetadata.hasCrossProjectDuplicationActivated() && reportMetadata.getCrossProjectDuplicationActivated());
+ }
+
+ @Override
+ public String getDescription() {
+ return "Load analysis metadata";
+ }
+}
LogScannerContextStep.class,
// Builds Component tree
+ LoadReportAnalysisMetadataHolderStep.class,
BuildComponentTreeStep.class,
ValidateProjectStep.class,
private AnalysisMetadataHolderImpl delegate = new AnalysisMetadataHolderImpl();
@Override
- protected void before() throws Throwable {
+ protected void after() {
delegate = new AnalysisMetadataHolderImpl();
}
return delegate.getAnalysisDate();
}
- @Override
- public void setAnalysisDate(Date date) {
+ public MutableAnalysisMetadataHolderRule setAnalysisDate(Date date) {
delegate.setAnalysisDate(date);
+ return this;
}
@Override
}
@Override
- public void setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot) {
+ public MutableAnalysisMetadataHolderRule setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot) {
delegate.setBaseProjectSnapshot(baseProjectSnapshot);
+ return this;
}
@Override
}
@Override
- public void setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
+ public MutableAnalysisMetadataHolderRule setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
delegate.setCrossProjectDuplicationEnabled(isCrossProjectDuplicationEnabled);
+ return this;
}
@Override
}
@Override
- public void setBranch(@Nullable String branch) {
+ public MutableAnalysisMetadataHolderRule setBranch(@Nullable String branch) {
delegate.setBranch(branch);
+ return this;
}
@Override
- public void setRootComponentRef(int rootComponentRef) {
+ public MutableAnalysisMetadataHolderRule setRootComponentRef(int rootComponentRef) {
delegate.setRootComponentRef(rootComponentRef);
+ return this;
}
@Override
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.sonar.api.utils.System2;
import org.sonar.batch.protocol.Constants;
import org.sonar.batch.protocol.output.BatchReport;
-import org.sonar.batch.protocol.output.BatchReport.Metadata;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
+import org.sonar.server.computation.analysis.AnalysisMetadataHolderImpl;
+import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolder;
import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolderRule;
import org.sonar.server.computation.batch.BatchReportReaderRule;
import org.sonar.server.computation.component.Component;
static final String REPORT_DIR_KEY_2 = "src/main/java/dir2";
static final String REPORT_FILE_KEY_2 = "src/main/java/dir2/File2.java";
+ static final Date ANALYSIS_DATE = new Date();
+
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
public MutableTreeRootHolderRule treeRootHolder = new MutableTreeRootHolderRule();
@Rule
- public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
-
- Date someDate = new Date();
+ public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule()
+ .setRootComponentRef(ROOT_REF)
+ .setAnalysisDate(ANALYSIS_DATE)
+ .setBranch(null);
DbClient dbClient = dbTester.getDbClient();
BuildComponentTreeStep underTest = new BuildComponentTreeStep(dbClient, reportReader, treeRootHolder, analysisMetadataHolder);
- @Before
- public void setUp() {
- reportReader.setMetadata(Metadata.newBuilder()
- .setRootComponentRef(ROOT_REF)
- .setAnalysisDate(someDate.getTime())
- .build());
- }
-
@Test(expected = NullPointerException.class)
public void fails_if_root_component_does_not_exist_in_reportReader() {
underTest.execute();
assertThat(root.getType()).isEqualTo(Component.Type.valueOf(componentType.name()));
assertThat(root.getReportAttributes().getRef()).isEqualTo(ROOT_REF);
assertThat(root.getChildren()).isEmpty();
-
- assertThat(analysisMetadataHolder.getAnalysisDate().getTime()).isEqualTo(someDate.getTime());
}
@Test
Component dir2 = module.getChildren().get(1);
verifyComponent(dir2, Component.Type.DIRECTORY, DIR_REF_2, 1);
verifyComponent(dir2.getChildren().iterator().next(), Component.Type.FILE, FILE_3_REF, 0);
-
- assertThat(analysisMetadataHolder.getAnalysisDate().getTime()).isEqualTo(someDate.getTime());
}
@Test
@Test
public void use_branch_to_generate_keys() {
- reportReader.setMetadata(BatchReport.Metadata.newBuilder()
+ MutableAnalysisMetadataHolder analysisMetadataHolder = new AnalysisMetadataHolderImpl()
.setRootComponentRef(ROOT_REF)
- .setBranch("origin/master")
- .setProjectKey("")
- .build());
+ .setAnalysisDate(ANALYSIS_DATE)
+ .setBranch("origin/master");
+
+ BuildComponentTreeStep underTest = new BuildComponentTreeStep(dbClient, reportReader, treeRootHolder, analysisMetadataHolder);
reportReader.putComponent(componentWithKey(ROOT_REF, PROJECT, REPORT_PROJECT_KEY, MODULE_REF));
reportReader.putComponent(componentWithKey(MODULE_REF, MODULE, REPORT_MODULE_KEY, DIR_REF_1));
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.computation.step;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.batch.protocol.output.BatchReport;
+import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolderRule;
+import org.sonar.server.computation.batch.BatchReportReaderRule;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class LoadReportAnalysisMetadataHolderStepTest {
+
+ static long ANALYSIS_DATE = 123456789L;
+
+ static String BRANCH = "origin/master";
+
+ @Rule
+ public BatchReportReaderRule reportReader = new BatchReportReaderRule();
+
+ @Rule
+ public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
+
+ ComputationStep underTest = new LoadReportAnalysisMetadataHolderStep(reportReader, analysisMetadataHolder);
+
+ @Test
+ public void set_root_component_ref() throws Exception {
+ reportReader.setMetadata(
+ BatchReport.Metadata.newBuilder()
+ .setRootComponentRef(1)
+ .build());
+
+ underTest.execute();
+
+ assertThat(analysisMetadataHolder.getRootComponentRef()).isEqualTo(1);
+ }
+
+ @Test
+ public void set_analysis_date() throws Exception {
+ reportReader.setMetadata(
+ BatchReport.Metadata.newBuilder()
+ .setAnalysisDate(ANALYSIS_DATE)
+ .build());
+
+ underTest.execute();
+
+ assertThat(analysisMetadataHolder.getAnalysisDate()).isEqualTo(ANALYSIS_DATE);
+ }
+
+ @Test
+ public void set_branch() throws Exception {
+ reportReader.setMetadata(
+ BatchReport.Metadata.newBuilder()
+ .setBranch(BRANCH)
+ .build());
+
+ underTest.execute();
+
+ assertThat(analysisMetadataHolder.getBranch()).isEqualTo(BRANCH);
+ }
+
+ @Test
+ public void set_null_branch_when_nothing_in_the_report() throws Exception {
+ reportReader.setMetadata(
+ BatchReport.Metadata.newBuilder()
+ .build());
+
+ underTest.execute();
+
+ assertThat(analysisMetadataHolder.getBranch()).isNull();
+ }
+
+ @Test
+ public void set_cross_project_duplication_to_true() throws Exception {
+ reportReader.setMetadata(
+ BatchReport.Metadata.newBuilder()
+ .setCrossProjectDuplicationActivated(true)
+ .build());
+
+ underTest.execute();
+
+ assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(true);
+ }
+
+ @Test
+ public void set_cross_project_duplication_to_false() throws Exception {
+ reportReader.setMetadata(
+ BatchReport.Metadata.newBuilder()
+ .setCrossProjectDuplicationActivated(false)
+ .build());
+
+ underTest.execute();
+
+ assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(false);
+ }
+
+ @Test
+ public void set_cross_project_duplication_to_false_when_nothing_in_the_report() throws Exception {
+ reportReader.setMetadata(
+ BatchReport.Metadata.newBuilder()
+ .build());
+
+ underTest.execute();
+
+ assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(false);
+ }
+
+}