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.

AtomicPushTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2015, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.transport;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertSame;
  13. import static org.junit.Assert.fail;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import org.eclipse.jgit.errors.TransportException;
  18. import org.eclipse.jgit.internal.JGitText;
  19. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  20. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  21. import org.eclipse.jgit.junit.TestRepository;
  22. import org.eclipse.jgit.lib.NullProgressMonitor;
  23. import org.eclipse.jgit.lib.ObjectId;
  24. import org.eclipse.jgit.lib.Repository;
  25. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  26. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  27. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  28. import org.junit.After;
  29. import org.junit.Before;
  30. import org.junit.Test;
  31. public class AtomicPushTest {
  32. private URIish uri;
  33. private TestProtocol<Object> testProtocol;
  34. private Object ctx = new Object();
  35. private InMemoryRepository server;
  36. private InMemoryRepository client;
  37. private ObjectId commit1;
  38. private ObjectId commit2;
  39. @Before
  40. public void setUp() throws Exception {
  41. server = newRepo("server");
  42. client = newRepo("client");
  43. testProtocol = new TestProtocol<>(
  44. null,
  45. new ReceivePackFactory<Object>() {
  46. @Override
  47. public ReceivePack create(Object req, Repository db)
  48. throws ServiceNotEnabledException,
  49. ServiceNotAuthorizedException {
  50. return new ReceivePack(db);
  51. }
  52. });
  53. uri = testProtocol.register(ctx, server);
  54. try (TestRepository<?> clientRepo = new TestRepository<>(client)) {
  55. commit1 = clientRepo.commit().noFiles().message("test commit 1")
  56. .create();
  57. commit2 = clientRepo.commit().noFiles().message("test commit 2")
  58. .create();
  59. }
  60. }
  61. @After
  62. public void tearDown() {
  63. Transport.unregister(testProtocol);
  64. }
  65. private static InMemoryRepository newRepo(String name) {
  66. return new InMemoryRepository(new DfsRepositoryDescription(name));
  67. }
  68. @Test
  69. public void pushNonAtomic() throws Exception {
  70. PushResult r;
  71. server.setPerformsAtomicTransactions(false);
  72. try (Transport tn = testProtocol.open(uri, client, "server")) {
  73. tn.setPushAtomic(false);
  74. r = tn.push(NullProgressMonitor.INSTANCE, commands());
  75. }
  76. RemoteRefUpdate one = r.getRemoteUpdate("refs/heads/one");
  77. RemoteRefUpdate two = r.getRemoteUpdate("refs/heads/two");
  78. assertSame(RemoteRefUpdate.Status.OK, one.getStatus());
  79. assertSame(
  80. RemoteRefUpdate.Status.REJECTED_REMOTE_CHANGED,
  81. two.getStatus());
  82. }
  83. @Test
  84. public void pushAtomicClientGivesUpEarly() throws Exception {
  85. PushResult r;
  86. try (Transport tn = testProtocol.open(uri, client, "server")) {
  87. tn.setPushAtomic(true);
  88. r = tn.push(NullProgressMonitor.INSTANCE, commands());
  89. }
  90. RemoteRefUpdate one = r.getRemoteUpdate("refs/heads/one");
  91. RemoteRefUpdate two = r.getRemoteUpdate("refs/heads/two");
  92. assertSame(
  93. RemoteRefUpdate.Status.REJECTED_OTHER_REASON,
  94. one.getStatus());
  95. assertSame(
  96. RemoteRefUpdate.Status.REJECTED_REMOTE_CHANGED,
  97. two.getStatus());
  98. assertEquals(JGitText.get().transactionAborted, one.getMessage());
  99. }
  100. @Test
  101. public void pushAtomicDisabled() throws Exception {
  102. List<RemoteRefUpdate> cmds = new ArrayList<>();
  103. cmds.add(new RemoteRefUpdate(
  104. null, null,
  105. commit1, "refs/heads/one",
  106. true /* force update */,
  107. null /* no local tracking ref */,
  108. ObjectId.zeroId()));
  109. cmds.add(new RemoteRefUpdate(
  110. null, null,
  111. commit2, "refs/heads/two",
  112. true /* force update */,
  113. null /* no local tracking ref */,
  114. ObjectId.zeroId()));
  115. server.setPerformsAtomicTransactions(false);
  116. try (Transport tn = testProtocol.open(uri, client, "server")) {
  117. tn.setPushAtomic(true);
  118. tn.push(NullProgressMonitor.INSTANCE, cmds);
  119. fail("did not throw TransportException");
  120. } catch (TransportException e) {
  121. assertEquals(
  122. uri + ": " + JGitText.get().atomicPushNotSupported,
  123. e.getMessage());
  124. }
  125. }
  126. private List<RemoteRefUpdate> commands() throws IOException {
  127. List<RemoteRefUpdate> cmds = new ArrayList<>();
  128. cmds.add(new RemoteRefUpdate(
  129. null, null,
  130. commit1, "refs/heads/one",
  131. true /* force update */,
  132. null /* no local tracking ref */,
  133. ObjectId.zeroId()));
  134. cmds.add(new RemoteRefUpdate(
  135. null, null,
  136. commit2, "refs/heads/two",
  137. true /* force update */,
  138. null /* no local tracking ref */,
  139. commit1));
  140. return cmds;
  141. }
  142. }