2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 2002, 2012 Oracle and/or its affiliates. All rights reserved.
9 * Generally useful functions :)
12 package com.sleepycat.db.test;
14 import static org.junit.Assert.fail;
16 import com.sleepycat.db.*;
18 import java.io.BufferedInputStream;
19 import java.io.BufferedReader;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.FileNotFoundException;
24 import java.io.InputStream;
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.util.Properties;
29 public class TestUtils
31 public static boolean config_loaded = false;
32 public static boolean verbose_flag = false;
33 public static int debug_level = 2;
35 // should be initialized by calling loadEnvVars. Shared between all tests.
36 public static String BASETEST_DBDIR = "";
37 public static File BASETEST_DBFILE = null; // new File(TestUtils.BASETEST_DBDIR);
38 public static String BASETEST_BACKUPDIR = "";
39 public static File BASETEST_BACKUPFILE = null; // new File(TestUtils.BASETEST_BACKUPDIR);
41 public static void ERR(String a)
43 System.err.println("FAIL: " + a);
47 public static void DEBUGOUT(String s)
52 public static void DEBUGOUT(int importance, String s)
54 if(importance > debug_level)
55 System.out.println("DEBUG: " +s);
58 public static void VERBOSEOUT(String s)
61 System.out.println(s);
64 public static void sysexit(int code)
69 public static void check_file_removed(String name, boolean fatal,
70 boolean force_remove_first)
72 File f = new File(name);
73 if (force_remove_first) {
78 System.out.print("FAIL: ");
79 DEBUGOUT(1, "File \"" + name + "\" still exists after check_file_removed\n");
81 fail("File \"" + name + "\" still exists after check_file_removed");
86 // remove any existing environment or database
87 public static void removeall(boolean use_db, boolean remove_env, String envpath, String dbname)
92 Environment.remove(new File(envpath), true, EnvironmentConfig.DEFAULT);
94 Database.remove(dbname, null, DatabaseConfig.DEFAULT);
96 catch (DatabaseException dbe) {
97 DEBUGOUT(1, "TestUtil::removeall exception caught: " + dbe);
99 catch (FileNotFoundException dbe) {
100 DEBUGOUT(1, "TestUtil::removeall exception caught: " + dbe);
103 check_file_removed(dbname, false, !use_db);
105 for (int i=0; i<8; i++) {
106 String fname = envpath + "/" + "__db." + i;
107 check_file_removed(fname, true, !use_db);
110 // ensure the user knows if there is junk remaining.
111 // clean out spurious log.00X files
112 File dir = new File(envpath);
113 if(dir.isDirectory()) {
114 String[] remainingfiles = dir.list();
115 for(int i = 0; i < remainingfiles.length; i++) {
116 if(remainingfiles[i].startsWith("log") || remainingfiles[i].endsWith("db2") ||
117 remainingfiles[i].endsWith("log") || remainingfiles[i].startsWith("__db")) {
118 DEBUGOUT(1, "TestUtils::removeall removing: " +remainingfiles[i]);
119 check_file_removed(envpath + "/" + remainingfiles[i], false, true);
121 if(remainingfiles[i].indexOf("del") == -1)
122 DEBUGOUT(3, "TestUtils::removeall warning, file: " + remainingfiles[i] + " remains in directory after cleanup.");
129 public static boolean removeDir(String dirname)
132 File deldir = new File(dirname);
134 if (!deldir.exists()) {
136 } else if(!deldir.isDirectory()) {
139 // The following will fail if the directory contains sub-dirs.
140 File[] contents = deldir.listFiles();
141 for (int i = 0; i < contents.length; i++)
142 contents[i].delete();
145 } catch (Exception e) {
146 TestUtils.DEBUGOUT(4, "Warning: error encountered removing directory.\n" + e);
151 static public String shownull(Object o)
160 * The config file is not currently required.
161 * The only variable that can be set via the
162 * config file is the base directory for the
163 * tests to be run in. The default is "data"
164 * and will be created for the tests.
166 public static void loadConfig(String envfilename)
171 String configname = envfilename;
172 if(envfilename == null)
174 String OSStr = java.lang.System.getProperty("os.name");
175 if((OSStr.toLowerCase()).indexOf("windows") != -1)
177 configname = "config_win32";
179 // assume a nix variant.
180 configname = "config_nix";
183 config_loaded = true;
185 InputStream in = new FileInputStream(configname);
186 DEBUGOUT(2, "Opened " + configname + " to read configuration.");
187 Properties props = new Properties();
190 String var = props.getProperty("BASETEST_DBDIR");
192 { // Property seems to encase things in "";
193 var = var.substring(1);
194 var = var.substring(0, var.length() -2);
195 BASETEST_DBDIR = var;
197 DEBUGOUT(2, "BASETEST_DBDIR is: " + BASETEST_DBDIR);
199 } catch (Exception e) {
200 // expected - the config file is optional.
201 DEBUGOUT(0, "loadEnvVars -- loading of default variables failed. error: " + e);
203 if (BASETEST_DBDIR == "")
204 BASETEST_DBDIR = "data";
205 BASETEST_DBFILE = new File(BASETEST_DBDIR);
206 if (!BASETEST_DBFILE.exists())
207 BASETEST_DBFILE.mkdirs();
208 BASETEST_BACKUPDIR = BASETEST_DBDIR + "_bak";
209 BASETEST_BACKUPFILE = new File(BASETEST_BACKUPDIR);
210 if (!BASETEST_BACKUPFILE.exists())
211 BASETEST_BACKUPFILE.mkdirs();
214 public static String getDBFileName(String dbname)
216 DEBUGOUT(1, "getDBFileName returning: " + BASETEST_DBDIR + "/" + dbname);
217 return BASETEST_DBDIR + "/" + dbname;
220 public static String getBackupFileName(String dbname)
222 DEBUGOUT(1, "getBackupFileName returning: " + BASETEST_BACKUPDIR + "/" + dbname);
223 return BASETEST_BACKUPDIR + "/" + dbname;
226 public static OutputStream getErrorStream()
228 OutputStream retval = System.err;
230 File outfile = new File(BASETEST_DBDIR + "/" + "errstream.log");
234 outfile.createNewFile();
236 outfile.createNewFile();
238 retval = new FileOutputStream(outfile);
239 } catch (FileNotFoundException fnfe) {
240 DEBUGOUT(3, "Unable to open error log file. " + fnfe);
241 } catch (IOException ioe) {
242 DEBUGOUT(3, "Unable to create error log file. " + ioe);