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
.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");
71 public void setUp() throws Exception
{
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
)
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();
92 out
.write(Constants
.encode(name
));
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
{
111 assertFalse(ctp
.eof());
112 assertEquals(m644
.getBits(), ctp
.mode
);
113 assertEquals("a", path());
114 assertEquals(hash_a
, ctp
.getEntryObjectId());
117 assertTrue(ctp
.eof());
120 public void testTwoEntries_ForwardOneAtATime() throws Exception
{
123 assertFalse(ctp
.eof());
124 assertEquals(m644
.getBits(), ctp
.mode
);
125 assertEquals("a", path());
126 assertEquals(hash_a
, ctp
.getEntryObjectId());
129 assertFalse(ctp
.eof());
130 assertEquals(m644
.getBits(), ctp
.mode
);
131 assertEquals("foo", path());
132 assertEquals(hash_foo
, ctp
.getEntryObjectId());
135 assertTrue(ctp
.eof());
138 public void testOneEntry_Seek1IsEOF() throws Exception
{
141 assertTrue(ctp
.eof());
144 public void testTwoEntries_Seek2IsEOF() throws Exception
{
147 assertTrue(ctp
.eof());
150 public void testThreeEntries_Seek3IsEOF() throws Exception
{
153 assertTrue(ctp
.eof());
156 public void testThreeEntries_Seek2() throws Exception
{
160 assertFalse(ctp
.eof());
161 assertFalse(ctp
.eof());
162 assertEquals(m644
.getBits(), ctp
.mode
);
163 assertEquals("foo", path());
164 assertEquals(hash_foo
, ctp
.getEntryObjectId());
167 assertTrue(ctp
.eof());
170 public void testOneEntry_Backwards() throws Exception
{
173 assertTrue(ctp
.eof());
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
{
185 assertTrue(ctp
.eof());
188 assertFalse(ctp
.eof());
189 assertEquals(m644
.getBits(), ctp
.mode
);
190 assertEquals("foo", path());
191 assertEquals(hash_foo
, ctp
.getEntryObjectId());
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
{
203 assertTrue(ctp
.eof());
206 assertFalse(ctp
.eof());
207 assertEquals(m644
.getBits(), ctp
.mode
);
208 assertEquals("a", path());
209 assertEquals(hash_a
, ctp
.getEntryObjectId());
212 assertFalse(ctp
.eof());
213 assertEquals(m644
.getBits(), ctp
.mode
);
214 assertEquals("foo", path());
215 assertEquals(hash_foo
, ctp
.getEntryObjectId());
218 assertTrue(ctp
.eof());
221 public void testThreeEntries_BackwardsTwo() throws Exception
{
224 assertTrue(ctp
.eof());
227 assertFalse(ctp
.eof());
228 assertEquals(mt
.getBits(), ctp
.mode
);
229 assertEquals("b_sometree", path());
230 assertEquals(hash_sometree
, ctp
.getEntryObjectId());
233 assertFalse(ctp
.eof());
234 assertEquals(m644
.getBits(), ctp
.mode
);
235 assertEquals("foo", path());
236 assertEquals(hash_foo
, ctp
.getEntryObjectId());
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
)));
247 assertTrue(ctp
.eof());
250 assertFalse(ctp
.eof());
251 assertEquals(mt
.getBits(), ctp
.mode
);
252 assertEquals(aVeryConfusingName
, path());
253 assertEquals(hash_sometree
, ctp
.getEntryObjectId());
256 assertFalse(ctp
.eof());
257 assertEquals(m644
.getBits(), ctp
.mode
);
258 assertEquals("a", path());
259 assertEquals(hash_a
, ctp
.getEntryObjectId());