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<Analysis> baseAnalysis = new InitializedProperty<>();
47 private final InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
49 private final InitializedProperty<Branch> branch = new InitializedProperty<>();
51 private final InitializedProperty<Project> project = new InitializedProperty<>();
53 private final InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
55 private final InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
57 private final InitializedProperty<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
60 public AnalysisMetadataHolderRule setOrganization(Organization organization) {
61 requireNonNull(organization, "organization can't be null");
62 this.organization.setProperty(organization);
66 public AnalysisMetadataHolderRule setOrganizationUuid(String uuid) {
67 requireNonNull(uuid, "organization uuid can't be null");
68 this.organization.setProperty(Organization.from(new OrganizationDto().setUuid(uuid).setKey("key_" + uuid).setName("name_" + uuid)));
73 public Organization getOrganization() {
74 checkState(organization.isInitialized(), "Organization has not been set");
75 return this.organization.getProperty();
79 public AnalysisMetadataHolderRule setUuid(String s) {
80 checkNotNull(s, "UUID must not be null");
81 this.uuid.setProperty(s);
86 public String getUuid() {
87 checkState(uuid.isInitialized(), "Analysis UUID has not been set");
88 return this.uuid.getProperty();
91 public AnalysisMetadataHolderRule setAnalysisDate(Date date) {
92 checkNotNull(date, "Date must not be null");
93 this.analysisDate.setProperty(date.getTime());
98 public AnalysisMetadataHolderRule setAnalysisDate(long date) {
99 checkNotNull(date, "Date must not be null");
100 this.analysisDate.setProperty(date);
105 public long getAnalysisDate() {
106 checkState(analysisDate.isInitialized(), "Analysis date has not been set");
107 return this.analysisDate.getProperty();
111 public boolean hasAnalysisDateBeenSet() {
112 return analysisDate.isInitialized();
116 public boolean isFirstAnalysis() {
117 return getBaseAnalysis() == null;
121 public AnalysisMetadataHolderRule setBaseAnalysis(@Nullable Analysis baseAnalysis) {
122 this.baseAnalysis.setProperty(baseAnalysis);
128 public Analysis getBaseAnalysis() {
129 checkState(baseAnalysis.isInitialized(), "Base analysis has not been set");
130 return baseAnalysis.getProperty();
134 public AnalysisMetadataHolderRule setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
135 this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
140 public boolean isCrossProjectDuplicationEnabled() {
141 checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
142 return crossProjectDuplicationEnabled.getProperty();
146 public AnalysisMetadataHolderRule setBranch(@Nullable Branch branch) {
147 this.branch.setProperty(branch);
152 public Optional<Branch> getBranch() {
153 checkState(branch.isInitialized(), "Branch has not been set");
154 return Optional.ofNullable(branch.getProperty());
158 public AnalysisMetadataHolderRule setProject(Project p) {
159 this.project.setProperty(p);
164 public Project getProject() {
165 checkState(project.isInitialized(), "Project has not been set");
166 return project.getProperty();
170 public AnalysisMetadataHolderRule setRootComponentRef(int rootComponentRef) {
171 this.rootComponentRef.setProperty(rootComponentRef);
176 public int getRootComponentRef() {
177 checkState(rootComponentRef.isInitialized(), "Root component ref has not been set");
178 return rootComponentRef.getProperty();
182 public AnalysisMetadataHolderRule setQProfilesByLanguage(Map<String, QualityProfile> qProfilesPerLanguage) {
183 this.qProfilesPerLanguage.setProperty(qProfilesPerLanguage);
188 public Map<String, QualityProfile> getQProfilesByLanguage() {
189 checkState(qProfilesPerLanguage.isInitialized(), "QProfile per language has not been set");
190 return qProfilesPerLanguage.getProperty();
194 public AnalysisMetadataHolderRule setScannerPluginsByKey(Map<String, ScannerPlugin> plugins) {
195 this.pluginsByKey.setProperty(plugins);
200 public Map<String, ScannerPlugin> getScannerPluginsByKey() {
201 checkState(pluginsByKey.isInitialized(), "Plugins per key has not been set");
202 return pluginsByKey.getProperty();
206 public boolean isShortLivingBranch() {
207 Branch property = this.branch.getProperty();
208 return property != null && property.getType() == BranchType.SHORT;
212 public boolean isLongLivingBranch() {
213 Branch property = this.branch.getProperty();
214 return property != null && property.getType() == BranchType.LONG;