From 70a99389ba85ae3e7d20c8f56acb571d2ee915a7 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Tue, 22 May 2012 10:51:06 +0200 Subject: [PATCH] SONAR-3016 More work on extension point for filter template Added mappers and dots for criteria and fiter_columns --- .../org/sonar/core/filter/CriteriaDto.java | 106 ++++++++++++++++++ .../org/sonar/core/filter/CriteriaMapper.java | 27 +++++ .../sonar/core/filter/FilterColumnDto.java | 96 ++++++++++++++++ .../sonar/core/filter/FilterColumnMapper.java | 27 +++++ .../java/org/sonar/core/filter/FilterDao.java | 14 ++- .../java/org/sonar/core/filter/FilterDto.java | 57 ++++------ .../org/sonar/core/persistence/MyBatis.java | 14 ++- .../core/filter/CriteriaMapper-oracle.xml | 12 ++ .../org/sonar/core/filter/CriteriaMapper.xml | 12 ++ .../core/filter/FilterColumnMapper-oracle.xml | 12 ++ .../sonar/core/filter/FilterColumnMapper.xml | 12 ++ .../org/sonar/core/filters/FilterDaoTest.java | 23 +++- .../FilterDaoTest/shouldInsert-result.xml | 21 ++++ .../main/java/org/sonar/api/web/Filter.java | 31 +++++ .../server/startup/RegisterNewFilters.java | 35 ++---- .../startup/RegisterNewFiltersTest.java | 89 ++++++++------- 16 files changed, 477 insertions(+), 111 deletions(-) create mode 100644 sonar-core/src/main/java/org/sonar/core/filter/CriteriaDto.java create mode 100644 sonar-core/src/main/java/org/sonar/core/filter/CriteriaMapper.java create mode 100644 sonar-core/src/main/java/org/sonar/core/filter/FilterColumnDto.java create mode 100644 sonar-core/src/main/java/org/sonar/core/filter/FilterColumnMapper.java create mode 100644 sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper-oracle.xml create mode 100644 sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper.xml create mode 100644 sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper-oracle.xml create mode 100644 sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper.xml diff --git a/sonar-core/src/main/java/org/sonar/core/filter/CriteriaDto.java b/sonar-core/src/main/java/org/sonar/core/filter/CriteriaDto.java new file mode 100644 index 00000000000..4d9aaa034b5 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/filter/CriteriaDto.java @@ -0,0 +1,106 @@ +/* + * 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.core.filter; + +/** + * @since 3.1 + */ +public final class CriteriaDto { + private Long id; + private Long filterId; + private String family; + private String key; + private String operator; + private Float value; + private String textValue; + private Boolean variation; + + public Long getId() { + return id; + } + + public CriteriaDto setId(Long id) { + this.id = id; + return this; + } + + public Long getFilterId() { + return filterId; + } + + public CriteriaDto setFilterId(Long filterId) { + this.filterId = filterId; + return this; + } + + public String getFamily() { + return family; + } + + public CriteriaDto setFamily(String family) { + this.family = family; + return this; + } + + public String getKey() { + return key; + } + + public CriteriaDto setKey(String key) { + this.key = key; + return this; + } + + public String getOperator() { + return operator; + } + + public CriteriaDto setOperator(String operator) { + this.operator = operator; + return this; + } + + public Float getValue() { + return value; + } + + public CriteriaDto setValue(Float value) { + this.value = value; + return this; + } + + public String getTextValue() { + return textValue; + } + + public CriteriaDto setTextValue(String textValue) { + this.textValue = textValue; + return this; + } + + public Boolean getVariation() { + return variation; + } + + public CriteriaDto setVariation(Boolean variation) { + this.variation = variation; + return this; + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/filter/CriteriaMapper.java b/sonar-core/src/main/java/org/sonar/core/filter/CriteriaMapper.java new file mode 100644 index 00000000000..9d21cd6ac44 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/filter/CriteriaMapper.java @@ -0,0 +1,27 @@ +/* + * 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.core.filter; + +/** + * @since 3.1 + */ +public interface CriteriaMapper { + void insert(CriteriaDto criteriaDto); +} diff --git a/sonar-core/src/main/java/org/sonar/core/filter/FilterColumnDto.java b/sonar-core/src/main/java/org/sonar/core/filter/FilterColumnDto.java new file mode 100644 index 00000000000..e7141a54731 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/filter/FilterColumnDto.java @@ -0,0 +1,96 @@ +/* + * 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.core.filter; + +/** + * @since 3.1 + */ +public final class FilterColumnDto { + private Long id; + private Long filterId; + private String family; + private String key; + private String sortDirection; + private Long orderIndex; + private Boolean variation; + + public Long getId() { + return id; + } + + public FilterColumnDto setId(Long id) { + this.id = id; + return this; + } + + public Long getFilterId() { + return filterId; + } + + public FilterColumnDto setFilterId(Long filterId) { + this.filterId = filterId; + return this; + } + + public String getFamily() { + return family; + } + + public FilterColumnDto setFamily(String family) { + this.family = family; + return this; + } + + public String getKey() { + return key; + } + + public FilterColumnDto setKey(String key) { + this.key = key; + return this; + } + + public String getSortDirection() { + return sortDirection; + } + + public FilterColumnDto setSortDirection(String sortDirection) { + this.sortDirection = sortDirection; + return this; + } + + public Long getOrderIndex() { + return orderIndex; + } + + public FilterColumnDto setOrderIndex(Long orderIndex) { + this.orderIndex = orderIndex; + return this; + } + + public Boolean getVariation() { + return variation; + } + + public FilterColumnDto setVariation(Boolean variation) { + this.variation = variation; + return this; + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/filter/FilterColumnMapper.java b/sonar-core/src/main/java/org/sonar/core/filter/FilterColumnMapper.java new file mode 100644 index 00000000000..b5cc739ea86 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/filter/FilterColumnMapper.java @@ -0,0 +1,27 @@ +/* + * 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.core.filter; + +/** + * @since 3.1 + */ +public interface FilterColumnMapper { + void insert(FilterColumnDto filterColumnDto); +} diff --git a/sonar-core/src/main/java/org/sonar/core/filter/FilterDao.java b/sonar-core/src/main/java/org/sonar/core/filter/FilterDao.java index be2f7b75504..809d91315b1 100644 --- a/sonar-core/src/main/java/org/sonar/core/filter/FilterDao.java +++ b/sonar-core/src/main/java/org/sonar/core/filter/FilterDao.java @@ -46,9 +46,19 @@ public class FilterDao implements BatchComponent, ServerComponent { public void insert(FilterDto filterDto) { SqlSession session = mybatis.openSession(); - FilterMapper mapper = session.getMapper(FilterMapper.class); + FilterMapper filterMapper = session.getMapper(FilterMapper.class); + CriteriaMapper criteriaMapper = session.getMapper(CriteriaMapper.class); + FilterColumnMapper columnMapper = session.getMapper(FilterColumnMapper.class); try { - mapper.insert(filterDto); + filterMapper.insert(filterDto); + for (CriteriaDto criteriaDto : filterDto.getCriterias()) { + criteriaDto.setFilterId(filterDto.getId()); + criteriaMapper.insert(criteriaDto); + } + for (FilterColumnDto filterColumnDto : filterDto.getColumns()) { + filterColumnDto.setFilterId(filterDto.getId()); + columnMapper.insert(filterColumnDto); + } session.commit(); } finally { MyBatis.closeQuietly(session); diff --git a/sonar-core/src/main/java/org/sonar/core/filter/FilterDto.java b/sonar-core/src/main/java/org/sonar/core/filter/FilterDto.java index 1075788ae1a..f37abd80254 100644 --- a/sonar-core/src/main/java/org/sonar/core/filter/FilterDto.java +++ b/sonar-core/src/main/java/org/sonar/core/filter/FilterDto.java @@ -19,9 +19,10 @@ */ package org.sonar.core.filter; -import com.google.common.base.Objects; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; +import com.google.common.collect.Lists; + +import java.util.Collection; +import java.util.List; /** * @since 3.1 @@ -30,12 +31,14 @@ public final class FilterDto { private Long id; private String name; private Long userId; - private boolean shared; - private boolean favourites; + private Boolean shared; + private Boolean favourites; private Long resourceId; private String defaultView; private Long pageSize; private Long periodIndex; + private List criteriaDtos = Lists.newArrayList(); + private List filterColumnDtos = Lists.newArrayList(); public Long getId() { return id; @@ -64,20 +67,20 @@ public final class FilterDto { return this; } - public boolean isShared() { + public Boolean isShared() { return shared; } - public FilterDto setShared(boolean shared) { + public FilterDto setShared(Boolean shared) { this.shared = shared; return this; } - public boolean isFavourites() { + public Boolean isFavourites() { return favourites; } - public FilterDto setFavourites(boolean favourites) { + public FilterDto setFavourites(Boolean favourites) { this.favourites = favourites; return this; } @@ -118,35 +121,21 @@ public final class FilterDto { return this; } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); + public Collection getCriterias() { + return criteriaDtos; } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } + public FilterDto addCriteria(CriteriaDto criteriaDto) { + criteriaDtos.add(criteriaDto); + return this; + } - FilterDto other = (FilterDto) o; - return Objects.equal(id, other.id) && - Objects.equal(name, other.name) && - Objects.equal(userId, other.userId) && - Objects.equal(shared, other.shared) && - Objects.equal(favourites, other.favourites) && - Objects.equal(resourceId, other.resourceId) && - Objects.equal(defaultView, other.defaultView) && - Objects.equal(pageSize, other.pageSize) && - Objects.equal(periodIndex, other.periodIndex); + public Collection getColumns() { + return filterColumnDtos; } - @Override - public int hashCode() { - return Objects.hashCode(id); + public FilterDto addColumn(FilterColumnDto filterColumnDto) { + filterColumnDtos.add(filterColumnDto); + return this; } } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java index 7c71f71ebc1..7847470ddf3 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java @@ -19,10 +19,6 @@ */ package org.sonar.core.persistence; -import org.sonar.core.filter.FilterDto; -import org.sonar.core.filter.FilterMapper; - - import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.ibatis.builder.xml.XMLMapperBuilder; @@ -47,6 +43,12 @@ import org.sonar.core.dashboard.WidgetPropertyDto; import org.sonar.core.dashboard.WidgetPropertyMapper; import org.sonar.core.duplication.DuplicationMapper; import org.sonar.core.duplication.DuplicationUnitDto; +import org.sonar.core.filter.CriteriaDto; +import org.sonar.core.filter.CriteriaMapper; +import org.sonar.core.filter.FilterColumnDto; +import org.sonar.core.filter.FilterColumnMapper; +import org.sonar.core.filter.FilterDto; +import org.sonar.core.filter.FilterMapper; import org.sonar.core.properties.PropertiesMapper; import org.sonar.core.properties.PropertyDto; import org.sonar.core.purge.PurgeMapper; @@ -91,6 +93,8 @@ public class MyBatis implements BatchComponent, ServerComponent { loadAlias(conf, "ActiveDashboard", ActiveDashboardDto.class); loadAlias(conf, "Author", AuthorDto.class); loadAlias(conf, "Filter", FilterDto.class); + loadAlias(conf, "Criteria", CriteriaDto.class); + loadAlias(conf, "FilterColumn", FilterColumnDto.class); loadAlias(conf, "Dashboard", DashboardDto.class); loadAlias(conf, "DuplicationUnit", DuplicationUnitDto.class); loadAlias(conf, "LoadedTemplate", LoadedTemplateDto.class); @@ -109,6 +113,8 @@ public class MyBatis implements BatchComponent, ServerComponent { loadMapper(conf, ActiveDashboardMapper.class); loadMapper(conf, AuthorMapper.class); loadMapper(conf, FilterMapper.class); + loadMapper(conf, CriteriaMapper.class); + loadMapper(conf, FilterColumnMapper.class); loadMapper(conf, DashboardMapper.class); loadMapper(conf, DuplicationMapper.class); loadMapper(conf, LoadedTemplateMapper.class); diff --git a/sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper-oracle.xml b/sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper-oracle.xml new file mode 100644 index 00000000000..257e54e76d4 --- /dev/null +++ b/sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper-oracle.xml @@ -0,0 +1,12 @@ + + + + + + + INSERT INTO criteria (id, filter_id, family, kee, operator, value, text_value, variation) + VALUES (criteria_seq.NEXTVAL, #{filterId, jdbcType=INTEGER}, #{family, jdbcType=VARCHAR}, #{key, jdbcType=VARCHAR}, #{operator, jdbcType=VARCHAR}, #{value, jdbcType=FLOAT}, + #{textValue, jdbcType=VARCHAR}, #{variation}) + + + diff --git a/sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper.xml b/sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper.xml new file mode 100644 index 00000000000..1201f28d9b3 --- /dev/null +++ b/sonar-core/src/main/resources/org/sonar/core/filter/CriteriaMapper.xml @@ -0,0 +1,12 @@ + + + + + + + INSERT INTO criteria (filter_id, family, kee, operator, value, text_value, variation) + VALUES (#{filterId, jdbcType=INTEGER}, #{family, jdbcType=VARCHAR}, #{key, jdbcType=VARCHAR}, #{operator, jdbcType=VARCHAR}, #{value, jdbcType=FLOAT}, + #{textValue, jdbcType=VARCHAR}, #{variation}) + + + diff --git a/sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper-oracle.xml b/sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper-oracle.xml new file mode 100644 index 00000000000..e7705006f51 --- /dev/null +++ b/sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper-oracle.xml @@ -0,0 +1,12 @@ + + + + + + + INSERT INTO filter_columns (id, filter_id, family, kee, sort_direction, order_index, variation) + VALUES (filter_columns_seq.NEXTVAL, #{filterId, jdbcType=INTEGER}, #{family, jdbcType=VARCHAR}, #{key, jdbcType=VARCHAR}, #{sortDirection, jdbcType=VARCHAR}, + #{orderIndex, jdbcType=INTEGER}, #{variation}) + + + diff --git a/sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper.xml b/sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper.xml new file mode 100644 index 00000000000..a7f71467f14 --- /dev/null +++ b/sonar-core/src/main/resources/org/sonar/core/filter/FilterColumnMapper.xml @@ -0,0 +1,12 @@ + + + + + + + INSERT INTO filter_columns (filter_id, family, kee, sort_direction, order_index, variation) + VALUES (#{filterId, jdbcType=INTEGER}, #{family, jdbcType=VARCHAR}, #{key, jdbcType=VARCHAR}, #{sortDirection, jdbcType=VARCHAR}, + #{orderIndex, jdbcType=INTEGER}, #{variation}) + + + diff --git a/sonar-core/src/test/java/org/sonar/core/filters/FilterDaoTest.java b/sonar-core/src/test/java/org/sonar/core/filters/FilterDaoTest.java index 3ba871e4ba8..14460634ef5 100644 --- a/sonar-core/src/test/java/org/sonar/core/filters/FilterDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/filters/FilterDaoTest.java @@ -19,6 +19,10 @@ */ package org.sonar.core.filters; +import org.sonar.core.filter.FilterColumnDto; + +import org.sonar.core.filter.CriteriaDto; + import org.junit.Before; import org.junit.Test; import org.sonar.core.filter.FilterDao; @@ -60,8 +64,25 @@ public class FilterDaoTest extends DaoTestCase { filterDto.setPageSize(10L); filterDto.setPeriodIndex(1L); + CriteriaDto criteriaDto = new CriteriaDto(); + criteriaDto.setFamily("family"); + criteriaDto.setKey("key"); + criteriaDto.setOperator("="); + criteriaDto.setValue(1.5f); + criteriaDto.setTextValue("1.5"); + criteriaDto.setVariation(true); + filterDto.addCriteria(criteriaDto); + + FilterColumnDto filterColumnDto = new FilterColumnDto(); + filterColumnDto.setFamily("family"); + filterColumnDto.setKey("key"); + filterColumnDto.setSortDirection("ASC"); + filterColumnDto.setOrderIndex(2L); + filterColumnDto.setVariation(true); + filterDto.addColumn(filterColumnDto); + dao.insert(filterDto); - checkTables("shouldInsert", "filters"); + checkTables("shouldInsert", "filters", "criteria", "filter_columns"); } } \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/filters/FilterDaoTest/shouldInsert-result.xml b/sonar-core/src/test/resources/org/sonar/core/filters/FilterDaoTest/shouldInsert-result.xml index 5bef3836c4b..2164c2bedce 100644 --- a/sonar-core/src/test/resources/org/sonar/core/filters/FilterDaoTest/shouldInsert-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/filters/FilterDaoTest/shouldInsert-result.xml @@ -12,4 +12,25 @@ period_index="1" /> + + + + 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 5c65c30febf..ef395f6b71f 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 @@ -27,10 +27,41 @@ package org.sonar.api.web; * @since 3.1 */ public class Filter { + // Criteria + // Columns + // + private boolean shared; + private boolean favouritesOnly; + private String defaultPeriod; + private Filter() { // The factory method should be used } + public boolean isShared() { + return shared; + } + + public void setShared(boolean shared) { + this.shared = shared; + } + + public boolean isFavouritesOnly() { + return favouritesOnly; + } + + public void setFavouritesOnly(boolean favouritesOnly) { + this.favouritesOnly = favouritesOnly; + } + + public String getDefaultPeriod() { + return defaultPeriod; + } + + public void setDefaultPeriod(String defaultPeriod) { + this.defaultPeriod = defaultPeriod; + } + /** * Creates a new {@link Filter}. */ 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 4cb131361fb..757765d3726 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 @@ -20,7 +20,6 @@ package org.sonar.server.startup; import com.google.common.collect.ImmutableList; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.utils.TimeProfiler; @@ -31,19 +30,17 @@ import org.sonar.core.filter.FilterDto; import org.sonar.core.template.LoadedTemplateDao; import org.sonar.core.template.LoadedTemplateDto; -import java.util.Comparator; import java.util.List; /** * @since 3.1 */ public final class RegisterNewFilters { - private static final Logger LOG = LoggerFactory.getLogger(RegisterNewFilters.class); - private List filterTemplates; - private FilterDao filterDao; - private LoadedTemplateDao loadedTemplateDao; + private final List filterTemplates; + private final FilterDao filterDao; + private final LoadedTemplateDao loadedTemplateDao; /** * Constructor used in case there is no {@link FilterTemplate}. @@ -71,7 +68,7 @@ public final class RegisterNewFilters { profiler.stop(); } - protected boolean shouldRegister(String filterName) { + private boolean shouldRegister(String filterName) { return loadedTemplateDao.countByTypeAndKey(LoadedTemplateDto.FILTER_TYPE, filterName) == 0; } @@ -87,22 +84,10 @@ public final class RegisterNewFilters { } protected FilterDto createDtoFromExtension(String name, Filter filter) { - FilterDto filterDto = new FilterDto(); - filterDto.setName(name); - - // filterDto.setShared(); - // filterDto.setFavourites(); - // filterDto.setResourceId(); - // filterDto.setDefaultView(); - // filterDto.setPageSize(); - // filterDto.setPeriodIndex(); - - return filterDto; - } - - protected static class FilterDtoComparator implements Comparator { - public int compare(FilterDto f1, FilterDto f2) { - return f1.getName().compareToIgnoreCase(f2.getName()); - } + return new FilterDto() + .setName(name) + .setShared(filter.isShared()) + .setFavourites(filter.isFavouritesOnly()) + .setDefaultView(filter.getDefaultPeriod()); } -} \ No newline at end of file +} 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 e1e7c751eef..47e6853eb19 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 @@ -21,7 +21,6 @@ package org.sonar.server.startup; import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; import org.sonar.api.web.Filter; import org.sonar.api.web.FilterTemplate; import org.sonar.core.filter.FilterDao; @@ -29,13 +28,12 @@ import org.sonar.core.filter.FilterDto; import org.sonar.core.template.LoadedTemplateDao; import org.sonar.core.template.LoadedTemplateDto; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -43,20 +41,22 @@ public class RegisterNewFiltersTest { private RegisterNewFilters register; private FilterDao filterDao; private LoadedTemplateDao loadedTemplateDao; - private FilterTemplate fakeFilterTemplate; + private FilterTemplate filterTemplate; @Before public void init() { - filterDao = Mockito.mock(FilterDao.class); - loadedTemplateDao = Mockito.mock(LoadedTemplateDao.class); - - fakeFilterTemplate = new FakeFilter(); + filterDao = mock(FilterDao.class); + loadedTemplateDao = mock(LoadedTemplateDao.class); + filterTemplate = mock(FilterTemplate.class); - register = new RegisterNewFilters(new FilterTemplate[] {fakeFilterTemplate}, filterDao, loadedTemplateDao); + register = new RegisterNewFilters(new FilterTemplate[] {filterTemplate}, filterDao, loadedTemplateDao); } @Test - public void should_start() { + public void should_insert_filters_on_start() { + when(loadedTemplateDao.countByTypeAndKey(eq(LoadedTemplateDto.FILTER_TYPE), anyString())).thenReturn(0); + when(filterTemplate.createFilter()).thenReturn(Filter.create()); + register.start(); verify(filterDao).insert(any(FilterDto.class)); @@ -64,26 +64,29 @@ public class RegisterNewFiltersTest { } @Test - public void should_register_filter_if_not_already_loaded() { - when(loadedTemplateDao.countByTypeAndKey(LoadedTemplateDto.FILTER_TYPE, "Fake")).thenReturn(0); - - boolean shouldRegister = register.shouldRegister("Fake"); + public void should_insert_nothing_if_no_template_is_available() { + register = new RegisterNewFilters(filterDao, loadedTemplateDao); + register.start(); - assertThat(shouldRegister).isTrue(); + verify(filterDao, never()).insert(any(FilterDto.class)); + verify(loadedTemplateDao, never()).insert(any(LoadedTemplateDto.class)); } @Test - public void should_not_register_if_already_loaded() { - when(loadedTemplateDao.countByTypeAndKey(LoadedTemplateDto.FILTER_TYPE, "Fake")).thenReturn(1); + public void should_insert_nothing_if_templates_are_alreday_loaded() { + when(loadedTemplateDao.countByTypeAndKey(eq(LoadedTemplateDto.FILTER_TYPE), anyString())).thenReturn(1); - boolean shouldRegister = register.shouldRegister("Fake"); + register.start(); - assertThat(shouldRegister).isFalse(); + verify(filterDao, never()).insert(any(FilterDto.class)); + verify(loadedTemplateDao, never()).insert(any(LoadedTemplateDto.class)); } @Test public void should_register_filter() { - FilterDto filterDto = register.register("Fake", fakeFilterTemplate.createFilter()); + when(filterTemplate.createFilter()).thenReturn(Filter.create()); + + FilterDto filterDto = register.register("Fake", filterTemplate.createFilter()); assertThat(filterDto).isNotNull(); verify(filterDao).insert(filterDto); @@ -91,34 +94,30 @@ public class RegisterNewFiltersTest { } @Test - public void should_create_dto_from_extension() { - FilterDto dto = register.createDtoFromExtension("Fake", fakeFilterTemplate.createFilter()); + public void should_not_recreate_filter() { + when(filterDao.findFilter("Fake")).thenReturn(new FilterDto()); - assertThat(dto.getUserId()).isNull(); - assertThat(dto.getName()).isEqualTo("Fake"); + FilterDto filterDto = register.register("Fake", null); + + assertThat(filterDto).isNull(); + verify(filterDao, never()).insert(filterDto); + verify(loadedTemplateDao).insert(eq(new LoadedTemplateDto("Fake", LoadedTemplateDto.FILTER_TYPE))); } @Test - public void should_compare_filter() { - FilterDto f1 = new FilterDto().setName("foo"); - FilterDto f2 = new FilterDto().setName("Bar"); - - List filterDtos = Arrays.asList(f1, f2); - Collections.sort(filterDtos, new RegisterNewFilters.FilterDtoComparator()); + 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); - assertThat(filterDtos).onProperty("name").containsExactly("Bar", "foo"); - } + FilterDto dto = register.createDtoFromExtension("Fake", filterTemplate.createFilter()); - public class FakeFilter extends FilterTemplate { - @Override - public String getName() { - return "Fake"; - } - - @Override - public Filter createFilter() { - Filter filter = Filter.create(); - return filter; - } + assertThat(dto.getUserId()).isNull(); + assertThat(dto.getName()).isEqualTo("Fake"); + assertThat(dto.isShared()).isTrue(); + assertThat(dto.isFavourites()).isFalse(); + assertThat(dto.getDefaultView()).isEqualTo("list"); } } -- 2.39.5