1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.internal.storage.dfs;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.ByteArrayInputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.IOException;
19 import java.util.Collections;
20 import java.util.Set;
21
22 import org.eclipse.jgit.junit.TestRepository;
23 import org.eclipse.jgit.lib.NullProgressMonitor;
24 import org.eclipse.jgit.lib.Ref;
25 import org.eclipse.jgit.lib.Repository;
26 import org.eclipse.jgit.revwalk.RevCommit;
27 import org.eclipse.jgit.transport.FetchResult;
28 import org.eclipse.jgit.transport.RefSpec;
29 import org.eclipse.jgit.transport.TransportBundleStream;
30 import org.eclipse.jgit.transport.URIish;
31 import org.junit.Before;
32 import org.junit.Test;
33
34 public class DfsBundleWriterTest {
35 private TestRepository<InMemoryRepository> git;
36
37 private InMemoryRepository repo;
38
39 @Before
40 public void setUp() throws IOException {
41 DfsRepositoryDescription desc = new DfsRepositoryDescription("test");
42 git = new TestRepository<>(new InMemoryRepository(desc));
43 repo = git.getRepository();
44 }
45
46 @Test
47 public void testRepo() throws Exception {
48 RevCommit commit0 = git.commit().message("0").create();
49 RevCommit commit1 = git.commit().message("1").parent(commit0).create();
50 git.update("master", commit1);
51
52 RevCommit commit2 = git.commit().message("0").create();
53
54 byte[] bundle = makeBundle();
55 try (Repository newRepo = new InMemoryRepository(
56 new DfsRepositoryDescription("copy"))) {
57 fetchFromBundle(newRepo, bundle);
58 Ref ref = newRepo.exactRef("refs/heads/master");
59 assertNotNull(ref);
60 assertEquals(commit1.toObjectId(), ref.getObjectId());
61
62
63 assertTrue(newRepo.getObjectDatabase().has(commit2));
64 }
65 }
66
67 private byte[] makeBundle() throws IOException {
68 ByteArrayOutputStream out = new ByteArrayOutputStream();
69 DfsBundleWriter.writeEntireRepositoryAsBundle(
70 NullProgressMonitor.INSTANCE, out, repo);
71 return out.toByteArray();
72 }
73
74 private static FetchResult fetchFromBundle(Repository newRepo,
75 byte[] bundle) throws Exception {
76 URIish uri = new URIish("in-memory://");
77 ByteArrayInputStream in = new ByteArrayInputStream(bundle);
78 RefSpec rs = new RefSpec("refs/heads/*:refs/heads/*");
79 Set<RefSpec> refs = Collections.singleton(rs);
80 try (TransportBundleStream transport = new TransportBundleStream(
81 newRepo, uri, in)) {
82 return transport.fetch(NullProgressMonitor.INSTANCE, refs);
83 }
84 }
85 }