test075.tcl
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:7k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. # See the file LICENSE for redistribution information.
  2. #
  3. # Copyright (c) 2000
  4. # Sleepycat Software.  All rights reserved.
  5. #
  6. # $Id: test075.tcl,v 11.9 2000/08/25 14:21:58 sue Exp $
  7. #
  8. # DB Test 75 (replacement)
  9. # Test the DB->rename method.
  10. proc test075 { method { tnum 75 } args } {
  11. global errorCode
  12. source ./include.tcl
  13. set omethod [convert_method $method]
  14. set args [convert_args $method $args]
  15. puts "Test0$tnum: $method ($args): Test of DB->rename()"
  16. # If we are using an env, then testfile should just be the db name.
  17. # Otherwise it is the test directory and the name.
  18. set eindex [lsearch -exact $args "-env"]
  19. if { $eindex == -1 } {
  20. set oldfile $testdir/test0$tnum-old.db
  21. set newfile $testdir/test0$tnum.db
  22. set env NULL
  23. set renargs ""
  24. } else {
  25. set oldfile test0$tnum-old.db
  26. set newfile test0$tnum.db
  27. # File existence checks won't work in an env, since $oldfile
  28. # and $newfile won't be in the current working directory.
  29. # We use this to skip them, and turn our secondary check
  30. # (opening the dbs and seeing that all is well) into the main
  31. # one.
  32. incr eindex
  33. set env [lindex $args $eindex]
  34. set renargs " -env $env"
  35. }
  36. # Make sure we're starting from a clean slate.
  37. cleanup $testdir $env
  38. if { $env == "NULL" } {
  39. error_check_bad "$oldfile exists" [file exists $oldfile] 1
  40. error_check_bad "$newfile exists" [file exists $newfile] 1
  41. }
  42. puts "tTest0$tnum.a: Create/rename file"
  43. puts "ttTest0$tnum.a.1: create"
  44. set db [eval {berkdb_open -create -mode 0644} $omethod $args $oldfile]
  45. error_check_good dbopen [is_valid_db $db] TRUE
  46. if { $env == "NULL" } {
  47. error_check_bad "$oldfile exists" [file exists $oldfile] 0
  48. error_check_bad "$newfile exists" [file exists $newfile] 1
  49. }
  50. # The nature of the key and data are unimportant; use numeric key
  51. # so record-based methods don't need special treatment.
  52. set key 1
  53. set data [pad_data $method data]
  54. error_check_good dbput [$db put $key $data] 0
  55. error_check_good dbclose [$db close] 0
  56. puts "ttTest0$tnum.a.2: rename"
  57. if { $env == "NULL" } {
  58. error_check_bad "$oldfile exists" [file exists $oldfile] 0
  59. error_check_bad "$newfile exists" [file exists $newfile] 1
  60. }
  61. error_check_good rename_file [eval {berkdb dbrename}
  62.     $renargs $oldfile $newfile] 0
  63. if { $env == "NULL" } {
  64. error_check_bad "$oldfile exists" [file exists $oldfile] 1
  65. error_check_bad "$newfile exists" [file exists $newfile] 0
  66. }
  67. puts "ttTest0$tnum.a.3: check"
  68. # Open again with create to make sure we're not caching or anything
  69. # silly.  In the normal case (no env), we already know the file doesn't
  70. # exist.
  71. set odb [eval {berkdb_open -create -mode 0644} $omethod $args $oldfile]
  72. set ndb [eval {berkdb_open -create -mode 0644} $omethod $args $newfile]
  73. error_check_good odb_open [is_valid_db $odb] TRUE
  74. error_check_good ndb_open [is_valid_db $ndb] TRUE
  75. set odbt [$odb get $key]
  76. set ndbt [$ndb get $key]
  77. # The DBT from the "old" database should be empty, not the "new" one.
  78. error_check_good odbt_empty [llength $odbt] 0
  79. error_check_bad ndbt_empty [llength $ndbt] 0
  80. error_check_good ndbt [lindex [lindex $ndbt 0] 1] $data
  81. error_check_good odb_close [$odb close] 0
  82. error_check_good ndb_close [$ndb close] 0
  83. if { $env != "NULL" } {
  84. puts "tTest0$tnum: External environment present; 
  85.     skipping remainder"
  86. return
  87. }
  88. # Now there's both an old and a new.  Rename the "new" to the "old"
  89. # and make sure that fails.
  90. #
  91. # XXX Ideally we'd do this test even when there's an external
  92. # environment, but that env has errpfx/errfile set now.  :-(
  93. puts "tTest0$tnum.b: Make sure rename fails instead of overwriting"
  94. set ret [catch {eval {berkdb dbrename} $renargs $newfile $oldfile} res]
  95. error_check_bad rename_overwrite $ret 0
  96. error_check_good rename_overwrite_ret [is_substr $errorCode EEXIST] 1
  97. # Verify and then start over from a clean slate.
  98. verify_dir $testdir "tTest0$tnum.c: "
  99. cleanup $testdir $env
  100. error_check_bad "$oldfile exists" [file exists $oldfile] 1
  101. error_check_bad "$newfile exists" [file exists $newfile] 1
  102. set oldfile test0$tnum-old.db
  103. set newfile test0$tnum.db
  104. puts "tTest0$tnum.d: Create/rename file in environment"
  105. set env [berkdb env -create -home $testdir]
  106. error_check_good env_open [is_valid_env $env] TRUE
  107. error_check_bad "$oldfile exists" [file exists $oldfile] 1
  108. error_check_bad "$newfile exists" [file exists $newfile] 1
  109. puts "ttTest0$tnum.d.1: create"
  110. set db [eval {berkdb_open -create -mode 0644} -env $env
  111.     $omethod $args $oldfile]
  112. error_check_good dbopen [is_valid_db $db] TRUE
  113. # We need to make sure that it didn't create/rename into the
  114. # current directory.
  115. error_check_bad "$oldfile exists" [file exists $oldfile] 1
  116. error_check_bad "$newfile exists" [file exists $newfile] 1
  117. error_check_bad "$testdir/$oldfile exists"
  118.     [file exists $testdir/$oldfile] 0
  119. error_check_bad "$testdir/$newfile exists"
  120.     [file exists $testdir/$newfile] 1
  121. error_check_good dbput [$db put $key $data] 0
  122. error_check_good dbclose [$db close] 0
  123. puts "ttTest0$tnum.d.2: rename"
  124. error_check_good rename_file [berkdb dbrename -env $env
  125.     $oldfile $newfile] 0
  126. error_check_bad "$oldfile exists" [file exists $oldfile] 1
  127. error_check_bad "$newfile exists" [file exists $newfile] 1
  128. error_check_bad "$testdir/$oldfile exists"
  129.     [file exists $testdir/$oldfile] 1
  130. error_check_bad "$testdir/$newfile exists"
  131.     [file exists $testdir/$newfile] 0
  132. puts "ttTest0$tnum.d.3: check"
  133. # Open again with create to make sure we're not caching or anything
  134. # silly.
  135. set odb [eval {berkdb_open -create -mode 0644} -env $env
  136.     $omethod $args $oldfile]
  137. set ndb [eval {berkdb_open -create -mode 0644} -env $env
  138.     $omethod $args $newfile]
  139. error_check_good odb_open [is_valid_db $odb] TRUE
  140. error_check_good ndb_open [is_valid_db $ndb] TRUE
  141. set odbt [$odb get $key]
  142. set ndbt [$ndb get $key]
  143. # The DBT from the "old" database should be empty, not the "new" one.
  144. error_check_good odbt_empty [llength $odbt] 0
  145. error_check_bad ndbt_empty [llength $ndbt] 0
  146. error_check_good ndbt [lindex [lindex $ndbt 0] 1] $data
  147. error_check_good odb_close [$odb close] 0
  148. error_check_good ndb_close [$ndb close] 0
  149. # XXX
  150. # We need to close and reopen the env since berkdb_open has
  151. # set its errfile/errpfx, and we can't unset that.
  152. error_check_good env_close [$env close] 0
  153. set env [berkdb env -home $testdir]
  154. error_check_good env_open2 [is_valid_env $env] TRUE
  155. puts "tTest0$tnum.e:
  156.     Make sure rename fails instead of overwriting in env"
  157. set ret [catch {eval {berkdb dbrename} -env $env $newfile $oldfile} res]
  158. error_check_bad rename_overwrite $ret 0
  159. error_check_good rename_overwrite_ret [is_substr $errorCode EEXIST] 1
  160. error_check_good env_close [$env close] 0
  161. puts "tTest0$tnum succeeded."
  162. }