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.assertTrue;
|
14
|
|
15
|
import java.net.URI;
|
16
|
import java.net.URISyntaxException;
|
17
|
import java.util.Set;
|
18
|
|
19
|
import javax.validation.ConstraintViolation;
|
20
|
import javax.validation.groups.Default;
|
21
|
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.junit.Assert;
|
24
|
import org.junit.Before;
|
25
|
import org.junit.Test;
|
26
|
|
27
|
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
|
28
|
import eu.etaxonomy.cdm.model.reference.IBook;
|
29
|
import eu.etaxonomy.cdm.model.reference.IBookSection;
|
30
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
31
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
32
|
import eu.etaxonomy.cdm.validation.constraint.InReferenceValidator;
|
33
|
import eu.etaxonomy.cdm.validation.constraint.NoRecursiveInReferenceValidator;
|
34
|
|
35
|
|
36
|
/**
|
37
|
*
|
38
|
* @author ben.clark
|
39
|
*
|
40
|
*/
|
41
|
@SuppressWarnings("unused")
|
42
|
public class ReferenceValidationTest extends ValidationTestBase {
|
43
|
private static final Logger logger = Logger.getLogger(ReferenceValidationTest.class);
|
44
|
|
45
|
private IBook book;
|
46
|
|
47
|
@Before
|
48
|
public void setUp() {
|
49
|
DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
|
50
|
vocabularyStore.initialize();
|
51
|
book = ReferenceFactory.newBook();
|
52
|
book.setTitleCache("Lorem ipsum", true);
|
53
|
book.setIsbn("1-919795-99-5");
|
54
|
}
|
55
|
|
56
|
|
57
|
/****************** TESTS *****************************/
|
58
|
|
59
|
/**
|
60
|
* Test validation at the second level with a valid reference
|
61
|
*/
|
62
|
@Test
|
63
|
public final void testLevel2ValidationWithValidBook() {
|
64
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class, Default.class);
|
65
|
assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
|
66
|
}
|
67
|
|
68
|
@Test
|
69
|
public final void testLevel2ValidationWithValidISBN() {
|
70
|
|
71
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
72
|
assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
|
73
|
}
|
74
|
|
75
|
@Test
|
76
|
public final void testLevel2ValidationWithInValidISBN() {
|
77
|
book.setIsbn("1-9197954-99-5");
|
78
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
79
|
assertFalse("There should be a constraint violation as this book has an invalid ISBN number",constraintViolations.isEmpty());
|
80
|
}
|
81
|
|
82
|
@Test
|
83
|
public final void testLevel2ValidationWithValidUri() {
|
84
|
try {
|
85
|
book.setUri(new URI("http://www.e-taxonomy.eu"));
|
86
|
} catch (URISyntaxException e) {
|
87
|
Assert.fail("URI is not valid");
|
88
|
}
|
89
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
90
|
assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
|
91
|
}
|
92
|
|
93
|
|
94
|
@Test
|
95
|
public final void testLevel3ValidationWithInValidInReference() {
|
96
|
|
97
|
IBookSection bookSection = ReferenceFactory.newBookSection();
|
98
|
bookSection.setTitleCache("test", true);
|
99
|
bookSection.setTitle("");
|
100
|
bookSection.setInReference((Reference)book);
|
101
|
Set<ConstraintViolation<IBookSection>> constraintViolations = validator.validate(bookSection, Level3.class);
|
102
|
assertTrue("There should be one constraint violation as this book has a valid Ref",constraintViolations.size() == 0);
|
103
|
|
104
|
Reference article = ReferenceFactory.newArticle();
|
105
|
article.setTitleCache("article", true);
|
106
|
bookSection.setInReference(article);
|
107
|
constraintViolations = validator.validate(bookSection, Level3.class);
|
108
|
// assertTrue("There should be a constraint violation as this book has an invalid inReference",constraintViolations.size() == 1);
|
109
|
assertHasConstraintOnValidator((Set)constraintViolations, InReferenceValidator.class);
|
110
|
}
|
111
|
|
112
|
|
113
|
@Test
|
114
|
public final void testValidationAfterCasting(){
|
115
|
((Reference)book).castReferenceToArticle();
|
116
|
Set<ConstraintViolation<IBook>> constraintViolations = validator.validate(book, Level2.class);
|
117
|
assertFalse("There should be one constraint violations as this article is not valid at level 2 (has an isbn)", constraintViolations.isEmpty());
|
118
|
}
|
119
|
|
120
|
@Test
|
121
|
public final void testNoRecursiveInReference(){
|
122
|
Reference myRef = ReferenceFactory.newBookSection();
|
123
|
myRef.setInReference(myRef);
|
124
|
myRef.setTitle("My book section");
|
125
|
assertHasConstraintOnValidator((Set)validator.validate(myRef, Level3.class), NoRecursiveInReferenceValidator.class);
|
126
|
|
127
|
myRef.setInBook(book);
|
128
|
assertNoConstraintOnValidator((Set)validator.validate(myRef, Level3.class), NoRecursiveInReferenceValidator.class);
|
129
|
|
130
|
}
|
131
|
|
132
|
|
133
|
}
|