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."); } }
/* * (non-Javadoc) * * @see junit.framework.TestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); // DB is a singleton, remove all tables between runs Database myDatabase = Database.getInstance(); myDatabase.startSystem(); Operator<Table> op = (Operator<Table>) myDatabase.getStorageInterface().getTables(); op.open(); Table t; while ((t = op.next()) != null) { myDatabase.getStorageInterface().deleteTable(t.getTableName()); } myDatabase.shutdownSystem(); // After test is done, remove all persisted tables for next tests File directory = new File("disk/"); // Get all files in directory File[] files = directory.listFiles(); for (File file : files) { System.out.println(file.getName()); if (!file.delete()) { System.out.println("Failed to delete " + file); } } }