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.lib.FileMode;
  54. import org.eclipse.jgit.lib.RepositoryTestCase;
  55. import org.junit.Test;
  56. public class PostOrderTreeWalkTest extends RepositoryTestCase {
  57. @Test
  58. public void testInitialize_NoPostOrder() throws Exception {
  59. final TreeWalk tw = new TreeWalk(db);
  60. assertFalse(tw.isPostOrderTraversal());
  61. }
  62. @Test
  63. public void testInitialize_TogglePostOrder() throws Exception {
  64. final TreeWalk tw = new TreeWalk(db);
  65. assertFalse(tw.isPostOrderTraversal());
  66. tw.setPostOrderTraversal(true);
  67. assertTrue(tw.isPostOrderTraversal());
  68. tw.setPostOrderTraversal(false);
  69. assertFalse(tw.isPostOrderTraversal());
  70. }
  71. @Test
  72. public void testResetDoesNotAffectPostOrder() throws Exception {
  73. final TreeWalk tw = new TreeWalk(db);
  74. tw.setPostOrderTraversal(true);
  75. assertTrue(tw.isPostOrderTraversal());
  76. tw.reset();
  77. assertTrue(tw.isPostOrderTraversal());
  78. tw.setPostOrderTraversal(false);
  79. assertFalse(tw.isPostOrderTraversal());
  80. tw.reset();
  81. assertFalse(tw.isPostOrderTraversal());
  82. }
  83. @Test
  84. public void testNoPostOrder() throws Exception {
  85. final DirCache tree = db.readDirCache();
  86. {
  87. final DirCacheBuilder b = tree.builder();
  88. b.add(makeFile("a"));
  89. b.add(makeFile("b/c"));
  90. b.add(makeFile("b/d"));
  91. b.add(makeFile("q"));
  92. b.finish();
  93. assertEquals(4, tree.getEntryCount());
  94. }
  95. final TreeWalk tw = new TreeWalk(db);
  96. tw.setPostOrderTraversal(false);
  97. tw.addTree(new DirCacheIterator(tree));
  98. assertModes("a", REGULAR_FILE, tw);
  99. assertModes("b", TREE, tw);
  100. assertTrue(tw.isSubtree());
  101. assertFalse(tw.isPostChildren());
  102. tw.enterSubtree();
  103. assertModes("b/c", REGULAR_FILE, tw);
  104. assertModes("b/d", REGULAR_FILE, tw);
  105. assertModes("q", REGULAR_FILE, tw);
  106. }
  107. @Test
  108. public void testWithPostOrder_EnterSubtree() throws Exception {
  109. final DirCache tree = db.readDirCache();
  110. {
  111. final DirCacheBuilder b = tree.builder();
  112. b.add(makeFile("a"));
  113. b.add(makeFile("b/c"));
  114. b.add(makeFile("b/d"));
  115. b.add(makeFile("q"));
  116. b.finish();
  117. assertEquals(4, tree.getEntryCount());
  118. }
  119. final 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. @Test
  135. public void testWithPostOrder_NoEnterSubtree() throws Exception {
  136. final DirCache tree = db.readDirCache();
  137. {
  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. }
  146. final TreeWalk tw = new TreeWalk(db);
  147. tw.setPostOrderTraversal(true);
  148. tw.addTree(new DirCacheIterator(tree));
  149. assertModes("a", REGULAR_FILE, tw);
  150. assertModes("b", TREE, tw);
  151. assertTrue(tw.isSubtree());
  152. assertFalse(tw.isPostChildren());
  153. assertModes("q", REGULAR_FILE, tw);
  154. }
  155. private DirCacheEntry makeFile(final 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. }