Project

General

Profile

Download (6.22 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

    
63
/****************** TESTS *****************************/
64

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

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

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

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

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

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

    
122

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

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

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

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

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

    
162
        Set<ConstraintViolation<IBotanicalName>> constraintViolations  = validator.validate(name, Default.class, Level2.class);
163
        assertTrue("There should not be a constraint violation as this name is valid at the default and second level",constraintViolations.isEmpty());
164
        constraintViolations  = validator.validate(name, Default.class,Level2.class, Level3.class);
165
        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());
166

    
167
	}
168

    
169
    @Override
170
    public void createTestDataSet() throws FileNotFoundException {}
171
}
    (1-1/1)