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
.name
;
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
;
18 import java
.io
.FileNotFoundException
;
19 import java
.util
.Arrays
;
20 import java
.util
.HashSet
;
21 import java
.util
.Iterator
;
22 import java
.util
.List
;
23 import java
.util
.Optional
;
25 import java
.util
.UUID
;
27 import org
.junit
.Before
;
28 import org
.junit
.Test
;
29 import org
.unitils
.dbunit
.annotation
.DataSet
;
30 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
32 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
33 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
34 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationship
;
35 import eu
.etaxonomy
.cdm
.model
.name
.IBotanicalName
;
36 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
37 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
38 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
39 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
40 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameFactory
;
41 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationBase
;
42 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
43 import eu
.etaxonomy
.cdm
.persistence
.dao
.name
.IHomotypicalGroupDao
;
44 import eu
.etaxonomy
.cdm
.persistence
.dao
.name
.ITaxonNameDao
;
45 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonDao
;
46 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNameParts
;
47 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
48 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
.SortOrder
;
49 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
50 import eu
.etaxonomy
.cdm
.test
.integration
.CdmIntegrationTest
;
53 public class TaxonNameDaoHibernateImplTest
extends CdmIntegrationTest
{
56 ITaxonNameDao taxonNameDao
;
62 IHomotypicalGroupDao homotypicalGroupDao
;
64 private UUID cryptocoryneGriffithiiUuid
;
65 private UUID acherontiaUuid
;
66 private UUID acherontiaLachesisUuid
;
67 private UUID atroposUuid
;
71 cryptocoryneGriffithiiUuid
= UUID
.fromString("497a9955-5c5a-4f2b-b08c-2135d336d633");
72 acherontiaUuid
= UUID
.fromString("c2cab2ad-3e3a-47b8-8aa8-d9e1c0857647");
73 acherontiaLachesisUuid
= UUID
.fromString("7969821b-a2cf-4d01-95ec-6a5ed0ca3f69");
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 List
<TypeDesignationBase
> result1
= taxonNameDao
.getTypeDesignations(acherontiaLachesis
, null, null, null, null, null);
152 assertNotNull("getTypeDesignations should return a list",result1
);
153 assertFalse("the list should not be empty", result1
.isEmpty());
154 assertEquals("getTypeDesignations should return 1 TypeDesignationBase instance",1,result1
.size());
156 List
<SpecimenTypeDesignation
> result2
= taxonNameDao
.getTypeDesignations(acherontiaLachesis
, SpecimenTypeDesignation
.class, null, null, null, null);
158 assertNotNull("getTypeDesignations should return a list",result2
);
159 assertFalse("the list should not be empty", result2
.isEmpty());
160 assertEquals("getTypeDesignations should return 1 TypeDesignationBase instance",1,result2
.size());
164 public void testCountTypeDesignations() {
165 TaxonName acherontiaLachesis
= taxonNameDao
.findByUuid(acherontiaLachesisUuid
);
166 assert acherontiaLachesis
!= null : "name must exist";
168 int count
= taxonNameDao
.countTypeDesignations(acherontiaLachesis
, null);
170 assertEquals("countTypeDesignations should return 1",1,count
);
174 public void testSearchNames() {
175 List
<TaxonName
> result
= taxonNameDao
.searchNames("Atropos", null, null, null, Rank
.GENUS(), null, null, null, null);
177 assertNotNull("searchNames should return a list",result
);
178 assertFalse("the list should not be empty", result
.isEmpty());
179 assertEquals("searchNames should return 3 TaxonName instances",3,result
.size());
183 public void testFindTaxonNameParts_genusOrUninomial() {
184 Integer pageSize
= 10;
185 Integer pageIndex
= 0;
187 List
<TaxonNameParts
> resuls
= taxonNameDao
.findTaxonNameParts(
188 Optional
.of("Atropos"), null, null, null,
190 pageSize
, pageIndex
, Arrays
.asList(new OrderHint("genusOrUninomial", SortOrder
.ASCENDING
)));
192 assertNotNull("searchNames should return a list",resuls
);
193 assertFalse(resuls
.isEmpty());
194 assertEquals(3, resuls
.size());
198 public void testFindTaxonNameParts_genusOrUninomial_wildcard() {
199 Integer pageSize
= 10;
200 Integer pageIndex
= 0;
202 List
<TaxonNameParts
> results
= taxonNameDao
.findTaxonNameParts(
203 Optional
.of("Atro*"), null, null, null,
205 pageSize
, pageIndex
, Arrays
.asList(new OrderHint("genusOrUninomial", SortOrder
.ASCENDING
)));
207 assertNotNull(results
);
208 assertFalse(results
.isEmpty());
209 assertEquals(3, results
.size());
211 results
= taxonNameDao
.findTaxonNameParts(
212 Optional
.of("Atro*"), null, null, null,
214 pageSize
, pageIndex
, Arrays
.asList(new OrderHint("genusOrUninomial", SortOrder
.ASCENDING
)));
216 assertNotNull(results
);
217 assertTrue(results
.isEmpty());
221 public void testFindTaxonNameParts_rankSpecies() {
222 Integer pageSize
= 10;
223 Integer pageIndex
= 0;
225 // Manduca chinchilla
226 // Manduca bergarmatipes
227 List
<TaxonNameParts
> results
= taxonNameDao
.findTaxonNameParts(
228 Optional
.of("Manduca"), null, Optional
.of("*"), null,
230 pageSize
, pageIndex
, Arrays
.asList(new OrderHint("specificEpithet", SortOrder
.ASCENDING
)));
232 assertEquals(3, results
.size());
233 assertEquals("afflicta", results
.get(0).getSpecificEpithet());
234 assertEquals("bergarmatipes", results
.get(1).getSpecificEpithet());
235 assertEquals("chinchilla", results
.get(2).getSpecificEpithet());
237 results
= taxonNameDao
.findTaxonNameParts(
238 Optional
.of("Manduca"), null, Optional
.of("chin*"), null,
240 pageSize
, pageIndex
, null);
242 assertEquals(1, results
.size());
243 assertEquals("chinchilla", results
.get(0).getSpecificEpithet());
247 public void testFindTaxonNameParts_rankBelowSpecies() {
249 Integer pageSize
= 10;
250 Integer pageIndex
= 0;
251 // Cryptocoryne x purpurea nothovar borneoensis
252 // Cryptocoryne cordata var. zonata
253 List
<TaxonNameParts
> results
= taxonNameDao
.findTaxonNameParts(
254 Optional
.of("Cryptocoryne"), null, null, Optional
.of("borneo*"),
256 pageSize
, pageIndex
, Arrays
.asList(new OrderHint("specificEpithet", SortOrder
.ASCENDING
)));
258 assertEquals(1, results
.size());
260 // now also with "infraGenericEpithet is null AND specificEpithet = purpurea"
261 results
= taxonNameDao
.findTaxonNameParts(
262 Optional
.of("Cryptocoryne"), Optional
.empty(), Optional
.of("purpurea"), Optional
.of("borneo*"),
264 pageSize
, pageIndex
, Arrays
.asList(new OrderHint("specificEpithet", SortOrder
.ASCENDING
)));
266 assertEquals(1, results
.size());
271 public void testCountNames() {
272 long count
= taxonNameDao
.countNames("Atropos", null, null, null, Rank
.GENUS());
274 assertEquals("countNames should return 3",3,count
);
278 public void testCountNamesByExample() {
279 TaxonName zoologicalName
= TaxonNameFactory
.NewZoologicalInstance(Rank
.GENUS());
280 zoologicalName
.setGenusOrUninomial("Atropos");
281 Set
<String
> includedProperties
= new HashSet
<String
>();
282 includedProperties
.add("genusOrUninomial");
283 includedProperties
.add("specificEpithet");
284 includedProperties
.add("infraSpecificEpithet");
285 includedProperties
.add("rank");
286 long count
= taxonNameDao
.count(zoologicalName
, includedProperties
);
288 assertEquals("countNames should return 3",3,count
);
293 * This test check for a specific bug (Ticket #686) where the rank of a taxon name base
294 * has no order index (=0)
296 public void testMissingRankOrderIndex() {
297 TaxonName acherontiaLachesis
= taxonNameDao
.findByUuid(acherontiaLachesisUuid
);
300 rank
= Rank
.getRankByName(acherontiaLachesis
.getRank().getLabel());
301 } catch (UnknownCdmTypeException e
) {
305 assertFalse("Rank are equal, level must not be higher", rank
.isHigher(acherontiaLachesis
.getRank()));
306 assertFalse("Rank are equal, level must not be lower", rank
.isLower(acherontiaLachesis
.getRank()));
310 public void testDeleteTaxon(){
311 TaxonName acherontiaLachesis
= taxonNameDao
.findByUuid(UUID
.fromString("497a9955-5c5a-4f2b-b08c-2135d336d633"));
312 HibernateProxyHelper
.deproxy(acherontiaLachesis
, TaxonName
.class);
313 Set
<TaxonBase
> taxonBases
= acherontiaLachesis
.getTaxonBases();
314 HomotypicalGroup group
= acherontiaLachesis
.getHomotypicalGroup();
315 UUID groupUuid
= group
.getUuid();
316 taxonNameDao
.delete(acherontiaLachesis
);
318 Iterator
<TaxonBase
> taxa
= taxonBases
.iterator();
319 TaxonBase taxon
= taxa
.next();
320 UUID taxonUuid
= taxon
.getUuid();
322 //int numbOfTaxa = taxonDao.count(TaxonBase.class);
323 List
<TaxonBase
> taxaList
= taxonDao
.list(100, 0);
325 acherontiaLachesis
= taxonNameDao
.findByUuid(UUID
.fromString("497a9955-5c5a-4f2b-b08c-2135d336d633"));
326 taxon
= taxonDao
.findByUuid(taxonUuid
);
327 group
= homotypicalGroupDao
.findByUuid(groupUuid
);
328 group
= HibernateProxyHelper
.deproxy(group
, HomotypicalGroup
.class);
329 assertNull("There should be no taxonName with the deleted uuid", acherontiaLachesis
);
330 assertNull("There should be no taxon with the deleted uuid", taxon
);
331 assertNull("There should be no homotypicalGroup with the deleted uuid", group
);
336 * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
339 public void createTestDataSet() throws FileNotFoundException
{
340 // TODO Auto-generated method stub