Project

General

Profile

Download (14.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.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 = Logger.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
	//private ReferenceFactory refFactory;
72

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

    
95
		//taxonNode1 = new TaxonNode(taxon1, taxonomicView1);
96
	}
97

    
98

    
99
//****************************** TESTS *****************************************/
100

    
101
	/**
102
	 * 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)}.
103
	 */
104
	@Test
105
	public void testAddRoot() {
106
		TaxonName synonymName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
107
		Synonym synonym = Synonym.NewInstance(synonymName, ref1);
108
		TaxonNode taxonNode1 = classification1.addChildTaxon(taxon1, null, null);
109
		taxonNode1.setSynonymToBeUsed(synonym);
110

    
111
		//test root node
112
		List<TaxonNode> rootNodes = classification1.getChildNodes();
113
		assertFalse("List of root nodes should not be empty", rootNodes.isEmpty());
114
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
115
		TaxonNode root = rootNodes.iterator().next();
116
		assertEquals(taxon1, root.getTaxon());
117
		assertSame(taxonNode1, root);
118
		assertNull(root.getReference());
119
		assertNull(root.getMicroReference());
120
		assertEquals(synonym, root.getSynonymToBeUsed());
121

    
122
		//any node
123
		List<TaxonNode> allNodes = classification1.getChildNodes();
124
		assertFalse("List of root nodes should not be empty", allNodes.isEmpty());
125
		assertEquals("Number of root nodes should be 1", 1, allNodes.size());
126
		TaxonNode anyNode = allNodes.iterator().next();
127
		assertSame("Taxon for TaxonNode should be the same added to the view", taxon1, anyNode.getTaxon());
128
		assertSame("TaxonNode should be the same added to the view", taxonNode1, anyNode);
129

    
130
	}
131

    
132
	@Test
133
	public void testIsTaxonInTree() {
134
		classification1.addChildTaxon(taxon1, null, null);
135

    
136
		assertTrue(classification1.isTaxonInTree(taxon1));
137
		Taxon anyTaxon = Taxon.NewInstance(null, null);
138
		assertFalse(classification1.isTaxonInTree(anyTaxon));
139
	}
140

    
141
	@Test
142
	public void testMakeRootChildOfOtherNode() {
143
		TaxonNode root1 = classification1.addChildTaxon(taxon1, null, null);
144
		TaxonNode root2 = classification1.addChildTaxon(taxon2, null, null);
145
		Taxon taxon3 = Taxon.NewInstance(null, null);
146
		root2.addChildTaxon(taxon3, null, null);
147
		String microRef = "p55";
148

    
149
		assertFalse("Root1 must not yet be child of root 2", root2.getChildNodes().contains(root1));
150
		assertNotSame("Root2 must not yet be parent of root 1", root2, root1.getParent());
151
		assertEquals("view must contain 3 nodes", 3, classification1.getAllNodes().size());
152
		assertEquals("view must still contain 2 root", 2, classification1.getChildNodes().size());
153
		assertEquals("root2 must have 1 child", 1, root2.getChildNodes().size());
154

    
155
		classification1.makeTopmostNodeChildOfOtherNode(root1, root2, ref1, microRef);
156
		assertTrue("Root1 must be child of root 2", root2.getChildNodes().contains(root1));
157
		assertSame("Root2 must be parent of root 1", root2, root1.getParent());
158
		assertEquals("view must contain 3 nodes", 3, classification1.getAllNodes().size());
159
		assertEquals("view must contain 1 root", 1, classification1.getChildNodes().size());
160
		assertEquals("new child node must have the expected reference for parent child relationship", ref1, root1.getReference());
161
		assertEquals("new child node must have the expected micro reference for parent child relationship", microRef, root1.getMicroReference());
162
		assertEquals("root2 must have 2 children", 2, root2.getChildNodes().size());
163

    
164
	}
165

    
166
	@Test
167
	public void testIsTopmostInTree() {
168
		TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
169

    
170
		assertTrue(classification1.isTaxonInTree(taxon1));
171
		assertTrue(classification1.isTopmostInTree(taxon1));
172
		Taxon anyTaxon = Taxon.NewInstance(null, null);
173
		assertFalse(classification1.isTaxonInTree(anyTaxon));
174
		assertFalse(classification1.isTopmostInTree(anyTaxon));
175
		Taxon child = Taxon.NewInstance(null, null);
176
		root.addChildTaxon(child, null, null);
177
		assertTrue(classification1.isTaxonInTree(child));
178
		assertFalse(classification1.isTopmostInTree(child));
179
	}
