Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / test / java / junit / src / com / sleepycat / db / test / TestUtils.java
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002, 2012 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7
8 /*
9  * Generally useful functions :)
10  */
11
12 package com.sleepycat.db.test;
13
14 import static org.junit.Assert.fail;
15
16 import com.sleepycat.db.*;
17
18 import java.io.BufferedInputStream;
19 import java.io.BufferedReader;
20 import java.io.File;
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;
28
29 public class TestUtils
30 {
31     public static boolean config_loaded = false;
32     public static boolean verbose_flag = false;
33     public static int debug_level = 2;
34  
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);
40  
41     public static void ERR(String a)
42     {
43         System.err.println("FAIL: " + a);
44         fail(a);
45     }
46
47     public static void DEBUGOUT(String s)
48     {
49         DEBUGOUT(1, s);
50     }
51
52     public static void DEBUGOUT(int importance, String s)
53     {
54         if(importance > debug_level)
55             System.out.println("DEBUG: " +s);
56     }
57
58     public static void VERBOSEOUT(String s)
59     {
60         if (verbose_flag)
61             System.out.println(s);
62     }
63
64     public static void sysexit(int code)
65     {
66         System.exit(code);
67     }
68
69     public static void check_file_removed(String name, boolean fatal,
70                                            boolean force_remove_first)
71     {
72         File f = new File(name);
73         if (force_remove_first) {
74             f.delete();
75         }
76         if (f.exists()) {
77             if (fatal)
78                 System.out.print("FAIL: ");
79             DEBUGOUT(1, "File \"" + name + "\" still exists after check_file_removed\n");
80             if (fatal)
81                 fail("File \"" + name + "\" still exists after check_file_removed");
82         }
83     }
84
85
86     // remove any existing environment or database
87     public static void removeall(boolean use_db, boolean remove_env, String envpath, String dbname)
88     {
89         {
90             try {
91                 if (remove_env)
92                     Environment.remove(new File(envpath), true, EnvironmentConfig.DEFAULT);
93                 if (use_db)
94                     Database.remove(dbname, null, DatabaseConfig.DEFAULT);
95             }
96             catch (DatabaseException dbe) {
97                 DEBUGOUT(1, "TestUtil::removeall exception caught: " + dbe);
98             }
99             catch (FileNotFoundException dbe) {
100                 DEBUGOUT(1, "TestUtil::removeall exception caught: " + dbe);
101             }
102         }
103         check_file_removed(dbname, false, !use_db);
104         if (remove_env) {
105             for (int i=0; i<8; i++) {
106                 String fname = envpath + "/" + "__db." + i;
107                 check_file_removed(fname, true, !use_db);
108             }
109          
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);
120                     } else {
121                         if(remainingfiles[i].indexOf("del") == -1)
122                             DEBUGOUT(3, "TestUtils::removeall warning, file: " + remainingfiles[i] + " remains in directory after cleanup.");
123                     }
124                 }
125             }
126         }
127     }
128  
129     public static boolean removeDir(String dirname)
130     {
131         try {
132             File deldir = new File(dirname);
133
134             if (!deldir.exists()) {
135                 return true;
136             } else if(!deldir.isDirectory()) {
137                 return false;
138             } else {
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();
143                 deldir.delete();
144             }
145         } catch (Exception e) {
146             TestUtils.DEBUGOUT(4, "Warning: error encountered removing directory.\n" + e);
147         }
148         return true;
149     }
150  
151     static public String shownull(Object o)
152     {
153         if (o == null)
154             return "null";
155         else
156             return "not null";
157     }
158
159         /*
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.
165          */
166     public static void loadConfig(String envfilename)
167     {
168         if(config_loaded)
169             return;
170      
171         String configname = envfilename;
172         if(envfilename == null)
173         {
174             String OSStr = java.lang.System.getProperty("os.name");
175             if((OSStr.toLowerCase()).indexOf("windows") != -1)
176             {
177                 configname = "config_win32";
178             } else {
179                 // assume a nix variant.
180                 configname = "config_nix";
181             }
182         }
183         config_loaded = true;
184         try {
185             InputStream in = new FileInputStream(configname);
186             DEBUGOUT(2, "Opened " + configname + " to read configuration.");
187             Properties props = new Properties();
188             props.load(in);
189          
190             String var = props.getProperty("BASETEST_DBDIR");
191             if(var != null)
192             { // Property seems to encase things in "";
193                 var = var.substring(1);
194                 var = var.substring(0, var.length() -2);
195                 BASETEST_DBDIR = var;
196             }             
197             DEBUGOUT(2, "BASETEST_DBDIR is: " + BASETEST_DBDIR);
198
199         } catch (Exception e) {
200                         // expected - the config file is optional.
201             DEBUGOUT(0, "loadEnvVars -- loading of default variables failed. error: " + e);
202         }
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();
212     }
213  
214     public static String getDBFileName(String dbname)
215     {
216         DEBUGOUT(1, "getDBFileName returning: " + BASETEST_DBDIR + "/" + dbname);
217         return BASETEST_DBDIR + "/" + dbname;
218     }
219
220     public static String getBackupFileName(String dbname)
221     {
222         DEBUGOUT(1, "getBackupFileName returning: " + BASETEST_BACKUPDIR + "/" + dbname);
223         return BASETEST_BACKUPDIR + "/" + dbname;
224     }
225  
226     public static OutputStream getErrorStream()
227     {
228         OutputStream retval = System.err;
229         try {
230             File outfile = new File(BASETEST_DBDIR + "/" + "errstream.log");
231             if(outfile.exists())
232             {
233                 outfile.delete();
234                 outfile.createNewFile();
235             } else {
236                 outfile.createNewFile();
237             }
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);
243         }
244         return retval;
245     }
246 }