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