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