1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.treewalk;
12
13 import static org.eclipse.jgit.lib.FileMode.REGULAR_FILE;
14 import static org.eclipse.jgit.lib.FileMode.TREE;
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertTrue;
18
19 import org.eclipse.jgit.dircache.DirCache;
20 import org.eclipse.jgit.dircache.DirCacheBuilder;
21 import org.eclipse.jgit.dircache.DirCacheEntry;
22 import org.eclipse.jgit.dircache.DirCacheIterator;
23 import org.eclipse.jgit.junit.RepositoryTestCase;
24 import org.eclipse.jgit.lib.FileMode;
25 import org.junit.Test;
26
27 public class PostOrderTreeWalkTest extends RepositoryTestCase {
28 @Test
29 public void testInitialize_NoPostOrder() throws Exception {
30 try (TreeWalk tw = new TreeWalk(db)) {
31 assertFalse(tw.isPostOrderTraversal());
32 }
33 }
34
35 @Test
36 public void testInitialize_TogglePostOrder() throws Exception {
37 try (TreeWalk tw = new TreeWalk(db)) {
38 assertFalse(tw.isPostOrderTraversal());
39 tw.setPostOrderTraversal(true);
40 assertTrue(tw.isPostOrderTraversal());
41 tw.setPostOrderTraversal(false);
42 assertFalse(tw.isPostOrderTraversal());
43 }
44 }
45
46 @Test
47 public void testResetDoesNotAffectPostOrder() throws Exception {
48 try (TreeWalk tw = new TreeWalk(db)) {
49 tw.setPostOrderTraversal(true);
50 assertTrue(tw.isPostOrderTraversal());
51 tw.reset();
52 assertTrue(tw.isPostOrderTraversal());
53
54 tw.setPostOrderTraversal(false);
55 assertFalse(tw.isPostOrderTraversal());
56 tw.reset();
57 assertFalse(tw.isPostOrderTraversal());
58 }
59 }
60
61 @Test
62 public void testNoPostOrder() throws Exception {
63 final DirCache tree = db.readDirCache();
64 final DirCacheBuilder b = tree.builder();
65
66 b.add(makeFile("a"));
67 b.add(makeFile("b/c"));
68 b.add(makeFile("b/d"));
69 b.add(makeFile("q"));
70
71 b.finish();
72 assertEquals(4, tree.getEntryCount());
73
74 try (TreeWalk tw = new TreeWalk(db)) {
75 tw.setPostOrderTraversal(false);
76 tw.addTree(new DirCacheIterator(tree));
77
78 assertModes("a", REGULAR_FILE, tw);
79 assertModes("b", TREE, tw);
80 assertTrue(tw.isSubtree());
81 assertFalse(tw.isPostChildren());
82 tw.enterSubtree();
83 assertModes("b/c", REGULAR_FILE, tw);
84 assertModes("b/d", REGULAR_FILE, tw);
85 assertModes("q", REGULAR_FILE, tw);
86 }
87 }
88
89 @Test
90 public void testWithPostOrder_EnterSubtree() throws Exception {
91 final DirCache tree = db.readDirCache();
92 final DirCacheBuilder b = tree.builder();
93
94 b.add(makeFile("a"));
95 b.add(makeFile("b/c"));
96 b.add(makeFile("b/d"));
97 b.add(makeFile("q"));
98
99 b.finish();
100 assertEquals(4, tree.getEntryCount());
101
102 try (TreeWalk tw = new TreeWalk(db)) {
103 tw.setPostOrderTraversal(true);
104 tw.addTree(new DirCacheIterator(tree));
105
106 assertModes("a", REGULAR_FILE, tw);
107
108 assertModes("b", TREE, tw);
109 assertTrue(tw.isSubtree());
110 assertFalse(tw.isPostChildren());
111 tw.enterSubtree();
112 assertModes("b/c", REGULAR_FILE, tw);
113 assertModes("b/d", REGULAR_FILE, tw);
114
115 assertModes("b", TREE, tw);
116 assertTrue(tw.isSubtree());
117 assertTrue(tw.isPostChildren());
118
119 assertModes("q", REGULAR_FILE, tw);
120 }
121 }
122
123 @Test
124 public void testWithPostOrder_NoEnterSubtree() throws Exception {
125 final DirCache tree = db.readDirCache();
126 final DirCacheBuilder b = tree.builder();
127
128 b.add(makeFile("a"));
129 b.add(makeFile("b/c"));
130 b.add(makeFile("b/d"));
131 b.add(makeFile("q"));
132
133 b.finish();
134 assertEquals(4, tree.getEntryCount());
135
136 try (TreeWalk tw = new TreeWalk(db)) {
137 tw.setPostOrderTraversal(true);
138 tw.addTree(new DirCacheIterator(tree));
139
140 assertModes("a", REGULAR_FILE, tw);
141
142 assertModes("b", TREE, tw);
143 assertTrue(tw.isSubtree());
144 assertFalse(tw.isPostChildren());
145
146 assertModes("q", REGULAR_FILE, tw);
147 }
148 }
149
150 private DirCacheEntry makeFile(String path) throws Exception {
151 return createEntry(path, REGULAR_FILE);
152 }
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 }