Project

General

Profile

Download (15.4 KB) Statistics
| Branch: | Tag: | Revision:
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.List;
26
import java.util.Set;
27

    
28
import org.apache.commons.lang.StringUtils;
29
import org.apache.log4j.Logger;
30
import org.junit.After;
31
import org.junit.AfterClass;
32
import org.junit.Assert;
33
import org.junit.Before;
34
import org.junit.BeforeClass;
35
import org.junit.Test;
36
import org.springframework.beans.BeanUtils;
37

    
38
import eu.etaxonomy.cdm.model.agent.Person;
39
import eu.etaxonomy.cdm.model.common.CdmBase;
40
import eu.etaxonomy.cdm.model.name.BotanicalName;
41
import eu.etaxonomy.cdm.model.name.Rank;
42
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
43
import eu.etaxonomy.cdm.model.name.ZoologicalName;
44
//import eu.etaxonomy.cdm.model.reference.Book;
45
//import eu.etaxonomy.cdm.model.reference.Journal;
46
import eu.etaxonomy.cdm.model.reference.Reference;
47
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
48

    
49
/**
50
 * @author a.mueller
51
 * @created 01.04.2009
52
 * @version 1.0
53
 */
54
public class ClassificationTest {
55
	private static final Logger logger = Logger.getLogger(ClassificationTest.class);
56

    
57
	private static String treeName1;
58
	private static Classification classification1;
59
	private static TaxonNode taxonNode1;
60
	private static TaxonNode taxonNode2;
61
	private static TaxonNode taxonNode3;
62
	private static TaxonNode taxonNode12;
63
	private static TaxonNode taxonNode121;
64
	private static Taxon taxon1;
65
	private static Taxon taxon2;
66
	private static Taxon taxon3;
67
	private static Taxon taxon12;
68
	private static Taxon taxon121;
69
	private static TaxonNameBase<?,?> taxonName1;
70
	private static TaxonNameBase<?,?> taxonName2;
71
	private static TaxonNameBase<?,?> taxonName3;
72
	private static TaxonNameBase<?,?> taxonName12;
73
	private static TaxonNameBase<?,?> taxonName121;
74
	private static Reference ref1;
75
	private static Reference ref2;
76
	private static Reference ref3;
77
	//private ReferenceFactory refFactory;
78

    
79

    
80

    
81
	/**
82
	 * @throws java.lang.Exception
83
	 */
84
	@BeforeClass
85
	public static void setUpBeforeClass() throws Exception {
86
	}
87

    
88
	/**
89
	 * @throws java.lang.Exception
90
	 */
91
	@AfterClass
92
	public static void tearDownAfterClass() throws Exception {
93
	}
94

    
95
	/**
96
	 * @throws java.lang.Exception
97
	 */
98
	@Before
99
	public void setUp() throws Exception {
100
		treeName1 = "Greuther, 1993";
101
		//refFactory = ReferenceFactory.newInstance();
102
		classification1 = Classification.NewInstance(treeName1);
103
		taxonName12 = BotanicalName.NewInstance(Rank.SPECIES());
104
		taxonName121 = BotanicalName.NewInstance(Rank.SUBSPECIES());
105
		taxonName1 = BotanicalName.NewInstance(Rank.GENUS());
106
		taxonName2 = ZoologicalName.NewInstance(Rank.GENUS());
107
		taxonName3 = BotanicalName.NewInstance(Rank.SPECIES());
108
		ref1 = ReferenceFactory.newJournal();
109
		ref2 = ReferenceFactory.newJournal();
110
		ref3 = ReferenceFactory.newJournal();
111
		taxon1 = Taxon.NewInstance(taxonName1, ref1);
112
		taxon2 = Taxon.NewInstance(taxonName2, ref2);
113
		taxon3 = Taxon.NewInstance(taxonName3, ref3);
114
		taxon12 = Taxon.NewInstance(taxonName12, ref3);
115
		taxon121 = Taxon.NewInstance(taxonName121, ref3);
116

    
117
		//taxonNode1 = new TaxonNode(taxon1, taxonomicView1);
118
	}
119

    
120
	/**
121
	 * @throws java.lang.Exception
122
	 */
123
	@After
124
	public void tearDown() throws Exception {
125
	}
126

    
127
//****************************** TESTS *****************************************/
128

    
129
	/**
130
	 * 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)}.
131
	 */
132
	@Test
133
	public void testAddRoot() {
134
		TaxonNameBase<?,?> synonymName = BotanicalName.NewInstance(Rank.SPECIES());
135
		Synonym synonym = Synonym.NewInstance(synonymName, ref1);
136
		TaxonNode taxonNode1 = classification1.addChildTaxon(taxon1, null, null);
137
		taxonNode1.setSynonymToBeUsed(synonym);
138

    
139

    
140

    
141
		//test root node
142
		List<TaxonNode> rootNodes = classification1.getChildNodes();
143
		assertFalse("List of root nodes should not be empty", rootNodes.isEmpty());
144
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
145
		TaxonNode root = rootNodes.iterator().next();
146
		assertEquals(taxon1, root.getTaxon());
147
		assertSame(taxonNode1, root);
148
		assertNull(root.getReference());
149
		assertNull(root.getMicroReference());
150
		assertEquals(synonym, root.getSynonymToBeUsed());
151

    
152
		//any node
153
		List<TaxonNode> allNodes = classification1.getChildNodes();
154
		assertFalse("List of root nodes should not be empty", allNodes.isEmpty());
155
		assertEquals("Number of root nodes should be 1", 1, allNodes.size());
156
		TaxonNode anyNode = allNodes.iterator().next();
157
		assertSame("Taxon for TaxonNode should be the same added to the view", taxon1, anyNode.getTaxon());
158
		assertSame("TaxonNode should be the same added to the view", taxonNode1, anyNode);
159

    
160
	}
161

    
162
	/**
163
	 * Test method for {@link eu.etaxonomy.cdm.model.taxon.Classification#isTaxonInView(eu.etaxonomy.cdm.model.taxon.Taxon)}.
164
	 */
165
	@Test
166
	public void testIsTaxonInTree() {
167
		classification1.addChildTaxon(taxon1, null, null);
168

    
169
		assertTrue(classification1.isTaxonInTree(taxon1));
170
		Taxon anyTaxon = Taxon.NewInstance(null, null);
171
		assertFalse(classification1.isTaxonInTree(anyTaxon));
172
	}
173

    
174

    
175
	/**
176
	 * 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)}.
177
	 */
178
	@Test
179
	public void testMakeRootChildOfOtherNode() {
180
		TaxonNode root1 = classification1.addChildTaxon(taxon1, null, null);
181
		TaxonNode root2 = classification1.addChildTaxon(taxon2, null, null);
182
		Taxon taxon3 = Taxon.NewInstance(null, null);
183
		root2.addChildTaxon(taxon3, null, null);
184
		String microRef = "p55";
185

    
186
		assertFalse("Root1 must not yet be child of root 2", root2.getChildNodes().contains(root1));
187
		assertNotSame("Root2 must not yet be parent of root 1", root2, root1.getParent());
188
		assertEquals("view must contain 3 nodes", 3, classification1.getAllNodes().size());
189
		assertEquals("view must still contain 2 root", 2, classification1.getChildNodes().size());
190
		assertEquals("root2 must have 1 child", 1, root2.getChildNodes().size());
191

    
192
		classification1.makeTopmostNodeChildOfOtherNode(root1, root2, ref1, microRef);
193
		assertTrue("Root1 must be child of root 2", root2.getChildNodes().contains(root1));
194
		assertSame("Root2 must be parent of root 1", root2, root1.getParent());
195
		assertEquals("view must contain 3 nodes", 3, classification1.getAllNodes().size());
196
		assertEquals("view must contain 1 root", 1, classification1.getChildNodes().size());
197
		assertEquals("new child node must have the expected reference for parent child relationship", ref1, root1.getReference());
198
		assertEquals("new child node must have the expected micro reference for parent child relationship", microRef, root1.getMicroReference());
199
		assertEquals("root2 must have 2 children", 2, root2.getChildNodes().size());
200

    
201
	}
202

    
203
	@Test
204
	public void testIsTopmostInTree() {
205
		TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
206

    
207
		assertTrue(classification1.isTaxonInTree(taxon1));
208
		assertTrue(classification1.isTopmostInTree(taxon1));
209
		Taxon anyTaxon = Taxon.NewInstance(null, null);
210
		assertFalse(classification1.isTaxonInTree(anyTaxon));
211
		assertFalse(classification1.isTopmostInTree(anyTaxon));
212
		Taxon child = Taxon.NewInstance(null, null);
213
		root.addChildTaxon(child, null, null);
214
		assertTrue(classification1.isTaxonInTree(child));
215
		assertFalse(classification1.isTopmostInTree(child));
216
	}
217

    
218
	@Test
219
	public void testGetTopmostNode() {
220
		TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
221

    
222
		assertEquals(root, classification1.getTopmostNode(taxon1));
223
		Taxon anyTaxon = Taxon.NewInstance(null, null);
224
		assertFalse(classification1.isTaxonInTree(anyTaxon));
225
		assertNull(classification1.getTopmostNode(anyTaxon));
226
		Taxon child = Taxon.NewInstance(null, null);
227
		root.addChildTaxon(child, null, null);
228
		assertTrue(classification1.isTaxonInTree(child));
229
		assertNull(classification1.getTopmostNode(child));
230
	}
231

    
232
	@Test
233
	public void testAddParentChild() {
234

    
235
		TaxonNameBase<?,?> synonymName = BotanicalName.NewInstance(Rank.SPECIES());
236
		Synonym synonym = Synonym.NewInstance(synonymName, ref1);
237
		TaxonNode rootNode = classification1.addChildTaxon(taxon1, null, null);
238
		rootNode.setSynonymToBeUsed(synonym);
239
		Assert.assertEquals(0,rootNode.getChildNodes().size());
240

    
241
		//add child to existing root
242
		classification1.addParentChild(taxon1, taxon2, ref1, "Micro1");
243
		Assert.assertTrue(classification1.isTaxonInTree(taxon2));
244
		Assert.assertFalse(classification1.isTopmostInTree(taxon2));
245
		Assert.assertEquals(1,rootNode.getChildNodes().size());
246
		TaxonNode childNode = rootNode.getChildNodes().iterator().next();
247
		Assert.assertEquals(taxon2, childNode.getTaxon());
248

    
249
		//relationship already exists
250
		classification1.addParentChild(taxon1, taxon2, ref2, "Micro2");
251
		Assert.assertTrue(classification1.isTaxonInTree(taxon2));
252
		Assert.assertFalse(classification1.isTopmostInTree(taxon2));
253
		Assert.assertEquals(2, classification1.getAllNodes().size());
254
		Assert.assertEquals(1,rootNode.getChildNodes().size());
255
		childNode = rootNode.getChildNodes().iterator().next();
256
		Assert.assertEquals(taxon2, childNode.getTaxon());
257
		Assert.assertEquals(ref2, childNode.getReference());
258
		Assert.assertEquals("Micro2", childNode.getMicroReference());
259

    
260
		logger.info("testAddParentChild not yet fully implemented");
261

    
262
	}
263

    
264
	/**
265
	 * Test method for {@link eu.etaxonomy.cdm.model.taxon.Classification#generateTitle()}.
266
	 */
267
	@Test
268
	public void testGenerateTitle() {
269
		Classification taxonomicViewLocal = Classification.NewInstance(treeName1);
270
		//Maybe changed if title cache is generated in a different way
271
		assertEquals(treeName1, taxonomicViewLocal.getTitleCache());
272
	}
273

    
274
	/**
275
	 * Test method for {@link eu.etaxonomy.cdm.model.taxon.Classification#generateTitle()}.
276
	 */
277
	@Test
278
	public void play() {
279

    
280
			CdmBase referencedCdmBase = Person.NewInstance();
281
			Set<Class<? extends CdmBase>> allCdmClasses = findAllCdmClasses();
282

    
283
			Class<? extends CdmBase> referencedClass = referencedCdmBase.getClass();
284
			Set<CdmBase> result = new HashSet<CdmBase>();
285
			System.out.println("Referenced Class: " + referencedClass.getName());
286

    
287

    
288
			for (Class<? extends CdmBase> cdmClass : allCdmClasses){
289
				Set<Field> fields = getFields(cdmClass);
290
				for (Field field: fields){
291
					Class<?> type = field.getType();
292
					if (! type.isInterface()){
293
						if (referencedClass.isAssignableFrom(type)||
294
								type.isAssignableFrom(referencedClass) && CdmBase.class.isAssignableFrom(type)){
295
							handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase);
296
						}
297
					}else{  //interface
298
						if (type.isAssignableFrom(referencedClass)){
299
							handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase);
300
						}
301
					}
302
//					Class[] interfaces = referencedClass.getInterfaces();
303
//					for (Class interfaze: interfaces){
304
//						if (interfaze == type){
305
////						if(interfaze.isAssignableFrom(returnType)){
306
//						}
307
//					}
308

    
309

    
310
				}
311
			}
