Project

General

Profile

Download (4.96 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
package eu.etaxonomy.cdm.validation;
10

    
11
import static org.junit.Assert.assertFalse;
12
import static org.junit.Assert.assertTrue;
13

    
14
import java.net.URISyntaxException;
15
import java.util.Set;
16

    
17
import javax.validation.ConstraintViolation;
18
import javax.validation.groups.Default;
19

    
20
import org.apache.log4j.Logger;
21
import org.junit.Assert;
22
import org.junit.Before;
23
import org.junit.Test;
24

    
25
import eu.etaxonomy.cdm.common.URI;
26
import eu.etaxonomy.cdm.model.reference.IBook;
27
import eu.etaxonomy.cdm.model.reference.IBookSection;
28
import eu.etaxonomy.cdm.model.reference.Reference;
29
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
30
import eu.etaxonomy.cdm.model.term.DefaultTermInitializer;
31
import eu.etaxonomy.cdm.validation.constraint.InReferenceValidator;
32
import eu.etaxonomy.cdm.validation.constraint.NoRecursiveInReferenceValidator;
33

    
34

    
35
/**
36
 *
37
 * @author ben.clark
38
 *
39
 */
40
@SuppressWarnings("unused")
41
public class ReferenceValidationTest extends ValidationTestBase {
42
	private static final Logger logger = Logger.getLogger(ReferenceValidationTest.class);
43

    
44
	private IBook book;
45

    
46
	@Before
47
	public void setUp() {
48
		DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
49
		vocabularyStore.initialize();
50
		book = ReferenceFactory.newBook();
51
		book.setTitleCache("Lorem ipsum", true);
52
		book.setIsbn("1-919795-99-5");
53
	}
54

    
55

    
56
/****************** TESTS *****************************/
57

    
58
	/**
59
	 * Test validation at the second level with a valid reference
60
	 */
61
	@Test
62
	public final void testLevel2ValidationWithValidBook() {
63
        Set<ConstraintViolation<IBook>> constraintViolations  = validator.validate(book, Level2.class, Default.class);
64
        assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
65
	}
66

    
67
	@Test
68
	public final void testLevel2ValidationWithValidISBN() {
69

    
70
        Set<ConstraintViolation<IBook>> constraintViolations  = validator.validate(book, Level2.class);
71
        assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
72
	}
73

    
74
	@Test
75
	public final void testLevel2ValidationWithInValidISBN() {
76
		book.setIsbn("1-9197954-99-5");
77
        Set<ConstraintViolation<IBook>> constraintViolations  = validator.validate(book, Level2.class);
78
        assertFalse("There should be a constraint violation as this book has an invalid ISBN number",constraintViolations.isEmpty());
79
	}
80

    
81
	@Test
82
	public final void testLevel2ValidationWithValidUri() {
83
		try {
84
			book.setUri(new URI("http://www.e-taxonomy.eu"));
85
		} catch (URISyntaxException e) {
86
			Assert.fail("URI is not valid");
87
		}
88
        Set<ConstraintViolation<IBook>> constraintViolations  = validator.validate(book, Level2.class);
89
        assertTrue("There should be no constraint violations as this book is valid at level 2",constraintViolations.isEmpty());
90
	}
91

    
92

    
93
   @Test
94
    public final void testLevel3ValidationWithInValidInReference() {
95

    
96
        IBookSection bookSection = ReferenceFactory.newBookSection();
97
        bookSection.setTitleCache("test", true);
98
        bookSection.setTitle("");
99
        bookSection.setInReference((Reference)book);
100
        Set<ConstraintViolation<IBookSection>> constraintViolations  = validator.validate(bookSection, Level3.class);
101
        assertTrue("There should be one constraint violation as this book has a valid Ref",constraintViolations.size() == 0);
102

    
103
        Reference article = ReferenceFactory.newArticle();
104
        article.setTitleCache("article", true);
105
        bookSection.setInReference(article);
106
        constraintViolations  = validator.validate(bookSection, Level3.class);
107
//        assertTrue("There should be a constraint violation as this book has an invalid inReference",constraintViolations.size() == 1);
108
        assertHasConstraintOnValidator((Set)constraintViolations, InReferenceValidator.class);
109
    }
110

    
111

    
112
	@Test
113
	public final void testValidationAfterCasting(){
114
		((Reference)book).castReferenceToArticle();
115
		Set<ConstraintViolation<IBook>> constraintViolations  = validator.validate(book, Level2.class);
116
        assertFalse("There should be one constraint violations as this article is not valid at level 2 (has an isbn)", constraintViolations.isEmpty());
117
	}
118

    
119
	@Test
120
	public final void testNoRecursiveInReference(){
121
	    Reference myRef = ReferenceFactory.newBookSection();
122
        myRef.setInReference(myRef);
123
        myRef.setTitle("My book section");
124
        assertHasConstraintOnValidator((Set)validator.validate(myRef, Level3.class), NoRecursiveInReferenceValidator.class);
125

    
126
        myRef.setInBook(book);
127
        assertNoConstraintOnValidator((Set)validator.validate(myRef, Level3.class), NoRecursiveInReferenceValidator.class);
128

    
129
	}
130

    
131

    
132
}
(6-6/12)