1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.model.taxon;
|
12
|
|
13
|
import static org.junit.Assert.assertEquals;
|
14
|
import static org.junit.Assert.assertFalse;
|
15
|
import static org.junit.Assert.assertNotSame;
|
16
|
import static org.junit.Assert.assertNull;
|
17
|
import static org.junit.Assert.assertSame;
|
18
|
import static org.junit.Assert.assertTrue;
|
19
|
|
20
|
import java.lang.reflect.Field;
|
21
|
import java.lang.reflect.Method;
|
22
|
import java.lang.reflect.Modifier;
|
23
|
import java.util.HashSet;
|
24
|
import java.util.Iterator;
|
25
|
import java.util.Set;
|
26
|
|
27
|
import junit.framework.Assert;
|
28
|
|
29
|
import org.apache.commons.lang.StringUtils;
|
30
|
import org.apache.log4j.Logger;
|
31
|
import org.junit.After;
|
32
|
import org.junit.AfterClass;
|
33
|
import org.junit.Before;
|
34
|
import org.junit.BeforeClass;
|
35
|
import org.junit.Test;
|
36
|
|
37
|
import eu.etaxonomy.cdm.model.agent.Person;
|
38
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
39
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
40
|
import eu.etaxonomy.cdm.model.name.Rank;
|
41
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
42
|
import eu.etaxonomy.cdm.model.name.ZoologicalName;
|
43
|
//import eu.etaxonomy.cdm.model.reference.Book;
|
44
|
//import eu.etaxonomy.cdm.model.reference.Journal;
|
45
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
46
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
47
|
|
48
|
/**
|
49
|
* @author a.mueller
|
50
|
* @created 01.04.2009
|
51
|
* @version 1.0
|
52
|
*/
|
53
|
public class TaxonomicTreeTest {
|
54
|
private static final Logger logger = Logger.getLogger(TaxonomicTreeTest.class);
|
55
|
|
56
|
private static String treeName1;
|
57
|
private static TaxonomicTree taxonomicTree1;
|
58
|
private static TaxonNode taxonNode1;
|
59
|
private static TaxonNode taxonNode2;
|
60
|
private static TaxonNode taxonNode3;
|
61
|
private static Taxon taxon1;
|
62
|
private static Taxon taxon2;
|
63
|
private static TaxonNameBase<?,?> taxonName1;
|
64
|
private static TaxonNameBase<?,?> taxonName2;
|
65
|
private static ReferenceBase ref1;
|
66
|
private static ReferenceBase ref2;
|
67
|
private ReferenceFactory refFactory;
|
68
|
|
69
|
|
70
|
|
71
|
/**
|
72
|
* @throws java.lang.Exception
|
73
|
*/
|
74
|
@BeforeClass
|
75
|
public static void setUpBeforeClass() throws Exception {
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* @throws java.lang.Exception
|
80
|
*/
|
81
|
@AfterClass
|
82
|
public static void tearDownAfterClass() throws Exception {
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* @throws java.lang.Exception
|
87
|
*/
|
88
|
@Before
|
89
|
public void setUp() throws Exception {
|
90
|
treeName1 = "Greuther, 1993";
|
91
|
refFactory = ReferenceFactory.newInstance();
|
92
|
taxonomicTree1 = TaxonomicTree.NewInstance(treeName1);
|
93
|
taxonName1 = BotanicalName.NewInstance(Rank.SPECIES());
|
94
|
taxonName1 = ZoologicalName.NewInstance(Rank.SPECIES());
|
95
|
ref1 = refFactory.newJournal();
|
96
|
ref2 = refFactory.newJournal();
|
97
|
taxon1 = Taxon.NewInstance(taxonName1, ref1);
|
98
|
taxon2 = Taxon.NewInstance(taxonName2, ref2);
|
99
|
|
100
|
//taxonNode1 = new TaxonNode(taxon1, taxonomicView1);
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* @throws java.lang.Exception
|
105
|
*/
|
106
|
@After
|
107
|
public void tearDown() throws Exception {
|
108
|
}
|
109
|
|
110
|
//****************************** TESTS *****************************************/
|
111
|
|
112
|
/**
|
113
|
* Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonomicTree#addRoot(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.reference.ReferenceBase, java.lang.String, eu.etaxonomy.cdm.model.taxon.Synonym)}.
|
114
|
*/
|
115
|
@Test
|
116
|
public void testAddRoot() {
|
117
|
TaxonNameBase<?,?> synonymName = BotanicalName.NewInstance(Rank.SPECIES());
|
118
|
Synonym synonym = Synonym.NewInstance(synonymName, ref1);
|
119
|
TaxonNode taxonNode1 = taxonomicTree1.addChildTaxon(taxon1, null, null, synonym);
|
120
|
|
121
|
|
122
|
|
123
|
|
124
|
//test root node
|
125
|
Set<TaxonNode> rootNodes = taxonomicTree1.getChildNodes();
|
126
|
assertFalse("List of root nodes should not be empty", rootNodes.isEmpty());
|
127
|
assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
|
128
|
TaxonNode root = rootNodes.iterator().next();
|
129
|
assertEquals(taxon1, root.getTaxon());
|
130
|
assertSame(taxonNode1, root);
|
131
|
assertNull(root.getReference());
|
132
|
assertNull(root.getMicroReference());
|
133
|
assertEquals(synonym, root.getSynonymToBeUsed());
|
134
|
|
135
|
//any node
|
136
|
Set<TaxonNode> allNodes = taxonomicTree1.getChildNodes();
|
137
|
assertFalse("List of root nodes should not be empty", allNodes.isEmpty());
|
138
|
assertEquals("Number of root nodes should be 1", 1, allNodes.size());
|
139
|
TaxonNode anyNode = allNodes.iterator().next();
|
140
|
assertSame("Taxon for TaxonNode should be the same added to the view", taxon1, anyNode.getTaxon());
|
141
|
assertSame("TaxonNode should be the same added to the view", taxonNode1, anyNode);
|
142
|
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonomicTree#isTaxonInView(eu.etaxonomy.cdm.model.taxon.Taxon)}.
|
147
|
*/
|
148
|
@Test
|
149
|
public void testIsTaxonInTree() {
|
150
|
taxonomicTree1.addChildTaxon(taxon1, null, null, null);
|
151
|
|
152
|
assertTrue(taxonomicTree1.isTaxonInTree(taxon1));
|
153
|
Taxon anyTaxon = Taxon.NewInstance(null, null);
|
154
|
assertFalse(taxonomicTree1.isTaxonInTree(anyTaxon));
|
155
|
}
|
156
|
|
157
|
|
158
|
/**
|
159
|
* Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonomicTree#makeRootChildOfOtherNode(eu.etaxonomy.cdm.model.taxon.TaxonNode, eu.etaxonomy.cdm.model.taxon.TaxonNode, eu.etaxonomy.cdm.model.reference.ReferenceBase, java.util.String)}.
|
160
|
*/
|
161
|
@Test
|
162
|
public void testMakeRootChildOfOtherNode() {
|
163
|
TaxonNode root1 = taxonomicTree1.addChildTaxon(taxon1, null, null, null);
|
164
|
TaxonNode root2 = taxonomicTree1.addChildTaxon(taxon2, null, null, null);
|
165
|
Taxon taxon3 = Taxon.NewInstance(null, null);
|
166
|
root2.addChildTaxon(taxon3, null, null, null);
|
167
|
String microRef = "p55";
|
168
|
|
169
|
assertFalse("Root1 must not yet be child of root 2", root2.getChildNodes().contains(root1));
|
170
|
assertNotSame("Root2 must not yet be parent of root 1", root2, root1.getParent());
|
171
|
assertEquals("view must contain 3 nodes", 3, taxonomicTree1.getAllNodes().size());
|
172
|
assertEquals("view must still contain 2 root", 2, taxonomicTree1.getChildNodes().size());
|
173
|
assertEquals("root2 must have 1 child", 1, root2.getChildNodes().size());
|
174
|
|
175
|
taxonomicTree1.makeTopmostNodeChildOfOtherNode(root1, root2, ref1, microRef);
|
176
|
assertTrue("Root1 must be child of root 2", root2.getChildNodes().contains(root1));
|
177
|
assertSame("Root2 must be parent of root 1", root2, root1.getParent());
|
178
|
assertEquals("view must contain 3 nodes", 3, taxonomicTree1.getAllNodes().size());
|
179
|
assertEquals("view must contain 1 root", 1, taxonomicTree1.getChildNodes().size());
|
180
|
assertEquals("new child node must have the expected reference for parent child relationship", ref1, root1.getReference());
|
181
|
assertEquals("new child node must have the expected micro reference for parent child relationship", microRef, root1.getMicroReference());
|
182
|
assertEquals("root2 must have 2 children", 2, root2.getChildNodes().size());
|
183
|
|
184
|
}
|
185
|
|
186
|
@Test
|
187
|
public void testIsTopmostInTree() {
|
188
|
TaxonNode root = taxonomicTree1.addChildTaxon(taxon1, null, null, null);
|
189
|
|
190
|
assertTrue(taxonomicTree1.isTaxonInTree(taxon1));
|
191
|
assertTrue(taxonomicTree1.isTopmostInTree(taxon1));
|
192
|
Taxon anyTaxon = Taxon.NewInstance(null, null);
|
193
|
assertFalse(taxonomicTree1.isTaxonInTree(anyTaxon));
|
194
|
assertFalse(taxonomicTree1.isTopmostInTree(anyTaxon));
|
195
|
Taxon child = Taxon.NewInstance(null, null);
|
196
|
root.addChildTaxon(child, null, null, null);
|
197
|
assertTrue(taxonomicTree1.isTaxonInTree(child));
|
198
|
assertFalse(taxonomicTree1.isTopmostInTree(child));
|
199
|
}
|
200
|
|
201
|
@Test
|
202
|
public void testGetTopmostNode() {
|
203
|
TaxonNode root = taxonomicTree1.addChildTaxon(taxon1, null, null, null);
|
204
|
|
205
|
assertEquals(root, taxonomicTree1.getTopmostNode(taxon1));
|
206
|
Taxon anyTaxon = Taxon.NewInstance(null, null);
|
207
|
assertFalse(taxonomicTree1.isTaxonInTree(anyTaxon));
|
208
|
assertNull(taxonomicTree1.getTopmostNode(anyTaxon));
|
209
|
Taxon child = Taxon.NewInstance(null, null);
|
210
|
root.addChildTaxon(child, null, null, null);
|
211
|
assertTrue(taxonomicTree1.isTaxonInTree(child));
|
212
|
assertNull(taxonomicTree1.getTopmostNode(child));
|
213
|
}
|
214
|
|
215
|
@Test
|
216
|
public void testAddParentChild() {
|
217
|
|
218
|
TaxonNameBase<?,?> synonymName = BotanicalName.NewInstance(Rank.SPECIES());
|
219
|
Synonym synonym = Synonym.NewInstance(synonymName, ref1);
|
220
|
TaxonNode rootNode = taxonomicTree1.addChildTaxon(taxon1, null, null, synonym);
|
221
|
Assert.assertEquals(0,rootNode.getChildNodes().size());
|
222
|
|
223
|
//add child to existing root
|
224
|
taxonomicTree1.addParentChild(taxon1, taxon2, ref1, "Micro1");
|
225
|
Assert.assertTrue(taxonomicTree1.isTaxonInTree(taxon2));
|
226
|
Assert.assertFalse(taxonomicTree1.isTopmostInTree(taxon2));
|
227
|
Assert.assertEquals(1,rootNode.getChildNodes().size());
|
228
|
TaxonNode childNode = rootNode.getChildNodes().iterator().next();
|
229
|
Assert.assertEquals(taxon2, childNode.getTaxon());
|
230
|
|
231
|
//relationship already exists
|
232
|
taxonomicTree1.addParentChild(taxon1, taxon2, ref2, "Micro2");
|
233
|
Assert.assertTrue(taxonomicTree1.isTaxonInTree(taxon2));
|
234
|
Assert.assertFalse(taxonomicTree1.isTopmostInTree(taxon2));
|
235
|
Assert.assertEquals(2, taxonomicTree1.getAllNodes().size());
|
236
|
Assert.assertEquals(1,rootNode.getChildNodes().size());
|
237
|
childNode = rootNode.getChildNodes().iterator().next();
|
238
|
Assert.assertEquals(taxon2, childNode.getTaxon());
|
239
|
Assert.assertEquals(ref2, childNode.getReference());
|
240
|
Assert.assertEquals("Micro2", childNode.getMicroReference());
|
241
|
|
242
|
logger.info("testAddParentChild not yet fully implemented");
|
243
|
|
244
|
}
|
245
|
|
246
|
/**
|
247
|
* Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonomicTree#generateTitle()}.
|
248
|
*/
|
249
|
@Test
|
250
|
public void testGenerateTitle() {
|
251
|
TaxonomicTree taxonomicViewLocal = TaxonomicTree.NewInstance(treeName1);
|
252
|
//Maybe changed if title cache is generated in a different way
|
253
|
assertEquals(treeName1, taxonomicViewLocal.getTitleCache());
|
254
|
}
|
255
|
|
256
|
/**
|
257
|
* Test method for {@link eu.etaxonomy.cdm.model.taxon.TaxonomicTree#generateTitle()}.
|
258
|
*/
|
259
|
@Test
|
260
|
public void play() {
|
261
|
|
262
|
CdmBase referencedCdmBase = Person.NewInstance();
|
263
|
Set<Class<? extends CdmBase>> allCdmClasses = findAllCdmClasses();
|
264
|
|
265
|
Class referencedClass = referencedCdmBase.getClass();
|
266
|
Set<CdmBase> result = new HashSet<CdmBase>();
|
267
|
System.out.println("Referenced Class: " + referencedClass.getName());
|
268
|
|
269
|
|
270
|
for (Class<? extends CdmBase> cdmClass : allCdmClasses){
|
271
|
Set<Field> fields = getFields(cdmClass);
|
272
|
for (Field field: fields){
|
273
|
Class<?> type = field.getType();
|
274
|
if (! type.isInterface()){
|
275
|
if (referencedClass.isAssignableFrom(type)||
|
276
|
type.isAssignableFrom(referencedClass) && CdmBase.class.isAssignableFrom(type)){
|
277
|
handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase);
|
278
|
}
|
279
|
}else{ //interface
|
280
|
if (type.isAssignableFrom(referencedClass)){
|
281
|
handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase);
|
282
|
}
|
283
|
}
|
284
|
// Class[] interfaces = referencedClass.getInterfaces();
|
285
|
// for (Class interfaze: interfaces){
|
286
|
// if (interfaze == type){
|
287
|
//// if(interfaze.isAssignableFrom(returnType)){
|
288
|
// }
|
289
|
// }
|
290
|
|
291
|
|
292
|
}
|
293
|
}
|
294
|
return ;
|
295
|
// find(cdmClass, )
|
296
|
|
297
|
}
|
298
|
|
299
|
private boolean handleSingleClass(Class classToBeSearchedFor, Class type, Field field, Class cdmClass, Set<CdmBase> result,CdmBase value){
|
300
|
if (! Modifier.isStatic(field.getModifiers())){
|
301
|
String methodName = StringUtils.rightPad(field.getName(), 30);
|
302
|
String className = StringUtils.rightPad(cdmClass.getSimpleName(), 30);
|
303
|
String returnTypeName = StringUtils.rightPad(type.getSimpleName(), 30);
|
304
|
|
305
|
System.out.println(methodName + "\t\t" + className + "\t\t" + returnTypeName);
|
306
|
// result_old.add(method);
|
307
|
result.addAll(getCdmBasesByFieldAndClass(field, cdmClass, value));
|
308
|
}
|
309
|
return true;
|
310
|
}
|
311
|
|
312
|
private Set<Field> getFields(Class clazz){
|
313
|
Set<Field> result = new HashSet<Field>();
|
314
|
for (Field field: clazz.getDeclaredFields()){
|
315
|
if (!Modifier.isStatic(field.getModifiers())){
|
316
|
result.add(field);
|
317
|
}
|
318
|
}
|
319
|
Class superclass = clazz.getSuperclass();
|
320
|
if (CdmBase.class.isAssignableFrom(superclass)){
|
321
|
result.addAll(getFields(superclass));
|
322
|
}
|
323
|
return result;
|
324
|
}
|
325
|
|
326
|
private Set<CdmBase> getCdmBasesByFieldAndClass(Field field, Class clazz, CdmBase value){
|
327
|
//FIXME make not dummy but use dao
|
328
|
Set<CdmBase> result = new HashSet<CdmBase>();
|
329
|
|
330
|
//genericDao.getCdmBasesByFieldAndClass(clazz, field.getName(), value);
|
331
|
|
332
|
|
333
|
BotanicalName name = BotanicalName.NewInstance(Rank.GENUS());
|
334
|
name.setTitleCache("A dummy name", true);
|
335
|
result.add(name);
|
336
|
ReferenceBase ref = refFactory.newBook();
|
337
|
ref.setTitleCache("A dummy book", true);
|
338
|
result.add(ref);
|
339
|
|
340
|
return result;
|
341
|
}
|
342
|
private Set<Class<? extends CdmBase>> findAllCdmClasses(){
|
343
|
|
344
|
|
345
|
//init
|
346
|
Set<Class<? extends CdmBase>> allCdmClasses = new HashSet<Class<? extends CdmBase>>();
|
347
|
allCdmClasses.add(TaxonBase.class);
|
348
|
allCdmClasses.add(BotanicalName.class);
|
349
|
|
350
|
int count;
|
351
|
do{
|
352
|
count = allCdmClasses.size();
|
353
|
Set<Class<? extends CdmBase>> iteratorSet = new HashSet<Class<? extends CdmBase>>();
|
354
|
iteratorSet.addAll(allCdmClasses);
|
355
|
for (Class<? extends CdmBase> cdmClass : iteratorSet){
|
356
|
Method[] methods = cdmClass.getMethods();
|
357
|
for (Method method: methods){
|
358
|
Class<?> returnType = method.getReturnType();
|
359
|
handleClass(allCdmClasses,returnType);
|
360
|
Class<?>[] params = method.getParameterTypes();
|
361
|
for (Class paramClass : params){
|
362
|
handleClass(allCdmClasses, paramClass);
|
363
|
}
|
364
|
}
|
365
|
}
|
366
|
}while (allCdmClasses.size() > count);
|
367
|
boolean withAbstract = false;
|
368
|
if (! withAbstract){
|
369
|
Iterator<Class<? extends CdmBase>> iterator = allCdmClasses.iterator();
|
370
|
while (iterator.hasNext()){
|
371
|
Class clazz = iterator.next();
|
372
|
if (Modifier.isAbstract(clazz.getModifiers())){
|
373
|
iterator.remove();
|
374
|
}
|
375
|
}
|
376
|
}
|
377
|
return allCdmClasses;
|
378
|
}
|
379
|
|
380
|
private void handleClass(Set<Class<? extends CdmBase>> allCdmClasses, Class returnType){
|
381
|
if (CdmBase.class.isAssignableFrom(returnType)){
|
382
|
if (! allCdmClasses.contains(returnType)){
|
383
|
//System.out.println(returnType.getSimpleName());
|
384
|
allCdmClasses.add((Class)returnType);
|
385
|
Class superClass = returnType.getSuperclass();
|
386
|
handleClass(allCdmClasses, superClass);
|
387
|
}
|
388
|
}
|
389
|
}
|
390
|
|
391
|
}
|