View Javadoc
1   /*
2    * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.internal.storage.file;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertFalse;
48  import static org.junit.Assert.fail;
49  
50  import java.io.File;
51  import java.util.Iterator;
52  import java.util.NoSuchElementException;
53  
54  import org.eclipse.jgit.errors.MissingObjectException;
55  import org.eclipse.jgit.internal.storage.file.PackIndex.MutableEntry;
56  import org.eclipse.jgit.junit.RepositoryTestCase;
57  import org.junit.Test;
58  
59  public abstract class PackIndexTestCase extends RepositoryTestCase {
60  
61  	PackIndex smallIdx;
62  
63  	PackIndex denseIdx;
64  
65  	@Override
66  	public void setUp() throws Exception {
67  		super.setUp();
68  		smallIdx = PackIndex.open(getFileForPack34be9032());
69  		denseIdx = PackIndex.open(getFileForPackdf2982f28());
70  	}
71  
72  	/**
73  	 * Return file with appropriate index version for prepared pack.
74  	 *
75  	 * @return file with index
76  	 */
77  	public abstract File getFileForPack34be9032();
78  
79  	/**
80  	 * Return file with appropriate index version for prepared pack.
81  	 *
82  	 * @return file with index
83  	 */
84  	public abstract File getFileForPackdf2982f28();
85  
86  	/**
87  	 * Verify CRC32 support.
88  	 *
89  	 * @throws MissingObjectException
90  	 * @throws UnsupportedOperationException
91  	 */
92  	public abstract void testCRC32() throws MissingObjectException,
93  			UnsupportedOperationException;
94  
95  	/**
96  	 * Test contracts of Iterator methods and this implementation remove()
97  	 * limitations.
98  	 */
99  	@Test
100 	public void testIteratorMethodsContract() {
101 		Iterator<PackIndex.MutableEntry> iter = smallIdx.iterator();
102 		while (iter.hasNext()) {
103 			iter.next();
104 		}
105 
106 		try {
107 			iter.next();
108 			fail("next() unexpectedly returned element");
109 		} catch (NoSuchElementException x) {
110 			// expected
111 		}
112 
113 		try {
114 			iter.remove();
115 			fail("remove() shouldn't be implemented");
116 		} catch (UnsupportedOperationException x) {
117 			// expected
118 		}
119 	}
120 
121 	/**
122 	 * Test results of iterator comparing to content of well-known (prepared)
123 	 * small index.
124 	 */
125 	@Test
126 	public void testIteratorReturnedValues1() {
127 		Iterator<PackIndex.MutableEntry> iter = smallIdx.iterator();
128 		assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", iter.next()
129 				.name());
130 		assertEquals("540a36d136cf413e4b064c2b0e0a4db60f77feab", iter.next()
131 				.name());
132 		assertEquals("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259", iter.next()
133 				.name());
134 		assertEquals("6ff87c4664981e4397625791c8ea3bbb5f2279a3", iter.next()
135 				.name());
136 		assertEquals("82c6b885ff600be425b4ea96dee75dca255b69e7", iter.next()
137 				.name());
138 		assertEquals("902d5476fa249b7abc9d84c611577a81381f0327", iter.next()
139 				.name());
140 		assertEquals("aabf2ffaec9b497f0950352b3e582d73035c2035", iter.next()
141 				.name());
142 		assertEquals("c59759f143fb1fe21c197981df75a7ee00290799", iter.next()
143 				.name());
144 		assertFalse(iter.hasNext());
145 	}
146 
147 	/**
148 	 * Compare offset from iterator entries with output of findOffset() method.
149 	 */
150 	@Test
151 	public void testCompareEntriesOffsetsWithFindOffsets() {
152 		for (MutableEntry me : smallIdx) {
153 			assertEquals(smallIdx.findOffset(me.toObjectId()), me.getOffset());
154 		}
155 		for (MutableEntry me : denseIdx) {
156 			assertEquals(denseIdx.findOffset(me.toObjectId()), me.getOffset());
157 		}
158 	}
159 
160 	/**
161 	 * Compare offset from iterator entries with output of getOffset() method.
162 	 */
163 	@Test
164 	public void testCompareEntriesOffsetsWithGetOffsets() {
165 		int i = 0;
166 		for (MutableEntry me : smallIdx) {
167 			assertEquals(smallIdx.getOffset(i++), me.getOffset());
168 		}
169 		int j = 0;
170 		for (MutableEntry me : denseIdx) {
171 			assertEquals(denseIdx.getOffset(j++), me.getOffset());
172 		}
173 	}
174 
175 	/**
176 	 * Test partial results of iterator comparing to content of well-known
177 	 * (prepared) dense index, that may need multi-level indexing.
178 	 */
179 	@Test
180 	public void testIteratorReturnedValues2() {
181 		Iterator<PackIndex.MutableEntry> iter = denseIdx.iterator();
182 		while (!iter.next().name().equals(
183 				"0a3d7772488b6b106fb62813c4d6d627918d9181")) {
184 			// just iterating
185 		}
186 		assertEquals("1004d0d7ac26fbf63050a234c9b88a46075719d3", iter.next()
187 				.name()); // same level-1
188 		assertEquals("10da5895682013006950e7da534b705252b03be6", iter.next()
189 				.name()); // same level-1
190 		assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8", iter.next()
191 				.name());
192 	}
193 
194 }