Project

General

Profile

Download (14.1 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.lang3.StringUtils;
28
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
29
import org.junit.Assert;
30
import org.junit.Before;
31
import org.junit.Test;
32
import org.springframework.beans.BeanUtils;
33

    
34
import eu.etaxonomy.cdm.model.agent.Person;
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.model.name.Rank;
37
import eu.etaxonomy.cdm.model.name.TaxonName;
38
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
39
//import eu.etaxonomy.cdm.model.reference.Book;
40
//import eu.etaxonomy.cdm.model.reference.Journal;
41
import eu.etaxonomy.cdm.model.reference.Reference;
42
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
43

    
44
/**
45
 * @author a.mueller
46
 * @since 01.04.2009
47
 */
48
public class ClassificationTest {
49
	private static final Logger logger = LogManager.getLogger(ClassificationTest.class);
50

    
51
	private static String treeName1;
52
	private static Classification classification1;
53
	private static TaxonNode taxonNode1;
54
	private static TaxonNode taxonNode2;
55
	private static TaxonNode taxonNode3;
56
	private static TaxonNode taxonNode12;
57
	private static TaxonNode taxonNode121;
58
	private static Taxon taxon1;
59
	private static Taxon taxon2;
60
	private static Taxon taxon3;
61
	private static Taxon taxon12;
62
	private static Taxon taxon121;
63
	private static TaxonName taxonName1;
64
	private static TaxonName taxonName2;
65
	private static TaxonName taxonName3;
66
	private static TaxonName taxonName12;
67
	private static TaxonName taxonName121;
68
	private static Reference ref1;
69
	private static Reference ref2;
70
	private static Reference ref3;
71

    
72
	@Before
73
	public void setUp() throws Exception {
74
		treeName1 = "Greuther, 1993";
75
		//refFactory = ReferenceFactory.newInstance();
76
		classification1 = Classification.NewInstance(treeName1);
77
		taxonName12 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
78
		taxonName121 = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
79
		taxonName1 = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
80
		taxonName2 = TaxonNameFactory.NewZoologicalInstance(Rank.GENUS());
81
		taxonName3 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
82
		ref1 = ReferenceFactory.newJournal();
83
		ref2 = ReferenceFactory.newJournal();
84
		ref3 = ReferenceFactory.newJournal();
85
		taxon1 = Taxon.NewInstance(taxonName1, ref1);
86
		taxon2 = Taxon.NewInstance(taxonName2, ref2);
87
		taxon3 = Taxon.NewInstance(taxonName3, ref3);
88
		taxon12 = Taxon.NewInstance(taxonName12, ref3);
89
		taxon121 = Taxon.NewInstance(taxonName121, ref3);
90

    
91
		//taxonNode1 = new TaxonNode(taxon1, taxonomicView1);
92
	}
93

    
94

    
95
//****************************** TESTS *****************************************/
96

    
97
	@Test
98
	public void testAddRoot() {
99
		TaxonName synonymName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
100
		Synonym synonym = Synonym.NewInstance(synonymName, ref1);
101
		TaxonNode taxonNode1 = classification1.addChildTaxon(taxon1, null, null);
102
		taxonNode1.setSynonymToBeUsed(synonym);
103

    
104
		//test root node
105
		List<TaxonNode> rootNodes = classification1.getChildNodes();
106
		assertFalse("List of root nodes should not be empty", rootNodes.isEmpty());
107
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
108
		TaxonNode root = rootNodes.iterator().next();
109
		assertEquals(taxon1, root.getTaxon());
110
		assertSame(taxonNode1, root);
111
		assertNull(root.getReference());
112
		assertNull(root.getMicroReference());
113
		assertEquals(synonym, root.getSynonymToBeUsed());
114

    
115
		//any node
116
		List<TaxonNode> allNodes = classification1.getChildNodes();
117
		assertFalse("List of root nodes should not be empty", allNodes.isEmpty());
118
		assertEquals("Number of root nodes should be 1", 1, allNodes.size());
119
		TaxonNode anyNode = allNodes.iterator().next();
120
		assertSame("Taxon for TaxonNode should be the same added to the view", taxon1, anyNode.getTaxon());
121
		assertSame("TaxonNode should be the same added to the view", taxonNode1, anyNode);
122

    
123
	}
124

    
125
	@Test
126
	public void testIsTaxonInTree() {
127
		classification1.addChildTaxon(taxon1, null, null);
128

    
129
		assertTrue(classification1.isTaxonInTree(taxon1));
130
		Taxon anyTaxon = Taxon.NewInstance(null, null);
131
		assertFalse(classification1.isTaxonInTree(anyTaxon));
132
	}
133

    
134
	@Test
135
	public void testMakeRootChildOfOtherNode() {
136
		TaxonNode root1 = classification1.addChildTaxon(taxon1, null, null);
137
		TaxonNode root2 = classification1.addChildTaxon(taxon2, null, null);
138
		Taxon taxon3 = Taxon.NewInstance(null, null);
139
		root2.addChildTaxon(taxon3, null, null);
140
		String microRef = "p55";
141

    
142
		assertFalse("Root1 must not yet be child of root 2", root2.getChildNodes().contains(root1));
143
		assertNotSame("Root2 must not yet be parent of root 1", root2, root1.getParent());
144
		assertEquals("view must contain 3 nodes", 3, classification1.getAllNodes().size());
145
		assertEquals("view must still contain 2 root", 2, classification1.getChildNodes().size());
146
		assertEquals("root2 must have 1 child", 1, root2.getChildNodes().size());
147

    
148
		classification1.makeTopmostNodeChildOfOtherNode(root1, root2, ref1, microRef);
149
		assertTrue("Root1 must be child of root 2", root2.getChildNodes().contains(root1));
150
		assertSame("Root2 must be parent of root 1", root2, root1.getParent());
151
		assertEquals("view must contain 3 nodes", 3, classification1.getAllNodes().size());
152
		assertEquals("view must contain 1 root", 1, classification1.getChildNodes().size());
153
		assertEquals("new child node must have the expected reference for parent child relationship", ref1, root1.getReference());
154
		assertEquals("new child node must have the expected micro reference for parent child relationship", microRef, root1.getMicroReference());
155
		assertEquals("root2 must have 2 children", 2, root2.getChildNodes().size());
156
	}
157

    
158
	@Test
159
	public void testIsTopmostInTree() {
160
		TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
161

    
162
		assertTrue(classification1.isTaxonInTree(taxon1));
163
		assertTrue(classification1.isTopmostInTree(taxon1));
164
		Taxon anyTaxon = Taxon.NewInstance(null, null);
165
		assertFalse(classification1.isTaxonInTree(anyTaxon));
166
		assertFalse(classification1.isTopmostInTree(anyTaxon));
167
		Taxon child = Taxon.NewInstance(null, null);
168
		root.addChildTaxon(child, null, null);
169
		assertTrue(classification1.isTaxonInTree(child));
170
		assertFalse(classification1.isTopmostInTree(child));
171
	}
172

    
173
	@Test
174
	public void testGetTopmostNode() {
175
		TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
176

    
177
		assertEquals(root, classification1.getTopmostNode(taxon1));
178
		Taxon anyTaxon = Taxon.NewInstance(null, null);
179
		assertFalse(classification1.isTaxonInTree(anyTaxon));
180
		assertNull(classification1.getTopmostNode(anyTaxon));
181
		Taxon child = Taxon.NewInstance(null, null);
182
		root.addChildTaxon(child, null, null);
183
		assertTrue(classification1.isTaxonInTree(child));
184
		assertNull(classification1.getTopmostNode(child));
185
	}
186

    
187
	@Test
188
	public void testAddParentChild() {
189

    
190
		TaxonName synonymName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
191
		Synonym synonym = Synonym.NewInstance(synonymName, ref1);
192
		TaxonNode rootNode = classification1.addChildTaxon(taxon1, null, null);
193
		rootNode.setSynonymToBeUsed(synonym);
194
		Assert.assertEquals(0,rootNode.getChildNodes().size());
195

    
196
		//add child to existing root
197
		classification1.addParentChild(taxon1, taxon2, ref1, "Micro1");
198
		Assert.assertTrue(classification1.isTaxonInTree(taxon2));
199
		Assert.assertFalse(classification1.isTopmostInTree(taxon2));
200
		Assert.assertEquals(1,rootNode.getChildNodes().size());
201
		TaxonNode childNode = rootNode.getChildNodes().iterator().next();
202
		Assert.assertEquals(taxon2, childNode.getTaxon());
203

    
204
		//relationship already exists
205
		classification1.addParentChild(taxon1, taxon2, ref2, "Micro2");
206
		Assert.assertTrue(classification1.isTaxonInTree(taxon2));
207
		Assert.assertFalse(classification1.isTopmostInTree(taxon2));
208
		Assert.assertEquals(2, classification1.getAllNodes().size());
209
		Assert.assertEquals(1,rootNode.getChildNodes().size());
210
		childNode = rootNode.getChildNodes().iterator().next();
211
		Assert.assertEquals(taxon2, childNode.getTaxon());
212
		Assert.assertEquals(ref2, childNode.getReference());
213
		Assert.assertEquals("Micro2", childNode.getMicroReference());
214

    
215
		logger.info("testAddParentChild not yet fully implemented");
216
	}
217

    
218
	@Test
219
	public void testGenerateTitle() {
220
		Classification taxonomicViewLocal = Classification.NewInstance(treeName1);
221
		//Maybe changed if title cache is generated in a different way
222
		assertEquals(treeName1, taxonomicViewLocal.getTitleCache());
223
	}
224

    
225
	@Test
226
	public void play() {
227

    
228
			CdmBase referencedCdmBase = Person.NewInstance();
229
			Set<Class<? extends CdmBase>> allCdmClasses = findAllCdmClasses();
230

    
231
			Class<? extends CdmBase> referencedClass = referencedCdmBase.getClass();
232
			Set<CdmBase> result = new HashSet<CdmBase>();
233
			System.out.println("Referenced Class: " + referencedClass.getName());
234

    
235

    
236
			for (Class<? extends CdmBase> cdmClass : allCdmClasses){
237
				Set<Field> fields = getFields(cdmClass);
238
				for (Field field: fields){
239
					Class<?> type = field.getType();
240
					if (! type.isInterface()){
241
						if (referencedClass.isAssignableFrom(type)||
242
								type.isAssignableFrom(referencedClass) && CdmBase.class.isAssignableFrom(type)){
243
							handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase);
244
						}
245
					}else{  //interface
246
						if (type.isAssignableFrom(referencedClass)){
247
							handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase);
248
						}
249
					}
250
//					Class[] interfaces = referencedClass.getInterfaces();
251
//					for (Class interfaze: interfaces){
252
//						if (interfaze == type){
253
////						if(interfaze.isAssignableFrom(returnType)){
254
//						}
255
//					}
256

    
257

    
258
				}
259
			}
260
			return ;
261
//			find(cdmClass, )
262

    
263
		}
