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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.junit.Assert.fail;
  50. import java.io.ByteArrayInputStream;
  51. import java.io.ByteArrayOutputStream;
  52. import java.io.FileNotFoundException;
  53. import java.io.IOException;
  54. import java.net.URISyntaxException;
  55. import java.util.Collections;
  56. import java.util.Set;
  57. import org.eclipse.jgit.errors.MissingBundlePrerequisiteException;
  58. import org.eclipse.jgit.errors.NotSupportedException;
  59. import org.eclipse.jgit.errors.TransportException;
  60. import org.eclipse.jgit.lib.NullProgressMonitor;
  61. import org.eclipse.jgit.lib.ObjectId;
  62. import org.eclipse.jgit.lib.Ref;
  63. import org.eclipse.jgit.lib.Repository;
  64. import org.eclipse.jgit.revwalk.RevCommit;
  65. import org.eclipse.jgit.revwalk.RevWalk;
  66. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  67. import org.junit.Test;
  68. public class BundleWriterTest extends SampleDataRepositoryTestCase {
  69. @Test
  70. public void testWriteSingleRef() throws Exception {
  71. // Create a tiny bundle, (well one of) the first commits only
  72. final byte[] bundle = makeBundle("refs/heads/firstcommit",
  73. "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", null);
  74. // Then we clone a new repo from that bundle and do a simple test. This
  75. // makes sure we could read the bundle we created.
  76. Repository newRepo = createBareRepository();
  77. FetchResult fetchResult = fetchFromBundle(newRepo, bundle);
  78. Ref advertisedRef = fetchResult
  79. .getAdvertisedRef("refs/heads/firstcommit");
  80. // We expect first commit to appear by id
  81. assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", advertisedRef
  82. .getObjectId().name());
  83. // ..and by name as the bundle created a new ref
  84. assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", newRepo
  85. .resolve("refs/heads/firstcommit").name());
  86. }
  87. @Test
  88. public void testWriteHEAD() throws Exception {
  89. byte[] bundle = makeBundle("HEAD",
  90. "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", null);
  91. Repository newRepo = createBareRepository();
  92. FetchResult fetchResult = fetchFromBundle(newRepo, bundle);
  93. Ref advertisedRef = fetchResult.getAdvertisedRef("HEAD");
  94. assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", advertisedRef
  95. .getObjectId().name());
  96. }
  97. @Test
  98. public void testIncrementalBundle() throws Exception {
  99. byte[] bundle;
  100. // Create a small bundle, an early commit
  101. bundle = makeBundle("refs/heads/aa", db.resolve("a").name(), null);
  102. // Then we clone a new repo from that bundle and do a simple test. This
  103. // makes sure
  104. // we could read the bundle we created.
  105. Repository newRepo = createBareRepository();
  106. FetchResult fetchResult = fetchFromBundle(newRepo, bundle);
  107. Ref advertisedRef = fetchResult.getAdvertisedRef("refs/heads/aa");
  108. assertEquals(db.resolve("a").name(), advertisedRef.getObjectId().name());
  109. assertEquals(db.resolve("a").name(), newRepo.resolve("refs/heads/aa")
  110. .name());
  111. assertNull(newRepo.resolve("refs/heads/a"));
  112. // Next an incremental bundle
  113. bundle = makeBundle("refs/heads/cc", db.resolve("c").name(),
  114. new RevWalk(db).parseCommit(db.resolve("a").toObjectId()));
  115. fetchResult = fetchFromBundle(newRepo, bundle);
  116. advertisedRef = fetchResult.getAdvertisedRef("refs/heads/cc");
  117. assertEquals(db.resolve("c").name(), advertisedRef.getObjectId().name());
  118. assertEquals(db.resolve("c").name(), newRepo.resolve("refs/heads/cc")
  119. .name());
  120. assertNull(newRepo.resolve("refs/heads/c"));
  121. assertNull(newRepo.resolve("refs/heads/a")); // still unknown
  122. try {
  123. // Check that we actually needed the first bundle
  124. Repository newRepo2 = createBareRepository();
  125. fetchResult = fetchFromBundle(newRepo2, bundle);
  126. fail("We should not be able to fetch from bundle with prerequisites that are not fulfilled");
  127. } catch (MissingBundlePrerequisiteException e) {
  128. assertTrue(e.getMessage()
  129. .indexOf(db.resolve("refs/heads/a").name()) >= 0);
  130. }
  131. }
  132. @Test
  133. public void testAbortWrite() throws Exception {
  134. boolean caught = false;
  135. try {
  136. makeBundleWithCallback(
  137. "refs/heads/aa", db.resolve("a").name(), null, false);
  138. } catch (WriteAbortedException e) {
  139. caught = true;
  140. }
  141. assertTrue(caught);
  142. }
  143. private static FetchResult fetchFromBundle(final Repository newRepo,
  144. final byte[] bundle) throws URISyntaxException,
  145. NotSupportedException, TransportException {
  146. final URIish uri = new URIish("in-memory://");
  147. final ByteArrayInputStream in = new ByteArrayInputStream(bundle);
  148. final RefSpec rs = new RefSpec("refs/heads/*:refs/heads/*");
  149. final Set<RefSpec> refs = Collections.singleton(rs);
  150. try (TransportBundleStream transport = new TransportBundleStream(
  151. newRepo, uri, in)) {
  152. return transport.fetch(NullProgressMonitor.INSTANCE, refs);
  153. }
  154. }
  155. private byte[] makeBundle(final String name,
  156. final String anObjectToInclude, final RevCommit assume)
  157. throws FileNotFoundException, IOException {
  158. return makeBundleWithCallback(name, anObjectToInclude, assume, true);
  159. }
  160. private byte[] makeBundleWithCallback(final String name,
  161. final String anObjectToInclude, final RevCommit assume,
  162. boolean value)
  163. throws FileNotFoundException, IOException {
  164. final BundleWriter bw;
  165. bw = new BundleWriter(db);
  166. bw.setObjectCountCallback(new NaiveObjectCountCallback(value));
  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. private static class NaiveObjectCountCallback
  175. implements ObjectCountCallback {
  176. private final boolean value;
  177. NaiveObjectCountCallback(boolean value) {
  178. this.value = value;
  179. }
  180. @Override
  181. public void setObjectCount(long unused) throws WriteAbortedException {
  182. if (!value)
  183. throw new WriteAbortedException();
  184. }
  185. }
  186. }