46e79893f5104a88abc89c3d5a7344a4f32616ad
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.test
.unit
;
12 import static org
.junit
.Assert
.fail
;
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
;
20 import javax
.persistence
.Entity
;
21 import javax
.persistence
.Transient
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.junit
.Ignore
;
25 import org
.junit
.Test
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
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.
36 public abstract class EntityTestBase
{
37 private static Logger logger
= Logger
.getLogger(EntityTestBase
.class);
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());
45 return (Class
<CdmBase
>)Class
.forName(className
);
46 } catch (ClassNotFoundException e
) {
47 logger
.warn(e
.getMessage());
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*()
64 public final void testPersistentGetterSetterPair() {
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());
73 for (Method method
: methods
){
74 if (Modifier
.isStatic( method
.getModifiers())){
77 String getMethodName
= method
.getName();
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);
87 logger
.error("Unknown getter method start");
90 Class params
= method
.getReturnType();
91 Method setMethod
= clazzToTest
.getDeclaredMethod(setMethodName
, params
);
92 if (setMethod
== null){fail();}
94 //no setter - do nothing
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") ) ){