diff options
Diffstat (limited to 'sonar-server/src/test/java/org')
4 files changed, 308 insertions, 5 deletions
diff --git a/sonar-server/src/test/java/org/sonar/server/component/ComponentQueryTest.java b/sonar-server/src/test/java/org/sonar/server/component/ComponentQueryTest.java new file mode 100644 index 00000000000..f3dfdd8164a --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/component/ComponentQueryTest.java @@ -0,0 +1,81 @@ +/* + * 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.server.component; + +import org.junit.Test; + +import static com.google.common.collect.Lists.newArrayList; +import static org.fest.assertions.Assertions.assertThat; + +public class ComponentQueryTest { + + @Test + public void should_build_query() throws Exception { + ComponentQuery query = ComponentQuery.builder() + .keys(newArrayList("org.codehaus")) + .names(newArrayList("Sona")) + .qualifiers(newArrayList("TRK")) + .pageSize(10) + .pageIndex(2) + .sort(ComponentQuery.SORT_BY_NAME) + .asc(true) + .build(); + assertThat(query.keys()).containsOnly("org.codehaus"); + assertThat(query.names()).containsOnly("Sona"); + assertThat(query.qualifiers()).containsOnly("TRK"); + assertThat(query.sort()).isEqualTo(ComponentQuery.SORT_BY_NAME); + assertThat(query.asc()).isTrue(); + assertThat(query.pageSize()).isEqualTo(10); + assertThat(query.pageIndex()).isEqualTo(2); + } + + @Test + public void should_accept_null_sort() throws Exception { + ComponentQuery query = ComponentQuery.builder().sort(null).build(); + assertThat(query.sort()).isNull(); + } + + @Test + public void should_sort_by_name_asc_by_default() throws Exception { + ComponentQuery query = ComponentQuery.builder().build(); + assertThat(query.sort()).isEqualTo(ComponentQuery.SORT_BY_NAME); + assertThat(query.asc()).isTrue(); + } + + @Test + public void should_throw_exception_if_sort_is_not_valid() throws Exception { + try { + ComponentQuery.builder() + .sort("UNKNOWN") + .build(); + } catch (Exception e) { + assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("Bad sort field: UNKNOWN"); + } + } + + @Test + public void test_default_page_index_and_size() throws Exception { + ComponentQuery query = ComponentQuery.builder().build(); + assertThat(query.pageSize()).isEqualTo(ComponentQuery.DEFAULT_PAGE_SIZE); + assertThat(query.pageIndex()).isEqualTo(ComponentQuery.DEFAULT_PAGE_INDEX); + } + +} diff --git a/sonar-server/src/test/java/org/sonar/server/component/ComponentsFinderSortTest.java b/sonar-server/src/test/java/org/sonar/server/component/ComponentsFinderSortTest.java new file mode 100644 index 00000000000..688009ccbd2 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/component/ComponentsFinderSortTest.java @@ -0,0 +1,55 @@ +/* + * 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.server.component; + +import org.junit.Test; +import org.sonar.api.component.Component; +import org.sonar.core.component.ComponentDto; + +import java.util.List; + +import static com.google.common.collect.Lists.newArrayList; +import static org.fest.assertions.Assertions.assertThat; + +public class ComponentsFinderSortTest { + + @Test + public void should_sort_by_name() { + List<? extends Component> dtoList = newArrayList( + new ComponentDto().setKey("org.codehaus.sonar").setName("Sonar"), + new ComponentDto().setKey("org.apache.tika:tika").setName("Apache Tika"), + new ComponentDto().setKey("org.picocontainer:picocontainer-parent").setName("PicoContainer Parent"), + new ComponentDto().setKey("org.codehaus.sample") + ); + + ComponentQuery query = ComponentQuery.builder().sort(ComponentQuery.SORT_BY_NAME).asc(true).build(); + ComponentsFinderSort issuesFinderSort = new ComponentsFinderSort(dtoList, query); + + List<Component> result = newArrayList(issuesFinderSort.sort()); + + assertThat(result).hasSize(4); + assertThat(result.get(0).name()).isEqualTo("Apache Tika"); + assertThat(result.get(1).name()).isEqualTo("PicoContainer Parent"); + assertThat(result.get(2).name()).isEqualTo("Sonar"); + assertThat(result.get(3).name()).isNull(); + } + +} diff --git a/sonar-server/src/test/java/org/sonar/server/component/DefaultComponentFinderTest.java b/sonar-server/src/test/java/org/sonar/server/component/DefaultComponentFinderTest.java new file mode 100644 index 00000000000..bba0fb77fa9 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/component/DefaultComponentFinderTest.java @@ -0,0 +1,124 @@ +/* + * 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.server.component; + +import org.junit.Test; +import org.sonar.api.component.Component; +import org.sonar.core.resource.ResourceDao; +import org.sonar.core.resource.ResourceDto; +import org.sonar.core.resource.ResourceQuery; + +import java.util.Iterator; +import java.util.List; + +import static com.google.common.collect.Lists.newArrayList; +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class DefaultComponentFinderTest { + + ResourceDao dao = mock(ResourceDao.class); + DefaultComponentFinder finder = new DefaultComponentFinder(dao); + + @Test + public void should_return_all_components_when_no_parameter() { + List<ResourceDto> dtoList = newArrayList( + new ResourceDto().setKey("org.codehaus.sonar").setName("Sonar").setQualifier("TRK"), + new ResourceDto().setKey("org.apache.tika:tika").setName("Apache Tika").setQualifier("TRK"), + new ResourceDto().setKey("org.picocontainer:picocontainer-parent").setName("PicoContainer Parent").setQualifier("TRK") + ); + when(dao.getResources(any(ResourceQuery.class))).thenReturn(dtoList); + + ComponentQuery query = ComponentQuery.builder().build(); + DefaultComponentQueryResult results = finder.find(query); + + assertThat(results.components()).hasSize(3); + Component component = results.components().iterator().next(); + assertThat(component.name()).isEqualTo("Apache Tika"); + assertThat(component.key()).isEqualTo("org.apache.tika:tika"); + assertThat(component.qualifier()).isEqualTo("TRK"); + } + + @Test + public void should_find_components_by_key_pattern() { + List<ResourceDto> dtoList = newArrayList( + new ResourceDto().setKey("org.codehaus.sonar").setName("Sonar").setQualifier("TRK"), + new ResourceDto().setKey("org.apache.tika:tika").setName("Apache Tika").setQualifier("TRK"), + new ResourceDto().setKey("org.apache.jackrabbit:jackrabbit").setName("Apache Jackrabbit").setQualifier("TRK") + ); + when(dao.getResources(any(ResourceQuery.class))).thenReturn(dtoList); + + ComponentQuery query = ComponentQuery.builder().keys(newArrayList("org.apache")).build(); + assertThat(finder.find(query).components()).hasSize(2); + } + + @Test + public void should_find_components_by_name_pattern() { + List<ResourceDto> dtoList = newArrayList( + new ResourceDto().setKey("org.codehaus.sonar").setName("Sonar").setQualifier("TRK"), + new ResourceDto().setKey("org.apache.tika:tika").setName("Apache Tika").setQualifier("TRK"), + new ResourceDto().setKey("org.apache.jackrabbit:jackrabbit").setName("Apache Jackrabbit").setQualifier("TRK") + ); + when(dao.getResources(any(ResourceQuery.class))).thenReturn(dtoList); + + ComponentQuery query = ComponentQuery.builder().names(newArrayList("Apache")).build(); + assertThat(finder.find(query).components()).hasSize(2); + } + + @Test + public void should_sort_result_by_name() { + List<ResourceDto> dtoList = newArrayList( + new ResourceDto().setKey("org.codehaus.sonar").setName("Sonar").setQualifier("TRK"), + new ResourceDto().setKey("org.apache.tika:tika").setName("Apache Tika").setQualifier("TRK"), + new ResourceDto().setKey("org.picocontainer:picocontainer-parent").setName("PicoContainer Parent").setQualifier("TRK") + ); + when(dao.getResources(any(ResourceQuery.class))).thenReturn(dtoList); + + ComponentQuery query = ComponentQuery.builder().build(); + DefaultComponentQueryResult results = finder.find(query); + + assertThat(results.components()).hasSize(3); + Iterator<? extends Component> iterator = results.components().iterator(); + assertThat(iterator.next().name()).isEqualTo("Apache Tika"); + assertThat(iterator.next().name()).isEqualTo("PicoContainer Parent"); + assertThat(iterator.next().name()).isEqualTo("Sonar"); + } + + @Test + public void should_find_paginate_result() { + ComponentQuery query = ComponentQuery.builder().pageSize(1).pageIndex(1).build(); + + List<ResourceDto> dtoList = newArrayList( + new ResourceDto().setKey("org.codehaus.sonar").setName("Sonar").setQualifier("TRK"), + new ResourceDto().setKey("org.apache.tika:tika").setName("Apache Tika").setQualifier("TRK"), + new ResourceDto().setKey("org.picocontainer:picocontainer-parent").setName("PicoContainer Parent").setQualifier("TRK") + ); + when(dao.getResources(any(ResourceQuery.class))).thenReturn(dtoList); + + DefaultComponentQueryResult results = finder.find(query); + assertThat(results.paging().offset()).isEqualTo(0); + assertThat(results.paging().pages()).isEqualTo(3); + assertThat(results.paging().total()).isEqualTo(3); + } + +} diff --git a/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java b/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java index d9911b33775..2a5cdb3051b 100644 --- a/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java @@ -21,29 +21,72 @@ package org.sonar.server.component; import org.junit.Before; +import org.junit.Test; import org.sonar.api.component.Component; import org.sonar.core.resource.ResourceDao; +import java.util.Map; + +import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Maps.newHashMap; import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; public class DefaultRubyComponentServiceTest { private ResourceDao resourceDao; + private DefaultComponentFinder finder; private DefaultRubyComponentService componentService; @Before - public void before(){ + public void before() { resourceDao = mock(ResourceDao.class); - componentService = new DefaultRubyComponentService(resourceDao); + finder = mock(DefaultComponentFinder.class); + componentService = new DefaultRubyComponentService(resourceDao, finder); } - @Before + @Test public void should_find_by_key() { Component component = mock(Component.class); when(resourceDao.findByKey("struts")).thenReturn(component); assertThat(componentService.findByKey("struts")).isEqualTo(component); } + + @Test + public void should_find() { + Map<String, Object> map = newHashMap(); + map.put("keys", newArrayList("org.codehaus.sonar")); + map.put("names", newArrayList("Sonar")); + map.put("qualifiers", newArrayList("TRK")); + map.put("pageSize", 10l); + map.put("pageIndex", 50); + map.put("sort", "NAME"); + map.put("asc", true); + + componentService.find(map); + verify(finder).find(any(ComponentQuery.class)); + } + + @Test + public void should_create_query_from_parameters() { + Map<String, Object> map = newHashMap(); + map.put("keys", newArrayList("org.codehaus.sonar")); + map.put("names", newArrayList("Sonar")); + map.put("qualifiers", newArrayList("TRK")); + map.put("pageSize", 10l); + map.put("pageIndex", 50); + map.put("sort", "NAME"); + map.put("asc", true); + + ComponentQuery query = DefaultRubyComponentService.toQuery(map); + assertThat(query.keys()).containsOnly("org.codehaus.sonar"); + assertThat(query.names()).containsOnly("Sonar"); + assertThat(query.qualifiers()).containsOnly("TRK"); + assertThat(query.pageSize()).isEqualTo(10); + assertThat(query.pageIndex()).isEqualTo(50); + assertThat(query.sort()).isEqualTo(ComponentQuery.SORT_BY_NAME); + assertThat(query.asc()).isTrue(); + } } |