* @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();
}
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);
}
--- /dev/null
+/*
+ * 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 + '\'' +
+ '}';
+ }
+}
});
}
+ @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();
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) {
--- /dev/null
+/*
+ * 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);
+ }
+}
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);
this.children = ImmutableList.copyOf(children);
this.projectViewAttributes = projectViewAttributes;
this.subViewAttributes = subViewAttributes;
+ this.viewAttributes = viewAttributes;
}
public static Builder builder(Type type, String key) {
private ProjectViewAttributes projectViewAttributes;
@CheckForNull
private SubViewAttributes subViewAttributes;
+ @CheckForNull
+ private ViewAttributes viewAttributes;
private Builder(Type type, String key) {
this.type = type;
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());
}
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);
}
}
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{" +
", children=" + children +
", projectViewAttributes=" + projectViewAttributes +
", subViewAttributes=" + subViewAttributes +
+ ", viewAttributes=" + viewAttributes +
'}';
}