2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.taxon
;
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
;
19 import java
.io
.FileNotFoundException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.HashSet
;
22 import java
.util
.List
;
24 import java
.util
.UUID
;
26 import org
.hibernate
.Hibernate
;
27 import org
.hibernate
.envers
.query
.AuditEntity
;
28 import org
.hibernate
.envers
.query
.criteria
.AuditCriterion
;
29 import org
.hibernate
.proxy
.HibernateProxy
;
30 import org
.junit
.After
;
31 import org
.junit
.Assert
;
32 import org
.junit
.Before
;
33 import org
.junit
.Test
;
34 import org
.unitils
.dbunit
.annotation
.DataSet
;
35 import org
.unitils
.dbunit
.annotation
.ExpectedDataSet
;
36 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
38 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
39 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
40 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
41 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
42 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
43 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymType
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
49 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
52 import eu
.etaxonomy
.cdm
.model
.view
.AuditEvent
;
53 import eu
.etaxonomy
.cdm
.model
.view
.AuditEventRecord
;
54 import eu
.etaxonomy
.cdm
.model
.view
.context
.AuditEventContextHolder
;
55 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.AuditEventSort
;
56 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IDefinedTermDao
;
57 import eu
.etaxonomy
.cdm
.persistence
.dao
.reference
.IReferenceDao
;
58 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.IClassificationDao
;
59 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonDao
;
60 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
61 import eu
.etaxonomy
.cdm
.persistence
.query
.GroupByCount
;
62 import eu
.etaxonomy
.cdm
.persistence
.query
.GroupByDate
;
63 import eu
.etaxonomy
.cdm
.persistence
.query
.Grouping
;
64 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
65 import eu
.etaxonomy
.cdm
.persistence
.query
.NativeSqlOrderHint
;
66 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
67 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
.SortOrder
;
68 import eu
.etaxonomy
.cdm
.test
.integration
.CdmTransactionalIntegrationTest
;
69 import eu
.etaxonomy
.cdm
.test
.unitils
.CleanSweepInsertLoadStrategy
;
75 public class TaxonDaoHibernateImplTest
extends CdmTransactionalIntegrationTest
{
78 private ITaxonDao taxonDao
;
81 private IClassificationDao classificationDao
;
84 private IReferenceDao referenceDao
;
87 IDefinedTermDao definedTermDao
;
90 private UUID sphingidae
;
91 private UUID acherontia
;
93 private UUID retheraSecCdmtest
;
94 private UUID atroposAgassiz
; // a Synonym
95 private UUID atroposOken
; // a Synonym
96 private UUID atroposLeach
; // a Synonym
97 private UUID acherontiaLachesis
;
100 private AuditEvent previousAuditEvent
;
101 private AuditEvent mostRecentAuditEvent
;
103 private UUID northernAmericaUuid
;
104 private UUID southernAmericaUuid
;
105 private UUID antarcticaUuid
;
107 private UUID classificationUuid
;
109 private boolean includeUnpublished
= true;
110 private boolean NO_UNPUBLISHED
= false;
112 private static final boolean doTaxa
= true;
113 private static final boolean noTaxa
= false;
114 private static final boolean doSynonyms
= true;
115 private static final boolean noSynonyms
= false;
116 private static final boolean doMisapplied
= true;
117 private static final boolean noMisapplied
= false;
118 private static final boolean doCommonNames
= true;
119 private static final boolean noCommonNames
= false;
123 @SuppressWarnings("unused")
124 private static final String
[] TABLE_NAMES
= new String
[] {
125 "HOMOTYPICALGROUP", "HOMOTYPICALGROUP_AUD", "REFERENCE", "REFERENCE_AUD", "TAXONBASE", "TAXONBASE_AUD"
126 , "TAXONNAME", "TAXONNAME_AUD", "TAXONRELATIONSHIP", "TAXONRELATIONSHIP_AUD" };
130 public void setUp() {
132 uuid
= UUID
.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2");
133 sphingidae
= UUID
.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9");
134 acherontia
= UUID
.fromString("c5cc8674-4242-49a4-aada-72d63194f5fa");
135 acherontiaLachesis
= UUID
.fromString("b04cc9cb-2b4a-4cc4-a94a-3c93a2158b06");
136 atroposAgassiz
= UUID
.fromString("d75b2e3d-7394-4ada-b6a5-93175b8751c1");
137 atroposOken
= UUID
.fromString("6bfedf25-6dbc-4d5c-9d56-84f9052f3b2a");
138 atroposLeach
= UUID
.fromString("3da4ab34-6c50-4586-801e-732615899b07");
139 rethera
= UUID
.fromString("a9f42927-e507-4fda-9629-62073a908aae");
140 retheraSecCdmtest
= UUID
.fromString("a9f42927-e507-4fda-9629-62073a908aae");
141 aus
= UUID
.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2");
143 previousAuditEvent
= new AuditEvent();
144 previousAuditEvent
.setRevisionNumber(1025);
145 previousAuditEvent
.setUuid(UUID
.fromString("a680fab4-365e-4765-b49e-768f2ee30cda"));
146 mostRecentAuditEvent
= new AuditEvent();
147 mostRecentAuditEvent
.setRevisionNumber(1026);
148 mostRecentAuditEvent
.setUuid(UUID
.fromString("afe8e761-8545-497b-9134-6a6791fc0b0d"));
149 AuditEventContextHolder
.clearContext(); // By default we're in the current view (i.e. view == null)
151 northernAmericaUuid
= UUID
.fromString("2757e726-d897-4546-93bd-7951d203bf6f");
152 southernAmericaUuid
= UUID
.fromString("6310b3ba-96f4-4855-bb5b-326e7af188ea");
153 antarcticaUuid
= UUID
.fromString("791b3aa0-54dd-4bed-9b68-56b4680aad0c");
155 classificationUuid
= UUID
.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
156 includeUnpublished
= true;
160 public void tearDown() {
161 AuditEventContextHolder
.clearContext();
165 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#TaxonDaoHibernateImpl()}.
169 public void testInit() {
170 logger
.warn("testInit()");
171 assertNotNull("Instance of ITaxonDao expected",taxonDao
);
172 assertNotNull("Instance of IReferenceDao expected",referenceDao
);
177 public void testGetTaxaByName() {
179 includeUnpublished
= true;
180 boolean accepted
= true;
182 Reference sec
= referenceDao
.findById(1);
183 assert sec
!= null : "sec must exist";
185 @SuppressWarnings("rawtypes")
186 List
<TaxonBase
> results
= taxonDao
.getTaxaByName("Aus", includeUnpublished
, sec
);
187 assertNotNull("getTaxaByName should return a List", results
);
188 //assertFalse("The list should not be empty", results.isEmpty());
189 assertEquals(1, results
.size());
191 results
= taxonDao
.getTaxaByName("A*", MatchMode
.BEGINNING
, accepted
, includeUnpublished
, null, null);
192 assertNotNull("getTaxaByName should return a List", results
);
194 int numberOfTaxaByName_A
= 5;
196 //logger.setLevel(Level.DEBUG); //FIXME #######################
197 if (logger
.isDebugEnabled()) {
198 for (int i
= 0; i
< results
.size(); i
++) {
199 String nameCache
= "";
200 TaxonName taxonName
= results
.get(i
).getName();
201 nameCache
= taxonName
.getNameCache();
202 logger
.debug(results
.get(i
).getClass() + "(" + i
+")" +
203 ": Name Cache = " + nameCache
+ ", Title Cache = " + results
.get(i
).getTitleCache());
207 assertEquals(numberOfTaxaByName_A
, results
.size());
209 includeUnpublished
= false;
210 results
= taxonDao
.getTaxaByName("A*", MatchMode
.BEGINNING
, accepted
, includeUnpublished
, null, null);
211 assertEquals(numberOfTaxaByName_A
, results
.size()); // no unpublished yet
214 //System.err.println("Species group: " + Rank.SPECIESGROUP().getId() + "Species: " + Rank.SPECIES().getId() + "Section Botany: "+ Rank.SECTION_BOTANY());
216 // assertEquals(results.get(0).getTitleCache(), "Abies sec. ???");
217 // assertEquals(results.get(1).getTitleCache(), "Abies Mill.");
218 // assertEquals(results.get(2).getTitleCache(), "Abies mill. sec. ???");
219 // assertEquals(results.get(3).getTitleCache(), "Abies alba sec. ???");
220 // assertEquals(results.get(4).getTitleCache(), "Abies alba Michx. sec. ???");
221 // assertEquals(results.get(5).getTitleCache(), "Abies alba Mill. sec. ???");
223 includeUnpublished
= true;
224 results
= taxonDao
.getTaxaByName("A*", MatchMode
.BEGINNING
, accepted
, includeUnpublished
, null, null);
225 assertNotNull("getTaxaByName should return a List", results
);
226 assertEquals(numberOfTaxaByName_A
, results
.size());
228 results
= taxonDao
.getTaxaByName("Aus", MatchMode
.EXACT
, accepted
, includeUnpublished
, null, null);
229 assertNotNull("getTaxaByName should return a List", results
);
230 assertEquals("Results list should contain one entity",1,results
.size());
235 @DataSet (loadStrategy
=CleanSweepInsertLoadStrategy
.class, value
="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
236 public void testGetTaxaByNameWithMisappliedNames(){
238 Classification classification
= classificationDao
.load(classificationUuid
);
241 * The testdata contains 3 misapplied names (1. nameCache = Aus, 2. nameCache = Rethera, 3. nameCache = Daphnis),
242 * two contained in the classification used in this test,
243 * the other one is not contained in any classification. This later case is the more general situation.
244 * Misapplied names should be found regardless of whether they are contained in a classification or not.
246 //two accepted taxa starting with R in classification "TestBaum"
247 @SuppressWarnings("rawtypes")
248 List
<TaxonBase
> results
= taxonDao
.getTaxaByName(doTaxa
, noSynonyms
, noMisapplied
, noCommonNames
, false, "R*", classification
, MatchMode
.BEGINNING
,
249 null, includeUnpublished
, null, null, null, null);
250 Assert
.assertEquals("There should be 2 Taxa", 2, results
.size());
252 //three taxa, 2 accepted and 1 misapplied name starting with R
253 results
= taxonDao
.getTaxaByName(doTaxa
, noSynonyms
, doMisapplied
, noCommonNames
, false, "R*", null, MatchMode
.BEGINNING
,
254 null, includeUnpublished
, null, null, null, null);
255 Assert
.assertEquals("There should be 3 Taxa", 3, results
.size());
257 //one synonym has no accepted taxon
258 results
= taxonDao
.getTaxaByName(doTaxa
, doSynonyms
, doMisapplied
, noCommonNames
, false, "A*", null, MatchMode
.BEGINNING
,
259 null, includeUnpublished
, null, null, null, null);
260 Assert
.assertEquals("There should be 11 Taxa",11, results
.size());
262 //two accepted taxa in classification and 1 misapplied name with accepted name in classification
263 results
= taxonDao
.getTaxaByName(doTaxa
, doSynonyms
, doMisapplied
, noCommonNames
, false, "R*", classification
, MatchMode
.BEGINNING
,
264 null, includeUnpublished
, null, null, null, null);
265 Assert
.assertEquals("There should be 3 Taxa", 3, results
.size());
266 //same with unpublished
267 includeUnpublished
= false;
268 results
= taxonDao
.getTaxaByName(doTaxa
, doSynonyms
, doMisapplied
, noCommonNames
, false, "R*", classification
, MatchMode
.BEGINNING
,
269 null, includeUnpublished
, null, null, null, null);
270 Assert
.assertEquals("There should be 3 Taxa", 3, results
.size());
271 includeUnpublished
= true;
274 //same as above because all taxa, synonyms and misapplied names starting with R are in the classification
275 results
= taxonDao
.getTaxaByName(doTaxa
, doSynonyms
, doMisapplied
, noCommonNames
, false, "R*", null, MatchMode
.BEGINNING
,
276 null, includeUnpublished
, null, null, null, null);
277 Assert
.assertEquals("There should be 3 Taxa", 3, results
.size());
279 //find misapplied names with accepted taxon in the classification, the accepted taxa of two misapplied names are in the classification
280 results
= taxonDao
.getTaxaByName(noTaxa
, noSynonyms
, doMisapplied
, noCommonNames
, false, "*", classification
, MatchMode
.BEGINNING
,
281 null, includeUnpublished
, null, null, null, null);
282 Assert
.assertEquals("There should be 2 Taxa", 2, results
.size());
284 //find misapplied names beginning with R
285 results
= taxonDao
.getTaxaByName(noTaxa
, noSynonyms
, doMisapplied
, noCommonNames
, false, "R*", null, MatchMode
.BEGINNING
,
286 null, includeUnpublished
, null, null, null, null);
287 Assert
.assertEquals("There should be 1 Taxa", 1, results
.size());
289 //find all three misapplied names
290 results
= taxonDao
.getTaxaByName(noTaxa
, noSynonyms
, doMisapplied
, noCommonNames
, false, "*", null, MatchMode
.BEGINNING
,
291 null, includeUnpublished
, null, null, null, null);
292 Assert
.assertEquals("There should be 3 Taxa", 3, results
.size());
296 * Test for all not covered possibilities of searches
299 @DataSet (loadStrategy
=CleanSweepInsertLoadStrategy
.class, value
="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
300 public void testGetTaxaByNameVariants(){
301 @SuppressWarnings("rawtypes")
302 List
<TaxonBase
> results
= taxonDao
.getTaxaByName(noTaxa
, noSynonyms
, noMisapplied
, doCommonNames
, false, "c*", null, MatchMode
.BEGINNING
,
303 null, includeUnpublished
, null, null, null, null);
304 Assert
.assertEquals("There should be 2 Taxa",2, results
.size());
306 results
= taxonDao
.getTaxaByName(noTaxa
, noSynonyms
, doMisapplied
, doCommonNames
, false, "R*", null, MatchMode
.BEGINNING
,
307 null, includeUnpublished
, null, null, null, null);
308 Assert
.assertEquals("There should be 1 Taxa", 1, results
.size());
310 results
= taxonDao
.getTaxaByName(noTaxa
, doSynonyms
, doMisapplied
, doCommonNames
, false, "R*", null, MatchMode
.BEGINNING
,
311 null, includeUnpublished
, null, null, null, null);
312 Assert
.assertEquals("There should be 1 Taxa", 1, results
.size());
314 results
= taxonDao
.getTaxaByName(noTaxa
, doSynonyms
, noMisapplied
, doCommonNames
, false, "c*", null, MatchMode
.BEGINNING
,
315 null, includeUnpublished
, null, null, null, null);
316 Assert
.assertEquals("There should be 2 Taxa", 2, results
.size());
318 results
= taxonDao
.getTaxaByName(doTaxa
, noSynonyms
, noMisapplied
, doCommonNames
, false, "c*", null, MatchMode
.BEGINNING
,
319 null, includeUnpublished
, null, null, null, null);
320 Assert
.assertEquals("There should be 2 Taxa", 2, results
.size());
322 Classification classification
= classificationDao
.load(classificationUuid
);
323 results
= taxonDao
.getTaxaByName(noTaxa
, noSynonyms
, noMisapplied
, doCommonNames
, false, "c*", classification
, MatchMode
.BEGINNING
,
324 null, includeUnpublished
, null, null, null, null);
325 Assert
.assertEquals("There should be 1 Taxa", 1, results
.size());
327 Set
<NamedArea
> namedAreas
= new HashSet
<>();
328 namedAreas
.add((NamedArea
)definedTermDao
.load(southernAmericaUuid
));
329 results
= taxonDao
.getTaxaByName(noTaxa
, noSynonyms
, noMisapplied
, doCommonNames
, false, "c*", null, MatchMode
.BEGINNING
,
330 namedAreas
, includeUnpublished
, null, null, null, null);
331 Assert
.assertEquals("There should be 1 Taxa", 1, results
.size());
335 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}.
339 public void testGetTaxaByNameForEditor() {
340 Reference sec
= referenceDao
.findById(1);
341 assert sec
!= null : "sec must exist";
343 @SuppressWarnings("rawtypes")
344 List
<UuidAndTitleCache
<?
extends IdentifiableEntity
>> results
= taxonDao
.getTaxaByNameForEditor(
345 doTaxa
, doSynonyms
, noMisapplied
, noCommonNames
, false, includeUnpublished
, "Acher", null, MatchMode
.BEGINNING
, null, null);
346 assertNotNull("getTaxaByName should return a List", results
);
347 assertFalse("The list should not be empty", results
.isEmpty());
348 assertEquals(4, results
.size());
351 results
= taxonDao
.getTaxaByNameForEditor(doTaxa
, doSynonyms
, noMisapplied
, noCommonNames
, false,includeUnpublished
, "A",null, MatchMode
.BEGINNING
, null, null);
352 assertNotNull("getTaxaByName should return a List", results
);
353 assertEquals(7, results
.size());
356 results
= taxonDao
.getTaxaByNameForEditor(doTaxa
, noSynonyms
, noMisapplied
, noCommonNames
, false, includeUnpublished
, "A", null, MatchMode
.BEGINNING
, null, null);
357 assertNotNull("getTaxaByName should return a List", results
);
358 assertEquals(5, results
.size());
359 assertEquals(results
.get(0).getType(), Taxon
.class);
361 results
= taxonDao
.getTaxaByNameForEditor(noTaxa
, doSynonyms
, noMisapplied
, noCommonNames
, false, includeUnpublished
, "A", null, MatchMode
.BEGINNING
, null, null);
362 assertNotNull("getTaxaByName should return a List", results
);
363 assertEquals(2, results
.size());
364 assertEquals(results
.get(0).getType(), Synonym
.class);
366 results
= taxonDao
.getTaxaByNameForEditor(doTaxa
, doSynonyms
, noMisapplied
, noCommonNames
, false,includeUnpublished
, "Aus", null, MatchMode
.EXACT
, null, null);
367 assertNotNull("getTaxaByName should return a List", results
);
368 assertEquals("Results list should contain one entity",1,results
.size());
370 results
= taxonDao
.getTaxaByNameForEditor(doTaxa
, doSynonyms
, doMisapplied
, noCommonNames
, false, includeUnpublished
, "A", null, MatchMode
.BEGINNING
, null, null);
371 assertNotNull("getTaxaByName should return a List", results
);
372 assertEquals("Results list should contain one entity", 8, results
.size());
374 //TODO: test the search for misapplied names
380 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}
381 * restricting the search by a set of Areas.
384 @DataSet(loadStrategy
=CleanSweepInsertLoadStrategy
.class, value
="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
385 public void testGetTaxaByNameAndArea() {
387 Set
<NamedArea
> namedAreas
= new HashSet
<>();
388 namedAreas
.add((NamedArea
)definedTermDao
.load(northernAmericaUuid
));
389 //namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
390 //namedAreas.add((NamedArea)definedTermDao.load(antarcticaUuid));
392 Classification taxonmicTree
= classificationDao
.findByUuid(classificationUuid
);
394 // prepare some synonym relation ships for some tests
395 Synonym synAtroposAgassiz
= (Synonym
)taxonDao
.findByUuid(atroposAgassiz
);
396 Taxon taxonRethera
= (Taxon
)taxonDao
.findByUuid(rethera
);
397 taxonRethera
.addSynonym(synAtroposAgassiz
, SynonymType
.SYNONYM_OF());
398 // logger.warn("addSynonym(..)");
400 Synonym synAtroposLeach
= (Synonym
)taxonDao
.findByUuid(atroposLeach
);
401 Taxon taxonRetheraSecCdmTest
= (Taxon
)taxonDao
.findByUuid(retheraSecCdmtest
);
402 taxonRetheraSecCdmTest
.addSynonym(synAtroposLeach
, SynonymType
.SYNONYM_OF());
403 this.taxonDao
.save(taxonRetheraSecCdmTest
);
405 Taxon test
= (Taxon
)this.taxonDao
.findByUuid(retheraSecCdmtest
);
406 // Set<Synonym> synonyms3 = test.getSynonyms();
407 // 1. searching for a taxon (Rethera)
408 //long numberOfTaxa = taxonDao.countTaxaByName(Taxon.class, "Rethera", null, MatchMode.BEGINNING, namedAreas);
410 @SuppressWarnings("rawtypes")
411 List
<TaxonBase
> results
= taxonDao
.getTaxaByName(doTaxa
, noSynonyms
, noMisapplied
, noCommonNames
, false, "Rethera", null, MatchMode
.BEGINNING
, namedAreas
,
412 includeUnpublished
, null, null, null, null);
413 assertNotNull("getTaxaByName should return a List", results
);
414 assertTrue("expected to find two taxa but found "+results
.size(), results
.size() == 2);
416 results
= taxonDao
.getTaxaByName(noTaxa
, noSynonyms
, noMisapplied
, doCommonNames
, false, "com*", null, MatchMode
.BEGINNING
, namedAreas
,
417 includeUnpublished
, null, null, null, null);
418 assertNotNull("getTaxaByName should return a List", results
);
419 assertTrue("expected to find one taxon but found "+results
.size(), results
.size() == 1);
421 // 2. searching for a taxon (Rethera) contained in a specific classification
422 results
= taxonDao
.getTaxaByName(doTaxa
, noSynonyms
, noMisapplied
, noCommonNames
, false, "Rethera", taxonmicTree
, MatchMode
.BEGINNING
, namedAreas
,
423 includeUnpublished
, null, null, null, null);
424 assertNotNull("getTaxaByName should return a List", results
);
425 assertTrue("expected to find one taxon but found "+results
.size(), results
.size() == 1);
428 // 3. searching for Synonyms
429 results
= taxonDao
.getTaxaByName(noTaxa
, doSynonyms
, noMisapplied
, noCommonNames
, false, "Atropo", null, MatchMode
.ANYWHERE
, null,
430 includeUnpublished
, null, null, null, null);
431 assertNotNull("getTaxaByName should return a List", results
);
432 /*System.err.println(results.get(0).getTitleCache() + " - " +results.get(1).getTitleCache() + " - " +results.get(2).getTitleCache() );
435 System.err.println(((Synonym)results.get(0)).getAcceptedTaxa().contains(taxonRethera)+ " - " +((Synonym)results.get(1)).getAcceptedTaxa().contains(taxonRethera)+ " - " +((Synonym)results.get(2)).getAcceptedTaxa().contains(taxonRethera)+ " - " );
437 assertTrue("expected to find three taxa but found "+results
.size(), results
.size() == 3);
439 // 4. searching for Synonyms
440 results
= taxonDao
.getTaxaByName(noTaxa
, doSynonyms
, noMisapplied
, noCommonNames
, false,"Atropo", null, MatchMode
.BEGINNING
, null,
441 includeUnpublished
, null, null, null, null);
442 assertNotNull("getTaxaByName should return a List", results
);
443 assertTrue("expected to find three taxa but found "+results
.size(), results
.size() == 3);
446 // 5. searching for a Synonyms and Taxa
447 results
= taxonDao
.getTaxaByName(doTaxa
, doSynonyms
, noMisapplied
, noCommonNames
, false,"A", null, MatchMode
.BEGINNING
, namedAreas
,
448 includeUnpublished
, null, null, null, null);
449 //only five taxa have a distribution
450 assertNotNull("getTaxaByName should return a List", results
);
451 assertTrue("expected to find 8 taxa but found "+results
.size(), results
.size() == 8);
456 * 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)}
457 * restricting the search by a set of Areas.
460 @DataSet(loadStrategy
=CleanSweepInsertLoadStrategy
.class, value
="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
461 public void testFindByNameTitleCache() {
463 Set
<NamedArea
> namedAreas
= new HashSet
<>();
464 namedAreas
.add((NamedArea
)definedTermDao
.load(northernAmericaUuid
));
465 //namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
466 //namedAreas.add((NamedArea)definedTermDao.load(antarcticaUuid));
468 Classification classification
= classificationDao
.findByUuid(classificationUuid
);
470 // prepare some synonym relation ships for some tests
471 Synonym synAtroposAgassiz
= (Synonym
)taxonDao
.findByUuid(atroposAgassiz
);
472 Taxon taxonRethera
= (Taxon
)taxonDao
.findByUuid(rethera
);
473 taxonRethera
.addSynonym(synAtroposAgassiz
, SynonymType
.SYNONYM_OF());
474 logger
.warn("addSynonym(..)");
475 this.taxonDao
.clear();
476 Synonym synAtroposLeach
= (Synonym
)taxonDao
.findByUuid(atroposLeach
);
477 Taxon taxonRetheraSecCdmtest
= (Taxon
)taxonDao
.findByUuid(retheraSecCdmtest
);
478 taxonRetheraSecCdmtest
.addSynonym(synAtroposLeach
, SynonymType
.SYNONYM_OF());
479 this.taxonDao
.clear();
480 // 1. searching for a taxon (Rethera)
481 //long numberOfTaxa = taxonDao.countTaxaByName(Taxon.class, "Rethera", null, MatchMode.BEGINNING, namedAreas);
483 @SuppressWarnings("rawtypes")
484 List
<TaxonBase
> results
= taxonDao
.findByNameTitleCache(true, false, includeUnpublished
, "Rethera Rothschild & Jordan, 1903", null, MatchMode
.EXACT
, namedAreas
,
485 null, null, null, null);
486 assertNotNull("getTaxaByName should return a List", results
);
487 assertTrue("expected to find two taxa but found "+results
.size(), results
.size() == 2);
489 // 2. searching for a taxon (Rethera) contained in a specific classification
490 results
= taxonDao
.findByNameTitleCache(true, false, includeUnpublished
, "Rethera Rothschild & Jordan, 1903", classification
, MatchMode
.EXACT
, namedAreas
,
491 null, null, null, null);
492 assertNotNull("getTaxaByName should return a List", results
);
493 assertTrue("expected to find one taxon but found "+results
.size(), results
.size() == 1);
496 // 3. searching for Synonyms
497 results
= taxonDao
.findByNameTitleCache(false, true, includeUnpublished
, "*Atropo", null, MatchMode
.ANYWHERE
, null,
498 null, null, null, null);
499 assertNotNull("getTaxaByName should return a List", results
);
500 assertTrue("expected to find two taxa but found "+results
.size(), results
.size() == 2);
502 // 4. searching for Synonyms
503 results
= taxonDao
.findByNameTitleCache(false, true, includeUnpublished
, "Atropo", null, MatchMode
.BEGINNING
, null,
504 null, null, null, null);
505 assertNotNull("getTaxaByName should return a List", results
);
506 assertTrue("expected to find two taxa but found "+results
.size(), results
.size() == 2);
509 // 5. searching for a Synonyms and Taxa
510 // attache a synonym first
511 Synonym syn
= (Synonym
)taxonDao
.findByUuid(this.atroposLeach
);
512 Taxon tax
= (Taxon
) taxonDao
.findByUuid(rethera
);
513 tax
.addSynonym(syn
, SynonymType
.HETEROTYPIC_SYNONYM_OF());
516 results
= taxonDao
.findByNameTitleCache(true, true, includeUnpublished
, "A", null, MatchMode
.BEGINNING
, namedAreas
,
517 null, null, null, null);
518 assertNotNull("getTaxaByName should return a List", results
);
519 assertTrue("expected to find 8 taxa but found "+results
.size(), results
.size() == 8);
523 @DataSet(loadStrategy
=CleanSweepInsertLoadStrategy
.class, value
="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
524 public void testTaxonNameInTwoClassifications(){
525 List
<String
> propertyPaths
= new ArrayList
<>();
526 propertyPaths
.add("taxonNodes");
527 @SuppressWarnings("rawtypes")
528 List
<TaxonBase
> taxa
= taxonDao
.getTaxaByName(doTaxa
, doSynonyms
, noMisapplied
, noCommonNames
, false,"P", null, MatchMode
.BEGINNING
,
529 null, includeUnpublished
, null, null, null, null);
530 Taxon taxon
= (Taxon
)taxa
.get(0);
531 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
532 assertTrue(nodes
.size() == 1);
533 //assertNotNull(taxa);
534 //assertTrue(taxa.size() > 0);
538 @DataSet(loadStrategy
=CleanSweepInsertLoadStrategy
.class, value
="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
539 public void testGetTaxaByNameProParteSynonym(){
541 @SuppressWarnings("rawtypes")
542 List
<TaxonBase
> taxa
= taxonDao
.getTaxaByName(noTaxa
, doSynonyms
, noMisapplied
, noCommonNames
, false, "A", null,
543 MatchMode
.BEGINNING
, null, includeUnpublished
, null, null, null, null);
544 Assert
.assertEquals("2 synonyms and 1 pro parte synonym should be returned.", 3, taxa
.size());
545 assertTrue("Pro parte should exist", existsInCollection(taxa
, acherontiaLachesis
));
546 assertTrue("Normal synonym should exist", existsInCollection(taxa
, atroposAgassiz
));
547 assertTrue("2. normal synonym should exist", existsInCollection(taxa
, atroposOken
));
548 //TODO shouldn't we also find orphaned synonyms (without accepted taxon) like Atropos Leach?
550 taxa
= taxonDao
.getTaxaByName(noTaxa
, doSynonyms
, noMisapplied
, noCommonNames
, false, "A", null,
551 MatchMode
.BEGINNING
, null, NO_UNPUBLISHED
, null, null, null, null);
552 Assert
.assertEquals("2 synonyms and no pro parte synonym should be returned.", 2, taxa
.size());
553 assertTrue("Normal synonym should exist", existsInCollection(taxa
, atroposAgassiz
));
554 assertTrue("2. normal synonym should exist", existsInCollection(taxa
, atroposOken
));
556 taxa
= taxonDao
.getTaxaByName(noTaxa
, noSynonyms
, doMisapplied
, noCommonNames
, false, "A", null,
557 MatchMode
.BEGINNING
, null, includeUnpublished
, null, null, null, null);
558 Assert
.assertEquals("1 misapplied name, no pro parte synonym should be returned.", 1, taxa
.size());
559 assertTrue("Pro parte should exist", existsInCollection(taxa
, aus
));
565 public void testFindByUuid() {
566 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(uuid
);
567 assertNotNull("findByUuid should return a taxon", taxon
);
568 assertFalse("findByUuid should not return a taxon with it's name initialized",Hibernate
.isInitialized(taxon
.getName()));
573 public void testLoad() {
574 List
<String
> propertyPaths
= new ArrayList
<>();
575 propertyPaths
.add("name");
576 propertyPaths
.add("sec");
577 Taxon taxon
= (Taxon
)taxonDao
.load(uuid
, propertyPaths
);
578 assertNotNull("findByUuid should return a taxon",taxon
);
579 assertTrue("load should return a taxon with it's name initialized, given that the property was specified in the method",Hibernate
.isInitialized(taxon
.getName()));
580 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()));
585 public void testCountTaxonRelationshipsByTaxon() {
586 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(sphingidae
);
587 assert taxon
!= null : "taxon must exist";
589 long numberOfRelatedTaxa
= taxonDao
.countTaxonRelationships(taxon
, TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(),
590 includeUnpublished
, TaxonRelationship
.Direction
.relatedTo
);
591 assertEquals("countTaxonRelationships should return 8", 8, numberOfRelatedTaxa
);
596 public void testCountTaxaByName() {
597 long numberOfTaxa
= taxonDao
.countTaxaByName(true, false, false, false,false, "A", null, MatchMode
.BEGINNING
, null, includeUnpublished
);
598 assertEquals(5, numberOfTaxa
);
599 numberOfTaxa
= taxonDao
.countTaxaByName(true, false, false, false, false,"Smerinthus kindermannii", null, MatchMode
.EXACT
, null, includeUnpublished
);
600 assertEquals(1, numberOfTaxa
);
601 numberOfTaxa
= taxonDao
.countTaxaByName(false, true, false, false, false,"A", null, MatchMode
.BEGINNING
, null, includeUnpublished
);
602 assertEquals(2, numberOfTaxa
);
603 numberOfTaxa
= taxonDao
.countTaxaByName(true, true, false, false, false,"A", null, MatchMode
.BEGINNING
, null, includeUnpublished
);
604 assertEquals(7, numberOfTaxa
);
605 numberOfTaxa
= taxonDao
.countTaxaByName(true, true, false, false,false, "Aasfwerfwf fffe", null, MatchMode
.BEGINNING
, null, includeUnpublished
);
606 assertEquals(0, numberOfTaxa
);
607 // FIXME implement test for search in specific classification
608 // Reference reference = referenceDao.findByUuid(UUID.fromString("596b1325-be50-4b0a-9aa2-3ecd610215f2"));
609 // numberOfTaxa = taxonDao.countTaxaByName("A*", MatchMode.BEGINNING, SelectMode.ALL, null, null);
610 // assertEquals(numberOfTaxa, 2);
615 public void testRelatedTaxa() {
616 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(sphingidae
);
617 assert taxon
!= null : "taxon must exist";
619 List
<String
> propertyPaths
= new ArrayList
<>();
620 propertyPaths
.add("fromTaxon");
621 propertyPaths
.add("fromTaxon.name");
622 List
<OrderHint
> orderHints
= new ArrayList
<>();
623 orderHints
.add(new OrderHint("relatedFrom.name.genusOrUninomial", SortOrder
.ASCENDING
));
624 orderHints
.add(new OrderHint("relatedFrom.name.specificEpithet", SortOrder
.ASCENDING
));
625 orderHints
.add(new OrderHint("relatedFrom.name.infraSpecificEpithet", SortOrder
.ASCENDING
));
627 long count
= taxonDao
.countTaxonRelationships(taxon
, TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(),
628 includeUnpublished
, TaxonRelationship
.Direction
.relatedTo
);
629 assertEquals("Count should return 8 (related taxa)", 8, count
);
631 List
<TaxonRelationship
> relatedTaxa
= taxonDao
.getTaxonRelationships(taxon
, TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(),
632 includeUnpublished
, null, null, orderHints
,propertyPaths
, TaxonRelationship
.Direction
.relatedTo
);
633 assertNotNull("getRelatedTaxa should return a List",relatedTaxa
);
634 assertEquals("getRelatedTaxa should return all 8 related taxa", 8, relatedTaxa
.size());
635 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate
.isInitialized(relatedTaxa
.get(0).getFromTaxon()));
636 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate
.isInitialized(relatedTaxa
.get(0).getFromTaxon().getName()));
638 assertEquals("Acherontia should appear first in the list of related taxa", relatedTaxa
.get(0).getFromTaxon().getTitleCache(), "Acherontia Laspeyres, 1809 sec. cate-sphingidae.org");
639 assertEquals("Sphingonaepiopsis should appear last in the list of related taxa", "Sphingonaepiopsis Wallengren, 1858 sec. cate-sphingidae.org", relatedTaxa
.get(relatedTaxa
.size()-1).getFromTaxon().getTitleCache());
644 public void testGetRelatedTaxaPaged() {
645 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(sphingidae
);
646 assert taxon
!= null : "taxon must exist";
648 List
<String
> propertyPaths
= new ArrayList
<>();
649 propertyPaths
.add("fromTaxon");
650 propertyPaths
.add("fromTaxon.name");
652 List
<OrderHint
> orderHints
= new ArrayList
<>();
653 orderHints
.add(new OrderHint("relatedFrom.titleCache", SortOrder
.ASCENDING
));
656 List
<TaxonRelationship
> firstPage
= taxonDao
.getTaxonRelationships(taxon
,TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(),
657 includeUnpublished
, pageSize
, 0, orderHints
,propertyPaths
, TaxonRelationship
.Direction
.relatedTo
);
658 List
<TaxonRelationship
> secondPage
= taxonDao
.getTaxonRelationships(taxon
,TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(),
659 includeUnpublished
, pageSize
, 1, orderHints
,propertyPaths
, TaxonRelationship
.Direction
.relatedTo
);
660 List
<TaxonRelationship
> thirdPage
= taxonDao
.getTaxonRelationships(taxon
,TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(),
661 includeUnpublished
, pageSize
, 2, orderHints
,propertyPaths
, TaxonRelationship
.Direction
.relatedTo
);
663 assertNotNull("getRelatedTaxa: 3, 0 should return a List",firstPage
);
664 assertEquals("getRelatedTaxa: 3, 0 should return a List with 3 elements", pageSize
,firstPage
.size());
665 assertNotNull("getRelatedTaxa: 3, 1 should return a List",secondPage
);
666 assertEquals("getRelatedTaxa: 3, 1 should return a List with 3 elements", pageSize
, secondPage
.size());
667 assertNotNull("getRelatedTaxa: 3, 2 should return a List",thirdPage
);
668 assertEquals("getRelatedTaxa: 3, 2 should return a List with 2 elements", 2, thirdPage
.size());
673 public void testCountSynonyms() {
674 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
675 assert taxon
!= null : "taxon must exist";
677 long numberOfRelatedSynonym
= taxonDao
.countSynonyms(taxon
,null);
678 assertEquals("countSynonyms should return 3", 3, numberOfRelatedSynonym
);
683 public void testGetSynonyms() {
684 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
685 assert taxon
!= null : "taxon must exist";
686 List
<String
> propertyPaths
= new ArrayList
<String
>();
687 propertyPaths
.add("synonym");
688 propertyPaths
.add("synonym.name");
690 List
<OrderHint
> orderHints
= new ArrayList
<>();
691 orderHints
.add(new OrderHint("titleCache", SortOrder
.ASCENDING
));
693 List
<Synonym
> synonyms
= taxonDao
.getSynonyms(taxon
, null, null, null,orderHints
,propertyPaths
);
695 assertNotNull("getSynonyms should return a List", synonyms
);
696 assertEquals("getSynonyms should return 3 synonyms", 3, synonyms
.size());
697 assertTrue("getSynonyms should return synonym objects with the synonym initialized", Hibernate
.isInitialized(synonyms
.get(0)));
702 public void testCountSynonymsByType() {
703 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
704 assert taxon
!= null : "taxon must exist";
706 long numberOfTaxonomicSynonyms
= taxonDao
.countSynonyms(taxon
, SynonymType
.HETEROTYPIC_SYNONYM_OF());
707 assertEquals("countSynonyms should return 4", 3, numberOfTaxonomicSynonyms
);
712 public void testSynonymsByType() {
713 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
714 assert taxon
!= null : "taxon must exist";
716 List
<Synonym
> synonyms
= taxonDao
.getSynonyms(taxon
, SynonymType
.HETEROTYPIC_SYNONYM_OF(), null, null,null,null);
718 assertNotNull("getSynonyms should return a List", synonyms
);
719 assertEquals("getSynonyms should return 4 Synonyms", 3, synonyms
.size());
724 public void testPageSynonyms(){
725 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
726 assert taxon
!= null : "taxon must exist";
729 List
<Synonym
> firstPage
= taxonDao
.getSynonyms(taxon
, null, pageSize
, 0,null,null);
730 List
<Synonym
> secondPage
= taxonDao
.getSynonyms(taxon
, null, pageSize
, 1,null,null);
732 assertNotNull("getSynonyms: 2, 0 should return a List",firstPage
);
733 assertEquals("getSynonyms: 2, 0 should return 2 synonyms", pageSize
,firstPage
.size());
734 assertNotNull("getSynonyms: 2, 1 should return a List",secondPage
);
735 assertEquals("getSynonyms: 2, 1 should return 1 synonym", 1, secondPage
.size());
740 public void testCountTaxonRelationships() {
741 long count
= taxonDao
.countTaxonRelationships(null);
742 assertEquals("There should be 11 relationships", 11, count
);
744 Set
<TaxonRelationshipType
> types
= new HashSet
<>();
745 count
= taxonDao
.countTaxonRelationships(types
);
746 assertEquals("Empty filter should return empty result", 0, count
);
748 types
.add(TaxonRelationshipType
.CONGRUENT_TO());
749 count
= taxonDao
.countTaxonRelationships(types
);
750 assertEquals("There should be no congruent relationship", 0, count
);
752 types
.add(TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN());
753 count
= taxonDao
.countTaxonRelationships(types
);
754 assertEquals("There should be 11 tax included relationships", 11, count
);
759 public void testlistTaxonRelationships() {
760 List
<TaxonRelationship
> rels
= taxonDao
.getTaxonRelationships(null, null, null, null, null);
761 assertEquals("There should be 11 relationships", 11, rels
.size());
763 rels
= taxonDao
.getTaxonRelationships(null, 2, 3, null, null);
764 assertEquals("There should be 11 relationships", 2, rels
.size());
766 Set
<TaxonRelationshipType
> types
= new HashSet
<>();
767 rels
= taxonDao
.getTaxonRelationships(types
, null, null, null, null);
768 assertEquals("Empty filter should return empty result", 0, rels
.size());
770 types
.add(TaxonRelationshipType
.CONGRUENT_TO());
771 rels
= taxonDao
.getTaxonRelationships(types
, null, null, null, null);
772 assertEquals("There should be no congruent relationship", 0, rels
.size());
774 types
.add(TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN());
775 rels
= taxonDao
.getTaxonRelationships(types
, null, null, null, null);
776 assertEquals("There should be 11 tax included relationships", 11, rels
.size());
780 @DataSet("TaxonNodeDaoHibernateImplTest.xml")
781 public void testListAcceptedTaxaFor() {
782 UUID acheontitia_ciprosus
= UUID
.fromString("3ef145f7-bd92-4a64-8afd-2b8203e00e02");
784 Synonym synonym
= (Synonym
)taxonDao
.findByUuid(acheontitia_ciprosus
);
785 assertNotNull("synonym must exist", synonym
);
787 Taxon taxon
= taxonDao
.acceptedTaxonFor(synonym
, null, null);
788 assertNotNull("listAcceptedTaxaFor should return a taxon", taxon
);
790 Classification classification
= classificationDao
.load(classificationUuid
);
791 assertNotNull("classification must exist", classification
);
793 taxon
= taxonDao
.acceptedTaxonFor(synonym
, classification
, null);
794 assertNull("listAcceptedTaxaFor should return not taxon due to classification filter", taxon
);
800 public void testGetTaxonMatchingUninomial() {
801 @SuppressWarnings("rawtypes")
802 List
<TaxonBase
> result
= taxonDao
.findTaxaByName(Taxon
.class, "Smerinthus", "*", "*", "*","*",null,null,null);
804 assertNotNull("findTaxaByName should return a List", result
);
805 assertEquals("findTaxaByName should return two Taxa",2,result
.size());
806 assertEquals("findTaxaByName should return a Taxon with id 5",5,result
.get(0).getId());
811 public void testGetTaxonMatchingSpeciesBinomial() {
812 @SuppressWarnings("rawtypes")
813 List
<TaxonBase
> result
= taxonDao
.findTaxaByName(Taxon
.class, "Smerinthus", null, "kindermannii", null,"*",null,null,null);
815 assertNotNull("findTaxaByName should return a List", result
);
816 assertEquals("findTaxaByName should return one Taxon",1,result
.size());
817 assertEquals("findTaxaByName should return a Taxon with id 8",8,result
.get(0).getId());
822 public void testGetTaxonMatchingTrinomial() {
823 @SuppressWarnings("rawtypes")
824 List
<TaxonBase
> result
= taxonDao
.findTaxaByName(Taxon
.class,"Cryptocoryne", null,"purpurea","borneoensis","*",null,null,null);
826 assertNotNull("findTaxaByName should return a List", result
);
827 assertEquals("findTaxaByName should return one Taxon",1,result
.size());
828 assertEquals("findTaxaByName should return a Taxon with id 38",38,result
.get(0).getId());
833 public void testNegativeMatch() {
834 @SuppressWarnings("rawtypes")
835 List
<TaxonBase
> result
= taxonDao
.findTaxaByName(Taxon
.class,"Acherontia", null,"atropos","dehli",null,null,null,null);
837 assertNotNull("findTaxaByName should return a List", result
);
838 assertTrue("findTaxaByName should return an empty List",result
.isEmpty());
843 public void testCountAllTaxa() {
844 long numberOfTaxa
= taxonDao
.count(Taxon
.class);
845 assertEquals("count should return 14 taxa", 14, numberOfTaxa
);
850 public void testListAllTaxa() {
851 List
<Taxon
> taxa
= taxonDao
.list(Taxon
.class,100, 0);
852 assertNotNull("list should return a List", taxa
);
853 assertEquals("list should return 14 taxa", 14, taxa
.size());
859 public void testDelete() {
860 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
861 assert taxon
!= null : "taxon must exist";
862 taxonDao
.delete(taxon
);
863 taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
864 assert taxon
== null : "taxon must not exist";
868 // printDataSet(new FileOutputStream("test.xml"), TABLE_NAMES);
869 // } catch (FileNotFoundException e) {
870 // e.printStackTrace();
876 @ExpectedDataSet("TaxonDaoHibernateImplTest.testDelete-result.xml")
877 public void testDeleteWithMarker() {
878 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
879 taxon
.addMarker(Marker
.NewInstance(MarkerType
.IS_DOUBTFUL(), true));
880 taxonDao
.save(taxon
);
881 assert taxon
!= null : "taxon must exist";
883 taxonDao
.delete(taxon
);
884 commitAndStartNewTransaction(null);
885 taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
886 assert taxon
== null : "taxon must not exist";
890 // printDataSet(new FileOutputStream("test.xml"), TABLE_NAMES);
891 // } catch (FileNotFoundException e) {
892 // e.printStackTrace();
897 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
898 public void testFindDeleted() {
899 TaxonBase
<?
> taxon
= taxonDao
.findByUuid(acherontia
);
900 assertNull("findByUuid should return null in this view", taxon
);
901 assertFalse("exist should return false in this view",taxonDao
.exists(acherontia
));
905 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
906 public void testFindDeletedInPreviousView() {
907 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
908 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
909 assertNotNull("findByUuid should return a taxon in this view",taxon
);
910 assertTrue("exists should return true in this view", taxonDao
.exists(acherontia
));
913 assertEquals("There should be 3 relations to this taxon in this view",3,taxon
.getRelationsToThisTaxon().size());
914 } catch(Exception e
) {
915 fail("We should not experience any problems initializing proxies with envers");
920 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
921 public void testGetAuditEvents() {
922 TaxonBase
<?
> taxon
= taxonDao
.findByUuid(sphingidae
);
923 assert taxon
!= null : "taxon cannot be null";
925 List
<String
> propertyPaths
= new ArrayList
<>();
926 propertyPaths
.add("name");
927 propertyPaths
.add("createdBy");
928 propertyPaths
.add("updatedBy");
930 @SuppressWarnings("rawtypes")
931 List
<AuditEventRecord
<TaxonBase
>> auditEvents
= taxonDao
.getAuditEvents(taxon
, null,null,null,propertyPaths
);
932 assertNotNull("getAuditEvents should return a list",auditEvents
);
933 assertFalse("the list should not be empty",auditEvents
.isEmpty());
934 assertEquals("There should be two AuditEventRecords in the list",2, auditEvents
.size());
938 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
939 public void testGetAuditEventsFromNow() {
940 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
941 TaxonBase
<?
> taxon
= taxonDao
.findByUuid(sphingidae
);
942 assert taxon
!= null : "taxon cannot be null";
944 @SuppressWarnings("rawtypes")
945 List
<AuditEventRecord
<TaxonBase
>> auditEvents
= taxonDao
.getAuditEvents(taxon
, null,null,AuditEventSort
.FORWARDS
,null);
946 assertNotNull("getAuditEvents should return a list",auditEvents
);
947 assertFalse("the list should not be empty",auditEvents
.isEmpty());
948 assertEquals("There should be one audit event in the list",1,auditEvents
.size());
952 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
953 public void testCountAuditEvents() {
954 TaxonBase
<?
> taxon
= taxonDao
.findByUuid(sphingidae
);
955 assert taxon
!= null : "taxon cannot be null";
957 long numberOfAuditEvents
= taxonDao
.countAuditEvents(taxon
, null);
958 assertEquals("countAuditEvents should return 2",numberOfAuditEvents
,2);
962 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
963 public void getPreviousAuditEvent() {
964 TaxonBase
<?
> taxon
= taxonDao
.findByUuid(sphingidae
);
965 assert taxon
!= null : "taxon cannot be null";
967 AuditEventRecord
<TaxonBase
> auditEvent
= taxonDao
.getPreviousAuditEvent(taxon
);
968 assertNotNull("getPreviousAuditEvent should not return null as there is at least one audit event prior to the current one",auditEvent
);
972 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
973 public void getPreviousAuditEventAtBeginning() {
974 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
975 TaxonBase
<?
> taxon
= taxonDao
.findByUuid(sphingidae
);
976 assert taxon
!= null : "taxon cannot be null";
978 @SuppressWarnings("rawtypes")
979 AuditEventRecord
<TaxonBase
> auditEvent
= taxonDao
.getPreviousAuditEvent(taxon
);
980 assertNull("getPreviousAuditEvent should return null if we're at the first audit event anyway",auditEvent
);
984 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
985 public void getNextAuditEvent() {
986 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
987 TaxonBase
<?
> taxon
= taxonDao
.findByUuid(sphingidae
);
988 assert taxon
!= null : "taxon cannot be null";
990 @SuppressWarnings("rawtypes")
991 AuditEventRecord
<TaxonBase
> auditEvent
= taxonDao
.getNextAuditEvent(taxon
);
992 assertNotNull("getNextAuditEvent should not return null as there is at least one audit event after the current one",auditEvent
);
996 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
997 public void getNextAuditEventAtEnd() {
998 AuditEventContextHolder
.getContext().setAuditEvent(mostRecentAuditEvent
);
999 TaxonBase
<?
> taxon
= taxonDao
.findByUuid(sphingidae
);
1000 assert taxon
!= null : "taxon cannot be null";
1002 @SuppressWarnings("rawtypes")
1003 AuditEventRecord
<TaxonBase
> auditEvent
= taxonDao
.getNextAuditEvent(taxon
);
1004 assertNull("getNextAuditEvent should return null as there no more audit events after the current one",auditEvent
);
1008 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1009 public void testFind() {
1010 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontiaLachesis
);
1011 assert taxon
!= null : "taxon cannot be null";
1013 assertEquals("getRelationsToThisTaxon should contain 1 TaxonRelationship in this view",1,taxon
.getRelationsToThisTaxon().size());
1017 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1018 public void testFindInPreviousView() {
1019 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
1020 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontiaLachesis
);
1021 assert taxon
!= null : "taxon cannot be null";
1023 assertTrue("getRelationsToThisTaxon should contain 0 TaxonRelationship in this view",taxon
.getRelationsToThisTaxon().isEmpty());
1027 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1028 public void testGetRelations() {
1029 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontiaLachesis
);
1030 assert taxon
!= null : "taxon cannot be null";
1032 List
<String
> propertyPaths
= new ArrayList
<>();
1033 propertyPaths
.add("fromTaxon");
1034 propertyPaths
.add("fromTaxon.name");
1036 List
<OrderHint
> orderHints
= new ArrayList
<>();
1037 orderHints
.add(new OrderHint("relatedFrom.titleCache", SortOrder
.ASCENDING
));
1039 List
<TaxonRelationship
> taxonRelations
= taxonDao
.getTaxonRelationships(taxon
, TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(),
1040 includeUnpublished
, null, null,orderHints
,propertyPaths
, TaxonRelationship
.Direction
.relatedFrom
);
1041 assertNotNull("getRelatedTaxa should return a list", taxonRelations
);
1042 assertEquals("there should be one TaxonRelationship in the list in the current view",1,taxonRelations
.size());
1043 assertTrue("TaxonRelationship.relatedFrom should be initialized",Hibernate
.isInitialized(taxonRelations
.get(0).getFromTaxon()));
1044 assertTrue("TaxonRelationship.relatedFrom.name should be initialized",Hibernate
.isInitialized(taxonRelations
.get(0).getFromTaxon().getName()));
1048 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1049 public void testCountRelations() {
1050 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontiaLachesis
);
1051 assert taxon
!= null : "taxon cannot be null";
1052 assertEquals("countRelatedTaxa should return 1 in the current view",1, taxonDao
.countTaxonRelationships(
1053 taxon
, TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(),
1054 includeUnpublished
, TaxonRelationship
.Direction
.relatedTo
));
1058 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1059 public void testGetRelationsInPreviousView() {
1060 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
1061 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontiaLachesis
);
1062 assert taxon
!= null : "taxon cannot be null";
1064 List
<String
> propertyPaths
= new ArrayList
<String
>();
1065 propertyPaths
.add("relatedFrom");
1066 propertyPaths
.add("relatedFrom.name");
1068 List
<OrderHint
> orderHints
= new ArrayList
<>();
1069 orderHints
.add(new OrderHint("relatedFrom.titleCache", SortOrder
.ASCENDING
));
1071 List
<TaxonRelationship
> taxonRelations
= taxonDao
.getTaxonRelationships(taxon
, TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(),
1072 includeUnpublished
, null, null, orderHints
,propertyPaths
, TaxonRelationship
.Direction
.relatedFrom
);
1073 assertNotNull("getRelatedTaxa should return a list",taxonRelations
);
1074 assertTrue("there should be no TaxonRelationships in the list in the prior view", taxonRelations
.isEmpty());
1078 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1079 public void testCountRelationsInPreviousView() {
1080 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
1081 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontiaLachesis
);
1082 assert taxon
!= null : "taxon cannot be null";
1083 assertEquals("countRelatedTaxa should return 0 in the current view",0,
1084 taxonDao
.countTaxonRelationships(taxon
,TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(),
1085 includeUnpublished
, TaxonRelationship
.Direction
.relatedTo
));
1090 public void testGroupTaxa() {
1091 List
<Grouping
> groups
= new ArrayList
<>();
1092 groups
.add(new GroupByCount("count",SortOrder
.DESCENDING
));
1093 groups
.add(new Grouping("name.genusOrUninomial", "genus", "n", SortOrder
.ASCENDING
));
1094 List
<Object
[]> results
= taxonDao
.group(null, null, null, groups
,null);
1095 if(logger
.isTraceEnabled()){
1096 System
.out
.println("count\tname.genuOrUninomial");
1097 for(Object
[] result
: results
) {
1098 System
.out
.println(result
[0] + "\t" + result
[1]);
1105 public void testGroupTaxaByClass() {
1106 List
<Grouping
> groups
= new ArrayList
<>();
1107 groups
.add(new GroupByCount("count",SortOrder
.DESCENDING
));
1108 groups
.add(new Grouping("class", "class",null, SortOrder
.ASCENDING
));
1109 List
<Object
[]> results
= taxonDao
.group(null, null, null, groups
,null);
1110 if(logger
.isTraceEnabled()){
1111 System
.out
.println("count\tclass");
1112 for(Object
[] result
: results
) {
1113 System
.out
.println(result
[0] + "\t" + result
[1]);
1120 public void testNativeSQLOrder() {
1121 List
<OrderHint
> orderHints
= new ArrayList
<>();
1122 orderHints
.add(new NativeSqlOrderHint("case when {alias}.titleCache like 'C%' then 0 else 1 end",SortOrder
.ASCENDING
));
1124 @SuppressWarnings("rawtypes")
1125 List
<TaxonBase
> results
= taxonDao
.list(null, null, orderHints
);
1126 if(logger
.isTraceEnabled()){
1127 System
.out
.println("native SQL order");
1128 for(TaxonBase
<?
> result
: results
) {
1129 System
.out
.println(result
.getTitleCache());
1136 public void testGroupByDateTaxa() {
1137 List
<Grouping
> groups
= new ArrayList
<>();
1138 groups
.add(new GroupByCount("count",null));
1139 groups
.add(new GroupByDate("created", "dateGroup", SortOrder
.ASCENDING
, GroupByDate
.Resolution
.MONTH
));
1140 List
<Object
[]> results
= taxonDao
.group(null, null, null, groups
,null);
1141 if(logger
.isTraceEnabled()){
1142 System
.out
.println("count\tyear\tmonth");
1143 for(Object
[] result
: results
) {
1144 System
.out
.println(result
[0] + "\t" + result
[1] + "\t" + result
[2]);
1153 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
1154 //NOTE: There is a problem with loading AuditEvents if this test runs
1155 //stand alone or as first (one of the first) in the test suite. For some reason
1156 //the AuditEvent records from the @DataSet are not inserted into the database then,
1157 //while those inserted by the terms dataset are inserted as well as a completely new one.
1158 //This problem for some reason does not happen if not running at first place
1159 public void testGetAuditEventsByTypeWithRestrictions() {
1160 commitAndStartNewTransaction(new String
[]{"AUDITEVENT", "TAXONBASE_AUD"});
1162 List
<String
> propertyPaths
= new ArrayList
<>();
1163 propertyPaths
.add("name");
1164 propertyPaths
.add("createdBy");
1165 propertyPaths
.add("updatedBy");
1167 List
<AuditCriterion
> criteria
= new ArrayList
<>();
1168 criteria
.add(AuditEntity
.property("lsid_lsid").isNotNull());
1170 long count
= taxonDao
.countAuditEvents(TaxonBase
.class, null, null, null);
1172 @SuppressWarnings("rawtypes")
1173 List
<AuditEventRecord
<TaxonBase
>> auditEvents
= taxonDao
.getAuditEvents(TaxonBase
.class, previousAuditEvent
, mostRecentAuditEvent
, criteria
,null, null, AuditEventSort
.FORWARDS
, propertyPaths
);
1174 assertNotNull("getAuditEvents should return a list",auditEvents
);
1175 assertFalse("the list should not be empty",auditEvents
.isEmpty());
1176 assertEquals("There should be one AuditEventRecord in the list",1, auditEvents
.size());
1181 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
1182 // @DataSets({ //for testing only
1183 // @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml"),
1184 // @DataSet("TaxonDaoHibernateImplTest.testFindDeletedAuditEvents.xml")
1186 //NOTE: There is a problem with loading AuditEvents if this test runs
1187 //stand alone or as first (one of the first) in the test suite. For some reason
1188 //the AuditEvent records from the @DataSet are not inserted into the database then,
1189 //while those inserted by the terms dataset are inserted as well as a completely new one.
1190 //This problem for some reason does not happen if not running at first place
1191 public void testGetAuditEventsByTypeWithNoRestrictions() {
1192 // printDataSet(System.out, new String[]{"AUDITEVENT", "TAXONBASE_AUD"});
1193 commitAndStartNewTransaction(new String
[]{"AUDITEVENT", "TAXONBASE_AUD"});
1195 List
<String
> propertyPaths
= new ArrayList
<>();
1196 propertyPaths
.add("name");
1197 propertyPaths
.add("createdBy");
1198 propertyPaths
.add("updatedBy");
1199 long count
= taxonDao
.countAuditEvents(TaxonBase
.class, null, null, null);
1200 @SuppressWarnings("rawtypes")
1201 List
<AuditEventRecord
<TaxonBase
>> auditEvents
= taxonDao
.getAuditEvents(TaxonBase
.class, previousAuditEvent
, mostRecentAuditEvent
, null,null, null, AuditEventSort
.FORWARDS
, propertyPaths
);
1202 assertNotNull("getAuditEvents should return a list", auditEvents
);
1203 assertFalse("the list should not be empty", auditEvents
.isEmpty());
1204 assertEquals("There should be thirty eight AuditEventRecords in the list", 2, auditEvents
.size());
1209 @DataSet("TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
1210 public void testGetCommonName(){
1211 List
<Taxon
> commonNameResults
= taxonDao
.getTaxaByCommonName("common%", null,
1212 MatchMode
.BEGINNING
, null, null, null, null);
1214 assertNotNull("getTaxaByCommonName should return a list", commonNameResults
);
1215 assertFalse("the list should not be empty", commonNameResults
.isEmpty());
1216 assertEquals("There should be two taxa with common name", 2,commonNameResults
.size());
1218 @SuppressWarnings("rawtypes")
1219 List
<UuidAndTitleCache
<IdentifiableEntity
>> list
= taxonDao
.getTaxaByCommonNameForEditor(
1220 "common%", null, MatchMode
.BEGINNING
, null);
1222 assertNotNull("getTaxaByCommonName should return a list", list
);
1223 assertFalse("the list should not be empty", list
.isEmpty());
1224 assertEquals("There should be two Taxon with common name", 2,list
.size());
1230 public void testGetTitleCache(){
1231 UUID uuid
= UUID
.fromString("7b8b5cb3-37ba-4dba-91ac-4c6ffd6ac331");
1232 String titleCache
= taxonDao
.getTitleCache(uuid
, false);
1233 Assert
.assertEquals("Acherontia styx Westwood, 1847 sec. cate-sphingidae.org", titleCache
);
1234 titleCache
= taxonDao
.getTitleCache(uuid
, true);
1235 Assert
.assertEquals("Acherontia styxx Westwood, 1847 sec. cate-sphingidae.org", titleCache
);
1240 @DataSet("TaxonDaoHibernateImplTest.testPropertyPath.xml")
1241 public void testPropertyPath(){
1242 //Test that BeanInitializer also works on HiberanteProxys
1243 Classification c
= classificationDao
.load(UUID
.fromString("4bceea53-893f-4685-8c63-6dcec6e85ab1"));
1244 TaxonNode singleNode
= c
.getRootNode().getChildNodes().iterator().next();
1245 Taxon taxonProxy
= singleNode
.getTaxon();
1246 Assert
.assertTrue("Object to test should be a proxy ", taxonProxy
instanceof HibernateProxy
);
1248 List
<String
> propertyPaths
= new ArrayList
<>();
1249 propertyPaths
.add("taxonNodes");
1250 Taxon taxon
= (Taxon
)this.taxonDao
.load(
1251 UUID
.fromString("4a5bc930-844f-45ec-aea8-dd155e1ab25f"),
1253 Assert
.assertSame("Returned object should be the same proxy to assure that we ran initialization on this proxy", taxonProxy
, taxon
);
1262 public void createTestDataSet() throws FileNotFoundException
{
1263 // Classification classification = Classification.NewInstance("Test");
1264 // BotanicalName TaxonName = null;
1265 // Reference sec = null;
1266 // Taxon taxon = Taxon.NewInstance(taxonName, sec);
1267 // classification.addChildTaxon(taxon, sec, null);
1269 // classificationDao.save(classification);
1270 // this.commitAndStartNewTransaction(null);
1272 // writeDbUnitDataSetFile(new String[] {
1273 // "CLASSIFICATION", "TAXONNAME",
1274 // "REFERENCE","TAXONNODE",
1275 // "TAXONBASE","LANGUAGESTRING",
1276 // "HIBERNATE_SEQUENCES" // IMPORTANT!!!
1278 // "testPropertyPath" );