From 0ce3af012a59e298c6c1c27d2a20a1a5694f026b Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 17 Jul 2017 18:49:13 +0200 Subject: [PATCH] SONAR-9551 Add attribute to view to detect applications --- .../projectanalysis/component/Component.java | 7 ++ .../component/ComponentImpl.java | 5 ++ .../component/ViewAttributes.java | 68 +++++++++++++++++++ .../component/ComponentImplTest.java | 15 ++++ .../component/ReportComponent.java | 5 ++ .../component/ViewAttributesTest.java | 67 ++++++++++++++++++ .../component/ViewsComponent.java | 21 +++++- 7 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ViewAttributes.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewAttributesTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/Component.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/Component.java index b89efeaad99..89481723f5f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/Component.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/Component.java @@ -112,4 +112,11 @@ public interface Component { * @throws IllegalStateException if the Component's type is not {@link Type#SUBVIEW} */ SubViewAttributes getSubViewAttributes(); + + /** + * The attributes of the Component if it's type is {@link Type#VIEW}. + * + * @throws IllegalStateException if the Component's type is not {@link Type#VIEW} + */ + ViewAttributes getViewAttributes(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImpl.java index 1a9c96008ef..490ed4e5345 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImpl.java @@ -110,6 +110,11 @@ public class ComponentImpl implements Component { throw new IllegalStateException("Only component of type SUBVIEW have a SubViewAttributes object"); } + @Override + public ViewAttributes getViewAttributes() { + throw new IllegalStateException("Only component of type VIEW have a ViewAttributes object"); + } + public static Builder builder(Type type) { return new Builder(type); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ViewAttributes.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ViewAttributes.java new file mode 100644 index 00000000000..4cfb07950c7 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ViewAttributes.java @@ -0,0 +1,68 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.task.projectanalysis.component; + +import java.util.Arrays; +import javax.annotation.concurrent.Immutable; +import org.sonar.api.resources.Qualifiers; + +import static java.util.Objects.requireNonNull; + +@Immutable +public class ViewAttributes { + + public enum Type { + PORTFOLIO(Qualifiers.VIEW), APPLICATION(Qualifiers.APP); + + private final String qualifier; + + Type(String qualifier) { + this.qualifier = qualifier; + } + + public String getQualifier() { + return qualifier; + } + + public static Type fromQualifier(String qualifier) { + return Arrays.stream(values()) + .filter(type -> type.getQualifier().equals(qualifier)) + .findFirst() + .orElseThrow(() -> new IllegalStateException(String.format("Qualifier '%s' is not supported", qualifier))); + } + } + + private final Type type; + + public ViewAttributes(Type type) { + this.type = requireNonNull(type, "Type cannot be null"); + } + + public Type getType() { + return type; + } + + @Override + public String toString() { + return "viewAttributes{" + + "type='" + type + '\'' + + '}'; + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImplTest.java index 0f4f0b2d686..1ef919a3185 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImplTest.java @@ -118,6 +118,21 @@ public class ComponentImplTest { }); } + @Test + public void getViewAttributes_throws_ISE_if_component_is_not_have_type_VIEW() { + Arrays.stream(Component.Type.values()) + .filter(type -> type != FILE) + .forEach((componentType) -> { + ComponentImpl component = buildSimpleComponent(componentType, componentType.name()).build(); + try { + component.getViewAttributes(); + fail("A IllegalStateException should have been raised"); + } catch (IllegalStateException e) { + assertThat(e).hasMessage("Only component of type VIEW have a ViewAttributes object"); + } + }); + } + @Test public void isUnitTest_returns_true_if_IsTest_is_set_in_BatchComponent() { ComponentImpl component = buildSimpleComponent(FILE, "file").setFileAttributes(new FileAttributes(true, null, 1)).build(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ReportComponent.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ReportComponent.java index 3d11af6d2a4..7c90e551f5b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ReportComponent.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ReportComponent.java @@ -121,6 +121,11 @@ public class ReportComponent implements Component { throw new IllegalStateException("Only component of type SUBVIEW have a SubViewAttributes object"); } + @Override + public ViewAttributes getViewAttributes() { + throw new IllegalStateException("Only component of type VIEW have a ViewAttributes object"); + } + @Override public boolean equals(@Nullable Object o) { if (this == o) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewAttributesTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewAttributesTest.java new file mode 100644 index 00000000000..040088a128f --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewAttributesTest.java @@ -0,0 +1,67 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.task.projectanalysis.component; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.resources.Qualifiers; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.server.computation.task.projectanalysis.component.ViewAttributes.Type.APPLICATION; +import static org.sonar.server.computation.task.projectanalysis.component.ViewAttributes.Type.PORTFOLIO; + +public class ViewAttributesTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private ViewAttributes underTest; + + @Test + public void create_portfolio() { + underTest = new ViewAttributes(PORTFOLIO); + + assertThat(underTest.getType()).isEqualTo(PORTFOLIO); + assertThat(underTest.getType().getQualifier()).isEqualTo(Qualifiers.VIEW); + } + + @Test + public void create_application() { + underTest = new ViewAttributes(APPLICATION); + + assertThat(underTest.getType()).isEqualTo(APPLICATION); + assertThat(underTest.getType().getQualifier()).isEqualTo(Qualifiers.APP); + } + + @Test + public void type_from_qualifier() { + assertThat(ViewAttributes.Type.fromQualifier(Qualifiers.VIEW)).isEqualTo(PORTFOLIO); + assertThat(ViewAttributes.Type.fromQualifier(Qualifiers.APP)).isEqualTo(APPLICATION); + } + + @Test + public void fail_if_unknown_view_qualifier() { + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Qualifier 'TRK' is not supported"); + + ViewAttributes.Type.fromQualifier(Qualifiers.PROJECT); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewsComponent.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewsComponent.java index 06a331e68fa..e129eea1222 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewsComponent.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewsComponent.java @@ -48,10 +48,12 @@ public class ViewsComponent implements Component { private final ProjectViewAttributes projectViewAttributes; @CheckForNull private final SubViewAttributes subViewAttributes; + @CheckForNull + private final ViewAttributes viewAttributes; private ViewsComponent(Type type, String key, @Nullable String uuid, @Nullable String name, @Nullable String description, List children, - @Nullable ProjectViewAttributes projectViewAttributes, @Nullable SubViewAttributes subViewAttributes) { + @Nullable ProjectViewAttributes projectViewAttributes, @Nullable SubViewAttributes subViewAttributes, @Nullable ViewAttributes viewAttributes) { checkArgument(type.isViewsType(), "Component type must be a Views type"); this.type = type; this.key = requireNonNull(key); @@ -61,6 +63,7 @@ public class ViewsComponent implements Component { this.children = ImmutableList.copyOf(children); this.projectViewAttributes = projectViewAttributes; this.subViewAttributes = subViewAttributes; + this.viewAttributes = viewAttributes; } public static Builder builder(Type type, String key) { @@ -85,6 +88,8 @@ public class ViewsComponent implements Component { private ProjectViewAttributes projectViewAttributes; @CheckForNull private SubViewAttributes subViewAttributes; + @CheckForNull + private ViewAttributes viewAttributes; private Builder(Type type, String key) { this.type = type; @@ -121,6 +126,11 @@ public class ViewsComponent implements Component { return this; } + public Builder setViewAttributes(@Nullable ViewAttributes viewAttributes) { + this.viewAttributes = viewAttributes; + return this; + } + public Builder addChildren(Component... c) { for (Component viewsComponent : c) { checkArgument(viewsComponent.getType().isViewsType()); @@ -130,7 +140,7 @@ public class ViewsComponent implements Component { } public ViewsComponent build() { - return new ViewsComponent(type, key, uuid, name, description, children, projectViewAttributes, subViewAttributes); + return new ViewsComponent(type, key, uuid, name, description, children, projectViewAttributes, subViewAttributes, viewAttributes); } } @@ -188,6 +198,12 @@ public class ViewsComponent implements Component { return this.subViewAttributes; } + @Override + public ViewAttributes getViewAttributes() { + checkState(this.type != Type.VIEW || this.viewAttributes != null, "A ViewAttributes object should have been set"); + return viewAttributes; + } + @Override public String toString() { return "ViewsComponent{" + @@ -198,6 +214,7 @@ public class ViewsComponent implements Component { ", children=" + children + ", projectViewAttributes=" + projectViewAttributes + ", subViewAttributes=" + subViewAttributes + + ", viewAttributes=" + viewAttributes + '}'; } -- 2.39.5