View Javadoc
1   /*
2    * Copyright (C) 2008, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.dircache;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertNotNull;
16  import static org.junit.Assert.assertSame;
17  import static org.junit.Assert.assertTrue;
18  
19  import java.util.Collections;
20  
21  import org.eclipse.jgit.junit.RepositoryTestCase;
22  import org.eclipse.jgit.lib.FileMode;
23  import org.eclipse.jgit.treewalk.TreeWalk;
24  import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
25  import org.junit.Test;
26  
27  public class DirCacheBuilderIteratorTest extends RepositoryTestCase {
28  	@Test
29  	public void testPathFilterGroup_DoesNotSkipTail() throws Exception {
30  		final DirCache dc = db.readDirCache();
31  
32  		final FileMode mode = FileMode.REGULAR_FILE;
33  		final String[] paths = { "a-", "a/b", "a/c", "a/d", "a0b" };
34  		final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
35  		for (int i = 0; i < paths.length; i++) {
36  			ents[i] = new DirCacheEntry(paths[i]);
37  			ents[i].setFileMode(mode);
38  		}
39  		{
40  			final DirCacheBuilder b = dc.builder();
41  			for (DirCacheEntry ent : ents) {
42  				b.add(ent);
43  			}
44  			b.finish();
45  		}
46  
47  		final int expIdx = 2;
48  		final DirCacheBuilder b = dc.builder();
49  		try (TreeWalk tw = new TreeWalk(db)) {
50  			tw.addTree(new DirCacheBuildIterator(b));
51  			tw.setRecursive(true);
52  			tw.setFilter(PathFilterGroup.createFromStrings(Collections
53  					.singleton(paths[expIdx])));
54  
55  			assertTrue("found " + paths[expIdx], tw.next());
56  			final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class);
57  			assertNotNull(c);
58  			assertEquals(expIdx, c.ptr);
59  			assertSame(ents[expIdx], c.getDirCacheEntry());
60  			assertEquals(paths[expIdx], tw.getPathString());
61  			assertEquals(mode.getBits(), tw.getRawMode(0));
62  			assertSame(mode, tw.getFileMode(0));
63  			b.add(c.getDirCacheEntry());
64  
65  			assertFalse("no more entries", tw.next());
66  		}
67  
68  		b.finish();
69  		assertEquals(ents.length, dc.getEntryCount());
70  		for (int i = 0; i < ents.length; i++)
71  			assertSame(ents[i], dc.getEntry(i));
72  	}
73  }