1
|
/**
|
2
|
* Copyright (C) 2007 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.model.term;
|
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.util.Iterator;
|
19
|
import java.util.Set;
|
20
|
import java.util.SortedSet;
|
21
|
import java.util.TreeSet;
|
22
|
|
23
|
import org.apache.log4j.Logger;
|
24
|
import org.junit.After;
|
25
|
import org.junit.AfterClass;
|
26
|
import org.junit.Before;
|
27
|
import org.junit.BeforeClass;
|
28
|
import org.junit.Test;
|
29
|
|
30
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
31
|
import eu.etaxonomy.cdm.model.term.OrderedTermBase;
|
32
|
import eu.etaxonomy.cdm.model.term.TermType;
|
33
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
34
|
import eu.etaxonomy.cdm.test.unit.EntityTestBase;
|
35
|
|
36
|
|
37
|
public class TermVocabularyTest extends EntityTestBase {
|
38
|
private static Logger logger = Logger.getLogger(TermVocabularyTest.class);
|
39
|
|
40
|
private DefinedTermBase<?> dtb1;
|
41
|
private DefinedTermBase<?> dtb2;
|
42
|
private DefinedTermBase<?> dtb3;
|
43
|
private DefinedTermBase<?> dtbFree;
|
44
|
private TermVocabulary<DefinedTermBase<?>> voc1;
|
45
|
private TermVocabulary<DefinedTermBase<?>> voc2;
|
46
|
|
47
|
|
48
|
@BeforeClass
|
49
|
public static void setUpBeforeClass() throws Exception {
|
50
|
}
|
51
|
|
52
|
@AfterClass
|
53
|
public static void tearDownAfterClass() throws Exception {
|
54
|
}
|
55
|
|
56
|
@Before
|
57
|
public void setUp() throws Exception {
|
58
|
dtb1 = new DerivedDefinedTermBase(TermType.Unknown, "otb1", "high", "h");
|
59
|
dtb1.setIdInVocabulary("x");
|
60
|
dtb2 = new DerivedDefinedTermBase(TermType.Unknown, "term", "middel", "m");
|
61
|
dtb3 = new DerivedDefinedTermBase(TermType.Unknown, "otb3", "low", "l");
|
62
|
dtbFree = new DerivedDefinedTermBase();
|
63
|
voc1 = new TermVocabulary<>();
|
64
|
voc1.addTerm(dtb1);
|
65
|
voc1.addTerm(dtb2);
|
66
|
voc1.addTerm(dtb3);
|
67
|
}
|
68
|
|
69
|
@After
|
70
|
public void tearDown() throws Exception {
|
71
|
}
|
72
|
|
73
|
private class DerivedDefinedTermBase extends OrderedTermBase<DerivedDefinedTermBase>{
|
74
|
private static final long serialVersionUID = 280869784120656292L;
|
75
|
private DerivedDefinedTermBase(){
|
76
|
super(TermType.Unknown);
|
77
|
}
|
78
|
private DerivedDefinedTermBase(TermType type, String term, String label, String labelAbbrev){
|
79
|
super(type, term, label, labelAbbrev);
|
80
|
}
|
81
|
@Override
|
82
|
protected void setDefaultTerms(TermVocabulary<DerivedDefinedTermBase> termVocabulary) {}
|
83
|
@Override
|
84
|
public void resetTerms() {}
|
85
|
}
|
86
|
|
87
|
/****************** TESTS ****************************************/
|
88
|
|
89
|
@Test
|
90
|
public final void testSetUp() {
|
91
|
assertEquals(3, voc1.size());
|
92
|
assertEquals(3, voc1.getTerms().size());
|
93
|
}
|
94
|
|
95
|
@Test
|
96
|
public final void testGetNewTermSet() {
|
97
|
// assertNotNull(voc1.getNewTermSet());
|
98
|
// assertTrue(Set.class.isAssignableFrom(voc1.getNewTermSet().getClass()));
|
99
|
}
|
100
|
|
101
|
@Test
|
102
|
public final void testTermVocabulary() {
|
103
|
assertNotNull(voc1);
|
104
|
}
|
105
|
|
106
|
@Test
|
107
|
public final void testTermVocabularyStringStringString() {
|
108
|
voc2 = new TermVocabulary<>(TermType.Unknown, "term", "label", null, URI.create("http://term.Source.Uri"));
|
109
|
assertEquals("label", voc2.getLabel());
|
110
|
}
|
111
|
|
112
|
@Test
|
113
|
public final void testTermIdInVocabularyComparator() {
|
114
|
assertNotNull(voc1);
|
115
|
Set<DefinedTermBase<?>> terms = voc1.getTerms();
|
116
|
TermIdInVocabularyComparator<DefinedTermBase<?>> comparator = new TermIdInVocabularyComparator<>();
|
117
|
int res = comparator.compare(dtb1, dtb2);
|
118
|
int res2 = comparator.compare(dtb2, dtb1);
|
119
|
assertEquals(res, -res2);
|
120
|
SortedSet<DefinedTermBase<?>> result = new TreeSet<>(comparator);
|
121
|
for (DefinedTermBase<?> term:terms){
|
122
|
result.add(term);
|
123
|
}
|
124
|
assertEquals(result.first(), dtb3);
|
125
|
assertEquals(result.last(), dtb1);
|
126
|
|
127
|
}
|
128
|
|
129
|
@Test
|
130
|
public final void testGetTerms() {
|
131
|
assertEquals(3, voc1.getTerms().size());
|
132
|
//assertNotSame(voc1.terms, voc1.getTerms());
|
133
|
assertTrue( voc1.terms.getClass().isAssignableFrom(voc1.getTerms().getClass()));
|
134
|
}
|
135
|
|
136
|
@Test
|
137
|
public final void testSetTerms() {
|
138
|
logger.warn("Not yet implemented"); // TODO
|
139
|
}
|
140
|
|
141
|
@Test
|
142
|
public final void testAddTerm() {
|
143
|
voc1.addTerm(dtbFree);
|
144
|
assertEquals(4, voc1.size());
|
145
|
}
|
146
|
|
147
|
@Test
|
148
|
public final void testAddTerm_2() {
|
149
|
// Rank rank = Rank.SPECIES();
|
150
|
// voc2 = new TermVocabulary<DefinedTermBase>();
|
151
|
// try {
|
152
|
// voc2.addTerm(rank);
|
153
|
// } catch (WrongTermTypeException e) {
|
154
|
// fail();
|
155
|
// }
|
156
|
// Language lang = Language.ENGLISH();
|
157
|
// try {
|
158
|
// voc2.addTerm(lang);
|
159
|
// fail("Exception should be thrown");
|
160
|
// } catch (WrongTermTypeException e) {
|
161
|
// assertTrue(true);
|
162
|
// }
|
163
|
}
|
164
|
|
165
|
@Test
|
166
|
public final void testRemoveTerm() {
|
167
|
assertEquals(3, voc1.size());
|
168
|
assertTrue(voc1.getTerms().contains(dtb3));
|
169
|
voc1.removeTerm(dtb3);
|
170
|
assertFalse(voc1.getTerms().contains(dtb3));
|
171
|
assertEquals(2, voc1.size());
|
172
|
voc1.removeTerm(dtb3);
|
173
|
assertEquals(2, voc1.size());
|
174
|
assertTrue(voc1.getTerms().contains(dtb1));
|
175
|
voc1.removeTerm(dtb1);
|
176
|
assertFalse(voc1.getTerms().contains(dtb1));
|
177
|
assertEquals(1, voc1.size());
|
178
|
assertTrue(voc1.getTerms().contains(dtb2));
|
179
|
voc1.removeTerm(dtb2);
|
180
|
assertFalse(voc1.getTerms().contains(dtb2));
|
181
|
assertEquals(0, voc1.size());
|
182
|
}
|
183
|
|
184
|
@Test
|
185
|
public final void testGetTermSourceUri() {
|
186
|
assertEquals(null, voc1.getTermSourceUri());
|
187
|
voc2 = new TermVocabulary<>(TermType.Unknown,"term", "label", null, URI.create("http://term.Source.Uri"));
|
188
|
assertEquals("http://term.Source.Uri", voc2.getTermSourceUri().toString());
|
189
|
}
|
190
|
|
191
|
@Test
|
192
|
public final void testSetTermSourceUri() {
|
193
|
voc1.setTermSourceUri(URI.create("http://term.Source.Uri"));
|
194
|
assertEquals("http://term.Source.Uri", voc1.getTermSourceUri().toString());
|
195
|
}
|
196
|
|
197
|
@Test
|
198
|
public final void testGetTermClass() {
|
199
|
// assertEquals(dtb1.getClass(), voc1.getTermClass());
|
200
|
}
|
201
|
|
202
|
@Test
|
203
|
public final void testIterator() {
|
204
|
Iterator<DefinedTermBase<?>> it = voc1.iterator();
|
205
|
int i = 0;
|
206
|
while (it.hasNext()){
|
207
|
i++;
|
208
|
assertTrue(voc1.getTerms().contains(it.next()));
|
209
|
}
|
210
|
assertEquals(3, i);
|
211
|
}
|
212
|
}
|