1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/*
* Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eike Stepper - initial API and implementation
*/
package org.eclipse.net4j.db.tests;
import org.eclipse.net4j.db.DBType;
import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.db.IDBAdapter;
import org.eclipse.net4j.db.IDBConnectionProvider;
import org.eclipse.net4j.db.ddl.IDBField;
import org.eclipse.net4j.db.ddl.IDBIndex;
import org.eclipse.net4j.db.ddl.IDBSchema;
import org.eclipse.net4j.db.ddl.IDBTable;
import org.eclipse.net4j.util.tests.AbstractOMTest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.util.Set;
/**
* @author Eike Stepper
*/
public abstract class AbstractDBTest<DATA_SOURCE extends DataSource> extends AbstractOMTest
{
protected IDBAdapter dbAdapter;
protected IDBConnectionProvider dbConnectionProvider;
@SuppressWarnings("unchecked")
@Override
protected void doSetUp() throws Exception
{
dbAdapter = createDBAdapter();
DATA_SOURCE dataSource = (DATA_SOURCE)dbAdapter.createJDBCDataSource();
configureDataSource(dataSource);
dbConnectionProvider = DBUtil.createConnectionProvider(dataSource);
}
@Override
protected void doTearDown() throws Exception
{
}
protected abstract IDBAdapter createDBAdapter();
protected abstract void configureDataSource(DATA_SOURCE dataSource);
protected Connection getConnection()
{
return dbConnectionProvider.getConnection();
}
public void testDBTypes() throws Exception
{
IDBSchema schema = DBUtil.createSchema("testDBTypes"); //$NON-NLS-1$
DBType[] dbTypes = DBType.values();
int count = 0;
int i = 0;
for (DBType dbType : dbTypes)
{
IDBTable table = schema.addTable("table_" + i); //$NON-NLS-1$
table.addField("field", dbType); //$NON-NLS-1$
++count;
if (dbAdapter.isTypeIndexable(dbType))
{
IDBTable idx_table = schema.addTable("idx_table" + i); //$NON-NLS-1$
IDBField idx_field = idx_table.addField("field", dbType); //$NON-NLS-1$
idx_table.addIndex(IDBIndex.Type.NON_UNIQUE, idx_field);
++count;
IDBTable uni_table = schema.addTable("uni_table" + i); //$NON-NLS-1$
IDBField uni_field = uni_table.addField("field", dbType); //$NON-NLS-1$
uni_table.addIndex(IDBIndex.Type.UNIQUE, uni_field);
++count;
IDBTable pk_table = schema.addTable("pk_table" + i); //$NON-NLS-1$
IDBField pk_field = pk_table.addField("field", dbType); //$NON-NLS-1$
pk_table.addIndex(IDBIndex.Type.PRIMARY_KEY, pk_field);
++count;
}
++i;
}
Set<IDBTable> tables = schema.create(dbAdapter, dbConnectionProvider);
assertEquals(count, tables.size());
}
// public void testEscapeStrings() throws Exception
// {
// IDBSchema schema = DBUtil.createSchema("testEscapeStrings"); //$NON-NLS-1$
// IDBTable table = schema.addTable("testtable"); //$NON-NLS-1$
// IDBField field = table.addField("strval", DBType.VARCHAR, 255); //$NON-NLS-1$
// schema.create(dbAdapter, dbConnectionProvider);
//
// insertString(field, "My name is 'nobody', not body"); //$NON-NLS-1$
// insertString(field, "a = 'hello'"); //$NON-NLS-1$
// insertString(field, "'hello' == a"); //$NON-NLS-1$
// insertString(field, "'hello'"); //$NON-NLS-1$
// }
//
// private void insertString(IDBField field, String val)
// {
// Connection connection = getConnection();
// IDBTable table = field.getTable();
//
// try
// {
// DBUtil.insertRow(connection, dbAdapter, table, val);
// Object[] result = DBUtil.select(connection, (String)null, field);
// assertEquals(val, result[0]);
// }
// finally
// {
// DBUtil.update(connection, "DELETE FROM " + table); //$NON-NLS-1$
// }
// }
}
|