2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.database
;
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
;
18 import java
.io
.FileInputStream
;
19 import java
.io
.FileOutputStream
;
20 import java
.io
.IOException
;
21 import java
.util
.Properties
;
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
;
36 import eu
.etaxonomy
.cdm
.config
.CdmPersistentSourceUtils
;
37 import eu
.etaxonomy
.cdm
.config
.CdmPersistentXMLSource
.CdmSourceProperties
;
44 public class CdmPersistentDataSourceTest
{
45 @SuppressWarnings("unused")
46 private static final Logger logger
= Logger
.getLogger(CdmPersistentDataSourceTest
.class);
48 private static CdmPersistentDataSource dataSource
;
51 * @throws java.lang.Exception
54 public static void setUpBeforeClass() throws Exception
{
58 * @throws java.lang.Exception
61 public static void tearDownAfterClass() throws Exception
{
65 * @throws java.lang.Exception
68 public void setUp() throws Exception
{
69 dataSource
= CdmPersistentDataSource
.NewInstance("default");
70 assertNotNull(dataSource
);
72 String dataSourceString
= "tmp";
75 CdmPersistentSourceUtils
.delete(CdmPersistentDataSource
.NewInstance(dataSourceString
));
76 } catch (DataSourceNotFoundException e
) {
79 assertFalse(CdmPersistentDataSource
.exists(dataSourceString
));
84 * @throws java.lang.Exception
87 public void tearDown() throws Exception
{
89 String dataSourceString
= "tmp";
92 CdmPersistentSourceUtils
.delete(CdmPersistentDataSource
.NewInstance(dataSourceString
));
93 } catch (DataSourceNotFoundException e
) {
96 assertFalse(CdmPersistentDataSource
.exists(dataSourceString
));
100 //********************** TESTS ***********************************************/
103 public void testDummy() {
108 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#NewDefaultInstance()}.
111 public void testNewDefaultInstance() {
113 CdmPersistentDataSource defaultDs
= CdmPersistentDataSource
.NewInstance("default");
114 assertNotNull(defaultDs
);
115 assertEquals(CdmPersistentDataSource
.NewInstance("default"), CdmPersistentDataSource
.NewDefaultInstance());
116 } catch (DataSourceNotFoundException e
) {
122 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#NewLocalHsqlInstance()}.
125 public void testNewLocalHsqlInstance() {
127 assertEquals(CdmPersistentDataSource
.NewInstance("localDefaultHsql"), CdmPersistentDataSource
.NewLocalHsqlInstance());
128 } catch (DataSourceNotFoundException e
) {
134 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#NewInstance(java.lang.String)}.
137 public void testNewInstance() {
138 assertNotNull(dataSource
);
142 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getName()}.
145 public void testGetName() {
146 assertEquals("default", dataSource
.getName());
150 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDatabaseType()}.
153 public void testGetDatabaseType() {
154 assertEquals(DatabaseTypeEnum
.MySQL
, dataSource
.getDatabaseType());
158 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDbProperty(eu.etaxonomy.cdm.database.CdmPersistentDataSource.DbProperties)}.
161 public void testGetDbProperty() {
162 assertEquals("com.mysql.jdbc.Driver", dataSource
.getCdmSourceProperty(CdmSourceProperties
.DRIVER_CLASS
));
166 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDatasourceBean()}.
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());
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());
183 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)}.
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
);
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"));
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
);
210 properties
= (Properties
)propValue
.getValue();
211 assertEquals(DbSchemaValidation
.VALIDATE
.toString(), properties
.getProperty("hibernate.hbm2ddl.auto"));
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)}.
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;
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
);
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"));
241 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#exists(java.lang.String)}.
244 public void testExists() {
245 assertTrue(CdmPersistentDataSource
.exists("default"));
246 assertTrue(CdmPersistentDataSource
.exists("localDefaultHsql"));
247 assertFalse(CdmPersistentDataSource
.exists("xlsj�dfl"));
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)}.
254 public void testSaveStringDatabaseTypeEnumStringStringIntStringString() {
255 String dataSourceString
= "tmp";
256 assertFalse(CdmPersistentDataSource
.exists(dataSourceString
));
258 DatabaseTypeEnum databaseType
= DatabaseTypeEnum
.SqlServer2005
;
259 String servername
= "server";
260 String db
= "database";
261 String username
= "username";
262 String password
= "password";
265 ICdmDataSource dataSource
= CdmDataSource
.NewInstance(databaseType
, servername
, db
, port
, username
, password
);
267 CdmPersistentDataSource
.save(dataSourceString
, dataSource
);
268 assertTrue(CdmPersistentDataSource
.exists(dataSourceString
));
270 CdmPersistentDataSource loadedDataSource
= null;
272 loadedDataSource
= CdmPersistentDataSource
.NewInstance(dataSourceString
);
273 } catch (DataSourceNotFoundException e1
) {
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
));
283 CdmPersistentSourceUtils
.delete(CdmPersistentDataSource
.NewInstance(dataSourceString
));
284 } catch (DataSourceNotFoundException e
) {
287 assertFalse(CdmPersistentDataSource
.exists(dataSourceString
));
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)}.
294 public void testSaveStringDatabaseTypeEnumStringStringStringString() {
295 //see testSaveStringDatabaseTypeEnumStringStringIntStringString
299 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#delete(eu.etaxonomy.cdm.database.CdmPersistentDataSource)}.
302 public void testDelete() {
303 testSaveStringDatabaseTypeEnumStringStringStringString();
307 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getAllDataSources()}.
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());
316 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#toString()}.
319 public void testToString() {
320 String dataSourceName
= "default";
322 assertEquals(dataSourceName
, CdmPersistentDataSource
.NewInstance(dataSourceName
).toString());
323 } catch (DataSourceNotFoundException e
) {
329 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDataSourceInputStream()}.
332 public void testGetDataSourceInputStream() {
333 FileInputStream is
= CdmPersistentSourceUtils
.getCdmSourceInputStream();
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");
346 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDataSourceOutputStream()}.
350 public void testGetDataSourceOutputStream() {
351 FileOutputStream os
= CdmPersistentSourceUtils
.getCdmSourceOutputStream();