312
			return ;
313
//			find(cdmClass, )
314

    
315
		}
316

    
317
	private boolean handleSingleClass(Class<?> classToBeSearchedFor, Class<?> type, Field field, Class<?> cdmClass, Set<CdmBase> result, CdmBase value){
318
		if (! Modifier.isStatic(field.getModifiers())){
319
			String methodName = StringUtils.rightPad(field.getName(), 30);
320
			String className = StringUtils.rightPad(cdmClass.getSimpleName(), 30);
321
			String returnTypeName = StringUtils.rightPad(type.getSimpleName(), 30);
322

    
323
			System.out.println(methodName +   "\t\t" + className + "\t\t" + returnTypeName);
324
//			result_old.add(method);
325
			result.addAll(getCdmBasesByFieldAndClass(field, cdmClass, value));
326
		}
327
		return true;
328
	}
329

    
330
	private Set<Field> getFields(Class<?> clazz){
331
		Set<Field> result = new HashSet<Field>();
332
		for (Field field: clazz.getDeclaredFields()){
333
			if (!Modifier.isStatic(field.getModifiers())){
334
				result.add(field);
335
			}
336
		}
337
		Class<?> superclass = clazz.getSuperclass();
338
		if (CdmBase.class.isAssignableFrom(superclass)){
339
			result.addAll(getFields(superclass));
340
		}
341
		return result;
342
	}
