1 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.taxon
;
3 import static junit
.framework
.Assert
.assertEquals
;
4 import static junit
.framework
.Assert
.assertFalse
;
5 import static junit
.framework
.Assert
.assertNotNull
;
6 import static junit
.framework
.Assert
.assertSame
;
7 import static junit
.framework
.Assert
.assertTrue
;
8 import static org
.junit
.Assert
.assertNull
;
9 import static org
.junit
.Assert
.fail
;
11 import java
.io
.FileNotFoundException
;
12 import java
.io
.FileOutputStream
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import org
.hibernate
.Hibernate
;
18 import org
.junit
.After
;
19 import org
.junit
.Before
;
20 import org
.junit
.Ignore
;
21 import org
.junit
.Test
;
22 import org
.unitils
.dbunit
.annotation
.DataSet
;
23 import org
.unitils
.dbunit
.annotation
.ExpectedDataSet
;
24 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
27 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
28 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
29 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationship
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
36 import eu
.etaxonomy
.cdm
.model
.view
.AuditEvent
;
37 import eu
.etaxonomy
.cdm
.model
.view
.AuditEventRecord
;
38 import eu
.etaxonomy
.cdm
.model
.view
.context
.AuditEventContextHolder
;
39 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.AuditEventSort
;
40 import eu
.etaxonomy
.cdm
.persistence
.dao
.reference
.IReferenceDao
;
41 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonDao
;
42 import eu
.etaxonomy
.cdm
.persistence
.fetch
.CdmFetch
;
43 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
44 import eu
.etaxonomy
.cdm
.persistence
.query
.SelectMode
;
45 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
.SortOrder
;
46 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
47 import eu
.etaxonomy
.cdm
.test
.integration
.CdmTransactionalIntegrationTest
;
54 public class TaxonDaoHibernateImplTest
extends CdmTransactionalIntegrationTest
{
57 private ITaxonDao taxonDao
;
60 private IReferenceDao referenceDao
;
63 private UUID sphingidae
;
64 private UUID acherontia
;
66 private UUID acherontiaLachesis
;
67 private AuditEvent previousAuditEvent
;
68 private AuditEvent mostRecentAuditEvent
;
72 uuid
= UUID
.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2");
73 sphingidae
= UUID
.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9");
74 acherontia
= UUID
.fromString("c5cc8674-4242-49a4-aada-72d63194f5fa");
75 acherontiaLachesis
= UUID
.fromString("b04cc9cb-2b4a-4cc4-a94a-3c93a2158b06");
76 mimas
= UUID
.fromString("900052b7-b69c-4e26-a8f0-01c215214c40");
77 previousAuditEvent
= new AuditEvent();
78 previousAuditEvent
.setRevisionNumber(1025);
79 previousAuditEvent
.setUuid(UUID
.fromString("a680fab4-365e-4765-b49e-768f2ee30cda"));
80 mostRecentAuditEvent
= new AuditEvent();
81 mostRecentAuditEvent
.setRevisionNumber(1026);
82 mostRecentAuditEvent
.setUuid(UUID
.fromString("afe8e761-8545-497b-9134-6a6791fc0b0d"));
83 AuditEventContextHolder
.clearContext(); // By default we're in the current view (i.e. view == null)
87 public void tearDown() {
88 AuditEventContextHolder
.clearContext();
92 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#TaxonDaoHibernateImpl()}.
96 public void testInit() {
97 assertNotNull("Instance of ITaxonDao expected",taxonDao
);
98 assertNotNull("Instance of IReferenceDao expected",referenceDao
);
102 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getRootTaxa(eu.etaxonomy.cdm.model.reference.ReferenceBase)}.
106 public void testGetRootTaxa() {
107 ReferenceBase sec1
= referenceDao
.findById(1);
108 assert sec1
!= null : "sec1 must exist";
109 ReferenceBase sec2
= referenceDao
.findById(2);
110 assert sec2
!= null : "sec2 must exist";
112 List
<Taxon
> rootTaxa
= taxonDao
.getRootTaxa(sec1
);
113 assertNotNull("getRootTaxa should return a List",rootTaxa
);
114 assertFalse("The list should not be empty",rootTaxa
.isEmpty());
115 assertEquals("There should be one root taxon",1, rootTaxa
.size());
117 rootTaxa
= taxonDao
.getRootTaxa(sec1
, CdmFetch
.FETCH_CHILDTAXA(), true, false);
118 assertNotNull("getRootTaxa should return a List",rootTaxa
);
119 assertFalse("The list should not be empty",rootTaxa
.isEmpty());
120 assertEquals("There should be one root taxon",1, rootTaxa
.size());
122 rootTaxa
= taxonDao
.getRootTaxa(Rank
.GENUS(), sec1
, CdmFetch
.FETCH_CHILDTAXA(), true, false, null);
123 assertNotNull("getRootTaxa should return a List",rootTaxa
);
124 assertFalse("The list should not be empty",rootTaxa
.isEmpty());
125 assertEquals("There should be one root taxon",1, rootTaxa
.size());
127 rootTaxa
= taxonDao
.getRootTaxa(Rank
.FAMILY(), sec2
, CdmFetch
.FETCH_CHILDTAXA(), true, false, null);
128 if (logger
.isDebugEnabled()) {
129 logger
.debug("Root taxa rank Family (" + rootTaxa
.size() + "):");
130 for (Taxon taxon
: rootTaxa
) {
131 logger
.debug(taxon
.getTitleCache());
134 assertEquals("There should be one root taxon rank Family",1, rootTaxa
.size());
135 rootTaxa
= taxonDao
.getRootTaxa(Rank
.GENUS(), sec2
, CdmFetch
.FETCH_CHILDTAXA(), true, false, null);
136 assertNotNull("getRootTaxa should return a List",rootTaxa
);
137 assertFalse("The list should not be empty",rootTaxa
.isEmpty());
138 if (logger
.isDebugEnabled()) {
139 logger
.debug("Root taxa rank Genus (" + rootTaxa
.size() + "):");
140 for (Taxon taxon
: rootTaxa
) {
141 logger
.debug(taxon
.getTitleCache());
144 assertEquals("There should be 22 root taxa rank Genus",22, rootTaxa
.size());
146 rootTaxa
= taxonDao
.getRootTaxa(Rank
.SPECIES(), sec2
, CdmFetch
.FETCH_CHILDTAXA(), true, false, null);
147 if (logger
.isDebugEnabled()) {
148 logger
.debug("Root taxa rank Species (" + rootTaxa
.size() + "):");
149 for (Taxon taxon
: rootTaxa
) {
150 logger
.debug(taxon
.getTitleCache());
153 assertEquals("There should be 4 root taxa rank Species",3, rootTaxa
.size());
157 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.ReferenceBase)}.
161 public void testGetTaxaByName() {
162 ReferenceBase sec
= referenceDao
.findById(1);
163 assert sec
!= null : "sec must exist";
165 List
<TaxonBase
> results
= taxonDao
.getTaxaByName("Aus", sec
);
166 assertNotNull("getTaxaByName should return a List", results
);
167 //assertFalse("The list should not be empty", results.isEmpty());
168 assertTrue(results
.size() == 1);
170 results
= taxonDao
.getTaxaByName("A*", MatchMode
.BEGINNING
,
172 assertNotNull("getTaxaByName should return a List", results
);
173 assertTrue(results
.size() == 9);
175 if (logger
.isDebugEnabled()) {
176 for (int i
= 0; i
< results
.size(); i
++) {
177 String nameCache
= "";
178 TaxonNameBase
<?
,?
> taxonNameBase
= ((TaxonBase
)results
.get(i
)).getName();
179 nameCache
= ((NonViralName
)taxonNameBase
).getNameCache();
180 logger
.debug(results
.get(i
).getClass() + "(" + i
+")" +
181 ": Name Cache = " + nameCache
+ ", Title Cache = " + results
.get(i
).getTitleCache());
184 // assertEquals(results.get(0).getTitleCache(), "Abies sec. ???");
185 // assertEquals(results.get(1).getTitleCache(), "Abies Mill.");
186 // assertEquals(results.get(2).getTitleCache(), "Abies mill. sec. ???");
187 // assertEquals(results.get(3).getTitleCache(), "Abies alba sec. ???");
188 // assertEquals(results.get(4).getTitleCache(), "Abies alba Michx. sec. ???");
189 // assertEquals(results.get(5).getTitleCache(), "Abies alba Mill. sec. ???");
191 results
= taxonDao
.getTaxaByName("A", MatchMode
.BEGINNING
,
193 assertNotNull("getTaxaByName should return a List", results
);
194 assertTrue(results
.size() == 9);
196 results
= taxonDao
.getTaxaByName("Aus", MatchMode
.EXACT
,
198 assertNotNull("getTaxaByName should return a List", results
);
199 assertEquals("Results list should contain one entity",1,results
.size());
205 public void testFindByUuid() {
206 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(uuid
);
207 assertNotNull("findByUuid should return a taxon",taxon
);
208 assertFalse("findByUuid should not return a taxon with it's name initialized",Hibernate
.isInitialized(taxon
.getName()));
213 public void testLoad() {
214 List
<String
> propertyPaths
= new ArrayList
<String
>();
215 propertyPaths
.add("name");
216 propertyPaths
.add("sec");
217 Taxon taxon
= (Taxon
)taxonDao
.load(uuid
, propertyPaths
);
218 assertNotNull("findByUuid should return a taxon",taxon
);
219 assertTrue("load should return a taxon with it's name initialized, given that the property was specified in the method",Hibernate
.isInitialized(taxon
.getName()));
220 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()));
225 public void testCountRelatedTaxa() {
226 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(sphingidae
);
227 assert taxon
!= null : "taxon must exist";
229 int numberOfRelatedTaxa
= taxonDao
.countRelatedTaxa(taxon
,TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN());
230 assertEquals("countRelatedTaxa should return 23", 23, numberOfRelatedTaxa
);
235 public void testCountTaxaByName() {
236 int numberOfTaxa
= taxonDao
.countTaxaByName("A*", MatchMode
.BEGINNING
, true);
237 assertEquals(numberOfTaxa
, 9);
238 numberOfTaxa
= taxonDao
.countTaxaByName("A*", MatchMode
.BEGINNING
, SelectMode
.TAXA
);
239 assertEquals(numberOfTaxa
, 9);
240 numberOfTaxa
= taxonDao
.countTaxaByName("A*", MatchMode
.BEGINNING
, false);
241 assertEquals(numberOfTaxa
, 3);
242 numberOfTaxa
= taxonDao
.countTaxaByName("A*", MatchMode
.BEGINNING
, SelectMode
.SYNONYMS
);
243 assertEquals(numberOfTaxa
, 3);
244 numberOfTaxa
= taxonDao
.countTaxaByName("A*", MatchMode
.BEGINNING
, SelectMode
.ALL
);
245 assertEquals(numberOfTaxa
, 12);
246 ReferenceBase reference
= referenceDao
.findByUuid(UUID
.fromString("596b1325-be50-4b0a-9aa2-3ecd610215f2"));
247 numberOfTaxa
= taxonDao
.countTaxaByName("A*", MatchMode
.BEGINNING
, SelectMode
.ALL
, reference
);
248 assertEquals(numberOfTaxa
, 2);
253 public void testRelatedTaxa() {
254 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(sphingidae
);
255 assert taxon
!= null : "taxon must exist";
257 List
<String
> propertyPaths
= new ArrayList
<String
>();
258 propertyPaths
.add("fromTaxon");
259 propertyPaths
.add("fromTaxon.name");
260 List
<OrderHint
> orderHints
= new ArrayList
<OrderHint
>();
261 orderHints
.add(new OrderHint("relatedFrom.titleCache", SortOrder
.ASCENDING
));
263 List
<TaxonRelationship
> relatedTaxa
= taxonDao
.getRelatedTaxa(taxon
, TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(), null, null, orderHints
,propertyPaths
);
264 assertNotNull("getRelatedTaxa should return a List",relatedTaxa
);
265 assertEquals("getRelatedTaxa should return all 23 related taxa",relatedTaxa
.size(),23);
266 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate
.isInitialized(relatedTaxa
.get(0).getFromTaxon()));
267 assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate
.isInitialized(relatedTaxa
.get(0).getFromTaxon().getName()));
269 assertEquals("Acherontia should appear first in the list of related taxa", relatedTaxa
.get(0).getFromTaxon().getTitleCache(), "Acherontia Laspeyres, 1809 sec. cate-sphingidae.org");
270 assertEquals("Sphingonaepiopsis should appear last in the list of related taxa", relatedTaxa
.get(22).getFromTaxon().getTitleCache(), "Sphinx Linnaeus, 1758 sec. cate-sphingidae.org");
275 public void testGetRelatedTaxaPaged() {
276 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(sphingidae
);
277 assert taxon
!= null : "taxon must exist";
279 List
<String
> propertyPaths
= new ArrayList
<String
>();
280 propertyPaths
.add("fromTaxon");
281 propertyPaths
.add("fromTaxon.name");
283 List
<OrderHint
> orderHints
= new ArrayList
<OrderHint
>();
284 orderHints
.add(new OrderHint("relatedFrom.titleCache", SortOrder
.ASCENDING
));
286 List
<TaxonRelationship
> firstPage
= taxonDao
.getRelatedTaxa(taxon
,TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(), 10, 0, orderHints
,propertyPaths
);
287 List
<TaxonRelationship
> secondPage
= taxonDao
.getRelatedTaxa(taxon
,TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(),10, 1, orderHints
,propertyPaths
);
288 List
<TaxonRelationship
> thirdPage
= taxonDao
.getRelatedTaxa(taxon
,TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(), 10, 2, orderHints
,propertyPaths
);
290 assertNotNull("getRelatedTaxa: 10, 0 should return a List",firstPage
);
291 assertEquals("getRelatedTaxa: 10, 0 should return a List with 10 elements",10,firstPage
.size());
292 assertNotNull("getRelatedTaxa: 10, 1 should return a List",secondPage
);
293 assertEquals("getRelatedTaxa: 10, 1 should return a List with 10 elements",secondPage
.size(),10);
294 assertNotNull("getRelatedTaxa: 10, 2 should return a List",thirdPage
);
295 assertEquals("getRelatedTaxa: 10, 2 should return a List with 3 elements",thirdPage
.size(),3);
300 public void testCountSynonymRelationships() {
301 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
302 assert taxon
!= null : "taxon must exist";
304 int numberOfSynonymRelationships
= taxonDao
.countSynonyms(taxon
,null);
305 assertEquals("countSynonymRelationships should return 5",5,numberOfSynonymRelationships
);
310 public void testSynonymRelationships() {
311 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
312 assert taxon
!= null : "taxon must exist";
313 List
<String
> propertyPaths
= new ArrayList
<String
>();
314 propertyPaths
.add("synonym");
315 propertyPaths
.add("synonym.name");
317 List
<OrderHint
> orderHints
= new ArrayList
<OrderHint
>();
318 orderHints
.add(new OrderHint("relatedFrom.titleCache", SortOrder
.ASCENDING
));
320 List
<SynonymRelationship
> synonyms
= taxonDao
.getSynonyms(taxon
, null, null, null,orderHints
,propertyPaths
);
322 assertNotNull("getSynonyms should return a List",synonyms
);
323 assertEquals("getSynonyms should return 5 SynonymRelationship entities",synonyms
.size(),5);
324 assertTrue("getSynonyms should return SynonymRelationship objects with the synonym initialized",Hibernate
.isInitialized(synonyms
.get(0).getSynonym()));
329 public void testCountSynonymRelationshipsByType() {
330 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
331 assert taxon
!= null : "taxon must exist";
333 int numberOfTaxonomicSynonyms
= taxonDao
.countSynonyms(taxon
, SynonymRelationshipType
.HETEROTYPIC_SYNONYM_OF());
334 assertEquals("countSynonyms should return 4",numberOfTaxonomicSynonyms
, 4);
339 public void testSynonymRelationshipsByType() {
340 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
341 assert taxon
!= null : "taxon must exist";
343 List
<SynonymRelationship
> synonyms
= taxonDao
.getSynonyms(taxon
, SynonymRelationshipType
.HETEROTYPIC_SYNONYM_OF(), null, null,null,null);
345 assertNotNull("getSynonyms should return a List",synonyms
);
346 assertEquals("getSynonyms should return 4 SynonymRelationship entities",synonyms
.size(),4);
351 public void testPageSynonymRelationships(){
352 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
353 assert taxon
!= null : "taxon must exist";
355 List
<SynonymRelationship
> firstPage
= taxonDao
.getSynonyms(taxon
, null, 4, 0,null,null);
356 List
<SynonymRelationship
> secondPage
= taxonDao
.getSynonyms(taxon
, null, 4, 1,null,null);
358 assertNotNull("getSynonyms: 4, 0 should return a List",firstPage
);
359 assertEquals("getSynonyms: 4, 0 should return 4 SynonymRelationships", firstPage
.size(),4);
360 assertNotNull("getSynonyms: 4, 1 should return a List",secondPage
);
361 assertEquals("getSynonyms: 4, 1 should return 1 SynonymRelationship",secondPage
.size(),1);
366 public void testGetTaxonMatchingUninomial() {
367 List
<TaxonBase
> result
= taxonDao
.findTaxaByName(Taxon
.class, "Smerinthus", "*", "*", "*",null,null,null);
369 assertNotNull("findTaxaByName should return a List", result
);
370 assertEquals("findTaxaByName should return two Taxa",2,result
.size());
371 assertEquals("findTaxaByName should return a Taxon with id 5",5,result
.get(0).getId());
376 public void testGetTaxonMatchingSpeciesBinomial() {
377 List
<TaxonBase
> result
= taxonDao
.findTaxaByName(Taxon
.class,"Smerinthus", null, "kindermannii", null,null,null,null);
379 assertNotNull("findTaxaByName should return a List", result
);
380 assertEquals("findTaxaByName should return one Taxon",1,result
.size());
381 assertEquals("findTaxaByName should return a Taxon with id 8",8,result
.get(0).getId());
386 public void testGetTaxonMatchingTrinomial() {
387 List
<TaxonBase
> result
= taxonDao
.findTaxaByName(Taxon
.class,"Cryptocoryne", null,"purpurea","borneoensis",null,null,null);
389 assertNotNull("findTaxaByName should return a List", result
);
390 assertEquals("findTaxaByName should return one Taxon",1,result
.size());
391 assertEquals("findTaxaByName should return a Taxon with id 38",38,result
.get(0).getId());
396 public void testNegativeMatch() {
397 List
<TaxonBase
> result
= taxonDao
.findTaxaByName(Taxon
.class,"Acherontia", null,"atropos","dehli",null,null,null);
399 assertNotNull("findTaxaByName should return a List", result
);
400 assertTrue("findTaxaByName should return an empty List",result
.isEmpty());
405 public void testCountAllTaxa() {
406 int numberOfTaxa
= taxonDao
.count(Taxon
.class);
407 assertEquals("count should return 33 taxa",33, numberOfTaxa
);
412 public void testListAllTaxa() {
413 List
<Taxon
> taxa
= taxonDao
.list(Taxon
.class,100, 0);
414 assertNotNull("list should return a List",taxa
);
415 assertEquals("list should return 33 taxa",33, taxa
.size());
421 public void testDelete() {
422 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
423 assert taxon
!= null : "taxon must exist";
424 taxonDao
.delete(taxon
);
431 public void testDeleteWithChildren() {
432 Taxon taxonWithChildren
= (Taxon
)taxonDao
.findByUuid(mimas
);
433 assert taxonWithChildren
!= null : "taxon must exist";
434 assertEquals(taxonWithChildren
.getTaxonomicChildrenCount(), 2);
435 Taxon parent
= (Taxon
)taxonDao
.findByUuid(sphingidae
);
436 assertSame(taxonWithChildren
.getTaxonomicParent(), parent
);
437 assertEquals(parent
.getTaxonomicChildrenCount(), 204);
438 taxonDao
.delete(taxonWithChildren
);
439 assertEquals(parent
.getTaxonomicChildrenCount(), 203);
443 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
444 public void testFindDeleted() {
445 TaxonBase taxon
= taxonDao
.findByUuid(acherontia
);
446 assertNull("findByUuid should return null in this view", taxon
);
447 assertFalse("exist should return false in this view",taxonDao
.exists(acherontia
));
451 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
452 public void testFindDeletedInPreviousView() {
453 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
454 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontia
);
455 assertNotNull("findByUuid should return a taxon in this view",taxon
);
456 assertTrue("exists should return true in this view", taxonDao
.exists(acherontia
));
459 assertEquals("There should be 3 relations to this taxon in this view",3,taxon
.getRelationsToThisTaxon().size());
460 } catch(Exception e
) {
461 fail("We should not experience any problems initializing proxies with envers");
466 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
467 public void testGetAuditEvents() {
468 TaxonBase taxon
= taxonDao
.findByUuid(sphingidae
);
469 assert taxon
!= null : "taxon cannot be null";
471 List
<AuditEventRecord
<TaxonBase
>> auditEvents
= taxonDao
.getAuditEvents(taxon
, null,null,null);
472 assertNotNull("getAuditEvents should return a list",auditEvents
);
473 assertFalse("the list should not be empty",auditEvents
.isEmpty());
474 assertEquals("There should be two AuditEventRecords in the list",2, auditEvents
.size());
478 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
479 public void testGetAuditEventsFromNow() {
480 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
481 TaxonBase taxon
= taxonDao
.findByUuid(sphingidae
);
482 assert taxon
!= null : "taxon cannot be null";
484 List
<AuditEventRecord
<TaxonBase
>> auditEvents
= taxonDao
.getAuditEvents(taxon
, null,null,AuditEventSort
.FORWARDS
);
485 assertNotNull("getAuditEvents should return a list",auditEvents
);
486 assertFalse("the list should not be empty",auditEvents
.isEmpty());
487 assertEquals("There should be one audit event in the list",1,auditEvents
.size());
491 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
492 public void testCountAuditEvents() {
493 TaxonBase taxon
= taxonDao
.findByUuid(sphingidae
);
494 assert taxon
!= null : "taxon cannot be null";
496 int numberOfAuditEvents
= taxonDao
.countAuditEvents(taxon
,null);
497 assertEquals("countAuditEvents should return 2",numberOfAuditEvents
,2);
501 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
502 public void getPreviousAuditEvent() {
503 TaxonBase taxon
= taxonDao
.findByUuid(sphingidae
);
504 assert taxon
!= null : "taxon cannot be null";
506 AuditEventRecord
<TaxonBase
> auditEvent
= taxonDao
.getPreviousAuditEvent(taxon
);
507 assertNotNull("getPreviousAuditEvent should not return null as there is at least one audit event prior to the current one",auditEvent
);
511 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
512 public void getPreviousAuditEventAtBeginning() {
513 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
514 TaxonBase taxon
= taxonDao
.findByUuid(sphingidae
);
515 assert taxon
!= null : "taxon cannot be null";
517 AuditEventRecord
<TaxonBase
> auditEvent
= taxonDao
.getPreviousAuditEvent(taxon
);
518 assertNull("getPreviousAuditEvent should return null if we're at the first audit event anyway",auditEvent
);
522 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
523 public void getNextAuditEvent() {
524 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
525 TaxonBase taxon
= taxonDao
.findByUuid(sphingidae
);
526 assert taxon
!= null : "taxon cannot be null";
528 AuditEventRecord
<TaxonBase
> auditEvent
= taxonDao
.getNextAuditEvent(taxon
);
529 assertNotNull("getNextAuditEvent should not return null as there is at least one audit event after the current one",auditEvent
);
533 @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
534 public void getNextAuditEventAtEnd() {
535 AuditEventContextHolder
.getContext().setAuditEvent(mostRecentAuditEvent
);
536 TaxonBase taxon
= taxonDao
.findByUuid(sphingidae
);
537 assert taxon
!= null : "taxon cannot be null";
539 AuditEventRecord
<TaxonBase
> auditEvent
= taxonDao
.getNextAuditEvent(taxon
);
540 assertNull("getNextAuditEvent should return null as there no more audit events after the current one",auditEvent
);
547 public void testAddChild() throws Exception
{
548 Taxon parent
= (Taxon
)taxonDao
.findByUuid(acherontiaLachesis
);
549 assert parent
!= null : "taxon cannot be null";
550 Taxon child
= Taxon
.NewInstance(null, null);
551 child
.setTitleCache("Acherontia lachesis diehli Eitschberger, 2003", true);
552 child
.addTaxonRelation(parent
, TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(),null, null);
553 taxonDao
.save(child
);
559 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
560 public void testFind() {
561 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontiaLachesis
);
562 assert taxon
!= null : "taxon cannot be null";
564 assertEquals("getTaxonomicChildrenCount should return 1 in this view",1,taxon
.getTaxonomicChildrenCount());
565 assertEquals("getRelationsToThisTaxon should contain 1 TaxonRelationship in this view",1,taxon
.getRelationsToThisTaxon().size());
569 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
570 public void testFindInPreviousView() {
571 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
572 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontiaLachesis
);
573 assert taxon
!= null : "taxon cannot be null";
575 assertEquals("getTaxonomicChildrenCount should return 0 in this view",0,taxon
.getTaxonomicChildrenCount());
576 assertTrue("getRelationsToThisTaxon should contain 0 TaxonRelationship in this view",taxon
.getRelationsToThisTaxon().isEmpty());
580 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
581 public void testGetRelations() {
582 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontiaLachesis
);
583 assert taxon
!= null : "taxon cannot be null";
585 List
<String
> propertyPaths
= new ArrayList
<String
>();
586 propertyPaths
.add("fromTaxon");
587 propertyPaths
.add("fromTaxon.name");
589 List
<OrderHint
> orderHints
= new ArrayList
<OrderHint
>();
590 orderHints
.add(new OrderHint("relatedFrom.titleCache", SortOrder
.ASCENDING
));
592 List
<TaxonRelationship
> taxonRelations
= taxonDao
.getRelatedTaxa(taxon
, TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(), null, null,orderHints
,propertyPaths
);
593 assertNotNull("getRelatedTaxa should return a list", taxonRelations
);
594 assertEquals("there should be one TaxonRelationship in the list in the current view",1,taxonRelations
.size());
595 assertTrue("TaxonRelationship.relatedFrom should be initialized",Hibernate
.isInitialized(taxonRelations
.get(0).getFromTaxon()));
596 assertTrue("TaxonRelationship.relatedFrom.name should be initialized",Hibernate
.isInitialized(taxonRelations
.get(0).getFromTaxon().getName()));
600 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
601 public void testCountRelations() {
602 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontiaLachesis
);
603 assert taxon
!= null : "taxon cannot be null";
604 assertEquals("countRelatedTaxa should return 1 in the current view",1, taxonDao
.countRelatedTaxa(taxon
,TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN()));
608 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
609 public void testGetRelationsInPreviousView() {
610 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
611 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontiaLachesis
);
612 assert taxon
!= null : "taxon cannot be null";
614 List
<String
> propertyPaths
= new ArrayList
<String
>();
615 propertyPaths
.add("relatedFrom");
616 propertyPaths
.add("relatedFrom.name");
618 List
<OrderHint
> orderHints
= new ArrayList
<OrderHint
>();
619 orderHints
.add(new OrderHint("relatedFrom.titleCache", SortOrder
.ASCENDING
));
621 List
<TaxonRelationship
> taxonRelations
= taxonDao
.getRelatedTaxa(taxon
, TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN(), null, null,orderHints
,propertyPaths
);
622 assertNotNull("getRelatedTaxa should return a list",taxonRelations
);
623 assertTrue("there should be no TaxonRelationships in the list in the prior view",taxonRelations
.isEmpty());
627 @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
628 public void testCountRelationsInPreviousView() {
629 AuditEventContextHolder
.getContext().setAuditEvent(previousAuditEvent
);
630 Taxon taxon
= (Taxon
)taxonDao
.findByUuid(acherontiaLachesis
);
631 assert taxon
!= null : "taxon cannot be null";
632 assertEquals("countRelatedTaxa should return 0 in the current view",0, taxonDao
.countRelatedTaxa(taxon
,TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN()));