Allow access to hibernate dialect via DatabaseTypeEnum, use this for foreign key...
[cdmlib.git] / cdmlib-persistence / src / test / java / eu / etaxonomy / cdm / database / CdmPersistentDataSourceTest.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.database;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17
18 import java.io.FileInputStream;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.util.Properties;
22
23 import org.apache.log4j.Logger;
24 import org.hibernate.cache.internal.NoCachingRegionFactory;
25 import org.hibernate.cache.spi.RegionFactory;
26 import org.junit.After;
27 import org.junit.AfterClass;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.springframework.beans.PropertyValue;
33 import org.springframework.beans.PropertyValues;
34 import org.springframework.beans.factory.config.BeanDefinition;
35
36 import eu.etaxonomy.cdm.database.CdmPersistentDataSource.DbProperties;
37
38 /**
39 * @author a.mueller
40 *
41 */
42 public class CdmPersistentDataSourceTest {
43 @SuppressWarnings("unused")
44 private static final Logger logger = Logger.getLogger(CdmPersistentDataSourceTest.class);
45
46 private static CdmPersistentDataSource dataSource;
47
48 /**
49 * @throws java.lang.Exception
50 */
51 @BeforeClass
52 public static void setUpBeforeClass() throws Exception {
53 }
54
55 /**
56 * @throws java.lang.Exception
57 */
58 @AfterClass
59 public static void tearDownAfterClass() throws Exception {
60 }
61
62 /**
63 * @throws java.lang.Exception
64 */
65 @Before
66 public void setUp() throws Exception {
67 dataSource = CdmPersistentDataSource.NewInstance("default");
68 assertNotNull(dataSource);
69 //delete tmp
70 String dataSourceString = "tmp";
71 //delete
72 try {
73 CdmPersistentDataSource.delete(CdmPersistentDataSource.NewInstance(dataSourceString));
74 } catch (DataSourceNotFoundException e) {
75 //;
76 }
77 assertFalse(CdmPersistentDataSource.exists(dataSourceString));
78
79 }
80
81 /**
82 * @throws java.lang.Exception
83 */
84 @After
85 public void tearDown() throws Exception {
86 //delete tmp
87 String dataSourceString = "tmp";
88 //delete
89 try {
90 CdmPersistentDataSource.delete(CdmPersistentDataSource.NewInstance(dataSourceString));
91 } catch (DataSourceNotFoundException e) {
92 //;
93 }
94 assertFalse(CdmPersistentDataSource.exists(dataSourceString));
95
96 }
97
98 //********************** TESTS ***********************************************/
99
100 @Test
101 public void testDummy() {
102 assertEquals(1,1);
103 }
104
105 /**
106 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#NewDefaultInstance()}.
107 */
108 @Test
109 public void testNewDefaultInstance() {
110 try {
111 CdmPersistentDataSource defaultDs = CdmPersistentDataSource.NewInstance("default");
112 assertNotNull(defaultDs);
113 assertEquals(CdmPersistentDataSource.NewInstance("default"), CdmPersistentDataSource.NewDefaultInstance());
114 } catch (DataSourceNotFoundException e) {
115 fail();
116 }
117 }
118
119 /**
120 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#NewLocalHsqlInstance()}.
121 */
122 @Test
123 public void testNewLocalHsqlInstance() {
124 try {
125 assertEquals(CdmPersistentDataSource.NewInstance("localDefaultHsql"), CdmPersistentDataSource.NewLocalHsqlInstance());
126 } catch (DataSourceNotFoundException e) {
127 fail();
128 }
129 }
130
131 /**
132 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#NewInstance(java.lang.String)}.
133 */
134 @Test
135 public void testNewInstance() {
136 assertNotNull(dataSource);
137 }
138
139 /**
140 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getName()}.
141 */
142 @Test
143 public void testGetName() {
144 assertEquals("default", dataSource.getName());
145 }
146
147 /**
148 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDatabaseType()}.
149 */
150 @Test
151 public void testGetDatabaseType() {
152 assertEquals(DatabaseTypeEnum.MySQL, dataSource.getDatabaseType());
153 }
154
155 /**
156 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDbProperty(eu.etaxonomy.cdm.database.CdmPersistentDataSource.DbProperties)}.
157 */
158 @Test
159 public void testGetDbProperty() {
160 assertEquals("com.mysql.jdbc.Driver", dataSource.getDatasourceProperty(DbProperties.DRIVER_CLASS));
161 }
162
163 /**
164 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDatasourceBean()}.
165 */
166 @Test
167 public void testGetDatasourceBean() {
168 BeanDefinition beanDef = dataSource.getDatasourceBean();
169 PropertyValues propValues = beanDef.getPropertyValues();
170 String propName = "driverClassName";
171 assertEquals("com.mysql.jdbc.Driver", propValues.getPropertyValue(propName).getValue());
172 propName = "url";
173 assertEquals("testUrl", propValues.getPropertyValue(propName).getValue());
174 propName = "username";
175 assertEquals("testUser", propValues.getPropertyValue(propName).getValue());
176 propName = "password";
177 assertEquals("testPassword", propValues.getPropertyValue(propName).getValue());
178 }
179
180 /**
181 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)}.
182 */
183 @Test
184 public void testGetHibernatePropertiesBeanHBM2DDL() {
185 DbSchemaValidation hbm2dll = DbSchemaValidation.CREATE;
186 BeanDefinition beanDef = dataSource.getHibernatePropertiesBean(hbm2dll);
187 PropertyValues propValues = beanDef.getPropertyValues();
188 String propName = "properties";
189 PropertyValue propValue = propValues.getPropertyValue(propName);
190 assertNotNull(propValue);
191 assertTrue( propValue.getValue() instanceof Properties);
192
193 Properties properties = (Properties)propValue.getValue();
194 assertEquals(hbm2dll.toString(), properties.getProperty("hibernate.hbm2ddl.auto"));
195 assertEquals(dataSource.getDatabaseType().getHibernateDialectCanonicalName(), properties.getProperty("hibernate.dialect"));
196 assertEquals(NoCachingRegionFactory.class.getName(), properties.getProperty("hibernate.cache.region.factory_class"));
197 assertEquals(String.valueOf(false), properties.getProperty("hibernate.show_sql"));
198 assertEquals(String.valueOf(false), properties.getProperty("hibernate.format_sql"));
199
200 //null
201 beanDef = dataSource.getHibernatePropertiesBean(null);
202 propValues = beanDef.getPropertyValues();
203 propName = "properties";
204 propValue = propValues.getPropertyValue(propName);
205 assertNotNull(propValue);
206 assertTrue( propValue.getValue() instanceof Properties);
207
208 properties = (Properties)propValue.getValue();
209 assertEquals(DbSchemaValidation.VALIDATE.toString(), properties.getProperty("hibernate.hbm2ddl.auto"));
210 }
211
212 /**
213 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL, java.lang.Boolean, java.lang.Boolean, java.lang.Class)}.
214 */
215 @Test
216 public void testGetHibernatePropertiesBeanHBM2DDLBooleanBooleanClassOfQextendsCacheProvider() {
217 DbSchemaValidation hbm2dll = DbSchemaValidation.CREATE;
218 boolean showSql = false;
219 boolean formatSql = false;
220 boolean registerSearchListener = false;
221 Class<? extends RegionFactory> cacheProviderClass = NoCachingRegionFactory.class;
222
223 BeanDefinition beanDef = dataSource.getHibernatePropertiesBean(hbm2dll, showSql, formatSql, registerSearchListener, cacheProviderClass);
224 PropertyValues propValues = beanDef.getPropertyValues();
225 String propName = "properties";
226 PropertyValue propValue = propValues.getPropertyValue(propName);
227 assertNotNull(propValue);
228 assertTrue( propValue.getValue() instanceof Properties);
229
230 Properties properties = (Properties)propValue.getValue();
231 assertEquals(hbm2dll.toString(), properties.getProperty("hibernate.hbm2ddl.auto"));
232 assertEquals(dataSource.getDatabaseType().getHibernateDialectCanonicalName(), properties.getProperty("hibernate.dialect"));
233 assertEquals(cacheProviderClass.getName(), properties.getProperty("hibernate.cache.region.factory_class"));
234 assertEquals(String.valueOf(showSql), properties.getProperty("hibernate.show_sql"));
235 assertEquals(String.valueOf(formatSql), properties.getProperty("hibernate.format_sql"));
236 }
237
238 /**
239 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#exists(java.lang.String)}.
240 */
241 @Test
242 public void testExists() {
243 assertTrue(CdmPersistentDataSource.exists("default"));
244 assertTrue(CdmPersistentDataSource.exists("localDefaultHsql"));
245 assertFalse(CdmPersistentDataSource.exists("xlsj�dfl"));
246 }
247
248 /**
249 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#save(java.lang.String, eu.etaxonomy.cdm.database.DatabaseTypeEnum, java.lang.String, java.lang.String, java.lang.String, java.lang.String)}.
250 */
251 @Test
252 public void testSaveStringDatabaseTypeEnumStringStringIntStringString() {
253 String dataSourceString = "tmp";
254 assertFalse(CdmPersistentDataSource.exists(dataSourceString));
255
256 DatabaseTypeEnum databaseType = DatabaseTypeEnum.SqlServer2005;
257 String servername = "server";
258 String db = "database";
259 String username = "username";
260 String password = "password";
261 int port = 1234;
262
263 ICdmDataSource dataSource = CdmDataSource.NewInstance(databaseType, servername, db, port, username, password);
264
265 CdmPersistentDataSource.save(dataSourceString, dataSource);
266 assertTrue(CdmPersistentDataSource.exists(dataSourceString));
267
268 CdmPersistentDataSource loadedDataSource = null;
269 try {
270 loadedDataSource = CdmPersistentDataSource.NewInstance(dataSourceString);
271 } catch (DataSourceNotFoundException e1) {
272 fail();
273 }
274 assertEquals(databaseType, dataSource.getDatabaseType());
275 assertEquals(DatabaseTypeEnum.SqlServer2005.getDriverClassName(), loadedDataSource.getDatasourceProperty(DbProperties.DRIVER_CLASS));
276 assertEquals("jdbc:sqlserver://server:1234;databaseName=database;SelectMethod=cursor", loadedDataSource.getDatasourceProperty(DbProperties.URL));
277 assertEquals(username, loadedDataSource.getDatasourceProperty(DbProperties.USERNAME));
278 assertEquals(password, loadedDataSource.getDatasourceProperty(DbProperties.PASSWORD));
279 //delete
280 try {
281 CdmPersistentDataSource.delete(CdmPersistentDataSource.NewInstance(dataSourceString));
282 } catch (DataSourceNotFoundException e) {
283 fail();
284 }
285 assertFalse(CdmPersistentDataSource.exists(dataSourceString));
286 }
287
288 /**
289 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#save(java.lang.String, eu.etaxonomy.cdm.database.DatabaseTypeEnum, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)}.
290 */
291 //@Test
292 public void testSaveStringDatabaseTypeEnumStringStringStringString() {
293 //see testSaveStringDatabaseTypeEnumStringStringIntStringString
294 }
295
296 /**
297 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#delete(eu.etaxonomy.cdm.database.CdmPersistentDataSource)}.
298 */
299 @Test
300 public void testDelete() {
301 testSaveStringDatabaseTypeEnumStringStringStringString();
302 }
303
304 /**
305 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getAllDataSources()}.
306 */
307 @Test
308 public void testGetAllDataSources() {
309 //assertEquals(6, CdmPersistentDataSource.getAllDataSources().size()); //does not run for all orders of tests of this class
310 assertEquals("default", CdmPersistentDataSource.getAllDataSources().get(0).getName());
311 }
312
313 /**
314 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#toString()}.
315 */
316 @Test
317 public void testToString() {
318 String dataSourceName = "default";
319 try {
320 assertEquals(dataSourceName, CdmPersistentDataSource.NewInstance(dataSourceName).toString());
321 } catch (DataSourceNotFoundException e) {
322 fail();
323 }
324 }
325
326 /**
327 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDataSourceInputStream()}.
328 */
329 @Test
330 public void testGetDataSourceInputStream() {
331 FileInputStream is = CdmPersistentDataSource.getDataSourceInputStream();
332 assertNotNull( is);
333 int firstInput;
334 try {
335 firstInput = is.read();
336 assertEquals("Input Stream should start with < (=Ascii(60))", 60, firstInput);
337 } catch (IOException e) {
338 fail("Exception occurred in datasource input stream read");
339 }
340
341 }
342
343 /**
344 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDataSourceOutputStream()}.
345 */
346 @Ignore
347 @Test
348 public void testGetDataSourceOutputStream() {
349 FileOutputStream os = CdmPersistentDataSource.getDataSourceOutputStream();
350 assertNotNull(os);
351 }
352
353 }