343

    
344
	private Set<CdmBase> getCdmBasesByFieldAndClass(Field field, Class<?> clazz, CdmBase value){
345
		//FIXME make not dummy but use dao
346
		Set<CdmBase> result = new HashSet<CdmBase>();
347

    
348
		//genericDao.getCdmBasesByFieldAndClass(clazz, field.getName(), value);
349

    
350

    
351
		BotanicalName name = BotanicalName.NewInstance(Rank.GENUS());
352
		name.setTitleCache("A dummy name", true);
353
		result.add(name);
354
		Reference ref = ReferenceFactory.newBook();
355
		ref.setTitleCache("A dummy book", true);
356
		result.add(ref);
357

    
358
		return result;
359
	}
360

    
361
	private Set<Class<? extends CdmBase>> findAllCdmClasses(){
362

    
363

    
364
		//init
365
		Set<Class<? extends CdmBase>> allCdmClasses = new HashSet<Class<? extends CdmBase>>();
366
		allCdmClasses.add(TaxonBase.class);
367
		allCdmClasses.add(BotanicalName.class);
368

    
369
		int count;
370
		do{
371
			count = allCdmClasses.size();
372
			Set<Class<? extends CdmBase>> iteratorSet = new HashSet<Class<? extends CdmBase>>();
373
			iteratorSet.addAll(allCdmClasses);
374
			for (Class<? extends CdmBase> cdmClass : iteratorSet){
375
				Method[] methods = cdmClass.getMethods();
376
				for (Method method: methods){
377
					Class<?> returnType = method.getReturnType();
378
					handleClass(allCdmClasses,returnType);
379
					Class<?>[] params = method.getParameterTypes();
380
					for (Class<?> paramClass : params){
381
						handleClass(allCdmClasses, paramClass);
382
					}
383
				}
384
			}
385
		}
386
		while (allCdmClasses.size() > count);
387
		boolean withAbstract = false;
388
		if (! withAbstract){
389
			Iterator<Class<? extends CdmBase>> iterator = allCdmClasses.iterator();
390
			while (iterator.hasNext()){
391
				Class<?> clazz = iterator.next();
392
				if (Modifier.isAbstract(clazz.getModifiers())){
393
					iterator.remove();
394
				}
395
			}
396
		}
397
		return allCdmClasses;
398
	}
