From b8fbb2bd8b62c21a5e5e422e19dc8ccea1b9940b Mon Sep 17 00:00:00 2001 From: David Gageot Date: Thu, 24 May 2012 13:50:44 +0200 Subject: [PATCH] Slightly better API for the filter extension point --- .../core/filters/MyFavouritesFilter.java | 2 +- .../plugins/core/filters/ProjectFilter.java | 2 +- .../plugins/core/filters/TreeMapFilter.java | 2 +- .../java/org/sonar/api/web/Criterion.java | 37 ++++++++++++++++--- .../java/org/sonar/api/web/CriterionTest.java | 12 +++--- .../startup/RegisterNewFiltersTest.java | 4 +- 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/filters/MyFavouritesFilter.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/filters/MyFavouritesFilter.java index ceb8b8ec1db..df2fc4d9768 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/filters/MyFavouritesFilter.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/filters/MyFavouritesFilter.java @@ -41,7 +41,7 @@ public class MyFavouritesFilter extends FilterTemplate { Filter filter = Filter.create(); filter.setDisplayAs(Filter.LIST); filter.setFavouritesOnly(true); - filter.add(Criterion.create("qualifier", null, Criterion.EQ, "VW,SVW,TRK,BRC,DIR,PAC,FIL,CLA,UTS,LIB", false)); + filter.add(Criterion.createForQualifier("VW", "SVW", "TRK", "BRC", "DIR", "PAC", "FIL", "CLA", "UTS", "LIB")); filter.add(FilterColumn.create("metric", "alert_status", FilterColumn.DESC, false)); filter.add(FilterColumn.create("name", null, FilterColumn.ASC, false)); filter.add(FilterColumn.create("metric", "ncloc", FilterColumn.DESC, false)); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/filters/ProjectFilter.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/filters/ProjectFilter.java index 368805b21bb..189d85d0ec2 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/filters/ProjectFilter.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/filters/ProjectFilter.java @@ -39,7 +39,7 @@ public class ProjectFilter extends FilterTemplate { public Filter createFilter() { Filter filter = Filter.create(); filter.setDisplayAs(Filter.LIST); - filter.add(Criterion.create("qualifier", null, Criterion.EQ, "TRK", false)); + filter.add(Criterion.createForQualifier("TRK")); filter.add(FilterColumn.create("metric", "alert_status", FilterColumn.DESC, false)); filter.add(FilterColumn.create("name", null, FilterColumn.ASC, false)); filter.add(FilterColumn.create("version", null, FilterColumn.DESC, false)); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/filters/TreeMapFilter.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/filters/TreeMapFilter.java index 0227fb71468..0d5cb7a863f 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/filters/TreeMapFilter.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/filters/TreeMapFilter.java @@ -40,7 +40,7 @@ public class TreeMapFilter extends FilterTemplate { public Filter createFilter() { Filter filter = Filter.create(); filter.setDisplayAs(Filter.TREEMAP); - filter.add(Criterion.create("qualifier", null, Criterion.EQ, "TRK", false)); + filter.add(Criterion.createForQualifier("TRK")); filter.add(FilterColumn.create("name", null, FilterColumn.ASC, false)); filter.add(FilterColumn.create("metric", "ncloc", FilterColumn.DESC, false)); filter.add(FilterColumn.create("metric", "violations_density", FilterColumn.DESC, false)); 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 f43bc68c659..abd70585aab 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 @@ -19,6 +19,8 @@ */ package org.sonar.api.web; +import com.google.common.base.Joiner; + import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSortedSet; @@ -60,9 +62,6 @@ public final class Criterion { * *

Valid values for the {@code operator} are {@value #EQ}, {@value #GT}, {@value #GTE}, {@value #LT} and {@value #LTE}

* - *

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, Float value, boolean variation) { @@ -74,15 +73,41 @@ public final class Criterion { * *

Valid values for the {@code operator} are {@value #EQ}, {@value #GT}, {@value #GTE}, {@value #LT} and {@value #LTE}

* - *

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); } + /** + * Creates a new {@link Criterion} on a metric, with a numerical value. + * + *

Valid values for the {@code operator} are {@value #EQ}, {@value #GT}, {@value #GTE}, {@value #LT} and {@value #LTE}

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

Valid values for the {@code operator} are {@value #EQ}, {@value #GT}, {@value #GTE}, {@value #LT} and {@value #LTE}

+ * + * @throws IllegalArgumentException if {@code operator} is not valid + */ + public static Criterion createForMetric(String key, String operator, String textValue, boolean variation) { + return new Criterion("metric", key, operator, null, textValue, variation); + } + + /** + * Creates a new {@link Criterion} on a qualifier. + */ + public static Criterion createForQualifier(Object... values) { + return new Criterion("qualifier", null, EQ, null, Joiner.on(',').join(values), false); + } + /** * Get the the criterion's family. * 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 0d08b992606..05097bca1ec 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,11 +29,11 @@ public class CriterionTest { @Test public void should_accept_valid_operators() { - Criterion.create("", "", "<=", "", false); - Criterion.create("", "", "<", "", false); - Criterion.create("", "", "=", "", false); - Criterion.create("", "", ">", "", false); - Criterion.create("", "", ">=", "", false); + Criterion.createForMetric("", "<=", "", false); + Criterion.createForMetric("", "<", "", false); + Criterion.createForMetric("", "=", "", false); + Criterion.createForMetric("", ">", "", false); + Criterion.createForMetric("", ">=", "", false); } @Test @@ -41,6 +41,6 @@ public class CriterionTest { exception.expect(IllegalArgumentException.class); exception.expectMessage("Valid operators are [<, <=, =, >, >=], not '<>'"); - Criterion.create("", "", "<>", "", false); + Criterion.createForMetric("", "<>", "", false); } } 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 e0ac7e02a9a..e4debe6d2cb 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 @@ -114,8 +114,8 @@ public class RegisterNewFiltersTest { when(filterTemplate.createFilter()).thenReturn(Filter.create() .setFavouritesOnly(false) .setDisplayAs("list") - .add(Criterion.create("metric", "complexity", Criterion.LT, 12f, false)) - .add(Criterion.create("metric", "LCOM4", Criterion.GTE, "5", true)) + .add(Criterion.createForMetric("complexity", Criterion.LT, 12f, false)) + .add(Criterion.createForMetric("LCOM4", Criterion.GTE, "5", true)) .add(FilterColumn.create("metric", "distance", "ASC", false)) .add(FilterColumn.create("metric", "instability", "DESC", true)) ); -- 2.39.5