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.

IndexTreeWalkerTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.util.ArrayList;
  48. import org.eclipse.jgit.lib.GitIndex.Entry;
  49. public class IndexTreeWalkerTest extends RepositoryTestCase {
  50. private ArrayList<String> treeOnlyEntriesVisited = new ArrayList<String>();
  51. private ArrayList<String> bothVisited = new ArrayList<String>();
  52. private ArrayList<String> indexOnlyEntriesVisited = new ArrayList<String>();
  53. private class TestIndexTreeVisitor extends AbstractIndexTreeVisitor {
  54. public void visitEntry(TreeEntry treeEntry, Entry indexEntry, File file) {
  55. if (treeEntry == null)
  56. indexOnlyEntriesVisited.add(indexEntry.getName());
  57. else if (indexEntry == null)
  58. treeOnlyEntriesVisited.add(treeEntry.getFullName());
  59. else bothVisited.add(indexEntry.getName());
  60. }
  61. }
  62. /*
  63. * Need to think about what I really need to be able to do....
  64. *
  65. * 1) Visit all entries in index and tree
  66. * 2) Get all directories that exist in the index, but not in the tree
  67. * -- I'm pretty sure that I don't need to do the other way around
  68. * because I already
  69. */
  70. public void testTreeOnlyOneLevel() throws IOException {
  71. GitIndex index = new GitIndex(db);
  72. Tree tree = new Tree(db);
  73. tree.addFile("foo");
  74. tree.addFile("bar");
  75. new IndexTreeWalker(index, tree, trash, new TestIndexTreeVisitor()).walk();
  76. assertTrue(treeOnlyEntriesVisited.get(0).equals("bar"));
  77. assertTrue(treeOnlyEntriesVisited.get(1).equals("foo"));
  78. }
  79. public void testIndexOnlyOneLevel() throws IOException {
  80. GitIndex index = new GitIndex(db);
  81. Tree tree = new Tree(db);
  82. index.add(trash, writeTrashFile("foo", "foo"));
  83. index.add(trash, writeTrashFile("bar", "bar"));
  84. new IndexTreeWalker(index, tree, trash, new TestIndexTreeVisitor()).walk();
  85. assertTrue(indexOnlyEntriesVisited.get(0).equals("bar"));
  86. assertTrue(indexOnlyEntriesVisited.get(1).equals("foo"));
  87. }
  88. public void testBoth() throws IOException {
  89. GitIndex index = new GitIndex(db);
  90. Tree tree = new Tree(db);
  91. index.add(trash, writeTrashFile("a", "a"));
  92. tree.addFile("b/b");
  93. index.add(trash, writeTrashFile("c", "c"));
  94. tree.addFile("c");
  95. new IndexTreeWalker(index, tree, trash, new TestIndexTreeVisitor()).walk();
  96. assertTrue(indexOnlyEntriesVisited.contains("a"));
  97. assertTrue(treeOnlyEntriesVisited.contains("b/b"));
  98. assertTrue(bothVisited.contains("c"));
  99. }
  100. public void testIndexOnlySubDirs() throws IOException {
  101. GitIndex index = new GitIndex(db);
  102. Tree tree = new Tree(db);
  103. index.add(trash, writeTrashFile("foo/bar/baz", "foobar"));
  104. index.add(trash, writeTrashFile("asdf", "asdf"));
  105. new IndexTreeWalker(index, tree, trash, new TestIndexTreeVisitor()).walk();
  106. assertEquals("asdf", indexOnlyEntriesVisited.get(0));
  107. assertEquals("foo/bar/baz", indexOnlyEntriesVisited.get(1));
  108. }
  109. public void testLeavingTree() throws IOException {
  110. GitIndex index = new GitIndex(db);
  111. index.add(trash, writeTrashFile("foo/bar", "foo/bar"));
  112. index.add(trash, writeTrashFile("foobar", "foobar"));
  113. new IndexTreeWalker(index, db.mapTree(index.writeTree()), trash, new AbstractIndexTreeVisitor() {
  114. @Override
  115. public void visitEntry(TreeEntry entry, Entry indexEntry, File f) {
  116. if (entry == null || indexEntry == null)
  117. fail();
  118. }
  119. @Override
  120. public void finishVisitTree(Tree tree, int i, String curDir)
  121. throws IOException {
  122. if (tree.memberCount() == 0)
  123. fail();
  124. if (i == 0)
  125. fail();
  126. }
  127. }).walk();
  128. }
  129. }