Project

General

Profile

Download (11.9 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.logging.log4j.LogManager;import org.apache.logging.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
import eu.etaxonomy.cdm.persistence.hibernate.HibernateConfiguration;
39

    
40

    
41
/**
42
 * @author a.mueller
43
 *
44
 */
45
@Ignore
46
public class CdmPersistentDataSourceTest {
47
	@SuppressWarnings("unused")
48
	private static final Logger logger = LogManager.getLogger(CdmPersistentDataSourceTest.class);
49

    
50
	private static CdmPersistentDataSource dataSource;
51

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

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

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

    
83
	}
84

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

    
100
	}
101

    
102
//********************** TESTS ***********************************************/
103

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
235
		Properties properties = (Properties)propValue.getValue();
236
		assertEquals(hbm2dll.toString(), properties.getProperty("hibernate.hbm2ddl.auto"));
237
		assertEquals(dataSource.getDatabaseType().getHibernateDialectCanonicalName(), properties.getProperty("hibernate.dialect"));
238
		assertEquals(cacheProviderClass.getName(), properties.getProperty("hibernate.cache.region.factory_class"));
239
		assertEquals(String.valueOf(showSql), properties.getProperty("hibernate.show_sql"));
240
		assertEquals(String.valueOf(formatSql), properties.getProperty("hibernate.format_sql"));
241
	}
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
	@Test
251
	public void testSaveStringDatabaseTypeEnumStringStringIntStringString() {
252
		String dataSourceString = "tmp";
253
		assertFalse(CdmPersistentDataSource.exists(dataSourceString));
254

    
255
		DatabaseTypeEnum databaseType = DatabaseTypeEnum.SqlServer2005;
256
		String servername = "server";
257
		String db = "database";
258
		String username = "username";
259
		String password = "password";
260
		int port = 1234;
261

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

    
264
		CdmPersistentDataSource.save(dataSourceString, dataSource);
265
		assertTrue(CdmPersistentDataSource.exists(dataSourceString));
266

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

    
287
	//@Test
288
	public void testSaveStringDatabaseTypeEnumStringStringStringString() {
289
		//see testSaveStringDatabaseTypeEnumStringStringIntStringString
290
	}
291

    
292
	/**
293
	 * Test method for {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource#delete(eu.etaxonomy.cdm.database.CdmPersistentDataSource)}.
294
	 */
295
	@Test
296
	public void testDelete() {
297
		testSaveStringDatabaseTypeEnumStringStringStringString();
298
	}
299

    
300
	@Test
301
	public void testGetAllDataSources() {
302
		//assertEquals(6, CdmPersistentDataSource.getAllDataSources().size());  //does not run for all orders of tests of this class
303
		assertEquals("default", CdmPersistentDataSource.getAllDataSources().get(0).getName());
304
	}
305

    
306
	@Test
307
	public void testToString() {
308
		String dataSourceName = "default";
309
		try {
310
			assertEquals(dataSourceName, CdmPersistentDataSource.NewInstance(dataSourceName).toString());
311
		} catch (DataSourceNotFoundException e) {
312
			fail();
313
		}
314
	}
315

    
316
	@Test
317
	public void testGetDataSourceInputStream() {
318
		FileInputStream is = CdmPersistentSourceUtils.getCdmSourceInputStream();
319
		assertNotNull( is);
320
		int firstInput;
321
		try {
322
			firstInput = is.read();
323
			assertEquals("Input Stream should start with < (=Ascii(60))", 60, firstInput);
324
		} catch (IOException e) {
325
			fail("Exception occurred in datasource input stream read");
326
		}
327

    
328
	}
329

    
330

    
331
	@Ignore
332
	@Test
333
	public void testGetDataSourceOutputStream() {
334
		FileOutputStream os = CdmPersistentSourceUtils.getCdmSourceOutputStream();
335
		assertNotNull(os);
336
	}
337

    
338
}
(2-2/6)