1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  /**
24   *
25   */
26  package com.gisgraphy.domain.geoloc.service.fulltextsearch;
27  
28  import static com.gisgraphy.domain.valueobject.Pagination.paginate;
29  
30  import java.util.HashMap;
31  
32  import javax.annotation.Resource;
33  
34  import org.apache.commons.lang.RandomStringUtils;
35  import org.junit.Test;
36  import org.springframework.mock.web.MockHttpServletRequest;
37  
38  import com.gisgraphy.domain.geoloc.entity.Adm;
39  import com.gisgraphy.domain.geoloc.entity.City;
40  import com.gisgraphy.domain.geoloc.entity.Country;
41  import com.gisgraphy.domain.geoloc.service.fulltextsearch.spell.SpellCheckerConfig;
42  import com.gisgraphy.domain.repository.ICountryDao;
43  import com.gisgraphy.domain.valueobject.Constants;
44  import com.gisgraphy.domain.valueobject.Output;
45  import com.gisgraphy.domain.valueobject.Pagination;
46  import com.gisgraphy.domain.valueobject.Output.OutputFormat;
47  import com.gisgraphy.domain.valueobject.Output.OutputStyle;
48  import com.gisgraphy.servlet.FulltextServlet;
49  import com.gisgraphy.servlet.GisgraphyServlet;
50  import com.gisgraphy.test.GeolocTestHelper;
51  
52  public class FulltextQueryTest extends AbstractIntegrationHttpSolrTestCase {
53  
54      @Resource
55      private ICountryDao countryDao;
56  
57      @Test
58      public void testFulltextQueryStringPaginationOutputClassOfQextendsGisFeature() {
59  	Pagination pagination = paginate().from(2).to(7);
60  	Output output = Output.withFormat(OutputFormat.JSON).withLanguageCode(
61  		"FR").withStyle(OutputStyle.FULL).withIndentation();
62  	FulltextQuery fulltextQuery = new FulltextQuery("text", pagination,
63  		output, Adm.class, "fr");
64  	assertEquals(pagination, fulltextQuery.getPagination());
65  	assertEquals(output, fulltextQuery.getOutput());
66  	assertEquals(Adm.class, fulltextQuery.getPlaceType());
67  	assertEquals("text", fulltextQuery.getQuery());
68  	assertTrue(fulltextQuery.isOutputIndented());
69  	assertEquals("fr", fulltextQuery.getCountryCode());
70      }
71  
72      @Test
73      public void testFulltextQueryFromAnHttpServletRequest() {
74  	MockHttpServletRequest request = GeolocTestHelper
75  		.createMockHttpServletRequestForFullText();
76  	FulltextQuery query = new FulltextQuery(request);
77  	int firstPaginationIndex = 3;
78  	assertEquals(firstPaginationIndex, query.getFirstPaginationIndex());
79  	assertEquals(FulltextServlet.DEFAULT_MAX_RESULTS+firstPaginationIndex-1, query.getLastPaginationIndex());
80  	assertEquals("the pagination should be limit to "
81  		+ FulltextServlet.DEFAULT_MAX_RESULTS,
82  		FulltextServlet.DEFAULT_MAX_RESULTS, query
83  			.getMaxNumberOfResults());
84  	assertEquals("FR", query.getCountryCode());
85  	assertEquals(OutputFormat.XML, query.getOutputFormat());
86  	assertEquals("FR", query.getOutputLanguage());
87  	assertEquals(OutputStyle.FULL, query.getOutputStyle());
88  	assertEquals(City.class, query.getPlaceType());
89  	assertEquals("query", query.getQuery());
90  
91  	// test first pagination index
92  	// with no value specified
93  	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
94  	request.removeParameter(FulltextServlet.FROM_PARAMETER);
95  	query = new FulltextQuery(request);
96  	assertEquals("When no " + FulltextServlet.FROM_PARAMETER
97  		+ " is specified, the parameter should be "
98  		+ Pagination.DEFAULT_FROM, Pagination.DEFAULT_FROM, query
99  		.getFirstPaginationIndex());
100 	// with a wrong value
101 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
102 	request.setParameter(FulltextServlet.FROM_PARAMETER, "-1");
103 	query = new FulltextQuery(request);
104 	assertEquals("When a wrong " + FulltextServlet.FROM_PARAMETER
105 		+ " is specified, the parameter should be "
106 		+ Pagination.DEFAULT_FROM, Pagination.DEFAULT_FROM, query
107 		.getFirstPaginationIndex());
108 	// with a non mumeric value
109 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
110 	request.setParameter(FulltextServlet.FROM_PARAMETER, "a");
111 	query = new FulltextQuery(request);
112 	assertEquals("When a wrong " + FulltextServlet.FROM_PARAMETER
113 		+ " is specified, the parameter should be "
114 		+ Pagination.DEFAULT_FROM, Pagination.DEFAULT_FROM, query
115 		.getFirstPaginationIndex());
116 
117 	// test last pagination index
118 	// with no value specified
119 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
120 	request.removeParameter(FulltextServlet.TO_PARAMETER);
121 	query = new FulltextQuery(request);
122 	 int expectedLastPagination = (FulltextServlet.DEFAULT_MAX_RESULTS+query.getFirstPaginationIndex()-1);
123 	    assertEquals(
124 	           GisgraphyServlet.TO_PARAMETER
125 		    + " is wrong when no "+GisgraphyServlet.TO_PARAMETER+" is specified ",
126 		    expectedLastPagination, query
127 		    .getLastPaginationIndex());
128 	assertEquals(
129 		"When no "
130 			+ FulltextServlet.TO_PARAMETER
131 			+ " is specified, the  parameter should be set to limit results to "
132 			+ FulltextServlet.DEFAULT_MAX_RESULTS,
133 		FulltextServlet.DEFAULT_MAX_RESULTS, query
134 			.getMaxNumberOfResults());
135 	// with a wrong value
136 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
137 	request.setParameter(FulltextServlet.TO_PARAMETER, "2");// to<from
138 	query = new FulltextQuery(request);
139 	 expectedLastPagination = (FulltextServlet.DEFAULT_MAX_RESULTS+query.getFirstPaginationIndex()-1);
140 	    assertEquals( GisgraphyServlet.TO_PARAMETER
141 		    + " is wrong when wrong "+GisgraphyServlet.TO_PARAMETER+" is specified ",
142 		    expectedLastPagination, query
143 			    .getLastPaginationIndex());
144 	assertEquals("When a wrong " + FulltextServlet.TO_PARAMETER
145 		+ " is specified, the number of results should be "
146 		+ FulltextServlet.DEFAULT_MAX_RESULTS,
147 		FulltextServlet.DEFAULT_MAX_RESULTS, query.getMaxNumberOfResults());
148 	assertEquals("a wrong to does not change the from value", 3, query
149 		.getFirstPaginationIndex());
150 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
151 	//non numeric
152 	request.setParameter(FulltextServlet.TO_PARAMETER, "a");
153 	query = new FulltextQuery(request);
154 	expectedLastPagination = (FulltextServlet.DEFAULT_MAX_RESULTS+query.getFirstPaginationIndex()-1);
155 	assertEquals( GisgraphyServlet.TO_PARAMETER
156 		    + " is wrong when non numeric "+GisgraphyServlet.TO_PARAMETER+" is specified ",
157 		    expectedLastPagination, query
158 			    .getLastPaginationIndex());
159 	assertEquals("a wrong to does not change the from value", 3, query
160 		.getFirstPaginationIndex());
161 	assertEquals("When a wrong " + FulltextServlet.TO_PARAMETER
162 		+ " is specified, the numberOf results should be "
163 		+ FulltextServlet.DEFAULT_MAX_RESULTS,
164 		FulltextServlet.DEFAULT_MAX_RESULTS, query.getMaxNumberOfResults());
165 
166 	// test countrycode
167 	// with no value specified
168 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
169 	request.removeParameter(FulltextServlet.COUNTRY_PARAMETER);
170 	query = new FulltextQuery(request);
171 	assertNull("When no " + FulltextServlet.COUNTRY_PARAMETER
172 		+ " is specified, the parameter should be set to null", query
173 		.getCountryCode());
174 	// with a wrong value
175 	// can not have a wrong value=>always a string
176 
177 	// test outputFormat
178 	// with no value specified
179 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
180 	request.removeParameter(FulltextServlet.FORMAT_PARAMETER);
181 	query = new FulltextQuery(request);
182 	assertEquals("When no " + FulltextServlet.FORMAT_PARAMETER
183 		+ " is specified, the  parameter should be set to  "
184 		+ OutputFormat.getDefault(), OutputFormat.getDefault(), query
185 		.getOutputFormat());
186 	// with wrong value
187 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
188 	request.setParameter(FulltextServlet.FORMAT_PARAMETER, "UNK");
189 	query = new FulltextQuery(request);
190 	assertEquals("When wrong " + FulltextServlet.FORMAT_PARAMETER
191 		+ " is specified, the  parameter should be set to  "
192 		+ OutputFormat.getDefault(), OutputFormat.getDefault(), query
193 		.getOutputFormat());
194 	// test case sensitive
195 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
196 	request.setParameter(FulltextServlet.FORMAT_PARAMETER, "json");
197 	query = new FulltextQuery(request);
198 	assertEquals(FulltextServlet.FORMAT_PARAMETER
199 		+ " should be case insensitive  ", OutputFormat.JSON, query
200 		.getOutputFormat());
201 
202 	// test language
203 	// with no value specified
204 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
205 	request.removeParameter(FulltextServlet.LANG_PARAMETER);
206 	query = new FulltextQuery(request);
207 	assertNull(FulltextServlet.LANG_PARAMETER
208 		+ " should be null when not specified  ", query
209 		.getOutputLanguage());
210 	// with empty string
211 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
212 	request.setParameter(FulltextServlet.LANG_PARAMETER, " ");
213 	query = new FulltextQuery(request);
214 	assertEquals(FulltextServlet.LANG_PARAMETER
215 		+ " should be null when not specified  ",
216 		Output.DEFAULT_LANGUAGE_CODE, query.getOutputLanguage());
217 
218 	// test uppercase
219 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
220 	request.setParameter(FulltextServlet.LANG_PARAMETER, "fr");
221 	query = new FulltextQuery(request);
222 	assertEquals(FulltextServlet.LANG_PARAMETER + " should be uppercase  ",
223 		"FR", query.getOutputLanguage());
224 
225 	// test placetype
226 	// with no value specified
227 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
228 	request.removeParameter(FulltextServlet.PLACETYPE_PARAMETER);
229 	query = new FulltextQuery(request);
230 	assertNull("When no " + FulltextServlet.PLACETYPE_PARAMETER
231 		+ " is specified, the  parameter should be set null ", query
232 		.getPlaceType());
233 	// with wrong value
234 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
235 	request.setParameter(FulltextServlet.PLACETYPE_PARAMETER, "unk");
236 	query = new FulltextQuery(request);
237 	assertNull("When wrong " + FulltextServlet.PLACETYPE_PARAMETER
238 		+ " is specified, the  parameter should be set null ", query
239 		.getPlaceType());
240 	// test case sensitive
241 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
242 	request.setParameter(FulltextServlet.FORMAT_PARAMETER, "city");
243 	query = new FulltextQuery(request);
244 	assertEquals(FulltextServlet.PLACETYPE_PARAMETER
245 		+ " should be case insensitive  ", City.class, query
246 		.getPlaceType());
247 
248 	// test output style
249 	// with no value specified
250 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
251 	request.removeParameter(FulltextServlet.STYLE_PARAMETER);
252 	query = new FulltextQuery(request);
253 	assertEquals("When no " + FulltextServlet.STYLE_PARAMETER
254 		+ " is specified, the  parameter should be set to  "
255 		+ OutputStyle.getDefault(), OutputStyle.getDefault(), query
256 		.getOutputStyle());
257 	// with wrong value
258 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
259 	request.setParameter(FulltextServlet.STYLE_PARAMETER, "UNK");
260 	query = new FulltextQuery(request);
261 	assertEquals("When wrong " + FulltextServlet.STYLE_PARAMETER
262 		+ " is specified, the  parameter should be set to  "
263 		+ OutputStyle.getDefault(), OutputStyle.getDefault(), query
264 		.getOutputStyle());
265 	// test case sensitive
266 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
267 	request.setParameter(FulltextServlet.STYLE_PARAMETER, "medium");
268 	query = new FulltextQuery(request);
269 	assertEquals(FulltextServlet.STYLE_PARAMETER
270 		+ " should be case insensitive  ", OutputStyle.MEDIUM, query
271 		.getOutputStyle());
272 
273 	// test indentation
274 	// with no value specified
275 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
276 	request.removeParameter(GisgraphyServlet.INDENT_PARAMETER);
277 	query = new FulltextQuery(request);
278 	assertFalse("When no " + GisgraphyServlet.INDENT_PARAMETER
279 		+ " is specified, the  parameter should be set to false", query
280 		.isOutputIndented());
281 	// with wrong value
282 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
283 	request.setParameter(GisgraphyServlet.INDENT_PARAMETER, "UNK");
284 	query = new FulltextQuery(request);
285 	assertFalse("When wrong " + GisgraphyServlet.INDENT_PARAMETER
286 		+ " is specified, the  parameter should be set to false", query
287 		.isOutputIndented());
288 	// test case sensitive
289 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
290 	request.setParameter(GisgraphyServlet.INDENT_PARAMETER, "True");
291 	query = new FulltextQuery(request);
292 	assertTrue(GisgraphyServlet.INDENT_PARAMETER
293 		+ " should be case insensitive  ", query.isOutputIndented());
294 	// test with on value
295 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
296 	request.setParameter(GisgraphyServlet.INDENT_PARAMETER, "oN");
297 	query = new FulltextQuery(request);
298 	assertTrue(
299 		GisgraphyServlet.INDENT_PARAMETER
300 			+ " should be true for 'on' value (case insensitive and on value)  ",
301 		query.isOutputIndented());
302 	
303 	
304 	// test spellchecking
305 	// with no value specified
306 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
307 	request.removeParameter(FulltextServlet.SPELLCHECKING_PARAMETER);
308 	query = new FulltextQuery(request);
309 	assertEquals("When no " + FulltextServlet.SPELLCHECKING_PARAMETER
310 		+ " is specified, the  parameter should be the default one",SpellCheckerConfig.activeByDefault, query
311 		.hasSpellChecking());
312 	// with wrong value
313 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
314 	request.setParameter(FulltextServlet.SPELLCHECKING_PARAMETER, "UNK");
315 	query = new FulltextQuery(request);
316 	assertEquals("When wrong " + FulltextServlet.SPELLCHECKING_PARAMETER
317 		+ " is specified, the  parameter should be set to the default one",SpellCheckerConfig.activeByDefault, query
318 		.hasSpellChecking());
319 	// test case sensitive
320 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
321 	request.setParameter(FulltextServlet.SPELLCHECKING_PARAMETER, String.valueOf(!SpellCheckerConfig.activeByDefault).toUpperCase());
322 	query = new FulltextQuery(request);
323 	assertEquals(FulltextServlet.SPELLCHECKING_PARAMETER
324 		+ " should be case insensitive  ", !SpellCheckerConfig.activeByDefault, query.hasSpellChecking());
325 	// test with on value
326 	
327 	boolean savedSpellCheckingValue = SpellCheckerConfig.activeByDefault;
328 	try {
329 		SpellCheckerConfig.activeByDefault = false;
330 		request = GeolocTestHelper.createMockHttpServletRequestForFullText();
331 		request.setParameter(FulltextServlet.SPELLCHECKING_PARAMETER, "oN");
332 		query = new FulltextQuery(request);
333 		assertTrue(
334 			FulltextServlet.SPELLCHECKING_PARAMETER
335 				+ " should be true for 'on' value (case insensitive and on value)  ",
336 			query.hasSpellChecking());
337 	} catch (RuntimeException e) {
338 	    fail(e.getMessage());
339 	} finally {
340 	    //reset the last value
341 		SpellCheckerConfig.activeByDefault = savedSpellCheckingValue;
342 	}
343 	
344 
345 	// test query
346 	//test with good value
347 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
348 	query = new FulltextQuery(request);
349 	assertEquals("query should be set when specified",request.getParameter(FulltextServlet.QUERY_PARAMETER), query.getQuery());
350 	
351 	
352 	// With no value specified
353 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
354 	request.removeParameter(FulltextServlet.QUERY_PARAMETER);
355 	try {
356 	    query = new FulltextQuery(request);
357 	    fail("A null query should throw");
358 	} catch (RuntimeException e) {
359 	}
360 	// empty string
361 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
362 	request.setParameter(FulltextServlet.QUERY_PARAMETER, " ");
363 	try {
364 	    query = new FulltextQuery(request);
365 	    fail("An empty query should throw");
366 	} catch (RuntimeException e) {
367 	}
368 	// too long string
369 	request = GeolocTestHelper.createMockHttpServletRequestForFullText();
370 	request.setParameter(FulltextServlet.QUERY_PARAMETER, RandomStringUtils
371 		.random(FulltextQuery.QUERY_MAX_LENGTH) + 1);
372 	try {
373 	    query = new FulltextQuery(request);
374 	    fail("query must have a maximmum length of "
375 		    + FulltextQuery.QUERY_MAX_LENGTH);
376 	} catch (RuntimeException e) {
377 	}
378 
379     }
380 
381     @Test
382     public void testFulltextQueryString() {
383 	FulltextQuery fulltextQuery = new FulltextQuery("text");
384 	assertEquals("text", fulltextQuery.getQuery());
385 	assertEquals(Output.DEFAULT_OUTPUT, fulltextQuery.getOutput());
386 	assertEquals(Pagination.DEFAULT_PAGINATION, fulltextQuery
387 		.getPagination());
388 	assertNull(fulltextQuery.getPlaceType());
389 	assertEquals("text", fulltextQuery.getQuery());
390     }
391 
392     @Test
393     public void testFulltextQueryWithNullQueryThrows() {
394 	Pagination pagination = paginate().from(2).to(7);
395 	Output output = Output.withFormat(OutputFormat.JSON).withLanguageCode(
396 		"FR").withStyle(OutputStyle.FULL);
397 	try {
398 	    new FulltextQuery(RandomStringUtils
399 		    .random(FulltextQuery.QUERY_MAX_LENGTH) + 1, pagination,
400 		    output, Adm.class, "FR");
401 	    fail("query must have a maximmum length of "
402 		    + FulltextQuery.QUERY_MAX_LENGTH);
403 	} catch (IllegalArgumentException e) {
404 
405 	}
406 
407     }
408 
409     @Test
410     public void testFulltextQueryWithTooLongQueryStringThrows() {
411 	Pagination pagination = paginate().from(2).to(7);
412 	Output output = Output.withFormat(OutputFormat.JSON).withLanguageCode(
413 		"FR").withStyle(OutputStyle.FULL);
414 	try {
415 	    new FulltextQuery(null, pagination, output, Adm.class, "FR");
416 	    fail("Null query should throws");
417 	} catch (IllegalArgumentException e) {
418 
419 	}
420     }
421 
422     @Test
423     public void testFulltextQueryWithEmptyQueryThrows() {
424 	Pagination pagination = paginate().from(2).to(7);
425 	Output output = Output.withFormat(OutputFormat.JSON).withLanguageCode(
426 		"FR").withStyle(OutputStyle.FULL);
427 	try {
428 	    new FulltextQuery(" ", pagination, output, Adm.class, "FR");
429 	    fail("empty query should throws");
430 	} catch (IllegalArgumentException e) {
431 
432 	}
433 
434 	try {
435 	    new FulltextQuery(" ");
436 	    fail("Empty query should throws");
437 	} catch (RuntimeException e) {
438 
439 	}
440     }
441 
442     @Test
443     public void testWithPaginationShouldBeSetToDefaultPaginationIfNull() {
444 	assertEquals(Pagination.DEFAULT_PAGINATION, new FulltextQuery("text")
445 		.withPagination(null).getPagination());
446     }
447 
448     @Test
449     public void testWithPaginationShouldSetThePagination() {
450 	assertEquals(5, new FulltextQuery("text").withPagination(
451 		paginate().from(5).to(23)).getPagination().getFrom());
452 	assertEquals(23, new FulltextQuery("text").withPagination(
453 		paginate().from(5).to(23)).getPagination().getTo());
454     }
455 
456     @Test
457     public void testWithOutputShouldBeSetToDefaultOutputIfNull() {
458 	assertEquals(Output.DEFAULT_OUTPUT, new FulltextQuery("text")
459 		.withOutput(null).getOutput());
460     }
461 
462     @Test
463     public void testWithOutputShouldSetTheOutput() {
464 	FulltextQuery fulltextQuery = new FulltextQuery("text");
465 	Pagination pagination = paginate().from(2).to(7);
466 	fulltextQuery.withPagination(pagination);
467 	assertEquals(pagination, fulltextQuery.getPagination());
468     }
469 
470     @Test
471     public void testWithPlaceTypeShouldBeSetToNullIfNull() {
472 	assertNull(new FulltextQuery("text").withPlaceType(null).getPlaceType());
473     }
474 
475     public void testLimitToCountryCodeShouldSetTheCountryCode() {
476 	FulltextQuery fulltextQuery = new FulltextQuery("text")
477 		.limitToCountryCode("FR");
478 	assertEquals("FR", fulltextQuery.getCountryCode());
479     }
480 
481     @Test
482     public void testLimitToCountryCodeShouldBeSetToNull() {
483 	assertNull(new FulltextQuery("text").limitToCountryCode(null)
484 		.getCountryCode());
485     }
486 
487     @Test
488     public void testWithPlaceTypeShouldSetTheplaceType() {
489 	FulltextQuery fulltextQuery = new FulltextQuery("text");
490 	fulltextQuery.withPlaceType(Adm.class);
491 	assertEquals(Adm.class, fulltextQuery.getPlaceType());
492     }
493 
494     @Test
495     public void testToQueryStringShouldreturnCorrectParams() {
496 	Country france = GeolocTestHelper.createCountryForFrance();
497 	Country saved = countryDao.save(france);
498 	assertNotNull(saved);
499 	assertNotNull(saved.getId());
500 	Pagination pagination = paginate().from(3).to(10);
501 	Output output = Output.withFormat(OutputFormat.JSON).withLanguageCode(
502 		"FR").withStyle(OutputStyle.SHORT).withIndentation();
503 	FulltextQuery fulltextQuery = new FulltextQuery("Saint-André",
504 		pagination, output, Adm.class, "fr");
505 	// split parameters
506 	HashMap<String, String> parameters = GeolocTestHelper.splitURLParams(
507 		fulltextQuery.toQueryString(), "&");
508 	// check parameters
509 	assertEquals(Output.OutputStyle.SHORT.getFieldList("FR"), parameters
510 		.get(Constants.FL_PARAMETER));
511 	assertEquals("wrong indent parameter found", "on", parameters
512 		.get(Constants.INDENT_PARAMETER));
513 	assertEquals("wrong echoparams parameter found", "none", parameters
514 		.get(Constants.ECHOPARAMS_PARAMETER));
515 	assertEquals("wrong start parameter found", "2", parameters
516 		.get(Constants.START_PARAMETER));
517 	assertEquals("wrong rows parameter found", "8", parameters
518 		.get(Constants.ROWS_PARAMETER));
519 	assertEquals("wrong output format parameter found", OutputFormat.JSON
520 		.getParameterValue(), parameters
521 		.get(Constants.OUTPUT_FORMAT_PARAMETER));
522 	assertEquals("wrong query type parameter found",
523 		Constants.SolrQueryType.typed.toString(), parameters
524 			.get(Constants.QT_PARAMETER));
525 	assertEquals("wrong query parameter found",
526 		"Saint-André France Admclass ", parameters
527 			.get(Constants.QUERY_PARAMETER));
528     }
529 
530     @Test
531     public void testToQueryStringShouldreturnCorrectParamsForGeoRSS() {
532 	Country france = GeolocTestHelper.createCountryForFrance();
533 	Country saved = countryDao.save(france);
534 	assertNotNull(saved);
535 	assertNotNull(saved.getId());
536 	Pagination pagination = paginate().from(3).to(10);
537 	Output output = Output.withFormat(OutputFormat.GEORSS)
538 		.withLanguageCode("FR").withStyle(OutputStyle.SHORT)
539 		.withIndentation();
540 	FulltextQuery fulltextQuery = new FulltextQuery("Saint-André",
541 		pagination, output, Adm.class, "fr");
542 	// split parameters
543 	HashMap<String, String> parameters = GeolocTestHelper.splitURLParams(
544 		fulltextQuery.toQueryString(), "&");
545 	// check parameters
546 	assertEquals("wrong field list", Output.OutputStyle.MEDIUM
547 		.getFieldList("FR"), parameters.get(Constants.FL_PARAMETER));
548 	assertEquals("wrong indent parameter found", "on", parameters
549 		.get(Constants.INDENT_PARAMETER));
550 	assertEquals("wrong echoparams parameter found", "none", parameters
551 		.get(Constants.ECHOPARAMS_PARAMETER));
552 	assertEquals("wrong start parameter found", "2", parameters
553 		.get(Constants.START_PARAMETER));
554 	assertEquals("wrong rows parameter found", "8", parameters
555 		.get(Constants.ROWS_PARAMETER));
556 	assertEquals("wrong output format parameter found", OutputFormat.GEORSS
557 		.getParameterValue(), parameters
558 		.get(Constants.OUTPUT_FORMAT_PARAMETER));
559 	assertEquals("wrong stylesheet", Constants.GEORSS_STYLESHEET,
560 		parameters.get(Constants.STYLESHEET_PARAMETER));
561 	assertEquals("wrong query type parameter found",
562 		Constants.SolrQueryType.typed.toString(), parameters
563 			.get(Constants.QT_PARAMETER));
564 	assertEquals("wrong query parameter found",
565 		"Saint-André France Admclass ", parameters
566 			.get(Constants.QUERY_PARAMETER));
567     }
568 
569     @Test
570     public void testToQueryStringShouldreturnCorrectParamsForAtom() {
571 	Country france = GeolocTestHelper.createCountryForFrance();
572 	Country saved = countryDao.save(france);
573 	assertNotNull(saved);
574 	assertNotNull(saved.getId());
575 	Pagination pagination = paginate().from(3).to(10);
576 	Output output = Output.withFormat(OutputFormat.ATOM).withLanguageCode(
577 		"FR").withStyle(OutputStyle.SHORT).withIndentation();
578 	FulltextQuery fulltextQuery = new FulltextQuery("Saint-André",
579 		pagination, output, Adm.class, "fr");
580 	// split parameters
581 	HashMap<String, String> parameters = GeolocTestHelper.splitURLParams(
582 		fulltextQuery.toQueryString(), "&");
583 	// check parameters
584 	assertEquals("wrong field list", Output.OutputStyle.MEDIUM
585 		.getFieldList("FR"), parameters.get(Constants.FL_PARAMETER));
586 	assertEquals("wrong indent parameter found", "on", parameters
587 		.get(Constants.INDENT_PARAMETER));
588 	assertEquals("wrong echoparams parameter found", "none", parameters
589 		.get(Constants.ECHOPARAMS_PARAMETER));
590 	assertEquals("wrong start parameter found", "2", parameters
591 		.get(Constants.START_PARAMETER));
592 	assertEquals("wrong rows parameter found", "8", parameters
593 		.get(Constants.ROWS_PARAMETER));
594 	assertEquals("wrong output format parameter found", OutputFormat.GEORSS
595 		.getParameterValue(), parameters
596 		.get(Constants.OUTPUT_FORMAT_PARAMETER));
597 	assertEquals("wrong stylesheet", Constants.ATOM_STYLESHEET, parameters
598 		.get(Constants.STYLESHEET_PARAMETER));
599 	assertEquals("wrong query type parameter found",
600 		Constants.SolrQueryType.typed.toString(), parameters
601 			.get(Constants.QT_PARAMETER));
602 	assertEquals("wrong query parameter found",
603 		"Saint-André France Admclass ", parameters
604 			.get(Constants.QUERY_PARAMETER));
605     }
606     
607     @Test
608     public void testToQueryStringShouldreturnCorrectParamsForSpellChecking() {
609     	boolean savedSpellCheckingValue = SpellCheckerConfig.activeByDefault;
610     	try {
611     	Country france = GeolocTestHelper.createCountryForFrance();
612 	Country saved = countryDao.save(france);
613 	assertNotNull(saved);
614 	assertNotNull(saved.getId());
615 	SpellCheckerConfig.activeByDefault= true;
616 	SpellCheckerConfig.enabled = false;
617 	Pagination pagination = paginate().from(3).to(10);
618 	Output output = Output.withFormat(OutputFormat.ATOM).withLanguageCode(
619 		"FR").withStyle(OutputStyle.SHORT).withIndentation();
620 	FulltextQuery fulltextQuery = new FulltextQuery("Saint-André",
621 		pagination, output, Adm.class, "fr").withSpellChecking();
622 	// split parameters
623 	HashMap<String, String> parameters = GeolocTestHelper.splitURLParams(
624 		fulltextQuery.toQueryString(), "&");
625 	// check parameters
626 	assertTrue("the fulltextquery should have spellchecking enabled even if spellchecker is disabled", fulltextQuery.hasSpellChecking());
627 	assertTrue("spellchecker should not be listed if spellchecker is disabled", !parameters
628 		.containsKey(Constants.SPELLCHECKER_ENABLED_PARAMETER));
629 	//active spellchecker and re test
630 	SpellCheckerConfig.enabled = true;
631 	fulltextQuery = new FulltextQuery("Saint-André",
632 			pagination, output, Adm.class, "fr").withSpellChecking();
633 	parameters = GeolocTestHelper.splitURLParams(
634 			fulltextQuery.toQueryString(), "&");
635 	assertTrue("the fulltextquery should have spellchecking enabled when spellchecker is enabled", fulltextQuery.hasSpellChecking());
636 	assertEquals("spellchecker should be enabled", "true", parameters
637 			.get(Constants.SPELLCHECKER_ENABLED_PARAMETER));
638 	assertEquals("spellchecker should be enabled", String.valueOf(SpellCheckerConfig.collateResults), parameters
639 			.get(Constants.SPELLCHECKER_COLLATE_RESULTS_PARAMETER));
640 	assertEquals("spellchecker should be enabled",  String.valueOf(SpellCheckerConfig.numberOfSuggestion), parameters
641 			.get(Constants.SPELLCHECKER_NUMBER_OF_SUGGESTION_PARAMETER));
642 	assertEquals("spellchecker should be enabled", SpellCheckerConfig.spellcheckerDictionaryName.toString(), parameters
643 			.get(Constants.SPELLCHECKER_DICTIONARY_NAME_PARAMETER));
644     	} catch (RuntimeException e) {
645 		fail(e.getMessage());
646 	} finally {
647 	    SpellCheckerConfig.activeByDefault = savedSpellCheckingValue;
648 	}
649     }
650     
651     @Test
652     public void testQueryShouldHaveSpellcheckingCorrectDefaultValue(){
653     	boolean savedSpellCheckingValue = SpellCheckerConfig.activeByDefault;
654     	try {
655 			FulltextQuery query = new FulltextQuery("test");
656 			assertEquals(savedSpellCheckingValue, query.hasSpellChecking());
657 			SpellCheckerConfig.activeByDefault = ! SpellCheckerConfig.activeByDefault;
658 			query = new FulltextQuery("test2");
659 			assertEquals(SpellCheckerConfig.activeByDefault, query.hasSpellChecking());
660 		} catch (RuntimeException e) {
661 			SpellCheckerConfig.activeByDefault = savedSpellCheckingValue;
662 		}
663     }
664 
665 }