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.assertFalse;
|
13
|
import static org.junit.Assert.assertNotNull;
|
14
|
import static org.junit.Assert.assertTrue;
|
15
|
|
16
|
import java.io.FileNotFoundException;
|
17
|
import java.util.Set;
|
18
|
|
19
|
import javax.validation.ConstraintViolation;
|
20
|
import javax.validation.Validator;
|
21
|
import javax.validation.groups.Default;
|
22
|
|
23
|
import org.apache.log4j.Logger;
|
24
|
import org.junit.Before;
|
25
|
import org.junit.Test;
|
26
|
import org.unitils.dbunit.annotation.DataSet;
|
27
|
import org.unitils.spring.annotation.SpringBeanByType;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
30
|
import eu.etaxonomy.cdm.model.name.IBotanicalName;
|
31
|
import eu.etaxonomy.cdm.model.name.Rank;
|
32
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
33
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
34
|
|
35
|
/**
|
36
|
* NOTE: In this test, the words "valid" and "invalid", loaded though
|
37
|
* these terms are when applied to taxonomic names, only mean "passes the
|
38
|
* rules of this validator" or not and should not be confused with the strict
|
39
|
* nomenclatural and taxonomic sense of these words.
|
40
|
*
|
41
|
* @author ben.clark
|
42
|
*
|
43
|
*/
|
44
|
@SuppressWarnings("unused")
|
45
|
public class ValidationTest extends CdmTransactionalIntegrationTest {
|
46
|
private static final Logger logger = Logger.getLogger(ValidationTest.class);
|
47
|
|
48
|
@SpringBeanByType
|
49
|
private Validator validator;
|
50
|
|
51
|
@SpringBeanByType
|
52
|
private ITermService termService;
|
53
|
|
54
|
private IBotanicalName name;
|
55
|
|
56
|
@Before
|
57
|
public void setUp() {
|
58
|
|
59
|
//Rank speciesRank = (Rank)termService.find(Rank.uuidSpecies);
|
60
|
name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
|
61
|
}
|
62
|
|
63
|
|
64
|
/****************** TESTS *****************************/
|
65
|
|
66
|
/**
|
67
|
* Test validation factory initialization and autowiring
|
68
|
* into an instance of javax.valdation.Validator
|
69
|
*/
|
70
|
@Test
|
71
|
@DataSet
|
72
|
public final void testValidatorAutowire() {
|
73
|
assertNotNull("the validator should not be null", validator);
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Test validation at the "default" level with a valid name
|
78
|
*/
|
79
|
@Test
|
80
|
@DataSet
|
81
|
public final void testDefaultValidationWithValidName() {
|
82
|
Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name);
|
83
|
assertTrue("There should be no constraint violations as this name is valid at the default level",constraintViolations.isEmpty());
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* Test validation at the "default" level with an invalid name
|
88
|
*/
|
89
|
@Test
|
90
|
@DataSet
|
91
|
public final void testDefaultValidationWithInValidName() {
|
92
|
name.setGenusOrUninomial("");
|
93
|
Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name);
|
94
|
assertTrue("There should not be a constraint violation as this name is invalid at the default level because the setter checks for the empty string",constraintViolations.isEmpty());
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* Test validation at level2 with a valid name
|
99
|
*/
|
100
|
@Test
|
101
|
@DataSet
|
102
|
public final void testLevel2ValidationWithValidName() {
|
103
|
name.setGenusOrUninomial("Abies");
|
104
|
name.setSpecificEpithet("balsamea");
|
105
|
name.setNameCache("Abies balsamea");
|
106
|
name.setAuthorshipCache("L.");
|
107
|
name.setTitleCache("Abies balsamea L.", true);
|
108
|
name.setFullTitleCache("Abies balsamea L.");
|
109
|
|
110
|
Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name, Default.class,Level2.class);
|
111
|
assertTrue("There should not be a constraint violation as this name is valid at the default and at the second level",constraintViolations.isEmpty());
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Test validation at level2 with an invalid name
|
116
|
*/
|
117
|
@Test
|
118
|
@DataSet
|
119
|
public final void testLevel2ValidationWithInValidName() {
|
120
|
name.setGenusOrUninomial("Abies");
|
121
|
name.setSpecificEpithet("balsamea");
|
122
|
|
123
|
|
124
|
Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name, Default.class);
|
125
|
assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations.isEmpty());
|
126
|
|
127
|
constraintViolations = validator.validate(name, Default.class,Level2.class);
|
128
|
assertFalse("There should be a constraint violation as this name is valid at the default level, but invalid at the second level",constraintViolations.isEmpty());
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Test validation at level3 with a valid name
|
133
|
*/
|
134
|
@Test
|
135
|
@DataSet
|
136
|
public final void testLevel3ValidationWithValidName() {
|
137
|
name.setGenusOrUninomial("Abies");
|
138
|
name.setSpecificEpithet("balsamea");
|
139
|
name.setNameCache("Abies balsamea");
|
140
|
name.setAuthorshipCache("L.");
|
141
|
name.setTitleCache("Abies balsamea L.", true);
|
142
|
name.setFullTitleCache("Abies balsamea L.");
|
143
|
|
144
|
Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name, Default.class, Level2.class/*, Level3.class*/);
|
145
|
assertTrue("There should not be a constraint violation as this name is valid at all levels",constraintViolations.isEmpty());
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* Test validation at the level3 with an invalid name
|
150
|
*/
|
151
|
@Test
|
152
|
@DataSet
|
153
|
public final void testLevel3ValidationWithInValidName() {
|
154
|
name.setGenusOrUninomial("Abies");
|
155
|
name.setSpecificEpithet("alba");
|
156
|
name.setNameCache("Abies alba");
|
157
|
name.setAuthorshipCache("Mill.");
|
158
|
name.setTitleCache("Abies alba Mill.", true);
|
159
|
name.setFullTitleCache("Abies alba Mill.");
|
160
|
name.setNomenclaturalReference(null);
|
161
|
//name.setNomenclaturalMicroReference(" ");
|
162
|
|
163
|
Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name, Default.class, Level2.class);
|
164
|
assertTrue("There should not be a constraint violation as this name is valid at the default and second level",constraintViolations.isEmpty());
|
165
|
constraintViolations = validator.validate(name, Default.class,Level2.class, Level3.class);
|
166
|
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());
|
167
|
|
168
|
}
|
169
|
|
170
|
@Override
|
171
|
public void createTestDataSet() throws FileNotFoundException {}
|
172
|
}
|