Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3760e79739ff4542cd7966dfd5fcb1e559034568 (plain) (blame)
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
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * 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();
    configureDataSourcer(dataSource);
    dbConnectionProvider = DBUtil.createConnectionProvider(dataSource);
  }

  @Override
  protected void doTearDown() throws Exception
  {
  }

  protected abstract IDBAdapter createDBAdapter();

  protected abstract void configureDataSourcer(DATA_SOURCE dataSource);

  protected Connection getConnection()
  {
    return dbConnectionProvider.getConnection();
  }

  public void _testDBTypes() throws Exception
  {
    IDBSchema schema = DBUtil.createSchema("testDBTypes");
    DBType[] dbTypes = DBType.values();

    int count = 0;
    int i = 0;
    for (DBType dbType : dbTypes)
    {
      IDBTable table = schema.addTable("table_" + i);
      table.addField("field", dbType);
      ++count;

      if (dbAdapter.isTypeIndexable(dbType))
      {
        IDBTable idx_table = schema.addTable("idx_table" + i);
        IDBField idx_field = idx_table.addField("field", dbType);
        idx_table.addIndex(IDBIndex.Type.NON_UNIQUE, idx_field);
        ++count;

        IDBTable uni_table = schema.addTable("uni_table" + i);
        IDBField uni_field = uni_table.addField("field", dbType);
        uni_table.addIndex(IDBIndex.Type.UNIQUE, uni_field);
        ++count;

        IDBTable pk_table = schema.addTable("pk_table" + i);
        IDBField pk_field = pk_table.addField("field", dbType);
        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");
    IDBTable table = schema.addTable("testtable");
    IDBField strval = table.addField("strval", DBType.VARCHAR, 255);
    schema.create(dbAdapter, dbConnectionProvider);

    // StringBuilder builder = new StringBuilder("insert into testtable values(");
    // dbAdapter.appendValue(builder, strval, "My name is \"nobody\", not body");
    // builder.append(")");
    String val = "My name is 'nobody', not body";
    DBUtil.insertRow(getConnection(), dbAdapter, table, val);
    Object[] result = DBUtil.select(getConnection(), (String)null, strval);
    assertEquals(val, result[0]);
  }
}

Back to the top