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
.api
.service
;
11 import static org
.junit
.Assert
.assertEquals
;
12 import static org
.junit
.Assert
.assertNotNull
;
13 import static org
.junit
.Assert
.assertSame
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.junit
.Assert
;
17 import org
.junit
.Ignore
;
18 import org
.junit
.Test
;
19 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
21 import eu
.etaxonomy
.cdm
.model
.common
.DescriptionElementSource
;
22 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
23 import eu
.etaxonomy
.cdm
.model
.common
.OrderedTermVocabulary
;
24 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
25 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
26 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
27 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
28 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
29 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationshipType
;
30 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
31 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
32 import eu
.etaxonomy
.cdm
.model
.name
.NameTypeDesignation
;
33 import eu
.etaxonomy
.cdm
.model
.name
.NameTypeDesignationStatus
;
34 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
35 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
36 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
37 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
38 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
39 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
40 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
41 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationBase
;
42 import eu
.etaxonomy
.cdm
.model
.occurrence
.Specimen
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
44 import eu
.etaxonomy
.cdm
.test
.integration
.CdmIntegrationTest
;
45 import eu
.etaxonomy
.cdm
.test
.integration
.CdmTransactionalIntegrationTest
;
51 public class NameServiceImplTest
extends CdmTransactionalIntegrationTest
{
52 private static final Logger logger
= Logger
.getLogger(NameServiceImplTest
.class);
55 private INameService nameService
;
58 private IOccurrenceService occurrenceService
;
61 private ITaxonService taxonService
;
64 private ITermService termService
;
67 /* ******************** TESTS ********************************************/
70 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#setDao(eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao)}.
73 public void testSetDao() {
74 // Assert.assertNotNull(((NameServiceImpl)nameService).dao);
78 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#setVocabularyDao(eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao)}.
81 public void testSetVocabularyDao() {
82 // Assert.assertNotNull(( (NameServiceImpl)nameService).vocabularyDao);
86 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#getNamesByName(java.lang.String)}.
89 public void testGetNamesByName() {
90 logger
.warn("Not yet implemented");
94 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#getTaxonNameByUuid(java.util.UUID)}.
97 public void testGetTaxonNameByUuid() {
98 logger
.warn("Not yet implemented");
102 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#saveTaxonName(eu.etaxonomy.cdm.model.name.TaxonNameBase)}.
105 public void testSaveTaxonName() {
106 logger
.warn("Not yet implemented");
110 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#saveTaxonNameAll(java.util.Collection)}.
113 public void testSaveTaxonNameAll() {
114 logger
.warn("Not yet implemented");
118 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#removeTaxon(eu.etaxonomy.cdm.model.name.TaxonNameBase)}.
121 public void testRemoveTaxon() {
122 logger
.warn("Not yet implemented");
126 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#getAllNames(int, int)}.
129 public void testGetAllNames() {
130 logger
.warn("Not yet implemented");
134 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#getRankVocabulary()}.
137 @Ignore //FIXME assertSame does not work yet
138 public void testGetRankVocabulary() {
139 //TODO move test to vocabulary service
140 OrderedTermVocabulary
<Rank
> rankVocabulary
= nameService
.getRankVocabulary();
141 assertNotNull(rankVocabulary
);
142 assertEquals(66, rankVocabulary
.size());
143 Rank highestRank
= rankVocabulary
.getHighestTerm();
144 assertEquals(Rank
.EMPIRE(), highestRank
);
145 assertEquals(Rank
.DOMAIN(), rankVocabulary
.getNextLowerTerm(highestRank
));
146 assertSame(Rank
.EMPIRE(), highestRank
);
147 assertSame(Rank
.DOMAIN(), rankVocabulary
.getNextLowerTerm(highestRank
));
151 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#getTypeDesignationVocabulary()}.
154 @Ignore //not yet correctly implemented
155 public void testGetTypeDesignationVocabulary() {
156 //TODO move test to vocabulary service
157 OrderedTermVocabulary
<SpecimenTypeDesignationStatus
> typeDesignationVocabulary
=
158 nameService
.getSpecimenTypeDesignationVocabulary();
159 assertNotNull(typeDesignationVocabulary
);
160 assertEquals(20, typeDesignationVocabulary
.size());
161 SpecimenTypeDesignationStatus highestType
= typeDesignationVocabulary
.getHighestTerm();
162 assertEquals(SpecimenTypeDesignationStatus
.EPITYPE(), highestType
);
163 assertEquals(SpecimenTypeDesignationStatus
.HOLOTYPE(), typeDesignationVocabulary
.getNextLowerTerm(highestType
));
164 assertSame(SpecimenTypeDesignationStatus
.EPITYPE(), highestType
);
165 assertSame(SpecimenTypeDesignationStatus
.HOLOTYPE(), typeDesignationVocabulary
.getNextLowerTerm(highestType
));
169 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#generateTitleCache()}.
172 public void testGenerateTitleCache() {
173 logger
.warn("Not yet implemented");
177 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#generateTitleCache()}.
180 public void testDeleteTaxonNameBaseWithNameRelations() {
181 final String
[] tableNames
= new String
[]{"TaxonNameBase","NameRelationship","HybridRelationship","DescriptionBase","NomenclaturalStatus","TaxonBase","SpecimenOrObservationBase","OriginalSourceBase","DescriptionElementBase"};
183 NonViralName
<?
> name1
= BotanicalName
.NewInstance(getSpeciesRank());
184 name1
.setTitleCache("Name1", true);
185 TaxonNameBase
<?
,?
> nameWithBasionym
= BotanicalName
.NewInstance(getSpeciesRank());
186 nameWithBasionym
.setTitleCache("nameWithBasionym", true);
188 NameRelationshipType nameRelType
= (NameRelationshipType
)termService
.find(NameRelationshipType
.BASIONYM().getUuid());
189 name1
.addRelationshipToName(nameWithBasionym
,nameRelType
, null, null, null);
190 // nameWithBasionym.addBasionym(name1);
191 nameService
.save(name1
);
192 commitAndStartNewTransaction(tableNames
);
195 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
196 nameService
.delete(name1
);
197 Assert
.fail("Delete should throw an error as long as name relationships exist.");
198 } catch (Exception e
) {
199 if (e
.getMessage().startsWith("Name can't be deleted as it is used in name relationship")){
201 endTransaction(); //exception rolls back transaction!
202 printDataSet(System
.out
, tableNames
);
203 startNewTransaction();
205 Assert
.fail("Unexpected error occurred when trying to delete taxon name: " + e
.getMessage());
208 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
209 Assert
.assertNotNull("Name should still be in database",name1
);
210 nameWithBasionym
= ((NameRelationship
)name1
.getNameRelations().iterator().next()).getToName();
211 nameWithBasionym
.removeBasionyms();
212 nameService
.delete(name1
); //should throw now exception
213 commitAndStartNewTransaction(tableNames
);
214 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
215 Assert
.assertNull("Name should not be in database anymore",name1
);
221 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#generateTitleCache()}.
224 public void testDeleteTaxonNameBaseConfiguratorWithNameRelations() {
225 final String
[] tableNames
= new String
[]{"TaxonNameBase","NameRelationship","HybridRelationship"};
227 NonViralName
<?
> name1
= BotanicalName
.NewInstance(getSpeciesRank());
228 name1
.setTitleCache("Name1", true);
229 TaxonNameBase
<?
,?
> nameWithBasionym
= BotanicalName
.NewInstance(getSpeciesRank());
230 nameWithBasionym
.setTitleCache("nameWithBasionym", true);
232 NameRelationshipType nameRelType
= (NameRelationshipType
)termService
.find(NameRelationshipType
.BASIONYM().getUuid());
233 name1
.addRelationshipToName(nameWithBasionym
,nameRelType
, null, null, null);
234 nameService
.save(name1
);
235 commitAndStartNewTransaction(tableNames
);
236 NameDeletionConfigurator config
= new NameDeletionConfigurator();
238 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
239 nameService
.delete(name1
, config
);
240 Assert
.fail("Delete should throw an error as long as name relationships exist.");
241 } catch (Exception e
) {
242 if (e
.getMessage().startsWith("Name can't be deleted as it is used in name relationship")){
244 endTransaction(); //exception rolls back transaction!
245 printDataSet(System
.out
, tableNames
);
246 startNewTransaction();
248 Assert
.fail("Unexpected error occurred when trying to delete taxon name: " + e
.getMessage());
251 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
252 Assert
.assertNotNull("Name should still be in database",name1
);
254 //ignore is basionym for
255 config
.setIgnoreIsBasionymFor(true);
257 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
258 nameService
.delete(name1
, config
);
259 commitAndStartNewTransaction(tableNames
);
260 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
261 Assert
.assertNull("Name should not be in database anymore",name1
);
262 } catch (Exception e
) {
263 Assert
.fail("Delete should not throw an error for .");
268 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#generateTitleCache()}.
271 public void testDeleteTaxonNameBaseConfiguratorWithNameRelationsAll() {
272 final String
[] tableNames
= new String
[]{"TaxonNameBase","NameRelationship","HybridRelationship"};
274 NonViralName
<?
> name1
= BotanicalName
.NewInstance(getSpeciesRank());
275 name1
.setTitleCache("Name1", true);
276 TaxonNameBase
<?
,?
> nameWithBasionym
= BotanicalName
.NewInstance(getSpeciesRank());
277 nameWithBasionym
.setTitleCache("nameWithBasionym", true);
279 NameRelationshipType nameRelType
= (NameRelationshipType
)termService
.find(NameRelationshipType
.BASIONYM().getUuid());
280 name1
.addRelationshipToName(nameWithBasionym
,nameRelType
, null, null, null);
281 nameService
.save(name1
);
282 commitAndStartNewTransaction(tableNames
);
283 NameDeletionConfigurator config
= new NameDeletionConfigurator();
285 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
286 nameService
.delete(name1
, config
);
287 Assert
.fail("Delete should throw an error as long as name relationships exist.");
288 } catch (Exception e
) {
289 if (e
.getMessage().startsWith("Name can't be deleted as it is used in name relationship")){
291 endTransaction(); //exception rolls back transaction!
292 printDataSet(System
.out
, tableNames
);
293 startNewTransaction();
295 Assert
.fail("Unexpected error occurred when trying to delete taxon name: " + e
.getMessage());
298 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
299 Assert
.assertNotNull("Name should still be in database",name1
);
301 //ignore all name relationships
302 config
.setRemoveAllNameRelationships(true);
304 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
305 nameService
.delete(name1
, config
);
306 commitAndStartNewTransaction(tableNames
);
307 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
308 Assert
.assertNull("Name should not be in database anymore",name1
);
309 } catch (Exception e
) {
310 Assert
.fail("Delete should not throw an error for .");
315 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#generateTitleCache()}.
318 public void testDeleteTaxonNameBaseConfiguratorWithHasBasionym() {
319 final String
[] tableNames
= new String
[]{"TaxonNameBase","NameRelationship","HybridRelationship"};
321 NonViralName
<?
> name1
= BotanicalName
.NewInstance(getSpeciesRank());
322 name1
.setTitleCache("Name1", true);
323 TaxonNameBase
<?
,?
> basionym
= BotanicalName
.NewInstance(getSpeciesRank());
324 basionym
.setTitleCache("basionym", true);
326 NameRelationshipType nameRelType
= (NameRelationshipType
)termService
.find(NameRelationshipType
.BASIONYM().getUuid());
327 basionym
.addRelationshipToName(name1
,nameRelType
, null, null, null);
328 nameService
.save(name1
);
329 commitAndStartNewTransaction(tableNames
);
330 NameDeletionConfigurator config
= new NameDeletionConfigurator();
331 config
.setIgnoreHasBasionym(false);
333 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
334 nameService
.delete(name1
, config
);
335 Assert
.fail("Delete should throw an error as long as name relationships exist.");
336 } catch (Exception e
) {
337 if (e
.getMessage().startsWith("Name can't be deleted as it is used in name relationship")){
339 endTransaction(); //exception rolls back transaction!
340 printDataSet(System
.out
, tableNames
);
341 startNewTransaction();
343 Assert
.fail("Unexpected error occurred when trying to delete taxon name: " + e
.getMessage());
346 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
347 Assert
.assertNotNull("Name should still be in database",name1
);
349 //ignore has basionym
350 config
.setIgnoreHasBasionym(true);
352 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
353 nameService
.delete(name1
, config
);
354 commitAndStartNewTransaction(tableNames
);
355 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
356 Assert
.assertNull("Name should not be in database anymore",name1
);
357 } catch (Exception e
) {
358 Assert
.fail("Delete should not throw an error for .");
364 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#generateTitleCache()}.
367 public void testDeleteTaxonNameBaseWithHybridRelations() {
368 final String
[] tableNames
= new String
[]{"TaxonNameBase","NameRelationship","HybridRelationship"};
370 NonViralName
<?
> name1
= BotanicalName
.NewInstance(getSpeciesRank());
371 name1
.setTitleCache("Name1", true);
372 NonViralName
<?
> parent
= BotanicalName
.NewInstance(getSpeciesRank());
373 parent
.setTitleCache("parent", true);
374 NonViralName
<?
> child
= BotanicalName
.NewInstance(getSpeciesRank());
375 child
.setTitleCache("child", true);
377 HybridRelationshipType relType
= (HybridRelationshipType
)termService
.find(HybridRelationshipType
.FIRST_PARENT().getUuid());
378 name1
.addHybridParent(parent
, relType
, null);
379 nameService
.save(name1
);
380 commitAndStartNewTransaction(tableNames
); //otherwise first save is rolled back with following failing delete
384 nameService
.delete(name1
);
385 Assert
.fail("Delete should throw an error as long as hybrid parent exist.");
386 } catch (Exception e
) {
387 if (e
.getMessage().startsWith("Name can't be deleted as it is a child in")){
389 endTransaction(); //exception rolls back transaction!
390 printDataSet(System
.out
, tableNames
);
391 startNewTransaction();
393 Assert
.fail("Unexpected error occurred when trying to delete taxon name: " + e
.getMessage());
396 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
397 Assert
.assertNotNull("Name should still be in database",name1
);
398 name1
.removeHybridParent(parent
);
399 nameService
.delete(name1
); //should throw now exception
400 commitAndStartNewTransaction(tableNames
);
401 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
402 Assert
.assertNull("Name should not be in database anymore",name1
);
405 name1
= BotanicalName
.NewInstance(getSpeciesRank());
406 name1
.addHybridChild(child
, relType
, null);
407 nameService
.save(name1
);
408 commitAndStartNewTransaction(tableNames
);
411 nameService
.delete(name1
);
412 Assert
.fail("Delete should throw an error as long as hybrid child exist.");
413 } catch (Exception e
) {
414 if (e
.getMessage().startsWith("Name can't be deleted as it is a parent in")){
416 endTransaction(); //exception rolls back transaction!
417 startNewTransaction();
419 Assert
.fail("Unexpected error occurred when trying to delete taxon name: " + e
.getMessage());
422 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
423 Assert
.assertNotNull("Name should still be in database",name1
);
424 name1
.removeHybridChild(child
);
425 nameService
.delete(name1
); //should throw now exception
426 commitAndStartNewTransaction(tableNames
);
427 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
428 Assert
.assertNull("Name should not be in database anymore",name1
);
432 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#generateTitleCache()}.
435 public void testDeleteTaxonNameBaseInConcept() {
436 final String
[] tableNames
= new String
[]{"TaxonNameBase","TaxonBase"};
438 NonViralName
<?
> name1
= BotanicalName
.NewInstance(getSpeciesRank());
439 name1
.setTitleCache("Name1", true);
440 TaxonNameBase
<?
,?
> basionym
= BotanicalName
.NewInstance(getSpeciesRank());
441 basionym
.setTitleCache("basionym", true);
443 Taxon taxon
= Taxon
.NewInstance(name1
, null);
444 nameService
.save(name1
);
445 taxonService
.save(taxon
);
446 commitAndStartNewTransaction(tableNames
);
449 nameService
.delete(name1
);
450 Assert
.fail("Delete should throw an error as long as name is used in a concept.");
451 } catch (Exception e
) {
452 if (e
.getMessage().startsWith("Name can't be deleted as it is used in concept")){
454 endTransaction(); //exception rolls back transaction!
455 startNewTransaction();
457 Assert
.fail("Unexpected error occurred when trying to delete taxon name: " + e
.getMessage());
460 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
461 Assert
.assertNotNull("Name should still be in database",name1
);
462 taxon
= (Taxon
)taxonService
.find(taxon
.getUuid());
463 Assert
.assertNotNull("Taxon should still be in database",taxon
);
464 taxon
.setName(basionym
);
465 taxonService
.save(taxon
);
466 nameService
.delete(name1
); //should throw now exception
467 commitAndStartNewTransaction(tableNames
);
468 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
469 Assert
.assertNull("Name should not be in database anymore",name1
);
470 taxon
= (Taxon
)taxonService
.find(taxon
.getUuid());
471 Assert
.assertNotNull("Taxon should still be in database",taxon
);
475 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#generateTitleCache()}.
478 public void testDeleteTaxonNameBaseAsStoredUnder() {
479 final String
[] tableNames
= new String
[]{"TaxonNameBase","SpecimenOrObservationBase"};
481 NonViralName
<?
> name1
= BotanicalName
.NewInstance(getSpeciesRank());
482 name1
.setTitleCache("Name1", true);
483 Specimen specimen
= Specimen
.NewInstance();
484 specimen
.setStoredUnder(name1
);
486 occurrenceService
.save(specimen
);
487 nameService
.save(name1
);
489 commitAndStartNewTransaction(tableNames
);
490 nameService
.delete(name1
);
491 Assert
.fail("Delete should throw an error as long as name is used for specimen#storedUnder.");
492 } catch (Exception e
) {
493 if (e
.getMessage().startsWith("Name can't be deleted as it is used as derivedUnit#storedUnder")){
495 endTransaction(); //exception rolls back transaction!
496 startNewTransaction();
498 Assert
.fail("Unexpected error occurred when trying to delete taxon name: " + e
.getMessage());
501 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
502 Assert
.assertNotNull("Name should still be in database",name1
);
503 specimen
= (Specimen
)occurrenceService
.find(specimen
.getUuid());
504 Assert
.assertNotNull("Specimen should still be in database",name1
);
505 specimen
.setStoredUnder(null);
506 occurrenceService
.saveOrUpdate(specimen
);
507 nameService
.delete(name1
); //should throw now exception
508 commitAndStartNewTransaction(tableNames
);
509 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
510 Assert
.assertNull("Name should not be in database anymore",name1
);
511 specimen
= (Specimen
)occurrenceService
.find(specimen
.getUuid());
512 Assert
.assertNotNull("Specimen should still be in database",specimen
);
514 occurrenceService
.delete(specimen
); //this is to better run this test in the test suit
519 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#generateTitleCache()}.
522 public void testDeleteTaxonNameBaseInSource() {
523 final String
[] tableNames
= new String
[]{"TaxonNameBase","DescriptionBase","TaxonBase","OriginalSourceBase","DescriptionElementBase"};
525 NonViralName
<?
> name1
= BotanicalName
.NewInstance(getSpeciesRank());
526 name1
.setTitleCache("Name1", true);
527 TaxonNameBase
<?
,?
> taxonName
= BotanicalName
.NewInstance(getSpeciesRank());
528 taxonName
.setTitleCache("taxonName", true);
529 Taxon taxon
= Taxon
.NewInstance(taxonName
, null);
531 TaxonDescription taxonDescription
= TaxonDescription
.NewInstance(taxon
);
532 Feature feature
= (Feature
)termService
.find(Feature
.DESCRIPTION().getUuid());
533 Language lang
= (Language
)termService
.find(Language
.DEFAULT().getUuid());
534 TextData textData
= TextData
.NewInstance("Any text", lang
, null);
535 textData
.setFeature(feature
);
536 taxonDescription
.addElement(textData
);
537 DescriptionElementSource source
= DescriptionElementSource
.NewInstance(null, null, name1
, "");
538 textData
.addSource(source
);
539 taxonService
.saveOrUpdate(taxon
);
540 nameService
.save(name1
);
542 commitAndStartNewTransaction(tableNames
);
543 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
544 nameService
.delete(name1
);
545 Assert
.fail("Delete should throw an error as long as name is used in a source.");
546 } catch (Exception e
) {
547 if (e
.getMessage().startsWith("Name can't be deleted as it is used as descriptionElementSource#nameUsedInSource")){
549 endTransaction(); //exception rolls back transaction!
550 startNewTransaction();
552 Assert
.fail("Unexpected error occurred when trying to delete taxon name: " + e
.getMessage());
555 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
556 Assert
.assertNotNull("Name should still be in database",name1
);
557 taxon
= (Taxon
)taxonService
.find(taxon
.getUuid());
558 Assert
.assertNotNull("Taxon should still be in database",name1
);
559 source
= taxon
.getDescriptions().iterator().next().getElements().iterator().next().getSources().iterator().next();
560 source
.setNameUsedInSource(null);
561 taxonService
.saveOrUpdate(taxon
);
562 nameService
.delete(name1
); //should throw now exception
563 commitAndStartNewTransaction(tableNames
);
564 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
565 Assert
.assertNull("Name should not be in database anymore",name1
);
566 taxon
= (Taxon
)taxonService
.find(taxon
.getUuid());
567 Assert
.assertNotNull("Taxon should still be in database",taxon
);
568 source
= taxon
.getDescriptions().iterator().next().getElements().iterator().next().getSources().iterator().next();
569 Assert
.assertNull("Source should not have a nameUsedInSource anymore",source
.getNameUsedInSource());
574 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#generateTitleCache()}.
577 public void testDeleteTaxonNameBaseAsType() {
578 final String
[] tableNames
= new String
[]{"TaxonNameBase","TypeDesignationBase","TypeDesignationBase_TaxonNameBase","TaxonNameBase_TypeDesignationBase"};
580 NonViralName
<?
> name1
= BotanicalName
.NewInstance(getSpeciesRank());
581 name1
.setTitleCache("Name used as type", true);
583 NonViralName
<?
> higherName
= BotanicalName
.NewInstance(getGenusRank());
584 higherName
.setTitleCache("genus name", true);
585 NameTypeDesignationStatus typeStatus
= (NameTypeDesignationStatus
)termService
.find(NameTypeDesignationStatus
.AUTOMATIC().getUuid());
586 boolean addToAllHomotypicNames
= true;
587 higherName
.addNameTypeDesignation(name1
, null, null, null, typeStatus
, addToAllHomotypicNames
);
588 nameService
.save(higherName
);
590 commitAndStartNewTransaction(tableNames
);
591 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
592 nameService
.delete(name1
);
593 Assert
.fail("Delete should throw an error as long as name is used in a source.");
594 } catch (Exception e
) {
595 if (e
.getMessage().startsWith("Name can't be deleted as it is used as a name type")){
597 endTransaction(); //exception rolls back transaction!
598 startNewTransaction();
600 Assert
.fail("Unexpected error occurred when trying to delete taxon name: " + e
.getMessage());
603 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
604 Assert
.assertNotNull("Name should still be in database",name1
);
605 higherName
= (NonViralName
<?
>)nameService
.find(higherName
.getUuid());
606 higherName
.removeTypeDesignation(higherName
.getNameTypeDesignations().iterator().next());
607 nameService
.delete(name1
); //should throw now exception
608 commitAndStartNewTransaction(tableNames
);
609 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
610 Assert
.assertNull("Name should not be in database anymore",name1
);
611 printDataSet(System
.out
, tableNames
);
612 higherName
= (NonViralName
<?
>)nameService
.find(higherName
.getUuid());
613 Assert
.assertNotNull("Higher name should still exist in database",higherName
);
614 Assert
.assertEquals("Higher name should not have type designations anymore",0, higherName
.getTypeDesignations().size());
620 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#generateTitleCache()}.
623 public void testDeleteTaxonNameBase() {
624 final String
[] tableNames
= new String
[]{"TaxonNameBase","NameRelationship","HybridRelationship","DescriptionBase","NomenclaturalStatus","TaxonBase","SpecimenOrObservationBase","OriginalSourceBase","DescriptionElementBase","TypeDesignationBase","TypeDesignationBase_TaxonNameBase","TaxonNameBase_TypeDesignationBase"};
626 NonViralName
<?
> name1
= BotanicalName
.NewInstance(getSpeciesRank());
627 name1
.setTitleCache("Name1", true);
629 //TaxonNameDescription
630 name1
= BotanicalName
.NewInstance(getSpeciesRank());
631 TaxonNameDescription
.NewInstance(name1
);
632 nameService
.save(name1
);
633 commitAndStartNewTransaction(tableNames
);
635 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
636 nameService
.delete(name1
); //should throw now exception
639 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
640 Assert
.assertNull("Name should not be in database anymore",name1
);
642 // printDataSet(System.out, tableNames);
645 //NomenclaturalStatus
646 name1
= BotanicalName
.NewInstance(getSpeciesRank());
647 NomenclaturalStatusType nomStatusType
= (NomenclaturalStatusType
)termService
.find(NomenclaturalStatusType
.ILLEGITIMATE().getUuid());
648 NomenclaturalStatus status
= NomenclaturalStatus
.NewInstance(nomStatusType
);
649 name1
.addStatus(status
);
650 nameService
.save(name1
);
651 commitAndStartNewTransaction(tableNames
);
653 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
654 nameService
.delete(name1
); //should throw now exception
657 // printDataSet(System.out, tableNames);
661 name1
= BotanicalName
.NewInstance(getSpeciesRank());
662 name1
.setTitleCache("Name with type designation", true);
663 SpecimenTypeDesignation typeDesignation
= SpecimenTypeDesignation
.NewInstance();
664 SpecimenTypeDesignationStatus typeStatus
= (SpecimenTypeDesignationStatus
)termService
.find(SpecimenTypeDesignationStatus
.HOLOTYPE().getUuid());
665 typeDesignation
.setTypeStatus(typeStatus
);
666 Specimen specimen
= Specimen
.NewInstance();
667 specimen
.setTitleCache("Type specimen", true);
668 occurrenceService
.save(specimen
);
669 typeDesignation
.setTypeSpecimen(specimen
);
671 name1
.addTypeDesignation(typeDesignation
, true);
672 nameService
.save(name1
);
673 commitAndStartNewTransaction(tableNames
);
674 // printDataSet(System.out, tableNames);
676 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
677 nameService
.delete(name1
); //should throw now exception
680 // printDataSet(System.out, tableNames);
685 * Test method for {@link eu.etaxonomy.cdm.api.service.NameServiceImpl#generateTitleCache()}.
688 @Ignore //Mapping not yet correctly implemented
689 public void testDeleteTaxonNameBaseWithTypeInHomotypicalGroup() {
690 final String
[] tableNames
= new String
[]{"TaxonNameBase","NameRelationship","HybridRelationship","DescriptionBase","NomenclaturalStatus","TaxonBase","SpecimenOrObservationBase","OriginalSourceBase","DescriptionElementBase","TypeDesignationBase","TypeDesignationBase_TaxonNameBase","TaxonNameBase_TypeDesignationBase"};
692 //Type Designations for homotypical group with > 1 names
693 NonViralName
<?
> name1
= BotanicalName
.NewInstance(getSpeciesRank());
694 name1
.setTitleCache("Name1 with type designation", true);
695 NonViralName
<?
> name2
= BotanicalName
.NewInstance(getSpeciesRank());
696 name2
.setTitleCache("Name2 with type designation", true);
697 name2
.setHomotypicalGroup(name1
.getHomotypicalGroup());
699 Specimen specimen
= Specimen
.NewInstance();
700 specimen
.setTitleCache("Type specimen 2", true);
701 occurrenceService
.save(specimen
);
702 SpecimenTypeDesignationStatus typeStatus
= (SpecimenTypeDesignationStatus
)termService
.find(SpecimenTypeDesignationStatus
.HOLOTYPE().getUuid());
704 SpecimenTypeDesignation typeDesignation
= SpecimenTypeDesignation
.NewInstance();
705 typeDesignation
.setTypeStatus(typeStatus
);
706 typeDesignation
.setTypeSpecimen(specimen
);
708 boolean addToAllNames
= true;
709 name1
.addTypeDesignation(typeDesignation
, addToAllNames
);
710 nameService
.save(name1
);
711 commitAndStartNewTransaction(tableNames
);
712 printDataSet(System
.out
, tableNames
);
714 name1
= (NonViralName
<?
>)nameService
.find(name1
.getUuid());
715 nameService
.delete(name1
); //should throw now exception
718 printDataSet(System
.out
, tableNames
);
725 private void commitAndStartNewTransaction(final String
[] tableNames
) {
728 // printDataSet(System.out, tableNames);
729 startNewTransaction();
736 private Rank
getSpeciesRank() {
737 return (Rank
)termService
.find(Rank
.uuidSpecies
);
743 private Rank
getGenusRank() {
744 return (Rank
)termService
.find(Rank
.uuidGenus
);