View Javadoc
1   /*
2    * Copyright (C) 2015, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.transport;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertSame;
48  import static org.junit.Assert.fail;
49  
50  import java.io.IOException;
51  import java.util.ArrayList;
52  import java.util.List;
53  
54  import org.eclipse.jgit.errors.TransportException;
55  import org.eclipse.jgit.internal.JGitText;
56  import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
57  import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
58  import org.eclipse.jgit.lib.Constants;
59  import org.eclipse.jgit.lib.NullProgressMonitor;
60  import org.eclipse.jgit.lib.ObjectId;
61  import org.eclipse.jgit.lib.ObjectInserter;
62  import org.eclipse.jgit.lib.Repository;
63  import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
64  import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
65  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
66  import org.junit.After;
67  import org.junit.Before;
68  import org.junit.Test;
69  
70  public class AtomicPushTest {
71  	private URIish uri;
72  	private TestProtocol<Object> testProtocol;
73  	private Object ctx = new Object();
74  	private InMemoryRepository server;
75  	private InMemoryRepository client;
76  	private ObjectId obj1;
77  	private ObjectId obj2;
78  
79  	@Before
80  	public void setUp() throws Exception {
81  		server = newRepo("server");
82  		client = newRepo("client");
83  		testProtocol = new TestProtocol<>(
84  				null,
85  				new ReceivePackFactory<Object>() {
86  					@Override
87  					public ReceivePack create(Object req, Repository db)
88  							throws ServiceNotEnabledException,
89  							ServiceNotAuthorizedException {
90  						return new ReceivePack(db);
91  					}
92  				});
93  		uri = testProtocol.register(ctx, server);
94  
95  		try (ObjectInserter ins = client.newObjectInserter()) {
96  			obj1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("test"));
97  			obj2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("file"));
98  			ins.flush();
99  		}
100 	}
101 
102 	@After
103 	public void tearDown() {
104 		Transport.unregister(testProtocol);
105 	}
106 
107 	private static InMemoryRepository newRepo(String name) {
108 		return new InMemoryRepository(new DfsRepositoryDescription(name));
109 	}
110 
111 	@Test
112 	public void pushNonAtomic() throws Exception {
113 		PushResult r;
114 		server.setPerformsAtomicTransactions(false);
115 		try (Transport tn = testProtocol.open(uri, client, "server")) {
116 			tn.setPushAtomic(false);
117 			r = tn.push(NullProgressMonitor.INSTANCE, commands());
118 		}
119 
120 		RemoteRefUpdate one = r.getRemoteUpdate("refs/heads/one");
121 		RemoteRefUpdate two = r.getRemoteUpdate("refs/heads/two");
122 		assertSame(RemoteRefUpdate.Status.OK, one.getStatus());
123 		assertSame(
124 				RemoteRefUpdate.Status.REJECTED_REMOTE_CHANGED,
125 				two.getStatus());
126 	}
127 
128 	@Test
129 	public void pushAtomicClientGivesUpEarly() throws Exception {
130 		PushResult r;
131 		try (Transport tn = testProtocol.open(uri, client, "server")) {
132 			tn.setPushAtomic(true);
133 			r = tn.push(NullProgressMonitor.INSTANCE, commands());
134 		}
135 
136 		RemoteRefUpdate one = r.getRemoteUpdate("refs/heads/one");
137 		RemoteRefUpdate two = r.getRemoteUpdate("refs/heads/two");
138 		assertSame(
139 				RemoteRefUpdate.Status.REJECTED_OTHER_REASON,
140 				one.getStatus());
141 		assertSame(
142 				RemoteRefUpdate.Status.REJECTED_REMOTE_CHANGED,
143 				two.getStatus());
144 		assertEquals(JGitText.get().transactionAborted, one.getMessage());
145 	}
146 
147 	@Test
148 	public void pushAtomicDisabled() throws Exception {
149 		List<RemoteRefUpdate> cmds = new ArrayList<>();
150 		cmds.add(new RemoteRefUpdate(
151 				null, null,
152 				obj1, "refs/heads/one",
153 				true /* force update */,
154 				null /* no local tracking ref */,
155 				ObjectId.zeroId()));
156 		cmds.add(new RemoteRefUpdate(
157 				null, null,
158 				obj2, "refs/heads/two",
159 				true /* force update */,
160 				null /* no local tracking ref */,
161 				ObjectId.zeroId()));
162 
163 		server.setPerformsAtomicTransactions(false);
164 		try (Transport tn = testProtocol.open(uri, client, "server")) {
165 			tn.setPushAtomic(true);
166 			tn.push(NullProgressMonitor.INSTANCE, cmds);
167 			fail("did not throw TransportException");
168 		} catch (TransportException e) {
169 			assertEquals(
170 					uri + ": " + JGitText.get().atomicPushNotSupported,
171 					e.getMessage());
172 		}
173 	}
174 
175 	private List<RemoteRefUpdate> commands() throws IOException {
176 		List<RemoteRefUpdate> cmds = new ArrayList<>();
177 		cmds.add(new RemoteRefUpdate(
178 				null, null,
179 				obj1, "refs/heads/one",
180 				true /* force update */,
181 				null /* no local tracking ref */,
182 				ObjectId.zeroId()));
183 		cmds.add(new RemoteRefUpdate(
184 				null, null,
185 				obj2, "refs/heads/two",
186 				true /* force update */,
187 				null /* no local tracking ref */,
188 				obj1));
189 		return cmds;
190 	}
191 }