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

File HiddenDocumentFilterTest.java

 

Code metrics

0
17
12
1
153
101
12
0.71
1.42
12
1

Classes

Class Line # Actions
HiddenDocumentFilterTest 41 17 0% 12 0
1.0100%
 

Contributing tests

This file is covered by 10 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 org.jmock.Expectations;
23    import org.junit.Before;
24    import org.junit.Test;
25    import org.slf4j.Logger;
26    import org.xwiki.component.util.ReflectionUtils;
27    import org.xwiki.configuration.ConfigurationSource;
28    import org.xwiki.query.Query;
29    import org.xwiki.query.QueryFilter;
30    import org.xwiki.test.jmock.AbstractMockingComponentTestCase;
31    import org.xwiki.test.jmock.annotation.MockingRequirement;
32   
33    import static org.junit.Assert.assertEquals;
34   
35    /**
36    * Tests for {@link HiddenDocumentFilter}
37    *
38    * @version $Id: 1d6944e11ba4ddea1d7a89f4e9c3776914266ff0 $
39    */
40    @MockingRequirement(HiddenDocumentFilter.class)
 
41    public class HiddenDocumentFilterTest extends AbstractMockingComponentTestCase
42    {
43    private QueryFilter filter;
44   
45    private ConfigurationSource userConfiguration;
46   
 
47  10 toggle @Before
48    public void configure() throws Exception
49    {
50  10 this.userConfiguration = getComponentManager().getInstance(ConfigurationSource.class, "user");
51  10 getMockery().checking(new Expectations()
 
52  10 toggle {{
53  10 ignoring(any(Logger.class)).method("debug");
54    // We need to set this expectation here because this call is made during the filter component initialization
55  10 oneOf(userConfiguration).getProperty("displayHiddenDocuments", Integer.class);
56  10 will(returnValue(0));
57    }});
58   
59  10 this.filter = getComponentManager().getInstance(QueryFilter.class, "hidden");
60    }
61   
 
62  1 toggle @Test
63    public void filterHQLStatementWithDoNotDisplayHiddenDocumentsInTheUserPreferences() throws Exception
64    {
65  1 assertEquals(
66    "select doc.fullName from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null) and (1=1)",
67    filter.filterStatement("select doc.fullName from XWikiDocument doc where 1=1", Query.HQL));
68    }
69   
 
70  1 toggle @Test
71    public void filterHQLStatementWithDisplayHiddenDocumentsInTheUserPreferences() throws Exception
72    {
73    // We need to do it that way since the expectation must be set in #configure() and the expectation sets the
74    // isActive property to true
75  1 ReflectionUtils.setFieldValue(this.filter, "isActive", false);
76   
77    // Insertions of distinct
78  1 assertEquals("select doc.fullName from XWikiDocument doc where 1=1",
79    filter.filterStatement("select doc.fullName from XWikiDocument doc where 1=1", Query.HQL));
80    }
81   
 
82  1 toggle @Test
83    public void filterIncorrectHQLStatement() throws Exception
84    {
85    // Insertions of distinct
86  1 assertEquals("select doc.fullName from XWikiDocument mydoc where 1=1",
87    filter.filterStatement("select doc.fullName from XWikiDocument mydoc where 1=1", Query.HQL));
88    }
89   
 
90  1 toggle @Test
91    public void filterXWQLStatement() throws Exception
92    {
93  1 assertEquals("select doc.fullName from XWikiDocument doc where 1=1",
94    filter.filterStatement("select doc.fullName from XWikiDocument doc where 1=1", Query.XWQL));
95    }
96   
 
97  1 toggle @Test
98    public void filterHQLStatementWithWhereAndOrderBy()
99    {
100    // Insertions of distinct
101  1 assertEquals("select doc.name from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null) and "
102    + "(1=1) order by doc.name",
103    filter.filterStatement("select doc.name from XWikiDocument doc where 1=1 order by doc.name",
104    Query.HQL));
105    }
106   
 
107  1 toggle @Test
108    public void filterHQLStatementWithWhereAndGroupBy()
109    {
110    // Insertions of distinct
111  1 assertEquals("select doc.name from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null) and "
112    + "(1=1) group by doc.name",
113    filter.filterStatement("select doc.name from XWikiDocument doc where 1=1 group by doc.name",
114    Query.HQL));
115    }
116   
 
117  1 toggle @Test
118    public void filterHQLStatementWithWhereAndOrderByAndGroupBy()
119    {
120    // Insertions of distinct
121  1 assertEquals("select doc.name from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null) and "
122    + "(1=1) order by doc.name group by doc.name",
123    filter.filterStatement("select doc.name from XWikiDocument doc where 1=1 order by doc.name group by "
124    + "doc.name", Query.HQL));
125    }
126   
 
127  1 toggle @Test
128    public void filterHQLStatementWithoutWhere()
129    {
130    // Insertions of distinct
131  1 assertEquals("select doc.name from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null)",
132    filter.filterStatement("select doc.name from XWikiDocument doc", Query.HQL));
133    }
134   
 
135  1 toggle @Test
136    public void filterHQLStatementWithoutWhereWithOrderBy()
137    {
138    // Insertions of distinct
139  1 assertEquals("select doc.name from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null) order by "
140    + "doc.name asc",
141    filter.filterStatement("select doc.name from XWikiDocument doc order by doc.name asc", Query.HQL));
142    }
143   
 
144  1 toggle @Test
145    public void filterHQLStatementWithoutWhereWithGroupBy()
146    {
147    // Insertions of distinct
148  1 assertEquals(
149    "select doc.web, doc.name from XWikiDocument doc where (doc.hidden <> true or doc.hidden is null) " +
150    "group by doc.web",
151    filter.filterStatement("select doc.web, doc.name from XWikiDocument doc group by doc.web", Query.HQL));
152    }
153    }