2 * Copyright (C) 2008, Google Inc.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.treewalk
;
40 import java
.io
.ByteArrayInputStream
;
42 import org
.spearce
.jgit
.dircache
.DirCache
;
43 import org
.spearce
.jgit
.dircache
.DirCacheBuilder
;
44 import org
.spearce
.jgit
.dircache
.DirCacheEntry
;
45 import org
.spearce
.jgit
.dircache
.DirCacheIterator
;
46 import org
.spearce
.jgit
.lib
.Constants
;
47 import org
.spearce
.jgit
.lib
.FileMode
;
48 import org
.spearce
.jgit
.lib
.ObjectWriter
;
49 import org
.spearce
.jgit
.lib
.RepositoryTestCase
;
51 import static org
.spearce
.jgit
.lib
.FileMode
.REGULAR_FILE
;
52 import static org
.spearce
.jgit
.lib
.FileMode
.TREE
;
54 public class PostOrderTreeWalkTest
extends RepositoryTestCase
{
55 public void testInitialize_NoPostOrder() throws Exception
{
56 final TreeWalk tw
= new TreeWalk(db
);
57 assertFalse(tw
.isPostOrderTraversal());
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());
69 public void testResetDoesNotAffectPostOrder() throws Exception
{
70 final TreeWalk tw
= new TreeWalk(db
);
71 tw
.setPostOrderTraversal(true);
72 assertTrue(tw
.isPostOrderTraversal());
74 assertTrue(tw
.isPostOrderTraversal());
76 tw
.setPostOrderTraversal(false);
77 assertFalse(tw
.isPostOrderTraversal());
79 assertFalse(tw
.isPostOrderTraversal());
82 public void testNoPostOrder() throws Exception
{
83 final DirCache tree
= DirCache
.read(db
);
85 final DirCacheBuilder b
= tree
.builder();
88 b
.add(makeFile("b/c"));
89 b
.add(makeFile("b/d"));
93 assertEquals(4, tree
.getEntryCount());
96 final TreeWalk tw
= new TreeWalk(db
);
98 tw
.setPostOrderTraversal(false);
99 tw
.addTree(new DirCacheIterator(tree
));
101 assertModes("a", REGULAR_FILE
, tw
);
102 assertModes("b", TREE
, tw
);
103 assertTrue(tw
.isSubtree());
104 assertFalse(tw
.isPostChildren());
106 assertModes("b/c", REGULAR_FILE
, tw
);
107 assertModes("b/d", REGULAR_FILE
, tw
);
108 assertModes("q", REGULAR_FILE
, tw
);
111 public void testWithPostOrder_EnterSubtree() throws Exception
{
112 final DirCache tree
= DirCache
.read(db
);
114 final DirCacheBuilder b
= tree
.builder();
116 b
.add(makeFile("a"));
117 b
.add(makeFile("b/c"));
118 b
.add(makeFile("b/d"));
119 b
.add(makeFile("q"));
122 assertEquals(4, tree
.getEntryCount());
125 final TreeWalk tw
= new TreeWalk(db
);
127 tw
.setPostOrderTraversal(true);
128 tw
.addTree(new DirCacheIterator(tree
));
130 assertModes("a", REGULAR_FILE
, tw
);
132 assertModes("b", TREE
, tw
);
133 assertTrue(tw
.isSubtree());
134 assertFalse(tw
.isPostChildren());
136 assertModes("b/c", REGULAR_FILE
, tw
);
137 assertModes("b/d", REGULAR_FILE
, tw
);
139 assertModes("b", TREE
, tw
);
140 assertTrue(tw
.isSubtree());
141 assertTrue(tw
.isPostChildren());
143 assertModes("q", REGULAR_FILE
, tw
);
146 public void testWithPostOrder_NoEnterSubtree() throws Exception
{
147 final DirCache tree
= DirCache
.read(db
);
149 final DirCacheBuilder b
= tree
.builder();
151 b
.add(makeFile("a"));
152 b
.add(makeFile("b/c"));
153 b
.add(makeFile("b/d"));
154 b
.add(makeFile("q"));
157 assertEquals(4, tree
.getEntryCount());
160 final TreeWalk tw
= new TreeWalk(db
);
162 tw
.setPostOrderTraversal(true);
163 tw
.addTree(new DirCacheIterator(tree
));
165 assertModes("a", REGULAR_FILE
, tw
);
167 assertModes("b", TREE
, tw
);
168 assertTrue(tw
.isSubtree());
169 assertFalse(tw
.isPostChildren());
171 assertModes("q", REGULAR_FILE
, tw
);
174 private DirCacheEntry
makeFile(final String path
) throws Exception
{
175 final byte[] pathBytes
= Constants
.encode(path
);
176 final DirCacheEntry ent
= new DirCacheEntry(path
);
177 ent
.setFileMode(REGULAR_FILE
);
178 ent
.setObjectId(new ObjectWriter(db
).computeBlobSha1(pathBytes
.length
,
179 new ByteArrayInputStream(pathBytes
)));
183 private static void assertModes(final String path
, final FileMode mode0
,
184 final TreeWalk tw
) throws Exception
{
185 assertTrue("has " + path
, tw
.next());
186 assertEquals(path
, tw
.getPathString());
187 assertEquals(mode0
, tw
.getFileMode(0));