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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 testWrite0() 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
  76. // we could read the bundle we created.
  77. Repository newRepo = createBareRepository();
  78. FetchResult fetchResult = fetchFromBundle(newRepo, bundle);
  79. Ref advertisedRef = fetchResult
  80. .getAdvertisedRef("refs/heads/firstcommit");
  81. // We expect firstcommit to appear by id
  82. assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", advertisedRef
  83. .getObjectId().name());
  84. // ..and by name as the bundle created a new ref
  85. assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", newRepo
  86. .resolve("refs/heads/firstcommit").name());
  87. }
  88. /**
  89. * Incremental bundle test
  90. *
  91. * @throws Exception
  92. */
  93. @Test
  94. public void testWrite1() throws Exception {
  95. byte[] bundle;
  96. // Create a small bundle, an early commit
  97. bundle = makeBundle("refs/heads/aa", db.resolve("a").name(), null);
  98. // Then we clone a new repo from that bundle and do a simple test. This
  99. // makes sure
  100. // we could read the bundle we created.
  101. Repository newRepo = createBareRepository();
  102. FetchResult fetchResult = fetchFromBundle(newRepo, bundle);
  103. Ref advertisedRef = fetchResult.getAdvertisedRef("refs/heads/aa");
  104. assertEquals(db.resolve("a").name(), advertisedRef.getObjectId().name());
  105. assertEquals(db.resolve("a").name(), newRepo.resolve("refs/heads/aa")
  106. .name());
  107. assertNull(newRepo.resolve("refs/heads/a"));
  108. // Next an incremental bundle
  109. bundle = makeBundle("refs/heads/cc", db.resolve("c").name(),
  110. new RevWalk(db).parseCommit(db.resolve("a").toObjectId()));
  111. fetchResult = fetchFromBundle(newRepo, bundle);
  112. advertisedRef = fetchResult.getAdvertisedRef("refs/heads/cc");
  113. assertEquals(db.resolve("c").name(), advertisedRef.getObjectId().name());
  114. assertEquals(db.resolve("c").name(), newRepo.resolve("refs/heads/cc")
  115. .name());
  116. assertNull(newRepo.resolve("refs/heads/c"));
  117. assertNull(newRepo.resolve("refs/heads/a")); // still unknown
  118. try {
  119. // Check that we actually needed the first bundle
  120. Repository newRepo2 = createBareRepository();
  121. fetchResult = fetchFromBundle(newRepo2, bundle);
  122. fail("We should not be able to fetch from bundle with prerequisites that are not fulfilled");
  123. } catch (MissingBundlePrerequisiteException e) {
  124. assertTrue(e.getMessage()
  125. .indexOf(db.resolve("refs/heads/a").name()) >= 0);
  126. }
  127. }
  128. private static FetchResult fetchFromBundle(final Repository newRepo,
  129. final byte[] bundle) throws URISyntaxException,
  130. NotSupportedException, TransportException {
  131. final URIish uri = new URIish("in-memory://");
  132. final ByteArrayInputStream in = new ByteArrayInputStream(bundle);
  133. final RefSpec rs = new RefSpec("refs/heads/*:refs/heads/*");
  134. final Set<RefSpec> refs = Collections.singleton(rs);
  135. return new TransportBundleStream(newRepo, uri, in).fetch(
  136. NullProgressMonitor.INSTANCE, refs);
  137. }
  138. private byte[] makeBundle(final String name,
  139. final String anObjectToInclude, final RevCommit assume)
  140. throws FileNotFoundException, IOException {
  141. final BundleWriter bw;
  142. bw = new BundleWriter(db);
  143. bw.include(name, ObjectId.fromString(anObjectToInclude));
  144. if (assume != null)
  145. bw.assume(assume);
  146. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  147. bw.writeBundle(NullProgressMonitor.INSTANCE, out);
  148. return out.toByteArray();
  149. }
  150. }