Make all AbstractTreeIterator implementations bi-directional
[egit.git] / org.spearce.jgit.test / tst / org / spearce / jgit / treewalk / CanonicalTreeParserTest.java
blobfd92844d7d741bc52730041e9613ca2366862cbb
1 /*
2 * Copyright (C) 2008, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
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
21 * written permission.
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.ByteArrayOutputStream;
42 import junit.framework.TestCase;
44 import org.spearce.jgit.lib.Constants;
45 import org.spearce.jgit.lib.FileMode;
46 import org.spearce.jgit.lib.ObjectId;
47 import org.spearce.jgit.util.RawParseUtils;
49 public class CanonicalTreeParserTest extends TestCase {
50 private final CanonicalTreeParser ctp = new CanonicalTreeParser();
52 private final FileMode m644 = FileMode.REGULAR_FILE;
54 private final FileMode mt = FileMode.TREE;
56 private final ObjectId hash_a = ObjectId
57 .fromString("6b9c715d21d5486e59083fb6071566aa6ecd4d42");
59 private final ObjectId hash_foo = ObjectId
60 .fromString("a213e8e25bb2442326e86cbfb9ef56319f482869");
62 private final ObjectId hash_sometree = ObjectId
63 .fromString("daf4bdb0d7bb24319810fe0e73aa317663448c93");
65 private byte[] tree1;
67 private byte[] tree2;
69 private byte[] tree3;
71 public void setUp() throws Exception {
72 super.setUp();
74 tree1 = mkree(entry(m644, "a", hash_a));
75 tree2 = mkree(entry(m644, "a", hash_a), entry(m644, "foo", hash_foo));
76 tree3 = mkree(entry(m644, "a", hash_a), entry(mt, "b_sometree",
77 hash_sometree), entry(m644, "foo", hash_foo));
80 private static byte[] mkree(final byte[]... data) throws Exception {
81 final ByteArrayOutputStream out = new ByteArrayOutputStream();
82 for (final byte[] e : data)
83 out.write(e);
84 return out.toByteArray();
87 private static byte[] entry(final FileMode mode, final String name,
88 final ObjectId id) throws Exception {
89 final ByteArrayOutputStream out = new ByteArrayOutputStream();
90 mode.copyTo(out);
91 out.write(' ');
92 out.write(Constants.encode(name));
93 out.write(0);
94 id.copyRawTo(out);
95 return out.toByteArray();
98 private String path() {
99 return RawParseUtils.decode(Constants.CHARSET, ctp.path,
100 ctp.pathOffset, ctp.pathLen);
103 public void testEmptyTree_AtEOF() throws Exception {
104 ctp.reset(new byte[0]);
105 assertTrue(ctp.eof());
108 public void testOneEntry_Forward() throws Exception {
109 ctp.reset(tree1);
111 assertFalse(ctp.eof());
112 assertEquals(m644.getBits(), ctp.mode);
113 assertEquals("a", path());
114 assertEquals(hash_a, ctp.getEntryObjectId());
116 ctp.next(1);
117 assertTrue(ctp.eof());
120 public void testTwoEntries_ForwardOneAtATime() throws Exception {
121 ctp.reset(tree2);
123 assertFalse(ctp.eof());
124 assertEquals(m644.getBits(), ctp.mode);
125 assertEquals("a", path());
126 assertEquals(hash_a, ctp.getEntryObjectId());
128 ctp.next(1);
129 assertFalse(ctp.eof());
130 assertEquals(m644.getBits(), ctp.mode);
131 assertEquals("foo", path());
132 assertEquals(hash_foo, ctp.getEntryObjectId());
134 ctp.next(1);
135 assertTrue(ctp.eof());
138 public void testOneEntry_Seek1IsEOF() throws Exception {
139 ctp.reset(tree1);
140 ctp.next(1);
141 assertTrue(ctp.eof());
144 public void testTwoEntries_Seek2IsEOF() throws Exception {
145 ctp.reset(tree2);
146 ctp.next(2);
147 assertTrue(ctp.eof());
150 public void testThreeEntries_Seek3IsEOF() throws Exception {
151 ctp.reset(tree3);
152 ctp.next(3);
153 assertTrue(ctp.eof());
156 public void testThreeEntries_Seek2() throws Exception {
157 ctp.reset(tree3);
159 ctp.next(2);
160 assertFalse(ctp.eof());
161 assertFalse(ctp.eof());
162 assertEquals(m644.getBits(), ctp.mode);
163 assertEquals("foo", path());
164 assertEquals(hash_foo, ctp.getEntryObjectId());
166 ctp.next(1);
167 assertTrue(ctp.eof());
170 public void testOneEntry_Backwards() throws Exception {
171 ctp.reset(tree1);
172 ctp.next(1);
173 assertTrue(ctp.eof());
175 ctp.back(1);
176 assertFalse(ctp.eof());
177 assertEquals(m644.getBits(), ctp.mode);
178 assertEquals("a", path());
179 assertEquals(hash_a, ctp.getEntryObjectId());
182 public void testTwoEntries_BackwardsOneAtATime() throws Exception {
183 ctp.reset(tree2);
184 ctp.next(2);
185 assertTrue(ctp.eof());
187 ctp.back(1);
188 assertFalse(ctp.eof());
189 assertEquals(m644.getBits(), ctp.mode);
190 assertEquals("foo", path());
191 assertEquals(hash_foo, ctp.getEntryObjectId());
193 ctp.back(1);
194 assertFalse(ctp.eof());
195 assertEquals(m644.getBits(), ctp.mode);
196 assertEquals("a", path());
197 assertEquals(hash_a, ctp.getEntryObjectId());
200 public void testTwoEntries_BackwardsTwo() throws Exception {
201 ctp.reset(tree2);
202 ctp.next(2);
203 assertTrue(ctp.eof());
205 ctp.back(2);
206 assertFalse(ctp.eof());
207 assertEquals(m644.getBits(), ctp.mode);
208 assertEquals("a", path());
209 assertEquals(hash_a, ctp.getEntryObjectId());
211 ctp.next(1);
212 assertFalse(ctp.eof());
213 assertEquals(m644.getBits(), ctp.mode);
214 assertEquals("foo", path());
215 assertEquals(hash_foo, ctp.getEntryObjectId());
217 ctp.next(1);
218 assertTrue(ctp.eof());
221 public void testThreeEntries_BackwardsTwo() throws Exception {
222 ctp.reset(tree3);
223 ctp.next(3);
224 assertTrue(ctp.eof());
226 ctp.back(2);
227 assertFalse(ctp.eof());
228 assertEquals(mt.getBits(), ctp.mode);
229 assertEquals("b_sometree", path());
230 assertEquals(hash_sometree, ctp.getEntryObjectId());
232 ctp.next(1);
233 assertFalse(ctp.eof());
234 assertEquals(m644.getBits(), ctp.mode);
235 assertEquals("foo", path());
236 assertEquals(hash_foo, ctp.getEntryObjectId());
238 ctp.next(1);
239 assertTrue(ctp.eof());
242 public void testBackwards_ConfusingPathName() throws Exception {
243 final String aVeryConfusingName = "confusing 644 entry 755 and others";
244 ctp.reset(mkree(entry(m644, "a", hash_a), entry(mt, aVeryConfusingName,
245 hash_sometree), entry(m644, "foo", hash_foo)));
246 ctp.next(3);
247 assertTrue(ctp.eof());
249 ctp.back(2);
250 assertFalse(ctp.eof());
251 assertEquals(mt.getBits(), ctp.mode);
252 assertEquals(aVeryConfusingName, path());
253 assertEquals(hash_sometree, ctp.getEntryObjectId());
255 ctp.back(1);
256 assertFalse(ctp.eof());
257 assertEquals(m644.getBits(), ctp.mode);
258 assertEquals("a", path());
259 assertEquals(hash_a, ctp.getEntryObjectId());