diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-12-04 13:18:06 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-12-04 13:18:19 +0100 |
commit | b22c1fdba382d361c9c59deba0a45d7d7d00ca95 (patch) | |
tree | 11e35e73987b66a03f516ef1aa3c1c640320b5a9 /sonar-core/src | |
parent | 44b9092bed4ebdd6cf213da6a6b01c1d0ff030a5 (diff) | |
download | sonarqube-b22c1fdba382d361c9c59deba0a45d7d7d00ca95.tar.gz sonarqube-b22c1fdba382d361c9c59deba0a45d7d7d00ca95.zip |
SONAR-3825 delete old mechanism of measure filters
Diffstat (limited to 'sonar-core/src')
29 files changed, 323 insertions, 1119 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/filter/CriterionDto.java b/sonar-core/src/main/java/org/sonar/core/filter/CriterionDto.java deleted file mode 100644 index d3ff4a1e7f6..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/filter/CriterionDto.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * 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 CriterionDto { - private Long id; - private Long filterId; - private String family; - private String key; - private String operator; - private String textValue; - private Float value; - private Boolean variation; - - /** - * @return the id - */ - public Long getId() { - return id; - } - - /** - * @return the family - */ - public String getFamily() { - return family; - } - - /** - * @return the filter id - */ - public Long getFilterId() { - return filterId; - } - - /** - * @return the key - */ - public String getKey() { - return key; - } - - /** - * @return the operator - */ - public String getOperator() { - return operator; - } - - /** - * @return the text value - */ - public String getTextValue() { - return textValue; - } - - /** - * @return the value - */ - public Float getValue() { - return value; - } - - /** - * @return the variation - */ - public Boolean getVariation() { - return variation; - } - - /** - * @param family the id to set - */ - public CriterionDto setId(Long id) { - this.id = id; - return this; - } - - /** - * @param family the family to set - */ - public CriterionDto setFamily(String family) { - this.family = family; - return this; - } - - /** - * @param filterId the filter id to set - */ - public CriterionDto setFilterId(Long filterId) { - this.filterId = filterId; - return this; - } - - /** - * @param key the key to set - */ - public CriterionDto setKey(String key) { - this.key = key; - return this; - } - - /** - * @param operator the operator to set - */ - public CriterionDto setOperator(String operator) { - this.operator = operator; - return this; - } - - /** - * @param textValue the textValue to set - */ - public CriterionDto setTextValue(String textValue) { - this.textValue = textValue; - return this; - } - - /** - * @param value the value to set - */ - public CriterionDto setValue(Float value) { - this.value = value; - return this; - } - - /** - * @param variation the variation to set - */ - public CriterionDto setVariation(Boolean variation) { - this.variation = variation; - return this; - } -} 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 deleted file mode 100644 index d69d617cc92..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/filter/FilterColumnDto.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * 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 Long orderIndex; - private String sortDirection; - private Boolean variation; - - /** - * @return the id - */ - public Long getId() { - return id; - } - - /** - * @return the family - */ - public String getFamily() { - return family; - } - - /** - * @return the filter id - */ - public Long getFilterId() { - return filterId; - } - - /** - * @return the key - */ - public String getKey() { - return key; - } - - /** - * @return the order index - */ - public Long getOrderIndex() { - return orderIndex; - } - - /** - * @return the sort direction - */ - public String getSortDirection() { - return sortDirection; - } - - /** - * @return the variation - */ - public Boolean getVariation() { - return variation; - } - - /** - * @param family the family to set - */ - public FilterColumnDto setFamily(String family) { - this.family = family; - return this; - } - - /** - * @param family the id to set - */ - public FilterColumnDto setId(Long id) { - this.id = id; - return this; - } - - /** - * @param filterId the filterId to set - */ - public FilterColumnDto setFilterId(Long filterId) { - this.filterId = filterId; - return this; - } - - /** - * @param key the key to set - */ - public FilterColumnDto setKey(String key) { - this.key = key; - return this; - } - - /** - * @param orderIndex the orderIndex to set - */ - public FilterColumnDto setOrderIndex(Long orderIndex) { - this.orderIndex = orderIndex; - return this; - } - - /** - * @param sortDirection the sortDirection to set - */ - public FilterColumnDto setSortDirection(String sortDirection) { - this.sortDirection = sortDirection; - return this; - } - - /** - * @param variation the variation to set - */ - 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 deleted file mode 100644 index 60706d87a17..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/filter/FilterColumnMapper.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 { - /** - * Insert a {@link FilterColumnDto}. - * - * @param filterColumnDto the filter column to insert - */ - void insert(FilterColumnDto filterColumnDto); -} 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 deleted file mode 100644 index cde7262d49f..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/filter/FilterDto.java +++ /dev/null @@ -1,213 +0,0 @@ -/* - * 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; - -import com.google.common.collect.Lists; - -import java.util.Collection; -import java.util.List; - -/** - * @since 3.1 - */ -public final class FilterDto { - private Long id; - private Long userId; - private String defaultView; - private Boolean favourites; - private String key; - private String name; - private Long pageSize; - private Long periodIndex; - private Long resourceId; - private Boolean shared; - private List<CriterionDto> criteria = Lists.newArrayList(); - private List<FilterColumnDto> filterColumns = Lists.newArrayList(); - - /** - * Add a {@link CriterionDto} to the list. - * - * @param criterion the criterion to add - */ - public FilterDto add(CriterionDto criterion) { - criteria.add(criterion); - return this; - } - - /** - * Add a {@link FilterColumnDto} to the list. - * - * @param filterColumn the column to add - */ - public FilterDto add(FilterColumnDto filterColumn) { - filterColumns.add(filterColumn); - return this; - } - - /** - * @return the column list - */ - public Collection<FilterColumnDto> getColumns() { - return filterColumns; - } - - /** - * @return the criterion list - */ - public Collection<CriterionDto> getCriteria() { - return criteria; - } - - /** - * @return the defaut view - */ - public String getDefaultView() { - return defaultView; - } - - /** - * @return <code>true</code> if the filter returns only favourites - */ - public Boolean getFavourites() { - return favourites; - } - - /** - * @return the column list - */ - public List<FilterColumnDto> getFilterColumns() { - return filterColumns; - } - - /** - * @return the id - */ - public Long getId() { - return id; - } - - /** - * @return the key - */ - public String getKey() { - return key; - } - - /** - * @return the name - */ - public String getName() { - return name; - } - - /** - * @return the page size - */ - public Long getPageSize() { - return pageSize; - } - - /** - * @return the period index - */ - public Long getPeriodIndex() { - return periodIndex; - } - - /** - * @return the resource id - */ - public Long getResourceId() { - return resourceId; - } - - /** - * @return <code>true</code> if the filter is shared - */ - public Boolean getShared() { - return shared; - } - - /** - * @return the user id - */ - public Long getUserId() { - return userId; - } - - /** - * @return <code>true</code> if the filter displays only favourite resources. - */ - public Boolean isFavourites() { - return favourites; - } - - /** - * @return <code>true</code> if the filter is shared - */ - public Boolean isShared() { - return shared; - } - - /** - * @param defaultView the defaultView to set - */ - public FilterDto setDefaultView(String defaultView) { - this.defaultView = defaultView; - return this; - } - - /** - * @param favourites the favourites to set - */ - public FilterDto setFavourites(Boolean favourites) { - this.favourites = favourites; - return this; - } - - /** - * @param key the key to set - */ - public FilterDto setKey(String key) { - this.key = key; - return this; - } - - /** - * @param name the name to set - */ - public FilterDto setName(String name) { - this.name = name; - return this; - } - - public FilterDto setPageSize(Long pageSize) { - this.pageSize = pageSize; - return this; - } - - /** - * @param shared the shared to set - */ - public FilterDto setShared(Boolean shared) { - this.shared = shared; - return this; - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/filter/FilterMapper.java b/sonar-core/src/main/java/org/sonar/core/filter/FilterMapper.java deleted file mode 100644 index b3b2fa3f2bf..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/filter/FilterMapper.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 FilterMapper { - /** - * Find a {@link FilterDto} by its unique key. - * - * @param key the key to search on - * @return the filter - */ - FilterDto findFilter(String key); - - /** - * Insert a {@link FilterDto}. - * - * @param filterDto the filter to insert - */ - void insert(FilterDto filterDto); -} diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterContext.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterContext.java index 9cf990ee4ee..728ad0e9db3 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterContext.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterContext.java @@ -27,7 +27,7 @@ class MeasureFilterContext { private Long userId; private SnapshotDto baseSnapshot; private String sql; - private String json; + private String data; Long getUserId() { return userId; @@ -56,19 +56,19 @@ class MeasureFilterContext { return this; } - String getJson() { - return json; + String getData() { + return data; } - MeasureFilterContext setJson(String json) { - this.json = json; + MeasureFilterContext setData(String data) { + this.data = data; return this; } @Override public String toString() { return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) - .append("filter", json) + .append("filter", data) .append("sql", sql) .append("user", userId) .toString(); diff --git a/sonar-core/src/main/java/org/sonar/core/filter/FilterDao.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDao.java index 8521bf4e48f..3a0fc891a5d 100644 --- a/sonar-core/src/main/java/org/sonar/core/filter/FilterDao.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDao.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.core.measure; import org.apache.ibatis.session.SqlSession; import org.sonar.api.BatchComponent; @@ -25,43 +25,33 @@ import org.sonar.api.ServerComponent; import org.sonar.core.persistence.MyBatis; /** - * @since 3.1 + * @since 3.4 */ -public class FilterDao implements BatchComponent, ServerComponent { +public class MeasureFilterDao implements BatchComponent, ServerComponent { private MyBatis mybatis; - public FilterDao(MyBatis mybatis) { + public MeasureFilterDao(MyBatis mybatis) { this.mybatis = mybatis; } - public FilterDto findFilter(String name) { + public MeasureFilterDto findSystemFilterByName(String name) { SqlSession session = mybatis.openSession(); try { - FilterMapper mapper = session.getMapper(FilterMapper.class); - return mapper.findFilter(name); + MeasureFilterMapper mapper = session.getMapper(MeasureFilterMapper.class); + return mapper.findSystemFilterByName(name); } finally { MyBatis.closeQuietly(session); } } - public void insert(FilterDto filterDto) { + public void insert(MeasureFilterDto filter) { SqlSession session = mybatis.openSession(); - FilterMapper filterMapper = session.getMapper(FilterMapper.class); - CriterionMapper criteriaMapper = session.getMapper(CriterionMapper.class); - FilterColumnMapper columnMapper = session.getMapper(FilterColumnMapper.class); + MeasureFilterMapper mapper = session.getMapper(MeasureFilterMapper.class); try { - filterMapper.insert(filterDto); - for (CriterionDto criteriaDto : filterDto.getCriteria()) { - criteriaDto.setFilterId(filterDto.getId()); - criteriaMapper.insert(criteriaDto); - } - for (FilterColumnDto filterColumnDto : filterDto.getColumns()) { - filterColumnDto.setFilterId(filterDto.getId()); - columnMapper.insert(filterColumnDto); - } + mapper.insert(filter); session.commit(); } finally { MyBatis.closeQuietly(session); } } -} +}
\ No newline at end of file diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDecoder.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDecoder.java deleted file mode 100644 index eed8ded2246..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDecoder.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * 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.measure; - -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; -import org.sonar.api.ServerComponent; -import org.sonar.api.measures.Metric; -import org.sonar.api.measures.MetricFinder; -import org.sonar.api.utils.DateUtils; - -import java.util.Calendar; -import java.util.Date; -import java.util.List; - -public class MeasureFilterDecoder implements ServerComponent { - private MetricFinder metricFinder; - private JSONParser parser = new JSONParser(); - - public MeasureFilterDecoder(MetricFinder metricFinder) { - this.metricFinder = metricFinder; - } - - public MeasureFilter decode(String text) throws ParseException { - MeasureFilter filter = new MeasureFilter(); - JSONObject map = (JSONObject) parser.parse(text); - parseResourceConditions(filter, map); - parseMeasureConditions(map, filter); - parseSorting(map, filter); - return filter; - } - - private void parseResourceConditions(MeasureFilter filter, JSONObject map) { - filter.setBaseResourceKey((String) map.get("base")); - if (map.containsKey("onBaseChildren")) { - filter.setOnBaseResourceChildren(((Boolean) map.get("onBaseChildren")).booleanValue()); - } - filter.setResourceScopes((List<String>) map.get("scopes")); - filter.setResourceQualifiers((List<String>) map.get("qualifiers")); - filter.setResourceLanguages((List<String>) map.get("languages")); - filter.setResourceName((String) map.get("name")); - filter.setResourceKeyRegexp((String) map.get("keyRegexp")); - - if (map.containsKey("fromDate")) { - filter.setFromDate(toDate(map, "fromDate")); - } - if (map.containsKey("afterDays")) { - filter.setFromDate(toDays(map, "afterDays")); - } - if (map.containsKey("toDate")) { - filter.setToDate(toDate(map, "toDate")); - } - if (map.containsKey("beforeDays")) { - filter.setToDate(toDays(map, "beforeDays")); - } - if (map.containsKey("favourites")) { - filter.setUserFavourites(((Boolean) map.get("favourites")).booleanValue()); - } - } - - private void parseSorting(JSONObject map, MeasureFilter filter) { - if (map.containsKey("sortAsc")) { - filter.setSortAsc(((Boolean) map.get("sortAsc")).booleanValue()); - } - String s = (String) map.get("sortField"); - if (s != null) { - filter.setSortOn(MeasureFilterSort.Field.valueOf(s)); - } - s = (String) map.get("sortField"); - if (s != null) { - filter.setSortOn(MeasureFilterSort.Field.valueOf((String) map.get("sortField"))); - } - s = (String) map.get("sortMetric"); - if (s != null) { - filter.setSortOnMetric(metricFinder.findByKey(s)); - } - if (map.containsKey("sortPeriod")) { - filter.setSortOnPeriod(((Long) map.get("sortPeriod")).intValue()); - } - } - - private void parseMeasureConditions(JSONObject map, MeasureFilter filter) { - JSONArray conditions = (JSONArray) map.get("conditions"); - if (conditions != null) { - for (Object obj : conditions) { - JSONObject c = (JSONObject) obj; - Metric metric = metricFinder.findByKey((String) c.get("metric")); - String operator = (String) c.get("op"); - Double value = (Double) c.get("val"); - MeasureFilterCondition condition = new MeasureFilterCondition(metric, MeasureFilterCondition.Operator.fromSql(operator), value); - if (c.containsKey("period")) { - condition.setPeriod(((Long) c.get("period")).intValue()); - } - filter.addCondition(condition); - } - } - } - - private static Date toDate(JSONObject map, String key) { - String date = (String) map.get(key); - if (date != null) { - return DateUtils.parseDate(date); - } - return null; - } - - private static Date toDays(JSONObject map, String key) { - int days = ((Long) map.get(key)).intValue(); - Date date = org.apache.commons.lang.time.DateUtils.truncate(new Date(), Calendar.DATE); - date = org.apache.commons.lang.time.DateUtils.addDays(date, -days); - return date; - } - -} diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDto.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDto.java new file mode 100644 index 00000000000..7b888e2a8b0 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterDto.java @@ -0,0 +1,108 @@ +/* + * 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.measure; + +import java.util.Date; + +/** + * @since 3.4 + */ +public class MeasureFilterDto { + private Long id; + private String name; + private Long userId; + private Boolean shared; + private String description; + private String data; + private Date createdAt; + private Date updatedAt; + + public Long getId() { + return id; + } + + public MeasureFilterDto setId(Long id) { + this.id = id; + return this; + } + + public String getName() { + return name; + } + + public MeasureFilterDto setName(String name) { + this.name = name; + return this; + } + + public Long getUserId() { + return userId; + } + + public MeasureFilterDto setUserId(Long userId) { + this.userId = userId; + return this; + } + + public Boolean isShared() { + return shared; + } + + public MeasureFilterDto setShared(Boolean shared) { + this.shared = shared; + return this; + } + + public String getDescription() { + return description; + } + + public MeasureFilterDto setDescription(String description) { + this.description = description; + return this; + } + + public String getData() { + return data; + } + + public MeasureFilterDto setData(String data) { + this.data = data; + return this; + } + + public Date getCreatedAt() { + return createdAt; + } + + public MeasureFilterDto setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + return this; + } + + public Date getUpdatedAt() { + return updatedAt; + } + + public MeasureFilterDto setUpdatedAt(Date updatedAt) { + this.updatedAt = updatedAt; + return this; + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java index a161e7e9d45..d394709789e 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java @@ -20,9 +20,6 @@ package org.sonar.core.measure; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Joiner; -import com.google.common.collect.Maps; -import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.SystemUtils; import org.json.simple.parser.ParseException; import org.slf4j.Logger; @@ -35,46 +32,28 @@ import java.util.List; import java.util.Map; public class MeasureFilterEngine implements ServerComponent { - private static final Logger FILTER_LOG = LoggerFactory.getLogger("org.sonar.MEASURE_FILTER"); - private final MeasureFilterDecoder decoder; private final MeasureFilterFactory factory; private final MeasureFilterExecutor executor; - public MeasureFilterEngine(MeasureFilterDecoder decoder, MeasureFilterFactory factory, MeasureFilterExecutor executor) { - this.decoder = decoder; + public MeasureFilterEngine(MeasureFilterFactory factory, MeasureFilterExecutor executor) { this.executor = executor; this.factory = factory; } - public List<MeasureFilterRow> execute(String filterJson, @Nullable Long userId) throws ParseException { - return execute(filterJson, userId, FILTER_LOG); - } - - public List<MeasureFilterRow> execute2(Map<String, Object> filterMap, @Nullable Long userId) throws ParseException { - Logger logger = FILTER_LOG; - MeasureFilterContext context = new MeasureFilterContext(); - context.setUserId(userId); - try { - long start = System.currentTimeMillis(); - MeasureFilter filter = factory.create(filterMap); - context.setJson(filter.toString()); - List<MeasureFilterRow> rows = executor.execute(filter, context); - log(context, rows, (System.currentTimeMillis() - start), logger); - return rows; - } catch (Exception e) { - throw new IllegalStateException("Fail to execute filter: " + context, e); - } + public List<MeasureFilterRow> execute(Map<String, Object> filterMap, @Nullable Long userId) throws ParseException { + return execute(filterMap, userId, LoggerFactory.getLogger("org.sonar.MEASURE_FILTER")); } @VisibleForTesting - List<MeasureFilterRow> execute(String filterJson, @Nullable Long userId, Logger logger) { + List<MeasureFilterRow> execute(Map<String, Object> filterMap, @Nullable Long userId, Logger logger) throws ParseException { + MeasureFilterContext context = new MeasureFilterContext(); - context.setJson(filterJson); context.setUserId(userId); + context.setData(filterMap.toString()); try { long start = System.currentTimeMillis(); - MeasureFilter filter = decoder.decode(filterJson); + MeasureFilter filter = factory.create(filterMap); List<MeasureFilterRow> rows = executor.execute(filter, context); log(context, rows, (System.currentTimeMillis() - start), logger); return rows; @@ -87,7 +66,7 @@ public class MeasureFilterEngine implements ServerComponent { if (logger.isDebugEnabled()) { StringBuilder log = new StringBuilder(); log.append(SystemUtils.LINE_SEPARATOR); - log.append(" filter: ").append(context.getJson()).append(SystemUtils.LINE_SEPARATOR); + log.append(" filter: ").append(context.getData()).append(SystemUtils.LINE_SEPARATOR); log.append(" sql: ").append(context.getSql()).append(SystemUtils.LINE_SEPARATOR); log.append("results: ").append(rows.size()).append(" rows in ").append(durationMs).append("ms").append(SystemUtils.LINE_SEPARATOR); logger.debug(log.toString()); diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterFactory.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterFactory.java index 010ae4e9e8a..42a92d07fdd 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterFactory.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterFactory.java @@ -87,9 +87,6 @@ public class MeasureFilterFactory implements ServerComponent { filter.addCondition(condition); } } -// if (map.containsKey("sortPeriod")) { -// filter.setSortOnPeriod(((Long) map.get("sortPeriod")).intValue()); -// } return filter; } diff --git a/sonar-core/src/main/java/org/sonar/core/filter/CriterionMapper.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterMapper.java index 25ca37bdb0f..635e90a598d 100644 --- a/sonar-core/src/main/java/org/sonar/core/filter/CriterionMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterMapper.java @@ -17,16 +17,13 @@ * 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; +package org.sonar.core.measure; /** - * @since 3.1 + * @since 3.4 */ -public interface CriterionMapper { - /** - * Insert a {@link CriterionDto}. - * - * @param criterionDto the criterion to insert - */ - void insert(CriterionDto criterionDto); +public interface MeasureFilterMapper { + MeasureFilterDto findSystemFilterByName(String name); + + void insert(MeasureFilterDto filter); } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java b/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java index 0dd2a13dff4..ee008ae6467 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java @@ -23,7 +23,7 @@ import com.google.common.collect.ImmutableList; import org.sonar.core.dashboard.ActiveDashboardDao; import org.sonar.core.dashboard.DashboardDao; import org.sonar.core.duplication.DuplicationDao; -import org.sonar.core.filter.FilterDao; +import org.sonar.core.measure.MeasureFilterDao; import org.sonar.core.properties.PropertiesDao; import org.sonar.core.purge.PurgeDao; import org.sonar.core.resource.ResourceDao; @@ -48,10 +48,10 @@ public final class DaoUtils { return ImmutableList.of( ActiveDashboardDao.class, AuthorDao.class, - FilterDao.class, DashboardDao.class, DuplicationDao.class, LoadedTemplateDao.class, + MeasureFilterDao.class, PropertiesDao.class, PurgeDao.class, ResourceIndexerDao.class, diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseUtils.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseUtils.java index dff45f0fe94..7819ab82d3f 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseUtils.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseUtils.java @@ -53,13 +53,10 @@ public final class DatabaseUtils { "characteristics", "characteristic_edges", "characteristic_properties", - "criteria", "dashboards", "dependencies", "duplications_index", "events", - "filters", - "filter_columns", "groups", "groups_users", "group_roles", diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java index f2d34dcd089..17380180b3e 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java @@ -32,7 +32,7 @@ import java.util.List; */ public class DatabaseVersion implements BatchComponent, ServerComponent { - public static final int LAST_VERSION = 359; + public static final int LAST_VERSION = 361; public static enum Status { UP_TO_DATE, REQUIRES_UPGRADE, REQUIRES_DOWNGRADE, FRESH_INSTALL 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 bf46da2839d..2cecc87304a 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 @@ -53,12 +53,8 @@ import org.sonar.core.dependency.ResourceSnapshotDto; import org.sonar.core.dependency.ResourceSnapshotMapper; import org.sonar.core.duplication.DuplicationMapper; import org.sonar.core.duplication.DuplicationUnitDto; -import org.sonar.core.filter.CriterionDto; -import org.sonar.core.filter.CriterionMapper; -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.measure.MeasureFilterDto; +import org.sonar.core.measure.MeasureFilterMapper; import org.sonar.core.properties.PropertiesMapper; import org.sonar.core.properties.PropertyDto; import org.sonar.core.purge.PurgeMapper; @@ -115,15 +111,13 @@ public class MyBatis implements BatchComponent, ServerComponent { loadAlias(conf, "ActiveDashboard", ActiveDashboardDto.class); loadAlias(conf, "Author", AuthorDto.class); - loadAlias(conf, "Filter", FilterDto.class); - loadAlias(conf, "Criterion", CriterionDto.class); - loadAlias(conf, "FilterColumn", FilterColumnDto.class); loadAlias(conf, "Dashboard", DashboardDto.class); loadAlias(conf, "Dependency", DependencyDto.class); loadAlias(conf, "DuplicationUnit", DuplicationUnitDto.class); loadAlias(conf, "Group", GroupDto.class); loadAlias(conf, "GroupRole", GroupRoleDto.class); loadAlias(conf, "LoadedTemplate", LoadedTemplateDto.class); + loadAlias(conf, "MeasureFilter", MeasureFilterDto.class); loadAlias(conf, "Property", PropertyDto.class); loadAlias(conf, "PurgeableSnapshot", PurgeableSnapshotDto.class); loadAlias(conf, "Resource", ResourceDto.class); @@ -142,8 +136,8 @@ public class MyBatis implements BatchComponent, ServerComponent { loadAlias(conf, "MeasureModel", MeasureModel.class); loadAlias(conf, "MeasureData", MeasureData.class); - Class<?>[] mappers = {ActiveDashboardMapper.class, AuthorMapper.class, FilterMapper.class, CriterionMapper.class, FilterColumnMapper.class, DashboardMapper.class, - DependencyMapper.class, DuplicationMapper.class, LoadedTemplateMapper.class, PropertiesMapper.class, PurgeMapper.class, + Class<?>[] mappers = {ActiveDashboardMapper.class, AuthorMapper.class, DashboardMapper.class, + DependencyMapper.class, DuplicationMapper.class, LoadedTemplateMapper.class, MeasureFilterMapper.class, PropertiesMapper.class, PurgeMapper.class, ResourceKeyUpdaterMapper.class, ResourceIndexerMapper.class, ResourceMapper.class, ResourceSnapshotMapper.class, ReviewCommentMapper.class, ReviewMapper.class, RoleMapper.class, RuleMapper.class, SchemaMigrationMapper.class, SemaphoreMapper.class, UserMapper.class, WidgetMapper.class, WidgetPropertyMapper.class, MeasureMapper.class}; diff --git a/sonar-core/src/main/resources/org/sonar/core/measure/MeasureFilterMapper.xml b/sonar-core/src/main/resources/org/sonar/core/measure/MeasureFilterMapper.xml new file mode 100644 index 00000000000..ea9823d3b17 --- /dev/null +++ b/sonar-core/src/main/resources/org/sonar/core/measure/MeasureFilterMapper.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> + +<mapper namespace="org.sonar.core.measure.MeasureFilterMapper"> + + <select id="findSystemFilterByName" parameterType="string" resultType="MeasureFilter"> + select id, name, user_id as "userId", shared, description, data, created_at as "createdAt", updated_at as "updatedAt" + from measure_filters WHERE user_id is null and name=#{id} + </select> + + <insert id="insert" parameterType="MeasureFilter" useGeneratedKeys="true" keyProperty="id"> + INSERT INTO measure_filters (name, user_id, shared, description, data, created_at, updated_at) + VALUES (#{name}, #{userId}, #{shared}, #{description}, #{data}, #{createdAt}, #{updatedAt}) + </insert> + + <!-- Oracle --> + <insert id="insert" databaseId="oracle" parameterType="MeasureFilter" keyColumn="id" useGeneratedKeys="true" keyProperty="id"> + <selectKey order="BEFORE" resultType="Long" keyProperty="id"> + select measure_filters_seq.NEXTVAL from DUAL + </selectKey> + INSERT INTO id, measure_filters (name, user_id, shared, description, data, created_at, updated_at) + VALUES (#{id}, #{name}, #{userId}, #{shared}, #{description}, #{data}, #{createdAt}, #{updatedAt}) + </insert> + +</mapper> diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql index aba45d45a1b..605577c00cb 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql @@ -187,6 +187,8 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('356'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('357'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('358'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('359'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('360'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('361'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '2011-09-26 22:27:48.0', '2011-09-26 22:27:48.0', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl index 76f26b33d85..bb29d4e21d2 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl @@ -18,17 +18,6 @@ CREATE TABLE "CHARACTERISTIC_EDGES" ( "PARENT_ID" INTEGER ); -CREATE TABLE "CRITERIA" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "FILTER_ID" INTEGER, - "FAMILY" VARCHAR(100), - "KEE" VARCHAR(100), - "OPERATOR" VARCHAR(20), - "VALUE" DOUBLE, - "TEXT_VALUE" VARCHAR(256), - "VARIATION" BOOLEAN -); - CREATE TABLE "DEPENDENCIES" ( "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "FROM_SNAPSHOT_ID" INTEGER, @@ -87,16 +76,6 @@ CREATE TABLE "WIDGETS" ( "RESOURCE_ID" INTEGER ); -CREATE TABLE "FILTER_COLUMNS" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "FILTER_ID" INTEGER, - "FAMILY" VARCHAR(100), - "KEE" VARCHAR(100), - "SORT_DIRECTION" VARCHAR(5), - "ORDER_INDEX" INTEGER, - "VARIATION" BOOLEAN -); - CREATE TABLE "MEASURE_DATA" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "MEASURE_ID" BIGINT, @@ -387,19 +366,6 @@ CREATE TABLE "USERS" ( "ACTIVE" BOOLEAN DEFAULT TRUE ); -CREATE TABLE "FILTERS" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "NAME" VARCHAR(100), - "KEE" VARCHAR(100), - "USER_ID" INTEGER, - "SHARED" BOOLEAN, - "FAVOURITES" BOOLEAN, - "RESOURCE_ID" INTEGER, - "DEFAULT_VIEW" VARCHAR(20), - "PAGE_SIZE" INTEGER, - "PERIOD_INDEX" INTEGER -); - CREATE TABLE "DASHBOARDS" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "USER_ID" INTEGER, 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 deleted file mode 100644 index 2be3f63b85a..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/filters/FilterDaoTest.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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.filters; - -import org.junit.Before; -import org.junit.Test; -import org.sonar.core.filter.CriterionDto; -import org.sonar.core.filter.FilterColumnDto; -import org.sonar.core.filter.FilterDao; -import org.sonar.core.filter.FilterDto; -import org.sonar.core.persistence.AbstractDaoTestCase; - -import static org.fest.assertions.Assertions.assertThat; - -public class FilterDaoTest extends AbstractDaoTestCase { - private FilterDao dao; - - @Before - public void createDao() { - dao = new FilterDao(getMyBatis()); - } - - @Test - public void should_find_filter() { - setupData("shouldFindFilter"); - - FilterDto filter = dao.findFilter("Projects"); - - assertThat(filter.getId()).isEqualTo(1L); - assertThat(filter.getName()).isEqualTo("Projects"); - assertThat(filter.getKey()).isEqualTo("Projects"); - assertThat(dao.findFilter("<UNKNOWN>")).isNull(); - } - - @Test - public void should_insert() { - setupData("shouldInsert"); - - FilterDto filterDto = new FilterDto(); - filterDto.setName("Projects"); - filterDto.setKey("Projects"); - filterDto.setShared(true); - filterDto.setFavourites(false); - filterDto.setDefaultView("list"); - filterDto.setPageSize(10L); - - CriterionDto criterionDto = new CriterionDto(); - criterionDto.setFamily("family"); - criterionDto.setKey("key"); - criterionDto.setOperator("="); - criterionDto.setValue(1.5f); - criterionDto.setTextValue("1.5"); - criterionDto.setVariation(true); - filterDto.add(criterionDto); - - FilterColumnDto filterColumnDto = new FilterColumnDto(); - filterColumnDto.setFamily("family"); - filterColumnDto.setKey("key"); - filterColumnDto.setSortDirection("ASC"); - filterColumnDto.setOrderIndex(2L); - filterColumnDto.setVariation(true); - filterDto.add(filterColumnDto); - - dao.insert(filterDto); - - checkTables("shouldInsert", "filters", "criteria", "filter_columns"); - } -}
\ No newline at end of file diff --git a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterContextTest.java b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterContextTest.java index 4e1174a8170..cf27685044f 100644 --- a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterContextTest.java +++ b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterContextTest.java @@ -19,8 +19,11 @@ */ package org.sonar.core.measure; +import com.google.common.collect.ImmutableMap; import org.junit.Test; +import java.util.Map; + import static org.fest.assertions.Assertions.assertThat; public class MeasureFilterContextTest { @@ -33,9 +36,9 @@ public class MeasureFilterContextTest { @Test public void test_toString() { MeasureFilterContext context = new MeasureFilterContext(); - context.setJson("{}"); + context.setData("{qualifiers=TRK}"); context.setSql("SELECT *"); context.setUserId(50L); - assertThat(context.toString()).isEqualTo("MeasureFilterContext[filter={},sql=SELECT *,user=50]"); + assertThat(context.toString()).isEqualTo("MeasureFilterContext[filter={qualifiers=TRK},sql=SELECT *,user=50]"); } } diff --git a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterDaoTest.java b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterDaoTest.java new file mode 100644 index 00000000000..a4d8fb7ce26 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterDaoTest.java @@ -0,0 +1,69 @@ +/* + * 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.measure; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.core.persistence.AbstractDaoTestCase; + +import static org.fest.assertions.Assertions.assertThat; + +public class MeasureFilterDaoTest extends AbstractDaoTestCase { + private MeasureFilterDao dao; + + @Before + public void createDao() { + dao = new MeasureFilterDao(getMyBatis()); + } + + @Test + public void should_find_filter() { + setupData("shared"); + + MeasureFilterDto filter = dao.findSystemFilterByName("Projects"); + + assertThat(filter.getId()).isEqualTo(1L); + assertThat(filter.getName()).isEqualTo("Projects"); + } + + + @Test + public void should_not_find_filter() { + setupData("shared"); + + assertThat(dao.findSystemFilterByName("Unknown")).isNull(); + } + + @Test + public void should_insert() { + setupData("shared"); + + MeasureFilterDto filterDto = new MeasureFilterDto(); + filterDto.setName("Project Treemap"); + filterDto.setUserId(123L); + filterDto.setShared(true); + filterDto.setDescription("Treemap of projects"); + filterDto.setData("qualifiers=TRK|display=treemap"); + + dao.insert(filterDto); + + checkTables("shouldInsert", new String[]{"created_at", "updated_at"}, "measure_filters"); + } +}
\ No newline at end of file diff --git a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterDecoderTest.java b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterDecoderTest.java deleted file mode 100644 index a4294dde995..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterDecoderTest.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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.measure; - -import org.json.simple.parser.ParseException; -import org.junit.Before; -import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.sonar.api.measures.Metric; -import org.sonar.api.measures.MetricFinder; - -import java.util.Date; - -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class MeasureFilterDecoderTest { - - private MetricFinder metricFinder; - - @Before - public void before() { - metricFinder = mock(MetricFinder.class); - when(metricFinder.findByKey(anyString())).thenAnswer(new Answer<Metric>() { - public Metric answer(InvocationOnMock invocationOnMock) throws Throwable { - return new Metric((String) invocationOnMock.getArguments()[0]); - } - }); - } - - @Test - public void should_decode() throws ParseException { - String json = "{\"base\": \"org.struts\", \"onBaseChildren\": true, \"scopes\": [\"PRJ\"], " + - "\"qualifiers\": [\"TRK\",\"CLA\"], \"keyRegexp\": \"*foo*\"" + - "\"languages\": [\"java\", \"php\"], \"name\": \"Struts\", \"fromDate\": \"2012-12-25\", " + - "\"toDate\": \"2013-01-31\", " + - "\"favourites\": true, " + - "\"sortAsc\": true, \"sortField\": \"METRIC\", \"sortMetric\": \"ncloc\", \"sortPeriod\":5, " + - "\"conditions\":[{\"metric\":\"lines\", \"op\":\">\", \"val\":123.0}]}"; - - MeasureFilter filter = new MeasureFilterDecoder(metricFinder).decode(json); - - assertThat(filter.getBaseResourceKey()).isEqualTo("org.struts"); - assertThat(filter.isOnBaseResourceChildren()).isTrue(); - assertThat(filter.getResourceScopes()).containsOnly("PRJ"); - assertThat(filter.getResourceQualifiers()).containsOnly("TRK", "CLA"); - assertThat(filter.getResourceLanguages()).containsOnly("java", "php"); - assertThat(filter.getResourceName()).isEqualTo("Struts"); - assertThat(filter.getResourceKeyRegexp()).isEqualTo("*foo*"); - assertThat(filter.getFromDate().getYear()).isEqualTo(2012 - 1900); - assertThat(filter.getToDate().getYear()).isEqualTo(2013 - 1900); - assertThat(filter.isOnFavourites()).isTrue(); - assertThat(filter.sort().metric().getKey()).isEqualTo("ncloc"); - assertThat(filter.sort().isAsc()).isTrue(); - MeasureFilterCondition condition = filter.getMeasureConditions().get(0); - assertThat(condition.metric().getKey()).isEqualTo("lines"); - assertThat(condition.operator()).isEqualTo(MeasureFilterCondition.Operator.GREATER); - assertThat(condition.value()).isEqualTo(123.0); - } - - @Test - public void should_set_max_date_by_number_of_days() throws ParseException { - String json = "{\"beforeDays\": 5}"; - - MeasureFilter filter = new MeasureFilterDecoder(metricFinder).decode(json); - - assertThat(filter.getFromDate()).isNull(); - assertThat(filter.getToDate().before(new Date())).isTrue(); - } - - @Test - public void should_set_min_date_by_number_of_days() throws ParseException { - String json = "{\"afterDays\": 5}"; - - MeasureFilter filter = new MeasureFilterDecoder(metricFinder).decode(json); - - assertThat(filter.getToDate()).isNull(); - assertThat(filter.getFromDate().before(new Date())).isTrue(); - } - - @Test - public void test_default_values() throws ParseException { - MeasureFilter filter = new MeasureFilterDecoder(metricFinder).decode("{}"); - - assertThat(filter.getBaseResourceKey()).isNull(); - assertThat(filter.isOnBaseResourceChildren()).isFalse(); - assertThat(filter.getResourceScopes()).isEmpty(); - assertThat(filter.getResourceQualifiers()).isEmpty(); - assertThat(filter.getResourceLanguages()).isEmpty(); - assertThat(filter.getResourceName()).isNull(); - assertThat(filter.getResourceKeyRegexp()).isNull(); - assertThat(filter.getFromDate()).isNull(); - assertThat(filter.getToDate()).isNull(); - assertThat(filter.isOnFavourites()).isFalse(); - assertThat(filter.sort().metric()).isNull(); - assertThat(filter.sort().getPeriod()).isNull(); - assertThat(filter.sort().onMeasures()).isFalse(); - assertThat(filter.sort().field()).isEqualTo(MeasureFilterSort.Field.NAME); - assertThat(filter.sort().isAsc()).isTrue(); - assertThat(filter.getMeasureConditions()).isEmpty(); - - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterEngineTest.java b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterEngineTest.java index 01a67868dce..06051a65d5d 100644 --- a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterEngineTest.java +++ b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterEngineTest.java @@ -19,6 +19,8 @@ */ package org.sonar.core.measure; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.json.simple.parser.ParseException; @@ -27,6 +29,9 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.slf4j.Logger; +import java.util.HashMap; +import java.util.Map; + import static org.mockito.Matchers.argThat; import static org.mockito.Mockito.*; @@ -36,22 +41,23 @@ public class MeasureFilterEngineTest { public ExpectedException thrown = ExpectedException.none(); @Test - public void should_decode_json_and_execute_filter() throws Exception { - MeasureFilterDecoder decoder = mock(MeasureFilterDecoder.class); + public void should_create_and_execute_filter() throws Exception { + Map<String,Object> filterMap = ImmutableMap.of("qualifiers", (Object) "TRK"); + MeasureFilterFactory factory = mock(MeasureFilterFactory.class); MeasureFilter filter = new MeasureFilter(); - when(decoder.decode("{}")).thenReturn(filter); + when(factory.create(filterMap)).thenReturn(filter); MeasureFilterExecutor executor = mock(MeasureFilterExecutor.class); Logger logger = mock(Logger.class); when(logger.isDebugEnabled()).thenReturn(true); - MeasureFilterEngine engine = new MeasureFilterEngine(decoder, null, executor); + MeasureFilterEngine engine = new MeasureFilterEngine(factory, executor); final long userId = 50L; - engine.execute("{}", userId, logger); + engine.execute(filterMap, userId, logger); verify(executor).execute(refEq(filter), argThat(new BaseMatcher<MeasureFilterContext>() { public boolean matches(Object o) { MeasureFilterContext context = (MeasureFilterContext) o; - return "{}".equals(context.getJson()) && context.getUserId() == userId; + return "{qualifiers=TRK}".equals(context.getData()) && context.getUserId() == userId; } public void describeTo(Description description) { @@ -63,13 +69,14 @@ public class MeasureFilterEngineTest { @Test public void throw_definition_of_filter_on_error() throws Exception { thrown.expect(IllegalStateException.class); - thrown.expectMessage("filter=<xml>"); + thrown.expectMessage("filter={qualifiers=TRK}"); - MeasureFilterDecoder decoder = mock(MeasureFilterDecoder.class); - when(decoder.decode("<xml>")).thenThrow(new ParseException(0)); + Map<String,Object> filterMap = ImmutableMap.of("qualifiers", (Object)"TRK"); + MeasureFilterFactory factory = mock(MeasureFilterFactory.class); + when(factory.create(filterMap)).thenThrow(new IllegalArgumentException()); MeasureFilterExecutor executor = mock(MeasureFilterExecutor.class); - MeasureFilterEngine engine = new MeasureFilterEngine(decoder, null, executor); - engine.execute("<xml>", 50L); + MeasureFilterEngine engine = new MeasureFilterEngine(factory, executor); + engine.execute(filterMap, 50L); } } diff --git a/sonar-core/src/test/resources/org/sonar/core/filters/FilterDaoTest/shouldFindFilter.xml b/sonar-core/src/test/resources/org/sonar/core/filters/FilterDaoTest/shouldFindFilter.xml deleted file mode 100644 index ef7b28c4804..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/filters/FilterDaoTest/shouldFindFilter.xml +++ /dev/null @@ -1,29 +0,0 @@ -<dataset> - - <filters - id="1" - name="Projects" - kee="Projects" - user_id="[null]" - shared="[true]" - favourites="[false]" - resource_id="[null]" - default_view="list" - page_size="[null]" - period_index="[null]" - /> - - <filters - id="2" - name="Treemap" - kee="Treemap" - user_id="[null]" - shared="[true]" - favourites="[false]" - resource_id="[null]" - default_view="treemap" - page_size="[null]" - period_index="[null]" - /> - -</dataset> 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 deleted file mode 100644 index 80849e355bb..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/filters/FilterDaoTest/shouldInsert-result.xml +++ /dev/null @@ -1,37 +0,0 @@ -<dataset> - - <filters - id="1" - name="Projects" - kee="Projects" - user_id="[null]" - shared="[true]" - favourites="[false]" - resource_id="[null]" - default_view="list" - page_size="10" - period_index="[null]" - /> - - <criteria - id="1" - filter_id="1" - family="family" - kee="key" - operator="=" - value="1.5" - text_value="1.5" - variation="[true]" - /> - - <filter_columns - id="1" - filter_id="1" - family="family" - kee="key" - sort_direction="ASC" - order_index="2" - variation="[true]" - /> - -</dataset> diff --git a/sonar-core/src/test/resources/org/sonar/core/filters/FilterDaoTest/shouldInsert.xml b/sonar-core/src/test/resources/org/sonar/core/filters/FilterDaoTest/shouldInsert.xml deleted file mode 100644 index 871dedcb5e9..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/filters/FilterDaoTest/shouldInsert.xml +++ /dev/null @@ -1,3 +0,0 @@ -<dataset> - -</dataset> diff --git a/sonar-core/src/test/resources/org/sonar/core/measure/MeasureFilterDaoTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/measure/MeasureFilterDaoTest/shared.xml new file mode 100644 index 00000000000..94d22922788 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/measure/MeasureFilterDaoTest/shared.xml @@ -0,0 +1,23 @@ +<dataset> + + <measure_filters + id="1" + name="Projects" + user_id="[null]" + shared="[true]" + description="All projects" + data="qualifiers=TRK" + created_at="2012-12-25" + updated_at="2012-12-25" /> + + <measure_filters + id="2" + name="Files" + user_id="[null]" + shared="[true]" + description="All files" + data="qualifiers=FIL" + created_at="2012-01-25" + updated_at="2012-01-25" /> + +</dataset> diff --git a/sonar-core/src/test/resources/org/sonar/core/measure/MeasureFilterDaoTest/shouldInsert-result.xml b/sonar-core/src/test/resources/org/sonar/core/measure/MeasureFilterDaoTest/shouldInsert-result.xml new file mode 100644 index 00000000000..1100b166b82 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/measure/MeasureFilterDaoTest/shouldInsert-result.xml @@ -0,0 +1,34 @@ +<dataset> + + <measure_filters + id="1" + name="Projects" + user_id="[null]" + shared="[true]" + description="All projects" + data="qualifiers=TRK" + created_at="2012-12-25" + updated_at="2012-12-25"/> + + <measure_filters + id="2" + name="Files" + user_id="[null]" + shared="[true]" + description="All files" + data="qualifiers=FIL" + created_at="2012-01-25" + updated_at="2012-01-25"/> + + + <measure_filters + id="3" + name="Project Treemap" + user_id="123" + shared="[true]" + description="Treemap of projects" + data="qualifiers=TRK|display=treemap" + created_at="2012-12-25" + updated_at="2012-12-25"/> + +</dataset> |