View Javadoc
1   /*
2    * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com> 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.internal.storage.file;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertThrows;
16  import static org.junit.Assert.fail;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.IOException;
21  import java.util.Iterator;
22  import java.util.NoSuchElementException;
23  
24  import org.eclipse.jgit.errors.MissingObjectException;
25  import org.eclipse.jgit.internal.JGitText;
26  import org.eclipse.jgit.internal.storage.file.PackIndex.MutableEntry;
27  import org.eclipse.jgit.junit.RepositoryTestCase;
28  import org.junit.Test;
29  
30  public abstract class PackIndexTestCase extends RepositoryTestCase {
31  
32  	PackIndex smallIdx;
33  
34  	PackIndex denseIdx;
35  
36  	@Override
37  	public void setUp() throws Exception {
38  		super.setUp();
39  		smallIdx = PackIndex.open(getFileForPack34be9032());
40  		denseIdx = PackIndex.open(getFileForPackdf2982f28());
41  	}
42  
43  	/**
44  	 * Return file with appropriate index version for prepared pack.
45  	 *
46  	 * @return file with index
47  	 */
48  	public abstract File getFileForPack34be9032();
49  
50  	/**
51  	 * Return file with appropriate index version for prepared pack.
52  	 *
53  	 * @return file with index
54  	 */
55  	public abstract File getFileForPackdf2982f28();
56  
57  	/**
58  	 * Return file with appropriate index version for bad fanout table test.
59  	 *
60  	 * @return file with index
61  	 */
62  	public abstract File getFileForBadFanoutTable();
63  
64  	/**
65  	 * Verify CRC32 support.
66  	 *
67  	 * @throws MissingObjectException
68  	 * @throws UnsupportedOperationException
69  	 */
70  	public abstract void testCRC32() throws MissingObjectException,
71  			UnsupportedOperationException;
72  
73  	/**
74  	 * Test contracts of Iterator methods and this implementation remove()
75  	 * limitations.
76  	 */
77  	@Test
78  	public void testIteratorMethodsContract() {
79  		Iterator<PackIndex.MutableEntry> iter = smallIdx.iterator();
80  		while (iter.hasNext()) {
81  			iter.next();
82  		}
83  
84  		try {
85  			iter.next();
86  			fail("next() unexpectedly returned element");
87  		} catch (NoSuchElementException x) {
88  			// expected
89  		}
90  
91  		try {
92  			iter.remove();
93  			fail("remove() shouldn't be implemented");
94  		} catch (UnsupportedOperationException x) {
95  			// expected
96  		}
97  	}
98  
99  	/**
100 	 * Test results of iterator comparing to content of well-known (prepared)
101 	 * small index.
102 	 */
103 	@Test
104 	public void testIteratorReturnedValues1() {
105 		Iterator<PackIndex.MutableEntry> iter = smallIdx.iterator();
106 		assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", iter.next()
107 				.name());
108 		assertEquals("540a36d136cf413e4b064c2b0e0a4db60f77feab", iter.next()
109 				.name());
110 		assertEquals("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259", iter.next()
111 				.name());
112 		assertEquals("6ff87c4664981e4397625791c8ea3bbb5f2279a3", iter.next()
113 				.name());
114 		assertEquals("82c6b885ff600be425b4ea96dee75dca255b69e7", iter.next()
115 				.name());
116 		assertEquals("902d5476fa249b7abc9d84c611577a81381f0327", iter.next()
117 				.name());
118 		assertEquals("aabf2ffaec9b497f0950352b3e582d73035c2035", iter.next()
119 				.name());
120 		assertEquals("c59759f143fb1fe21c197981df75a7ee00290799", iter.next()
121 				.name());
122 		assertFalse(iter.hasNext());
123 	}
124 
125 	/**
126 	 * Compare offset from iterator entries with output of findOffset() method.
127 	 */
128 	@Test
129 	public void testCompareEntriesOffsetsWithFindOffsets() {
130 		for (MutableEntry me : smallIdx) {
131 			assertEquals(smallIdx.findOffset(me.toObjectId()), me.getOffset());
132 		}
133 		for (MutableEntry me : denseIdx) {
134 			assertEquals(denseIdx.findOffset(me.toObjectId()), me.getOffset());
135 		}
136 	}
137 
138 	/**
139 	 * Compare offset from iterator entries with output of getOffset() method.
140 	 */
141 	@Test
142 	public void testCompareEntriesOffsetsWithGetOffsets() {
143 		int i = 0;
144 		for (MutableEntry me : smallIdx) {
145 			assertEquals(smallIdx.getOffset(i++), me.getOffset());
146 		}
147 		int j = 0;
148 		for (MutableEntry me : denseIdx) {
149 			assertEquals(denseIdx.getOffset(j++), me.getOffset());
150 		}
151 	}
152 
153 	/**
154 	 * Test partial results of iterator comparing to content of well-known
155 	 * (prepared) dense index, that may need multi-level indexing.
156 	 */
157 	@Test
158 	public void testIteratorReturnedValues2() {
159 		Iterator<PackIndex.MutableEntry> iter = denseIdx.iterator();
160 		while (!iter.next().name().equals(
161 				"0a3d7772488b6b106fb62813c4d6d627918d9181")) {
162 			// just iterating
163 		}
164 		assertEquals("1004d0d7ac26fbf63050a234c9b88a46075719d3", iter.next()
165 				.name()); // same level-1
166 		assertEquals("10da5895682013006950e7da534b705252b03be6", iter.next()
167 				.name()); // same level-1
168 		assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8", iter.next()
169 				.name());
170 	}
171 
172 	@Test
173 	public void testBadFanoutTable() {
174 		IOException ex = assertThrows(IOException.class, () -> {
175 			try (FileInputStream fis = new FileInputStream(
176 					getFileForBadFanoutTable())) {
177 				PackIndex.read(fis);
178 			}
179 		});
180 		assertEquals(JGitText.get().indexFileIsTooLargeForJgit,
181 				ex.getMessage());
182 	}
183 }