]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9551 Add attribute to view to detect applications
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 17 Jul 2017 16:49:13 +0000 (18:49 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 31 Jul 2017 09:27:51 +0000 (11:27 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/Component.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImpl.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ViewAttributes.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImplTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ReportComponent.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewAttributesTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ViewsComponent.java

index b89efeaad9947da3fbb92d354d6dd6b41a5d3070..89481723f5fafc03fd3882e607cd573d65d6aa49 100644 (file)
@@ -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();
 }
index 1a9c96008ef822650f8f6ad2dacc7275d4f005f1..490ed4e534519b7f1c0310966f51a4fd568528df 100644 (file)
@@ -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 (file)
index 0000000..4cfb079
--- /dev/null
@@ -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 + '\'' +
+      '}';
+  }
+}
index 0f4f0b2d6861faae177f19907ea1a84de3b42b33..1ef919a3185af190e41061f2266df3fb1397a4b7 100644 (file)
@@ -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();
index 3d11af6d2a428a10b787d25f001eea79854fe04b..7c90e551f5b92209762e14852de1e4d7f80d4539 100644 (file)
@@ -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 (file)
index 0000000..040088a
--- /dev/null
@@ -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);
+  }
+}
index 06a331e68fa9afe1cc15ff5f05549adec25efd5f..e129eea122224977f6ca49b756bf577a7a21d298 100644 (file)
@@ -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<Component> 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 +
       '}';
   }