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.
9 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.name
;
11 import static org
.junit
.Assert
.assertEquals
;
12 import static org
.junit
.Assert
.assertFalse
;
13 import static org
.junit
.Assert
.assertNotNull
;
14 import static org
.junit
.Assert
.assertNull
;
15 import static org
.junit
.Assert
.assertTrue
;
17 import java
.io
.FileNotFoundException
;
18 import java
.util
.Arrays
;
19 import java
.util
.HashSet
;
20 import java
.util
.Iterator
;
21 import java
.util
.List
;
22 import java
.util
.Optional
;
24 import java
.util
.UUID
;
26 import org
.junit
.Before
;
27 import org
.junit
.Test
;
28 import org
.unitils
.dbunit
.annotation
.DataSet
;
29 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
33 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationship
;
34 import eu
.etaxonomy
.cdm
.model
.name
.IBotanicalName
;
35 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
36 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
37 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
38 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
39 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameFactory
;
40 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationBase
;
41 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
42 import eu
.etaxonomy
.cdm
.persistence
.dao
.name
.IHomotypicalGroupDao
;
43 import eu
.etaxonomy
.cdm
.persistence
.dao
.name
.ITaxonNameDao
;
44 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonDao
;
45 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNameParts
;
46 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
47 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
.SortOrder
;
48 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
49 import eu
.etaxonomy
.cdm
.test
.integration
.CdmIntegrationTest
;
52 public class TaxonNameDaoHibernateImplTest
extends CdmIntegrationTest
{
55 private ITaxonNameDao taxonNameDao
;
58 private ITaxonDao taxonDao
;
61 private IHomotypicalGroupDao homotypicalGroupDao
;
63 private UUID cryptocoryneGriffithiiUuid
;
64 private UUID acherontiaUuid
;
65 private UUID acherontiaLachesisUuid
;
66 private UUID atroposUuid
;
70 cryptocoryneGriffithiiUuid
= UUID
.fromString("497a9955-5c5a-4f2b-b08c-2135d336d633");
71 acherontiaUuid
= UUID
.fromString("c2cab2ad-3e3a-47b8-8aa8-d9e1c0857647");
72 acherontiaLachesisUuid
= UUID
.fromString("7969821b-a2cf-4d01-95ec-6a5ed0ca3f69");
73 // Atropos Agassiz, 1846
74 atroposUuid
= UUID
.fromString("27004fcc-14d4-47d4-a3e1-75750fdb5b79");
78 public void testGetHybridRelationships() {
79 IBotanicalName cryptocoryneGriffithii
= taxonNameDao
.findByUuid(cryptocoryneGriffithiiUuid
);
80 assert cryptocoryneGriffithii
!= null : "name must exist";
82 List
<HybridRelationship
> result
= taxonNameDao
.getHybridNames(cryptocoryneGriffithii
, null, null, null,null,null);
84 assertNotNull("getHybridNames should return a list",result
);
85 assertFalse("the list should not be empty", result
.isEmpty());
86 assertEquals("getHybridNames should return 1 HybridRelationship instance",1,result
.size());
90 public void testCountHybridRelationships() {
91 IBotanicalName cryptocoryneGriffithii
= taxonNameDao
.findByUuid(cryptocoryneGriffithiiUuid
);
92 assert cryptocoryneGriffithii
!= null : "name must exist";
94 int count
= taxonNameDao
.countHybridNames(cryptocoryneGriffithii
, null);
96 assertEquals("countHybridNames should return 1",1,count
);
100 public void testGetNameRelationships() {
101 TaxonName acherontia
= taxonNameDao
.findByUuid(acherontiaUuid
);
102 assert acherontia
!= null : "name must exist";
104 List
<NameRelationship
> result
= taxonNameDao
.getNameRelationships(acherontia
, NameRelationship
.Direction
.relatedFrom
, null, null,null,null, null);
106 assertNotNull("getRelatedNames should return a list",result
);
107 assertFalse("the list should not be empty", result
.isEmpty());
108 assertEquals("getRelatedNames should return 1 NameRelationship instance",1,result
.size());
110 // testing inverted direction
111 TaxonName atropos
= taxonNameDao
.findByUuid(atroposUuid
);
112 assert atropos
!= null : "name must exist";
114 result
= taxonNameDao
.getNameRelationships(atropos
, NameRelationship
.Direction
.relatedTo
, null, null,null,null, null);
116 assertNotNull("getRelatedNames should return a list",result
);
117 assertFalse("the list should not be empty", result
.isEmpty());
118 assertEquals("getRelatedNames should return 2 NameRelationship instance",2,result
.size());
120 result
= taxonNameDao
.getNameRelationships(null, null, null, null,null,null, null);
122 assertNotNull("getRelatedNames should return a list",result
);
123 assertFalse("the list should not be empty", result
.isEmpty());
124 assertEquals("getRelatedNames should return all 2 NameRelationship instance",2,result
.size());
128 public void testCountNameRelationships() {
129 TaxonName acherontia
= taxonNameDao
.findByUuid(acherontiaUuid
);
130 assert acherontia
!= null : "name must exist";
132 int count
= taxonNameDao
.countNameRelationships(acherontia
, NameRelationship
.Direction
.relatedFrom
, null);
134 assertEquals("countRelatedNames should return 1",1,count
);
136 // testing inverted direction
137 TaxonName atropos
= taxonNameDao
.findByUuid(atroposUuid
);
138 assert atropos
!= null : "name must exist";
140 count
= taxonNameDao
.countNameRelationships(atropos
, NameRelationship
.Direction
.relatedTo
, null);
142 assertEquals("countRelatedNames should return 2",2,count
);
146 public void testGetTypeDesignations() {
147 TaxonName acherontiaLachesis
= taxonNameDao
.findByUuid(acherontiaLachesisUuid
);
148 assert acherontiaLachesis
!= null : "name must exist";
150 @SuppressWarnings("rawtypes")
151 List
<TypeDesignationBase
> result1
= taxonNameDao
.getTypeDesignations(acherontiaLachesis
, null, null, null, null, null);
153 assertNotNull("getTypeDesignations should return a list",result1
);
154 assertFalse("the list should not be empty", result1
.isEmpty());
155 assertEquals("getTypeDesignations should return 1 TypeDesignationBase instance",1,result1
.size());
157 List
<SpecimenTypeDesignation
> result2
= taxonNameDao
.getTypeDesignations(acherontiaLachesis
, SpecimenTypeDesignation
.class, null, null, null, null);
159 assertNotNull("getTypeDesignations should return a list",result2
);
160 assertFalse("the list should not be empty", result2
.isEmpty());
161 assertEquals("getTypeDesignations should return 1 TypeDesignationBase instance",1,result2
.size());
165 public void testCountTypeDesignations() {
166 TaxonName acherontiaLachesis
= taxonNameDao
.findByUuid(acherontiaLachesisUuid
);
167 assert acherontiaLachesis
!= null : "name must exist";
169 long count
= taxonNameDao
.countTypeDesignations(acherontiaLachesis
, null);
171 assertEquals("countTypeDesignations should return 1",1,count
);
175 public void testSearchNames() {
176 List
<TaxonName
> result
= taxonNameDao
.searchNames("Atropos", null, null, null, Rank
.GENUS(), null, null, null, null);
178 assertNotNull("searchNames should return a list",result
);
179 assertFalse("the list should not be empty", result
.isEmpty());
180 assertEquals("searchNames should return 3 TaxonName instances",3,result
.size());
184 public void testFindTaxonNameParts_genusOrUninomial() {
185 Integer pageSize
= 10;
186 Integer pageIndex
= 0;
188 List
<TaxonNameParts
> resuls
= taxonNameDao
.findTaxonNameParts(
189 Optional
.of("Atropos"), null, null, null,
191 pageSize
, pageIndex
, Arrays
.asList(new OrderHint("genusOrUninomial", SortOrder
.ASCENDING
)));
193 assertNotNull("searchNames should return a list",resuls
);
194 assertFalse(resuls
.isEmpty());
195 assertEquals(3, resuls
.size());
199 public void testFindTaxonNameParts_genusOrUninomial_wildcard() {
200 Integer pageSize
= 10;
201 Integer pageIndex
= 0;
203 List
<TaxonNameParts
> results
= taxonNameDao
.findTaxonNameParts(
204 Optional
.of("Atro*"), null, null, null,
206 pageSize
, pageIndex
, Arrays
.asList(new OrderHint("genusOrUninomial", SortOrder
.ASCENDING
)));
208 assertNotNull(results
);
209 assertFalse(results
.isEmpty());
210 assertEquals(3, results
.size());
212 TaxonName n_atropos_agassiz
= taxonNameDao
.load(atroposUuid
);
213 results
= taxonNameDao
.findTaxonNameParts(
214 Optional
.of("Atro*"), null, null, null,
215 Rank
.GENUS(), Arrays
.asList(n_atropos_agassiz
.getUuid()),
216 pageSize
, pageIndex
, Arrays
.asList(new OrderHint("genusOrUninomial", SortOrder
.ASCENDING
)));
218 assertNotNull(results
);
219 assertFalse(results
.isEmpty());
220 assertEquals(2, results
.size());
222 results
= taxonNameDao
.findTaxonNameParts(
223 Optional
.of("Atro*"), null, null, null,
224 Rank
.SPECIES(), null,
225 pageSize
, pageIndex
, Arrays
.asList(new OrderHint("genusOrUninomial", SortOrder
.ASCENDING
)));
227 assertNotNull(results
);
228 assertTrue(results
.isEmpty());
232 public void testFindTaxonNameParts_rankSpecies() {
233 Integer pageSize
= 10;
234 Integer pageIndex
= 0;
236 // Manduca chinchilla
237 // Manduca bergarmatipes
238 List
<TaxonNameParts
> results
= taxonNameDao
.findTaxonNameParts(
239 Optional
.of("Manduca"), null, Optional
.of("*"), null,
240 Rank
.SPECIES(), null,
241 pageSize
, pageIndex
, Arrays
.asList(new OrderHint("specificEpithet", SortOrder
.ASCENDING
)));
243 assertEquals(3, results
.size());
244 assertEquals("afflicta", results
.get(0).getSpecificEpithet());
245 assertEquals("bergarmatipes", results
.get(1).getSpecificEpithet());
246 assertEquals("chinchilla", results
.get(2).getSpecificEpithet());
248 results
= taxonNameDao
.findTaxonNameParts(
249 Optional
.of("Manduca"), null, Optional
.of("chin*"), null,
250 Rank
.SPECIES(), null,
251 pageSize
, pageIndex
, null);
253 assertEquals(1, results
.size());
254 assertEquals("chinchilla", results
.get(0).getSpecificEpithet());
258 public void testFindTaxonNameParts_rankBelowSpecies() {
260 Integer pageSize
= 10;
261 Integer pageIndex
= 0;
262 // Cryptocoryne x purpurea nothovar borneoensis
263 // Cryptocoryne cordata var. zonata
264 List
<TaxonNameParts
> results
= taxonNameDao
.findTaxonNameParts(
265 Optional
.of("Cryptocoryne"), null, null, Optional
.of("borneo*"),
266 Rank
.VARIETY(), null,
267 pageSize
, pageIndex
, Arrays
.asList(new OrderHint("specificEpithet", SortOrder
.ASCENDING
)));
269 assertEquals(1, results
.size());
271 // now also with "infraGenericEpithet is null AND specificEpithet = purpurea"
272 results
= taxonNameDao
.findTaxonNameParts(
273 Optional
.of("Cryptocoryne"), Optional
.empty(), Optional
.of("purpurea"), Optional
.of("borneo*"),
274 Rank
.VARIETY(), null,
275 pageSize
, pageIndex
, Arrays
.asList(new OrderHint("specificEpithet", SortOrder
.ASCENDING
)));
277 assertEquals(1, results
.size());
282 public void testCountNames() {
283 long count
= taxonNameDao
.countNames("Atropos", null, null, null, Rank
.GENUS());
285 assertEquals("countNames should return 3",3,count
);
289 public void testCountNamesByExample() {
290 TaxonName zoologicalName
= TaxonNameFactory
.NewZoologicalInstance(Rank
.GENUS());
291 zoologicalName
.setGenusOrUninomial("Atropos");
292 Set
<String
> includedProperties
= new HashSet
<>();
293 includedProperties
.add("genusOrUninomial");
294 includedProperties
.add("specificEpithet");
295 includedProperties
.add("infraSpecificEpithet");
296 includedProperties
.add("rank");
297 long count
= taxonNameDao
.count(zoologicalName
, includedProperties
);
299 assertEquals("countNames should return 3",3,count
);
304 * This test check for a specific bug (Ticket #686) where the rank of a taxon name base
305 * has no order index (=0)
307 public void testMissingRankOrderIndex() {
308 TaxonName acherontiaLachesis
= taxonNameDao
.findByUuid(acherontiaLachesisUuid
);
311 rank
= Rank
.getRankByLatinName(acherontiaLachesis
.getRank().getLabel());
312 } catch (UnknownCdmTypeException e
) {
316 assertFalse("Rank are equal, level must not be higher", rank
.isHigher(acherontiaLachesis
.getRank()));
317 assertFalse("Rank are equal, level must not be lower", rank
.isLower(acherontiaLachesis
.getRank()));
321 public void testDeleteTaxon(){
322 TaxonName acherontiaLachesis
= CdmBase
.deproxy(taxonNameDao
.findByUuid(cryptocoryneGriffithiiUuid
));
323 @SuppressWarnings("rawtypes")
324 Set
<TaxonBase
> taxonBases
= acherontiaLachesis
.getTaxonBases();
325 HomotypicalGroup group
= acherontiaLachesis
.getHomotypicalGroup();
326 UUID groupUuid
= group
.getUuid();
327 taxonNameDao
.delete(acherontiaLachesis
);
329 @SuppressWarnings("rawtypes")
330 Iterator
<TaxonBase
> taxa
= taxonBases
.iterator();
331 TaxonBase
<?
> taxon
= taxa
.next();
332 UUID taxonUuid
= taxon
.getUuid();
334 acherontiaLachesis
= taxonNameDao
.findByUuid(cryptocoryneGriffithiiUuid
);
335 taxon
= taxonDao
.findByUuid(taxonUuid
);
336 group
= CdmBase
.deproxy(homotypicalGroupDao
.findByUuid(groupUuid
));
337 assertNull("There should be no taxonName with the deleted uuid", acherontiaLachesis
);
338 assertNull("There should be no taxon with the deleted uuid", taxon
);
339 assertNull("There should be no homotypicalGroup with the deleted uuid", group
);
343 * Test if the listener on nomenclatural source also works if the name is retrieved
347 public void testNomenclaturalSourceListener(){
348 UUID uuidAusAus
= UUID
.fromString("05a438d6-065f-49ef-84db-c7dc2c259975");
349 TaxonName ausAus
= taxonNameDao
.findByUuid(uuidAusAus
);
351 assertEquals("Aus aus, Sp. Pl.", ausAus
.getFullTitleCache());
353 ausAus
.getNomenclaturalSource().setCitationMicroReference("23");
354 assertEquals("Here the full cache should show the cache as stored in the database but did not",
355 "Aus aus, Sp. Pl.: 23", ausAus
.getFullTitleCache());
359 public void createTestDataSet() throws FileNotFoundException
{}