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 javax.annotation.CheckForNull;
25 import javax.annotation.Nullable;
26 import org.junit.rules.ExternalResource;
27 import org.sonar.db.component.BranchType;
28 import org.sonar.db.organization.OrganizationDto;
29 import org.sonar.server.computation.util.InitializedProperty;
30 import org.sonar.server.qualityprofile.QualityProfile;
32 import static com.google.common.base.Preconditions.checkNotNull;
33 import static com.google.common.base.Preconditions.checkState;
34 import static java.util.Objects.requireNonNull;
36 public class AnalysisMetadataHolderRule extends ExternalResource implements MutableAnalysisMetadataHolder {
38 private final InitializedProperty<Organization> organization = new InitializedProperty<>();
40 private final InitializedProperty<String> uuid = new InitializedProperty<>();
42 private final InitializedProperty<Long> analysisDate = new InitializedProperty<>();
44 private final InitializedProperty<Analysis> baseAnalysis = new InitializedProperty<>();
46 private final InitializedProperty<Boolean> crossProjectDuplicationEnabled = new InitializedProperty<>();
48 private final InitializedProperty<Branch> branch = new InitializedProperty<>();
50 private final InitializedProperty<Project> project = new InitializedProperty<>();
52 private final InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
54 private final InitializedProperty<Map<String, QualityProfile>> qProfilesPerLanguage = new InitializedProperty<>();
56 private final InitializedProperty<Map<String, ScannerPlugin>> pluginsByKey = new InitializedProperty<>();
59 public AnalysisMetadataHolderRule setOrganization(Organization organization) {
60 requireNonNull(organization, "organization can't be null");
61 this.organization.setProperty(organization);
65 public AnalysisMetadataHolderRule setOrganizationUuid(String uuid) {
66 requireNonNull(uuid, "organization uuid can't be null");
67 this.organization.setProperty(Organization.from(new OrganizationDto().setUuid(uuid).setKey("key_" + uuid).setName("name_" + uuid)));
72 public Organization getOrganization() {
73 checkState(organization.isInitialized(), "Organization has not been set");
74 return this.organization.getProperty();
78 public AnalysisMetadataHolderRule setUuid(String s) {
79 checkNotNull(s, "UUID must not be null");
80 this.uuid.setProperty(s);
85 public String getUuid() {
86 checkState(uuid.isInitialized(), "Analysis UUID has not been set");
87 return this.uuid.getProperty();
90 public AnalysisMetadataHolderRule setAnalysisDate(Date date) {
91 checkNotNull(date, "Date must not be null");
92 this.analysisDate.setProperty(date.getTime());
97 public AnalysisMetadataHolderRule setAnalysisDate(long date) {
98 checkNotNull(date, "Date must not be null");
99 this.analysisDate.setProperty(date);
104 public long getAnalysisDate() {
105 checkState(analysisDate.isInitialized(), "Analysis date has not been set");
106 return this.analysisDate.getProperty();
110 public boolean hasAnalysisDateBeenSet() {
111 return analysisDate.isInitialized();
115 public boolean isFirstAnalysis() {
116 return getBaseAnalysis() == null;
120 public AnalysisMetadataHolderRule setBaseAnalysis(@Nullable Analysis baseAnalysis) {
121 this.baseAnalysis.setProperty(baseAnalysis);
127 public Analysis getBaseAnalysis() {
128 checkState(baseAnalysis.isInitialized(), "Base analysis has not been set");
129 return baseAnalysis.getProperty();
133 public AnalysisMetadataHolderRule setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
134 this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
139 public boolean isCrossProjectDuplicationEnabled() {
140 checkState(crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has not been set");
141 return crossProjectDuplicationEnabled.getProperty();
145 public AnalysisMetadataHolderRule setBranch(Branch branch) {
146 this.branch.setProperty(branch);
151 public Branch getBranch() {
152 checkState(branch.isInitialized(), "Branch has not been set");
153 return branch.getProperty();
157 public AnalysisMetadataHolderRule setProject(Project p) {
158 this.project.setProperty(p);
163 public Project getProject() {
164 checkState(project.isInitialized(), "Project has not been set");
165 return project.getProperty();
169 public AnalysisMetadataHolderRule setRootComponentRef(int rootComponentRef) {
170 this.rootComponentRef.setProperty(rootComponentRef);
175 public int getRootComponentRef() {
176 checkState(rootComponentRef.isInitialized(), "Root component ref has not been set");
177 return rootComponentRef.getProperty();
181 public AnalysisMetadataHolderRule setQProfilesByLanguage(Map<String, QualityProfile> qProfilesPerLanguage) {
182 this.qProfilesPerLanguage.setProperty(qProfilesPerLanguage);
187 public Map<String, QualityProfile> getQProfilesByLanguage() {
188 checkState(qProfilesPerLanguage.isInitialized(), "QProfile per language has not been set");
189 return qProfilesPerLanguage.getProperty();
193 public AnalysisMetadataHolderRule setScannerPluginsByKey(Map<String, ScannerPlugin> plugins) {
194 this.pluginsByKey.setProperty(plugins);
199 public Map<String, ScannerPlugin> getScannerPluginsByKey() {
200 checkState(pluginsByKey.isInitialized(), "Plugins per key has not been set");
201 return pluginsByKey.getProperty();
205 public boolean isShortLivingBranch() {
206 Branch property = this.branch.getProperty();
207 return property != null && property.getType() == BranchType.SHORT;
211 public boolean isLongLivingBranch() {
212 Branch property = this.branch.getProperty();
213 return property != null && property.getType() == BranchType.LONG;