Committing large number of changes relating to versioning implementation (#108)
[cdmlib.git] / cdmlib-persistence / src / test / java / eu / etaxonomy / cdm / persistence / dao / hibernate / FreeTextSearchIntegration.java
1 package eu.etaxonomy.cdm.persistence.dao.hibernate;
2 import static org.junit.Assert.assertEquals;
3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertTrue;
5
6 import java.util.List;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.hibernate.Hibernate;
11 import org.junit.Test;
12 import org.unitils.dbunit.annotation.DataSet;
13 import org.unitils.spring.annotation.SpringApplicationContext;
14 import org.unitils.spring.annotation.SpringBeanByType;
15
16 import eu.etaxonomy.cdm.model.description.TaxonDescription;
17 import eu.etaxonomy.cdm.model.description.TextData;
18 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
19 import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionElementDao;
20 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
21 import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
22
23 @DataSet
24 public class FreeTextSearchIntegration extends CdmIntegrationTest {
25
26 private static Log log = LogFactory.getLog(FreeTextSearchIntegration.class);
27
28 @SpringBeanByType
29 ITaxonDao taxonDao;
30
31 @SpringBeanByType
32 IDescriptionElementDao descriptionElementDao;
33
34 // @SpringBeanByType
35 // TaxonAlternativeSpellingSuggestionParser alternativeSpellingSuggestionParser;
36
37 // @Test
38 // public void test() {
39 // taxonDao.rebuildIndex();
40 // taxonDao.optimizeIndex();
41 //
42 // descriptionElementDao.rebuildIndex();
43 // descriptionElementDao.optimizeIndex();
44 // }
45
46 // @Test
47 // public void test1() {
48 // alternativeSpellingSuggestionParser.refresh();
49 // }
50
51 @Test
52 public void testSearchTextData() {
53 List<TextData> results = descriptionElementDao.searchTextData("Lorem",null,null);
54
55 assertNotNull("searchTextData should return a List",results);
56 assertEquals("there should be 4 TextData entities in the list",4,results.size());
57
58
59 assertTrue("DescriptionElementBase.feature should be initialized",Hibernate.isInitialized(results.get(0).getFeature()));
60 assertTrue("DescriptionElementBase.inDescription should be initialized",Hibernate.isInitialized(results.get(0).getInDescription()));
61 assertTrue("inDescription should be an instance of TaxonDescription",results.get(0).getInDescription() instanceof TaxonDescription);
62 TaxonDescription taxonDescription = (TaxonDescription)results.get(0).getInDescription();
63 assertTrue("TaxonDescription.taxon should be initialized",Hibernate.isInitialized(taxonDescription.getTaxon()));
64 assertEquals("The results should be sorted alphabetically","Aglaodorum Schott sec. cate-araceae.org",taxonDescription.getTaxon().getTitleCache());
65 }
66
67 @Test
68 public void testCountTextData() {
69 int matches = descriptionElementDao.countTextData("Lorem");
70 assertEquals("countTextData should return 4",4,matches);
71 }
72
73 @Test
74 public void testSearchWord() {
75 List<TaxonBase> results = taxonDao.searchTaxa("Arum",null, null, null);
76 assertEquals("searchTaxa should return 43 results",43,results.size());
77 assertTrue("TaxonBase.name should be initialized",Hibernate.isInitialized(results.get(0).getName()));
78 }
79
80 @Test
81 public void testSearchCount() {
82 int numberOfResults = taxonDao.countTaxa("Arum",null);
83 assertEquals("countTaxa should return 43",43,numberOfResults);
84
85 }
86
87 @Test
88 public void testSearchPaged() {
89 List<TaxonBase> page1 = taxonDao.searchTaxa("Arum",null, 30, 0);
90 List<TaxonBase> page2 = taxonDao.searchTaxa("Arum",null, 30, 1);
91
92 assertEquals("page 1 should contain 30 taxa",30,page1.size());
93 assertEquals("page 1 should be sorted alphabetically","Arum L.",page1.get(0).getName().getTitleCache());
94 assertEquals("page 1 should be sorted alphabetically","Arum lucanum Cavara & Grande",page1.get(29).getName().getTitleCache());
95 assertEquals("page 2 should contain 13 taxa",13,page2.size());
96 assertEquals("page 2 should be sorted alphabetically","Arum maculatum L.",page2.get(0).getName().getTitleCache());
97 assertEquals("page 2 should be sorted alphabetically","Arum x sooi TerpĆ³",page2.get(12).getName().getTitleCache());
98 }
99
100 @Test
101 public void testSearchPhrase() {
102 List<TaxonBase> results = taxonDao.searchTaxa("\"Arum italicum\"",null, null, null);
103 assertEquals("searchTaxa should return 5 taxa",5,results.size());
104 }
105
106 @Test
107 public void testSearchWildcard() {
108 List<TaxonBase> results = taxonDao.searchTaxa("Aroph*", null, null, null);
109 assertEquals("searchTaxa should return 6 taxa",7,results.size());
110 }
111
112 @Test
113 public void testSuggestSingleTerm() {
114 String suggestion = taxonDao.suggestQuery("Aram");
115 assertNotNull("suggestQuery should return a String",suggestion);
116 assertEquals("The spelling suggestion for \"Aram\" should be \"arum\"","arum",suggestion);
117 }
118
119 @Test
120 public void testSuggestSingleTermInCompositeQuery() {
121 String suggestion = taxonDao.suggestQuery("Aram italicum");
122 assertNotNull("suggestQuery should return a String",suggestion);
123 assertEquals("The spelling suggestion for \"Aram italicum\" should be \"arum italicum\"","arum italicum",suggestion);
124 }
125
126 @Test
127 public void testSuggestMultipleTermsInCompositeQuery() {
128 String suggestion = taxonDao.suggestQuery("Aram italocum");
129 assertNotNull("suggestQuery should return a String",suggestion);
130 assertEquals("The spelling suggestion for \"Aram italocum\" should be \"arum italicum\"","arum italicum",suggestion);
131 }
132
133 @Test
134 public void testSuggestMultipleTermsInCompositeQueryWithAnd() {
135 String suggestion = taxonDao.suggestQuery("Aram AND italocum");
136 assertNotNull("suggestQuery should return a String",suggestion);
137 assertEquals("The spelling suggestion for \"Aram AND italocum\" should be \"+arum +italicum\"","+arum +italicum",suggestion);
138 }
139 }