• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src/router/db-4.8.30/test/scr016/src/com/sleepycat/db/test/
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002-2009 Oracle.  All rights reserved.
5 *
6 */
7
8/*
9 * Generally useful functions :)
10 */
11
12package com.sleepycat.db.test;
13
14import static org.junit.Assert.fail;
15
16import com.sleepycat.db.*;
17
18import java.io.BufferedInputStream;
19import java.io.BufferedReader;
20import java.io.File;
21import java.io.FileInputStream;
22import java.io.FileOutputStream;
23import java.io.FileNotFoundException;
24import java.io.InputStream;
25import java.io.IOException;
26import java.io.OutputStream;
27import java.util.Properties;
28
29public 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
39    public static void ERR(String a)
40    {
41        System.err.println("FAIL: " + a);
42        fail(a);
43    }
44
45    public static void DEBUGOUT(String s)
46    {
47        DEBUGOUT(1, s);
48    }
49
50    public static void DEBUGOUT(int importance, String s)
51    {
52        if(importance > debug_level)
53            System.out.println("DEBUG: " +s);
54    }
55
56    public static void VERBOSEOUT(String s)
57    {
58        if (verbose_flag)
59            System.out.println(s);
60    }
61
62    public static void sysexit(int code)
63    {
64        System.exit(code);
65    }
66
67    public static void check_file_removed(String name, boolean fatal,
68					   boolean force_remove_first)
69    {
70        File f = new File(name);
71        if (force_remove_first) {
72            f.delete();
73        }
74        if (f.exists()) {
75            if (fatal)
76                System.out.print("FAIL: ");
77            DEBUGOUT(1, "File \"" + name + "\" still exists after check_file_removed\n");
78            if (fatal)
79                fail("File \"" + name + "\" still exists after check_file_removed");
80        }
81    }
82
83
84    // remove any existing environment or database
85    public static void removeall(boolean use_db, boolean remove_env, String envpath, String dbname)
86    {
87        {
88            try {
89                if (remove_env)
90                    Environment.remove(new File(envpath), true, EnvironmentConfig.DEFAULT);
91                if (use_db)
92                    Database.remove(dbname, null, DatabaseConfig.DEFAULT);
93            }
94            catch (DatabaseException dbe) {
95                DEBUGOUT(1, "TestUtil::removeall exception caught: " + dbe);
96            }
97            catch (FileNotFoundException dbe) {
98                DEBUGOUT(1, "TestUtil::removeall exception caught: " + dbe);
99            }
100        }
101        check_file_removed(dbname, false, !use_db);
102        if (remove_env) {
103            for (int i=0; i<8; i++) {
104                String fname = envpath + "/" + "__db." + i;
105                check_file_removed(fname, true, !use_db);
106            }
107
108            // ensure the user knows if there is junk remaining.
109            // clean out spurious log.00X files
110            File dir = new File(envpath);
111            if(dir.isDirectory()) {
112                String[] remainingfiles = dir.list();
113                for(int i = 0; i < remainingfiles.length; i++) {
114                    if(remainingfiles[i].startsWith("log") || remainingfiles[i].endsWith("db2") ||
115                        remainingfiles[i].endsWith("log") || remainingfiles[i].startsWith("__db")) {
116                        DEBUGOUT(1, "TestUtils::removeall removing: " +remainingfiles[i]);
117                        check_file_removed(envpath + "/" + remainingfiles[i], false, true);
118                    } else {
119                        if(remainingfiles[i].indexOf("del") == -1)
120                            DEBUGOUT(3, "TestUtils::removeall warning, file: " + remainingfiles[i] + " remains in directory after cleanup.");
121                    }
122                }
123            }
124        }
125    }
126
127    public static boolean removeDir(String dirname)
128    {
129        try {
130            File deldir = new File(dirname);
131
132            if (!deldir.exists()) {
133                return true;
134            } else if(!deldir.isDirectory()) {
135                return false;
136            } else {
137                // The following will fail if the directory contains sub-dirs.
138                File[] contents = deldir.listFiles();
139                for (int i = 0; i < contents.length; i++)
140                    contents[i].delete();
141                deldir.delete();
142            }
143        } catch (Exception e) {
144            TestUtils.DEBUGOUT(4, "Warning: error encountered removing directory.\n" + e);
145        }
146        return true;
147    }
148
149    static public String shownull(Object o)
150    {
151        if (o == null)
152            return "null";
153        else
154            return "not null";
155    }
156
157	/*
158	 * The config file is not currently required.
159	 * The only variable that can be set via the
160	 * config file is the base directory for the
161	 * tests to be run in. The default is "data"
162	 * and will be created for the tests.
163	 */
164    public static void loadConfig(String envfilename)
165    {
166        if(config_loaded)
167            return;
168
169        String configname = envfilename;
170        if(envfilename == null)
171        {
172            String OSStr = java.lang.System.getProperty("os.name");
173            if((OSStr.toLowerCase()).indexOf("windows") != -1)
174            {
175                configname = "config_win32";
176            } else {
177                // assume a nix variant.
178                configname = "config_nix";
179            }
180        }
181        config_loaded = true;
182        try {
183            InputStream in = new FileInputStream(configname);
184            DEBUGOUT(2, "Opened " + configname + " to read configuration.");
185            Properties props = new Properties();
186            props.load(in);
187
188            String var = props.getProperty("BASETEST_DBDIR");
189            if(var != null)
190            { // Property seems to encase things in "";
191                var = var.substring(1);
192                var = var.substring(0, var.length() -2);
193                BASETEST_DBDIR = var;
194            }
195            DEBUGOUT(2, "BASETEST_DBDIR is: " + BASETEST_DBDIR);
196
197        } catch (Exception e) {
198			// expected - the config file is optional.
199            DEBUGOUT(0, "loadEnvVars -- loading of default variables failed. error: " + e);
200        }
201		if (BASETEST_DBDIR == "")
202			BASETEST_DBDIR = "data";
203        BASETEST_DBFILE = new File(BASETEST_DBDIR);
204        if (!BASETEST_DBFILE.exists())
205		    BASETEST_DBFILE.mkdirs();
206    }
207
208    public static String getDBFileName(String dbname)
209    {
210        DEBUGOUT(1, "getDBFileName returning: " + BASETEST_DBDIR + "/" + dbname);
211        return BASETEST_DBDIR + "/" + dbname;
212    }
213
214    public static OutputStream getErrorStream()
215    {
216        OutputStream retval = System.err;
217        try {
218            File outfile = new File(BASETEST_DBDIR + "/" + "errstream.log");
219            if(outfile.exists())
220            {
221                outfile.delete();
222                outfile.createNewFile();
223            } else {
224                outfile.createNewFile();
225            }
226            retval = new FileOutputStream(outfile);
227        } catch (FileNotFoundException fnfe) {
228            DEBUGOUT(3, "Unable to open error log file. " + fnfe);
229        } catch (IOException ioe) {
230            DEBUGOUT(3, "Unable to create error log file. " + ioe);
231        }
232        return retval;
233    }
234}
235