Merge branch 'release/5.45.0'
[cdmlib.git] / cdmlib-model / src / test / java / eu / etaxonomy / cdm / validation / CorrectEpithetsForRankTest.java
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.util.Set;
15
16 import javax.validation.ConstraintViolation;
17 import javax.validation.groups.Default;
18
19 import org.apache.logging.log4j.LogManager;
20 import org.apache.logging.log4j.Logger;
21 import org.junit.Before;
22 import org.junit.Test;
23
24 import eu.etaxonomy.cdm.model.name.IBotanicalName;
25 import eu.etaxonomy.cdm.model.name.Rank;
26 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
27 import eu.etaxonomy.cdm.validation.constraint.CorrectEpithetsForRankValidator;
28
29 /**
30 * NOTE: In this test, the words "valid" and "invalid", loaded though
31 * these terms are when applied to taxonomic names, only mean "passes the
32 * rules of this validator" or not and should not be confused with the strict
33 * nomenclatural and taxonomic sense of these words.
34 *
35 * @author ben.clark
36 */
37 public class CorrectEpithetsForRankTest extends ValidationTestBase {
38
39 @SuppressWarnings("unused")
40 private static final Logger logger = LogManager.getLogger();
41
42 private IBotanicalName name;
43
44 @Before
45 public void setUp() {
46 name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
47 name.setNameCache("Aus aus");
48 name.setAuthorshipCache("L.");
49 name.setFullTitleCache("Aus aus L.");
50 name.setTitleCache("Aus aus L.", true);
51 }
52
53 /****************** TESTS *****************************/
54
55 @Test
56 public void testValidSpecificName() {
57 name.setGenusOrUninomial("Aus");
58 name.setSpecificEpithet("aus");
59 Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name, Level2.class, Default.class);
60 assertTrue("There should be no constraint violations as this name has the correct epithets for its rank", constraintViolations.isEmpty());
61 }
62
63 @Test
64 public void testInValidSpecificName() {
65 name.setGenusOrUninomial("Aus");
66 name.setSpecificEpithet(null); // at the default level, this property can be null
67 Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name, Level2.class);
68 assertFalse("There should be a constraint violation as this name does not have a specific epithet", constraintViolations.isEmpty());
69 assertHasConstraintOnValidator((Set)constraintViolations, CorrectEpithetsForRankValidator.class);
70 }
71
72 @Test
73 public void testValidFamilyGroupName() {
74 name.setGenusOrUninomial("Aus");
75 name.setRank(Rank.FAMILY());
76 Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name, Level2.class);
77 assertTrue("There should be no constraint violations as this name has the correct epithets for it rank", constraintViolations.isEmpty());
78 }
79
80 @Test
81 public void testInValidFamilyGroupName() {
82 name.setGenusOrUninomial("Aus");
83 name.setRank(Rank.FAMILY()); // at the default level, this property can be null
84 name.setSpecificEpithet("aus");
85 Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name, Level2.class);
86 assertFalse("There should be a constraint violation as this name does not have a specific epithet", constraintViolations.isEmpty());
87 }
88
89 @Test
90 public void testValidGenusGroupName() {
91 name.setGenusOrUninomial("Aus");
92 name.setInfraGenericEpithet("bus");
93 name.setRank(Rank.SUBGENUS());
94 Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name, Level2.class);
95 assertTrue("There should be no constraint violations as this name has the correct epithets for it rank",constraintViolations.isEmpty());
96 }
97
98 @Test
99 public void testInValidGenusGroupName() {
100 name.setGenusOrUninomial("Aus");
101 name.setRank(Rank.SUBGENUS()); // at the default level, this property can be null
102 name.setSpecificEpithet("aus");
103 Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name, Level2.class);
104 assertFalse("There should be a constraint violation as this name does not have a specific epithet",constraintViolations.isEmpty());
105 }
106
107 @Test
108 public void testValidInfraSpecificName() {
109 name.setGenusOrUninomial("Aus");
110 name.setSpecificEpithet("aus");
111 name.setInfraSpecificEpithet("ceus");
112 name.setRank(Rank.SUBSPECIES());
113 Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name, Level2.class);
114 assertTrue("There should be no constraint violations as this name has the correct epithets for it rank",constraintViolations.isEmpty());
115 }
116
117 @Test
118 public void testInValidInfraSpecificName() {
119 name.setGenusOrUninomial("Aus");
120 name.setRank(Rank.SUBGENUS()); // at the default level, this property can be null
121 name.setSpecificEpithet("aus");
122 Set<ConstraintViolation<IBotanicalName>> constraintViolations = validator.validate(name, Level2.class);
123 assertFalse("There should be a constraint violation as this name does not have a specific epithet",constraintViolations.isEmpty());
124 }
125 }