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
.dircache
;
40 import java
.util
.Collections
;
42 import org
.spearce
.jgit
.lib
.FileMode
;
43 import org
.spearce
.jgit
.lib
.RepositoryTestCase
;
44 import org
.spearce
.jgit
.treewalk
.TreeWalk
;
45 import org
.spearce
.jgit
.treewalk
.filter
.PathFilterGroup
;
47 public class DirCacheIteratorTest
extends RepositoryTestCase
{
48 public void testEmptyTree_NoTreeWalk() throws Exception
{
49 final DirCache dc
= DirCache
.read(db
);
50 assertEquals(0, dc
.getEntryCount());
52 final DirCacheIterator i
= new DirCacheIterator(dc
);
56 public void testEmptyTree_WithTreeWalk() throws Exception
{
57 final DirCache dc
= DirCache
.read(db
);
58 assertEquals(0, dc
.getEntryCount());
60 final TreeWalk tw
= new TreeWalk(db
);
62 tw
.addTree(new DirCacheIterator(dc
));
63 assertFalse(tw
.next());
66 public void testNoSubtree_NoTreeWalk() throws Exception
{
67 final DirCache dc
= DirCache
.read(db
);
69 final String
[] paths
= { "a.", "a0b" };
70 final DirCacheEntry
[] ents
= new DirCacheEntry
[paths
.length
];
71 for (int i
= 0; i
< paths
.length
; i
++)
72 ents
[i
] = new DirCacheEntry(paths
[i
]);
74 final DirCacheBuilder b
= dc
.builder();
75 for (int i
= 0; i
< ents
.length
; i
++)
79 final DirCacheIterator i
= new DirCacheIterator(dc
);
81 for (; !i
.eof(); i
.next(1)) {
82 assertEquals(pathIdx
, i
.ptr
);
83 assertSame(ents
[pathIdx
], i
.getDirCacheEntry());
86 assertEquals(paths
.length
, pathIdx
);
89 public void testNoSubtree_WithTreeWalk() throws Exception
{
90 final DirCache dc
= DirCache
.read(db
);
92 final String
[] paths
= { "a.", "a0b" };
93 final FileMode
[] modes
= { FileMode
.EXECUTABLE_FILE
, FileMode
.GITLINK
};
94 final DirCacheEntry
[] ents
= new DirCacheEntry
[paths
.length
];
95 for (int i
= 0; i
< paths
.length
; i
++) {
96 ents
[i
] = new DirCacheEntry(paths
[i
]);
97 ents
[i
].setFileMode(modes
[i
]);
100 final DirCacheBuilder b
= dc
.builder();
101 for (int i
= 0; i
< ents
.length
; i
++)
105 final DirCacheIterator i
= new DirCacheIterator(dc
);
106 final TreeWalk tw
= new TreeWalk(db
);
111 assertSame(i
, tw
.getTree(0, DirCacheIterator
.class));
112 assertEquals(pathIdx
, i
.ptr
);
113 assertSame(ents
[pathIdx
], i
.getDirCacheEntry());
114 assertEquals(paths
[pathIdx
], tw
.getPathString());
115 assertEquals(modes
[pathIdx
].getBits(), tw
.getRawMode(0));
116 assertSame(modes
[pathIdx
], tw
.getFileMode(0));
119 assertEquals(paths
.length
, pathIdx
);
122 public void testSingleSubtree_NoRecursion() throws Exception
{
123 final DirCache dc
= DirCache
.read(db
);
125 final String
[] paths
= { "a.", "a/b", "a/c", "a/d", "a0b" };
126 final DirCacheEntry
[] ents
= new DirCacheEntry
[paths
.length
];
127 for (int i
= 0; i
< paths
.length
; i
++) {
128 ents
[i
] = new DirCacheEntry(paths
[i
]);
129 ents
[i
].setFileMode(FileMode
.REGULAR_FILE
);
132 final DirCacheBuilder b
= dc
.builder();
133 for (int i
= 0; i
< ents
.length
; i
++)
137 final String
[] expPaths
= { "a.", "a", "a0b" };
138 final FileMode
[] expModes
= { FileMode
.REGULAR_FILE
, FileMode
.TREE
,
139 FileMode
.REGULAR_FILE
};
140 final int expPos
[] = { 0, -1, 4 };
142 final DirCacheIterator i
= new DirCacheIterator(dc
);
143 final TreeWalk tw
= new TreeWalk(db
);
146 tw
.setRecursive(false);
149 assertSame(i
, tw
.getTree(0, DirCacheIterator
.class));
150 assertEquals(expModes
[pathIdx
].getBits(), tw
.getRawMode(0));
151 assertSame(expModes
[pathIdx
], tw
.getFileMode(0));
152 assertEquals(expPaths
[pathIdx
], tw
.getPathString());
154 if (expPos
[pathIdx
] >= 0) {
155 assertEquals(expPos
[pathIdx
], i
.ptr
);
156 assertSame(ents
[expPos
[pathIdx
]], i
.getDirCacheEntry());
158 assertSame(FileMode
.TREE
, tw
.getFileMode(0));
163 assertEquals(expPaths
.length
, pathIdx
);
166 public void testSingleSubtree_Recursive() throws Exception
{
167 final DirCache dc
= DirCache
.read(db
);
169 final FileMode mode
= FileMode
.REGULAR_FILE
;
170 final String
[] paths
= { "a.", "a/b", "a/c", "a/d", "a0b" };
171 final DirCacheEntry
[] ents
= new DirCacheEntry
[paths
.length
];
172 for (int i
= 0; i
< paths
.length
; i
++) {
173 ents
[i
] = new DirCacheEntry(paths
[i
]);
174 ents
[i
].setFileMode(mode
);
177 final DirCacheBuilder b
= dc
.builder();
178 for (int i
= 0; i
< ents
.length
; i
++)
182 final DirCacheIterator i
= new DirCacheIterator(dc
);
183 final TreeWalk tw
= new TreeWalk(db
);
186 tw
.setRecursive(true);
189 final DirCacheIterator c
= tw
.getTree(0, DirCacheIterator
.class);
191 assertEquals(pathIdx
, c
.ptr
);
192 assertSame(ents
[pathIdx
], c
.getDirCacheEntry());
193 assertEquals(paths
[pathIdx
], tw
.getPathString());
194 assertEquals(mode
.getBits(), tw
.getRawMode(0));
195 assertSame(mode
, tw
.getFileMode(0));
198 assertEquals(paths
.length
, pathIdx
);
201 public void testTwoLevelSubtree_Recursive() throws Exception
{
202 final DirCache dc
= DirCache
.read(db
);
204 final FileMode mode
= FileMode
.REGULAR_FILE
;
205 final String
[] paths
= { "a.", "a/b", "a/c/e", "a/c/f", "a/d", "a0b" };
206 final DirCacheEntry
[] ents
= new DirCacheEntry
[paths
.length
];
207 for (int i
= 0; i
< paths
.length
; i
++) {
208 ents
[i
] = new DirCacheEntry(paths
[i
]);
209 ents
[i
].setFileMode(mode
);
212 final DirCacheBuilder b
= dc
.builder();
213 for (int i
= 0; i
< ents
.length
; i
++)
217 final TreeWalk tw
= new TreeWalk(db
);
219 tw
.addTree(new DirCacheIterator(dc
));
220 tw
.setRecursive(true);
223 final DirCacheIterator c
= tw
.getTree(0, DirCacheIterator
.class);
225 assertEquals(pathIdx
, c
.ptr
);
226 assertSame(ents
[pathIdx
], c
.getDirCacheEntry());
227 assertEquals(paths
[pathIdx
], tw
.getPathString());
228 assertEquals(mode
.getBits(), tw
.getRawMode(0));
229 assertSame(mode
, tw
.getFileMode(0));
232 assertEquals(paths
.length
, pathIdx
);
235 public void testTwoLevelSubtree_FilterPath() throws Exception
{
236 final DirCache dc
= DirCache
.read(db
);
238 final FileMode mode
= FileMode
.REGULAR_FILE
;
239 final String
[] paths
= { "a.", "a/b", "a/c/e", "a/c/f", "a/d", "a0b" };
240 final DirCacheEntry
[] ents
= new DirCacheEntry
[paths
.length
];
241 for (int i
= 0; i
< paths
.length
; i
++) {
242 ents
[i
] = new DirCacheEntry(paths
[i
]);
243 ents
[i
].setFileMode(mode
);
246 final DirCacheBuilder b
= dc
.builder();
247 for (int i
= 0; i
< ents
.length
; i
++)
251 final TreeWalk tw
= new TreeWalk(db
);
252 for (int victimIdx
= 0; victimIdx
< paths
.length
; victimIdx
++) {
254 tw
.addTree(new DirCacheIterator(dc
));
255 tw
.setFilter(PathFilterGroup
.createFromStrings(Collections
256 .singleton(paths
[victimIdx
])));
257 tw
.setRecursive(tw
.getFilter().shouldBeRecursive());
258 assertTrue(tw
.next());
259 final DirCacheIterator c
= tw
.getTree(0, DirCacheIterator
.class);
261 assertEquals(victimIdx
, c
.ptr
);
262 assertSame(ents
[victimIdx
], c
.getDirCacheEntry());
263 assertEquals(paths
[victimIdx
], tw
.getPathString());
264 assertEquals(mode
.getBits(), tw
.getRawMode(0));
265 assertSame(mode
, tw
.getFileMode(0));
266 assertFalse(tw
.next());