Project

General

Profile

Download (6.24 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.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
@SuppressWarnings("unused")
44
public class ValidationTest extends CdmTransactionalIntegrationTest {
45
	private static final Logger logger = Logger.getLogger(ValidationTest.class);
46

    
47
	@SpringBeanByType
48
	private Validator validator;
49

    
50
	@SpringBeanByType
51
	private ITermService termService;
52

    
53
	private IBotanicalName name;
54

    
55
	@Before
56
	public void setUp() {
57

    
58
		//Rank speciesRank = (Rank)termService.find(Rank.uuidSpecies);
59
		name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
60
	}
61

    
62
/****************** TESTS *****************************/
63

    
64
	/**
65
	 * Test validation factory initialization and autowiring
66
	 * into an instance of javax.valdation.Validator
67
	 */
68
	@Test
69
	@DataSet
70
	public final void testValidatorAutowire() {
71
        assertNotNull("the validator should not be null", validator);
72
	}
73

    
74
	/**
75
	 * Test validation at the "default" level with a valid name
76
	 */
77
	@Test
78
	@DataSet
79
	public final void testDefaultValidationWithValidName() {
80
        Set<ConstraintViolation<IBotanicalName>> constraintViolations  = validator.validate(name);
81
        assertTrue("There should be no constraint violations as this name is valid at the default level",constraintViolations.isEmpty());
82
	}
83

    
84
	/**
85
	 * Test validation at the "default" level with an invalid name
86
	 */
87
	@Test
88
	@DataSet
89
	public final void testDefaultValidationWithInValidName() {
90
		name.setGenusOrUninomial("");
91
        Set<ConstraintViolation<IBotanicalName>> constraintViolations  = validator.validate(name);
92
        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());
93
	}
94

    
95
	/**
96
	 * Test validation at level2 with a valid name
97
	 */
98
	@Test
99
	@DataSet
100
	public final void testLevel2ValidationWithValidName() {
101
		name.setGenusOrUninomial("Abies");
102
		name.setSpecificEpithet("balsamea");
103
		name.setNameCache("Abies balsamea");
104
		name.setAuthorshipCache("L.");
105
		name.setTitleCache("Abies balsamea L.", true);
106
		name.setFullTitleCache("Abies balsamea L.");
107

    
108
        Set<ConstraintViolation<IBotanicalName>> constraintViolations  = validator.validate(name, Default.class,Level2.class);
109
        assertTrue("There should not be a constraint violation as this name is valid at the default and at the second level",constraintViolations.isEmpty());
110
	}
111

    
112
	/**
113
	 * Test validation at level2 with an invalid name
114
	 */
115
	@Test
116
	@DataSet
117
	public final void testLevel2ValidationWithInValidName() {
118
		name.setGenusOrUninomial("Abies");
119
		name.setSpecificEpithet("balsamea");
120

    
121

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

    
125
        constraintViolations  = validator.validate(name, Default.class,Level2.class);
126
        assertFalse("There should be a constraint violation as this name is valid at the default level, but invalid at the second level",constraintViolations.isEmpty());
127
	}
128

    
129
	/**
130
	 * Test validation at level3 with a valid name
131
	 */
132
	@Test
133
	@DataSet
134
	public final void testLevel3ValidationWithValidName() {
135
		name.setGenusOrUninomial("Abies");
136
		name.setSpecificEpithet("balsamea");
137
		name.setNameCache("Abies balsamea");
138
		name.setAuthorshipCache("L.");
139
		name.setTitleCache("Abies balsamea L.", true);
140
		name.setFullTitleCache("Abies balsamea L.");
141

    
142
        Set<ConstraintViolation<IBotanicalName>> constraintViolations  = validator.validate(name, Default.class, Level2.class/*, Level3.class*/);
143
        assertTrue("There should not be a constraint violation as this name is valid at all levels",constraintViolations.isEmpty());
144
	}
145

    
146
	/**
147
	 * Test validation at the level3 with an invalid name
148
	 */
149
	@Test
150
	@DataSet
151
	public final void testLevel3ValidationWithInValidName() {
152
		name.setGenusOrUninomial("Abies");
153
		name.setSpecificEpithet("alba");
154
		name.setNameCache("Abies alba");
155
		name.setAuthorshipCache("Mill.");
156
		name.setTitleCache("Abies alba Mill.", true);
157
		name.setFullTitleCache("Abies alba Mill.");
158
		name.setNomenclaturalReference(null);
159
		//name.setNomenclaturalMicroReference(" ");
160

    
161
        Set<ConstraintViolation<IBotanicalName>> constraintViolations  = validator.validate(name, Default.class, Level2.class);
162
        assertTrue("There should not be a constraint violation as this name is valid at the default and second level", constraintViolations.isEmpty());
163
        constraintViolations  = validator.validate(name, Default.class,Level2.class, Level3.class);
164
        assertFalse("There should be a constraint violation as this name is valid at the default and second level, but invalid at the third level as duplicates exist", constraintViolations.isEmpty());
165
	}
166

    
167
    @Override
168
    public void createTestDataSet() throws FileNotFoundException {}
169
}
    (1-1/1)