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 //@Ignore //FIXME ignoring just for today 9.6.2010 a.kohlbecker !!!!!!!!!!!!!!!!!!!!!
66 public class ValidationTest
extends CdmIntegrationTest
{
67 private static final Logger logger
= Logger
.getLogger(ValidationTest
.class);
70 private Validator validator
;
73 private ITermService termService
;
75 private BotanicalName name
;
79 /*DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
80 vocabularyStore.initialize();
81 ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
82 validator = validatorFactory.getValidator();*/
84 Rank speciesRank
= (Rank
)termService
.find(Rank
.uuidSpecies
);
85 name
= BotanicalName
.NewInstance(speciesRank
);
89 /****************** TESTS *****************************/
92 * Test validation factory initialization and autowiring
93 * into an instance of javax.valdation.Validator
97 public final void testValidatorAutowire() {
98 assertNotNull("the validator should not be null", validator
);
102 * Test validation at the "default" level with a valid name
106 public final void testDefaultValidationWithValidName() {
107 Set
<ConstraintViolation
<BotanicalName
>> constraintViolations
= validator
.validate(name
);
108 assertTrue("There should be no constraint violations as this name is valid at the default level",constraintViolations
.isEmpty());
112 * Test validation at the "default" level with an invalid name
116 public final void testDefaultValidationWithInValidName() {
117 name
.setGenusOrUninomial("");
118 Set
<ConstraintViolation
<BotanicalName
>> constraintViolations
= validator
.validate(name
);
119 assertFalse("There should be a constraint violation as this name is invalid at the default level",constraintViolations
.isEmpty());
123 * Test validation at level2 with a valid name
127 public final void testLevel2ValidationWithValidName() {
128 name
.setGenusOrUninomial("Abies");
129 name
.setSpecificEpithet("balsamea");
130 name
.setNameCache("Abies balsamea");
131 name
.setAuthorshipCache("L.");
132 name
.setTitleCache("Abies balsamea L.", true);
133 name
.setFullTitleCache("Abies balsamea L.");
135 Set
<ConstraintViolation
<BotanicalName
>> constraintViolations
= validator
.validate(name
, Default
.class,Level2
.class);
136 assertTrue("There should not be a constraint violation as this name is valid at the default and at the second level",constraintViolations
.isEmpty());
140 * Test validation at level2 with an invalid name
144 public final void testLevel2ValidationWithInValidName() {
145 name
.setGenusOrUninomial("Abies");
146 name
.setSpecificEpithet("balsamea");
149 Set
<ConstraintViolation
<BotanicalName
>> constraintViolations
= validator
.validate(name
, Default
.class);
150 assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations
.isEmpty());
152 constraintViolations
= validator
.validate(name
, Default
.class,Level2
.class);
153 assertFalse("There should be a constraint violation as this name is valid at the default level, but invalid at the second level",constraintViolations
.isEmpty());
157 * Test validation at level3 with a valid name
161 public final void testLevel3ValidationWithValidName() {
162 name
.setGenusOrUninomial("Abies");
163 name
.setSpecificEpithet("balsamea");
164 name
.setNameCache("Abies balsamea");
165 name
.setAuthorshipCache("L.");
166 name
.setTitleCache("Abies balsamea L.", true);
167 name
.setFullTitleCache("Abies balsamea L.");
169 Set
<ConstraintViolation
<BotanicalName
>> constraintViolations
= validator
.validate(name
, Default
.class,Level2
.class, Level3
.class);
170 assertTrue("There should not be a constraint violation as this name is valid at all levels",constraintViolations
.isEmpty());
174 * Test validation at the level3 with an invalid name
178 public final void testLevel3ValidationWithInValidName() {
179 name
.setGenusOrUninomial("Abies");
180 name
.setSpecificEpithet("alba");
181 name
.setNameCache("Abies alba");
182 name
.setAuthorshipCache("Mill.");
183 name
.setTitleCache("Abies alba Mill.", true);
184 name
.setFullTitleCache("Abies alba Mill.");
185 name
.setNomenclaturalReference(null);
186 //name.setNomenclaturalMicroReference(" ");
188 Set
<ConstraintViolation
<BotanicalName
>> constraintViolations
= validator
.validate(name
, Default
.class, Level2
.class);
189 assertTrue("There should not be a constraint violation as this name is valid at the default and second level",constraintViolations
.isEmpty());
190 constraintViolations
= validator
.validate(name
, Default
.class,Level2
.class, Level3
.class);
191 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());