This is an automated email from the git hooks/post-receive script. New commit to branch feature/2892 in repository topia. See http://git.nuiton.org/topia.git commit 9d93f47211fbd13f4cca02c09456dde1a7028ff2 Author: Arnaud Thimel <thimel@codelutin.com> Date: Fri Jan 16 18:08:16 2015 +0100 refs #2892 Fix TopiaConnectionProviderTest --- .../TopiaConnectionProviderHardCoded.java | 3 +- .../framework/TopiaConnectionProviderTest.java | 175 +++++++++++---------- .../TopiaConnectionProviderHardcoded.properties | 34 ---- 3 files changed, 91 insertions(+), 121 deletions(-) diff --git a/topia-it/src/test/java/org/nuiton/topia/it/legacy/framework/TopiaConnectionProviderHardCoded.java b/topia-it/src/test/java/org/nuiton/topia/it/legacy/framework/TopiaConnectionProviderHardCoded.java index 73d25c3..85fda3a 100644 --- a/topia-it/src/test/java/org/nuiton/topia/it/legacy/framework/TopiaConnectionProviderHardCoded.java +++ b/topia-it/src/test/java/org/nuiton/topia/it/legacy/framework/TopiaConnectionProviderHardCoded.java @@ -46,7 +46,8 @@ import java.util.Map; import java.util.Properties; /** - * Customized connection provider. + * Customized connection provider. It will not use the JDBC URL provided by Hibernate, but the one from + * {@link TopiaConnectionProviderTest#TEST_URL}. * * @author Tony Chemit - chemit@codelutin.com * @since 2.5.3 diff --git a/topia-it/src/test/java/org/nuiton/topia/it/legacy/framework/TopiaConnectionProviderTest.java b/topia-it/src/test/java/org/nuiton/topia/it/legacy/framework/TopiaConnectionProviderTest.java index b6aa977..5d92169 100644 --- a/topia-it/src/test/java/org/nuiton/topia/it/legacy/framework/TopiaConnectionProviderTest.java +++ b/topia-it/src/test/java/org/nuiton/topia/it/legacy/framework/TopiaConnectionProviderTest.java @@ -24,6 +24,24 @@ package org.nuiton.topia.it.legacy.framework; * #L% */ +import java.io.File; +import java.util.Locale; +import java.util.Map; + +import org.hibernate.cfg.AvailableSettings; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.nuiton.topia.it.legacy.TopiaItLegacyDatabase; +import org.nuiton.topia.it.legacy.TopiaItLegacyTopiaApplicationContext; +import org.nuiton.topia.it.legacy.TopiaItLegacyTopiaPersistenceContext; +import org.nuiton.topia.it.legacy.test.entities.Person; +import org.nuiton.topia.it.legacy.test.entities.PersonTopiaDao; +import org.nuiton.topia.it.legacy.topiatest.Personne; +import org.nuiton.topia.persistence.BeanTopiaConfiguration; +import org.nuiton.topia.persistence.TopiaConfiguration; +import org.nuiton.topia.persistence.TopiaConfigurationBuilder; +import org.nuiton.topia.persistence.TopiaException; import org.nuiton.topia.persistence.internal.TopiaConnectionProvider; /** @@ -35,90 +53,75 @@ import org.nuiton.topia.persistence.internal.TopiaConnectionProvider; */ public class TopiaConnectionProviderTest { -//// private static final Log log = -//// LogFactory.getLog(TopiaConnectionProviderTest.class); -// - public static final String TEST_URL = "testURL"; -// -// @Rule -// public final TopiaItLegacyDatabase db = -// new TopiaItLegacyDatabase("/TopiaConnectionProviderHardcoded.properties") { -// -// @Override -// protected void onDbConfigurationCreate(Properties configuration, -// File testdir, -// String dbPath) { -// -// Assert.assertFalse(testdir.exists()); -// -// String dbPathFake = new File(testdir, "fake" + File.separator + "db").getAbsolutePath(); -// -// Assert.assertFalse(new File(dbPathFake).getParentFile().exists()); -// -// configuration.setProperty("dbPath", dbPath); -// configuration.setProperty("dbPathFake", dbPathFake); -// -// // give the path where connection provider will create db -// configuration.setProperty(TEST_URL, -// "jdbc:h2:file:" + dbPath); -// -// // give a fake db path (we will make sure it is never create after hibernate usage). -// configuration.setProperty(TopiaConfigurationConstants.CONFIG_URL, -// "jdbc:h2:file:" + dbPathFake); -// } -// }; -// -// @Test -// public void testWithHardcoded() throws Exception { -// -//// Properties dbProperties = TestHelper.loadHibernateConfiguration( -//// "/TopiaConnectionProviderHardcoded.properties"); -//// -//// File directory = new File(TestHelper.getDbName(testBasedir, "testWithHardcoded")); -// -// String dbPath = (String) db.getDbConfiguration().get("dbPath"); -// String dbPathFake = (String) db.getDbConfiguration().get("dbPathFake"); -// -//// new File(directory, "real" + File.separator + "db").getAbsolutePath(); -//// Assert.assertFalse(new File(dbPath).getParentFile().exists()); -// -//// String dbPathFake = new File(directory, "fake" + File.separator + "db").getAbsolutePath(); -// -//// Assert.assertFalse(new File(dbPathFake).getParentFile().exists()); -// -//// // give the path where connection provider will create db -//// dbProperties.setProperty(TEST_URL, "jdbc:h2:file:" + dbPath); -//// -//// // give a fake db path (we will make sure it is never create after hibernate usage). -//// dbProperties.setProperty(Environment.URL, "jdbc:h2:file:" + dbPathFake); -//// -//// root = TopiaContextFactory.getContext(dbProperties); -// -// Locale.setDefault(Locale.FRANCE); -// -// doStuffOnDb(); -// -// // the db file must have been created -// Assert.assertTrue(new File(dbPath).getParentFile().exists()); -// -// // make sure the fake db path was never used -// Assert.assertFalse(new File(dbPathFake).getParentFile().exists()); -// } -// -// private void doStuffOnDb() throws TopiaException { -// TopiaItLegacyTopiaPersistenceContext transaction = db.newPersistenceContext(); -// -// try { -// PersonTopiaDao dao = transaction.getPersonDao(); -// -// Person personne = dao.create(Personne.PROPERTY_NAME, "Jack Bauer"); -// transaction.commit(); -// String idPersonne = personne.getTopiaId(); -// assertNotNull(idPersonne); -// -// transaction.commit(); -// } finally { -// transaction.close(); -// } -// } + public static final String TEST_URL = "hibernate.taiste.testURL"; + + @Rule + public final TopiaItLegacyDatabase db = + new TopiaItLegacyDatabase() { + @Override + protected TopiaItLegacyTopiaApplicationContext createApplicationContext(TopiaConfiguration topiaConfiguration) { + + BeanTopiaConfiguration configuration = TopiaConfigurationBuilder.copyOf(topiaConfiguration); + + Map<String, String> extraConf = configuration.getHibernateExtraConfiguration(); + + // Declare an alternative connection provider. This connection provider will use a different DB path + extraConf.put(AvailableSettings.CONNECTION_PROVIDER, TopiaConnectionProviderHardCoded.class.getName()); + + String tmpdir = System.getProperty("java.io.tmpdir"); + File testDir = new File(tmpdir, String.valueOf(Math.random())); + + // Real DB Path is where the DB will be created + File realFileDir = new File(testDir, "real"); + String realDbPath = new File(realFileDir,"db").getAbsolutePath(); + extraConf.put("hibernate.taiste.realDbPath", realDbPath); + + // Fake DB Path is where the DB would have been created by the classic connection provider + File fakeFileDir = new File(testDir, "fake"); + String fakeDbPath = new File(fakeFileDir, "db").getAbsolutePath(); + extraConf.put("hibernate.taiste.fakeDbPath", fakeDbPath); + + // The custom connection provider will use another JDBC URL than the expected one for Hibernate + extraConf.put(TEST_URL, "jdbc:h2:file:" + realDbPath); + // Let Hibernate believe that this is the good db path + configuration.setJdbcConnectionUrl("jdbc:h2:file:" + fakeDbPath); + + return super.createApplicationContext(configuration); + } + }; + + @Test + public void testWithHardcoded() throws Exception { + + Map<String, String> hibernateExtraConfiguration = db.getApplicationContext().getConfiguration().getHibernateExtraConfiguration(); + String realDbPath = hibernateExtraConfiguration.get("hibernate.taiste.realDbPath"); + String fakeDbPath = hibernateExtraConfiguration.get("hibernate.taiste.fakeDbPath"); + + Locale.setDefault(Locale.FRANCE); + + doStuffOnDb(); + + // the db file must have been created (the one created by our hardcoded connection provider) + Assert.assertTrue(new File(realDbPath).getParentFile().exists()); + + // make sure the fake db path was never used (the one Hibernate may have created) + Assert.assertFalse(new File(fakeDbPath).getParentFile().exists()); + } + + private void doStuffOnDb() throws TopiaException { + TopiaItLegacyTopiaPersistenceContext transaction = db.newPersistenceContext(); + + try { + PersonTopiaDao dao = transaction.getPersonDao(); + + Person personne = dao.create(Personne.PROPERTY_NAME, "Jack Bauer"); + transaction.commit(); + String idPersonne = personne.getTopiaId(); + Assert.assertNotNull(idPersonne); + + transaction.commit(); + } finally { + transaction.close(); + } + } } diff --git a/topia-it/src/test/resources/TopiaConnectionProviderHardcoded.properties b/topia-it/src/test/resources/TopiaConnectionProviderHardcoded.properties deleted file mode 100644 index b40c953..0000000 --- a/topia-it/src/test/resources/TopiaConnectionProviderHardcoded.properties +++ /dev/null @@ -1,34 +0,0 @@ -### -# #%L -# ToPIA :: IT -# $Id$ -# $HeadURL$ -# %% -# Copyright (C) 2004 - 2014 CodeLutin -# %% -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Lesser Public License for more details. -# -# You should have received a copy of the GNU General Lesser Public -# License along with this program. If not, see -# <http://www.gnu.org/licenses/lgpl-3.0.html>. -# #L% -### -# Proprietes par defaut pour une base de donnees de type H2 -hibernate.hbm2ddl.auto=update -hibernate.show_sql=false - -hibernate.dialect=org.hibernate.dialect.H2Dialect -hibernate.connection.username=sa -hibernate.connection.password= -hibernate.connection.driver_class=org.h2.Driver -hibernate.connection.provider_class=org.nuiton.topia.it.legacy.framework.TopiaConnectionProviderHardCoded -# tchemit 2010-11-28 : comment this line, each test must define his own db path -#hibernate.connection.url=jdbc:h2:file:target/surefire-workdir/h2data -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.