Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PostOrderTreeWalkTest.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2008, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.treewalk;
  44. import static org.eclipse.jgit.lib.FileMode.REGULAR_FILE;
  45. import static org.eclipse.jgit.lib.FileMode.TREE;
  46. import org.eclipse.jgit.dircache.DirCache;
  47. import org.eclipse.jgit.dircache.DirCacheBuilder;
  48. import org.eclipse.jgit.dircache.DirCacheEntry;
  49. import org.eclipse.jgit.dircache.DirCacheIterator;
  50. import org.eclipse.jgit.lib.Constants;
  51. import org.eclipse.jgit.lib.FileMode;
  52. import org.eclipse.jgit.lib.ObjectInserter;
  53. import org.eclipse.jgit.lib.RepositoryTestCase;
  54. public class PostOrderTreeWalkTest extends RepositoryTestCase {
  55. public void testInitialize_NoPostOrder() throws Exception {
  56. final TreeWalk tw = new TreeWalk(db);
  57. assertFalse(tw.isPostOrderTraversal());
  58. }
  59. public void testInitialize_TogglePostOrder() throws Exception {
  60. final TreeWalk tw = new TreeWalk(db);
  61. assertFalse(tw.isPostOrderTraversal());
  62. tw.setPostOrderTraversal(true);
  63. assertTrue(tw.isPostOrderTraversal());
  64. tw.setPostOrderTraversal(false);
  65. assertFalse(tw.isPostOrderTraversal());
  66. }
  67. public void testResetDoesNotAffectPostOrder() throws Exception {
  68. final TreeWalk tw = new TreeWalk(db);
  69. tw.setPostOrderTraversal(true);
  70. assertTrue(tw.isPostOrderTraversal());
  71. tw.reset();
  72. assertTrue(tw.isPostOrderTraversal());
  73. tw.setPostOrderTraversal(false);
  74. assertFalse(tw.isPostOrderTraversal());
  75. tw.reset();
  76. assertFalse(tw.isPostOrderTraversal());
  77. }
  78. public void testNoPostOrder() throws Exception {
  79. final DirCache tree = db.readDirCache();
  80. {
  81. final DirCacheBuilder b = tree.builder();
  82. b.add(makeFile("a"));
  83. b.add(makeFile("b/c"));
  84. b.add(makeFile("b/d"));
  85. b.add(makeFile("q"));
  86. b.finish();
  87. assertEquals(4, tree.getEntryCount());
  88. }
  89. final TreeWalk tw = new TreeWalk(db);
  90. tw.setPostOrderTraversal(false);
  91. tw.addTree(new DirCacheIterator(tree));
  92. assertModes("a", REGULAR_FILE, tw);
  93. assertModes("b", TREE, tw);
  94. assertTrue(tw.isSubtree());
  95. assertFalse(tw.isPostChildren());
  96. tw.enterSubtree();
  97. assertModes("b/c", REGULAR_FILE, tw);
  98. assertModes("b/d", REGULAR_FILE, tw);
  99. assertModes("q", REGULAR_FILE, tw);
  100. }
  101. public void testWithPostOrder_EnterSubtree() throws Exception {
  102. final DirCache tree = db.readDirCache();
  103. {
  104. final DirCacheBuilder b = tree.builder();
  105. b.add(makeFile("a"));
  106. b.add(makeFile("b/c"));
  107. b.add(makeFile("b/d"));
  108. b.add(makeFile("q"));
  109. b.finish();
  110. assertEquals(4, tree.getEntryCount());
  111. }
  112. final TreeWalk tw = new TreeWalk(db);
  113. tw.setPostOrderTraversal(true);
  114. tw.addTree(new DirCacheIterator(tree));
  115. assertModes("a", REGULAR_FILE, tw);
  116. assertModes("b", TREE, tw);
  117. assertTrue(tw.isSubtree());
  118. assertFalse(tw.isPostChildren());
  119. tw.enterSubtree();
  120. assertModes("b/c", REGULAR_FILE, tw);
  121. assertModes("b/d", REGULAR_FILE, tw);
  122. assertModes("b", TREE, tw);
  123. assertTrue(tw.isSubtree());
  124. assertTrue(tw.isPostChildren());
  125. assertModes("q", REGULAR_FILE, tw);
  126. }
  127. public void testWithPostOrder_NoEnterSubtree() throws Exception {
  128. final DirCache tree = db.readDirCache();
  129. {
  130. final DirCacheBuilder b = tree.builder();
  131. b.add(makeFile("a"));
  132. b.add(makeFile("b/c"));
  133. b.add(makeFile("b/d"));
  134. b.add(makeFile("q"));
  135. b.finish();
  136. assertEquals(4, tree.getEntryCount());
  137. }
  138. final TreeWalk tw = new TreeWalk(db);
  139. tw.setPostOrderTraversal(true);
  140. tw.addTree(new DirCacheIterator(tree));
  141. assertModes("a", REGULAR_FILE, tw);
  142. assertModes("b", TREE, tw);
  143. assertTrue(tw.isSubtree());
  144. assertFalse(tw.isPostChildren());
  145. assertModes("q", REGULAR_FILE, tw);
  146. }
  147. private DirCacheEntry makeFile(final String path) throws Exception {
  148. final DirCacheEntry ent = new DirCacheEntry(path);
  149. ent.setFileMode(REGULAR_FILE);
  150. ent.setObjectId(new ObjectInserter.Formatter().idFor(
  151. Constants.OBJ_BLOB, Constants.encode(path)));
  152. return ent;
  153. }
  154. private static void assertModes(final String path, final FileMode mode0,
  155. final TreeWalk tw) throws Exception {
  156. assertTrue("has " + path, tw.next());
  157. assertEquals(path, tw.getPathString());
  158. assertEquals(mode0, tw.getFileMode(0));
  159. }
  160. }