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.assertTrue;
|
14
|
|
15
|
import java.util.Set;
|
16
|
|
17
|
import javax.validation.ConstraintViolation;
|
18
|
import javax.validation.constraints.Pattern;
|
19
|
import javax.validation.groups.Default;
|
20
|
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator;
|
23
|
import org.junit.Before;
|
24
|
import org.junit.Ignore;
|
25
|
import org.junit.Test;
|
26
|
|
27
|
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
|
28
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
29
|
import eu.etaxonomy.cdm.model.name.Rank;
|
30
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
31
|
|
32
|
|
33
|
/**
|
34
|
* NOTE: In this test, the words "valid" and "invalid", loaded though
|
35
|
* these terms are when applied to taxonomic names, only mean "passes the
|
36
|
* rules of this validator" or not and should not be confused with the strict
|
37
|
* nomenclatural and taxonomic sense of these words.
|
38
|
*
|
39
|
* @author ben.clark
|
40
|
*
|
41
|
*/
|
42
|
@SuppressWarnings("unused")
|
43
|
public class NameValidCharactersTest extends ValidationTestBase {
|
44
|
private static final Logger logger = Logger.getLogger(NameValidCharactersTest.class);
|
45
|
|
46
|
private BotanicalName name;
|
47
|
|
48
|
@Before
|
49
|
public void setUp() {
|
50
|
DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
|
51
|
vocabularyStore.initialize();
|
52
|
name = TaxonNameBase.NewBotanicalInstance(Rank.SPECIES());
|
53
|
name.setGenusOrUninomial("Abies");
|
54
|
name.setSpecificEpithet("balsamea");
|
55
|
name.setNameCache("Abies balsamea");
|
56
|
name.setAuthorshipCache("L.");
|
57
|
name.setTitleCache("Abies balsamea L.", true);
|
58
|
name.setFullTitleCache("Abies balsamea L.");
|
59
|
}
|
60
|
|
61
|
|
62
|
/****************** TESTS *****************************/
|
63
|
|
64
|
/**
|
65
|
* Test validation at level2 with an invalid name - this should pass as there
|
66
|
* are international characters that are not allowed - grave and acute are forbidden
|
67
|
*/
|
68
|
@Test
|
69
|
public final void testForbiddenAccents() {
|
70
|
name.setSpecificEpithet("bals�me�");
|
71
|
|
72
|
|
73
|
Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class);
|
74
|
assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations.isEmpty());
|
75
|
|
76
|
constraintViolations = validator.validate(name, Default.class,Level2.class);
|
77
|
assertFalse("There should be a constraint violation as this name is valid at the default level, but contains a letter with a grave and an acute",constraintViolations.isEmpty());
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Test validation at level2 with an valid name - this should pass the
|
82
|
* diaeresis is allowed under the botanical code.
|
83
|
*/
|
84
|
@Test
|
85
|
@Ignore // setting this to ignore because the character is not showsn correctly in mac os.
|
86
|
public final void testAllowedAccents() {
|
87
|
name.setSpecificEpithet("bals�mea");
|
88
|
|
89
|
Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class);
|
90
|
assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations.isEmpty());
|
91
|
|
92
|
constraintViolations = validator.validate(name, Default.class,Level2.class);
|
93
|
assertTrue("There should not be a constraint violation as this name is valid at both levels, despite containing a diaeresis",constraintViolations.isEmpty());
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* Test validation at level2 with an invalid name - this should pass as the genus part
|
98
|
* does not have a capitalized first letter
|
99
|
*/
|
100
|
@Test
|
101
|
public final void testWithoutCapitalizedUninomial() {
|
102
|
name.setGenusOrUninomial("abies");
|
103
|
|
104
|
Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class);
|
105
|
assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations.isEmpty());
|
106
|
|
107
|
constraintViolations = validator.validate(name, Default.class,Level2.class);
|
108
|
assertFalse("There should be a constraint violation as this name is valid at the default level, the first letter of the genus part is not capitalized",constraintViolations.isEmpty());
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Test validation at level2 with an invalid name - this should pass as the genus part
|
113
|
* does not have a capitalized first letter
|
114
|
*/
|
115
|
@Test
|
116
|
public final void testWithCapitalizedNonFirstLetterInUninomial() {
|
117
|
name.setGenusOrUninomial("ABies");
|
118
|
|
119
|
Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Default.class);
|
120
|
assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations.isEmpty());
|
121
|
|
122
|
constraintViolations = validator.validate(name, Default.class,Level2.class);
|
123
|
assertFalse("There should be a constraint violation as this name is valid at the default level, the second letter of the genus part is capitalized",constraintViolations.isEmpty());
|
124
|
}
|
125
|
|
126
|
/**
|
127
|
* Test validation at level2 with an invalid name - this should pass as the genus part
|
128
|
* does not have a capitalized first letter
|
129
|
*/
|
130
|
@Test
|
131
|
public final void testAuthorship() {
|
132
|
Set<ConstraintViolation<BotanicalName>> constraintViolations = validator.validate(name, Level2.class);
|
133
|
assertNoConstraintOnValidator((Set)constraintViolations, Pattern.class);
|
134
|
|
135
|
name.setAuthorshipCache("", true);
|
136
|
constraintViolations = validator.validate(name, Level2.class);
|
137
|
assertHasConstraintOnValidator((Set)constraintViolations, PatternValidator.class);
|
138
|
|
139
|
name.setAuthorshipCache(null, true);
|
140
|
constraintViolations = validator.validate(name, Level2.class);
|
141
|
assertNoConstraintOnValidator((Set)constraintViolations, PatternValidator.class);
|
142
|
|
143
|
name.setAuthorshipCache("L\\u05EB", true);
|
144
|
constraintViolations = validator.validate(name, Level2.class);
|
145
|
assertHasConstraintOnValidator((Set)constraintViolations, PatternValidator.class);
|
146
|
|
147
|
}
|
148
|
}
|