399

    
400
	private void handleClass(Set<Class<? extends CdmBase>> allCdmClasses, Class<?> returnType){
401
		if (CdmBase.class.isAssignableFrom(returnType)){
402
			if (! allCdmClasses.contains(returnType)){
403
				//System.out.println(returnType.getSimpleName());
404
				allCdmClasses.add((Class)returnType);
405
				Class<?> superClass = returnType.getSuperclass();
406
				handleClass(allCdmClasses, superClass);
407
			}
408
		}
409
	}
410

    
411
	@Test
412
	public void testCloneClassification(){
413

    
414
		taxonNode1 = classification1.addChildTaxon(taxon1, null, null);
415
		taxonName1.setTitleCache("name1", true);
416
		taxonName12.setTitleCache("name12", true);
417
		taxonName121.setTitleCache("name121", true);
418
		taxonName2.setTitleCache("name2", true);
419
		taxonName3.setTitleCache("name3", true);
420

    
421
		taxonNode12 = taxonNode1.addChildTaxon(taxon12, null, null);
422
		taxonNode121 = taxonNode12.addChildTaxon(taxon121, null, null);
423
		taxonNode2 = classification1.addChildTaxon(taxon2, null, null);
424
		taxonNode2.addChildTaxon(taxon3, null, null);
425
		Classification clone = (Classification)classification1.clone();
426
		assertEquals(classification1.getAllNodes().size(), clone.getAllNodes().size());
427
		TaxonNode cloneNode = clone.getNode(taxon1);
428
		assertNotSame(cloneNode, taxonNode1);
429
	}
430

    
431
    @Test
432
    public void beanTests(){
433
//	    #5307 Test that BeanUtils does not fail
434
        BeanUtils.getPropertyDescriptors(Classification.class);
435
    }
436

    
437
}
(1-1/7)