View Javadoc
1   /*
2    * #%L
3    * $Id: SensitivityUtilsTest.java 4156 2014-12-09 11:27:18Z echatellier $
4    * $HeadURL: https://svn.codelutin.com/isis-fish/tags/isis-fish-4.4.0.2/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/SensitivityUtilsTest.java $
5    * %%
6    * Copyright (C) 2012 Ifremer, Codelutin, Chatellier Eric
7    * %%
8    * This program is free software: you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as
10   * published by the Free Software Foundation, either version 3 of the 
11   * License, or (at your option) any later version.
12   * 
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   * 
18   * You should have received a copy of the GNU General Public 
19   * License along with this program.  If not, see
20   * <http://www.gnu.org/licenses/gpl-3.0.html>.
21   * #L%
22   */
23  
24  package fr.ifremer.isisfish.simulator.sensitivity;
25  
26  import java.lang.reflect.InvocationTargetException;
27  import java.lang.reflect.Method;
28  import java.util.Properties;
29  
30  import org.apache.commons.lang3.StringUtils;
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.TopiaContext;
36  import org.nuiton.topia.TopiaContextFactory;
37  import org.nuiton.topia.TopiaException;
38  import org.nuiton.topia.TopiaNotFoundException;
39  import org.nuiton.topia.persistence.TopiaDAO;
40  import org.nuiton.topia.persistence.TopiaEntity;
41  
42  import fr.ifremer.isisfish.IsisFishDAOHelper;
43  import fr.ifremer.isisfish.datastore.IsisH2Config;
44  
45  /**
46   * Test for SensitivityUtils class.
47   * 
48   * @author chatellier
49   * @version $Revision: 4156 $
50   * 
51   * Last update : $Date: 2014-12-09 12:27:18 +0100 (Tue, 09 Dec 2014) $
52   * By : $Author: echatellier $
53   */
54  public class SensitivityUtilsTest {
55  
56      /** Logger for this class */
57      private static final Log log = LogFactory.getLog(SensitivityUtilsTest.class);
58  
59      /**
60       * Test le résultat attendu de l'echappement des noms de facteurs.
61       */
62      @Test
63      public void testEspaceFactorName() {
64          Assert.assertEquals("test_factor_pour_R", SensitivityUtils.espaceFactorName("test factor pour R"));
65          Assert.assertEquals("factor2_selectivite", SensitivityUtils.espaceFactorName("factor2.sélectivité"));
66      }
67      
68      /**
69       * Return a basic valid topia context.
70       * 
71       * @throws TopiaNotFoundException 
72       */
73      protected TopiaContext getTopiaContext() throws TopiaNotFoundException {
74          Properties config = new Properties();
75          IsisH2Config.addMemDatabaseConfig(config, "test");
76          IsisH2Config.addHibernateMapping(config);
77          TopiaContext context = TopiaContextFactory.getContext(config);
78          return context;
79      }
80  
81      /**
82       * Test (par introspection) que tous les facteurs existent.
83       * 
84       * @throws ClassNotFoundException 
85       * @throws IllegalAccessException 
86       * @throws InstantiationException 
87       * @throws NoSuchMethodException 
88       * @throws SecurityException 
89       * @throws InvocationTargetException 
90       * @throws IllegalArgumentException 
91       * @throws TopiaException 
92       */
93      @Test
94      public void testFactorExistence() throws InstantiationException, IllegalAccessException,
95          ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException,
96          InvocationTargetException, TopiaException {
97          Properties factors = SensitivityUtils.getProperties();
98  
99          TopiaContext testTC = getTopiaContext();
100         TopiaContext context = testTC.beginTransaction();
101         for (String factorName : factors.stringPropertyNames()) {
102             String className = factorName.substring(0, factorName.indexOf("."));
103             String propertyName = StringUtils.capitalize(factorName.substring(factorName.indexOf(".") + 1));
104 
105             // Harder method
106             // but topia context is needed by some getXXX() methods
107             Method mStatic = IsisFishDAOHelper.class.getMethod("get" + className + "DAO", TopiaContext.class);
108             TopiaDAO<TopiaEntity> dao = (TopiaDAO<TopiaEntity>) mStatic.invoke(null, new Object[] { context });
109             Assert.assertNotNull("No DOA found for factor " + factorName, dao);
110 
111             // call proper property
112             if (log.isDebugEnabled()) {
113                 log.debug(" and call get" + propertyName + "() on " + className);
114             }
115             Method m = dao.getEntityClass().getMethod("get" + propertyName);
116             Assert.assertNotNull("getter not found for " + factorName, m);
117         }
118         context.closeContext();
119     }
120 }