3 * Copyright (C) 2009-2020 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.ce.task.projectanalysis.component;
22 import com.google.common.collect.ImmutableList;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Optional;
26 import javax.annotation.CheckForNull;
27 import javax.annotation.Nullable;
29 import static com.google.common.base.Preconditions.checkArgument;
30 import static com.google.common.base.Preconditions.checkState;
31 import static java.util.Arrays.asList;
32 import static java.util.Objects.requireNonNull;
35 * Implementation of {@link Component} to unit test report components.
37 public class ReportComponent implements Component {
39 private static final FileAttributes DEFAULT_FILE_ATTRIBUTES = new FileAttributes(false, null, 1);
41 public static final Component DUMB_PROJECT = builder(Type.PROJECT, 1)
42 .setKey("PROJECT_KEY")
43 .setPublicKey("PUBLIC_PROJECT_KEY")
44 .setUuid("PROJECT_UUID")
45 .setName("Project Name")
46 .setProjectVersion("1.0-SNAPSHOT")
49 private final Type type;
50 private final Status status;
51 private final String name;
52 private final String shortName;
54 private final String description;
55 private final String key;
56 private final String publicKey;
57 private final String uuid;
58 private final ProjectAttributes projectAttributes;
59 private final ReportAttributes reportAttributes;
60 private final FileAttributes fileAttributes;
61 private final List<Component> children;
63 private ReportComponent(Builder builder) {
64 this.type = builder.type;
65 this.status = builder.status;
66 this.key = builder.key;
67 this.publicKey = builder.publicKey;
68 this.name = builder.name == null ? String.valueOf(builder.key) : builder.name;
69 this.shortName = builder.shortName == null ? this.name : builder.shortName;
70 this.description = builder.description;
71 this.uuid = builder.uuid;
72 this.projectAttributes = Optional.ofNullable(builder.projectVersion)
73 .map(v -> new ProjectAttributes(v, builder.buildString, builder.scmRevisionId))
75 this.reportAttributes = ReportAttributes.newBuilder(builder.ref)
77 this.fileAttributes = builder.fileAttributes == null ? DEFAULT_FILE_ATTRIBUTES : builder.fileAttributes;
78 this.children = ImmutableList.copyOf(builder.children);
82 public Type getType() {
87 public Status getStatus() {
92 public String getUuid() {
94 throw new UnsupportedOperationException(String.format("Component uuid of ref '%d' has not be fed yet", this.reportAttributes.getRef()));
100 public String getDbKey() {
102 throw new UnsupportedOperationException(String.format("Component key of ref '%d' has not be fed yet", this.reportAttributes.getRef()));
108 public String getKey() {
109 if (publicKey == null) {
110 throw new UnsupportedOperationException(String.format("Component key of ref '%d' has not be fed yet", this.reportAttributes.getRef()));
116 public String getName() {
121 public String getShortName() {
122 return this.shortName;
127 public String getDescription() {
128 return this.description;
132 public List<Component> getChildren() {
137 public ProjectAttributes getProjectAttributes() {
138 checkState(this.type == Type.PROJECT);
139 return this.projectAttributes;
143 public ReportAttributes getReportAttributes() {
144 return this.reportAttributes;
148 public FileAttributes getFileAttributes() {
149 checkState(this.type == Type.FILE, "Only component of type FILE can have a FileAttributes object");
150 return this.fileAttributes;
154 public ProjectViewAttributes getProjectViewAttributes() {
155 throw new IllegalStateException("Only component of type PROJECT_VIEW can have a ProjectViewAttributes object");
159 public SubViewAttributes getSubViewAttributes() {
160 throw new IllegalStateException("Only component of type SUBVIEW have a SubViewAttributes object");
164 public ViewAttributes getViewAttributes() {
165 throw new IllegalStateException("Only component of type VIEW have a ViewAttributes object");
169 public boolean equals(@Nullable Object o) {
173 if (o == null || getClass() != o.getClass()) {
176 ReportComponent that = (ReportComponent) o;
177 return uuid.equals(that.uuid);
181 public int hashCode() {
182 return uuid.hashCode();
186 public String toString() {
187 return "ReportComponent{" +
188 "ref=" + this.reportAttributes.getRef() +
189 ", key='" + key + '\'' +
194 public static Builder builder(Type type, int ref) {
195 String key = "key_" + ref;
196 return new Builder(type, ref).setKey(key).setPublicKey(key).setUuid("uuid_" + ref).setName("name_" + ref);
199 public static final class Builder {
200 private final Type type;
201 private final int ref;
202 private Status status;
205 private String publicKey;
207 private String shortName;
208 private String projectVersion;
209 private String buildString;
210 private String scmRevisionId;
211 private String description;
212 private FileAttributes fileAttributes;
213 private final List<Component> children = new ArrayList<>();
215 private Builder(Type type, int ref) {
216 checkArgument(type.isReportType(), "Component type must be a report type");
219 if (type == Type.PROJECT) {
220 this.projectVersion = "toBeDefined";
224 public Builder setStatus(Status s) {
225 this.status = requireNonNull(s);
229 public Builder setUuid(String s) {
230 this.uuid = requireNonNull(s);
234 public Builder setName(@Nullable String s) {
239 public Builder setShortName(@Nullable String s) {
244 public Builder setKey(String s) {
245 this.key = requireNonNull(s);
249 public Builder setPublicKey(String publicKey) {
250 this.publicKey = requireNonNull(publicKey);
254 public Builder setProjectVersion(@Nullable String s) {
255 checkProjectVersion(s);
256 this.projectVersion = s;
260 public Builder setBuildString(@Nullable String buildString) {
261 checkBuildString(buildString);
262 this.buildString = buildString;
266 public Builder setScmRevisionId(@Nullable String scmRevisionId) {
267 this.scmRevisionId = scmRevisionId;
271 public Builder setFileAttributes(FileAttributes fileAttributes) {
272 checkState(type == Type.FILE, "Only Component of type File can have File attributes");
273 this.fileAttributes = fileAttributes;
277 public Builder setDescription(@Nullable String description) {
278 this.description = description;
282 public Builder addChildren(Component... c) {
283 for (Component component : c) {
284 checkArgument(component.getType().isReportType());
286 this.children.addAll(asList(c));
290 public ReportComponent build() {
291 checkProjectVersion(this.projectVersion);
292 checkBuildString(this.buildString);
293 return new ReportComponent(this);
296 private void checkProjectVersion(@Nullable String s) {
297 checkArgument(type != Type.PROJECT ^ s != null, "Project version must and can only be set on Project");
300 private void checkBuildString(@Nullable String s) {
301 checkArgument(type == Type.PROJECT || s == null, "BuildString can only be set on Project");