Project

General

Profile

Download (5.89 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
package eu.etaxonomy.cdm.validation;
10

    
11
import static org.junit.Assert.assertFalse;
12
import static org.junit.Assert.assertTrue;
13

    
14
import java.util.Set;
15

    
16
import javax.validation.ConstraintViolation;
17
import javax.validation.constraints.Pattern;
18
import javax.validation.groups.Default;
19

    
20
import org.apache.log4j.Logger;
21
import org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator;
22
import org.junit.Before;
23
import org.junit.Ignore;
24
import org.junit.Test;
25

    
26
import eu.etaxonomy.cdm.model.name.IBotanicalName;
27
import eu.etaxonomy.cdm.model.name.Rank;
28
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
29

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

    
42
	private IBotanicalName name;
43

    
44
	@Before
45
	public void setUp() {
46
		name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
47
		name.setGenusOrUninomial("Abies");
48
		name.setSpecificEpithet("balsamea");
49
		name.setNameCache("Abies balsamea");
50
		name.setAuthorshipCache("L.");
51
		name.setTitleCache("Abies balsamea L.", true);
52
		name.setFullTitleCache("Abies balsamea L.");
53
	}
54

    
55
/****************** TESTS *****************************/
56

    
57
	/**
58
	 * Test validation at level2 with an invalid name - this should pass as there
59
	 * are international characters that are not allowed - grave and acute are forbidden
60
	 */
61
	@Test
62
	public final void testForbiddenAccents() {
63
		name.setSpecificEpithet("bals�me�");
64

    
65

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

    
69
        constraintViolations  = validator.validate(name, Default.class,Level2.class);
70
        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());
71
	}
72

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

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

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

    
89
	/**
90
	 * Test validation at level2 with an invalid name - this should pass as the genus part
91
	 * does not have a capitalized first letter
92
	 */
93
	@Test
94
	public final void testWithoutCapitalizedUninomial() {
95
		name.setGenusOrUninomial("abies");
96

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

    
100
        constraintViolations  = validator.validate(name, Default.class,Level2.class);
101
        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());
102
	}
103

    
104
	/**
105
	 * Test validation at level2 with an invalid name - this should pass as the genus part
106
	 * does not have a capitalized first letter
107
	 */
108
	@Test
109
	public final void testWithCapitalizedNonFirstLetterInUninomial() {
110
		name.setGenusOrUninomial("ABies");
111

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

    
115
        constraintViolations  = validator.validate(name, Default.class,Level2.class);
116
        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());
117
	}
118

    
119
	/**
120
     * Test validation at level2 with an invalid name - this should pass as the genus part
121
     * does not have a capitalized first letter
122
     */
123
    @Test
124
    public final void testAuthorship() {
125
        Set<ConstraintViolation<IBotanicalName>> constraintViolations  = validator.validate(name, Level2.class);
126
        assertNoConstraintOnValidator((Set)constraintViolations, Pattern.class);
127

    
128
        name.setAuthorshipCache("", true);
129
        constraintViolations  = validator.validate(name, Level2.class);
130
        assertHasConstraintOnValidator((Set)constraintViolations, PatternValidator.class);
131

    
132
        name.setAuthorshipCache(null, true);
133
        constraintViolations  = validator.validate(name, Level2.class);
134
        assertNoConstraintOnValidator((Set)constraintViolations, PatternValidator.class);
135

    
136
        name.setAuthorshipCache("L\\u05EB", true);
137
        constraintViolations  = validator.validate(name, Level2.class);
138
        assertHasConstraintOnValidator((Set)constraintViolations, PatternValidator.class);
139
    }
140
}
(6-6/13)