adding recordBasis to SpecimenOrObservationBase and removing some DerivedUnit classes...
[cdmlib.git] / cdmlib-persistence / src / test / java / eu / etaxonomy / cdm / persistence / dao / hibernate / FreeTextSearchIntegration.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.persistence.dao.hibernate;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.hibernate.Hibernate;
21 import org.junit.Ignore;
22 import org.junit.Test;
23 import org.unitils.dbunit.annotation.DataSet;
24 import org.unitils.spring.annotation.SpringBeanByType;
25
26 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
27 import eu.etaxonomy.cdm.model.description.TaxonDescription;
28 import eu.etaxonomy.cdm.model.description.TextData;
29 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
30 import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionElementDao;
31 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
32 import eu.etaxonomy.cdm.persistence.query.OrderHint;
33 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
34 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
35
36 @DataSet
37 @Ignore //TODO indexing does not work at all, even before the unitils upgrade
38 public class FreeTextSearchIntegration extends CdmTransactionalIntegrationTest {
39
40 @SuppressWarnings("unused")
41 private static Log log = LogFactory.getLog(FreeTextSearchIntegration.class);
42
43 @SpringBeanByType
44 ITaxonDao taxonDao;
45
46 @SpringBeanByType
47 IDescriptionElementDao descriptionElementDao;
48
49 // @SpringBeanByType
50 // TaxonAlternativeSpellingSuggestionParser alternativeSpellingSuggestionParser;
51 //
52 // @Test
53 // public void test() {
54 // taxonDao.rebuildIndex();
55 // taxonDao.optimizeIndex();
56 //
57 // descriptionElementDao.rebuildIndex();
58 // descriptionElementDao.optimizeIndex();
59 // setComplete();
60 // endTransaction();
61 // taxonDao.countTaxa("Arum",null); // For some reason this flushes the indexes and allows the next method to create the spellings index
62 // }
63 //
64 // @Test
65 // public void test1() {
66 // alternativeSpellingSuggestionParser.refresh();
67 // }
68
69 @Test
70 public void testSearchTextData() {
71 List<OrderHint> orderHints = new ArrayList<OrderHint>();
72 orderHints.add(new OrderHint("inDescription.titleCache",SortOrder.ASCENDING));
73
74 List<String> propertyPaths = new ArrayList<String>();
75 propertyPaths.add("inDescription");
76 propertyPaths.add("inDescription.taxon");
77 List<DescriptionElementBase> results = descriptionElementDao.search(TextData.class,"Lorem",null,null,orderHints,propertyPaths);
78
79 assertNotNull("searchTextData should return a List",results);
80 assertEquals("there should be 4 TextData entities in the list",4,results.size());
81
82 assertTrue("DescriptionElementBase.feature should be initialized",Hibernate.isInitialized(results.get(0).getFeature()));
83 assertTrue("DescriptionElementBase.inDescription should be initialized",Hibernate.isInitialized(results.get(0).getInDescription()));
84 assertTrue("inDescription should be an instance of TaxonDescription",results.get(0).getInDescription() instanceof TaxonDescription);
85 TaxonDescription taxonDescription = (TaxonDescription)results.get(0).getInDescription();
86 assertTrue("TaxonDescription.taxon should be initialized",Hibernate.isInitialized(taxonDescription.getTaxon()));
87 assertEquals("The results should be sorted alphabetically","Aglaodorum Schott sec. cate-araceae.org",taxonDescription.getTaxon().getTitleCache());
88 }
89
90 @Test
91 public void testCountTextData() {
92 int matches = descriptionElementDao.count(TextData.class,"Lorem");
93 assertEquals("countTextData should return 4",4,matches);
94 }
95
96 @Test
97 public void testSearchWord() {
98 List<OrderHint> orderHints = new ArrayList<OrderHint>();
99 orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));
100 List<String> propertyPaths = new ArrayList<String>();
101 propertyPaths.add("name");
102
103 List<TaxonBase> results = taxonDao.search(null,"Arum", null, null, orderHints, propertyPaths);
104 assertEquals("searchTaxa should return 463 results",46,results.size());
105 assertTrue("TaxonBase.name should be initialized",Hibernate.isInitialized(results.get(0).getName()));
106 }
107
108 @Test
109 public void testSearchCount() {
110 int numberOfResults = taxonDao.count(null,"Arum");
111 assertEquals("countTaxa should return 46",46,numberOfResults);
112
113 }
114
115 @Test
116 public void testSearchPaged() {
117 List<OrderHint> orderHints = new ArrayList<OrderHint>();
118 orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));
119 List<String> propertyPaths = new ArrayList<String>();
120 propertyPaths.add("name");
121 List<TaxonBase> page1 = taxonDao.search(null,"Arum", 30, 0,orderHints,propertyPaths);
122 List<TaxonBase> page2 = taxonDao.search(null,"Arum", 30, 1,orderHints,propertyPaths);
123
124 assertEquals("page 1 should contain 30 taxa",30,page1.size());
125 assertEquals("page 1 should be sorted alphabetically","Arum L.",page1.get(0).getName().getTitleCache());
126 assertEquals("page 1 should be sorted alphabetically","Arum lucanum Cavara & Grande",page1.get(29).getName().getTitleCache());
127 assertEquals("page 2 should contain 16 taxa",16,page2.size());
128 assertEquals("page 2 should be sorted alphabetically","Arum maculatum L.",page2.get(0).getName().getTitleCache());
129 assertEquals("page 2 should be sorted alphabetically","Arum x sooi TerpĆ³",page2.get(15).getName().getTitleCache());
130 }
131
132 @Test
133 public void testSearchPhrase() {
134 List<OrderHint> orderHints = new ArrayList<OrderHint>();
135 orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));
136 List<String> propertyPaths = new ArrayList<String>();
137 propertyPaths.add("name");
138
139 List<TaxonBase> results = taxonDao.search(null,"\"Arum italicum\"", null, null,orderHints,propertyPaths);
140 assertEquals("searchTaxa should return 5 taxa",5,results.size());
141 }
142
143 @Test
144 public void testSearchWildcard() {
145 List<OrderHint> orderHints = new ArrayList<OrderHint>();
146 orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));
147 List<String> propertyPaths = new ArrayList<String>();
148 propertyPaths.add("name");
149
150 List<TaxonBase> results = taxonDao.search(null,"Aroph*", null, null,orderHints,propertyPaths);
151 assertEquals("searchTaxa should return 6 taxa",7,results.size());
152 }
153
154 @Test
155 @Ignore //we currently don't use suggest anymore
156 public void testSuggestSingleTerm() {
157 String suggestion = taxonDao.suggestQuery("Aram");
158 assertNotNull("suggestQuery should return a String",suggestion);
159 assertEquals("The spelling suggestion for \"Aram\" should be \"arum\"","arum",suggestion);
160 }
161
162 @Test
163 @Ignore //we currently don't use suggest anymore
164 public void testSuggestSingleTermInCompositeQuery() {
165 String suggestion = taxonDao.suggestQuery("Aram italicum");
166 assertNotNull("suggestQuery should return a String",suggestion);
167 assertEquals("The spelling suggestion for \"Aram italicum\" should be \"arum italicum\"","arum italicum",suggestion);
168 }
169
170 @Test
171 @Ignore //we currently don't use suggest anymore
172 public void testSuggestMultipleTermsInCompositeQuery() {
173 String suggestion = taxonDao.suggestQuery("Aram italocum");
174 assertNotNull("suggestQuery should return a String",suggestion);
175 assertEquals("The spelling suggestion for \"Aram italocum\" should be \"arum italicum\"","arum italicum",suggestion);
176 }
177
178 @Test
179 @Ignore //we currently don't use suggest anymore
180 public void testSuggestMultipleTermsInCompositeQueryWithAnd() {
181 String suggestion = taxonDao.suggestQuery("Aram AND italocum");
182 assertNotNull("suggestQuery should return a String",suggestion);
183 assertEquals("The spelling suggestion for \"Aram AND italocum\" should be \"+arum +italicum\"","+arum +italicum",suggestion);
184 }
185 }