Project

General

Profile

Download (15.4 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.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.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.Reference;
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 ClassificationTest {
54
	private static final Logger logger = Logger.getLogger(ClassificationTest.class);
55

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

    
78

    
79

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

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

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

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

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

    
126
//****************************** TESTS *****************************************/
127

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

    
138

    
139

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

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

    
159
	}
160

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

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

    
173

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

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

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

    
200
	}
201

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

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

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

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

    
231
	@Test
232
	public void testAddParentChild() {
233

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

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

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

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

    
261
	}
262

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

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

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

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

    
286

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

    
308

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

    
314
		}
315

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

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

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

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

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

    
349

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

    
357
		return result;
358
	}
359

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

    
362

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

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

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

    
410
	@Test
411
	public void testCloneClassification(){
412

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

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

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

    
436
}
(1-1/7)