diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2013-06-13 16:19:11 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2013-06-13 16:19:11 +0200 |
commit | a06137eb1111b48e20ccc2f0ac5989e35bd83c55 (patch) | |
tree | e676920c3b554508a58ca8558d9941a561cb0515 /sonar-core | |
parent | 9678ebc554ee2c2581268c956040e3ebc7adcb12 (diff) | |
download | sonarqube-a06137eb1111b48e20ccc2f0ac5989e35bd83c55.tar.gz sonarqube-a06137eb1111b48e20ccc2f0ac5989e35bd83c55.zip |
SONAR-4383 Save issue filter
Diffstat (limited to 'sonar-core')
3 files changed, 141 insertions, 33 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueFilter.java b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueFilter.java index e5047917679..020f89b1ae2 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueFilter.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueFilter.java @@ -37,14 +37,14 @@ import static com.google.common.collect.Maps.newHashMap; public class DefaultIssueFilter { - public static String SEPARATOR = "|"; - public static String KEY_VALUE_SEPARATOR = "="; - public static String LIST_SEPARATOR = ","; + public final static String SEPARATOR = "|"; + public final static String KEY_VALUE_SEPARATOR = "="; + public final static String LIST_SEPARATOR = ","; private Long id; private String name; private String user; - private Boolean shared; + private Boolean shared = false; private String description; private String data; private Date createdAt; @@ -54,6 +54,18 @@ public class DefaultIssueFilter { } + public static DefaultIssueFilter create(String name) { + DefaultIssueFilter issueFilter = new DefaultIssueFilter(); + Date now = new Date(); + issueFilter.setName(name); + issueFilter.setCreatedAt(now).setUpdatedAt(now); + return issueFilter; + } + + public DefaultIssueFilter(Map<String, Object> mapData) { + this.data = mapAsdata(mapData); + } + public Long id() { return id; } @@ -127,6 +139,9 @@ public class DefaultIssueFilter { return this; } + /** + * Used by ui + */ public Map<String, Object> dataAsMap(){ return dataAsMap(data); } @@ -138,16 +153,15 @@ public class DefaultIssueFilter { Iterable<String> keyValues = Splitter.on(DefaultIssueFilter.SEPARATOR).split(data); for (String keyValue : keyValues) { String[] keyValueSplit = StringUtils.split(keyValue, DefaultIssueFilter.KEY_VALUE_SEPARATOR); - if (keyValueSplit.length != 2) { - throw new IllegalArgumentException("Key value should be separate by a '"+ DefaultIssueFilter.KEY_VALUE_SEPARATOR + "'"); - } - String key = keyValueSplit[0]; - String value = keyValueSplit[1]; - String[] listValues = StringUtils.split(value, DefaultIssueFilter.LIST_SEPARATOR); - if (listValues.length > 1) { - map.put(key, newArrayList(listValues)); - } else { - map.put(key, value); + if (keyValueSplit.length == 2) { + String key = keyValueSplit[0]; + String value = keyValueSplit[1]; + String[] listValues = StringUtils.split(value, DefaultIssueFilter.LIST_SEPARATOR); + if (listValues.length > 1) { + map.put(key, newArrayList(listValues)); + } else { + map.put(key, value); + } } } return map; @@ -160,27 +174,32 @@ public class DefaultIssueFilter { for (Map.Entry<String, Object> entries : map.entrySet()){ String key = entries.getKey(); Object value = entries.getValue(); - - stringBuilder.append(key); - stringBuilder.append(DefaultIssueFilter.KEY_VALUE_SEPARATOR); - - List valuesList = newArrayList(); - if (value instanceof List) { - // assume that it contains only strings - valuesList = (List) value; - } else if (value instanceof CharSequence) { - valuesList = Lists.newArrayList(Splitter.on(',').omitEmptyStrings().split((CharSequence) value)); - } else { - stringBuilder.append(value); - } - for (Iterator<Object> valueListIter = valuesList.iterator(); valueListIter.hasNext();) { - Object valueList = valueListIter.next(); - stringBuilder.append(valueList); - if (valueListIter.hasNext()) { - stringBuilder.append(DefaultIssueFilter.LIST_SEPARATOR); + if (value != null) { + List valuesList = newArrayList(); + if (value instanceof List) { + // assume that it contains only strings + valuesList = (List) value; + } else if (value instanceof CharSequence) { + valuesList = Lists.newArrayList(Splitter.on(',').omitEmptyStrings().split((CharSequence) value)); + } else { + stringBuilder.append(key); + stringBuilder.append(DefaultIssueFilter.KEY_VALUE_SEPARATOR); + stringBuilder.append(value); + stringBuilder.append(DefaultIssueFilter.SEPARATOR); + } + if (!valuesList.isEmpty()) { + stringBuilder.append(key); + stringBuilder.append(DefaultIssueFilter.KEY_VALUE_SEPARATOR); + for (Iterator<Object> valueListIter = valuesList.iterator(); valueListIter.hasNext();) { + Object valueList = valueListIter.next(); + stringBuilder.append(valueList); + if (valueListIter.hasNext()) { + stringBuilder.append(DefaultIssueFilter.LIST_SEPARATOR); + } + } + stringBuilder.append(DefaultIssueFilter.SEPARATOR); } } - stringBuilder.append(DefaultIssueFilter.SEPARATOR); } if (stringBuilder.length() > 0) { diff --git a/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueFilterTest.java b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueFilterTest.java new file mode 100644 index 00000000000..4bf9d656f50 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueFilterTest.java @@ -0,0 +1,87 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.core.issue; + +import org.junit.Test; + +import java.util.List; +import java.util.Map; + +import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Maps.newLinkedHashMap; +import static org.fest.assertions.Assertions.assertThat; + +public class DefaultIssueFilterTest { + + DefaultIssueFilter issueFilter = new DefaultIssueFilter(); + + @Test + public void should_convert_data_to_map() { + String data = "issues=ABCDE1234|severities=MAJOR,MINOR|resolved=true|pageSize=10|pageIndex=50"; + + Map<String, Object> map = issueFilter.dataAsMap(data); + + assertThat(map).hasSize(5); + assertThat(map.get("issues")).isEqualTo("ABCDE1234"); + assertThat(map.get("severities")).isInstanceOf(List.class); + assertThat((List<String>) map.get("severities")).contains("MAJOR", "MINOR"); + assertThat(map.get("resolved")).isEqualTo("true"); + assertThat(map.get("pageSize")).isEqualTo("10"); + assertThat(map.get("pageIndex")).isEqualTo("50"); + } + + @Test + public void should_remove_empty_value_when_converting_data_to_map() { + String data = "issues=ABCDE1234|severities="; + + Map<String, Object> map = issueFilter.dataAsMap(data); + + assertThat(map).hasSize(1); + assertThat(map.get("issues")).isEqualTo("ABCDE1234"); + } + + @Test + public void should_convert_map_to_data() { + Map<String, Object> map = newLinkedHashMap(); + map.put("issues", newArrayList("ABCDE1234")); + map.put("severities", newArrayList("MAJOR", "MINOR")); + map.put("resolved", true); + map.put("pageSize", 10l); + map.put("pageIndex", 50); + + String result = issueFilter.mapAsdata(map); + + assertThat(result).isEqualTo("issues=ABCDE1234|severities=MAJOR,MINOR|resolved=true|pageSize=10|pageIndex=50"); + } + + @Test + public void should_remove_empty_value_when_converting_convert_map_to_data() { + Map<String, Object> map = newLinkedHashMap(); + map.put("issues", newArrayList("ABCDE1234")); + map.put("resolved", null); + map.put("pageSize", ""); + + String result = issueFilter.mapAsdata(map); + + assertThat(result).isEqualTo("issues=ABCDE1234"); + } + +} diff --git a/sonar-core/src/test/java/org/sonar/core/issue/db/IssueFilterDaoTest.java b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueFilterDaoTest.java index b8619fae88b..de841318b98 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/db/IssueFilterDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueFilterDaoTest.java @@ -72,6 +72,8 @@ public class IssueFilterDaoTest extends AbstractDaoTestCase { dao.insert(filterDto); + assertThat(filterDto.getId()).isNotNull(); + checkTables("should_insert", new String[]{"created_at", "updated_at"}, "issue_filters"); } |