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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.fail;
  48. import java.io.File;
  49. import java.io.IOException;
  50. import java.util.ArrayList;
  51. import org.eclipse.jgit.lib.GitIndex.Entry;
  52. import org.junit.Test;
  53. public class IndexTreeWalkerTest extends RepositoryTestCase {
  54. private ArrayList<String> treeOnlyEntriesVisited = new ArrayList<String>();
  55. private ArrayList<String> bothVisited = new ArrayList<String>();
  56. private ArrayList<String> indexOnlyEntriesVisited = new ArrayList<String>();
  57. private class TestIndexTreeVisitor extends AbstractIndexTreeVisitor {
  58. public void visitEntry(TreeEntry treeEntry, Entry indexEntry, File file) {
  59. if (treeEntry == null)
  60. indexOnlyEntriesVisited.add(indexEntry.getName());
  61. else if (indexEntry == null)
  62. treeOnlyEntriesVisited.add(treeEntry.getFullName());
  63. else bothVisited.add(indexEntry.getName());
  64. }
  65. }
  66. /*
  67. * Need to think about what I really need to be able to do....
  68. *
  69. * 1) Visit all entries in index and tree
  70. * 2) Get all directories that exist in the index, but not in the tree
  71. * -- I'm pretty sure that I don't need to do the other way around
  72. * because I already
  73. */
  74. @Test
  75. public void testTreeOnlyOneLevel() throws IOException {
  76. GitIndex index = new GitIndex(db);
  77. Tree tree = new Tree(db);
  78. tree.addFile("foo");
  79. tree.addFile("bar");
  80. new IndexTreeWalker(index, tree, trash, new TestIndexTreeVisitor()).walk();
  81. assertTrue(treeOnlyEntriesVisited.get(0).equals("bar"));
  82. assertTrue(treeOnlyEntriesVisited.get(1).equals("foo"));
  83. }
  84. @Test
  85. public void testIndexOnlyOneLevel() throws IOException {
  86. GitIndex index = new GitIndex(db);
  87. Tree tree = new Tree(db);
  88. index.add(trash, writeTrashFile("foo", "foo"));
  89. index.add(trash, writeTrashFile("bar", "bar"));
  90. new IndexTreeWalker(index, tree, trash, new TestIndexTreeVisitor()).walk();
  91. assertTrue(indexOnlyEntriesVisited.get(0).equals("bar"));
  92. assertTrue(indexOnlyEntriesVisited.get(1).equals("foo"));
  93. }
  94. @Test
  95. public void testBoth() throws IOException {
  96. GitIndex index = new GitIndex(db);
  97. Tree tree = new Tree(db);
  98. index.add(trash, writeTrashFile("a", "a"));
  99. tree.addFile("b/b");
  100. index.add(trash, writeTrashFile("c", "c"));
  101. tree.addFile("c");
  102. new IndexTreeWalker(index, tree, trash, new TestIndexTreeVisitor()).walk();
  103. assertTrue(indexOnlyEntriesVisited.contains("a"));
  104. assertTrue(treeOnlyEntriesVisited.contains("b/b"));
  105. assertTrue(bothVisited.contains("c"));
  106. }
  107. @Test
  108. public void testIndexOnlySubDirs() throws IOException {
  109. GitIndex index = new GitIndex(db);
  110. Tree tree = new Tree(db);
  111. index.add(trash, writeTrashFile("foo/bar/baz", "foobar"));
  112. index.add(trash, writeTrashFile("asdf", "asdf"));
  113. new IndexTreeWalker(index, tree, trash, new TestIndexTreeVisitor()).walk();
  114. assertEquals("asdf", indexOnlyEntriesVisited.get(0));
  115. assertEquals("foo/bar/baz", indexOnlyEntriesVisited.get(1));
  116. }
  117. @Test
  118. public void testLeavingTree() throws IOException {
  119. GitIndex index = new GitIndex(db);
  120. index.add(trash, writeTrashFile("foo/bar", "foo/bar"));
  121. index.add(trash, writeTrashFile("foobar", "foobar"));
  122. new IndexTreeWalker(index, db.mapTree(index.writeTree()), trash, new AbstractIndexTreeVisitor() {
  123. @Override
  124. public void visitEntry(TreeEntry entry, Entry indexEntry, File f) {
  125. if (entry == null || indexEntry == null)
  126. fail();
  127. }
  128. @Override
  129. public void finishVisitTree(Tree tree, int i, String curDir)
  130. throws IOException {
  131. if (tree.memberCount() == 0)
  132. fail();
  133. if (i == 0)
  134. fail();
  135. }
  136. }).walk();
  137. }
  138. }