00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package com.sleepycat.collections.test;
00011
00012 import java.io.BufferedInputStream;
00013 import java.io.BufferedOutputStream;
00014 import java.io.File;
00015 import java.io.FileOutputStream;
00016 import java.io.IOException;
00017 import java.io.InputStream;
00018 import java.io.OutputStream;
00019
00020 import junit.framework.TestCase;
00021
00022 import com.sleepycat.db.DatabaseConfig;
00023
00027 public class DbTestUtil {
00028
00029 public static final DatabaseConfig DBCONFIG_CREATE = new DatabaseConfig();
00030 static {
00031 DBCONFIG_CREATE.setAllowCreate(true);
00032 }
00033
00034 private static final String separator = ":::";
00035
00036 private static final File TEST_DIR;
00037 static {
00038 String dir = System.getProperty("testdestdir");
00039 if (dir == null || dir.length() == 0) {
00040 dir = ".";
00041 }
00042 TEST_DIR = new File(dir, "tmp");
00043 }
00044
00045 public static void printTestName(String name) {
00046
00047
00048 }
00049
00050 public static File getExistingDir(String name)
00051 throws IOException {
00052
00053 File dir = new File(TEST_DIR, name);
00054 if (!dir.exists() || !dir.isDirectory()) {
00055 throw new IllegalStateException(
00056 "Not an existing directory: " + dir);
00057 }
00058 return dir;
00059 }
00060
00061 public static File getNewDir()
00062 throws IOException {
00063
00064 return getNewDir("test-dir");
00065 }
00066
00067 public static File getNewDir(String name)
00068 throws IOException {
00069
00070 File dir = new File(TEST_DIR, name);
00071 if (dir.isDirectory()) {
00072 String[] files = dir.list();
00073 if (files != null) {
00074 for (int i = 0; i < files.length; i += 1) {
00075 new File(dir, files[i]).delete();
00076 }
00077 }
00078 } else {
00079 dir.delete();
00080 dir.mkdirs();
00081 }
00082 return dir;
00083 }
00084
00085 public static File getNewFile()
00086 throws IOException {
00087
00088 return getNewFile("test-file");
00089 }
00090
00091 public static File getNewFile(String name)
00092 throws IOException {
00093
00094 return getNewFile(TEST_DIR, name);
00095 }
00096
00097 public static File getNewFile(File dir, String name)
00098 throws IOException {
00099
00100 File file = new File(dir, name);
00101 file.delete();
00102 return file;
00103 }
00104
00105 public static boolean copyResource(Class cls, String fileName, File toDir)
00106 throws IOException {
00107
00108 InputStream in = cls.getResourceAsStream("testdata/" + fileName);
00109 if (in == null) {
00110 return false;
00111 }
00112 in = new BufferedInputStream(in);
00113 File file = new File(toDir, fileName);
00114 OutputStream out = new FileOutputStream(file);
00115 out = new BufferedOutputStream(out);
00116 int c;
00117 while ((c = in.read()) >= 0) out.write(c);
00118 in.close();
00119 out.close();
00120 return true;
00121 }
00122
00123 public static String qualifiedTestName(TestCase test) {
00124
00125 String s = test.getClass().getName();
00126 int i = s.lastIndexOf('.');
00127 if (i >= 0) {
00128 s = s.substring(i + 1);
00129 }
00130 return s + '.' + test.getName();
00131 }
00132 }