You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BundleWriterTest.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Mike Ralphson <mike@abacus.co.uk>
  4. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.transport;
  46. import static java.nio.charset.StandardCharsets.UTF_8;
  47. import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
  48. import static org.junit.Assert.assertEquals;
  49. import static org.junit.Assert.assertNotNull;
  50. import static org.junit.Assert.assertNull;
  51. import static org.junit.Assert.assertTrue;
  52. import static org.junit.Assert.fail;
  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. import org.eclipse.jgit.errors.MissingBundlePrerequisiteException;
  61. import org.eclipse.jgit.errors.MissingObjectException;
  62. import org.eclipse.jgit.errors.NotSupportedException;
  63. import org.eclipse.jgit.errors.TransportException;
  64. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  65. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  66. import org.eclipse.jgit.lib.Constants;
  67. import org.eclipse.jgit.lib.NullProgressMonitor;
  68. import org.eclipse.jgit.lib.ObjectId;
  69. import org.eclipse.jgit.lib.ObjectInserter;
  70. import org.eclipse.jgit.lib.ObjectReader;
  71. import org.eclipse.jgit.lib.Ref;
  72. import org.eclipse.jgit.lib.Repository;
  73. import org.eclipse.jgit.revwalk.RevCommit;
  74. import org.eclipse.jgit.revwalk.RevWalk;
  75. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  76. import org.junit.Test;
  77. public class BundleWriterTest extends SampleDataRepositoryTestCase {
  78. @Test
  79. public void testWriteSingleRef() throws Exception {
  80. // Create a tiny bundle, (well one of) the first commits only
  81. final byte[] bundle = makeBundle("refs/heads/firstcommit",
  82. "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", null);
  83. // Then we clone a new repo from that bundle and do a simple test. This
  84. // makes sure we could read the bundle we created.
  85. Repository newRepo = createBareRepository();
  86. FetchResult fetchResult = fetchFromBundle(newRepo, bundle);
  87. Ref advertisedRef = fetchResult
  88. .getAdvertisedRef("refs/heads/firstcommit");
  89. // We expect first commit to appear by id
  90. assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", advertisedRef
  91. .getObjectId().name());
  92. // ..and by name as the bundle created a new ref
  93. assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", newRepo
  94. .resolve("refs/heads/firstcommit").name());
  95. }
  96. @Test
  97. public void testWriteHEAD() throws Exception {
  98. byte[] bundle = makeBundle("HEAD",
  99. "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", null);
  100. Repository newRepo = createBareRepository();
  101. FetchResult fetchResult = fetchFromBundle(newRepo, bundle);
  102. Ref advertisedRef = fetchResult.getAdvertisedRef("HEAD");
  103. assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", advertisedRef
  104. .getObjectId().name());
  105. }
  106. @Test
  107. public void testIncrementalBundle() throws Exception {
  108. byte[] bundle;
  109. // Create a small bundle, an early commit
  110. bundle = makeBundle("refs/heads/aa", db.resolve("a").name(), null);
  111. // Then we clone a new repo from that bundle and do a simple test. This
  112. // makes sure
  113. // we could read the bundle we created.
  114. Repository newRepo = createBareRepository();
  115. FetchResult fetchResult = fetchFromBundle(newRepo, bundle);
  116. Ref advertisedRef = fetchResult.getAdvertisedRef("refs/heads/aa");
  117. assertEquals(db.resolve("a").name(), advertisedRef.getObjectId().name());
  118. assertEquals(db.resolve("a").name(), newRepo.resolve("refs/heads/aa")
  119. .name());
  120. assertNull(newRepo.resolve("refs/heads/a"));
  121. // Next an incremental bundle
  122. try (RevWalk rw = new RevWalk(db)) {
  123. bundle = makeBundle("refs/heads/cc", db.resolve("c").name(),
  124. rw.parseCommit(db.resolve("a").toObjectId()));
  125. fetchResult = fetchFromBundle(newRepo, bundle);
  126. advertisedRef = fetchResult.getAdvertisedRef("refs/heads/cc");
  127. assertEquals(db.resolve("c").name(), advertisedRef.getObjectId().name());
  128. assertEquals(db.resolve("c").name(), newRepo.resolve("refs/heads/cc")
  129. .name());
  130. assertNull(newRepo.resolve("refs/heads/c"));
  131. assertNull(newRepo.resolve("refs/heads/a")); // still unknown
  132. try {
  133. // Check that we actually needed the first bundle
  134. Repository newRepo2 = createBareRepository();
  135. fetchResult = fetchFromBundle(newRepo2, bundle);
  136. fail("We should not be able to fetch from bundle with prerequisites that are not fulfilled");
  137. } catch (MissingBundlePrerequisiteException e) {
  138. assertTrue(e.getMessage()
  139. .indexOf(db.resolve("refs/heads/a").name()) >= 0);
  140. }
  141. }
  142. }
  143. @Test
  144. public void testAbortWrite() throws Exception {
  145. boolean caught = false;
  146. try {
  147. makeBundleWithCallback(
  148. "refs/heads/aa", db.resolve("a").name(), null, false);
  149. } catch (WriteAbortedException e) {
  150. caught = true;
  151. }
  152. assertTrue(caught);
  153. }
  154. @Test
  155. public void testCustomObjectReader() throws Exception {
  156. String refName = "refs/heads/blob";
  157. String data = "unflushed data";
  158. ObjectId id;
  159. ByteArrayOutputStream out = new ByteArrayOutputStream();
  160. try (Repository repo = new InMemoryRepository(
  161. new DfsRepositoryDescription("repo"));
  162. ObjectInserter ins = repo.newObjectInserter();
  163. ObjectReader or = ins.newReader()) {
  164. id = ins.insert(OBJ_BLOB, Constants.encode(data));
  165. BundleWriter bw = new BundleWriter(or);
  166. bw.include(refName, id);
  167. bw.writeBundle(NullProgressMonitor.INSTANCE, out);
  168. assertNull(repo.exactRef(refName));
  169. try {
  170. repo.open(id, OBJ_BLOB);
  171. fail("We should not be able to open the unflushed blob");
  172. } catch (MissingObjectException e) {
  173. // Expected.
  174. }
  175. }
  176. try (Repository repo = new InMemoryRepository(
  177. new DfsRepositoryDescription("copy"))) {
  178. fetchFromBundle(repo, out.toByteArray());
  179. Ref ref = repo.exactRef(refName);
  180. assertNotNull(ref);
  181. assertEquals(id, ref.getObjectId());
  182. assertEquals(data,
  183. new String(repo.open(id, OBJ_BLOB).getBytes(), UTF_8));
  184. }
  185. }
  186. private static FetchResult fetchFromBundle(final Repository newRepo,
  187. final byte[] bundle) throws URISyntaxException,
  188. NotSupportedException, TransportException {
  189. final URIish uri = new URIish("in-memory://");
  190. final ByteArrayInputStream in = new ByteArrayInputStream(bundle);
  191. final RefSpec rs = new RefSpec("refs/heads/*:refs/heads/*");
  192. final Set<RefSpec> refs = Collections.singleton(rs);
  193. try (TransportBundleStream transport = new TransportBundleStream(
  194. newRepo, uri, in)) {
  195. return transport.fetch(NullProgressMonitor.INSTANCE, refs);
  196. }
  197. }
  198. private byte[] makeBundle(final String name,
  199. final String anObjectToInclude, final RevCommit assume)
  200. throws FileNotFoundException, IOException {
  201. return makeBundleWithCallback(name, anObjectToInclude, assume, true);
  202. }
  203. private byte[] makeBundleWithCallback(final String name,
  204. final String anObjectToInclude, final RevCommit assume,
  205. boolean value)
  206. throws FileNotFoundException, IOException {
  207. final BundleWriter bw;
  208. bw = new BundleWriter(db);
  209. bw.setObjectCountCallback(new NaiveObjectCountCallback(value));
  210. bw.include(name, ObjectId.fromString(anObjectToInclude));
  211. if (assume != null)
  212. bw.assume(assume);
  213. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  214. bw.writeBundle(NullProgressMonitor.INSTANCE, out);
  215. return out.toByteArray();
  216. }
  217. private static class NaiveObjectCountCallback
  218. implements ObjectCountCallback {
  219. private final boolean value;
  220. NaiveObjectCountCallback(boolean value) {
  221. this.value = value;
  222. }
  223. @Override
  224. public void setObjectCount(long unused) throws WriteAbortedException {
  225. if (!value)
  226. throw new WriteAbortedException();
  227. }
  228. }
  229. }