From 2087be37445a344cf19e45bd701715e60f7efe88 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Tue, 22 May 2012 16:09:55 +0200 Subject: [PATCH] SONAR-3016 Partial documentation of extension point for filter template --- .../java/org/sonar/api/web/Criterion.java | 117 +++++++++++------- .../java/org/sonar/api/web/FilterColumn.java | 91 ++++++++------ .../java/org/sonar/api/web/CriterionTest.java | 14 +-- .../org/sonar/api/web/FilterColumnTest.java | 44 +++++++ .../server/startup/RegisterNewFilters.java | 2 +- .../startup/RegisterNewFiltersTest.java | 12 +- 6 files changed, 188 insertions(+), 92 deletions(-) create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/web/FilterColumnTest.java diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/Criterion.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/Criterion.java index 7142c745dd5..ccdeed0d292 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/web/Criterion.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/Criterion.java @@ -20,9 +20,9 @@ package org.sonar.api.web; import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSortedSet; -import java.util.List; +import java.util.Set; /** * Definition of a criterion to be used to narrow down a {@see Filter}. @@ -30,78 +30,105 @@ import java.util.List; * @since 3.1 */ public class Criterion { - public static final List OPERATORS = ImmutableList.of("=", ">", "<", ">=", "<="); + public static final Set OPERATORS = ImmutableSortedSet.of("=", ">", "<", ">=", "<="); - private String family; - private String key; - private String operator; - private Float value; - private String textValue; - private boolean variation; + private final String family; + private final String key; + private final String operator; + private final Float value; + private final String textValue; + private final boolean variation; - private Criterion() { - // The factory method should be used + private Criterion(String family, String key, String operator, Float value, String textValue, boolean variation) { + Preconditions.checkArgument(OPERATORS.contains(operator), "Valid operators are %s, not '%s'", OPERATORS, operator); + + this.family = family; + this.key = key; + this.operator = operator; + this.value = value; + this.textValue = textValue; + this.variation = variation; } /** - * Creates a new {@link Criterion}. + * Creates a new {@link Criterion} with a numerical value. + * + *

Valid values for the {@code operator} are =, >, >=, < and <=

+ * + *

When the {@link Filter} is persisted, a validation is made on the {@code family} and the {@code key}. + * They should point to a valid criterion.

+ * + * @throws IllegalArgumentException if {@code operator} is not valid */ - public static Criterion create() { - return new Criterion(); + public static Criterion create(String family, String key, String operator, Float value, boolean variation) { + return new Criterion(family, key, operator, value, null, variation); } - public String getFamily() { - return family; + /** + * Creates a new {@link Criterion} with a text value. + * + *

Valid values for the {@code operator} are =, >, >=, < and <=

+ * + *

When the {@link Filter} is persisted, a validation is made on the {@code family} and the {@code key}. + * They should point to a valid criterion.

+ * + * @throws IllegalArgumentException if {@code operator} is not valid + */ + public static Criterion create(String family, String key, String operator, String textValue, boolean variation) { + return new Criterion(family, key, operator, null, textValue, variation); } - public Criterion setFamily(String family) { - this.family = family; - return this; + /** + * Get the the criterion's family. + * + * @return the family + */ + public String getFamily() { + return family; } + /** + * Get the the criterion's key. + * + * @return the key + */ public String getKey() { return key; } - public Criterion setKey(String key) { - this.key = key; - return this; - } - + /** + * Get the the criterion's operator. + * + * @return the operator + */ public String getOperator() { return operator; } - public Criterion setOperator(String operator) { - Preconditions.checkArgument(OPERATORS.contains(operator), "Valid operators are %s, not %s", OPERATORS, operator); - this.operator = operator; - return this; - } - + /** + * Get the the criterion's value. + * + * @return the value + */ public Float getValue() { return value; } - public Criterion setValue(Float value) { - this.value = value; - return this; - } - + /** + * Get the the criterion's value as text. + * + * @return the value as text + */ public String getTextValue() { return textValue; } - public Criterion setTextValue(String textValue) { - this.textValue = textValue; - return this; - } - + /** + * A criterion can be based on the varation of a value rather than on the value itself. + * + * @return true when the variation is used rather than the value + */ public boolean isVariation() { return variation; } - - public Criterion setVariation(boolean variation) { - this.variation = variation; - return this; - } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/FilterColumn.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/FilterColumn.java index 09c266e749f..65a2e6717e7 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/web/FilterColumn.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/FilterColumn.java @@ -19,71 +19,92 @@ */ package org.sonar.api.web; +import com.google.common.collect.ImmutableSortedSet; + +import java.util.Set; + +import com.google.common.base.Preconditions; + /** * Definition of a {@see Filter} column. * * @since 3.1 */ public class FilterColumn { - private String family; - private String key; - private String sortDirection; - private Long orderIndex; - private boolean variation; + public static final Set DIRECTIONS = ImmutableSortedSet.of("ASC", "DESC"); + + private final String family; + private final String key; + private final String sortDirection; + private final int orderIndex; + private final boolean variation; + + private FilterColumn(String family, String key, String sortDirection, int orderIndex, boolean variation) { + Preconditions.checkArgument(DIRECTIONS.contains(sortDirection), "Valid directions are %s, not '%s'", DIRECTIONS, sortDirection); - private FilterColumn() { - // The factory method should be used + this.family = family; + this.key = key; + this.sortDirection = sortDirection; + this.orderIndex = orderIndex; + this.variation = variation; } /** * Creates a new {@link FilterColumn}. + * + *

Valid values for the {@code sortDirection} are ASC and DESC

+ * + *

When the @{see Filter} is persisted, a validation is made on the {@code family} and the {@code key}. + * They should point to a valid column description.

+ * + * @throws IllegalArgumentException if {@code sortDirection} is not valid */ - public static FilterColumn create() { - return new FilterColumn(); + public static FilterColumn create(String family, String key, String sortDirection, int orderIndex, boolean variation) { + return new FilterColumn(family, key, sortDirection, orderIndex, variation); } + /** + * Get the the column's family. + * + * @return the family + */ public String getFamily() { return family; } - public FilterColumn setFamily(String family) { - this.family = family; - return this; - } - + /** + * Get the the column's key. + * + * @return the key + */ public String getKey() { return key; } - public FilterColumn setKey(String key) { - this.key = key; - return this; - } - + /** + * Get the the column's sort direction. + * + * @return the sort direction + */ public String getSortDirection() { return sortDirection; } - public FilterColumn setSortDirection(String sortDirection) { - this.sortDirection = sortDirection; - return this; - } - - public Long getOrderIndex() { + /** + * Get the the column's index. + * + * @return the index + */ + public int getOrderIndex() { return orderIndex; } - public FilterColumn setOrderIndex(Long orderIndex) { - this.orderIndex = orderIndex; - return this; - } - + /** + * A column can be based on the varation of a value rather than on the value itself. + * + * @return true when the variation is used rather than the value + */ public boolean isVariation() { return variation; } - - public FilterColumn setVariation(boolean variation) { - this.variation = variation; - return this; - } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/web/CriterionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/web/CriterionTest.java index 1526d4fb82e..0d08b992606 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/web/CriterionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/web/CriterionTest.java @@ -29,18 +29,18 @@ public class CriterionTest { @Test public void should_accept_valid_operators() { - Criterion.create().setOperator("<="); - Criterion.create().setOperator("<"); - Criterion.create().setOperator("="); - Criterion.create().setOperator(">"); - Criterion.create().setOperator(">="); + Criterion.create("", "", "<=", "", false); + Criterion.create("", "", "<", "", false); + Criterion.create("", "", "=", "", false); + Criterion.create("", "", ">", "", false); + Criterion.create("", "", ">=", "", false); } @Test public void should_fail_on_invalid_operators() { exception.expect(IllegalArgumentException.class); - exception.expectMessage("Valid operators are [=, >, <, >=, <=], not <>"); + exception.expectMessage("Valid operators are [<, <=, =, >, >=], not '<>'"); - Criterion.create().setOperator("<>"); + Criterion.create("", "", "<>", "", false); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/web/FilterColumnTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/web/FilterColumnTest.java new file mode 100644 index 00000000000..8a9fa10a5f6 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/web/FilterColumnTest.java @@ -0,0 +1,44 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.web; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class FilterColumnTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void should_accept_valid_direction() { + FilterColumn.create("", "", "ASC", 0, false); + FilterColumn.create("", "", "DESC", 0, false); + } + + @Test + public void should_fail_on_invalid_direction() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Valid directions are [ASC, DESC], not 'UNKNOWN'"); + + FilterColumn.create("", "", "UNKNOWN", 0, false); + } + +} diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterNewFilters.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterNewFilters.java index 32026da5528..83e39a4b6da 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterNewFilters.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterNewFilters.java @@ -128,7 +128,7 @@ public final class RegisterNewFilters { filterDto.add(new FilterColumnDto() .setFamily(column.getFamily()) .setKey(column.getKey()) - .setOrderIndex(column.getOrderIndex()) + .setOrderIndex((long) column.getOrderIndex()) .setSortDirection(column.getSortDirection()) .setVariation(column.isVariation())); } diff --git a/sonar-server/src/test/java/org/sonar/server/startup/RegisterNewFiltersTest.java b/sonar-server/src/test/java/org/sonar/server/startup/RegisterNewFiltersTest.java index 1017a42f247..523594dd9f0 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/RegisterNewFiltersTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/RegisterNewFiltersTest.java @@ -124,8 +124,10 @@ public class RegisterNewFiltersTest { .setLanguage("java") .setSearchFor("TRK,BRC") .setPageSize(200) - .add(Criterion.create().setFamily("metric").setKey("complexity").setOperator("<").setValue(12f)) - .add(FilterColumn.create().setFamily("metric").setKey("distance").setOrderIndex(1L).setSortDirection("ASC")) + .add(Criterion.create("metric", "complexity", "<", 12f, false)) + .add(Criterion.create("metric", "LCOM4", ">=", "5", true)) + .add(FilterColumn.create("metric", "distance", "ASC", 1, false)) + .add(FilterColumn.create("metric", "instability", "DESC", 2, true)) ); FilterDto dto = register.createDtoFromExtension("Fake", filterTemplate.createFilter()); @@ -141,14 +143,16 @@ public class RegisterNewFiltersTest { assertThat(dto.getDefaultView()).isEqualTo("list"); assertThat(dto.getPageSize()).isEqualTo(200L); - assertThat(dto.getCriteria()).hasSize(5); + assertThat(dto.getCriteria()).hasSize(6); assertThat(dto.getCriteria()).satisfies(contains(new CriterionDto().setFamily("key").setOperator("=").setTextValue("*KEY*"))); assertThat(dto.getCriteria()).satisfies(contains(new CriterionDto().setFamily("name").setOperator("=").setTextValue("*NAME*"))); assertThat(dto.getCriteria()).satisfies(contains(new CriterionDto().setFamily("language").setOperator("=").setTextValue("java"))); assertThat(dto.getCriteria()).satisfies(contains(new CriterionDto().setFamily("qualifier").setOperator("=").setTextValue("TRK,BRC"))); assertThat(dto.getCriteria()).satisfies(contains(new CriterionDto().setFamily("metric").setKey("complexity").setOperator("<").setValue(12f).setVariation(false))); + assertThat(dto.getCriteria()).satisfies(contains(new CriterionDto().setFamily("metric").setKey("LCOM4").setOperator(">=").setTextValue("5").setVariation(true))); - assertThat(dto.getColumns()).hasSize(1); + assertThat(dto.getColumns()).hasSize(2); assertThat(dto.getColumns()).satisfies(contains(new FilterColumnDto().setFamily("metric").setKey("distance").setOrderIndex(1L).setSortDirection("ASC").setVariation(false))); + assertThat(dto.getColumns()).satisfies(contains(new FilterColumnDto().setFamily("metric").setKey("instability").setOrderIndex(2L).setSortDirection("DESC").setVariation(true))); } } -- 2.39.5