public void testPersistence() { // get new database instance Database myDatabase = Database.getInstance(); // initialize table schema (empty schema) HashMap<String, Type> tableSchema = new HashMap<String, Type>(); tableSchema.put("attribute1", Types.getCharType(5)); tableSchema.put("attribute2", Types.getDateType()); // start DB (should be empty before this stage) myDatabase.startSystem(); // create 1 new table try { myDatabase.getStorageInterface().createTable(tName, tableSchema); } catch (TableAlreadyExistsException e) { fail("Table creation failed"); } // shut down db (persist) myDatabase.shutdownSystem(); // could completely shutdown application at this point (will be tested) // start system again myDatabase.startSystem(); // get all tables stored Operator<Table> tablesOp = (Operator<Table>) myDatabase.getStorageInterface().getTables(); tablesOp.open(); Table t; int i = 0; for (i = 0; (t = tablesOp.next()) != null; i++) { assertEquals(tName, t.getTableName()); // one table inserted assertEquals(i, 0); } tablesOp.close(); if (i != 1) { fail("Incorrect number of tables."); } }
/** Test deletion of tables */ public void testTableDeletion() { // get new database instance Database myDatabase = Database.getInstance(); // initialize table schema (empty schema) HashMap<String, Type> tableSchema = new HashMap<String, Type>(); tableSchema.put("col1", Types.getCharType(10)); tableSchema.put("col2", Types.getDateType()); // start DB myDatabase.startSystem(); // create 1 new table try { myDatabase.getStorageInterface().createTable(tName, tableSchema); } catch (TableAlreadyExistsException e) { fail("unexpected"); } try { myDatabase.getStorageInterface().getTableByName(tName); } catch (NoSuchTableException e) { fail("unexpected"); } // delete table instance try { myDatabase.getStorageInterface().deleteTable(tName); } catch (NoSuchTableException e) { // TODO Auto-generated catch block fail("Table should have been deleted"); } try { myDatabase.getStorageInterface().deleteTable(tName); fail("Should have received an exception when deleting non-existing table"); } catch (NoSuchTableException ex) { // nice exception! } } }
@Override public Type getColumnType() { return Types.getFloatType(); }