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
.validation
;
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
;
18 import java
.util
.UUID
;
20 import javax
.validation
.ConstraintViolation
;
21 import javax
.validation
.Validation
;
22 import javax
.validation
.Validator
;
23 import javax
.validation
.ValidatorFactory
;
24 import javax
.validation
.groups
.Default
;
26 import junit
.framework
.Assert
;
28 import org
.apache
.log4j
.Logger
;
29 import org
.junit
.Before
;
30 import org
.junit
.Ignore
;
31 import org
.junit
.Test
;
32 import org
.unitils
.UnitilsJUnit4
;
33 import org
.unitils
.dbunit
.annotation
.DataSet
;
34 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
36 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
37 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
38 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
39 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
40 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
41 import eu
.etaxonomy
.cdm
.model
.common
.DefaultTermInitializer
;
42 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
43 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
44 import eu
.etaxonomy
.cdm
.model
.common
.User
;
45 import eu
.etaxonomy
.cdm
.model
.description
.SpecimenDescription
;
46 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
47 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
48 import eu
.etaxonomy
.cdm
.model
.occurrence
.Specimen
;
49 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
51 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
52 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
53 import eu
.etaxonomy
.cdm
.test
.integration
.CdmIntegrationTest
;
56 * NOTE: In this test, the words "valid" and "invalid", loaded though
57 * these terms are when applied to taxonomic names, only mean "passes the
58 * rules of this validator" or not and should not be confused with the strict
59 * nomenclatural and taxonomic sense of these words.
64 @SuppressWarnings("unused")
65 public class ValidationTest
extends CdmIntegrationTest
{
66 private static final Logger logger
= Logger
.getLogger(ValidationTest
.class);
69 private Validator validator
;
72 private ITermService termService
;
74 private BotanicalName name
;
78 /*DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
79 vocabularyStore.initialize();
80 ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
81 validator = validatorFactory.getValidator();*/
83 Rank speciesRank
= (Rank
)termService
.find(Rank
.uuidSpecies
);
84 name
= BotanicalName
.NewInstance(speciesRank
);
88 /****************** TESTS *****************************/
91 * Test validation factory initialization and autowiring
92 * into an instance of javax.valdation.Validator
96 public final void testValidatorAutowire() {
97 assertNotNull("the validator should not be null", validator
);
101 * Test validation at the "default" level with a valid name
105 public final void testDefaultValidationWithValidName() {
106 Set
<ConstraintViolation
<BotanicalName
>> constraintViolations
= validator
.validate(name
);
107 assertTrue("There should be no constraint violations as this name is valid at the default level",constraintViolations
.isEmpty());
111 * Test validation at the "default" level with an invalid name
115 public final void testDefaultValidationWithInValidName() {
116 name
.setGenusOrUninomial("");
117 Set
<ConstraintViolation
<BotanicalName
>> constraintViolations
= validator
.validate(name
);
118 assertFalse("There should be a constraint violation as this name is invalid at the default level",constraintViolations
.isEmpty());
122 * Test validation at level2 with a valid name
126 public final void testLevel2ValidationWithValidName() {
127 name
.setGenusOrUninomial("Abies");
128 name
.setSpecificEpithet("balsamea");
129 name
.setNameCache("Abies balsamea");
130 name
.setAuthorshipCache("L.");
131 name
.setTitleCache("Abies balsamea L.", true);
132 name
.setFullTitleCache("Abies balsamea L.");
134 Set
<ConstraintViolation
<BotanicalName
>> constraintViolations
= validator
.validate(name
, Default
.class,Level2
.class);
135 assertTrue("There should not be a constraint violation as this name is valid at the default and at the second level",constraintViolations
.isEmpty());
139 * Test validation at level2 with an invalid name
143 public final void testLevel2ValidationWithInValidName() {
144 name
.setGenusOrUninomial("Abies");
145 name
.setSpecificEpithet("balsamea");
148 Set
<ConstraintViolation
<BotanicalName
>> constraintViolations
= validator
.validate(name
, Default
.class);
149 assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations
.isEmpty());
151 constraintViolations
= validator
.validate(name
, Default
.class,Level2
.class);
152 assertFalse("There should be a constraint violation as this name is valid at the default level, but invalid at the second level",constraintViolations
.isEmpty());
156 * Test validation at level3 with a valid name
160 public final void testLevel3ValidationWithValidName() {
161 name
.setGenusOrUninomial("Abies");
162 name
.setSpecificEpithet("balsamea");
163 name
.setNameCache("Abies balsamea");
164 name
.setAuthorshipCache("L.");
165 name
.setTitleCache("Abies balsamea L.", true);
166 name
.setFullTitleCache("Abies balsamea L.");
168 Set
<ConstraintViolation
<BotanicalName
>> constraintViolations
= validator
.validate(name
, Default
.class,Level2
.class, Level3
.class);
169 assertTrue("There should not be a constraint violation as this name is valid at all levels",constraintViolations
.isEmpty());
173 * Test validation at the level3 with an invalid name
177 public final void testLevel3ValidationWithInValidName() {
178 name
.setGenusOrUninomial("Abies");
179 name
.setSpecificEpithet("alba");
180 name
.setNameCache("Abies alba");
181 name
.setAuthorshipCache("Mill.");
182 name
.setTitleCache("Abies alba Mill.", true);
183 name
.setFullTitleCache("Abies alba Mill.");
184 name
.setNomenclaturalReference(null);
185 //name.setNomenclaturalMicroReference(" ");
187 Set
<ConstraintViolation
<BotanicalName
>> constraintViolations
= validator
.validate(name
, Default
.class, Level2
.class);
188 assertTrue("There should not be a constraint violation as this name is valid at the default and second level",constraintViolations
.isEmpty());
189 constraintViolations
= validator
.validate(name
, Default
.class,Level2
.class, Level3
.class);
190 assertFalse("There should be a constraint violation as this name is valid at the default and second level, but invalid at the third level",constraintViolations
.isEmpty());