180

    
181
	@Test
182
	public void testGetTopmostNode() {
183
		TaxonNode root = classification1.addChildTaxon(taxon1, null, null);
184

    
185
		assertEquals(root, classification1.getTopmostNode(taxon1));
186
		Taxon anyTaxon = Taxon.NewInstance(null, null);
187
		assertFalse(classification1.isTaxonInTree(anyTaxon));
188
		assertNull(classification1.getTopmostNode(anyTaxon));
189
		Taxon child = Taxon.NewInstance(null, null);
190
		root.addChildTaxon(child, null, null);
191
		assertTrue(classification1.isTaxonInTree(child));
192
		assertNull(classification1.getTopmostNode(child));
193
	}
194

    
195
	@Test
196
	public void testAddParentChild() {
197

    
198
		TaxonName synonymName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
199
		Synonym synonym = Synonym.NewInstance(synonymName, ref1);
200
		TaxonNode rootNode = classification1.addChildTaxon(taxon1, null, null);
201
		rootNode.setSynonymToBeUsed(synonym);
202
		Assert.assertEquals(0,rootNode.getChildNodes().size());
203

    
204
		//add child to existing root
205
		classification1.addParentChild(taxon1, taxon2, ref1, "Micro1");
206
		Assert.assertTrue(classification1.isTaxonInTree(taxon2));
207
		Assert.assertFalse(classification1.isTopmostInTree(taxon2));
208
		Assert.assertEquals(1,rootNode.getChildNodes().size());
209
		TaxonNode childNode = rootNode.getChildNodes().iterator().next();
210
		Assert.assertEquals(taxon2, childNode.getTaxon());
211

    
212
		//relationship already exists
213
		classification1.addParentChild(taxon1, taxon2, ref2, "Micro2");
214
		Assert.assertTrue(classification1.isTaxonInTree(taxon2));
215
		Assert.assertFalse(classification1.isTopmostInTree(taxon2));
216
		Assert.assertEquals(2, classification1.getAllNodes().size());
217
		Assert.assertEquals(1,rootNode.getChildNodes().size());
218
		childNode = rootNode.getChildNodes().iterator().next();
219
		Assert.assertEquals(taxon2, childNode.getTaxon());
220
		Assert.assertEquals(ref2, childNode.getReference());
221
		Assert.assertEquals("Micro2", childNode.getMicroReference());
222

    
223
		logger.info("testAddParentChild not yet fully implemented");
224

    
225
	}
226

    
227
	@Test
228
	public void testGenerateTitle() {
229
		Classification taxonomicViewLocal = Classification.NewInstance(treeName1);
230
		//Maybe changed if title cache is generated in a different way
231
		assertEquals(treeName1, taxonomicViewLocal.getTitleCache());
232
	}
233

    
234
	@Test
235
	public void play() {
236

    
237
			CdmBase referencedCdmBase = Person.NewInstance();
238
			Set<Class<? extends CdmBase>> allCdmClasses = findAllCdmClasses();
239

    
240
			Class<? extends CdmBase> referencedClass = referencedCdmBase.getClass();
241
			Set<CdmBase> result = new HashSet<CdmBase>();
242
			System.out.println("Referenced Class: " + referencedClass.getName());
243

    
244

    
245
			for (Class<? extends CdmBase> cdmClass : allCdmClasses){
246
				Set<Field> fields = getFields(cdmClass);
247
				for (Field field: fields){
248
					Class<?> type = field.getType();
249
					if (! type.isInterface()){
250
						if (referencedClass.isAssignableFrom(type)||
251
								type.isAssignableFrom(referencedClass) && CdmBase.class.isAssignableFrom(type)){
252
							handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase);
253
						}
254
					}else{  //interface
255
						if (type.isAssignableFrom(referencedClass)){
256
							handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase);
257
						}
258
					}
259
//					Class[] interfaces = referencedClass.getInterfaces();
260
//					for (Class interfaze: interfaces){
261
//						if (interfaze == type){
262
////						if(interfaze.isAssignableFrom(returnType)){
263
//						}
264
//					}
265

    
266

    
267
				}
