1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 package org.eclipse.jgit.treewalk;
46
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertNotNull;
49 import static org.junit.Assert.assertNotSame;
50 import static org.junit.Assert.assertSame;
51 import static org.junit.Assert.assertTrue;
52
53 import java.io.IOException;
54
55 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
56 import org.eclipse.jgit.lib.Config;
57 import org.eclipse.jgit.lib.Constants;
58 import org.eclipse.jgit.lib.FileMode;
59 import org.eclipse.jgit.lib.ObjectReader;
60 import org.junit.Test;
61
62
63 public class AbstractTreeIteratorTest {
64 private static String prefix(String path) {
65 final int s = path.lastIndexOf('/');
66 return s > 0 ? path.substring(0, s) : "";
67 }
68
69 public class FakeTreeIterator extends WorkingTreeIterator {
70 public FakeTreeIterator(String pathName, FileMode fileMode) {
71 super(prefix(pathName), new Config().get(WorkingTreeOptions.KEY));
72 mode = fileMode.getBits();
73
74 final int s = pathName.lastIndexOf('/');
75 final byte[] name = Constants.encode(pathName.substring(s + 1));
76 ensurePathCapacity(pathOffset + name.length, pathOffset);
77 System.arraycopy(name, 0, path, pathOffset, name.length);
78 pathLen = pathOffset + name.length;
79 }
80
81 @Override
82 public AbstractTreeIterator createSubtreeIterator(ObjectReader reader)
83 throws IncorrectObjectTypeException, IOException {
84 return null;
85 }
86 }
87
88 @Test
89 public void testPathCompare() throws Exception {
90 assertTrue(new FakeTreeIterator("a", FileMode.REGULAR_FILE).pathCompare(
91 new FakeTreeIterator("a", FileMode.TREE)) < 0);
92
93 assertTrue(new FakeTreeIterator("a", FileMode.TREE).pathCompare(
94 new FakeTreeIterator("a", FileMode.REGULAR_FILE)) > 0);
95
96 assertTrue(new FakeTreeIterator("a", FileMode.REGULAR_FILE).pathCompare(
97 new FakeTreeIterator("a", FileMode.REGULAR_FILE)) == 0);
98
99 assertTrue(new FakeTreeIterator("a", FileMode.TREE).pathCompare(
100 new FakeTreeIterator("a", FileMode.TREE)) == 0);
101 }
102
103 @Test
104 public void testGrowPath() throws Exception {
105 final FakeTreeIterator i = new FakeTreeIterator("ab", FileMode.TREE);
106 final byte[] origpath = i.path;
107 assertEquals(i.path[0], 'a');
108 assertEquals(i.path[1], 'b');
109
110 i.growPath(2);
111
112 assertNotSame(origpath, i.path);
113 assertEquals(origpath.length * 2, i.path.length);
114 assertEquals(i.path[0], 'a');
115 assertEquals(i.path[1], 'b');
116 }
117
118 @Test
119 public void testEnsurePathCapacityFastCase() throws Exception {
120 final FakeTreeIterator i = new FakeTreeIterator("ab", FileMode.TREE);
121 final int want = 50;
122 final byte[] origpath = i.path;
123 assertEquals(i.path[0], 'a');
124 assertEquals(i.path[1], 'b');
125 assertTrue(want < i.path.length);
126
127 i.ensurePathCapacity(want, 2);
128
129 assertSame(origpath, i.path);
130 assertEquals(i.path[0], 'a');
131 assertEquals(i.path[1], 'b');
132 }
133
134 @Test
135 public void testEnsurePathCapacityGrows() throws Exception {
136 final FakeTreeIterator i = new FakeTreeIterator("ab", FileMode.TREE);
137 final int want = 384;
138 final byte[] origpath = i.path;
139 assertEquals(i.path[0], 'a');
140 assertEquals(i.path[1], 'b');
141 assertTrue(i.path.length < want);
142
143 i.ensurePathCapacity(want, 2);
144
145 assertNotSame(origpath, i.path);
146 assertEquals(512, i.path.length);
147 assertEquals(i.path[0], 'a');
148 assertEquals(i.path[1], 'b');
149 }
150
151 @Test
152 public void testEntryFileMode() {
153 for (FileMode m : new FileMode[] { FileMode.TREE,
154 FileMode.REGULAR_FILE, FileMode.EXECUTABLE_FILE,
155 FileMode.GITLINK, FileMode.SYMLINK }) {
156 final FakeTreeIterator i = new FakeTreeIterator("a", m);
157 assertEquals(m.getBits(), i.getEntryRawMode());
158 assertSame(m, i.getEntryFileMode());
159 }
160 }
161
162 @Test
163 public void testEntryPath() {
164 FakeTreeIterator i = new FakeTreeIterator("a/b/cd", FileMode.TREE);
165 assertEquals("a/b/cd", i.getEntryPathString());
166 assertEquals(2, i.getNameLength());
167 byte[] b = new byte[3];
168 b[0] = 0x0a;
169 i.getName(b, 1);
170 assertEquals(0x0a, b[0]);
171 assertEquals('c', b[1]);
172 assertEquals('d', b[2]);
173 }
174
175 @Test
176 public void testCreateEmptyTreeIterator() {
177 FakeTreeIterator i = new FakeTreeIterator("a/b/cd", FileMode.TREE);
178 EmptyTreeIterator e = i.createEmptyTreeIterator();
179 assertNotNull(e);
180 assertEquals(i.getEntryPathString() + "/", e.getEntryPathString());
181 }
182 }