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