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.

PostOrderTreeWalkTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertFalse;
  48. import static org.junit.Assert.assertTrue;
  49. import org.eclipse.jgit.dircache.DirCache;
  50. import org.eclipse.jgit.dircache.DirCacheBuilder;
  51. import org.eclipse.jgit.dircache.DirCacheEntry;
  52. import org.eclipse.jgit.dircache.DirCacheIterator;
  53. import org.eclipse.jgit.junit.RepositoryTestCase;
  54. import org.eclipse.jgit.lib.FileMode;
  55. import org.junit.Test;
  56. public class PostOrderTreeWalkTest extends RepositoryTestCase {
  57. @Test
  58. public void testInitialize_NoPostOrder() throws Exception {
  59. try (TreeWalk tw = new TreeWalk(db)) {
  60. assertFalse(tw.isPostOrderTraversal());
  61. }
  62. }
  63. @Test
  64. public void testInitialize_TogglePostOrder() throws Exception {
  65. try (TreeWalk tw = new TreeWalk(db)) {
  66. assertFalse(tw.isPostOrderTraversal());
  67. tw.setPostOrderTraversal(true);
  68. assertTrue(tw.isPostOrderTraversal());
  69. tw.setPostOrderTraversal(false);
  70. assertFalse(tw.isPostOrderTraversal());
  71. }
  72. }
  73. @Test
  74. public void testResetDoesNotAffectPostOrder() throws Exception {
  75. try (TreeWalk tw = new TreeWalk(db)) {
  76. tw.setPostOrderTraversal(true);
  77. assertTrue(tw.isPostOrderTraversal());
  78. tw.reset();
  79. assertTrue(tw.isPostOrderTraversal());
  80. tw.setPostOrderTraversal(false);
  81. assertFalse(tw.isPostOrderTraversal());
  82. tw.reset();
  83. assertFalse(tw.isPostOrderTraversal());
  84. }
  85. }
  86. @Test
  87. public void testNoPostOrder() throws Exception {
  88. final DirCache tree = db.readDirCache();
  89. final DirCacheBuilder b = tree.builder();
  90. b.add(makeFile("a"));
  91. b.add(makeFile("b/c"));
  92. b.add(makeFile("b/d"));
  93. b.add(makeFile("q"));
  94. b.finish();
  95. assertEquals(4, tree.getEntryCount());
  96. try (TreeWalk tw = new TreeWalk(db)) {
  97. tw.setPostOrderTraversal(false);
  98. tw.addTree(new DirCacheIterator(tree));
  99. assertModes("a", REGULAR_FILE, tw);
  100. assertModes("b", TREE, tw);
  101. assertTrue(tw.isSubtree());
  102. assertFalse(tw.isPostChildren());
  103. tw.enterSubtree();
  104. assertModes("b/c", REGULAR_FILE, tw);
  105. assertModes("b/d", REGULAR_FILE, tw);
  106. assertModes("q", REGULAR_FILE, tw);
  107. }
  108. }
  109. @Test
  110. public void testWithPostOrder_EnterSubtree() throws Exception {
  111. final DirCache tree = db.readDirCache();
  112. final DirCacheBuilder b = tree.builder();
  113. b.add(makeFile("a"));
  114. b.add(makeFile("b/c"));
  115. b.add(makeFile("b/d"));
  116. b.add(makeFile("q"));
  117. b.finish();
  118. assertEquals(4, tree.getEntryCount());
  119. try (TreeWalk tw = new TreeWalk(db)) {
  120. tw.setPostOrderTraversal(true);
  121. tw.addTree(new DirCacheIterator(tree));
  122. assertModes("a", REGULAR_FILE, tw);
  123. assertModes("b", TREE, tw);
  124. assertTrue(tw.isSubtree());
  125. assertFalse(tw.isPostChildren());
  126. tw.enterSubtree();
  127. assertModes("b/c", REGULAR_FILE, tw);
  128. assertModes("b/d", REGULAR_FILE, tw);
  129. assertModes("b", TREE, tw);
  130. assertTrue(tw.isSubtree());
  131. assertTrue(tw.isPostChildren());
  132. assertModes("q", REGULAR_FILE, tw);
  133. }
  134. }
  135. @Test
  136. public void testWithPostOrder_NoEnterSubtree() throws Exception {
  137. final DirCache tree = db.readDirCache();
  138. final DirCacheBuilder b = tree.builder();
  139. b.add(makeFile("a"));
  140. b.add(makeFile("b/c"));
  141. b.add(makeFile("b/d"));
  142. b.add(makeFile("q"));
  143. b.finish();
  144. assertEquals(4, tree.getEntryCount());
  145. try (TreeWalk tw = new TreeWalk(db)) {
  146. tw.setPostOrderTraversal(true);
  147. tw.addTree(new DirCacheIterator(tree));
  148. assertModes("a", REGULAR_FILE, tw);
  149. assertModes("b", TREE, tw);
  150. assertTrue(tw.isSubtree());
  151. assertFalse(tw.isPostChildren());
  152. assertModes("q", REGULAR_FILE, tw);
  153. }
  154. }
  155. private DirCacheEntry makeFile(String path) throws Exception {
  156. return createEntry(path, REGULAR_FILE);
  157. }
  158. private static void assertModes(final String path, final FileMode mode0,
  159. final TreeWalk tw) throws Exception {
  160. assertTrue("has " + path, tw.next());
  161. assertEquals(path, tw.getPathString());
  162. assertEquals(mode0, tw.getFileMode(0));
  163. }
  164. }