Project

General

Profile

Download (3.13 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.test.unit;
11

    
12
import org.apache.log4j.Logger;
13
import eu.etaxonomy.cdm.model.common.CdmBase;
14

    
15
/**
16
 * Superclass for all (hibernate)entities to test if certain (hibernate) needs are fulfilled.
17
 * E.g. testing if all persistent getter have an according setter.
18
 * @author a.mueller
19
 *
20
 */
21
public abstract class EntityTestBase {
22
	private static Logger logger = Logger.getLogger(EntityTestBase.class);
23

    
24
	protected Class<CdmBase> clazzToTest = clazzToTest();
25
	protected Class<CdmBase> clazzToTest(){
26
		String testClassName = this.getClass().getName();
27
		if (testClassName.endsWith("Test")){
28
			String className = testClassName.substring(0, testClassName.length() - "Test".length());
29
			try {
30
				return (Class<CdmBase>)Class.forName(className);
31
			} catch (ClassNotFoundException e) {
32
				logger.warn(e.getMessage());
33
				return null;
34
			}
35
		}else{
36
			return null;
37
		}
38
	}
39
	
40
//	/**
41
//	 * Tests if all persistent (not transient) getter have an according setter.
42
//	 * Not needed anymore as we switched to field-level hibernate annotations.
43
//	 * Maybe useful once we try to fully compliant with the beans specification. 
44
//	 */
45
//	@Test 
46
//	@Ignore
47
//	public final void testPersistentGetterSetterPair() {
48
//		//
49
//		Annotation annotation = clazzToTest.getAnnotation(Entity.class);
50
//		if (annotation != null){
51
//			Method[] methods = clazzToTest.getDeclaredMethods();
52
//			List<String> strMethods = new ArrayList<String>(); 
53
//			for (Method method : methods){
54
//				strMethods.add(method.getName());
55
//			}		
56
//			for (Method method : methods){
57
//				if (Modifier.isStatic( method.getModifiers())){
58
//					continue;
59
//				}
60
//				String getMethodName = method.getName();
61
//				try {
62
//					if ( ( getMethodName.startsWith("get") || getMethodName.startsWith("is") )
63
//							&& method.getAnnotation(Transient.class) == null){
64
//						String setMethodName = null;
65
//						if ( getMethodName.startsWith("get")){
66
//							setMethodName = "s" + getMethodName.substring(1);
67
//						}else if ( getMethodName.startsWith("is")){
68
//							setMethodName = "set" + getMethodName.substring(2);
69
//						}else{
70
//							logger.error("Unknown getter method start");
71
//							fail();
72
//						}
73
//						Class params = method.getReturnType();
74
//						Method setMethod = clazzToTest.getDeclaredMethod(setMethodName, params);
75
//						if (setMethod == null){fail();}
76
//					}else{
77
//						//no setter - do nothing
78
//					}
79
//				} catch (SecurityException e) {
80
//					logger.info(e.getMessage());
81
//				} catch (Exception e) {
82
//					String warning = "Missing setter for getter - a non transient getter method should also have a setter: " + getMethodName;
83
//					logger.warn(warning);
84
//					if (! (clazzToTest == (Class)NonViralName.class && getMethodName.equals("getCitation") ) ){
85
//						fail(warning);
86
//					}
87
//				}
88
//
89
//			}
90
//		}
91
//		
92
//	}
93

    
94
}
    (1-1/1)