264

    
265
	private boolean handleSingleClass(Class<?> classToBeSearchedFor, Class<?> type, Field field, Class<?> cdmClass, Set<CdmBase> result, CdmBase value){
266
		if (! Modifier.isStatic(field.getModifiers())){
267
			String methodName = StringUtils.rightPad(field.getName(), 30);
268
			String className = StringUtils.rightPad(cdmClass.getSimpleName(), 30);
269
			String returnTypeName = StringUtils.rightPad(type.getSimpleName(), 30);
270

    
271
			System.out.println(methodName +   "\t\t" + className + "\t\t" + returnTypeName);
272
//			result_old.add(method);
273
			result.addAll(getCdmBasesByFieldAndClass(field, cdmClass, value));
274
		}
275
		return true;
276
	}
277

    
278
	private Set<Field> getFields(Class<?> clazz){
279
		Set<Field> result = new HashSet<Field>();
280
		for (Field field: clazz.getDeclaredFields()){
281
			if (!Modifier.isStatic(field.getModifiers())){
282
				result.add(field);
283
			}
284
		}
285
		Class<?> superclass = clazz.getSuperclass();
286
		if (CdmBase.class.isAssignableFrom(superclass)){
287
			result.addAll(getFields(superclass));
288
		}
289
		return result;
290
	}
