aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/midx/MultiPackIndexLoaderTest.java
blob: 494f1d1137dd3a504e1adb4138336ab1b66e1dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (C) 2024, GerritForge Inc. and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.internal.storage.midx;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.eclipse.jgit.internal.storage.file.PackIndex;
import org.eclipse.jgit.junit.FakeIndexFactory;
import org.eclipse.jgit.junit.JGitTestUtil;
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.junit.Test;

/**
 * Test that the loader accepts valid files, discard broken files
 * <p>
 * Contents and lookups are covered in the MultiPackIndexTest
 */
public class MultiPackIndexLoaderTest {

	@Test
	public void load_validFile_basic_upstream() throws Exception {
		MultiPackIndex midx = MultiPackIndexLoader
				.open(JGitTestUtil.getTestResourceFile("multi-pack-index.v1"));
		assertNotNull(midx);
	}

	@Test
	public void load_validFile_basic_jgit() throws Exception {
		PackIndex idxOne = FakeIndexFactory.indexOf(List.of(
				new FakeIndexFactory.IndexObject(
						"0000000000000000000000000000000000000001", 500),
				new FakeIndexFactory.IndexObject(
						"0000000000000000000000000000000000000005", 12),
				new FakeIndexFactory.IndexObject(
						"0000000000000000000000000000000000000010", 1500)));
		PackIndex idxTwo = FakeIndexFactory.indexOf(List.of(
				new FakeIndexFactory.IndexObject(
						"0000000000000000000000000000000000000002", 501),
				new FakeIndexFactory.IndexObject(
						"0000000000000000000000000000000000000003", 13),
				new FakeIndexFactory.IndexObject(
						"0000000000000000000000000000000000000015", 1501)));
		PackIndex idxThree = FakeIndexFactory.indexOf(List.of(
				new FakeIndexFactory.IndexObject(
						"0000000000000000000000000000000000000004", 502),
				new FakeIndexFactory.IndexObject(
						"0000000000000000000000000000000000000007", 14),
				new FakeIndexFactory.IndexObject(
						"0000000000000000000000000000000000000012", 1502)));

		Map<String, PackIndex> packs = Map.of("p1", idxOne, "p2", idxTwo, "p3",
				idxThree);
		MultiPackIndexWriter writer = new MultiPackIndexWriter();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		writer.write(NullProgressMonitor.INSTANCE, out, packs);

		MultiPackIndex midx = MultiPackIndexLoader
				.read(new ByteArrayInputStream(out.toByteArray()));
		assertNotNull(midx);
	}

	@Test
	public void load_emptyFile() {
		assertThrows(IOException.class, () -> MultiPackIndexLoader
				.read(new ByteArrayInputStream(new byte[0])));
	}

	@Test
	public void load_rubbishFile() {
		assertThrows(MultiPackIndexLoader.MultiPackIndexFormatException.class,
				() -> MultiPackIndexLoader.read(new ByteArrayInputStream(
						"More than 12 bytes of not-midx".getBytes(UTF_8))));
	}
}