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

File XWikiDismaxQParserPluginTest.java

 

Code metrics

0
50
5
1
157
93
5
0.1
10
5
1

Classes

Class Line # Actions
XWikiDismaxQParserPluginTest 40 50 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 5 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.search.solr.internal;
21   
22    import static org.junit.Assert.*;
23   
24    import java.util.Arrays;
25    import java.util.Collections;
26    import java.util.HashMap;
27    import java.util.HashSet;
28    import java.util.Map;
29   
30    import org.apache.solr.common.params.MapSolrParams;
31    import org.apache.solr.common.params.SolrParams;
32    import org.junit.Test;
33   
34    /**
35    * Unit tests for {@link XWikiDismaxQParserPlugin}.
36    *
37    * @version $Id: b4908d1d080cfa1912e5cffd5672bcd3534cccaa $
38    * @since 5.3RC1
39    */
 
40    public class XWikiDismaxQParserPluginTest
41    {
42    /**
43    * The object being tested.
44    */
45    private XWikiDismaxQParserPlugin plugin = new XWikiDismaxQParserPlugin();
46   
 
47  1 toggle @Test
48    public void extractFieldNames()
49    {
50  1 assertEquals(Collections.emptySet(), plugin.extractFieldNames(""));
51  1 assertEquals(Collections.emptySet(), plugin.extractFieldNames("text"));
52  1 assertEquals(Collections.emptySet(), plugin.extractFieldNames("+(^:text"));
53  1 assertEquals(Collections.emptySet(), plugin.extractFieldNames("foo :bar"));
54   
55  1 assertEquals(Collections.singleton("foo"), plugin.extractFieldNames("foo:bar"));
56  1 assertEquals(Collections.singleton("both"), plugin.extractFieldNames("both:(one two)"));
57  1 assertEquals(Collections.singleton("title__"), plugin.extractFieldNames("title__:text"));
58  1 assertEquals(Collections.singleton("title_zh_TW"), plugin.extractFieldNames("title_zh_TW:text"));
59  1 assertEquals(Collections.singleton("property.Blog.BlogPostClass.title"),
60    plugin.extractFieldNames("property.Blog.BlogPostClass.title:value"));
61  1 assertEquals(Collections.singleton("property.Blog.Blog..Post$5EClass.title"),
62    plugin.extractFieldNames("property.Blog.Blog..Post$5EClass.title:value"));
63   
64  1 assertEquals(
65    new HashSet<String>(Arrays.asList("abc", "g_h.i", "m$n-o",
66    "_\u0103\u00EE\u00E2\u0219\u021B\u00E8\u00E9\u00EA\u00EB")),
67    plugin.extractFieldNames("+abc:def AND -g_h.i:jkl AND (m$n-o:pqr OR "
68    + "_\u0103\u00EE\u00E2\u0219\u021B\u00E8\u00E9\u00EA\u00EB:stu^3)"));
69    }
70   
 
71  1 toggle @Test
72    public void withFieldAliases()
73    {
74  1 Map<String, String> parameters = new HashMap<String, String>();
75  1 parameters.put("qf", "title^0.4 comment^0.40 date^1.0");
76  1 parameters.put("xwiki.multilingualFields", "title, property.*, foo, comment");
77  1 parameters.put("xwiki.supportedLocales", "en, fr, zh_TW");
78  1 parameters.put("xwiki.typedDynamicFields", "property.*");
79  1 parameters.put("xwiki.dynamicFieldTypes", "boolean, int");
80   
81  1 String query = "title:text AND x:y AND property.Blog.BlogPostClass.summary:wiki AND title_ro:value";
82  1 SolrParams paramsWithAliases = plugin.withFieldAliases(query, new MapSolrParams(parameters));
83   
84  1 assertEquals("title__ title_en title_fr title_zh_TW", paramsWithAliases.get("f.title.qf"));
85  1 assertEquals("property.Blog.BlogPostClass.summary__ property.Blog.BlogPostClass.summary_en "
86    + "property.Blog.BlogPostClass.summary_fr property.Blog.BlogPostClass.summary_zh_TW "
87    + "property.Blog.BlogPostClass.summary_boolean property.Blog.BlogPostClass.summary_int",
88    paramsWithAliases.get("f.property.Blog.BlogPostClass.summary.qf"));
89   
90    // Event if this field doesn't appear in the query, it's a default field so it has to have the alias.
91  1 assertEquals("comment__ comment_en comment_fr comment_zh_TW", paramsWithAliases.get("f.comment.qf"));
92   
93    // These fields are not declared as multilingual.
94  1 assertNull(paramsWithAliases.get("f.x.qf"));
95  1 assertNull(paramsWithAliases.get("f.title_ro.qf"));
96   
97    // This is a default field but it's not declared as multilingual.
98  1 assertNull(paramsWithAliases.get("f.date.qf"));
99   
100    // This multilingual field doesn't appear in the query and it's not a default field either.
101  1 assertNull(paramsWithAliases.get("f.foo.qf"));
102    }
103   
 
104  1 toggle @Test
105    public void withFieldAliasesWhenNoSupportedLocales()
106    {
107  1 Map<String, String> parameters = new HashMap<String, String>();
108  1 parameters.put("qf", "comment^0.40");
109  1 parameters.put("xwiki.multilingualFields", "title, comment");
110   
111  1 SolrParams paramsWithAliases = plugin.withFieldAliases("title:text", new MapSolrParams(parameters));
112   
113    // Aliases for the ROOT locale.
114  1 assertEquals("title__", paramsWithAliases.get("f.title.qf"));
115  1 assertEquals("comment__", paramsWithAliases.get("f.comment.qf"));
116    }
117   
 
118  1 toggle @Test
119    public void withFieldAliasesWhenNoMultilingualFields()
120    {
121  1 Map<String, String> parameters = new HashMap<String, String>();
122  1 parameters.put("qf", "title^0.4 comment^0.40 date^1.0");
123  1 parameters.put("xwiki.supportedLocales", "en, ro");
124   
125  1 SolrParams paramsWithAliases = plugin.withFieldAliases("title:text", new MapSolrParams(parameters));
126   
127    // The existing parameters should have been preserved.
128  1 assertEquals(2, paramsWithAliases.toNamedList().size());
129  1 assertEquals("title^0.4 comment^0.40 date^1.0", paramsWithAliases.get("qf"));
130  1 assertEquals("en, ro", paramsWithAliases.get("xwiki.supportedLocales"));
131    }
132   
 
133  1 toggle @Test
134    public void withFieldAliasesWhenNoFieldsInQuery()
135    {
136  1 Map<String, String> parameters = new HashMap<String, String>();
137  1 parameters.put("qf", "title^0.4 comment^0.40");
138  1 parameters.put("xwiki.multilingualFields", "title, foo");
139  1 parameters.put("xwiki.supportedLocales", "en, fr");
140  1 parameters.put("xwiki.typedDynamicFields", "property.*");
141  1 parameters.put("xwiki.dynamicFieldTypes", "long, date");
142   
143  1 SolrParams paramsWithAliases = plugin.withFieldAliases("text", new MapSolrParams(parameters));
144   
145    // 5 existing parameters plus one alias.
146  1 assertEquals(6, paramsWithAliases.toNamedList().size());
147   
148    // A default multilingual field.
149  1 assertEquals("title__ title_en title_fr", paramsWithAliases.get("f.title.qf"));
150   
151    // Not a multilingual field.
152  1 assertNull(paramsWithAliases.get("f.comment.qf"));
153   
154    // Not a default field.
155  1 assertNull(paramsWithAliases.get("f.foo.qf"));
156    }
157    }