Project

General

Profile

Download (12.7 KB) Statistics
| Branch: | Tag: | Revision:
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
@Ignore
45
public class CdmPersistentDataSourceTest {
46
	@SuppressWarnings("unused")
47
	private static final Logger logger = Logger.getLogger(CdmPersistentDataSourceTest.class);
48

    
49
	private static CdmPersistentDataSource dataSource;
50

    
51
	/**
52
	 * @throws java.lang.Exception
53
	 */
54
	@BeforeClass
55
	public static void setUpBeforeClass() throws Exception {
56
	}
57

    
58
	/**
59
	 * @throws java.lang.Exception
60
	 */
61
	@AfterClass
62
	public static void tearDownAfterClass() throws Exception {
63
	}
64

    
65
	/**
66
	 * @throws java.lang.Exception
67
	 */
68
	@Before
69
	public void setUp() throws Exception {
70
		dataSource = CdmPersistentDataSource.NewInstance("default");
71
		assertNotNull(dataSource);
72
		//delete tmp
73
		String dataSourceString = "tmp";
74
		//delete
75
		try {
76
			CdmPersistentSourceUtils.delete(CdmPersistentDataSource.NewInstance(dataSourceString));
77
		} catch (DataSourceNotFoundException e) {
78
			//;
79
		}
80
		assertFalse(CdmPersistentDataSource.exists(dataSourceString));
81

    
82
	}
83

    
84
	/**
85
	 * @throws java.lang.Exception
86
	 */
87
	@After
88
	public void tearDown() throws Exception {
89
		//delete tmp
90
		String dataSourceString = "tmp";
91
		//delete
92
		try {
93
			CdmPersistentSourceUtils.delete(CdmPersistentDataSource.NewInstance(dataSourceString));
94
		} catch (DataSourceNotFoundException e) {
95
			//;
96
		}
97
		assertFalse(CdmPersistentDataSource.exists(dataSourceString));
98

    
99
	}
100

    
101
//********************** TESTS ***********************************************/
102

    
103
	@Test
104
	public void testDummy() {
105
		assertEquals(1,1);
106
	}
107

    
108
	/**
109
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#NewDefaultInstance()}.
110
	 */
111
	@Test
112
	public void testNewDefaultInstance() {
113
		try {
114
			CdmPersistentDataSource defaultDs = CdmPersistentDataSource.NewInstance("default");
115
			assertNotNull(defaultDs);
116
			assertEquals(CdmPersistentDataSource.NewInstance("default"), CdmPersistentDataSource.NewDefaultInstance());
117
		} catch (DataSourceNotFoundException e) {
118
			fail();
119
		}
120
	}
121

    
122
	/**
123
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#NewLocalHsqlInstance()}.
124
	 */
125
	@Test
126
	public void testNewLocalHsqlInstance() {
127
		try {
128
			assertEquals(CdmPersistentDataSource.NewInstance("localDefaultHsql"), CdmPersistentDataSource.NewLocalHsqlInstance());
129
		} catch (DataSourceNotFoundException e) {
130
			fail();
131
		}
132
	}
133

    
134
	/**
135
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#NewInstance(java.lang.String)}.
136
	 */
137
	@Test
138
	public void testNewInstance() {
139
		assertNotNull(dataSource);
140
	}
141

    
142
	/**
143
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getName()}.
144
	 */
145
	@Test
146
	public void testGetName() {
147
		assertEquals("default", dataSource.getName());
148
	}
149

    
150
	/**
151
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDatabaseType()}.
152
	 */
153
	@Test
154
	public void testGetDatabaseType() {
155
		assertEquals(DatabaseTypeEnum.MySQL, dataSource.getDatabaseType());
156
	}
157

    
158
	/**
159
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDbProperty(eu.etaxonomy.cdm.database.CdmPersistentDataSource.DbProperties)}.
160
	 */
161
	@Test
162
	public void testGetDbProperty() {
163
		assertEquals("com.mysql.cj.jdbc.Driver", dataSource.getCdmSourceProperty(CdmSourceProperties.DRIVER_CLASS));
164
	}
165

    
166
	/**
167
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDatasourceBean()}.
168
	 */
169
	@Test
170
	public void testGetDatasourceBean() {
171
		BeanDefinition beanDef = dataSource.getDatasourceBean();
172
		PropertyValues propValues = beanDef.getPropertyValues();
173
		String propName =  "driverClassName";
174
		assertEquals("com.mysql.cj.jdbc.Driver", propValues.getPropertyValue(propName).getValue());
175
		propName =  "url";
176
		assertEquals("testUrl", propValues.getPropertyValue(propName).getValue());
177
		propName =  "username";
178
		assertEquals("testUser", propValues.getPropertyValue(propName).getValue());
179
		propName =  "password";
180
		assertEquals("testPassword", propValues.getPropertyValue(propName).getValue());
181
	}
182

    
183
	/**
184
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)}.
185
	 */
186
	@Test
187
	public void testGetHibernatePropertiesBeanHBM2DDL() {
188
		DbSchemaValidation hbm2dll = DbSchemaValidation.CREATE;
189
		BeanDefinition beanDef = dataSource.getHibernatePropertiesBean(hbm2dll);
190
		PropertyValues propValues = beanDef.getPropertyValues();
191
		String propName =  "properties";
192
		PropertyValue propValue =  propValues.getPropertyValue(propName);
193
		assertNotNull(propValue);
194
		assertTrue( propValue.getValue() instanceof Properties);
195

    
196
		Properties properties = (Properties)propValue.getValue();
197
		assertEquals(hbm2dll.toString(), properties.getProperty("hibernate.hbm2ddl.auto"));
198
		assertEquals(dataSource.getDatabaseType().getHibernateDialectCanonicalName(), properties.getProperty("hibernate.dialect"));
199
		assertEquals(NoCachingRegionFactory.class.getName(), properties.getProperty("hibernate.cache.region.factory_class"));
200
		assertEquals(String.valueOf(false), properties.getProperty("hibernate.show_sql"));
201
		assertEquals(String.valueOf(false), properties.getProperty("hibernate.format_sql"));
202

    
203
		//null
204
		beanDef = dataSource.getHibernatePropertiesBean(null);
205
		propValues = beanDef.getPropertyValues();
206
		propName =  "properties";
207
		propValue =  propValues.getPropertyValue(propName);
208
		assertNotNull(propValue);
209
		assertTrue( propValue.getValue() instanceof Properties);
210

    
211
		properties = (Properties)propValue.getValue();
212
		assertEquals(DbSchemaValidation.VALIDATE.toString(), properties.getProperty("hibernate.hbm2ddl.auto"));
213
	}
214

    
215
	/**
216
	 * 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)}.
217
	 */
218
	@Test
219
	public void testGetHibernatePropertiesBeanHBM2DDLBooleanBooleanClassOfQextendsCacheProvider() {
220
		DbSchemaValidation hbm2dll = DbSchemaValidation.CREATE;
221
		boolean showSql = false;
222
		boolean formatSql = false;
223
		boolean registerSearchListener = false;
224
		Class<? extends RegionFactory> cacheProviderClass = NoCachingRegionFactory.class;
225

    
226
		BeanDefinition beanDef = dataSource.getHibernatePropertiesBean(hbm2dll, showSql, formatSql, registerSearchListener, cacheProviderClass);
227
		PropertyValues propValues = beanDef.getPropertyValues();
228
		String propName =  "properties";
229
		PropertyValue propValue =  propValues.getPropertyValue(propName);
230
		assertNotNull(propValue);
231
		assertTrue( propValue.getValue() instanceof Properties);
232

    
233
		Properties properties = (Properties)propValue.getValue();
234
		assertEquals(hbm2dll.toString(), properties.getProperty("hibernate.hbm2ddl.auto"));
235
		assertEquals(dataSource.getDatabaseType().getHibernateDialectCanonicalName(), properties.getProperty("hibernate.dialect"));
236
		assertEquals(cacheProviderClass.getName(), properties.getProperty("hibernate.cache.region.factory_class"));
237
		assertEquals(String.valueOf(showSql), properties.getProperty("hibernate.show_sql"));
238
		assertEquals(String.valueOf(formatSql), properties.getProperty("hibernate.format_sql"));
239
	}
240

    
241
	/**
242
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#exists(java.lang.String)}.
243
	 */
244
	@Test
245
	public void testExists() {
246
		assertTrue(CdmPersistentDataSource.exists("default"));
247
		assertTrue(CdmPersistentDataSource.exists("localDefaultHsql"));
248
		assertFalse(CdmPersistentDataSource.exists("xlsj�dfl"));
249
	}
250

    
251
	/**
252
	 * 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)}.
253
	 */
254
	@Test
255
	public void testSaveStringDatabaseTypeEnumStringStringIntStringString() {
256
		String dataSourceString = "tmp";
257
		assertFalse(CdmPersistentDataSource.exists(dataSourceString));
258

    
259
		DatabaseTypeEnum databaseType = DatabaseTypeEnum.SqlServer2005;
260
		String servername = "server";
261
		String db = "database";
262
		String username = "username";
263
		String password = "password";
264
		int port = 1234;
265

    
266
		ICdmDataSource dataSource = CdmDataSource.NewInstance(databaseType, servername, db, port, username, password);
267

    
268
		CdmPersistentDataSource.save(dataSourceString, dataSource);
269
		assertTrue(CdmPersistentDataSource.exists(dataSourceString));
270

    
271
		CdmPersistentDataSource loadedDataSource = null;
272
		try {
273
			loadedDataSource = CdmPersistentDataSource.NewInstance(dataSourceString);
274
		} catch (DataSourceNotFoundException e1) {
275
			fail();
276
		}
277
		assertEquals(databaseType, dataSource.getDatabaseType());
278
		assertEquals(DatabaseTypeEnum.SqlServer2005.getDriverClassName(), loadedDataSource.getCdmSourceProperty(CdmSourceProperties.DRIVER_CLASS));
279
		assertEquals("jdbc:sqlserver://server:1234;databaseName=database;SelectMethod=cursor", loadedDataSource.getCdmSourceProperty(CdmSourceProperties.URL));
280
		assertEquals(username, loadedDataSource.getCdmSourceProperty(CdmSourceProperties.USERNAME));
281
		assertEquals(password, loadedDataSource.getCdmSourceProperty(CdmSourceProperties.PASSWORD));
282
		//delete
283
		try {
284
			CdmPersistentSourceUtils.delete(CdmPersistentDataSource.NewInstance(dataSourceString));
285
		} catch (DataSourceNotFoundException e) {
286
			fail();
287
		}
288
		assertFalse(CdmPersistentDataSource.exists(dataSourceString));
289
	}
290

    
291
	/**
292
	 * 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)}.
293
	 */
294
	//@Test
295
	public void testSaveStringDatabaseTypeEnumStringStringStringString() {
296
		//see testSaveStringDatabaseTypeEnumStringStringIntStringString
297
	}
298

    
299
	/**
300
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#delete(eu.etaxonomy.cdm.database.CdmPersistentDataSource)}.
301
	 */
302
	@Test
303
	public void testDelete() {
304
		testSaveStringDatabaseTypeEnumStringStringStringString();
305
	}
306

    
307
	/**
308
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getAllDataSources()}.
309
	 */
310
	@Test
311
	public void testGetAllDataSources() {
312
		//assertEquals(6, CdmPersistentDataSource.getAllDataSources().size());  //does not run for all orders of tests of this class
313
		assertEquals("default", CdmPersistentDataSource.getAllDataSources().get(0).getName());
314
	}
315

    
316
	/**
317
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#toString()}.
318
	 */
319
	@Test
320
	public void testToString() {
321
		String dataSourceName = "default";
322
		try {
323
			assertEquals(dataSourceName, CdmPersistentDataSource.NewInstance(dataSourceName).toString());
324
		} catch (DataSourceNotFoundException e) {
325
			fail();
326
		}
327
	}
328

    
329
	/**
330
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDataSourceInputStream()}.
331
	 */
332
	@Test
333
	public void testGetDataSourceInputStream() {
334
		FileInputStream is = CdmPersistentSourceUtils.getCdmSourceInputStream();
335
		assertNotNull( is);
336
		int firstInput;
337
		try {
338
			firstInput = is.read();
339
			assertEquals("Input Stream should start with < (=Ascii(60))", 60, firstInput);
340
		} catch (IOException e) {
341
			fail("Exception occurred in datasource input stream read");
342
		}
343

    
344
	}
345

    
346
	/**
347
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#getDataSourceOutputStream()}.
348
	 */
349
	@Ignore
350
	@Test
351
	public void testGetDataSourceOutputStream() {
352
		FileOutputStream os = CdmPersistentSourceUtils.getCdmSourceOutputStream();
353
		assertNotNull(os);
354
	}
355

    
356
}
(2-2/6)