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.assertNotNull;
|
14
|
import static org.junit.Assert.assertTrue;
|
15
|
import static org.junit.Assert.fail;
|
16
|
|
17
|
import java.net.URI;
|
18
|
import java.util.SortedSet;
|
19
|
|
20
|
import org.apache.log4j.Logger;
|
21
|
import org.junit.Before;
|
22
|
import org.junit.Ignore;
|
23
|
import org.junit.Test;
|
24
|
|
25
|
import eu.etaxonomy.cdm.model.term.OrderedTermBase;
|
26
|
import eu.etaxonomy.cdm.model.term.OrderedTermVocabulary;
|
27
|
import eu.etaxonomy.cdm.model.term.TermType;
|
28
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
29
|
import eu.etaxonomy.cdm.test.unit.EntityTestBase;
|
30
|
|
31
|
|
32
|
public class OrderedTermVocabularyTest extends EntityTestBase {
|
33
|
@SuppressWarnings("unused")
|
34
|
private static Logger logger = Logger.getLogger(OrderedTermVocabularyTest.class);
|
35
|
|
36
|
private OrderedTermBase<?> otb1;
|
37
|
private OrderedTermBase<?> otb2;
|
38
|
private OrderedTermBase<?> otb3;
|
39
|
private OrderedTermBase<?> otbFree;
|
40
|
private OrderedTermVocabulary<OrderedTermBase<?>> oVoc1;
|
41
|
private OrderedTermVocabulary<OrderedTermBase<?>> oVoc2;
|
42
|
|
43
|
@Before
|
44
|
public void setUp() throws Exception {
|
45
|
otb1 = new DerivedOrderedTermBase(TermType.Unknown,"otb1", "high", null);
|
46
|
otb2 = new DerivedOrderedTermBase(TermType.Unknown, "term", "middel", null);
|
47
|
otb3 = new DerivedOrderedTermBase(TermType.Unknown, "otb3", "low", null);
|
48
|
otbFree = new DerivedOrderedTermBase();
|
49
|
oVoc1 = new OrderedTermVocabulary<>();
|
50
|
oVoc1.addTerm(otb1);
|
51
|
oVoc1.addTerm(otb2);
|
52
|
oVoc1.addTerm(otb3);
|
53
|
}
|
54
|
|
55
|
|
56
|
private class DerivedOrderedTermBase extends OrderedTermBase<DerivedOrderedTermBase>{
|
57
|
private static final long serialVersionUID = -6661559531712274867L;
|
58
|
private DerivedOrderedTermBase(){
|
59
|
super(TermType.Unknown);
|
60
|
}
|
61
|
private DerivedOrderedTermBase(TermType type, String term, String label, String labelAbbrev){
|
62
|
super(type, term, label, labelAbbrev);
|
63
|
}
|
64
|
@Override
|
65
|
protected void setDefaultTerms(TermVocabulary<DerivedOrderedTermBase> termVocabulary) {}
|
66
|
@Override
|
67
|
public void resetTerms() {}
|
68
|
}
|
69
|
|
70
|
|
71
|
//*************************** TESTS *************************************/
|
72
|
|
73
|
@Test
|
74
|
public final void testSetUp() {
|
75
|
assertEquals(3, oVoc1.size());
|
76
|
assertEquals(otb3, oVoc1.getLowestTerm());
|
77
|
assertEquals(otb1, oVoc1.getHighestTerm());
|
78
|
assertEquals(0, oVoc1.getHigherTerms(otb1).size());
|
79
|
assertEquals(0, oVoc1.getLowerTerms(otb3).size());
|
80
|
}
|
81
|
|
82
|
@Test
|
83
|
public final void testGetNewTermSet() {
|
84
|
assertNotNull(oVoc1.getNewTermSet());
|
85
|
assertTrue(SortedSet.class.isAssignableFrom(oVoc1.getNewTermSet().getClass()));
|
86
|
}
|
87
|
|
88
|
|
89
|
|
90
|
@Test
|
91
|
public final void testGetTerms() {
|
92
|
assertEquals(3, oVoc1.getTerms().size());
|
93
|
// assertNotSame(oVoc1.terms, oVoc1.getTerms());
|
94
|
assertTrue( oVoc1.terms.getClass().isAssignableFrom(oVoc1.getTerms().getClass()));
|
95
|
}
|
96
|
|
97
|
@Test
|
98
|
public final void testAddTerm() {
|
99
|
assertEquals(3, oVoc1.size());
|
100
|
assertEquals(otb3, oVoc1.getLowestTerm());
|
101
|
oVoc1.addTerm(otbFree);
|
102
|
|
103
|
assertEquals(4, oVoc1.size());
|
104
|
assertEquals(otbFree, oVoc1.getLowestTerm());
|
105
|
}
|
106
|
|
107
|
@Test
|
108
|
public final void testRemoveTerm() {
|
109
|
assertEquals(3, oVoc1.size());
|
110
|
assertEquals(otb3, oVoc1.getLowestTerm());
|
111
|
oVoc1.removeTerm(otb3);
|
112
|
assertEquals(2, oVoc1.size());
|
113
|
assertEquals(otb2, oVoc1.getLowestTerm());
|
114
|
oVoc1.removeTerm(otb1);
|
115
|
assertEquals(1, oVoc1.size());
|
116
|
assertEquals(otb2, oVoc1.getLowestTerm());
|
117
|
assertEquals(otb2, oVoc1.getHighestTerm());
|
118
|
oVoc1.removeTerm(otb2);
|
119
|
assertEquals(0, oVoc1.size());
|
120
|
assertEquals(null, oVoc1.getHighestTerm());
|
121
|
}
|
122
|
|
123
|
@Test
|
124
|
public final void testOrderedTermVocabulary() {
|
125
|
assertNotNull(oVoc1);
|
126
|
}
|
127
|
|
128
|
@Test
|
129
|
public final void testOrderedTermVocabularyStringStringString() {
|
130
|
oVoc2 = new OrderedTermVocabulary<>(TermType.Unknown, "term", "label", null, URI.create("http://term.Source.Uri"));
|
131
|
assertEquals("label", oVoc2.getLabel());
|
132
|
}
|
133
|
|
134
|
@Test
|
135
|
public final void testGetLowerTerms() {
|
136
|
assertEquals(0, oVoc1.getLowerTerms(otb3).size());
|
137
|
assertEquals(1, oVoc1.getLowerTerms(otb2).size());
|
138
|
assertEquals(2, oVoc1.getLowerTerms(otb1).size());
|
139
|
assertEquals(otb2, oVoc1.getLowerTerms(otb1).last());
|
140
|
}
|
141
|
|
142
|
|
143
|
@Test
|
144
|
@Ignore
|
145
|
public final void testGetEqualTerms() {
|
146
|
assertEquals(1, oVoc1.getEqualTerms(otb1).size());
|
147
|
// otbFree.orderIndex = otb2.orderIndex;
|
148
|
// oVoc1.addTerm(otbFree);
|
149
|
assertEquals(3, oVoc1.size());
|
150
|
assertEquals(1, oVoc1.getEqualTerms(otb1).size());
|
151
|
assertEquals(1, oVoc1.getEqualTerms(otb2).size());
|
152
|
assertEquals(1, oVoc1.getEqualTerms(otb3).size());
|
153
|
oVoc1.addTermEqualLevel(otbFree, otb2);
|
154
|
assertEquals(4, oVoc1.size());
|
155
|
assertEquals(2, oVoc1.getEqualTerms(otb2).size());
|
156
|
|
157
|
//as long as orderedTermVocabulary.terms is a set
|
158
|
//this won't work because terms.add() will not result
|
159
|
//in adding the term
|
160
|
|
161
|
}
|
162
|
|
163
|
@Test
|
164
|
public final void testGetHigherTerms() {
|
165
|
assertEquals(2, oVoc1.getHigherTerms(otb3).size());
|
166
|
assertEquals(1, oVoc1.getHigherTerms(otb2).size());
|
167
|
assertEquals(0, oVoc1.getHigherTerms(otb1).size());
|
168
|
assertEquals(otb2, oVoc1.getHigherTerms(otb3).first());
|
169
|
}
|
170
|
|
171
|
@Test
|
172
|
public final void testGetNextHigherTerm() {
|
173
|
assertEquals(otb2.getLabel(), oVoc1.getNextHigherTerm(otb3).getLabel());
|
174
|
assertEquals(null, oVoc1.getNextHigherTerm(otb1));
|
175
|
}
|
176
|
|
177
|
@Test
|
178
|
public final void testGetNextLowerTerm() {
|
179
|
assertEquals(otb2.getLabel(), oVoc1.getNextLowerTerm(otb1).getLabel());
|
180
|
assertEquals(null, oVoc1.getNextLowerTerm(otb3));
|
181
|
}
|
182
|
|
183
|
@Test
|
184
|
public final void testAddTermAbove() {
|
185
|
try {
|
186
|
oVoc1.addTermAbove(otbFree, otb2);
|
187
|
} catch (Exception e) {
|
188
|
fail();
|
189
|
}
|
190
|
assertEquals(2, oVoc1.getLowerTerms(otbFree).size());
|
191
|
assertEquals(otbFree.getLabel(), oVoc1.getNextLowerTerm(otb1).getLabel());
|
192
|
assertEquals(otbFree.getLabel(), oVoc1.getNextHigherTerm(otb2).getLabel());
|
193
|
}
|
194
|
|
195
|
@Test
|
196
|
public final void testAddTermBelow() {
|
197
|
try {
|
198
|
oVoc1.addTermBelow(otbFree, otb2);
|
199
|
} catch (Exception e) {
|
200
|
fail();
|
201
|
}
|
202
|
assertEquals(1, oVoc1.getLowerTerms(otbFree).size());
|
203
|
assertEquals(otbFree.getLabel(), oVoc1.getNextLowerTerm(otb2).getLabel());
|
204
|
assertEquals(otbFree.getLabel(), oVoc1.getNextHigherTerm(otb3).getLabel());
|
205
|
}
|
206
|
|
207
|
@Test
|
208
|
public final void testAddTermEqualLevel() {
|
209
|
System.out.println(otb2.orderIndex);
|
210
|
oVoc1.addTermEqualLevel(otbFree, otb2);
|
211
|
|
212
|
assertEquals(1, oVoc1.getLowerTerms(otbFree).size());
|
213
|
assertEquals(2, oVoc1.getLowerAndEqualTerms(otbFree).size());
|
214
|
assertEquals(otb1.getLabel(), oVoc1.getNextHigherTerm(otbFree).getLabel());
|
215
|
assertEquals(otb3.getLabel(), oVoc1.getNextLowerTerm(otbFree).getLabel());
|
216
|
}
|
217
|
|
218
|
@Test
|
219
|
public final void testIndexChangeAllowed() {
|
220
|
// assertFalse(oVoc1.indexChangeAllowed(otb1));
|
221
|
}
|
222
|
|
223
|
@Test
|
224
|
public final void testSize() {
|
225
|
assertEquals(3, oVoc1.size());
|
226
|
oVoc2 = new OrderedTermVocabulary<>();
|
227
|
assertEquals(0, oVoc2.size());
|
228
|
}
|
229
|
}
|