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