View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2009, Tor Arne Vestbø <torarnv@gmail.com>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }