1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.query.internal

File UniqueDocumentFilterTest.java

 

Code metrics

0
21
7
1
115
76
7
0.33
3
7
1

Classes

Class Line # Actions
UniqueDocumentFilterTest 40 21 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.query.internal;
21   
22    import static org.hamcrest.CoreMatchers.*;
23    import static org.junit.Assert.*;
24   
25    import java.util.Arrays;
26    import java.util.List;
27   
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.xwiki.query.Query;
32    import org.xwiki.query.QueryFilter;
33    import org.xwiki.test.mockito.MockitoComponentMockingRule;
34   
35    /**
36    * Tests for {@link org.xwiki.query.internal.UniqueDocumentFilter}
37    *
38    * @version $Id: 606f6b9e19377bf73c58e2554190b9b68f223359 $
39    */
 
40    public class UniqueDocumentFilterTest
41    {
42    @Rule
43    public MockitoComponentMockingRule<UniqueDocumentFilter> mocker =
44    new MockitoComponentMockingRule<UniqueDocumentFilter>(UniqueDocumentFilter.class);
45   
46    private QueryFilter filter;
47   
 
48  6 toggle @Before
49    public void setUp() throws Exception
50    {
51  6 this.filter = this.mocker.getComponentUnderTest();
52    }
53   
 
54  1 toggle @Test
55    public void filterSelectStatement() throws Exception
56    {
57  1 assertEquals("select distinct doc.fullName from XWikiDocument doc",
58    filter.filterStatement("select doc.fullName from XWikiDocument doc", Query.HQL));
59   
60  1 List<String> items = Arrays.asList("doc1", "doc2");
61  1 assertThat((List<String>) filter.filterResults(items), is(items));
62    }
63   
 
64  1 toggle @Test
65    public void filterSelectWithAsStatement() throws Exception
66    {
67  1 assertEquals("select distinct doc.fullName from XWikiDocument as doc",
68    filter.filterStatement("select doc.fullName from XWikiDocument as doc", Query.HQL));
69    }
70   
 
71  1 toggle @Test
72    public void filterSelectStatementWithMismatchingDocAlias() throws Exception
73    {
74  1 assertEquals("select mydoc.fullName from XWikiDocument mydoc",
75    filter.filterStatement("select mydoc.fullName from XWikiDocument mydoc", Query.HQL));
76    }
77   
 
78  1 toggle @Test
79    public void filterStatementWhenStatementAlreadyContainsDistinct() throws Exception
80    {
81  1 assertEquals("select distinct doc.fullName from XWikiDocument doc",
82    filter.filterStatement("select distinct doc.fullName from XWikiDocument doc", Query.HQL));
83    }
84   
 
85  1 toggle @Test
86    public void filterStatementWhenStatementContainsAnotherOrderBy() throws Exception
87    {
88  1 assertEquals("select distinct doc.fullName, doc.name from XWikiDocument doc order by doc.name asc",
89    filter.filterStatement("select doc.fullName from XWikiDocument doc order by doc.name asc", Query.HQL));
90   
91  1 List<Object[]> results = filter.filterResults(
92    Arrays.asList(new Object[] {"full1", "name1"}, new Object[] {"full2", "name2"}));
93  1 assertEquals(2, results.size());
94  1 assertEquals("full1", results.get(0));
95  1 assertEquals("full2", results.get(1));
96    }
97   
 
98  1 toggle @Test
99    public void filterStatementWhenTheFirstSelectColumnIsNotFullName() throws Exception
100    {
101  1 assertEquals("select distinct doc.fullName, doc.name from XWikiDocument doc order by doc.name asc",
102    filter.filterStatement("select doc.name, doc.fullName from XWikiDocument doc order by doc.name asc",
103    Query.HQL));
104   
105  1 List<Object[]> results = filter.filterResults(
106    Arrays.asList(new Object[] {"full1", "name1"}, new Object[] {"full2", "name2"}));
107  1 assertEquals(2, results.size());
108  1 assertEquals(2, results.get(0).length);
109  1 assertEquals("full1", results.get(0)[0]);
110  1 assertEquals("name1", results.get(0)[1]);
111  1 assertEquals(2, results.get(1).length);
112  1 assertEquals("full2", results.get(1)[0]);
113  1 assertEquals("name2", results.get(1)[1]);
114    }
115    }