ref #3547 Cleanup TaxonDao.testGetTaxaByNameAndArea.xml test data a bit
[cdmlib.git] / cdmlib-persistence / src / test / java / eu / etaxonomy / cdm / persistence / dao / hibernate / taxon / TaxonDaoHibernateImplTest.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.taxon;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertNull;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18
19 import java.io.FileNotFoundException;
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.UUID;
25
26 import org.apache.log4j.Level;
27 import org.hibernate.Hibernate;
28 import org.hibernate.envers.query.AuditEntity;
29 import org.hibernate.envers.query.criteria.AuditCriterion;
30 import org.hibernate.proxy.HibernateProxy;
31 import org.junit.After;
32 import org.junit.Assert;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.unitils.dbunit.annotation.DataSet;
36 import org.unitils.spring.annotation.SpringBeanByType;
37
38 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
39 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
40 import eu.etaxonomy.cdm.model.common.Marker;
41 import eu.etaxonomy.cdm.model.common.MarkerType;
42 import eu.etaxonomy.cdm.model.location.NamedArea;
43 import eu.etaxonomy.cdm.model.name.NonViralName;
44 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
45 import eu.etaxonomy.cdm.model.reference.Reference;
46 import eu.etaxonomy.cdm.model.taxon.Classification;
47 import eu.etaxonomy.cdm.model.taxon.Synonym;
48 import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
49 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
50 import eu.etaxonomy.cdm.model.taxon.Taxon;
51 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
52 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
53 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
54 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
55 import eu.etaxonomy.cdm.model.view.AuditEvent;
56 import eu.etaxonomy.cdm.model.view.AuditEventRecord;
57 import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
58 import eu.etaxonomy.cdm.persistence.dao.common.AuditEventSort;
59 import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
60 import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
61 import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
62 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
63 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
64 import eu.etaxonomy.cdm.persistence.query.GroupByCount;
65 import eu.etaxonomy.cdm.persistence.query.GroupByDate;
66 import eu.etaxonomy.cdm.persistence.query.Grouping;
67 import eu.etaxonomy.cdm.persistence.query.MatchMode;
68 import eu.etaxonomy.cdm.persistence.query.NativeSqlOrderHint;
69 import eu.etaxonomy.cdm.persistence.query.OrderHint;
70 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
71 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
72 import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
73
74 /**
75 * @author a.mueller
76 * @author ben.clark
77 *
78 */
79 public class TaxonDaoHibernateImplTest extends CdmTransactionalIntegrationTest {
80
81 @SpringBeanByType
82 private ITaxonDao taxonDao;
83
84 @SpringBeanByType
85 private IClassificationDao classificationDao;
86
87 @SpringBeanByType
88 private IReferenceDao referenceDao;
89
90 @SpringBeanByType
91 IDefinedTermDao definedTermDao;
92
93 private UUID uuid;
94 private UUID sphingidae;
95 private UUID acherontia;
96 private UUID rethera;
97 private UUID retheraSecCdmtest;
98 private UUID atroposAgassiz; // a Synonym
99 private UUID atroposLeach; // a Synonym
100 private UUID acherontiaLachesis;
101
102 private AuditEvent previousAuditEvent;
103 private AuditEvent mostRecentAuditEvent;
104
105 private UUID northernAmericaUuid;
106 private UUID southernAmericaUuid;
107 private UUID antarcticaUuid;
108
109 private UUID classificationUuid;
110
111 private static final String[] TABLE_NAMES = new String[] {
112 "HOMOTYPICALGROUP", "HOMOTYPICALGROUP_AUD", "REFERENCE", "REFERENCE_AUD", "SYNONYMRELATIONSHIP", "SYNONYMRELATIONSHIP_AUD", "TAXONBASE", "TAXONBASE_AUD"
113 , "TAXONNAMEBASE", "TAXONNAMEBASE_AUD", "TAXONRELATIONSHIP", "TAXONRELATIONSHIP_AUD" };
114
115
116 @Before
117 public void setUp() {
118
119 uuid = UUID.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2");
120 sphingidae = UUID.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9");
121 acherontia = UUID.fromString("c5cc8674-4242-49a4-aada-72d63194f5fa");
122 acherontiaLachesis = UUID.fromString("b04cc9cb-2b4a-4cc4-a94a-3c93a2158b06");
123 atroposAgassiz = UUID.fromString("d75b2e3d-7394-4ada-b6a5-93175b8751c1");
124 atroposLeach = UUID.fromString("3da4ab34-6c50-4586-801e-732615899b07");
125 rethera = UUID.fromString("a9f42927-e507-4fda-9629-62073a908aae");
126 retheraSecCdmtest = UUID.fromString("a9f42927-e507-4fda-9629-62073a908aae");
127
128 previousAuditEvent = new AuditEvent();
129 previousAuditEvent.setRevisionNumber(1025);
130 previousAuditEvent.setUuid(UUID.fromString("a680fab4-365e-4765-b49e-768f2ee30cda"));
131 mostRecentAuditEvent = new AuditEvent();
132 mostRecentAuditEvent.setRevisionNumber(1026);
133 mostRecentAuditEvent.setUuid(UUID.fromString("afe8e761-8545-497b-9134-6a6791fc0b0d"));
134 AuditEventContextHolder.clearContext(); // By default we're in the current view (i.e. view == null)
135
136 northernAmericaUuid = UUID.fromString("2757e726-d897-4546-93bd-7951d203bf6f");
137 southernAmericaUuid = UUID.fromString("6310b3ba-96f4-4855-bb5b-326e7af188ea");
138 antarcticaUuid = UUID.fromString("791b3aa0-54dd-4bed-9b68-56b4680aad0c");
139
140 classificationUuid = UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
141 }
142
143 @After
144 public void tearDown() {
145 AuditEventContextHolder.clearContext();
146 }
147
148 /**
149 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#TaxonDaoHibernateImpl()}.
150 */
151 @Test
152 @DataSet
153 public void testInit() {
154 logger.warn("testInit()");
155 assertNotNull("Instance of ITaxonDao expected",taxonDao);
156 assertNotNull("Instance of IReferenceDao expected",referenceDao);
157 }
158
159 /**
160 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}.
161 */
162 @Test
163 @DataSet
164 public void testGetTaxaByName() {
165 Reference sec = referenceDao.findById(1);
166 assert sec != null : "sec must exist";
167
168 List<TaxonBase> results = taxonDao.getTaxaByName("Aus", sec);
169 assertNotNull("getTaxaByName should return a List", results);
170 //assertFalse("The list should not be empty", results.isEmpty());
171 assertTrue(results.size() == 1);
172
173 results = taxonDao.getTaxaByName("A*", MatchMode.BEGINNING, true, null, null);
174 assertNotNull("getTaxaByName should return a List", results);
175
176 int numberOfTaxaByName_A = 9;
177
178 logger.setLevel(Level.DEBUG); //FIXME #######################
179 if (logger.isDebugEnabled()) {
180 for (int i = 0; i < results.size(); i++) {
181 String nameCache = "";
182 TaxonNameBase<?,?> taxonNameBase= results.get(i).getName();
183 nameCache = HibernateProxyHelper.deproxy(taxonNameBase, NonViralName.class).getNameCache();
184 logger.debug(results.get(i).getClass() + "(" + i +")" +
185 ": Name Cache = " + nameCache + ", Title Cache = " + results.get(i).getTitleCache());
186 }
187 }
188
189 assertEquals(numberOfTaxaByName_A, results.size());
190
191
192 //System.err.println("Species group: " + Rank.SPECIESGROUP().getId() + "Species: " + Rank.SPECIES().getId() + "Section Botany: "+ Rank.SECTION_BOTANY());
193
194 // assertEquals(results.get(0).getTitleCache(), "Abies sec. ???");
195 // assertEquals(results.get(1).getTitleCache(), "Abies Mill.");
196 // assertEquals(results.get(2).getTitleCache(), "Abies mill. sec. ???");
197 // assertEquals(results.get(3).getTitleCache(), "Abies alba sec. ???");
198 // assertEquals(results.get(4).getTitleCache(), "Abies alba Michx. sec. ???");
199 // assertEquals(results.get(5).getTitleCache(), "Abies alba Mill. sec. ???");
200
201 results = taxonDao.getTaxaByName("A*", MatchMode.BEGINNING, true, null, null);
202 assertNotNull("getTaxaByName should return a List", results);
203 assertEquals(numberOfTaxaByName_A, results.size());
204
205 results = taxonDao.getTaxaByName("Aus", MatchMode.EXACT, true, null, null);
206 assertNotNull("getTaxaByName should return a List", results);
207 assertEquals("Results list should contain one entity",1,results.size());
208 }
209
210
211 @Test
212 @DataSet (loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
213 public void testGetTaxaByNameWithMisappliedNames(){
214
215 Classification classification = classificationDao.load(classificationUuid);
216
217 /* NOTE:
218 * The testdata contains 3 misapplied names (1. nameCache = Aus, 2. nameCache = Rethera, 3. nameCache = Daphnis), two contained in the classification used in this test,
219 * the other one is not contained in any classification. This latter case is the more general situation.
220 * Misapplied names should be found regardless of whether they are contained in a classification or not.
221 */
222 //two accepted taxa starting with R in classification "TestBaum"
223 List<TaxonBase> results = taxonDao.getTaxaByName(true, false, false, "R*", classification, MatchMode.BEGINNING, null, null, null, null);
224 Assert.assertEquals("There should be 2 Taxa", 2, results.size());
225
226 //three taxa, 2 accepted and 1 misapplied name starting with R
227 results = taxonDao.getTaxaByName(true, false, true, "R*", null, MatchMode.BEGINNING, null, null, null, null);
228 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
229
230 //one synonym is not in a synonymrelationship
231 results = taxonDao.getTaxaByName(true, true, true, "A*", null, MatchMode.BEGINNING, null, null, null, null);
232 Assert.assertEquals("There should be 11 Taxa",11, results.size());
233
234 //two accepted taxa in classification and 1 misapplied name with accepted name in classification
235 results = taxonDao.getTaxaByName(true, true, true, "R*", classification, MatchMode.BEGINNING, null, null, null, null);
236 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
237
238 //same as above because all taxa, synonyms and misapplied names starting with R are in the classification
239 results = taxonDao.getTaxaByName(true, true, true, "R*", null, MatchMode.BEGINNING, null, null, null, null);
240 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
241
242 //find misapplied names with accepted taxon in the classification, the accepted taxa of two misapplied names are in the classification
243 results = taxonDao.getTaxaByName(false, false, true, "*", classification, MatchMode.BEGINNING, null, null, null, null);
244 Assert.assertEquals("There should be 2 Taxa", 2, results.size());
245
246 //find misapplied names beginning with R
247 results = taxonDao.getTaxaByName(false, false, true, "R*", null, MatchMode.BEGINNING, null, null, null, null);
248 Assert.assertEquals("There should be 1 Taxa", 1, results.size());
249
250 //find all three misapplied names
251 results = taxonDao.getTaxaByName(false, false, true, "*", null, MatchMode.BEGINNING, null, null, null, null);
252 Assert.assertEquals("There should be 3 Taxa", 3, results.size());
253
254 }
255
256 /**
257 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}.
258 */
259 @Test
260 @DataSet
261 public void testGetTaxaByNameForEditor() {
262 Reference sec = referenceDao.findById(1);
263 assert sec != null : "sec must exist";
264
265 List<UuidAndTitleCache<IdentifiableEntity>> results = taxonDao.getTaxaByNameForEditor(true, true, false,false,"Mand", null, MatchMode.BEGINNING, null);
266 assertNotNull("getTaxaByName should return a List", results);
267 //assertFalse("The list should not be empty", results.isEmpty());
268 assertTrue(results.size() == 5);
269
270
271 results = taxonDao.getTaxaByNameForEditor(true, true, false, false,"A",null, MatchMode.BEGINNING, null);
272 assertNotNull("getTaxaByName should return a List", results);
273 assertEquals(results.size(), 12);
274
275
276 results = taxonDao.getTaxaByNameForEditor(true, false,false, false,"A", null,MatchMode.BEGINNING, null);
277 assertNotNull("getTaxaByName should return a List", results);
278 assertTrue(results.size() == 9);
279 assertEquals(results.get(0).getType(), Taxon.class);
280
281 results = taxonDao.getTaxaByNameForEditor(false, true,false,false, "A", null,MatchMode.BEGINNING, null);
282 assertNotNull("getTaxaByName should return a List", results);
283 assertTrue(results.size() == 3);
284 assertEquals(results.get(0).getType(), Synonym.class);
285
286 results = taxonDao.getTaxaByNameForEditor(true, true,false,false,"Aus", null,MatchMode.EXACT, null);
287 assertNotNull("getTaxaByName should return a List", results);
288 assertEquals("Results list should contain one entity",1,results.size());
289
290 results = taxonDao.getTaxaByNameForEditor(true, true,true,false,"A", null,MatchMode.BEGINNING, null);
291 assertNotNull("getTaxaByName should return a List", results);
292 assertEquals("Results list should contain one entity",15,results.size());
293
294 //TODO: test the search for misapplied names
295
296 }
297
298
299 /**
300 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}
301 * restricting the search by a set of Areas.
302 */
303 @Test
304 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
305 public void testGetTaxaByNameAndArea() {
306
307 Set<NamedArea> namedAreas = new HashSet<NamedArea>();
308 namedAreas.add((NamedArea)definedTermDao.load(northernAmericaUuid));
309 //namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
310 //namedAreas.add((NamedArea)definedTermDao.load(antarcticaUuid));
311
312 Classification taxonmicTree = classificationDao.findByUuid(classificationUuid);
313
314 // prepare some synonym relation ships for some tests
315 Synonym synAtroposAgassiz = (Synonym)taxonDao.findByUuid(atroposAgassiz);
316 Taxon taxonRethera = (Taxon)taxonDao.findByUuid(rethera);
317 taxonRethera.addSynonym(synAtroposAgassiz, SynonymRelationshipType.SYNONYM_OF());
318 logger.warn("addSynonym(..)");
319
320 Synonym synAtroposLeach = (Synonym)taxonDao.findByUuid(atroposLeach);
321 Taxon taxonRetheraSecCdmtest = (Taxon)taxonDao.findByUuid(retheraSecCdmtest);
322 taxonRetheraSecCdmtest.addSynonym(synAtroposLeach, SynonymRelationshipType.SYNONYM_OF());
323 this.taxonDao.save(taxonRetheraSecCdmtest);
324
325 Taxon test = (Taxon)this.taxonDao.findByUuid(retheraSecCdmtest);
326 // Set<Synonym> synonyms3 = test.getSynonyms();
327 // 1. searching for a taxon (Rethera)
328 //long numberOfTaxa = taxonDao.countTaxaByName(Taxon.class, "Rethera", null, MatchMode.BEGINNING, namedAreas);
329
330 List<TaxonBase> results = taxonDao.getTaxaByName(true,false, false, "Rethera", null, MatchMode.BEGINNING, namedAreas,
331 null, null, null);
332 assertNotNull("getTaxaByName should return a List", results);
333 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
334
335 // 2. searching for a taxon (Rethera) contained in a specific classification
336 results = taxonDao.getTaxaByName(true, false, false, "Rethera", taxonmicTree, MatchMode.BEGINNING, namedAreas,
337 null, null, null);
338 assertNotNull("getTaxaByName should return a List", results);
339 assertTrue("expected to find one taxon but found "+results.size(), results.size() == 1);
340
341
342 // 3. searching for Synonyms
343 results = taxonDao.getTaxaByName(false, true, false, "Atropo", null, MatchMode.ANYWHERE, null,
344 null, null, null);
345 assertNotNull("getTaxaByName should return a List", results);
346 /*System.err.println(results.get(0).getTitleCache() + " - " +results.get(1).getTitleCache() + " - " +results.get(2).getTitleCache() );
347
348
349 System.err.println(((Synonym)results.get(0)).getAcceptedTaxa().contains(taxonRethera)+ " - " +((Synonym)results.get(1)).getAcceptedTaxa().contains(taxonRethera)+ " - " +((Synonym)results.get(2)).getAcceptedTaxa().contains(taxonRethera)+ " - " );
350 */
351 assertTrue("expected to find three taxa but found "+results.size(), results.size() == 3);
352
353 // 4. searching for Synonyms
354 results = taxonDao.getTaxaByName(false, true, false, "Atropo", null, MatchMode.BEGINNING, null,
355 null, null, null);
356 assertNotNull("getTaxaByName should return a List", results);
357 assertTrue("expected to find three taxa but found "+results.size(), results.size() == 3);
358
359
360 // 5. searching for a Synonyms and Taxa
361 // create a synonym relationship first
362 results = taxonDao.getTaxaByName(true, true, false, "A", null, MatchMode.BEGINNING, namedAreas,
363 null, null, null);
364 //only five taxa have a distribution
365 assertNotNull("getTaxaByName should return a List", results);
366 assertTrue("expected to find 8 taxa but found "+results.size(), results.size() == 8);
367 }
368
369
370 /**
371 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#findByNameTitleCache(Class<? extends TaxonBase>clazz, String queryString, Classification classification, MatchMode matchMode, Set<NamedArea> namedAreas, Integer pageNumber, Integer pageSize, List<String> propertyPaths)}
372 * restricting the search by a set of Areas.
373 */
374 @Test
375 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
376 public void testFindByNameTitleCache() {
377
378 Set<NamedArea> namedAreas = new HashSet<NamedArea>();
379 namedAreas.add((NamedArea)definedTermDao.load(northernAmericaUuid));
380 //namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
381 //namedAreas.add((NamedArea)definedTermDao.load(antarcticaUuid));
382
383 Classification classification = classificationDao.findByUuid(classificationUuid);
384
385 // prepare some synonym relation ships for some tests
386 Synonym synAtroposAgassiz = (Synonym)taxonDao.findByUuid(atroposAgassiz);
387 Taxon taxonRethera = (Taxon)taxonDao.findByUuid(rethera);
388 taxonRethera.addSynonym(synAtroposAgassiz, SynonymRelationshipType.SYNONYM_OF());
389 logger.warn("addSynonym(..)");
390 this.taxonDao.clear();
391 Synonym synAtroposLeach = (Synonym)taxonDao.findByUuid(atroposLeach);
392 Taxon taxonRetheraSecCdmtest = (Taxon)taxonDao.findByUuid(retheraSecCdmtest);
393 taxonRetheraSecCdmtest.addSynonym(synAtroposLeach, SynonymRelationshipType.SYNONYM_OF());
394 this.taxonDao.clear();
395 // 1. searching for a taxon (Rethera)
396 //long numberOfTaxa = taxonDao.countTaxaByName(Taxon.class, "Rethera", null, MatchMode.BEGINNING, namedAreas);
397
398 List<TaxonBase> results = taxonDao.findByNameTitleCache(true, false, "Rethera Rothschild & Jordan, 1903", null, MatchMode.EXACT, namedAreas,
399 null, null, null);
400 assertNotNull("getTaxaByName should return a List", results);
401 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
402
403 // 2. searching for a taxon (Rethera) contained in a specific classification
404 results = taxonDao.findByNameTitleCache(true, false, "Rethera Rothschild & Jordan, 1903", classification, MatchMode.EXACT, namedAreas,
405 null, null, null);
406 assertNotNull("getTaxaByName should return a List", results);
407 assertTrue("expected to find one taxon but found "+results.size(), results.size() == 1);
408
409
410 // 3. searching for Synonyms
411 results = taxonDao.findByNameTitleCache(false, true, "*Atropo", null, MatchMode.ANYWHERE, null,
412 null, null, null);
413 assertNotNull("getTaxaByName should return a List", results);
414
415 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
416
417 // 4. searching for Synonyms
418 results = taxonDao.findByNameTitleCache(false, true, "Atropo", null, MatchMode.BEGINNING, null,
419 null, null, null);
420 assertNotNull("getTaxaByName should return a List", results);
421 assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
422
423
424 // 5. searching for a Synonyms and Taxa
425 // create a synonym relationship first
426 Synonym syn = (Synonym)taxonDao.findByUuid(this.atroposLeach);
427 Taxon tax = (Taxon) taxonDao.findByUuid(rethera);
428 tax.addSynonym(syn, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
429
430 taxonDao.save(tax);
431 results = taxonDao.findByNameTitleCache(true, true, "A", null, MatchMode.BEGINNING, namedAreas,
432 null, null, null);
433 assertNotNull("getTaxaByName should return a List", results);
434 assertTrue("expected to find 8 taxa but found "+results.size(), results.size() == 8);
435 }
436
437 @Test
438 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
439 public void testTaxonNameInTwoClassifications(){
440 int numberOfClassifications = classificationDao.count();
441 List<String> propertyPaths = new ArrayList<String>();
442 propertyPaths.add("taxonNodes");
443 List<TaxonBase> taxa = taxonDao.getTaxaByName(true, true, false, "P", null, MatchMode.BEGINNING, null, null, null, null);
444 Taxon taxon = (Taxon)taxa.get(0);
445 Set<TaxonNode> nodes = taxon.getTaxonNodes();
446 assertTrue(nodes.size() == 1);
447 //assertNotNull(taxa);
448 //assertTrue(taxa.size() > 0);
449 }
450
451 @Test
452 @DataSet
453 public void testFindByUuid() {
454 Taxon taxon = (Taxon)taxonDao.findByUuid(uuid);
455 assertNotNull("findByUuid should return a taxon",taxon);
456 assertFalse("findByUuid should not return a taxon with it's name initialized",Hibernate.isInitialized(taxon.getName()));
457 }
458
459 @Test
460 @DataSet
461 public void testLoad() {
462 List<String> propertyPaths = new ArrayList<String>();
463 propertyPaths.add("name");
464 propertyPaths.add("sec");
465 Taxon taxon = (Taxon)taxonDao.load(uuid, propertyPaths);
466 assertNotNull("findByUuid should return a taxon",taxon);
467 assertTrue("load should return a taxon with it's name initialized, given that the property was specified in the method",Hibernate.isInitialized(taxon.getName()));
468 assertTrue("load should return a taxon with it's secundum reference initialized, given that the property was specified in the method",Hibernate.isInitialized(taxon.getSec()));
469 }
470
471 @Test
472 @DataSet
473 public void testCountTaxonRelationships() {
474 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
475 assert taxon != null : "taxon must exist";
476
477 int numberOfRelatedTaxa = taxonDao.countTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), TaxonRelationship.Direction.relatedTo);
478 assertEquals("countTaxonRelationships should return 23", 23, numberOfRelatedTaxa);
479 }
480
481 @Test
482 @DataSet
483 public void testCountTaxaByName() {
484 long numberOfTaxa = taxonDao.countTaxaByName(true, false, false, "A", null, MatchMode.BEGINNING, null);
485 assertEquals(numberOfTaxa, 9);
486 numberOfTaxa = taxonDao.countTaxaByName(true, false, false, "Aus aus", null, MatchMode.EXACT, null);
487 assertEquals(numberOfTaxa, 1);
488 numberOfTaxa = taxonDao.countTaxaByName(false, true, false, "A", null, MatchMode.BEGINNING, null);
489 assertEquals(numberOfTaxa, 3);
490 numberOfTaxa = taxonDao.countTaxaByName(true, true, false, "A", null, MatchMode.BEGINNING, null);
491 assertEquals(numberOfTaxa,12);
492 numberOfTaxa = taxonDao.countTaxaByName(true, true, false, "Aasfwerfwf fffe", null, MatchMode.BEGINNING, null);
493 assertEquals(numberOfTaxa, 0);
494 // FIXME implement test for search in specific classification
495 // Reference reference = referenceDao.findByUuid(UUID.fromString("596b1325-be50-4b0a-9aa2-3ecd610215f2"));
496 // numberOfTaxa = taxonDao.countTaxaByName("A*", MatchMode.BEGINNING, SelectMode.ALL, null, null);
497 // assertEquals(numberOfTaxa, 2);
498 }
499
500 @Test
501 @DataSet
502 public void testRelatedTaxa() {
503 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
504 assert taxon != null : "taxon must exist";
505
506 List<String> propertyPaths = new ArrayList<String>();
507 propertyPaths.add("fromTaxon");
508 propertyPaths.add("fromTaxon.name");
509 List<OrderHint> orderHints = new ArrayList<OrderHint>();
510 orderHints.add(new OrderHint("relatedFrom.name.genusOrUninomial", SortOrder.ASCENDING));
511 orderHints.add(new OrderHint("relatedFrom.name.specificEpithet", SortOrder.ASCENDING));
512 orderHints.add(new OrderHint("relatedFrom.name.infraSpecificEpithet", SortOrder.ASCENDING));
513
514 List<TaxonRelationship> relatedTaxa = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), null, null, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
515 assertNotNull("getRelatedTaxa should return a List",relatedTaxa);
516 assertEquals("getRelatedTaxa should return all 23 related taxa",relatedTaxa.size(),23);
517 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate.isInitialized(relatedTaxa.get(0).getFromTaxon()));
518 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate.isInitialized(relatedTaxa.get(0).getFromTaxon().getName()));
519
520 assertEquals("Acherontia should appear first in the list of related taxa", relatedTaxa.get(0).getFromTaxon().getTitleCache(), "Acherontia Laspeyres, 1809 sec. cate-sphingidae.org");
521 assertEquals("Sphingonaepiopsis should appear last in the list of related taxa", relatedTaxa.get(22).getFromTaxon().getTitleCache(), "Sphinx Linnaeus, 1758 sec. cate-sphingidae.org");
522 }
523
524 @Test
525 @DataSet
526 public void testGetRelatedTaxaPaged() {
527 Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
528 assert taxon != null : "taxon must exist";
529
530 List<String> propertyPaths = new ArrayList<String>();
531 propertyPaths.add("fromTaxon");
532 propertyPaths.add("fromTaxon.name");
533
534 List<OrderHint> orderHints = new ArrayList<OrderHint>();
535 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
536
537 List<TaxonRelationship> firstPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), 10, 0, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
538 List<TaxonRelationship> secondPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),10, 1, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
539 List<TaxonRelationship> thirdPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), 10, 2, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
540
541 assertNotNull("getRelatedTaxa: 10, 0 should return a List",firstPage);
542 assertEquals("getRelatedTaxa: 10, 0 should return a List with 10 elements",10,firstPage.size());
543 assertNotNull("getRelatedTaxa: 10, 1 should return a List",secondPage);
544 assertEquals("getRelatedTaxa: 10, 1 should return a List with 10 elements",secondPage.size(),10);
545 assertNotNull("getRelatedTaxa: 10, 2 should return a List",thirdPage);
546 assertEquals("getRelatedTaxa: 10, 2 should return a List with 3 elements",thirdPage.size(),3);
547 }
548
549 @Test
550 @DataSet
551 public void testCountSynonymRelationships() {
552 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
553 assert taxon != null : "taxon must exist";
554
555 int numberOfSynonymRelationships = taxonDao.countSynonyms(taxon,null);
556 assertEquals("countSynonymRelationships should return 5",5,numberOfSynonymRelationships);
557 }
558
559 @Test
560 @DataSet
561 public void testSynonymRelationships() {
562 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
563 assert taxon != null : "taxon must exist";
564 List<String> propertyPaths = new ArrayList<String>();
565 propertyPaths.add("synonym");
566 propertyPaths.add("synonym.name");
567
568 List<OrderHint> orderHints = new ArrayList<OrderHint>();
569 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
570
571 List<SynonymRelationship> synonyms = taxonDao.getSynonyms(taxon, null, null, null,orderHints,propertyPaths);
572
573 assertNotNull("getSynonyms should return a List",synonyms);
574 assertEquals("getSynonyms should return 5 SynonymRelationship entities",synonyms.size(),5);
575 assertTrue("getSynonyms should return SynonymRelationship objects with the synonym initialized",Hibernate.isInitialized(synonyms.get(0).getSynonym()));
576 }
577
578 @Test
579 @DataSet
580 public void testCountSynonymRelationshipsByType() {
581 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
582 assert taxon != null : "taxon must exist";
583
584 int numberOfTaxonomicSynonyms = taxonDao.countSynonyms(taxon, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
585 assertEquals("countSynonyms should return 4",numberOfTaxonomicSynonyms, 4);
586 }
587
588 @Test
589 @DataSet
590 public void testSynonymRelationshipsByType() {
591 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
592 assert taxon != null : "taxon must exist";
593
594 List<SynonymRelationship> synonyms = taxonDao.getSynonyms(taxon, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF(), null, null,null,null);
595
596 assertNotNull("getSynonyms should return a List",synonyms);
597 assertEquals("getSynonyms should return 4 SynonymRelationship entities",synonyms.size(),4);
598 }
599
600 @Test
601 @DataSet
602 public void testPageSynonymRelationships(){
603 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
604 assert taxon != null : "taxon must exist";
605
606 List<SynonymRelationship> firstPage = taxonDao.getSynonyms(taxon, null, 4, 0,null,null);
607 List<SynonymRelationship> secondPage = taxonDao.getSynonyms(taxon, null, 4, 1,null,null);
608
609 assertNotNull("getSynonyms: 4, 0 should return a List",firstPage);
610 assertEquals("getSynonyms: 4, 0 should return 4 SynonymRelationships", 4,firstPage.size());
611 assertNotNull("getSynonyms: 4, 1 should return a List",secondPage);
612 assertEquals("getSynonyms: 4, 1 should return 1 SynonymRelationship", 1, secondPage.size());
613 }
614
615 @Test
616 @DataSet("TaxonNodeDaoHibernateImplTest.xml")
617 public void testListAcceptedTaxaFor() {
618 UUID acheontitia_ciprosus = UUID.fromString("3ef145f7-bd92-4a64-8afd-2b8203e00e02");
619
620 Synonym synonym = (Synonym)taxonDao.findByUuid(acheontitia_ciprosus);
621 assertNotNull("synonym must exist", synonym);
622
623 List<Taxon> list = taxonDao.listAcceptedTaxaFor(synonym, null, 4, 0, null, null);
624 assertNotNull("listAcceptedTaxaFor should return a List");
625 assertEquals("listAcceptedTaxaFor should return 2 Taxa", 2, list.size());
626
627 Classification classification = classificationDao.load(classificationUuid);
628 assertNotNull("classification must exist", classification);
629
630 list = taxonDao.listAcceptedTaxaFor(synonym, classification, 4, 0, null, null);
631 assertNotNull("listAcceptedTaxaFor should return a List");
632 assertEquals("listAcceptedTaxaFor should return 1 Taxa", 1, list.size());
633 }
634
635
636 @Test
637 @DataSet
638 public void testGetTaxonMatchingUninomial() {
639 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class, "Smerinthus", "*", "*", "*","*",null,null,null);
640
641 assertNotNull("findTaxaByName should return a List", result);
642 assertEquals("findTaxaByName should return two Taxa",2,result.size());
643 assertEquals("findTaxaByName should return a Taxon with id 5",5,result.get(0).getId());
644 }
645
646 @Test
647 @DataSet
648 public void testGetTaxonMatchingSpeciesBinomial() {
649 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class, "Smerinthus", null, "kindermannii", null,"*",null,null,null);
650
651 assertNotNull("findTaxaByName should return a List", result);
652 assertEquals("findTaxaByName should return one Taxon",1,result.size());
653 assertEquals("findTaxaByName should return a Taxon with id 8",8,result.get(0).getId());
654 }
655
656 @Test
657 @DataSet
658 public void testGetTaxonMatchingTrinomial() {
659 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Cryptocoryne", null,"purpurea","borneoensis","*",null,null,null);
660
661 assertNotNull("findTaxaByName should return a List", result);
662 assertEquals("findTaxaByName should return one Taxon",1,result.size());
663 assertEquals("findTaxaByName should return a Taxon with id 38",38,result.get(0).getId());
664 }
665
666 @Test
667 @DataSet
668 public void testNegativeMatch() {
669 List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Acherontia", null,"atropos","dehli",null,null,null,null);
670
671 assertNotNull("findTaxaByName should return a List", result);
672 assertTrue("findTaxaByName should return an empty List",result.isEmpty());
673 }
674
675 @Test
676 @DataSet
677 public void testCountAllTaxa() {
678 int numberOfTaxa = taxonDao.count(Taxon.class);
679 assertEquals("count should return 33 taxa",33, numberOfTaxa);
680 }
681
682 @Test
683 @DataSet
684 public void testListAllTaxa() {
685 List<Taxon> taxa = taxonDao.list(Taxon.class,100, 0);
686 assertNotNull("list should return a List",taxa);
687 assertEquals("list should return 33 taxa",33, taxa.size());
688 }
689
690 @Test
691 @DataSet
692 // @ExpectedDataSet
693 public void testDelete() {
694 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
695 assert taxon != null : "taxon must exist";
696 taxonDao.delete(taxon);
697 taxon = (Taxon)taxonDao.findByUuid(acherontia);
698 assert taxon == null : "taxon must not exist";
699 setComplete();
700 endTransaction();
701 // try {
702 // printDataSet(new FileOutputStream("test.xml"), TABLE_NAMES);
703 // } catch (FileNotFoundException e) {
704 // e.printStackTrace();
705 // }
706 }
707
708 @Test
709 @DataSet
710 // @ExpectedDataSet("TaxonDaoHibernateImplTest.testDelete-result.xml")
711 public void testDeleteWithMarker() {
712 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
713 taxon.addMarker(Marker.NewInstance(MarkerType.IS_DOUBTFUL(), true));
714 taxonDao.save(taxon);
715 assert taxon != null : "taxon must exist";
716
717 taxonDao.delete(taxon);
718 commitAndStartNewTransaction(null);
719 taxon = (Taxon)taxonDao.findByUuid(acherontia);
720 assert taxon == null : "taxon must not exist";
721 setComplete();
722 endTransaction();
723 // try {
724 // printDataSet(new FileOutputStream("test.xml"), TABLE_NAMES);
725 // } catch (FileNotFoundException e) {
726 // e.printStackTrace();
727 // }
728 }
729
730 @Test
731 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
732 public void testFindDeleted() {
733 TaxonBase<?> taxon = taxonDao.findByUuid(acherontia);
734 assertNull("findByUuid should return null in this view", taxon);
735 assertFalse("exist should return false in this view",taxonDao.exists(acherontia));
736 }
737
738 @Test
739 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
740 public void testFindDeletedInPreviousView() {
741 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
742 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
743 assertNotNull("findByUuid should return a taxon in this view",taxon);
744 assertTrue("exists should return true in this view", taxonDao.exists(acherontia));
745
746 try{
747 assertEquals("There should be 3 relations to this taxon in this view",3,taxon.getRelationsToThisTaxon().size());
748 } catch(Exception e) {
749 fail("We should not experience any problems initializing proxies with envers");
750 }
751 }
752
753 @Test
754 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
755 public void testGetAuditEvents() {
756 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
757 assert taxon != null : "taxon cannot be null";
758
759 List<String> propertyPaths = new ArrayList<String>();
760 propertyPaths.add("name");
761 propertyPaths.add("createdBy");
762 propertyPaths.add("updatedBy");
763
764 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(taxon, null,null,null,propertyPaths);
765 assertNotNull("getAuditEvents should return a list",auditEvents);
766 assertFalse("the list should not be empty",auditEvents.isEmpty());
767 assertEquals("There should be two AuditEventRecords in the list",2, auditEvents.size());
768 }
769
770 @Test
771 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
772 public void testGetAuditEventsFromNow() {
773 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
774 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
775 assert taxon != null : "taxon cannot be null";
776
777 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(taxon, null,null,AuditEventSort.FORWARDS,null);
778 assertNotNull("getAuditEvents should return a list",auditEvents);
779 assertFalse("the list should not be empty",auditEvents.isEmpty());
780 assertEquals("There should be one audit event in the list",1,auditEvents.size());
781 }
782
783 @Test
784 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
785 public void testCountAuditEvents() {
786 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
787 assert taxon != null : "taxon cannot be null";
788
789 int numberOfAuditEvents = taxonDao.countAuditEvents(taxon,null);
790 assertEquals("countAuditEvents should return 2",numberOfAuditEvents,2);
791 }
792
793 @Test
794 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
795 public void getPreviousAuditEvent() {
796 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
797 assert taxon != null : "taxon cannot be null";
798
799 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getPreviousAuditEvent(taxon);
800 assertNotNull("getPreviousAuditEvent should not return null as there is at least one audit event prior to the current one",auditEvent);
801 }
802
803 @Test
804 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
805 public void getPreviousAuditEventAtBeginning() {
806 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
807 TaxonBase taxon = taxonDao.findByUuid(sphingidae);
808 assert taxon != null : "taxon cannot be null";
809
810 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getPreviousAuditEvent(taxon);
811 assertNull("getPreviousAuditEvent should return null if we're at the first audit event anyway",auditEvent);
812 }
813
814 @Test
815 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
816 public void getNextAuditEvent() {
817 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
818 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
819 assert taxon != null : "taxon cannot be null";
820
821 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getNextAuditEvent(taxon);
822 assertNotNull("getNextAuditEvent should not return null as there is at least one audit event after the current one",auditEvent);
823 }
824
825 @Test
826 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
827 public void getNextAuditEventAtEnd() {
828 AuditEventContextHolder.getContext().setAuditEvent(mostRecentAuditEvent);
829 TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
830 assert taxon != null : "taxon cannot be null";
831
832 AuditEventRecord<TaxonBase> auditEvent = taxonDao.getNextAuditEvent(taxon);
833 assertNull("getNextAuditEvent should return null as there no more audit events after the current one",auditEvent);
834 }
835
836 @Test
837 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
838 public void testFind() {
839 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
840 assert taxon != null : "taxon cannot be null";
841
842 assertEquals("getRelationsToThisTaxon should contain 1 TaxonRelationship in this view",1,taxon.getRelationsToThisTaxon().size());
843 }
844
845 @Test
846 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
847 public void testFindInPreviousView() {
848 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
849 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
850 assert taxon != null : "taxon cannot be null";
851
852 assertTrue("getRelationsToThisTaxon should contain 0 TaxonRelationship in this view",taxon.getRelationsToThisTaxon().isEmpty());
853 }
854
855 @Test
856 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
857 public void testGetRelations() {
858 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
859 assert taxon != null : "taxon cannot be null";
860
861 List<String> propertyPaths = new ArrayList<String>();
862 propertyPaths.add("fromTaxon");
863 propertyPaths.add("fromTaxon.name");
864
865 List<OrderHint> orderHints = new ArrayList<OrderHint>();
866 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
867
868 List<TaxonRelationship> taxonRelations = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), null, null,orderHints,propertyPaths, TaxonRelationship.Direction.relatedFrom);
869 assertNotNull("getRelatedTaxa should return a list", taxonRelations);
870 assertEquals("there should be one TaxonRelationship in the list in the current view",1,taxonRelations.size());
871 assertTrue("TaxonRelationship.relatedFrom should be initialized",Hibernate.isInitialized(taxonRelations.get(0).getFromTaxon()));
872 assertTrue("TaxonRelationship.relatedFrom.name should be initialized",Hibernate.isInitialized(taxonRelations.get(0).getFromTaxon().getName()));
873 }
874
875 @Test
876 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
877 public void testCountRelations() {
878 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
879 assert taxon != null : "taxon cannot be null";
880 assertEquals("countRelatedTaxa should return 1 in the current view",1, taxonDao.countTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), TaxonRelationship.Direction.relatedTo));
881 }
882
883 @Test
884 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
885 public void testGetRelationsInPreviousView() {
886 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
887 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
888 assert taxon != null : "taxon cannot be null";
889
890 List<String> propertyPaths = new ArrayList<String>();
891 propertyPaths.add("relatedFrom");
892 propertyPaths.add("relatedFrom.name");
893
894 List<OrderHint> orderHints = new ArrayList<OrderHint>();
895 orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
896
897 List<TaxonRelationship> taxonRelations = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), null, null,orderHints,propertyPaths, TaxonRelationship.Direction.relatedFrom);
898 assertNotNull("getRelatedTaxa should return a list",taxonRelations);
899 assertTrue("there should be no TaxonRelationships in the list in the prior view",taxonRelations.isEmpty());
900 }
901
902 @Test
903 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
904 public void testCountRelationsInPreviousView() {
905 AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
906 Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
907 assert taxon != null : "taxon cannot be null";
908 assertEquals("countRelatedTaxa should return 0 in the current view",0, taxonDao.countTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(), TaxonRelationship.Direction.relatedTo));
909 }
910
911 @Test
912 @DataSet
913 public void testGroupTaxa() {
914 List<Grouping> groups = new ArrayList<Grouping>();
915 groups.add(new GroupByCount("count",SortOrder.DESCENDING));
916 groups.add(new Grouping("name.genusOrUninomial", "genus", "n", SortOrder.ASCENDING));
917 List<Object[]> results = taxonDao.group(null, null, null, groups,null);
918 System.out.println("count\tname.genuOrUninomial");
919 for(Object[] result : results) {
920 System.out.println(result[0] + "\t" + result[1]);
921 }
922 }
923
924 @Test
925 @DataSet
926 public void testGroupTaxaByClass() {
927 List<Grouping> groups = new ArrayList<Grouping>();
928 groups.add(new GroupByCount("count",SortOrder.DESCENDING));
929 groups.add(new Grouping("class", "class",null, SortOrder.ASCENDING));
930 List<Object[]> results = taxonDao.group(null, null, null, groups,null);
931 System.out.println("count\tclass");
932 for(Object[] result : results) {
933 System.out.println(result[0] + "\t" + result[1]);
934 }
935 }
936
937 @Test
938 @DataSet
939 public void testNativeSQLOrder() {
940 List<OrderHint> orderHints = new ArrayList<OrderHint>();
941 orderHints.add(new NativeSqlOrderHint("case when {alias}.titleCache like 'C%' then 0 else 1 end",SortOrder.ASCENDING));
942
943 List<TaxonBase> results = taxonDao.list(null, null, orderHints);
944 System.out.println("native SQL order");
945 for(TaxonBase<?> result : results) {
946 System.out.println(result.getTitleCache());
947 }
948 }
949
950 @Test
951 @DataSet
952 public void testGroupByDateTaxa() {
953 List<Grouping> groups = new ArrayList<Grouping>();
954 groups.add(new GroupByCount("count",null));
955 groups.add(new GroupByDate("created", "dateGroup", SortOrder.ASCENDING, GroupByDate.Resolution.MONTH));
956 List<Object[]> results = taxonDao.group(null, null, null, groups,null);
957 System.out.println("count\tyear\tmonth");
958 for(Object[] result : results) {
959 System.out.println(result[0] + "\t" + result[1] + "\t" + result[2]);
960 }
961 }
962
963 @Test
964 @DataSet ("TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
965 public final void testGetTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(){
966 Classification classification = classificationDao.findByUuid(classificationUuid);
967 List<UuidAndTitleCache<TaxonNode>> result = taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, null, null);
968 assertNotNull(result);
969 assertEquals(5, result.size());
970
971 //test exclude
972 UUID excludeUUID = UUID.fromString("a9f42927-e507-4fda-9629-62073a908aae");
973 List<UUID> excludeUUids = new ArrayList<UUID>();
974 excludeUUids.add(excludeUUID);
975 result = taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, null, null);
976 assertEquals(5, result.size());
977
978 //test limit
979 int limit = 2;
980 result = taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, limit, null);
981 assertEquals(2, result.size());
982
983 //test pattern
984 String pattern = "*Rothschi*";
985 result = taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, 2, pattern);
986 assertNotNull(result);
987 assertEquals(1, result.size());
988 assertEquals("0b5846e5-b8d2-4ca9-ac51-099286ea4adc", result.get(0).getUuid().toString());
989
990 }
991
992
993 @Test
994 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
995 //NOTE: There is a problem with loading AuditEvents if this test runs
996 //stand alone or as first (one of the first) in the test suite. For some reason
997 //the AuditEvent records from the @DataSet are not inserted into the database then,
998 //while those inserted by the terms dataset are inserted as well as a completely new one.
999 //This problem for some reason does not happen if not running at first place
1000 public void testGetAuditEventsByTypeWithRestrictions() {
1001 commitAndStartNewTransaction(new String[]{"AUDITEVENT", "TAXONBASE_AUD"});
1002
1003 List<String> propertyPaths = new ArrayList<String>();
1004 propertyPaths.add("name");
1005 propertyPaths.add("createdBy");
1006 propertyPaths.add("updatedBy");
1007
1008 List<AuditCriterion> criteria = new ArrayList<AuditCriterion>();
1009 criteria.add(AuditEntity.property("lsid_lsid").isNotNull());
1010
1011 int count = taxonDao.countAuditEvents(TaxonBase.class, null, null, null);
1012
1013 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(TaxonBase.class, previousAuditEvent, mostRecentAuditEvent, criteria,null, null, AuditEventSort.FORWARDS, propertyPaths);
1014 assertNotNull("getAuditEvents should return a list",auditEvents);
1015 assertFalse("the list should not be empty",auditEvents.isEmpty());
1016 assertEquals("There should be one AuditEventRecord in the list",1, auditEvents.size());
1017 }
1018
1019
1020 @Test
1021 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
1022 // @DataSets({ //for testing only
1023 // @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml"),
1024 // @DataSet("TaxonDaoHibernateImplTest.testFindDeletedAuditEvents.xml")
1025 // })
1026 //NOTE: There is a problem with loading AuditEvents if this test runs
1027 //stand alone or as first (one of the first) in the test suite. For some reason
1028 //the AuditEvent records from the @DataSet are not inserted into the database then,
1029 //while those inserted by the terms dataset are inserted as well as a completely new one.
1030 //This problem for some reason does not happen if not running at first place
1031 public void testGetAuditEventsByTypeWithNoRestrictions() {
1032 printDataSet(System.out, new String[]{"AUDITEVENT", "TAXONBASE_AUD"});
1033 commitAndStartNewTransaction(new String[]{"AUDITEVENT", "TAXONBASE_AUD"});
1034
1035 List<String> propertyPaths = new ArrayList<String>();
1036 propertyPaths.add("name");
1037 propertyPaths.add("createdBy");
1038 propertyPaths.add("updatedBy");
1039 int count = taxonDao.countAuditEvents(TaxonBase.class, null, null, null);
1040 List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(TaxonBase.class, previousAuditEvent, mostRecentAuditEvent, null,null, null, AuditEventSort.FORWARDS, propertyPaths);
1041 assertNotNull("getAuditEvents should return a list", auditEvents);
1042 assertFalse("the list should not be empty", auditEvents.isEmpty());
1043 assertEquals("There should be thirty eight AuditEventRecords in the list", 38, auditEvents.size());
1044 }
1045
1046
1047 @Test
1048 @DataSet("TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
1049 public void testGetCommonName(){
1050 List<Taxon> commonNameResults = taxonDao.getTaxaByCommonName("common%", null,
1051 MatchMode.BEGINNING, null, null, null, null);
1052
1053 assertNotNull("getTaxaByCommonName should return a list", commonNameResults);
1054 assertFalse("the list should not be empty", commonNameResults.isEmpty());
1055 assertEquals("There should be one Taxon with common name", 1,commonNameResults.size());
1056 assertEquals(" sec. ???", ((TaxonBase)commonNameResults.get(0)).getTitleCache());
1057 }
1058
1059 @Test
1060 @DataSet
1061 public void testGetTitleCache(){
1062 UUID uuid = UUID.fromString("7b8b5cb3-37ba-4dba-91ac-4c6ffd6ac331");
1063 String titleCache = taxonDao.getTitleCache(uuid, false);
1064 Assert.assertEquals("Acherontia styx Westwood, 1847 sec. cate-sphingidae.org", titleCache);
1065 titleCache = taxonDao.getTitleCache(uuid, true);
1066 Assert.assertEquals("Acherontia styxx Westwood, 1847 sec. cate-sphingidae.org", titleCache);
1067 }
1068
1069
1070 @Test
1071 @DataSet("TaxonDaoHibernateImplTest.testPropertyPath.xml")
1072 public void testPropertyPath(){
1073 //Test that BeanInitializer also works on HiberanteProxys
1074 Classification c = classificationDao.load(UUID.fromString("4bceea53-893f-4685-8c63-6dcec6e85ab1"));
1075 TaxonNode singleNode = c.getRootNode().getChildNodes().iterator().next();
1076 Taxon taxonProxy = singleNode.getTaxon();
1077 Assert.assertTrue("Object to test should be a proxy ", taxonProxy instanceof HibernateProxy);
1078
1079 List<String> propertyPaths = new ArrayList<String>();
1080 propertyPaths.add("taxonNodes");
1081 Taxon taxon = (Taxon)this.taxonDao.load(
1082 UUID.fromString("4a5bc930-844f-45ec-aea8-dd155e1ab25f"),
1083 propertyPaths);
1084 Assert.assertSame("Returned object should be the same proxy to assure that we ran initialization on this proxy", taxonProxy, taxon);
1085 }
1086
1087 /**
1088 * {@inheritDoc}
1089 */
1090 @Override
1091 // @Test
1092 public void createTestDataSet() throws FileNotFoundException {
1093 // Classification classification = Classification.NewInstance("Test");
1094 // BotanicalName taxonNameBase = null;
1095 // Reference sec = null;
1096 // Taxon taxon = Taxon.NewInstance(taxonNameBase, sec);
1097 // classification.addChildTaxon(taxon, sec, null);
1098 //
1099 // classificationDao.save(classification);
1100 // this.commitAndStartNewTransaction(null);
1101 //
1102 // writeDbUnitDataSetFile(new String[] {
1103 // "CLASSIFICATION", "TAXONNAMEBASE",
1104 // "REFERENCE","TAXONNODE",
1105 // "TAXONBASE","LANGUAGESTRING",
1106 // "HIBERNATE_SEQUENCES" // IMPORTANT!!!
1107 // },
1108 // "testPropertyPath" );
1109 }
1110
1111 }