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
|
package eu.etaxonomy.cdm.test.unit;
|
10
|
|
11
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
12
|
|
13
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
14
|
import eu.etaxonomy.cdm.test.TermTestBase;
|
15
|
|
16
|
/**
|
17
|
* Superclass for all (hibernate)entities to test if certain (hibernate) needs are fulfilled.
|
18
|
* E.g. testing if all persistent getter have an according setter.
|
19
|
*
|
20
|
* @author a.mueller
|
21
|
*/
|
22
|
public abstract class EntityTestBase extends TermTestBase {
|
23
|
|
24
|
private static Logger logger = LogManager.getLogger(EntityTestBase.class);
|
25
|
|
26
|
protected Class<CdmBase> clazzToTest = clazzToTest();
|
27
|
protected Class<CdmBase> clazzToTest(){
|
28
|
String testClassName = this.getClass().getName();
|
29
|
if (testClassName.endsWith("Test")){
|
30
|
String className = testClassName.substring(0, testClassName.length() - "Test".length());
|
31
|
try {
|
32
|
return (Class<CdmBase>)Class.forName(className);
|
33
|
} catch (ClassNotFoundException e) {
|
34
|
logger.warn(e.getMessage());
|
35
|
return null;
|
36
|
}
|
37
|
}else{
|
38
|
return null;
|
39
|
}
|
40
|
}
|
41
|
|
42
|
// /**
|
43
|
// * Tests if all persistent (not transient) getter have an according setter.
|
44
|
// * Not needed anymore as we switched to field-level hibernate annotations.
|
45
|
// * Maybe useful once we try to be fully compliant with the beans specification.
|
46
|
// */
|
47
|
// @Test
|
48
|
// @Ignore
|
49
|
// public final void testPersistentGetterSetterPair() {
|
50
|
// //
|
51
|
// Annotation annotation = clazzToTest.getAnnotation(Entity.class);
|
52
|
// if (annotation != null){
|
53
|
// Method[] methods = clazzToTest.getDeclaredMethods();
|
54
|
// List<String> strMethods = new ArrayList<String>();
|
55
|
// for (Method method : methods){
|
56
|
// strMethods.add(method.getName());
|
57
|
// }
|
58
|
// for (Method method : methods){
|
59
|
// if (Modifier.isStatic( method.getModifiers())){
|
60
|
// continue;
|
61
|
// }
|
62
|
// String getMethodName = method.getName();
|
63
|
// try {
|
64
|
// if ( ( getMethodName.startsWith("get") || getMethodName.startsWith("is") )
|
65
|
// && method.getAnnotation(Transient.class) == null){
|
66
|
// String setMethodName = null;
|
67
|
// if ( getMethodName.startsWith("get")){
|
68
|
// setMethodName = "s" + getMethodName.substring(1);
|
69
|
// }else if ( getMethodName.startsWith("is")){
|
70
|
// setMethodName = "set" + getMethodName.substring(2);
|
71
|
// }else{
|
72
|
// logger.error("Unknown getter method start");
|
73
|
// fail();
|
74
|
// }
|
75
|
// Class params = method.getReturnType();
|
76
|
// Method setMethod = clazzToTest.getDeclaredMethod(setMethodName, params);
|
77
|
// if (setMethod == null){fail();}
|
78
|
// }else{
|
79
|
// //no setter - do nothing
|
80
|
// }
|
81
|
// } catch (SecurityException e) {
|
82
|
// logger.info(e.getMessage());
|
83
|
// } catch (Exception e) {
|
84
|
// String warning = "Missing setter for getter - a non transient getter method should also have a setter: " + getMethodName;
|
85
|
// logger.warn(warning);
|
86
|
// if (! (clazzToTest == (Class)NonViralName.class && getMethodName.equals("getCitation") ) ){
|
87
|
// fail(warning);
|
88
|
// }
|
89
|
// }
|
90
|
//
|
91
|
// }
|
92
|
// }
|
93
|
//
|
94
|
// }
|
95
|
|
96
|
}
|