From fd7f6a27f9bfc895298a57be47fe7511d9d004b4 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Tue, 22 May 2012 11:56:58 +0200 Subject: [PATCH] SONAR-3016 More work on extension point for filter template Recognize more filter criterias --- .../main/java/org/sonar/api/web/Filter.java | 62 ++++++++++++++++--- .../java/org/sonar/api/web/FilterTest.java | 45 ++++++++++++++ .../server/startup/RegisterNewFilters.java | 17 ++++- .../startup/RegisterNewFiltersTest.java | 27 ++++++-- .../java/org/sonar/test/MoreConditions.java | 17 +++++ .../org/sonar/test/MoreConditionsTest.java | 37 +++++++++++ 6 files changed, 191 insertions(+), 14 deletions(-) create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/web/FilterTest.java diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/Filter.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/Filter.java index ef395f6b71f..e906d07faf0 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/web/Filter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/Filter.java @@ -19,6 +19,8 @@ */ package org.sonar.api.web; +import com.google.common.base.Preconditions; + /** * Definition of a filter. *

@@ -33,39 +35,83 @@ public class Filter { private boolean shared; private boolean favouritesOnly; private String defaultPeriod; + private String resourceKeyLike; + private String resourceNameLike; + private String language; + private String searchFor; private Filter() { // The factory method should be used } + /** + * Creates a new {@link Filter}. + */ + public static Filter create() { + return new Filter(); + } + public boolean isShared() { return shared; } - public void setShared(boolean shared) { + public Filter setShared(boolean shared) { this.shared = shared; + return this; } public boolean isFavouritesOnly() { return favouritesOnly; } - public void setFavouritesOnly(boolean favouritesOnly) { + public Filter setFavouritesOnly(boolean favouritesOnly) { this.favouritesOnly = favouritesOnly; + return this; } public String getDefaultPeriod() { return defaultPeriod; } - public void setDefaultPeriod(String defaultPeriod) { + public Filter setDefaultPeriod(String defaultPeriod) { + Preconditions.checkArgument("list".equals(defaultPeriod) || "treemap".equals(defaultPeriod), "Default period should be either list or treemap, not %s", defaultPeriod); this.defaultPeriod = defaultPeriod; + return this; } - /** - * Creates a new {@link Filter}. - */ - public static Filter create() { - return new Filter(); + public String getResourceKeyLike() { + return resourceKeyLike; + } + + public Filter setResourceKeyLike(String resourceKeyLike) { + this.resourceKeyLike = resourceKeyLike; + return this; + } + + public String getResourceNameLike() { + return resourceNameLike; + } + + public Filter setResourceNameLike(String resourceNameLike) { + this.resourceNameLike = resourceNameLike; + return this; + } + + public String getLanguage() { + return language; + } + + public Filter setLanguage(String language) { + this.language = language; + return this; + } + + public String getSearchFor() { + return searchFor; + } + + public Filter setSearchFor(String searchFor) { + this.searchFor = searchFor; + return this; } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/web/FilterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/web/FilterTest.java new file mode 100644 index 00000000000..9466e8d0c04 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/web/FilterTest.java @@ -0,0 +1,45 @@ +/* + * 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.rules.ExpectedException; + +import org.junit.Rule; + +import org.junit.Test; + +public class FilterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void should_accept_valid_periods() { + Filter.create().setDefaultPeriod("list"); + Filter.create().setDefaultPeriod("treemap"); + } + + @Test + public void should_fail_on_invalid_period() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Default period should be either list or treemap, not "); + + Filter.create().setDefaultPeriod(""); + } +} 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 757765d3726..cbc29fc4c52 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 @@ -19,6 +19,8 @@ */ package org.sonar.server.startup; +import org.sonar.core.filter.CriteriaDto; + import com.google.common.collect.ImmutableList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -83,11 +85,24 @@ public final class RegisterNewFilters { return dto; } + private static void addCriteria(FilterDto filterDto, String family, String operator, String textValue) { + if (textValue != null) { + filterDto.addCriteria(new CriteriaDto().setFamily(family).setOperator(operator).setTextValue(textValue)); + } + } + protected FilterDto createDtoFromExtension(String name, Filter filter) { - return new FilterDto() + FilterDto filterDto = new FilterDto() .setName(name) .setShared(filter.isShared()) .setFavourites(filter.isFavouritesOnly()) .setDefaultView(filter.getDefaultPeriod()); + + addCriteria(filterDto, "key", "=", filter.getResourceKeyLike()); + addCriteria(filterDto, "name", "=", filter.getResourceNameLike()); + addCriteria(filterDto, "language", "=", filter.getLanguage()); + addCriteria(filterDto, "qualifier", "=", filter.getSearchFor()); + + return filterDto; } } 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 47e6853eb19..14af9023c51 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 @@ -19,10 +19,12 @@ */ package org.sonar.server.startup; +import com.google.common.collect.Iterables; import org.junit.Before; import org.junit.Test; import org.sonar.api.web.Filter; import org.sonar.api.web.FilterTemplate; +import org.sonar.core.filter.CriteriaDto; import org.sonar.core.filter.FilterDao; import org.sonar.core.filter.FilterDto; import org.sonar.core.template.LoadedTemplateDao; @@ -36,6 +38,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.sonar.test.MoreConditions.contains; public class RegisterNewFiltersTest { private RegisterNewFilters register; @@ -106,18 +109,32 @@ public class RegisterNewFiltersTest { @Test public void should_create_dto_from_extension() { - Filter filter = Filter.create(); - filter.setShared(true); - filter.setFavouritesOnly(false); - filter.setDefaultPeriod("list"); - when(filterTemplate.createFilter()).thenReturn(filter); + when(filterTemplate.createFilter()).thenReturn(Filter.create() + .setShared(true) + .setFavouritesOnly(false) + .setDefaultPeriod("list") + .setResourceKeyLike("*KEY*") + .setResourceNameLike("*NAME*") + .setLanguage("java") + .setSearchFor("TRK,BRC") + ); FilterDto dto = register.createDtoFromExtension("Fake", filterTemplate.createFilter()); + CriteriaDto criteriaResourceKeyDto = Iterables.get(dto.getCriterias(), 0); + CriteriaDto criteriaResourceNameDto = Iterables.get(dto.getCriterias(), 1); + CriteriaDto criteriaLangageDto = Iterables.get(dto.getCriterias(), 2); + CriteriaDto criteriaSearchForDto = Iterables.get(dto.getCriterias(), 3); assertThat(dto.getUserId()).isNull(); assertThat(dto.getName()).isEqualTo("Fake"); assertThat(dto.isShared()).isTrue(); assertThat(dto.isFavourites()).isFalse(); assertThat(dto.getDefaultView()).isEqualTo("list"); + + assertThat(dto.getCriterias()).hasSize(4); + assertThat(dto.getCriterias()).satisfies(contains(new CriteriaDto().setFamily("key").setOperator("=").setTextValue("*KEY*"))); + assertThat(dto.getCriterias()).satisfies(contains(new CriteriaDto().setFamily("name").setOperator("=").setTextValue("*NAME*"))); + assertThat(dto.getCriterias()).satisfies(contains(new CriteriaDto().setFamily("language").setOperator("=").setTextValue("java"))); + assertThat(dto.getCriterias()).satisfies(contains(new CriteriaDto().setFamily("qualifier").setOperator("=").setTextValue("TRK,BRC"))); } } diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/MoreConditions.java b/sonar-testing-harness/src/main/java/org/sonar/test/MoreConditions.java index c83e5ff9ec1..c06b78a65e6 100644 --- a/sonar-testing-harness/src/main/java/org/sonar/test/MoreConditions.java +++ b/sonar-testing-harness/src/main/java/org/sonar/test/MoreConditions.java @@ -20,8 +20,11 @@ package org.sonar.test; import com.google.common.base.CharMatcher; +import org.apache.commons.lang.builder.EqualsBuilder; import org.fest.assertions.Condition; +import java.util.Collection; + /** * Conditions for use with FestAssert. */ @@ -42,4 +45,18 @@ public final class MoreConditions { } }.as("equal to " + strippedText); } + + public static Condition> contains(final Object expected) { + return new Condition>() { + @Override + public boolean matches(Collection collection) { + for (Object actual : collection) { + if (EqualsBuilder.reflectionEquals(expected, actual)) { + return true; + } + } + return false; + } + }; + } } diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/MoreConditionsTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/MoreConditionsTest.java index 86ba9114c6e..ad83811e999 100644 --- a/sonar-testing-harness/src/test/java/org/sonar/test/MoreConditionsTest.java +++ b/sonar-testing-harness/src/test/java/org/sonar/test/MoreConditionsTest.java @@ -21,7 +21,11 @@ package org.sonar.test; import org.junit.Test; +import java.util.Arrays; +import java.util.Collection; + import static org.fest.assertions.Assertions.assertThat; +import static org.sonar.test.MoreConditions.contains; import static org.sonar.test.MoreConditions.equalsIgnoreEOL; public class MoreConditionsTest { @@ -49,4 +53,37 @@ public class MoreConditionsTest { assertThat("").satisfies(equalsIgnoreEOL("\n\r")); assertThat("\n\r").satisfies(equalsIgnoreEOL("")); } + + @Test + public void should_find_value_in_collection() { + Collection collection = Arrays.asList("ONE", "TWO"); + + assertThat(collection).satisfies(contains("ONE")); + assertThat(collection).satisfies(contains("TWO")); + assertThat(collection).doesNotSatisfy(contains("THREE")); + } + + @Test + public void should_find_value_in_collection_using_reflection() { + Collection collection = Arrays.asList( + new Bean("key1", "value1"), + null, + new Bean("key2", "value2")); + + assertThat(collection).satisfies(contains(new Bean("key1", "value1"))); + assertThat(collection).satisfies(contains(new Bean("key2", "value2"))); + assertThat(collection).doesNotSatisfy(contains(new Bean("key1", "value2"))); + assertThat(collection).doesNotSatisfy(contains(new Bean("key2", "value1"))); + assertThat(collection).doesNotSatisfy(contains(new Bean("", ""))); + } + + static final class Bean { + final String key; + final String value; + + Bean(String key, String value) { + this.key = key; + this.value = value; + } + } } -- 2.39.5