1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: SharedTestUtils.java,v 12.1 2008/02/07 17:12:33 mark Exp $
7 */
8
9package com.sleepycat.util.test;
10
11import java.io.BufferedInputStream;
12import java.io.BufferedOutputStream;
13import java.io.File;
14import java.io.FileInputStream;
15import java.io.FileOutputStream;
16import java.io.IOException;
17import java.io.InputStream;
18import java.io.OutputStream;
19
20import junit.framework.TestCase;
21
22import com.sleepycat.db.DatabaseConfig;
23
24/**
25 * Test utility methods shared by JE and DB core tests.  Collections and
26 * persist package test are used in both JE and DB core.
27 */
28public class SharedTestUtils {
29
30    /* Common system properties for running tests */
31    public static String DEST_DIR = "testdestdir";
32    public static String NO_SYNC = "txnnosync";
33    public static String LONG_TEST =  "longtest";
34
35    public static final DatabaseConfig DBCONFIG_CREATE = new DatabaseConfig();
36    static {
37        DBCONFIG_CREATE.setAllowCreate(true);
38    }
39
40    private static File getTestDir() {
41        String dir = System.getProperty(DEST_DIR);
42        if (dir == null || dir.length() == 0) {
43            throw new IllegalArgumentException
44                ("System property must be set to test data directory: " +
45                 DEST_DIR);
46        }
47        return new File(dir);
48    }
49
50    /**
51     * @return true if long running tests are enabled via setting the system
52     * property longtest=true.
53     */
54    public static boolean runLongTests() {
55        String longTestProp =  System.getProperty(LONG_TEST);
56        if ((longTestProp != null)  &&
57            longTestProp.equalsIgnoreCase("true")) {
58            return true;
59        } else {
60            return false;
61        }
62    }
63
64    public static void printTestName(String name) {
65        // don't want verbose printing for now
66        // System.out.println(name);
67    }
68
69    public static File getExistingDir(String name)
70        throws IOException {
71
72        File dir = new File(getTestDir(), name);
73        if (!dir.exists() || !dir.isDirectory()) {
74            throw new IllegalStateException(
75                    "Not an existing directory: " + dir);
76        }
77        return dir;
78    }
79
80    public static File getNewDir()
81        throws IOException {
82
83        return getNewDir("test-dir");
84    }
85
86    public static void emptyDir(File dir)
87        throws IOException {
88
89        if (dir.isDirectory()) {
90            String[] files = dir.list();
91            if (files != null) {
92                for (int i = 0; i < files.length; i += 1) {
93                    new File(dir, files[i]).delete();
94                }
95            }
96        } else {
97            dir.delete();
98            dir.mkdirs();
99        }
100    }
101
102    public static File getNewDir(String name)
103        throws IOException {
104
105        File dir = new File(getTestDir(), name);
106        emptyDir(dir);
107        return dir;
108    }
109
110    public static File getNewFile()
111        throws IOException {
112
113        return getNewFile("test-file");
114    }
115
116    public static File getNewFile(String name)
117        throws IOException {
118
119        return getNewFile(getTestDir(), name);
120    }
121
122    public static File getNewFile(File dir, String name)
123        throws IOException {
124
125        File file = new File(dir, name);
126        file.delete();
127        return file;
128    }
129
130    public static boolean copyResource(Class cls, String fileName, File toDir)
131        throws IOException {
132
133        InputStream in = cls.getResourceAsStream("testdata/" + fileName);
134        if (in == null) {
135            return false;
136        }
137        in = new BufferedInputStream(in);
138        File file = new File(toDir, fileName);
139        OutputStream out = new FileOutputStream(file);
140        out = new BufferedOutputStream(out);
141        int c;
142        while ((c = in.read()) >= 0) out.write(c);
143        in.close();
144        out.close();
145        return true;
146    }
147
148    public static String qualifiedTestName(TestCase test) {
149
150        String s = test.getClass().getName();
151        int i = s.lastIndexOf('.');
152        if (i >= 0) {
153            s = s.substring(i + 1);
154        }
155        return s + '.' + test.getName();
156    }
157
158    /**
159     * Copies all files in fromDir to toDir.  Does not copy subdirectories.
160     */
161    public static void copyFiles(File fromDir, File toDir)
162        throws IOException {
163
164        String[] names = fromDir.list();
165        if (names != null) {
166            for (int i = 0; i < names.length; i += 1) {
167                File fromFile = new File(fromDir, names[i]);
168                if (fromFile.isDirectory()) {
169                    continue;
170                }
171                File toFile = new File(toDir, names[i]);
172                int len = (int) fromFile.length();
173                byte[] data = new byte[len];
174                FileInputStream fis = null;
175                FileOutputStream fos = null;
176                try {
177                    fis = new FileInputStream(fromFile);
178                    fos = new FileOutputStream(toFile);
179                    fis.read(data);
180                    fos.write(data);
181                } finally {
182                    if (fis != null) {
183                        fis.close();
184                    }
185                    if (fos != null) {
186                        fos.close();
187                    }
188                }
189            }
190        }
191    }
192}
193