3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.analysis;
22 import java.util.Date;
24 import java.util.Optional;
25 import javax.annotation.CheckForNull;
26 import javax.annotation.Nullable;
27 import org.junit.rules.ExternalResource;
28 import org.sonar.db.component.BranchType;
29 import org.sonar.db.organization.OrganizationDto;
30 import org.sonar.server.computation.util.InitializedProperty;
31 import org.sonar.server.qualityprofile.QualityProfile;
33 import static com.google.common.base.Preconditions.checkNotNull;
34 import static com.google.common.base.Preconditions.checkState;
35 import static java.util.Objects.requireNonNull;
37 public class AnalysisMetadataHolderRule extends ExternalResource implements MutableAnalysisMetadataHolder {
39 private final InitializedProperty<Organization> organization = new InitializedProperty<>();
41 private final InitializedProperty<String> uuid = new InitializedProperty<>();
43 private final InitializedProperty<Long> analysisDate = new InitializedProperty<>();
45 private final InitializedProperty<Boolean> incremental = new InitializedProperty<>();
47 private final InitializedProperty<Analysis> baseAnalysis = new InitializedProperty<>();
49 private final InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
51 private final InitializedProperty<Branch> branch = new InitializedProperty<>();
53 private final InitializedProperty<Project> project = new InitializedProperty<>();
55 private final InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
57 private final InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
59 private final InitializedProperty<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
62 public AnalysisMetadataHolderRule setOrganization(Organization organization) {
63 requireNonNull(organization, "organization can't be null");
64 this.organization.setProperty(organization);
68 public AnalysisMetadataHolderRule setOrganizationUuid(String uuid) {
69 requireNonNull(uuid, "organization uuid can't be null");
70 this.organization.setProperty(Organization.from(new OrganizationDto().setUuid(uuid).setKey("key_" + uuid).setName("name_" + uuid)));
75 public Organization getOrganization() {
76 checkState(organization.isInitialized(), "Organization has not been set");
77 return this.organization.getProperty();
81 public AnalysisMetadataHolderRule setUuid(String s) {
82 checkNotNull(s, "UUID must not be null");
83 this.uuid.setProperty(s);
88 public String getUuid() {
89 checkState(uuid.isInitialized(), "Analysis UUID has not been set");
90 return this.uuid.getProperty();
93 public AnalysisMetadataHolderRule setAnalysisDate(Date date) {
94 checkNotNull(date, "Date must not be null");
95 this.analysisDate.setProperty(date.getTime());
100 public AnalysisMetadataHolderRule setAnalysisDate(long date) {
101 checkNotNull(date, "Date must not be null");
102 this.analysisDate.setProperty(date);
107 public long getAnalysisDate() {
108 checkState(analysisDate.isInitialized(), "Analysis date has not been set");
109 return this.analysisDate.getProperty();
113 public boolean hasAnalysisDateBeenSet() {
114 return analysisDate.isInitialized();
118 public boolean isFirstAnalysis() {
119 return getBaseAnalysis() == null;
123 public AnalysisMetadataHolderRule setBaseAnalysis(@Nullable Analysis baseAnalysis) {
124 this.baseAnalysis.setProperty(baseAnalysis);
130 public Analysis getBaseAnalysis() {
131 checkState(baseAnalysis.isInitialized(), "Base analysis has not been set");
132 return baseAnalysis.getProperty();
136 public AnalysisMetadataHolderRule setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
137 this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
142 public boolean isCrossProjectDuplicationEnabled() {
143 checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
144 return crossProjectDuplicationEnabled.getProperty();
148 public AnalysisMetadataHolderRule setBranch(@Nullable Branch branch) {
149 this.branch.setProperty(branch);
154 public Optional<Branch> getBranch() {
155 checkState(branch.isInitialized(), "Branch has not been set");
156 return Optional.ofNullable(branch.getProperty());
160 public AnalysisMetadataHolderRule setProject(Project p) {
161 this.project.setProperty(p);
166 public Project getProject() {
167 checkState(project.isInitialized(), "Project has not been set");
168 return project.getProperty();
172 public AnalysisMetadataHolderRule setRootComponentRef(int rootComponentRef) {
173 this.rootComponentRef.setProperty(rootComponentRef);
178 public int getRootComponentRef() {
179 checkState(rootComponentRef.isInitialized(), "Root component ref has not been set");
180 return rootComponentRef.getProperty();
184 public AnalysisMetadataHolderRule setQProfilesByLanguage(Map<String, QualityProfile> qProfilesPerLanguage) {
185 this.qProfilesPerLanguage.setProperty(qProfilesPerLanguage);
190 public Map<String, QualityProfile> getQProfilesByLanguage() {
191 checkState(qProfilesPerLanguage.isInitialized(), "QProfile per language has not been set");
192 return qProfilesPerLanguage.getProperty();
196 public AnalysisMetadataHolderRule setScannerPluginsByKey(Map<String, ScannerPlugin> plugins) {
197 this.pluginsByKey.setProperty(plugins);
202 public Map<String, ScannerPlugin> getScannerPluginsByKey() {
203 checkState(pluginsByKey.isInitialized(), "Plugins per key has not been set");
204 return pluginsByKey.getProperty();
208 public boolean isIncrementalAnalysis() {
209 checkState(incremental.isInitialized(), "Incremental mode flag has not been set");
210 return incremental.getProperty();
214 public AnalysisMetadataHolderRule setIncrementalAnalysis(boolean isIncrementalAnalysis) {
215 this.incremental.setProperty(isIncrementalAnalysis);
220 public boolean isShortLivingBranch() {
221 Branch property = this.branch.getProperty();
222 return property != null && property.getType() == BranchType.SHORT;