Bonjour,

J'ai une application fonctionnant avec Hibernate, qui utilise une datasource dont les paramètres de connexion sont spécifiés dans le fichier de contexte context.xml.

Or je veux utiliser Junit pour réaliser des tests unitaires, et je ne parviens pas à "coller les briques" Hibernate, Junit, Jndi.

Extrait du fichier HibernateAbondementEnveloppeDaoTest.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class HibernateAbondementEnveloppeDaoTest {
 
	private IEnveloppeDao enveloppeDAO;
	private Context initContext;
	private Context webContext;
	private DataSource ds;
	private Connection con = null;
 
 
	@Test
	public void testFindAllAbondementEnveloppes() throws NamingException {
		Collection<Enveloppe> enveloppes = null;
 
		try {
		/*	Context initContext = new InitialContext();
	        Context webContext = (Context)initContext.lookup("java:/comp/env");
 
	        DataSource ds = (DataSource) webContext.lookup("jdbc/sigale");*/
			JndiDatasourceCreator.createJndiDataSource();
			enveloppeDAO = SigaleDaoFactory.getEnveloppeDao();
			enveloppes = enveloppeDAO.findAllEnveloppes();
 
		} catch (TechniqueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertNotNull(enveloppes);
		assertTrue(enveloppes.size() > 0);
	}
La classe JndiDatasourceCreator :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class JndiDatasourceCreator {
	private static final String url = "jdbc:postgresql://10.114.179.34:5433/sigale";
	private static final String username = "gipam";
	private static final String password = "gipam";
	private static final String jndiName = "sigale";
 
	protected JndiDatasourceCreator() {
		try {
			this.create();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
 
	static private JndiDatasourceCreator _instance = null;
 
	static public JndiDatasourceCreator createJndiDataSource() {
		if (null == _instance) {
			_instance = new JndiDatasourceCreator();
		}
		return _instance;
	}
 
	private void create() throws Exception {
		/*
		 * try { //final SimpleNamingContextBuilder builder = new
		 * SimpleNamingContextBuilder(); InitialContext context = new
		 * InitialContext(); PGConnectionPoolDataSource ds = new
		 * PGConnectionPoolDataSource(); ds.setServerName(url);
		 * ds.setUser(username); ds.setPassword(password);
		 * //builder.bind("java:comp/env/jdbc/" + jndiName, ds);
		 * //builder.activate(); context.rebind("java:comp/env/jdbc/" +
		 * jndiName, ds); } catch (NamingException ex) { ex.printStackTrace(); }
		 */
		try {
			// Create initial context
			System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
					"org.apache.naming.java.javaURLContextFactory");
			System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
			InitialContext ic = new InitialContext();
 
			ic.createSubcontext("java:");
			ic.createSubcontext("java:/comp");
			ic.createSubcontext("java:/comp/env");
			ic.createSubcontext("java:/comp/env/jdbc");
 
			// Construct DataSource
			PGConnectionPoolDataSource ds = new PGConnectionPoolDataSource();
			ds.setServerName("10.114.179.34");
			ds.setPortNumber(5433);
			ds.setUser(username);
			ds.setPassword(password);
			ds.setDatabaseName("sigale");
 
			ic.bind("java:/comp/env/jdbc/" + jndiName, ds);
		} catch (NamingException ex) {
			ex.printStackTrace();
		}
 
	}
 
}
Fichier hibernate.cfg.xml:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
 
    <session-factory>
     <!-- Start of user code -->
 
		<!-- Connexion avec PostgreSQL directe -->
			<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
 
		<property name="hibernate.connection.datasource">java:/comp/env/jdbc/sigale</property>
 
 
		<!-- Definition du cache OpenSymphony Cache (OSCache) -->
		<property name="hibernate.cache.provider_class">i2.application.commun.util.cache.OSCacheProvider</property>
 
		<property name="hibernate.cache.use_query_cache">true</property>
 
		<property name="current_session_context_class">thread</property>
		    <!-- End of user code -->   
        <!-- mapping files -->
 
        <mapping resource="i2/application/sigale/integration/budgets/hibernate/Enveloppe.hbm.xml"/>
        <mapping resource="i2/application/sigale/integration/budgets/hibernate/MoaAbondante.hbm.xml"/>
        <mapping resource="i2/application/sigale/integration/budgets/hibernate/Operation.hbm.xml"/>
        <mapping resource="i2/application/sigale/integration/budgets/hibernate/Projet.hbm.xml"/>
        <mapping resource="i2/application/sigale/integration/budgets/hibernate/DepartementOperationnel.hbm.xml"/>
        <mapping resource="i2/application/sigale/integration/budgets/hibernate/EntiteOrganisationnelle.hbm.xml"/>
        <mapping resource="i2/application/sigale/integration/budgets/hibernate/MoaOperationnelle.hbm.xml"/>
        <mapping resource="i2/application/sigale/integration/budgets/hibernate/Transaction.hbm.xml"/>
        <mapping resource="i2/application/sigale/integration/budgets/hibernate/UniteOperationnelle.hbm.xml"/>
        <mapping resource="i2/application/sigale/integration/budgets/hibernate/Programme.hbm.xml"/>
        <mapping resource="i2/application/sigale/integration/budgets/hibernate/SystemeInformation.hbm.xml"/>
        <mapping resource="i2/application/sigale/integration/budgets/hibernate/Metier.hbm.xml"/>
 
    </session-factory>
 
</hibernate-configuration>
L'erreur que j'obtiens est la suivante:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
org.hibernate.connection.DatasourceConnectionProvider ERROR - Could not find datasource: java:/comp/env/jdbc/sigale ()
java.lang.ClassCastException: org.postgresql.ds.PGConnectionPoolDataSource
	at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
	at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:137)
	at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:79)
	at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:425)
	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:89)
	at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2119)
	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2115)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1339)
	at fr.obeo.fwk.dao.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:15)
	at i2.application.sigale.integration.budgets.hibernate.HibernateEnveloppeDao.findAllEnveloppes(HibernateEnveloppeDao.java:117)
	at i2.application.sigale.test.integration.budgets.hibernate.HibernateAbondementEnveloppeDaoTest.testFindAllAbondementEnveloppes(HibernateAbondementEnveloppeDaoTest.java:135)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
	at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
En déboggant pas à pas, c'est dans lors de l'exécution de findAllEnveloppes, lors de la récupération de la session factory de Hibernate, que ça bugge, lors de l'exécution de ce code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
static {
		try {
			// Cr�e la SessionFactory
			sessionFactory = new Configuration().configure()
					.buildSessionFactory();
		} catch (HibernateException ex) {
			throw new RuntimeException("Probleme de configuration : "
					+ ex.getMessage(), ex);
		}
	}
Je suppose donc que la datasource créée dans la classe jndidatasourcecreator n'est pas comprise par Hibernate, et je ne vois pas pourquoi.

Avez vous une idée?

Merci d'avance.