268
			}
269
			return ;
270
//			find(cdmClass, )
271

    
272
		}
273

    
274
	private boolean handleSingleClass(Class<?> classToBeSearchedFor, Class<?> type, Field field, Class<?> cdmClass, Set<CdmBase> result, CdmBase value){
275
		if (! Modifier.isStatic(field.getModifiers())){
276
			String methodName = StringUtils.rightPad(field.getName(), 30);
277
			String className = StringUtils.rightPad(cdmClass.getSimpleName(), 30);
278
			String returnTypeName = StringUtils.rightPad(type.getSimpleName(), 30);
279

    
280
			System.out.println(methodName +   "\t\t" + className + "\t\t" + returnTypeName);
281
//			result_old.add(method);
282
			result.addAll(getCdmBasesByFieldAndClass(field, cdmClass, value));
283
		}
284
		return true;
285
	}
286

    
287
	private Set<Field> getFields(Class<?> clazz){
288
		Set<Field> result = new HashSet<Field>();
289
		for (Field field: clazz.getDeclaredFields()){
290
			if (!Modifier.isStatic(field.getModifiers())){
291
				result.add(field);
292
			}
293
		}
294
		Class<?> superclass = clazz.getSuperclass();
295
		if (CdmBase.class.isAssignableFrom(superclass)){
296
			result.addAll(getFields(superclass));
297
		}
298
		return result;
299
	}
300

    
301
	private Set<CdmBase> getCdmBasesByFieldAndClass(Field field, Class<?> clazz, CdmBase value){
302
		//FIXME make not dummy but use dao
303
		Set<CdmBase> result = new HashSet<>();
304

    
305
		//genericDao.getCdmBasesByFieldAndClass(clazz, field.getName(), value);
306

    
307

    
308
		TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
309
		name.setTitleCache("A dummy name", true);
310
		result.add(name);
311
		Reference ref = ReferenceFactory.newBook();
312
		ref.setTitleCache("A dummy book", true);
313
		result.add(ref);
314

    
315
		return result;
316
	}
317

    
318
	private Set<Class<? extends CdmBase>> findAllCdmClasses(){
319

    
320

    
321
		//init
322
		Set<Class<? extends CdmBase>> allCdmClasses = new HashSet<>();
323
		allCdmClasses.add(TaxonBase.class);
324
		allCdmClasses.add(TaxonName.class);
325

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

    
357
	private void handleClass(Set<Class<? extends CdmBase>> allCdmClasses, Class<?> returnType){
358
		if (CdmBase.class.isAssignableFrom(returnType)){
359
			if (! allCdmClasses.contains(returnType)){
360
				//System.out.println(returnType.getSimpleName());
361
				allCdmClasses.add((Class)returnType);
362
				Class<?> superClass = returnType.getSuperclass();
363
				handleClass(allCdmClasses, superClass);
364
			}
365
		}
366
	}
367

    
368
	@Test
369
	public void testCloneClassification(){
370

    
371
		taxonNode1 = classification1.addChildTaxon(taxon1, null, null);
372
		taxonName1.setTitleCache("name1", true);
373
		taxonName12.setTitleCache("name12", true);
374
		taxonName121.setTitleCache("name121", true);
375
		taxonName2.setTitleCache("name2", true);
376
		taxonName3.setTitleCache("name3", true);
377

    
378
		taxonNode12 = taxonNode1.addChildTaxon(taxon12, null, null);
379
		taxonNode121 = taxonNode12.addChildTaxon(taxon121, null, null);
380
		taxonNode2 = classification1.addChildTaxon(taxon2, null, null);
381
		taxonNode2.addChildTaxon(taxon3, null, null);
382
		Classification clone = (Classification)classification1.clone();
383
		assertEquals(classification1.getAllNodes().size(), clone.getAllNodes().size());
384
		TaxonNode cloneNode = clone.getNode(taxon1);
385
		assertNotSame(cloneNode, taxonNode1);
386
	}
387

    
388
    @Test
389
    public void beanTests(){
390
//	    #5307 Test that BeanUtils does not fail
391
        BeanUtils.getPropertyDescriptors(Classification.class);
392
    }
393

    
394
}
(1-1/8)