View Javadoc
1   /*
2    * Copyright (C) 2008, Imran M Yousuf <imyousuf@smartitengineering.com>
3    * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.internal.storage.file;
13  
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertNull;
16  import static org.junit.Assert.assertTrue;
17  import static org.junit.Assert.fail;
18  
19  import org.eclipse.jgit.errors.CorruptObjectException;
20  import org.eclipse.jgit.internal.storage.file.PackIndex.MutableEntry;
21  import org.eclipse.jgit.junit.JGitTestUtil;
22  import org.eclipse.jgit.junit.RepositoryTestCase;
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  public class PackReverseIndexTest extends RepositoryTestCase {
27  
28  	private PackIndex idx;
29  
30  	private PackReverseIndex reverseIdx;
31  
32  	/**
33  	 * Set up tested class instance, test constructor by the way.
34  	 */
35  	@Override
36  	@Before
37  	public void setUp() throws Exception {
38  		super.setUp();
39  		// index with both small (< 2^31) and big offsets
40  		idx = PackIndex.open(JGitTestUtil.getTestResourceFile(
41  				"pack-huge.idx"));
42  		reverseIdx = new PackReverseIndex(idx);
43  	}
44  
45  	/**
46  	 * Test findObject() for all index entries.
47  	 */
48  	@Test
49  	public void testFindObject() {
50  		for (MutableEntry me : idx)
51  			assertEquals(me.toObjectId(), reverseIdx.findObject(me.getOffset()));
52  	}
53  
54  	/**
55  	 * Test findObject() with illegal argument.
56  	 */
57  	@Test
58  	public void testFindObjectWrongOffset() {
59  		assertNull(reverseIdx.findObject(0));
60  	}
61  
62  	/**
63  	 * Test findNextOffset() for all index entries.
64  	 *
65  	 * @throws CorruptObjectException
66  	 */
67  	@Test
68  	public void testFindNextOffset() throws CorruptObjectException {
69  		long offset = findFirstOffset();
70  		assertTrue(offset > 0);
71  		for (int i = 0; i < idx.getObjectCount(); i++) {
72  			long newOffset = reverseIdx.findNextOffset(offset, Long.MAX_VALUE);
73  			assertTrue(newOffset > offset);
74  			if (i == idx.getObjectCount() - 1)
75  				assertEquals(newOffset, Long.MAX_VALUE);
76  			else
77  				assertEquals(newOffset, idx.findOffset(reverseIdx
78  						.findObject(newOffset)));
79  			offset = newOffset;
80  		}
81  	}
82  
83  	/**
84  	 * Test findNextOffset() with wrong illegal argument as offset.
85  	 */
86  	@Test
87  	public void testFindNextOffsetWrongOffset() {
88  		try {
89  			reverseIdx.findNextOffset(0, Long.MAX_VALUE);
90  			fail("findNextOffset() should throw exception");
91  		} catch (CorruptObjectException x) {
92  			// expected
93  		}
94  	}
95  
96  	private long findFirstOffset() {
97  		long min = Long.MAX_VALUE;
98  		for (MutableEntry me : idx)
99  			min = Math.min(min, me.getOffset());
100 		return min;
101 	}
102 }