291

    
292
	private Set<CdmBase> getCdmBasesByFieldAndClass(Field field, Class<?> clazz, CdmBase value){
293
		//FIXME make not dummy but use dao
294
		Set<CdmBase> result = new HashSet<>();
295

    
296
		//genericDao.getCdmBasesByFieldAndClass(clazz, field.getName(), value);
297

    
298

    
299
		TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
300
		name.setTitleCache("A dummy name", true);
301
		result.add(name);
302
		Reference ref = ReferenceFactory.newBook();
303
		ref.setTitleCache("A dummy book", true);
304
		result.add(ref);
305

    
306
		return result;
307
	}
308

    
309
	private Set<Class<? extends CdmBase>> findAllCdmClasses(){
310

    
311

    
312
		//init
313
		Set<Class<? extends CdmBase>> allCdmClasses = new HashSet<>();
314
		allCdmClasses.add(TaxonBase.class);
315
		allCdmClasses.add(TaxonName.class);
316

    
317
		int count;
318
		do{
319
			count = allCdmClasses.size();
320
			Set<Class<? extends CdmBase>> iteratorSet = new HashSet<>();
321
			iteratorSet.addAll(allCdmClasses);
322
			for (Class<? extends CdmBase> cdmClass : iteratorSet){
323
				Method[] methods = cdmClass.getMethods();
324
				for (Method method: methods){
325
					Class<?> returnType = method.getReturnType();
326
					handleClass(allCdmClasses,returnType);
327
					Class<?>[] params = method.getParameterTypes();
328
					for (Class<?> paramClass : params){
329
						handleClass(allCdmClasses, paramClass);
330
					}
331
				}
332
			}
333
		}
334
		while (allCdmClasses.size() > count);
335
		boolean withAbstract = false;
336
		if (! withAbstract){
337
			Iterator<Class<? extends CdmBase>> iterator = allCdmClasses.iterator();
338
			while (iterator.hasNext()){
339
				Class<?> clazz = iterator.next();
340
				if (Modifier.isAbstract(clazz.getModifiers())){
341
					iterator.remove();
342
				}
343
			}
344
		}
345
		return allCdmClasses;
346
	}
