Project

General

Profile

Download (7.89 KB) Statistics
| Branch: | Tag: | Revision:
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
package eu.etaxonomy.cdm.persistence.dao.hibernate;
10

    
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.io.FileNotFoundException;
16
import java.util.ArrayList;
17
import java.util.List;
18

    
19
import org.apache.commons.logging.Log;
20
import org.apache.commons.logging.LogFactory;
21
import org.hibernate.Hibernate;
22
import org.junit.Ignore;
23
import org.junit.Test;
24
import org.unitils.dbunit.annotation.DataSet;
25
import org.unitils.spring.annotation.SpringBeanByType;
26

    
27
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
28
import eu.etaxonomy.cdm.model.description.TaxonDescription;
29
import eu.etaxonomy.cdm.model.description.TextData;
30
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionElementDao;
32
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
33
import eu.etaxonomy.cdm.persistence.query.OrderHint;
34
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
35
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
36

    
37
@DataSet
38
@Ignore //TODO indexing does not work at all, even before the unitils upgrade
39
public class FreeTextSearchIntegrationTest extends CdmTransactionalIntegrationTest {
40

    
41
	@SuppressWarnings("unused")
42
	private static Log log = LogFactory.getLog(FreeTextSearchIntegrationTest.class);
43

    
44
	@SpringBeanByType
45
	ITaxonDao taxonDao;
46

    
47
	@SpringBeanByType
48
	IDescriptionElementDao descriptionElementDao;
49

    
50
//	@SpringBeanByType
51
//	TaxonAlternativeSpellingSuggestionParser alternativeSpellingSuggestionParser;
52
//
53
//	@Test
54
//	public void test() {
55
//		taxonDao.rebuildIndex();
56
//		taxonDao.optimizeIndex();
57
//
58
//		descriptionElementDao.rebuildIndex();
59
//		descriptionElementDao.optimizeIndex();
60
//		setComplete();
61
//		endTransaction();
62
//		taxonDao.countTaxa("Arum",null); // For some reason this flushes the indexes and allows the next method to create the spellings index
63
//	}
64
//
65
//	@Test
66
//	public void test1() {
67
//		alternativeSpellingSuggestionParser.refresh();
68
//	}
69

    
70
	@Test
71
	public void testSearchTextData() {
72
		List<OrderHint> orderHints = new ArrayList<>();
73
		orderHints.add(new OrderHint("inDescription.titleCache",SortOrder.ASCENDING));
74

    
75
		List<String> propertyPaths = new ArrayList<>();
76
		propertyPaths.add("inDescription");
77
		propertyPaths.add("inDescription.taxon");
78
		List<DescriptionElementBase> results = descriptionElementDao.search(TextData.class,"Lorem",null,null,orderHints,propertyPaths);
79

    
80
		assertNotNull("searchTextData should return a List",results);
81
		assertEquals("there should be 4 TextData entities in the list",4,results.size());
82

    
83
		assertTrue("DescriptionElementBase.feature should be initialized",Hibernate.isInitialized(results.get(0).getFeature()));
84
		assertTrue("DescriptionElementBase.inDescription should be initialized",Hibernate.isInitialized(results.get(0).getInDescription()));
85
		assertTrue("inDescription should be an instance of TaxonDescription",results.get(0).getInDescription() instanceof TaxonDescription);
86
		TaxonDescription taxonDescription = (TaxonDescription)results.get(0).getInDescription();
87
		assertTrue("TaxonDescription.taxon should be initialized",Hibernate.isInitialized(taxonDescription.getTaxon()));
88
		assertEquals("The results should be sorted alphabetically","Aglaodorum Schott sec. cate-araceae.org",taxonDescription.getTaxon().getTitleCache());
89
	}
90

    
91
    @Test
92
    public void testCountTextData() {
93
    	long matches = descriptionElementDao.count(TextData.class,"Lorem");
94
    	assertEquals("countTextData should return 4",4,matches);
95
    }
96

    
97
    @Test
98
    public void testSearchWord() {
99
    	List<OrderHint> orderHints = new ArrayList<>();
100
    	orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));
101
    	List<String> propertyPaths = new ArrayList<>();
102
    	propertyPaths.add("name");
103

    
104
    	List<TaxonBase> results = taxonDao.search(null,"Arum", null, null, orderHints, propertyPaths);
105
		assertEquals("searchTaxa should return 463 results",46,results.size());
106
		assertTrue("TaxonBase.name should be initialized",Hibernate.isInitialized(results.get(0).getName()));
107
    }
108

    
109
    @Test
110
    public void testSearchCount() {
111
		long numberOfResults = taxonDao.count(null,"Arum");
112
		assertEquals("countTaxa should return 46",46,numberOfResults);
113
    }
114

    
115
    @Test
116
    public void testSearchPaged() {
117
    	List<OrderHint> orderHints = new ArrayList<>();
118
    	orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));
119
    	List<String> propertyPaths = new ArrayList<>();
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<>();
135
    	orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));
136
    	List<String> propertyPaths = new ArrayList<>();
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<>();
146
    	orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));
147
    	List<String> propertyPaths = new ArrayList<>();
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

    
186
    @Override
187
    public void createTestDataSet() throws FileNotFoundException {}
188
}
    (1-1/1)