2 * Copyright (C) 2007 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
.description
;
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
.assertTrue
;
17 import java
.util
.ArrayList
;
18 import java
.util
.HashSet
;
19 import java
.util
.Iterator
;
20 import java
.util
.List
;
23 import java
.util
.UUID
;
25 import junit
.framework
.Assert
;
27 import org
.hibernate
.Hibernate
;
28 import org
.junit
.Before
;
29 import org
.junit
.Test
;
30 import org
.unitils
.database
.annotations
.Transactional
;
31 import org
.unitils
.database
.util
.TransactionMode
;
32 import org
.unitils
.dbunit
.annotation
.DataSet
;
33 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
35 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
36 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
37 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
38 import eu
.etaxonomy
.cdm
.model
.description
.AbsenceTerm
;
39 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
40 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
41 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
42 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
43 import eu
.etaxonomy
.cdm
.model
.description
.PresenceTerm
;
44 import eu
.etaxonomy
.cdm
.model
.description
.Scope
;
45 import eu
.etaxonomy
.cdm
.model
.description
.Sex
;
46 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
47 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
48 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
49 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
51 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IDefinedTermDao
;
52 import eu
.etaxonomy
.cdm
.persistence
.dao
.description
.IDescriptionDao
;
53 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonDao
;
54 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
55 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
.SortOrder
;
56 import eu
.etaxonomy
.cdm
.test
.integration
.CdmIntegrationTest
;
57 import eu
.etaxonomy
.cdm
.test
.integration
.CdmTransactionalIntegrationTest
;
60 public class DescriptionDaoHibernateImplTest
extends CdmTransactionalIntegrationTest
{
63 IDescriptionDao descriptionDao
;
66 IDefinedTermDao definedTermDao
;
71 private Set
<NamedArea
> namedAreas
;
72 private Set
<Feature
> features
;
74 private UUID northernAmericaUuid
;
75 private UUID southernAmericaUuid
;
76 private UUID antarcticaUuid
;
80 private UUID taxonSphingidaeUuid
;
82 private static final String
[] TABLE_NAMES
= new String
[] {"DESCRIPTIONBASE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONELEMENTBASE_LANGUAGESTRING", "HOMOTYPICALGROUP","LANGUAGESTRING"
83 , "ORIGINALSOURCEBASE", "REFERENCE", "TAXONBASE", "TAXONNAMEBASE", "HIBERNATE_SEQUENCES" };
87 uuid
= UUID
.fromString("5f3265ed-68ad-4ec3-826f-0d29d25986b9");
89 namedAreas
= new HashSet
<NamedArea
>();
90 northernAmericaUuid
= UUID
.fromString("2757e726-d897-4546-93bd-7951d203bf6f");
91 southernAmericaUuid
= UUID
.fromString("6310b3ba-96f4-4855-bb5b-326e7af188ea");
92 antarcticaUuid
= UUID
.fromString("791b3aa0-54dd-4bed-9b68-56b4680aad0c");
93 taxonSphingidaeUuid
= UUID
.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9");
95 features
= new HashSet
<Feature
>();
97 // loadDataSet(getClass().getClassLoader().getResourceAsStream("eu/etaxonomy/cdm/persistence/dao/hibernate/description/DescriptionDaoHibernateImplTest.xml"));
98 // printDataSet(System.err, TABLE_NAMES);
103 public void testCountByDistribution() {
104 // printDataSet(System.err, TABLE_NAMES);
105 NamedArea northernAmerica
= (NamedArea
)definedTermDao
.findByUuid(northernAmericaUuid
);
106 NamedArea southernAmerica
= (NamedArea
)definedTermDao
.findByUuid(southernAmericaUuid
);
107 NamedArea antarctica
= (NamedArea
)definedTermDao
.findByUuid(antarcticaUuid
);
109 assert northernAmerica
!= null : "term must exist";
110 assert southernAmerica
!= null : "term must exist";
111 assert antarctica
!= null : "term must exist";
113 namedAreas
.add(northernAmerica
);
114 namedAreas
.add(southernAmerica
);
115 namedAreas
.add(antarctica
);
117 int numberOfDescriptions
= descriptionDao
.countDescriptionByDistribution(namedAreas
, null);
118 assertEquals("countDescriptionsByDistribution should return 23",23,numberOfDescriptions
);
122 public void testCountByDistributionWithStatus() {
123 NamedArea northernAmerica
= (NamedArea
)definedTermDao
.findByUuid(northernAmericaUuid
);
124 NamedArea southernAmerica
= (NamedArea
)definedTermDao
.findByUuid(southernAmericaUuid
);
125 NamedArea antarctica
= (NamedArea
)definedTermDao
.findByUuid(antarcticaUuid
);
127 assert northernAmerica
!= null : "term must exist";
128 assert southernAmerica
!= null : "term must exist";
129 assert antarctica
!= null : "term must exist";
131 namedAreas
.add(northernAmerica
);
132 namedAreas
.add(southernAmerica
);
133 namedAreas
.add(antarctica
);
135 int numberOfDescriptions
= descriptionDao
.countDescriptionByDistribution(namedAreas
, PresenceTerm
.PRESENT());
136 assertEquals("countDescriptionsByDistribution should return 20",20,numberOfDescriptions
);
140 public void testGetByDistribution() {
141 NamedArea northernAmerica
= (NamedArea
)definedTermDao
.findByUuid(northernAmericaUuid
);
142 NamedArea southernAmerica
= (NamedArea
)definedTermDao
.findByUuid(southernAmericaUuid
);
143 NamedArea antarctica
= (NamedArea
)definedTermDao
.findByUuid(antarcticaUuid
);
145 assert northernAmerica
!= null : "term must exist";
146 assert southernAmerica
!= null : "term must exist";
147 assert antarctica
!= null : "term must exist";
149 namedAreas
.add(northernAmerica
);
150 namedAreas
.add(southernAmerica
);
151 namedAreas
.add(antarctica
);
153 List
<String
> propertyPaths
= new ArrayList
<String
>();
154 propertyPaths
.add("taxon");
156 List
<OrderHint
> orderHints
= new ArrayList
<OrderHint
>();
157 orderHints
.add(new OrderHint("titleCache",SortOrder
.ASCENDING
));
159 List
<TaxonDescription
> descriptions
= descriptionDao
.searchDescriptionByDistribution(namedAreas
, null, 10,2,orderHints
,propertyPaths
);
160 assertNotNull("searchDescriptionByDistribution should return a List",descriptions
);
161 assertFalse("searchDescriptionsByDistribution should not be empty",descriptions
.isEmpty());
162 assertEquals("searchDescriptionsByDistribution should return 3 elements",3,descriptions
.size());
163 assertTrue("TaxonDescription.taxon should be initialized",Hibernate
.isInitialized(descriptions
.get(0).getTaxon()));
164 assertEquals("Sphingidae Linnaeus, 1758 sec. cate-sphingidae.org should come first","Sphingidae Linnaeus, 1758 sec. cate-sphingidae.org",descriptions
.get(0).getTitleCache());
165 assertEquals("Sphinx Linnaeus, 1758 sec. cate-sphingidae.org should come last","Sphinx Linnaeus, 1758 sec. cate-sphingidae.org",descriptions
.get(2).getTitleCache());
169 public void testGetByDistributionWithStatus() {
170 NamedArea northernAmerica
= (NamedArea
)definedTermDao
.findByUuid(northernAmericaUuid
);
171 NamedArea southernAmerica
= (NamedArea
)definedTermDao
.findByUuid(southernAmericaUuid
);
172 NamedArea antarctica
= (NamedArea
)definedTermDao
.findByUuid(antarcticaUuid
);
174 assert northernAmerica
!= null : "term must exist";
175 assert southernAmerica
!= null : "term must exist";
176 assert antarctica
!= null : "term must exist";
178 namedAreas
.add(northernAmerica
);
179 namedAreas
.add(southernAmerica
);
180 namedAreas
.add(antarctica
);
182 List
<TaxonDescription
> descriptions
= descriptionDao
.searchDescriptionByDistribution(namedAreas
, AbsenceTerm
.ABSENT(), 10,0,null,null);
183 assertNotNull("searchDescriptionByDistribution should return a List",descriptions
);
184 assertFalse("searchDescriptionsByDistribution should not be empty",descriptions
.isEmpty());
185 assertEquals("searchDescriptionsByDistribution should return 3 elements",3,descriptions
.size());
189 public void testCountDescriptionsWithText() {
190 int numberOfDescriptions
= descriptionDao
.countDescriptions(TaxonDescription
.class, null, true, null);
192 assertNotNull("countDescriptions should return a 2",numberOfDescriptions
);
196 public void testListDescriptionsWithText() {
197 List
<OrderHint
> orderHints
= new ArrayList
<OrderHint
>();
198 orderHints
.add(new OrderHint("titleCache",SortOrder
.ASCENDING
));
199 List
<String
> propertyPaths
= new ArrayList
<String
>();
200 propertyPaths
.add("taxon");
201 List
<DescriptionBase
> descriptions
= descriptionDao
.listDescriptions(TaxonDescription
.class, null, true, null,null,null,orderHints
,propertyPaths
);
203 assertNotNull("listDescriptions should return a List",descriptions
);
204 assertFalse("listDescriptions should not be empty", descriptions
.isEmpty());
205 assertEquals("listDescriptions should return 2 descriptions",2,descriptions
.size());
209 public void testCountDescriptionsWithTextAndFeatures() {
210 features
.add(Feature
.ECOLOGY());
211 int numberOfDescriptions
= descriptionDao
.countDescriptions(TaxonDescription
.class, null, true, features
);
213 assertNotNull("countDescriptions should return a 1",numberOfDescriptions
);
217 public void testListDescriptionsWithTextAndFeatures() {
218 assert Feature
.ECOLOGY() != null;
219 features
.add(Feature
.ECOLOGY());
221 List
<DescriptionBase
> descriptions
= descriptionDao
.listDescriptions(TaxonDescription
.class, null, true, features
, null, null, null, null);
223 assertNotNull("listDescriptions should return a List",descriptions
);
224 assertFalse("listDescriptions should not be empty", descriptions
.isEmpty());
225 assertEquals("listDescriptions should return 1 descriptions",1,descriptions
.size());
229 public void testCountDescriptionElements() {
230 DescriptionBase description
= descriptionDao
.findByUuid(uuid
);
231 assert description
!= null : "description must exist";
233 int numberOfDescriptionElements
= descriptionDao
.countDescriptionElements(description
, null, TextData
.class);
235 assertEquals("countDescriptionElements should return 2",2,numberOfDescriptionElements
);
239 public void testGetDescriptionElements() {
240 DescriptionBase description
= descriptionDao
.findByUuid(uuid
);
241 assert description
!= null : "description must exist";
243 List
<String
> propertyPaths
= new ArrayList
<String
>();
244 propertyPaths
.add("multilanguageText");
245 propertyPaths
.add("media");
246 propertyPaths
.add("citation");
247 propertyPaths
.add("feature");
248 propertyPaths
.add("sources");
250 List
<DescriptionElementBase
> elements
= descriptionDao
.getDescriptionElements(description
, null, TextData
.class, null, null,propertyPaths
);
252 for (DescriptionElementBase descElB
: elements
){
253 if (descElB
instanceof TextData
){
254 Map
<Language
, LanguageString
> multiLanguage
= ((TextData
)descElB
).getMultilanguageText();
255 LanguageString defaultString
= multiLanguage
.get(Language
.DEFAULT());
259 Iterator
<DescriptionElementBase
> elements2
= description
.getElements().iterator();
260 while(elements2
.hasNext()){
261 DescriptionElementBase element
= elements2
.next();
262 if (element
instanceof TextData
){
263 Map
<Language
, LanguageString
> multiLanguage
= ((TextData
)element
).getMultilanguageText();
264 LanguageString defaultString
= multiLanguage
.get(Language
.DEFAULT());
265 defaultString
.setText("blablub");
268 elements
= descriptionDao
.getDescriptionElements(description
, null, TextData
.class, null, null, propertyPaths
);
270 DescriptionElementBase element34
= null;
271 for (DescriptionElementBase descElB
: elements
){
272 if (descElB
instanceof TextData
){
273 Map
<Language
, LanguageString
> multiLanguage
= ((TextData
)descElB
).getMultilanguageText();
274 LanguageString defaultString
= multiLanguage
.get(Language
.DEFAULT());
277 if (descElB
.getId() == 34){
282 assertNotNull("getDescriptionElements should return a List", elements
);
283 assertFalse("getDescriptionElements should not be empty",elements
.isEmpty());
284 assertEquals("getDescriptionElement should return 2 elements",2,elements
.size());
285 assertNotNull("Description Element with ID 34 should be part of the list",element34
);
286 assertTrue("DescriptionElement.sources should be initialized",Hibernate
.isInitialized(element34
.getSources()));
287 assertTrue("DescriptionElement.sources should have elements",element34
.getSources().size() > 0);
288 assertTrue("ReferencedEntityBase.citation should be initialized",Hibernate
.isInitialized(element34
.getSources().iterator().next().getCitation()));
289 assertTrue("DescriptionElementBase.feature should be initialized",Hibernate
.isInitialized(element34
.getFeature()));
290 assertTrue("DescriptionElementBase.media should be initialized",Hibernate
.isInitialized(element34
.getMedia()));
291 assertTrue("TextData.multilanguageText should be initialized",Hibernate
.isInitialized(((TextData
)element34
).getMultilanguageText()));
295 public void testCountDescriptionElementsFeature() {
296 features
.add(Feature
.ECOLOGY());
297 DescriptionBase description
= descriptionDao
.findByUuid(uuid
);
298 assert description
!= null : "description must exist";
300 int numberOfDescriptionElements
= descriptionDao
.countDescriptionElements(description
, features
, TextData
.class);
302 assertEquals("countDescriptionElements should return 1",1,numberOfDescriptionElements
);
306 public void testGetDescriptionElementsByFeature() {
307 features
.add(Feature
.ECOLOGY());
308 DescriptionBase description
= descriptionDao
.findByUuid(uuid
);
309 assert description
!= null : "description must exist";
311 List
<DescriptionElementBase
> elements
= descriptionDao
.getDescriptionElements(description
, features
, TextData
.class, null, null,null);
313 assertNotNull("getDescriptionElements should return a List");
314 assertFalse("getDescriptionElements should not be empty",elements
.isEmpty());
315 assertEquals("getDescriptionElement should return 1 elements",1,elements
.size());
319 public void testGetDescriptionElementForTaxon() {
321 Taxon taxonSphingidae
= (Taxon
) taxonDao
.load(taxonSphingidaeUuid
);
322 assert taxonSphingidae
!= null : "taxon must exist";
326 List
<DescriptionElementBase
> elements1
= descriptionDao
.getDescriptionElementForTaxon(
327 taxonSphingidae
, null, null, null, 0, null);
329 assertNotNull("getDescriptionElementForTaxon should return a List", elements1
);
330 assertFalse("getDescriptionElementForTaxon should not be empty",elements1
.isEmpty());
331 assertEquals("getDescriptionElementForTaxon should return 1 elements",1,elements1
.size());
335 List
<DescriptionElementBase
> elements2
= descriptionDao
.getDescriptionElementForTaxon(
336 taxonSphingidae
, null, DescriptionElementBase
.class, null, 0, null);
338 assertNotNull("getDescriptionElementForTaxon should return a List", elements2
);
339 assertTrue("getDescriptionElementForTaxon should be empty",elements2
.isEmpty());
343 List
<Distribution
> elements3
= descriptionDao
.getDescriptionElementForTaxon(
344 taxonSphingidae
, null, Distribution
.class, null, 0, null);
346 assertNotNull("getDescriptionElementForTaxon should return a List", elements3
);
347 assertFalse("getDescriptionElementForTaxon should not be empty",elements3
.isEmpty());
348 assertEquals("getDescriptionElementForTaxon should return 1 elements",1,elements3
.size());
353 public void testSaveClonedDescription() {
355 // printDataSet(System.err, new String[]{"TAXONBASE"});
357 Taxon taxon
= Taxon
.NewInstance(null, null);
358 taxon
.setTitleCache("##### created in testSaveClonedDescription()", true);
359 taxonDao
.save(taxon
);
360 commitAndStartNewTransaction(null);
362 TaxonDescription description
= TaxonDescription
.NewInstance(taxon
);
363 this.descriptionDao
.saveOrUpdate(description
);
364 // TaxonDescription clonedDescription = (TaxonDescription)description.clone();
365 // this.descriptionDao.saveOrUpdate(clonedDescription);
366 // printDataSet(System.err, new String[]{"TAXONBASE"});
374 public void testSaveScope(){
375 int n1
= this.descriptionDao
.count();
376 Taxon taxon
= Taxon
.NewInstance(null, null);
377 TaxonDescription description
= TaxonDescription
.NewInstance(taxon
);
378 this.taxonDao
.save(taxon
);
379 int n2
= this.descriptionDao
.count();
380 Assert
.assertEquals(1, n2
-n1
);
382 Sex scope
= Sex
.FEMALE();
383 description
.addScope(scope
);
385 this.descriptionDao
.saveOrUpdate(description
);
390 public void testListTaxonDescriptionWithMarker(){
391 Taxon taxon
= (Taxon
)this.taxonDao
.findByUuid(UUID
.fromString("b04cc9cb-2b4a-4cc4-a94a-3c93a2158b06"));
392 Set
<Scope
> scopes
= null;
393 Set
<NamedArea
> geographicalScope
= null;
394 Integer pageSize
= null;
395 Integer pageNumber
= null;
396 List
<String
> propertyPaths
= null;
399 MarkerType completeMarkerType
= (MarkerType
)this.definedTermDao
.findByUuid(UUID
.fromString("b4b1b2ab-89a8-4ce6-8110-d60b8b1bc433")); //Marker "complete"
400 Assert
.assertNotNull("MarkerType for 'complete' should exist", completeMarkerType
);
401 Set
<MarkerType
> markerTypes
= new HashSet
<MarkerType
>();
402 markerTypes
.add(completeMarkerType
);
403 int n1
= this.descriptionDao
.countTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
);
404 Assert
.assertEquals("There should be 1 description marked 'complete'", 1, n1
);
405 List
<TaxonDescription
> descriptions
= this.descriptionDao
.getTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
406 Assert
.assertEquals("There should be 1 description marked 'complete'", 1, descriptions
.size());
409 MarkerType isDoubtfulMarkerType
= (MarkerType
)this.definedTermDao
.findByUuid(UUID
.fromString("b51325c8-05fe-421a-832b-d86fc249ef6e")); //Marker "doubtful"
410 Assert
.assertNotNull("MarkerType for 'doubtful' should exist", isDoubtfulMarkerType
);
411 markerTypes
= new HashSet
<MarkerType
>(); //reset
412 markerTypes
.add(isDoubtfulMarkerType
);
413 int n2
= this.descriptionDao
.countTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
);
414 Assert
.assertEquals("There should be no description marked 'doubtful'", 0, n2
);
415 descriptions
= this.descriptionDao
.getTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
416 Assert
.assertEquals("There should be 0 description marked 'doubtful'", 0, descriptions
.size());
419 UUID uuidImported
= UUID
.fromString("96878790-4ceb-42a2-9738-a2242079b679");
420 MarkerType importedMarkerType
= (MarkerType
)this.definedTermDao
.findByUuid(uuidImported
);
421 Assert
.assertNotNull("MarkerType for 'imported' should exist", completeMarkerType
);
422 markerTypes
= new HashSet
<MarkerType
>();
423 markerTypes
.add(importedMarkerType
);
424 int n3
= this.descriptionDao
.countTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
);
425 Assert
.assertEquals("There should be no description marked 'imported' as true", 0, n3
);
426 descriptions
= this.descriptionDao
.getTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
427 Assert
.assertEquals("There should be no description marked 'imported' as true", 0, descriptions
.size());
429 descriptions
= this.descriptionDao
.getTaxonDescriptions(taxon
, scopes
, geographicalScope
, markerTypes
, pageSize
, pageNumber
, propertyPaths
);
430 Assert
.assertEquals("There should be 1 description", 1, descriptions
.size());
431 TaxonDescription desc
= descriptions
.iterator().next();
432 boolean hasMarkerImportedAsFalse
= desc
.hasMarker(importedMarkerType
, false);
433 Assert
.assertTrue("The only description should have a negative marker on 'imported'", hasMarkerImportedAsFalse
);