001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: DbTestUtil.java,v 1.25.2.2 2008/01/07 15:14:24 cwl Exp $
007: */
008:
009: package com.sleepycat.collections.test;
010:
011: import java.io.BufferedInputStream;
012: import java.io.BufferedOutputStream;
013: import java.io.File;
014: import java.io.FileOutputStream;
015: import java.io.IOException;
016: import java.io.InputStream;
017: import java.io.OutputStream;
018:
019: import junit.framework.TestCase;
020:
021: import com.sleepycat.je.DatabaseConfig;
022:
023: /**
024: * @author Mark Hayes
025: */
026: public class DbTestUtil {
027:
028: public static final DatabaseConfig DBCONFIG_CREATE = new DatabaseConfig();
029: static {
030: DBCONFIG_CREATE.setAllowCreate(true);
031: }
032:
033: private static final File TEST_DIR;
034: static {
035: String dir = System.getProperty("testdestdir");
036: if (dir == null || dir.length() == 0) {
037: dir = ".";
038: }
039: TEST_DIR = new File(dir, "tmp");
040: }
041:
042: public static void printTestName(String name) {
043: // don't want verbose printing for now
044: // System.out.println(name);
045: }
046:
047: public static File getExistingDir(String name) throws IOException {
048:
049: File dir = new File(TEST_DIR, name);
050: if (!dir.exists() || !dir.isDirectory()) {
051: throw new IllegalStateException(
052: "Not an existing directory: " + dir);
053: }
054: return dir;
055: }
056:
057: public static File getNewDir() throws IOException {
058:
059: return getNewDir("test-dir");
060: }
061:
062: public static File getNewDir(String name) throws IOException {
063:
064: File dir = new File(TEST_DIR, name);
065: if (dir.isDirectory()) {
066: String[] files = dir.list();
067: if (files != null) {
068: for (int i = 0; i < files.length; i += 1) {
069: new File(dir, files[i]).delete();
070: }
071: }
072: } else {
073: dir.delete();
074: dir.mkdirs();
075: }
076: return dir;
077: }
078:
079: public static File getNewFile() throws IOException {
080:
081: return getNewFile("test-file");
082: }
083:
084: public static File getNewFile(String name) throws IOException {
085:
086: return getNewFile(TEST_DIR, name);
087: }
088:
089: public static File getNewFile(File dir, String name)
090: throws IOException {
091:
092: File file = new File(dir, name);
093: file.delete();
094: return file;
095: }
096:
097: public static boolean copyResource(Class cls, String fileName,
098: File toDir) throws IOException {
099:
100: InputStream in = cls
101: .getResourceAsStream("testdata/" + fileName);
102: if (in == null) {
103: return false;
104: }
105: in = new BufferedInputStream(in);
106: File file = new File(toDir, fileName);
107: OutputStream out = new FileOutputStream(file);
108: out = new BufferedOutputStream(out);
109: int c;
110: while ((c = in.read()) >= 0)
111: out.write(c);
112: in.close();
113: out.close();
114: return true;
115: }
116:
117: public static String qualifiedTestName(TestCase test) {
118:
119: String s = test.getClass().getName();
120: int i = s.lastIndexOf('.');
121: if (i >= 0) {
122: s = s.substring(i + 1);
123: }
124: return s + '.' + test.getName();
125: }
126: }
|