Skip to content

Commit

Permalink
SONAR-9551 Add attribute to view to detect applications
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Jul 31, 2017
1 parent d275093 commit 0ce3af0
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,11 @@ public boolean isViewsType() {
* @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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public SubViewAttributes getSubViewAttributes() {
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ public void getSubViewAttributes_throws_ISE_if_component_is_not_have_type_SUBVIE
});
}

@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public SubViewAttributes getSubViewAttributes() {
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -61,6 +63,7 @@ private ViewsComponent(Type type, String key, @Nullable String uuid, @Nullable S
this.children = ImmutableList.copyOf(children);
this.projectViewAttributes = projectViewAttributes;
this.subViewAttributes = subViewAttributes;
this.viewAttributes = viewAttributes;
}

public static Builder builder(Type type, String key) {
Expand All @@ -85,6 +88,8 @@ public static final class Builder {
private ProjectViewAttributes projectViewAttributes;
@CheckForNull
private SubViewAttributes subViewAttributes;
@CheckForNull
private ViewAttributes viewAttributes;

private Builder(Type type, String key) {
this.type = type;
Expand Down Expand Up @@ -121,6 +126,11 @@ public Builder setSubViewAttributes(@Nullable SubViewAttributes subViewAttribute
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());
Expand All @@ -130,7 +140,7 @@ public Builder addChildren(Component... c) {
}

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);
}
}

Expand Down Expand Up @@ -188,6 +198,12 @@ public SubViewAttributes getSubViewAttributes() {
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{" +
Expand All @@ -198,6 +214,7 @@ public String toString() {
", children=" + children +
", projectViewAttributes=" + projectViewAttributes +
", subViewAttributes=" + subViewAttributes +
", viewAttributes=" + viewAttributes +
'}';
}

Expand Down

0 comments on commit 0ce3af0

Please sign in to comment.