1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.validation;
|
11
|
|
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;
|
16
|
|
17
|
import java.util.Set;
|
18
|
import java.util.UUID;
|
19
|
|
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;
|
25
|
|
26
|
import junit.framework.Assert;
|
27
|
|
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;
|
35
|
|
36
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
37
|
import eu.etaxonomy.cdm.model.agent.Person;
|
38
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
39
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
40
|
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
|
41
|
import eu.etaxonomy.cdm.model.common.Group;
|
42
|
import eu.etaxonomy.cdm.model.common.Language;
|
43
|
import eu.etaxonomy.cdm.model.common.User;
|
44
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
45
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
46
|
import eu.etaxonomy.cdm.model.name.Rank;
|
47
|
import eu.etaxonomy.cdm.model.occurrence.Specimen;
|
48
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
49
|
|
50
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
51
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
52
|
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
|
53
|
|
54
|
/**
|
55
|
* NOTE: In this test, the words "valid" and "invalid", loaded though
|
56
|
* these terms are when applied to taxonomic names, only mean "passes the
|
57
|
* rules of this validator" or not and should not be confused with the strict
|
58
|
* nomenclatural and taxonomic sense of these words.
|
59
|
*
|
60
|
* @author ben.clark
|
61
|
*
|
62
|
*/
|
63
|
@SuppressWarnings("unused")
|
64
|
//@Ignore //FIXME ignoring just for today 9.6.2010 a.kohlbecker !!!!!!!!!!!!!!!!!!!!!
|
65
|
public class ValidationTest extends CdmIntegrationTest {
|
66
|
private static final Logger logger = Logger.getLogger(ValidationTest.class);
|
67
|
|
68
|
@SpringBeanByType
|
69
|
private Validator validator;
|
70
|
|
71
|
private BotanicalName name;
|
72
|
|
73
|
@Before
|
74
|
public void setUp() {
|
75
|
/*DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
|
76
|
vocabularyStore.initialize();
|
77
|
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
|
78
|
validator = validatorFactory.getValidator();*/
|
79
|
name = BotanicalName.NewInstance(Rank.SPECIES());
|
80
|
}
|
81
|
|
82
|
|
83
|
/****************** TESTS *****************************/
|
84
|
|
85
|
/**
|
86
|
* Test validation factory initialization and autowiring
|
87
|
* into an instance of javax.valdation.Validator
|
88
|
*/
|
89
|
@Test
|
90
|
@DataSet
|
91
|
public final void testValidatorAutowire() {
|
92
|
assertNotNull("the validator should not be null", validator);
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* Test validation at the "default" level with a valid name
|
97
|
*/
|
98
|
@Test
|
99
|
@DataSet
|
100
|
public final void testDefaultValidationWithValidName() {
|
101
|
Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name);
|
102
|
assertTrue("There should be no constraint violations as this name is valid at the default level",constraintViolations.isEmpty());
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* Test validation at the "default" level with an invalid name
|
107
|
*/
|
108
|
@Test
|
109
|
@DataSet
|
110
|
public final void testDefaultValidationWithInValidName() {
|
111
|
name.setGenusOrUninomial("");
|
112
|
Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name);
|
113
|
assertFalse("There should be a constraint violation as this name is invalid at the default level",constraintViolations.isEmpty());
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* Test validation at level2 with a valid name
|
118
|
*/
|
119
|
@Test
|
120
|
@DataSet
|
121
|
public final void testLevel2ValidationWithValidName() {
|
122
|
name.setGenusOrUninomial("Abies");
|
123
|
name.setSpecificEpithet("balsamea");
|
124
|
name.setNameCache("Abies balsamea");
|
125
|
name.setAuthorshipCache("L.");
|
126
|
name.setTitleCache("Abies balsamea L.", true);
|
127
|
name.setFullTitleCache("Abies balsamea L.");
|
128
|
|
129
|
Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class,Level2.class);
|
130
|
assertTrue("There should not be a constraint violation as this name is valid at the default and at the second level",constraintViolations.isEmpty());
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Test validation at level2 with an invalid name
|
135
|
*/
|
136
|
@Test
|
137
|
@DataSet
|
138
|
public final void testLevel2ValidationWithInValidName() {
|
139
|
name.setGenusOrUninomial("Abies");
|
140
|
name.setSpecificEpithet("balsamea");
|
141
|
|
142
|
|
143
|
Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class);
|
144
|
assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations.isEmpty());
|
145
|
|
146
|
constraintViolations = validator.validate(name, Default.class,Level2.class);
|
147
|
assertFalse("There should be a constraint violation as this name is valid at the default level, but invalid at the second level",constraintViolations.isEmpty());
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* Test validation at level3 with a valid name
|
152
|
*/
|
153
|
@Test
|
154
|
@DataSet
|
155
|
public final void testLevel3ValidationWithValidName() {
|
156
|
name.setGenusOrUninomial("Abies");
|
157
|
name.setSpecificEpithet("balsamea");
|
158
|
name.setNameCache("Abies balsamea");
|
159
|
name.setAuthorshipCache("L.");
|
160
|
name.setTitleCache("Abies balsamea L.", true);
|
161
|
name.setFullTitleCache("Abies balsamea L.");
|
162
|
|
163
|
Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class,Level2.class, Level3.class);
|
164
|
assertTrue("There should not be a constraint violation as this name is valid at all levels",constraintViolations.isEmpty());
|
165
|
}
|
166
|
|
167
|
/**
|
168
|
* Test validation at the level3 with an invalid name
|
169
|
*/
|
170
|
@Test
|
171
|
@DataSet
|
172
|
public final void testLevel3ValidationWithInValidName() {
|
173
|
name.setGenusOrUninomial("Abies");
|
174
|
name.setSpecificEpithet("alba");
|
175
|
name.setNameCache("Abies alba");
|
176
|
name.setAuthorshipCache("Mill.");
|
177
|
name.setTitleCache("Abies alba Mill.", true);
|
178
|
name.setFullTitleCache("Abies alba Mill.");
|
179
|
name.setNomenclaturalReference(null);
|
180
|
//name.setNomenclaturalMicroReference(" ");
|
181
|
|
182
|
Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class, Level2.class);
|
183
|
assertTrue("There should not be a constraint violation as this name is valid at the default and second level",constraintViolations.isEmpty());
|
184
|
constraintViolations = validator.validate(name, Default.class,Level2.class, Level3.class);
|
185
|
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());
|
186
|
|
187
|
}
|
188
|
}
|