View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: PersistenceConventionTest.java 4156 2014-12-09 11:27:18Z echatellier $
6    * $HeadURL: https://svn.codelutin.com/isis-fish/tags/isis-fish-4.4.0.2/src/test/java/fr/ifremer/isisfish/entity/PersistenceConventionTest.java $
7    * %%
8    * Copyright (C) 2009 - 2010 Ifremer, CodeLutin, Chatellier Eric
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/gpl-3.0.html>.
23   * #L%
24   */
25  
26  package fr.ifremer.isisfish.entity;
27  
28  import java.lang.reflect.Method;
29  import java.lang.reflect.Modifier;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.junit.Assert;
34  import org.junit.Test;
35  import org.nuiton.topia.TopiaException;
36  import org.nuiton.topia.persistence.TopiaEntity;
37  
38  import fr.ifremer.isisfish.IsisFishDAOHelper;
39  import fr.ifremer.isisfish.IsisFishDAOHelper.IsisFishEntityEnum;
40  import fr.ifremer.isisfish.entities.Equation;
41  
42  /**
43   * Test for convention defined on entities.
44   * 
45   * @author chatellier
46   * @version $Revision: 4156 $
47   * 
48   * Last update : $Date: 2014-12-09 12:27:18 +0100 (Tue, 09 Dec 2014) $
49   * By : $Author: echatellier $
50   */
51  public class PersistenceConventionTest {
52  
53      /** Logger for this class. */
54      private static final Log log = LogFactory.getLog(PersistenceConventionTest.class);
55  
56      /**
57       * A convention has been defined on all entities for equation getter/setter.
58       * 
59       * If getter is :
60       * <pre>Equation getXXX()</pre>
61       * setter has to be
62       * <pre>void setXXXContent(String)</pre>
63       * 
64       * @throws TopiaException 
65       * @throws NoSuchMethodException 
66       * @throws SecurityException 
67       */
68      @Test
69      public void testEquationSetterName() throws TopiaException, SecurityException, NoSuchMethodException {
70  
71          for (IsisFishEntityEnum entityEnum : IsisFishDAOHelper.IsisFishEntityEnum.values()) {
72              Class<? extends TopiaEntity> entity = entityEnum.getImplementation();
73  
74              if (log.isDebugEnabled()) {
75                  log.debug("Testing convention on : " + entity);
76              }
77  
78              for (Method method : entity.getMethods()) {
79                  Class<?> returnType = method.getReturnType();
80  
81                  // method return an equation, search for getter
82                  if (returnType.isAssignableFrom(Equation.class)) {
83                      String methodName = method.getName();
84                      methodName = methodName.replaceFirst("g", "s") + "Content";
85  
86                      try {
87                          Method methodSet = entity.getMethod(methodName, String.class);
88                          
89                          Assert.assertTrue("Method " + entity.getName() + "#" +
90                                  methodName + "(" + String.class.getName() + ") is not accessible", Modifier.isPublic(methodSet.getModifiers()));
91                      }
92                      catch (NoSuchMethodException ex) {
93                          Assert.fail("Method " + entity.getName() + "#" +
94                              methodName + "(" + String.class.getName() + ") not found");
95                      }
96                  }
97              }
98          }
99      }
100 }