Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2013-03-03 14:56:30 -0500
committerEike Stepper2013-03-07 08:44:12 -0500
commitfab9f3b32fccae1c8be5f34776d2fd0bf18f7c3c (patch)
tree672024d95e6affb307087755ca5f32ecf282629c
parent2174001afb162668a7ccaca8aaae293ff7f9eb24 (diff)
downloadcdo-fab9f3b32fccae1c8be5f34776d2fd0bf18f7c3c.tar.gz
cdo-fab9f3b32fccae1c8be5f34776d2fd0bf18f7c3c.tar.xz
cdo-fab9f3b32fccae1c8be5f34776d2fd0bf18f7c3c.zip
[401763] Make CDO Server more robust against data dictionary changes
https://bugs.eclipse.org/bugs/show_bug.cgi?id=401763
-rw-r--r--plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/AbstractDBTest.java102
-rw-r--r--plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/DerbyTest.java2
-rw-r--r--plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/H2Test.java11
-rw-r--r--plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/HsqldbTest.java2
-rw-r--r--plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java37
5 files changed, 113 insertions, 41 deletions
diff --git a/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/AbstractDBTest.java b/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/AbstractDBTest.java
index d37695fb3b..51fe109ef9 100644
--- a/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/AbstractDBTest.java
+++ b/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/AbstractDBTest.java
@@ -32,19 +32,21 @@ import java.util.Set;
*/
public abstract class AbstractDBTest extends AbstractOMTest
{
- private IDBAdapter dbAdapter;
+ public static final String SCHEMA_NAME = "test";
- private IDBConnectionProvider dbConnectionProvider;
+ private IDBAdapter adapter;
+
+ private IDBConnectionProvider connectionProvider;
private Connection internalConnection;
@Override
protected void doSetUp() throws Exception
{
- dbAdapter = createDBAdapter();
+ adapter = createAdapter();
DataSource dataSource = createDataSource();
- dbConnectionProvider = DBUtil.createConnectionProvider(dataSource);
+ connectionProvider = DBUtil.createConnectionProvider(dataSource);
}
@Override
@@ -57,11 +59,11 @@ public abstract class AbstractDBTest extends AbstractOMTest
internalConnection = null;
}
- dbConnectionProvider = null;
- dbAdapter = null;
+ connectionProvider = null;
+ adapter = null;
}
- protected abstract IDBAdapter createDBAdapter();
+ protected abstract IDBAdapter createAdapter();
protected abstract DataSource createDataSource();
@@ -69,7 +71,7 @@ public abstract class AbstractDBTest extends AbstractOMTest
{
if (internalConnection == null)
{
- internalConnection = dbConnectionProvider.getConnection();
+ internalConnection = connectionProvider.getConnection();
}
return internalConnection;
@@ -88,7 +90,7 @@ public abstract class AbstractDBTest extends AbstractOMTest
table.addField("field", dbType);
++count;
- if (dbAdapter.isTypeIndexable(dbType))
+ if (adapter.isTypeIndexable(dbType))
{
IDBTable idx_table = schema.addTable("idx_table" + i);
IDBField idx_field = idx_table.addField("field", dbType);
@@ -109,19 +111,19 @@ public abstract class AbstractDBTest extends AbstractOMTest
++i;
}
- Set<IDBTable> tables = schema.create(dbAdapter, getConnection());
+ Set<IDBTable> tables = schema.create(adapter, getConnection());
assertEquals(count, tables.size());
}
public void testSchemaEmpty() throws Exception
{
- IDBSchema schema = DBUtil.readSchema("test", getConnection());
+ IDBSchema schema = DBUtil.readSchema(SCHEMA_NAME, getConnection());
assertEquals(true, schema.isEmpty());
}
- public void testSchemaAddition() throws Exception
+ public void testSchemaCreation() throws Exception
{
- IDBDatabase database = DBUtil.createDatabase(dbAdapter, dbConnectionProvider, "test");
+ IDBDatabase database = DBUtil.openDatabase(adapter, connectionProvider, SCHEMA_NAME);
assertEquals(true, database.getSchema().isLocked());
assertEquals(true, database.getSchema().isEmpty());
@@ -177,4 +179,78 @@ public abstract class AbstractDBTest extends AbstractOMTest
assertEquals(index23.getName(), database.getSchema().getTables()[1].getIndex(2).getName());
assertEquals(index23.getType(), database.getSchema().getTables()[1].getIndex(2).getType());
}
+
+ public void testSchemaAddition() throws Exception
+ {
+ IDBDatabase database = DBUtil.openDatabase(adapter, connectionProvider, SCHEMA_NAME);
+ IDBSchemaTransaction schemaTransaction = database.openSchemaTransaction();
+
+ IDBTable table1 = schemaTransaction.getSchema().addTable("table1");
+ IDBField field11 = table1.addField("field1", DBType.INTEGER);
+ IDBField field12 = table1.addField("field2", DBType.VARCHAR, 64);
+ IDBField field13 = table1.addField("field3", DBType.BOOLEAN);
+ IDBIndex index11 = table1.addIndex("index1", IDBIndex.Type.PRIMARY_KEY, field11, field12);
+ IDBIndex index12 = table1.addIndex("index2", IDBIndex.Type.UNIQUE, field11, field12);
+ IDBIndex index13 = table1.addIndex("index3", IDBIndex.Type.NON_UNIQUE, field12);
+
+ schemaTransaction.commit();
+ assertEquals(1, database.getSchema().getTables().length);
+
+ database = DBUtil.openDatabase(adapter, connectionProvider, SCHEMA_NAME);
+ assertEquals(true, database.getSchema().isLocked());
+ assertEquals(false, database.getSchema().isEmpty());
+ assertEquals(1, database.getSchema().getTables().length);
+
+ IDBTable table = database.getSchema().getTable("table1");
+ field11 = table.getField("field1");
+ field12 = table.getField("field2");
+ field13 = table.getField("field3");
+ index11 = table.getIndex("index1");
+ index12 = table.getIndex("index2");
+ index13 = table.getIndex("index3");
+
+ schemaTransaction = database.openSchemaTransaction();
+ assertEquals(false, database.getSchema().isLocked());
+ assertEquals(false, database.getSchema().isEmpty());
+ assertEquals(1, database.getSchema().getTables().length);
+
+ IDBTable table2 = schemaTransaction.getSchema().addTable("table2");
+ IDBField field21 = table2.addField("field1", DBType.INTEGER);
+ IDBField field22 = table2.addField("field2", DBType.VARCHAR, 64);
+ IDBField field23 = table2.addField("field3", DBType.BOOLEAN);
+ IDBIndex index21 = table2.addIndex("index1", IDBIndex.Type.PRIMARY_KEY, field21, field22);
+ IDBIndex index22 = table2.addIndex("index2", IDBIndex.Type.UNIQUE, field21, field22);
+ IDBIndex index23 = table2.addIndex("index3", IDBIndex.Type.NON_UNIQUE, field22);
+
+ schemaTransaction.commit();
+ assertEquals(true, database.getSchema().isLocked());
+ assertEquals(false, database.getSchema().isEmpty());
+ assertEquals(2, database.getSchema().getTables().length);
+
+ assertEquals(table1.getName(), database.getSchema().getTables()[0].getName());
+ assertEquals(table1.getFieldCount(), database.getSchema().getTables()[0].getFieldCount());
+ assertEquals(field11.getName(), database.getSchema().getTables()[0].getField(0).getName());
+ assertEquals(field12.getName(), database.getSchema().getTables()[0].getField(1).getName());
+ assertEquals(field13.getName(), database.getSchema().getTables()[0].getField(2).getName());
+ assertEquals(table1.getIndexCount(), database.getSchema().getTables()[0].getIndexCount());
+ assertEquals(index11.getName(), database.getSchema().getTables()[0].getIndex(0).getName());
+ assertEquals(index11.getType(), database.getSchema().getTables()[0].getIndex(0).getType());
+ assertEquals(index12.getName(), database.getSchema().getTables()[0].getIndex(1).getName());
+ assertEquals(index12.getType(), database.getSchema().getTables()[0].getIndex(1).getType());
+ assertEquals(index13.getName(), database.getSchema().getTables()[0].getIndex(2).getName());
+ assertEquals(index13.getType(), database.getSchema().getTables()[0].getIndex(2).getType());
+
+ assertEquals(table2.getName(), database.getSchema().getTables()[1].getName());
+ assertEquals(table2.getFieldCount(), database.getSchema().getTables()[1].getFieldCount());
+ assertEquals(field21.getName(), database.getSchema().getTables()[1].getField(0).getName());
+ assertEquals(field22.getName(), database.getSchema().getTables()[1].getField(1).getName());
+ assertEquals(field23.getName(), database.getSchema().getTables()[1].getField(2).getName());
+ assertEquals(table2.getIndexCount(), database.getSchema().getTables()[1].getIndexCount());
+ assertEquals(index21.getName(), database.getSchema().getTables()[1].getIndex(0).getName());
+ assertEquals(index21.getType(), database.getSchema().getTables()[1].getIndex(0).getType());
+ assertEquals(index22.getName(), database.getSchema().getTables()[1].getIndex(1).getName());
+ assertEquals(index22.getType(), database.getSchema().getTables()[1].getIndex(1).getType());
+ assertEquals(index23.getName(), database.getSchema().getTables()[1].getIndex(2).getName());
+ assertEquals(index23.getType(), database.getSchema().getTables()[1].getIndex(2).getType());
+ }
}
diff --git a/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/DerbyTest.java b/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/DerbyTest.java
index 689f10fe20..f086b82803 100644
--- a/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/DerbyTest.java
+++ b/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/DerbyTest.java
@@ -27,7 +27,7 @@ public class DerbyTest extends AbstractDBTest
private File dbFolder;
@Override
- protected IDBAdapter createDBAdapter()
+ protected IDBAdapter createAdapter()
{
return new org.eclipse.net4j.db.derby.EmbeddedDerbyAdapter();
}
diff --git a/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/H2Test.java b/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/H2Test.java
index e6b2fbce69..d5371a8991 100644
--- a/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/H2Test.java
+++ b/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/H2Test.java
@@ -10,6 +10,7 @@
*/
package org.eclipse.net4j.db.tests;
+import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.db.IDBAdapter;
import org.eclipse.net4j.util.io.IOUtil;
@@ -27,7 +28,7 @@ public class H2Test extends AbstractDBTest
private File dbFolder;
@Override
- protected IDBAdapter createDBAdapter()
+ protected IDBAdapter createAdapter()
{
return new org.eclipse.net4j.db.h2.H2Adapter();
}
@@ -39,8 +40,14 @@ public class H2Test extends AbstractDBTest
deleteDBFolder();
msg("Using DB folder: " + dbFolder.getAbsolutePath());
+ String url = "jdbc:h2:" + dbFolder.getAbsolutePath() + "/h2test";
+
JdbcDataSource dataSource = new JdbcDataSource();
- dataSource.setURL("jdbc:h2:" + dbFolder.getAbsolutePath() + "/h2test");
+ dataSource.setURL(url);
+ DBUtil.createSchema(dataSource, SCHEMA_NAME, true);
+
+ dataSource = new JdbcDataSource();
+ dataSource.setURL(url + ";SCHEMA=" + SCHEMA_NAME);
return dataSource;
}
diff --git a/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/HsqldbTest.java b/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/HsqldbTest.java
index 6442d3fa82..2650fa394e 100644
--- a/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/HsqldbTest.java
+++ b/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/HsqldbTest.java
@@ -21,7 +21,7 @@ import javax.sql.DataSource;
public class HsqldbTest extends AbstractDBTest
{
@Override
- protected IDBAdapter createDBAdapter()
+ protected IDBAdapter createAdapter()
{
return new org.eclipse.net4j.db.hsqldb.HSQLDBAdapter();
}
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java
index e23695ba82..d8b77aa4e9 100644
--- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java
+++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java
@@ -13,8 +13,8 @@ package org.eclipse.net4j.db;
import org.eclipse.net4j.db.ddl.IDBField;
import org.eclipse.net4j.db.ddl.IDBSchema;
import org.eclipse.net4j.db.ddl.IDBTable;
-import org.eclipse.net4j.internal.db.DBTransaction;
import org.eclipse.net4j.internal.db.DBDatabase;
+import org.eclipse.net4j.internal.db.DBTransaction;
import org.eclipse.net4j.internal.db.DataSourceConnectionProvider;
import org.eclipse.net4j.internal.db.bundle.OM;
import org.eclipse.net4j.spi.db.DBAdapter;
@@ -141,7 +141,7 @@ public final class DBUtil
/**
* @since 4.2
*/
- public static IDBDatabase createDatabase(IDBAdapter dbAdapter, IDBConnectionProvider dbConnectionProvider,
+ public static IDBDatabase openDatabase(IDBAdapter dbAdapter, IDBConnectionProvider dbConnectionProvider,
String schemaName)
{
return new DBDatabase((DBAdapter)dbAdapter, dbConnectionProvider, schemaName);
@@ -402,32 +402,21 @@ public final class DBUtil
/**
* @since 4.2
*/
- public static void createSchema(DataSource dataSource, String name, boolean dropIfExists)
+ public static void createSchema(DataSource dataSource, final String name, final boolean dropIfExists)
{
- Connection conn = null;
- Statement stmt = null;
-
- try
+ execute(createConnectionProvider(dataSource), new RunnableWithConnection<Object>()
{
- conn = dataSource.getConnection();
- stmt = conn.createStatement();
-
- if (dropIfExists)
+ public Object run(Connection connection) throws SQLException
{
- stmt.execute("DROP SCHEMA IF EXISTS " + name);
- }
+ if (dropIfExists)
+ {
+ execute(connection, "DROP SCHEMA IF EXISTS " + name);
+ }
- stmt.execute("CREATE SCHEMA IF NOT EXISTS " + name);
- }
- catch (SQLException ex)
- {
- throw new DBException(ex);
- }
- finally
- {
- close(stmt);
- close(conn);
- }
+ execute(connection, "CREATE SCHEMA IF NOT EXISTS " + name);
+ return null;
+ }
+ });
}
/**

Back to the top