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