KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > collections > test > DbTestUtil


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DbTestUtil.java,v 1.25 2006/10/30 21:14:38 bostic Exp $
7  */

8
9 package com.sleepycat.collections.test;
10
11 import java.io.BufferedInputStream JavaDoc;
12 import java.io.BufferedOutputStream JavaDoc;
13 import java.io.File JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.io.OutputStream JavaDoc;
18
19 import junit.framework.TestCase;
20
21 import com.sleepycat.je.DatabaseConfig;
22
23 /**
24  * @author Mark Hayes
25  */

26 public class DbTestUtil {
27
28     public static final DatabaseConfig DBCONFIG_CREATE = new DatabaseConfig();
29     static {
30         DBCONFIG_CREATE.setAllowCreate(true);
31     }
32
33     private static final File JavaDoc TEST_DIR;
34     static {
35         String JavaDoc dir = System.getProperty("testdestdir");
36         if (dir == null || dir.length() == 0) {
37             dir = ".";
38         }
39         TEST_DIR = new File JavaDoc(dir, "tmp");
40     }
41
42     public static void printTestName(String JavaDoc name) {
43         // don't want verbose printing for now
44
// System.out.println(name);
45
}
46
47     public static File JavaDoc getExistingDir(String JavaDoc name)
48         throws IOException JavaDoc {
49
50         File JavaDoc dir = new File JavaDoc(TEST_DIR, name);
51         if (!dir.exists() || !dir.isDirectory()) {
52             throw new IllegalStateException JavaDoc(
53                     "Not an existing directory: " + dir);
54         }
55         return dir;
56     }
57
58     public static File JavaDoc getNewDir()
59         throws IOException JavaDoc {
60
61         return getNewDir("test-dir");
62     }
63
64     public static File JavaDoc getNewDir(String JavaDoc name)
65         throws IOException JavaDoc {
66
67         File JavaDoc dir = new File JavaDoc(TEST_DIR, name);
68         if (dir.isDirectory()) {
69             String JavaDoc[] files = dir.list();
70             if (files != null) {
71                 for (int i = 0; i < files.length; i += 1) {
72                     new File JavaDoc(dir, files[i]).delete();
73                 }
74             }
75         } else {
76             dir.delete();
77             dir.mkdirs();
78         }
79         return dir;
80     }
81
82     public static File JavaDoc getNewFile()
83         throws IOException JavaDoc {
84
85         return getNewFile("test-file");
86     }
87
88     public static File JavaDoc getNewFile(String JavaDoc name)
89         throws IOException JavaDoc {
90
91         return getNewFile(TEST_DIR, name);
92     }
93
94     public static File JavaDoc getNewFile(File JavaDoc dir, String JavaDoc name)
95         throws IOException JavaDoc {
96
97         File JavaDoc file = new File JavaDoc(dir, name);
98         file.delete();
99         return file;
100     }
101
102     public static boolean copyResource(Class JavaDoc cls, String JavaDoc fileName, File JavaDoc toDir)
103         throws IOException JavaDoc {
104
105         InputStream JavaDoc in = cls.getResourceAsStream("testdata/" + fileName);
106         if (in == null) {
107             return false;
108         }
109         in = new BufferedInputStream JavaDoc(in);
110         File JavaDoc file = new File JavaDoc(toDir, fileName);
111         OutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
112         out = new BufferedOutputStream JavaDoc(out);
113         int c;
114         while ((c = in.read()) >= 0) out.write(c);
115         in.close();
116         out.close();
117         return true;
118     }
119
120     public static String JavaDoc qualifiedTestName(TestCase test) {
121
122         String JavaDoc s = test.getClass().getName();
123         int i = s.lastIndexOf('.');
124         if (i >= 0) {
125             s = s.substring(i + 1);
126         }
127         return s + '.' + test.getName();
128     }
129 }
130
Popular Tags