> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Aaron
> Zeckoski
> Sent: Wednesday, November 05, 2008 10:52 PM
> To: Derby Discussion
> Subject: Re: Embedded database which only stores data in memory?
>
> Do you have the code for this that you can share?
>
> On Wed, Nov 5, 2008 at 7:31 PM, Jim Newsham <[EMAIL PROTECTED]>
> wrote:
> >
> >
> > No need for manual deletion or an external script... just clear out the
> > database in your test tear-down code. Here is our strategy (junit 4.x):
> >
> > - Before all test cases (@BeforeClass): Generate a temporary directory
> > randomly; create a database there, to be used by tests.
> > - After each test case (@After): Drop all tables from the database
> > - After all test cases (@AfterClass): Delete the temporary directory
> > recursively.
> >
> > Jim
private static File derbyDir;
private static DataSource dataSource;
private static DataSource shutdownDataSource;
@BeforeClass
public static void createDatabase() throws Exception {
// create a temp dir for holding the database
derbyDir = makeTempDir();
File dbDir = new File(derbyDir, "db");
// create database and initialize it
dataSource = getCreateDataSource(dbDir);
initDatabaseStructureOrWhatever();
// create a data source for shutting down later
shutdownDataSource = getShutdownDataSource(dbDir);
}
/**
* Shuts down and deletes the temporary database after all tests have
* completed.
*/
@AfterClass
public static void deleteDatabase() throws Exception {
try {
shutdownDataSource.getConnection();
}
catch(SQLException sqle) {
// successful shutdown throws an exception
}
System.gc();
assertTrue("failed to delete temp dir", deleteRecursively(derbyDir));
derbyDir = null;
}
@Before
public void initDatabase() throws Exception {
//...do standard per-test database setup
//...this might involve creating tables, indexes, or whatever
}
@After
public void cleanupDatabase() throws Exception {
//...do standard per-test database cleanup
}
@After
public void resetDatabase() throws Exception {
//... reset the database to a clean state for the next test
//... this may involve dropping tables, indexes, or whatever
}
/**
* Gets a data source for the derby database at the given path, and which
* creates the database if it doesn't already exist. The data source will
* connect to a local derby database using embedded mode.
* @param path the path to the directory containing the database
* @return a data source
*/
private static DataSource getCreateDataSource(File path) {
if (path == null) {
throw new IllegalArgumentException("path is null");
}
EmbeddedDataSource dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName(path.getPath());
dataSource.setCreateDatabase("create");
return dataSource;
}
/**
* Gets a data source for the derby database at the given path, and which
* shuts down the database when it is connected to. The data source will
* connect to a local derby database using embedded mode.
* @param path the path to the directory containing the database
* @return a data source
*/
private static DataSource getShutdownDataSource(File path) {
if (path == null) {
throw new IllegalArgumentException("path is null");
}
EmbeddedDataSource dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName(path.getPath());
dataSource.setShutdownDatabase("shutdown");
return dataSource;
}
private static final String RANDOM_FILENAME_CHARS =
"abcdefghijklmnopqrstuvwxyz";
private static final int DEFAULT_RANDOM_FILENAME_LENGTH = 12;
/**
* Gets the system temporary directory.
* @return the system temporary directory
*/
private static File getSystemTempDir() {
return new File(System.getProperty("java.io.tmpdir"));
}
/**
* Creates and returns a temporary directory under the system temporary
* directory. The directory will be deleted on system exit.
* @return a newly created temporary directory which will be deleted on
* system exit
*/
private static File makeTempDir() {
File sysDir = getSystemTempDir();
File tempDir = null;
while (tempDir == null || tempDir.exists()) {
tempDir = new File(sysDir, generateRandomFilename());
}
tempDir.mkdir();
tempDir.deleteOnExit();
return tempDir;
}
/**
* Deletes the given file or directory, and all of its contained files and
* directories. If deletion does not complete successfully, some files
may
* have been deleted.
* @param file the file or directory to delete
* @return whether the file and its children were deleted successfully
*/
private static boolean deleteRecursively(File file) {
File[] files = file.listFiles();
if (files != null) {
for (File f : files) {
if (!deleteRecursively(f)) {
return false;
}
}
}
return file.delete();
}
/**
* Generates a random string suitable for use as a filename. Because the
* string is randomly generated, it is unlikely though possible that a
* matching file exists.
* @return a random string suitable for use as a filename
*/
private static String generateRandomFilename() {
return generateRandomFilename(DEFAULT_RANDOM_FILENAME_LENGTH);
}
/**
* Generates a random string suitable for use as a filename. Because the
* string is randomly generated, it is unlikely though possible that a
* matching file exists.
* @param length the filename length, in characters; must be positive
* @return a random string suitable for use as a filename
*/
private static String generateRandomFilename(int length) {
if (length < 1) {
throw new IllegalArgumentException("length is not positive");
}
Random rand = new Random();
char[] filename = new char[length];
for (int i = 0; i < filename.length; i++) {
int index = (rand.nextInt() & Integer.MAX_VALUE) %
RANDOM_FILENAME_CHARS.length();
filename[i] = RANDOM_FILENAME_CHARS.charAt(index);
}
return new String(filename);
}