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.assertEquals;
|
13
|
import static org.junit.Assert.assertFalse;
|
14
|
import static org.junit.Assert.assertNotNull;
|
15
|
import static org.junit.Assert.assertTrue;
|
16
|
|
17
|
import java.net.URI;
|
18
|
import java.net.URISyntaxException;
|
19
|
import java.util.Set;
|
20
|
import java.util.UUID;
|
21
|
|
22
|
import javax.validation.ConstraintViolation;
|
23
|
import javax.validation.Validation;
|
24
|
import javax.validation.Validator;
|
25
|
import javax.validation.ValidatorFactory;
|
26
|
import javax.validation.groups.Default;
|
27
|
|
28
|
import junit.framework.Assert;
|
29
|
|
30
|
import org.apache.log4j.Logger;
|
31
|
import org.junit.Before;
|
32
|
import org.junit.Ignore;
|
33
|
import org.junit.Test;
|
34
|
import org.unitils.dbunit.annotation.DataSet;
|
35
|
import org.unitils.spring.annotation.SpringBeanByType;
|
36
|
|
37
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
38
|
import eu.etaxonomy.cdm.model.agent.Person;
|
39
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
40
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
41
|
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
|
42
|
import eu.etaxonomy.cdm.model.common.Group;
|
43
|
import eu.etaxonomy.cdm.model.common.Language;
|
44
|
import eu.etaxonomy.cdm.model.common.User;
|
45
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
46
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
47
|
import eu.etaxonomy.cdm.model.name.Rank;
|
48
|
import eu.etaxonomy.cdm.model.occurrence.Specimen;
|
49
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
50
|
import eu.etaxonomy.cdm.model.reference.IArticle;
|
51
|
import eu.etaxonomy.cdm.model.reference.IBook;
|
52
|
import eu.etaxonomy.cdm.model.reference.IBookSection;
|
53
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
54
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
55
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
56
|
|
57
|
|
58
|
/**
|
59
|
*
|
60
|
* @author ben.clark
|
61
|
*
|
62
|
*/
|
63
|
@SuppressWarnings("unused")
|
64
|
@Ignore //FIXME ignoring only for merging 8.6.2010 a.kohlbecker
|
65
|
public class ReferenceValidationTest {
|
66
|
private static final Logger logger = Logger.getLogger(ReferenceValidationTest.class);
|
67
|
|
68
|
|
69
|
private Validator validator;
|
70
|
|
71
|
private IBook book;
|
72
|
|
73
|
@Before
|
74
|
public void setUp() {
|
75
|
DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
|
76
|
vocabularyStore.initialize();
|
77
|
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
|
78
|
validator = validatorFactory.getValidator();
|
79
|
book = ReferenceFactory.newBook();
|
80
|
book.setTitleCache("Lorem ipsum", true);
|
81
|
book.setIsbn("1-919795-99-5");
|
82
|
}
|
83
|
|
84
|
|
85
|
/****************** TESTS *****************************/
|
86
|
|
87
|
/**
|
88
|
* Test validation at the second level with a valid reference
|
89
|
*/
|
90
|
@Test
|
91
|
public final void testLevel2ValidationWithValidBook() {
|
92
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class, Default.class);
|
93
|
assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
|
94
|
}
|
95
|
|
96
|
@Test
|
97
|
public final void testLevel2ValidationWithValidISBN() {
|
98
|
|
99
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
100
|
assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
|
101
|
|
102
|
|
103
|
|
104
|
}
|
105
|
|
106
|
@Test
|
107
|
public final void testLevel2ValidationWithInValidISBN() {
|
108
|
book.setIsbn("1-9197954-99-5");
|
109
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
110
|
assertFalse("There should be a constraint violation as this book has an invalid ISBN number",constraintViolations.isEmpty());
|
111
|
}
|
112
|
|
113
|
@Test
|
114
|
public final void testLevel2ValidationWithValidUri() {
|
115
|
try {
|
116
|
book.setUri(new URI("http://www.e-taxonomy.eu"));
|
117
|
} catch (URISyntaxException e) {
|
118
|
Assert.fail("URI is not valid");
|
119
|
}
|
120
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
121
|
assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
|
122
|
}
|
123
|
|
124
|
|
125
|
@Test
|
126
|
public final void testLevel2ValidationWithInValidInReference() {
|
127
|
|
128
|
IBookSection bookSection = ReferenceFactory.newBookSection();
|
129
|
bookSection.setTitleCache("test", true);
|
130
|
bookSection.setTitle("");
|
131
|
bookSection.setInReference((Reference)book);
|
132
|
Set<ConstraintViolation<IBookSection>> constraintViolations = validator.validate(bookSection, Level2.class);
|
133
|
assertTrue("There should be one constraint violation as this book has a valid Ref",constraintViolations.size() == 0);
|
134
|
|
135
|
Reference article = ReferenceFactory.newArticle();
|
136
|
article.setTitleCache("article");
|
137
|
bookSection.setInReference(article);
|
138
|
constraintViolations = validator.validate(bookSection, Level2.class);
|
139
|
assertTrue("There should be a constraint violation as this book has an invalid inReference",constraintViolations.size() == 1);
|
140
|
|
141
|
|
142
|
|
143
|
}
|
144
|
@Test
|
145
|
public final void testValidationAfterCasting(){
|
146
|
|
147
|
((Reference)book).castReferenceToArticle();
|
148
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
149
|
assertFalse("There should be one constraint violations as this article is not valid at level 2 (has an isbn)",constraintViolations.isEmpty());
|
150
|
}
|
151
|
|
152
|
}
|