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