1 /*
2 * #%L
3 * ToPIA :: Persistence
4 *
5 * $Id: TopiaConnectionProviderTest.java 2416 2012-03-02 10:08:59Z tchemit $
6 * $HeadURL: http://svn.nuiton.org/svn/topia/tags/topia-2.8-rc-1/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java $
7 * %%
8 * Copyright (C) 2004 - 2011 CodeLutin
9 * %%
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser 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 Lesser Public License for more details.
19 *
20 * You should have received a copy of the GNU General Lesser Public
21 * License along with this program. If not, see
22 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23 * #L%
24 */
25 package org.nuiton.topia.framework;
26
27 import org.junit.Assert;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.nuiton.topia.TopiaContext;
31 import org.nuiton.topia.TopiaContextFactory;
32 import org.nuiton.topia.TopiaDatabase;
33 import org.nuiton.topia.TopiaException;
34 import org.nuiton.topia.TopiaTestDAOHelper;
35 import org.nuiton.topia.test.entities.Person;
36 import org.nuiton.topia.test.entities.PersonDAO;
37 import org.nuiton.topiatest.Personne;
38
39 import java.io.File;
40 import java.util.Locale;
41 import java.util.Properties;
42
43 import static org.junit.Assert.assertNotNull;
44
45 /**
46 * To test the {@link TopiaConnectionProvider} and make sure all connections
47 * are done from here...
48 *
49 * @author tchemit <chemit@codelutin.com>
50 * @since 2.5.3
51 */
52 public class TopiaConnectionProviderTest {
53
54 // private static final Log log =
55 // LogFactory.getLog(TopiaConnectionProviderTest.class);
56
57 public static final String TEST_URL = "testURL";
58
59 @Rule
60 public final TopiaDatabase db =
61 new TopiaDatabase("/TopiaConnectionProviderHardcoded.properties") {
62
63 @Override
64 protected void onDbConfigurationCreate(Properties configuration,
65 File testdir,
66 String dbPath) {
67
68 Assert.assertFalse(testdir.exists());
69
70 String dbPathFake = new File(testdir, "fake" + File.separator + "db").getAbsolutePath();
71
72 Assert.assertFalse(new File(dbPathFake).getParentFile().exists());
73
74 configuration.setProperty("dbPath", dbPath);
75 configuration.setProperty("dbPathFake", dbPathFake);
76
77 // give the path where connection provider will create db
78 configuration.setProperty(TEST_URL,
79 "jdbc:h2:file:" + dbPath);
80
81 // give a fake db path (we will make sure it is never create after hibernate usage).
82 configuration.setProperty(TopiaContextFactory.CONFIG_URL,
83 "jdbc:h2:file:" + dbPathFake);
84 }
85 };
86
87 @Test
88 public void testWithHardcoded() throws Exception {
89
90 // Properties dbProperties = TestHelper.loadHibernateConfiguration(
91 // "/TopiaConnectionProviderHardcoded.properties");
92 //
93 // File directory = new File(TestHelper.getDbName(testBasedir, "testWithHardcoded"));
94
95 String dbPath = (String) db.getDbConfiguration().get("dbPath");
96 String dbPathFake = (String) db.getDbConfiguration().get("dbPathFake");
97
98 // new File(directory, "real" + File.separator + "db").getAbsolutePath();
99 // Assert.assertFalse(new File(dbPath).getParentFile().exists());
100
101 // String dbPathFake = new File(directory, "fake" + File.separator + "db").getAbsolutePath();
102
103 // Assert.assertFalse(new File(dbPathFake).getParentFile().exists());
104
105 // // give the path where connection provider will create db
106 // dbProperties.setProperty(TEST_URL, "jdbc:h2:file:" + dbPath);
107 //
108 // // give a fake db path (we will make sure it is never create after hibernate usage).
109 // dbProperties.setProperty(Environment.URL, "jdbc:h2:file:" + dbPathFake);
110 //
111 // root = TopiaContextFactory.getContext(dbProperties);
112
113 Locale.setDefault(Locale.FRANCE);
114
115 doStuffOnDb();
116
117 // the db file must have been created
118 Assert.assertTrue(new File(dbPath).getParentFile().exists());
119
120 // make sure the fake db path was never used
121 Assert.assertFalse(new File(dbPathFake).getParentFile().exists());
122 }
123
124 private void doStuffOnDb() throws TopiaException {
125 TopiaContext transaction = db.beginTransaction();
126
127 try {
128 PersonDAO dao = TopiaTestDAOHelper.getPersonDAO(transaction);
129
130 Person personne = dao.create(Personne.PROPERTY_NAME, "Jack Bauer");
131 transaction.commitTransaction();
132 String idPersonne = personne.getTopiaId();
133 assertNotNull(idPersonne);
134
135 transaction.commitTransaction();
136 } finally {
137 transaction.closeContext();
138 }
139 }
140 }