347

    
348
	private void handleClass(Set<Class<? extends CdmBase>> allCdmClasses, Class<?> returnType){
349
		if (CdmBase.class.isAssignableFrom(returnType)){
350
			if (! allCdmClasses.contains(returnType)){
351
				//System.out.println(returnType.getSimpleName());
352
				allCdmClasses.add((Class)returnType);
353
				Class<?> superClass = returnType.getSuperclass();
354
				handleClass(allCdmClasses, superClass);
355
			}
356
		}
357
	}
358

    
359
	@Test
360
	public void testCloneClassification(){
361

    
362
		taxonNode1 = classification1.addChildTaxon(taxon1, null, null);
363
		taxonName1.setTitleCache("name1", true);
364
		taxonName12.setTitleCache("name12", true);
365
		taxonName121.setTitleCache("name121", true);
366
		taxonName2.setTitleCache("name2", true);
367
		taxonName3.setTitleCache("name3", true);
368

    
369
		taxonNode12 = taxonNode1.addChildTaxon(taxon12, null, null);
370
		taxonNode121 = taxonNode12.addChildTaxon(taxon121, null, null);
371
		taxonNode2 = classification1.addChildTaxon(taxon2, null, null);
372
		taxonNode2.addChildTaxon(taxon3, null, null);
373
		Classification clone = classification1.clone();
374
		assertEquals(classification1.getAllNodes().size(), clone.getAllNodes().size());
375
		TaxonNode cloneNode = clone.getNode(taxon1);
376
		assertNotSame(cloneNode, taxonNode1);
377
	}
378

    
379
    @Test
380
    public void beanTests(){
381
//	    #5307 Test that BeanUtils does not fail
382
        BeanUtils.getPropertyDescriptors(Classification.class);
383
    }
384

    
385
}
(1-1/5)