Project

General

Profile

Download (5.99 KB) Statistics
| Branch: | Tag: | Revision:
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

    
31

    
32
/**
33
 * NOTE: In this test, the words "valid" and "invalid", loaded though
34
 * these terms are when applied to taxonomic names, only mean "passes the
35
 * rules of this validator" or not and should not be confused with the strict
36
 * nomenclatural and taxonomic sense of these words.
37
 *
38
 * @author ben.clark
39
 *
40
 */
41
@SuppressWarnings("unused")
42
public class NameValidCharactersTest extends ValidationTestBase  {
43
	private static final Logger logger = Logger.getLogger(NameValidCharactersTest.class);
44

    
45
	private BotanicalName name;
46

    
47
	@Before
48
	public void setUp() {
49
		DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
50
		vocabularyStore.initialize();
51
		name = BotanicalName.NewInstance(Rank.SPECIES());
52
		name.setGenusOrUninomial("Abies");
53
		name.setSpecificEpithet("balsamea");
54
		name.setNameCache("Abies balsamea");
55
		name.setAuthorshipCache("L.");
56
		name.setTitleCache("Abies balsamea L.", true);
57
		name.setFullTitleCache("Abies balsamea L.");
58
	}
59

    
60

    
61
/****************** TESTS *****************************/
62

    
63
	/**
64
	 * Test validation at level2 with an invalid name - this should pass as there
65
	 * are international characters that are not allowed - grave and acute are forbidden
66
	 */
67
	@Test
68
	public final void testForbiddenAccents() {
69
		name.setSpecificEpithet("bals�me�");
70

    
71

    
72
        Set<ConstraintViolation<BotanicalName>> constraintViolations  = validator.validate(name, Default.class);
73
        assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations.isEmpty());
74

    
75
        constraintViolations  = validator.validate(name, Default.class,Level2.class);
76
        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());
77
	}
78

    
79
	/**
80
	 * Test validation at level2 with an valid name - this should pass the
81
	 * diaeresis is allowed under the botanical code.
82
	 */
83
	@Test
84
	@Ignore // setting this to ignore because the character is not showsn correctly in mac os.
85
	public final void testAllowedAccents() {
86
		name.setSpecificEpithet("bals�mea");
87

    
88
        Set<ConstraintViolation<BotanicalName>> constraintViolations  = validator.validate(name, Default.class);
89
        assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations.isEmpty());
90

    
91
        constraintViolations  = validator.validate(name, Default.class,Level2.class);
92
        assertTrue("There should not be a constraint violation as this name is valid at both levels, despite containing a diaeresis",constraintViolations.isEmpty());
93
	}
94

    
95
	/**
96
	 * Test validation at level2 with an invalid name - this should pass as the genus part
97
	 * does not have a capitalized first letter
98
	 */
99
	@Test
100
	public final void testWithoutCapitalizedUninomial() {
101
		name.setGenusOrUninomial("abies");
102

    
103
        Set<ConstraintViolation<BotanicalName>> constraintViolations  = validator.validate(name, Default.class);
104
        assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations.isEmpty());
105

    
106
        constraintViolations  = validator.validate(name, Default.class,Level2.class);
107
        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());
108
	}
109

    
110
	/**
111
	 * Test validation at level2 with an invalid name - this should pass as the genus part
112
	 * does not have a capitalized first letter
113
	 */
114
	@Test
115
	public final void testWithCapitalizedNonFirstLetterInUninomial() {
116
		name.setGenusOrUninomial("ABies");
117

    
118
        Set<ConstraintViolation<BotanicalName>> constraintViolations  = validator.validate(name, Default.class);
119
        assertTrue("There should not be a constraint violation as this name is valid at the default level",constraintViolations.isEmpty());
120

    
121
        constraintViolations  = validator.validate(name, Default.class,Level2.class);
122
        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());
123
	}
124

    
125
	/**
126
     * Test validation at level2 with an invalid name - this should pass as the genus part
127
     * does not have a capitalized first letter
128
     */
129
    @Test
130
    public final void testAuthorship() {
131
        Set<ConstraintViolation<BotanicalName>> constraintViolations  = validator.validate(name, Level2.class);
132
        assertNoConstraintOnValidator((Set)constraintViolations, Pattern.class);
133

    
134
        name.setAuthorshipCache("", true);
135
        constraintViolations  = validator.validate(name, Level2.class);
136
        assertHasConstraintOnValidator((Set)constraintViolations, PatternValidator.class);
137

    
138
        name.setAuthorshipCache(null, true);
139
        constraintViolations  = validator.validate(name, Level2.class);
140
        assertNoConstraintOnValidator((Set)constraintViolations, PatternValidator.class);
141

    
142
        name.setAuthorshipCache("L\\u05EB", true);
143
        constraintViolations  = validator.validate(name, Level2.class);
144
        assertHasConstraintOnValidator((Set)constraintViolations, PatternValidator.class);
145

    
146
    }
147
}
(4-4/10)