Project

General

Profile

Download (6.85 KB) Statistics
| Branch: | Tag: | Revision:
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.common;
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.test.unit.EntityTestBase;
26

    
27

    
28
public class OrderedTermVocabularyTest extends EntityTestBase {
29
	@SuppressWarnings("unused")
30
    private static Logger logger = Logger.getLogger(OrderedTermVocabularyTest.class);
31

    
32
	private OrderedTermBase<?> otb1;
33
	private OrderedTermBase<?> otb2;
34
	private OrderedTermBase<?> otb3;
35
	private OrderedTermBase<?> otbFree;
36
	private OrderedTermVocabulary<OrderedTermBase<?>> oVoc1;
37
	private OrderedTermVocabulary<OrderedTermBase<?>> oVoc2;
38

    
39
	@Before
40
	public void setUp() throws Exception {
41
		otb1 = new DerivedOrderedTermBase(TermType.Unknown,"otb1", "high", null);
42
		otb2 = new DerivedOrderedTermBase(TermType.Unknown, "term", "middel", null);
43
		otb3 = new DerivedOrderedTermBase(TermType.Unknown, "otb3", "low", null);
44
		otbFree = new DerivedOrderedTermBase();
45
		oVoc1 = new OrderedTermVocabulary<>();
46
		oVoc1.addTerm(otb1);
47
		oVoc1.addTerm(otb2);
48
		oVoc1.addTerm(otb3);
49
	}
50

    
51

    
52
	private class DerivedOrderedTermBase extends OrderedTermBase<DerivedOrderedTermBase>{
53
        private static final long serialVersionUID = -6661559531712274867L;
54
        private DerivedOrderedTermBase(){
55
			super(TermType.Unknown);
56
		}
57
		private DerivedOrderedTermBase(TermType type, String term, String label, String labelAbbrev){
58
			super(type, term, label, labelAbbrev);
59
		}
60
		@Override
61
		protected void setDefaultTerms(TermVocabulary<DerivedOrderedTermBase> termVocabulary) {}
62
		@Override
63
		public void resetTerms() {}
64
	}
65

    
66

    
67
//*************************** TESTS *************************************/
68

    
69
	@Test
70
	public final void testSetUp() {
71
		assertEquals(3, oVoc1.size());
72
		assertEquals(otb3, oVoc1.getLowestTerm());
73
		assertEquals(otb1, oVoc1.getHighestTerm());
74
		assertEquals(0, oVoc1.getHigherTerms(otb1).size());
75
		assertEquals(0, oVoc1.getLowerTerms(otb3).size());
76
	}
77

    
78
	@Test
79
	public final void testGetNewTermSet() {
80
		assertNotNull(oVoc1.getNewTermSet());
81
		assertTrue(SortedSet.class.isAssignableFrom(oVoc1.getNewTermSet().getClass()));
82
	}
83

    
84

    
85

    
86
	@Test
87
	public final void testGetTerms() {
88
		assertEquals(3, oVoc1.getTerms().size());
89
//		assertNotSame(oVoc1.terms, oVoc1.getTerms());
90
		assertTrue( oVoc1.terms.getClass().isAssignableFrom(oVoc1.getTerms().getClass()));
91
	}
92

    
93
	@Test
94
	public final void testAddTerm() {
95
		assertEquals(3, oVoc1.size());
96
		assertEquals(otb3, oVoc1.getLowestTerm());
97
		oVoc1.addTerm(otbFree);
98

    
99
		assertEquals(4, oVoc1.size());
100
		assertEquals(otbFree, oVoc1.getLowestTerm());
101
	}
102

    
103
	@Test
104
	public final void testRemoveTerm() {
105
		assertEquals(3, oVoc1.size());
106
		assertEquals(otb3, oVoc1.getLowestTerm());
107
		oVoc1.removeTerm(otb3);
108
		assertEquals(2, oVoc1.size());
109
		assertEquals(otb2, oVoc1.getLowestTerm());
110
		oVoc1.removeTerm(otb1);
111
		assertEquals(1, oVoc1.size());
112
		assertEquals(otb2, oVoc1.getLowestTerm());
113
		assertEquals(otb2, oVoc1.getHighestTerm());
114
		oVoc1.removeTerm(otb2);
115
		assertEquals(0, oVoc1.size());
116
		assertEquals(null, oVoc1.getHighestTerm());
117
	}
118

    
119
	@Test
120
	public final void testOrderedTermVocabulary() {
121
		assertNotNull(oVoc1);
122
	}
123

    
124
	@Test
125
	public final void testOrderedTermVocabularyStringStringString() {
126
		oVoc2 = new OrderedTermVocabulary<>(TermType.Unknown, "term", "label", null, URI.create("http://term.Source.Uri"));
127
		assertEquals("label", oVoc2.getLabel());
128
	}
129

    
130
	@Test
131
	public final void testGetLowerTerms() {
132
		assertEquals(0, oVoc1.getLowerTerms(otb3).size());
133
		assertEquals(1, oVoc1.getLowerTerms(otb2).size());
134
		assertEquals(2, oVoc1.getLowerTerms(otb1).size());
135
		assertEquals(otb2, oVoc1.getLowerTerms(otb1).last());
136
	}
137

    
138

    
139
	@Test
140
	@Ignore
141
	public final void testGetEqualTerms() {
142
		assertEquals(1, oVoc1.getEqualTerms(otb1).size());
143
//		otbFree.orderIndex = otb2.orderIndex;
144
//		oVoc1.addTerm(otbFree);
145
		assertEquals(3, oVoc1.size());
146
		assertEquals(1, oVoc1.getEqualTerms(otb1).size());
147
		assertEquals(1, oVoc1.getEqualTerms(otb2).size());
148
		assertEquals(1, oVoc1.getEqualTerms(otb3).size());
149
		oVoc1.addTermEqualLevel(otbFree, otb2);
150
		assertEquals(4, oVoc1.size());
151
		assertEquals(2, oVoc1.getEqualTerms(otb2).size());
152

    
153
		//as long as orderedTermVocabulary.terms is a set
154
		//this won't work because terms.add() will not result
155
		//in adding the term
156

    
157
	}
158

    
159
	@Test
160
	public final void testGetHigherTerms() {
161
		assertEquals(2, oVoc1.getHigherTerms(otb3).size());
162
		assertEquals(1, oVoc1.getHigherTerms(otb2).size());
163
		assertEquals(0, oVoc1.getHigherTerms(otb1).size());
164
		assertEquals(otb2, oVoc1.getHigherTerms(otb3).first());
165
	}
166

    
167
	@Test
168
	public final void testGetNextHigherTerm() {
169
		assertEquals(otb2.getLabel(), oVoc1.getNextHigherTerm(otb3).getLabel());
170
		assertEquals(null, oVoc1.getNextHigherTerm(otb1));
171
	}
172

    
173
	@Test
174
	public final void testGetNextLowerTerm() {
175
		assertEquals(otb2.getLabel(), oVoc1.getNextLowerTerm(otb1).getLabel());
176
		assertEquals(null, oVoc1.getNextLowerTerm(otb3));
177
	}
178

    
179
	@Test
180
	public final void testAddTermAbove() {
181
		try {
182
			oVoc1.addTermAbove(otbFree, otb2);
183
		} catch (Exception e) {
184
			fail();
185
		}
186
		assertEquals(2, oVoc1.getLowerTerms(otbFree).size());
187
		assertEquals(otbFree.getLabel(), oVoc1.getNextLowerTerm(otb1).getLabel());
188
		assertEquals(otbFree.getLabel(), oVoc1.getNextHigherTerm(otb2).getLabel());
189
	}
190

    
191
	@Test
192
	public final void testAddTermBelow() {
193
		try {
194
			oVoc1.addTermBelow(otbFree, otb2);
195
		} catch (Exception e) {
196
			fail();
197
		}
198
		assertEquals(1, oVoc1.getLowerTerms(otbFree).size());
199
		assertEquals(otbFree.getLabel(), oVoc1.getNextLowerTerm(otb2).getLabel());
200
		assertEquals(otbFree.getLabel(), oVoc1.getNextHigherTerm(otb3).getLabel());
201
	}
202

    
203
	@Test
204
	public final void testAddTermEqualLevel() {
205
		System.out.println(otb2.orderIndex);
206
		oVoc1.addTermEqualLevel(otbFree, otb2);
207

    
208
		assertEquals(1, oVoc1.getLowerTerms(otbFree).size());
209
		assertEquals(2, oVoc1.getLowerAndEqualTerms(otbFree).size());
210
		assertEquals(otb1.getLabel(), oVoc1.getNextHigherTerm(otbFree).getLabel());
211
		assertEquals(otb3.getLabel(), oVoc1.getNextLowerTerm(otbFree).getLabel());
212
	}
213

    
214
	@Test
215
	public final void testIndexChangeAllowed() {
216
//		assertFalse(oVoc1.indexChangeAllowed(otb1));
217
	}
218

    
219
	@Test
220
	public final void testSize() {
221
		assertEquals(3, oVoc1.size());
222
		oVoc2 = new OrderedTermVocabulary<>();
223
		assertEquals(0, oVoc2.size());
224
	}
225
}
(13-13/19)