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 6.0KB

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