You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.0 KiB

11 years ago
11 years ago
9 years ago
9 years ago
9 years ago
  1. package embedded_filer
  2. import (
  3. "os"
  4. "strings"
  5. "testing"
  6. )
  7. func TestDirectory(t *testing.T) {
  8. dm, _ := NewDirectoryManagerInMap("/tmp/dir.log")
  9. defer func() {
  10. if true {
  11. os.Remove("/tmp/dir.log")
  12. }
  13. }()
  14. dm.MakeDirectory("/a/b/c")
  15. dm.MakeDirectory("/a/b/d")
  16. dm.MakeDirectory("/a/b/e")
  17. dm.MakeDirectory("/a/b/e/f")
  18. dm.MakeDirectory("/a/b/e/f/g")
  19. dm.MoveUnderDirectory("/a/b/e/f/g", "/a/b", "t")
  20. if _, err := dm.FindDirectory("/a/b/e/f/g"); err == nil {
  21. t.Fatal("/a/b/e/f/g should not exist any more after moving")
  22. }
  23. if _, err := dm.FindDirectory("/a/b/t"); err != nil {
  24. t.Fatal("/a/b/t should exist after moving")
  25. }
  26. if _, err := dm.FindDirectory("/a/b/g"); err == nil {
  27. t.Fatal("/a/b/g should not exist after moving")
  28. }
  29. dm.MoveUnderDirectory("/a/b/e/f", "/a/b", "")
  30. if _, err := dm.FindDirectory("/a/b/f"); err != nil {
  31. t.Fatal("/a/b/g should not exist after moving")
  32. }
  33. dm.MakeDirectory("/a/b/g/h/i")
  34. dm.DeleteDirectory("/a/b/e/f")
  35. dm.DeleteDirectory("/a/b/e")
  36. dirNames, _ := dm.ListDirectories("/a/b/e")
  37. for _, v := range dirNames {
  38. println("sub1 dir:", v.Name, "id", v.Id)
  39. }
  40. dm.logFile.Close()
  41. var path []string
  42. printTree(dm.Root, path)
  43. dm2, e := NewDirectoryManagerInMap("/tmp/dir.log")
  44. if e != nil {
  45. println("load error", e.Error())
  46. }
  47. if !compare(dm.Root, dm2.Root) {
  48. t.Fatal("restored dir not the same!")
  49. }
  50. printTree(dm2.Root, path)
  51. }
  52. func printTree(node *DirectoryEntryInMap, path []string) {
  53. println(strings.Join(path, "/") + "/" + node.Name)
  54. path = append(path, node.Name)
  55. for _, v := range node.subDirectories {
  56. printTree(v, path)
  57. }
  58. }
  59. func compare(root1 *DirectoryEntryInMap, root2 *DirectoryEntryInMap) bool {
  60. if len(root1.subDirectories) != len(root2.subDirectories) {
  61. return false
  62. }
  63. if root1.Name != root2.Name {
  64. return false
  65. }
  66. if root1.Id != root2.Id {
  67. return false
  68. }
  69. if !(root1.Parent == nil && root2.Parent == nil) {
  70. if root1.Parent.Id != root2.Parent.Id {
  71. return false
  72. }
  73. }
  74. for k, v := range root1.subDirectories {
  75. if !compare(v, root2.subDirectories[k]) {
  76. return false
  77. }
  78. }
  79. return true
  80. }