46e79893f5104a88abc89c3d5a7344a4f32616ad
[cdmlib.git] / cdmlib-model / src / test / java / eu / etaxonomy / cdm / test / unit / EntityTestBase.java
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 static org.junit.Assert.fail;
13
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Modifier;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import javax.persistence.Entity;
21 import javax.persistence.Transient;
22
23 import org.apache.log4j.Logger;
24 import org.junit.Ignore;
25 import org.junit.Test;
26
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.name.NonViralName;
29
30 /**
31 * Superclass for all (hibernate)entities to test if certain (hibernate) needs are fulfilled.
32 * E.g. testing if all persistent getter have an according setter.
33 * @author a.mueller
34 *
35 */
36 public abstract class EntityTestBase {
37 private static Logger logger = Logger.getLogger(EntityTestBase.class);
38
39 protected Class<CdmBase> clazzToTest = clazzToTest();
40 protected Class<CdmBase> clazzToTest(){
41 String testClassName = this.getClass().getName();
42 if (testClassName.endsWith("Test")){
43 String className = testClassName.substring(0, testClassName.length() - "Test".length());
44 try {
45 return (Class<CdmBase>)Class.forName(className);
46 } catch (ClassNotFoundException e) {
47 logger.warn(e.getMessage());
48 return null;
49 }
50 }else{
51 return null;
52 }
53 }
54
55 /**
56 * Tests if all persistent (not transient) getter have an according setter.
57 * FIXME Having matched getter / setter pairs is not strictly necessary if we
58 * use field-level hibernate annotations, and is perhaps even desirable (i.e.
59 * prevent the use of set() methods on *-to-Many fields by enforcing use of add*()
60 * and remove*())
61 */
62 @Test
63 @Ignore
64 public final void testPersistentGetterSetterPair() {
65 //
66 Annotation annotation = clazzToTest.getAnnotation(Entity.class);
67 if (annotation != null){
68 Method[] methods = clazzToTest.getDeclaredMethods();
69 List<String> strMethods = new ArrayList<String>();
70 for (Method method : methods){
71 strMethods.add(method.getName());
72 }
73 for (Method method : methods){
74 if (Modifier.isStatic( method.getModifiers())){
75 continue;
76 }
77 String getMethodName = method.getName();
78 try {
79 if ( ( getMethodName.startsWith("get") || getMethodName.startsWith("is") )
80 && method.getAnnotation(Transient.class) == null){
81 String setMethodName = null;
82 if ( getMethodName.startsWith("get")){
83 setMethodName = "s" + getMethodName.substring(1);
84 }else if ( getMethodName.startsWith("is")){
85 setMethodName = "set" + getMethodName.substring(2);
86 }else{
87 logger.error("Unknown getter method start");
88 fail();
89 }
90 Class params = method.getReturnType();
91 Method setMethod = clazzToTest.getDeclaredMethod(setMethodName, params);
92 if (setMethod == null){fail();}
93 }else{
94 //no setter - do nothing
95 }
96 } catch (SecurityException e) {
97 logger.info(e.getMessage());
98 } catch (Exception e) {
99 String warning = "Missing setter for getter - a non transient getter method should also have a setter: " + getMethodName;
100 logger.warn(warning);
101 if (! (clazzToTest == (Class)NonViralName.class && getMethodName.equals("getCitation") ) ){
102 fail(warning);
103 }
104 }
105
106 }
107 }
108
109 }
110
111 }