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 package org.eclipse.jgit.transport;
47
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertNull;
50 import static org.junit.Assert.assertTrue;
51 import static org.junit.Assert.fail;
52
53 import java.io.ByteArrayInputStream;
54 import java.io.ByteArrayOutputStream;
55 import java.io.FileNotFoundException;
56 import java.io.IOException;
57 import java.net.URISyntaxException;
58 import java.util.Collections;
59 import java.util.Set;
60
61 import org.eclipse.jgit.errors.MissingBundlePrerequisiteException;
62 import org.eclipse.jgit.errors.NotSupportedException;
63 import org.eclipse.jgit.errors.TransportException;
64 import org.eclipse.jgit.lib.NullProgressMonitor;
65 import org.eclipse.jgit.lib.ObjectId;
66 import org.eclipse.jgit.lib.Ref;
67 import org.eclipse.jgit.lib.Repository;
68 import org.eclipse.jgit.revwalk.RevCommit;
69 import org.eclipse.jgit.revwalk.RevWalk;
70 import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
71 import org.junit.Test;
72
73 public class BundleWriterTest extends SampleDataRepositoryTestCase {
74
75 @Test
76 public void testWriteSingleRef() throws Exception {
77
78 final byte[] bundle = makeBundle("refs/heads/firstcommit",
79 "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", null);
80
81
82
83 Repository newRepo = createBareRepository();
84 FetchResult fetchResult = fetchFromBundle(newRepo, bundle);
85 Ref advertisedRef = fetchResult
86 .getAdvertisedRef("refs/heads/firstcommit");
87
88
89 assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", advertisedRef
90 .getObjectId().name());
91
92 assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", newRepo
93 .resolve("refs/heads/firstcommit").name());
94 }
95
96 @Test
97 public void testWriteHEAD() throws Exception {
98 byte[] bundle = makeBundle("HEAD",
99 "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", null);
100
101 Repository newRepo = createBareRepository();
102 FetchResult fetchResult = fetchFromBundle(newRepo, bundle);
103 Ref advertisedRef = fetchResult.getAdvertisedRef("HEAD");
104
105 assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", advertisedRef
106 .getObjectId().name());
107 }
108
109 @Test
110 public void testIncrementalBundle() throws Exception {
111 byte[] bundle;
112
113
114 bundle = makeBundle("refs/heads/aa", db.resolve("a").name(), null);
115
116
117
118
119 Repository newRepo = createBareRepository();
120 FetchResult fetchResult = fetchFromBundle(newRepo, bundle);
121 Ref advertisedRef = fetchResult.getAdvertisedRef("refs/heads/aa");
122
123 assertEquals(db.resolve("a").name(), advertisedRef.getObjectId().name());
124 assertEquals(db.resolve("a").name(), newRepo.resolve("refs/heads/aa")
125 .name());
126 assertNull(newRepo.resolve("refs/heads/a"));
127
128
129 bundle = makeBundle("refs/heads/cc", db.resolve("c").name(),
130 new RevWalk(db).parseCommit(db.resolve("a").toObjectId()));
131 fetchResult = fetchFromBundle(newRepo, bundle);
132 advertisedRef = fetchResult.getAdvertisedRef("refs/heads/cc");
133 assertEquals(db.resolve("c").name(), advertisedRef.getObjectId().name());
134 assertEquals(db.resolve("c").name(), newRepo.resolve("refs/heads/cc")
135 .name());
136 assertNull(newRepo.resolve("refs/heads/c"));
137 assertNull(newRepo.resolve("refs/heads/a"));
138
139 try {
140
141 Repository newRepo2 = createBareRepository();
142 fetchResult = fetchFromBundle(newRepo2, bundle);
143 fail("We should not be able to fetch from bundle with prerequisites that are not fulfilled");
144 } catch (MissingBundlePrerequisiteException e) {
145 assertTrue(e.getMessage()
146 .indexOf(db.resolve("refs/heads/a").name()) >= 0);
147 }
148 }
149
150 private static FetchResult fetchFromBundle(final Repository newRepo,
151 final byte[] bundle) throws URISyntaxException,
152 NotSupportedException, TransportException {
153 final URIish uri = new URIish("in-memory://");
154 final ByteArrayInputStream in = new ByteArrayInputStream(bundle);
155 final RefSpec rs = new RefSpec("refs/heads/*:refs/heads/*");
156 final Set<RefSpec> refs = Collections.singleton(rs);
157 return new TransportBundleStream(newRepo, uri, in).fetch(
158 NullProgressMonitor.INSTANCE, refs);
159 }
160
161 private byte[] makeBundle(final String name,
162 final String anObjectToInclude, final RevCommit assume)
163 throws FileNotFoundException, IOException {
164 final BundleWriter bw;
165
166 bw = new BundleWriter(db);
167 bw.include(name, ObjectId.fromString(anObjectToInclude));
168 if (assume != null)
169 bw.assume(assume);
170 final ByteArrayOutputStream out = new ByteArrayOutputStream();
171 bw.writeBundle(NullProgressMonitor.INSTANCE, out);
172 return out.toByteArray();
173 }
174
175 }