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
|
}
|
82
|
|
83
|
|
84
|
/****************** TESTS *****************************/
|
85
|
|
86
|
/**
|
87
|
* Test validation at the second level with a valid reference
|
88
|
*/
|
89
|
@Test
|
90
|
public final void testLevel2ValidationWithValidBook() {
|
91
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class, Default.class);
|
92
|
assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
|
93
|
}
|
94
|
|
95
|
@Test
|
96
|
public final void testLevel2ValidationWithValidISBN() {
|
97
|
book.setIsbn("1-919795-99-5");
|
98
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
99
|
assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
|
100
|
}
|
101
|
|
102
|
@Test
|
103
|
public final void testLevel2ValidationWithInValidISBN() {
|
104
|
book.setIsbn("1-9197954-99-5");
|
105
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
106
|
assertFalse("There should be a constraint violation as this book has an invalid ISBN number",constraintViolations.isEmpty());
|
107
|
}
|
108
|
|
109
|
@Test
|
110
|
public final void testLevel2ValidationWithValidUri() {
|
111
|
try {
|
112
|
book.setUri(new URI("http://www.e-taxonomy.eu"));
|
113
|
} catch (URISyntaxException e) {
|
114
|
Assert.fail("URI is not valid");
|
115
|
}
|
116
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
117
|
assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
|
118
|
}
|
119
|
|
120
|
/*@Test
|
121
|
public final void testLevel2ValidationWithInValidUri() {
|
122
|
try {
|
123
|
book.setUri(new URI("http://java-tutor.com/index.html"));
|
124
|
} catch (URISyntaxException e) {
|
125
|
Assert.fail("URI is not valid");
|
126
|
}
|
127
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
128
|
assertTrue("There should be a constraint violation as this book has an invalid URI",constraintViolations.isEmpty());
|
129
|
}
|
130
|
*/
|
131
|
@Test
|
132
|
public final void testLevel2ValidationWithInValidInReference() {
|
133
|
|
134
|
IBookSection bookSection = ReferenceFactory.newBookSection();
|
135
|
bookSection.setTitleCache("test", true);
|
136
|
bookSection.setTitle("");
|
137
|
bookSection.setInReference((Reference)book);
|
138
|
Set<ConstraintViolation<IBookSection>> constraintViolations = validator.validate(bookSection, Level2.class);
|
139
|
//assertTrue("There should be no constraint violation as this book has a valid Ref",constraintViolations.isEmpty());
|
140
|
System.err.println("nr of violations " + constraintViolations.size() );
|
141
|
for (ConstraintViolation conViol: constraintViolations){
|
142
|
System.err.println(conViol.getMessage() + " - "+conViol.getPropertyPath() + " - " + conViol.getInvalidValue());
|
143
|
}
|
144
|
Reference article = ReferenceFactory.newArticle();
|
145
|
article.setTitleCache("article");
|
146
|
bookSection.setInReference(ReferenceFactory.newArticle());
|
147
|
constraintViolations = validator.validate(bookSection, Level2.class);
|
148
|
assertFalse("There should be a constraint violation as this book has an invalid inReference",constraintViolations.isEmpty());
|
149
|
System.err.println("nr of violations " + constraintViolations.size() );
|
150
|
for (ConstraintViolation conViol: constraintViolations){
|
151
|
System.err.println(conViol.getMessage() + " - "+conViol.getPropertyPath() + " - " + conViol.getInvalidValue());
|
152
|
}
|
153
|
|
154
|
}
|
155
|
|
156
|
}
|