/tags/SN-6.0/db4/test/scr016/src/com/sleepycat/db/test/TestUtils.java

https://gitlab.com/OpenSourceMirror/sourcenav · Java · 227 lines · 186 code · 24 blank · 17 comment · 38 complexity · ea98d9330676b1160776448d04ecb001 MD5 · raw file

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