2 * Copyright (C) 2008, Charles O'Farrell <charleso@charleso.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.lib
;
41 import java
.io
.IOException
;
43 import java
.util
.Map
.Entry
;
45 import org
.spearce
.jgit
.lib
.RefUpdate
.Result
;
46 import org
.spearce
.jgit
.revwalk
.RevCommit
;
48 public class RefUpdateTest
extends RepositoryTestCase
{
50 private RefUpdate
updateRef(final String name
) throws IOException
{
51 final RefUpdate ref
= db
.updateRef(name
);
52 ref
.setNewObjectId(db
.resolve(Constants
.HEAD
));
56 private void delete(final RefUpdate ref
, final Result expected
)
58 delete(ref
, expected
, true, true);
61 private void delete(final RefUpdate ref
, final Result expected
,
62 final boolean exists
, final boolean removed
) throws IOException
{
63 assertEquals(exists
, db
.getAllRefs().containsKey(ref
.getName()));
64 assertEquals(expected
, ref
.delete());
65 assertEquals(!removed
, db
.getAllRefs().containsKey(ref
.getName()));
68 public void testNoCacheObjectIdSubclass() throws IOException
{
69 final String newRef
= "refs/heads/abc";
70 final RefUpdate ru
= updateRef(newRef
);
71 final RevCommit newid
= new RevCommit(ru
.getNewObjectId()) {
74 ru
.setNewObjectId(newid
);
75 Result update
= ru
.update();
76 assertEquals(Result
.NEW
, update
);
77 final Ref r
= db
.getAllRefs().get(newRef
);
79 assertEquals(newRef
, r
.getName());
80 assertNotNull(r
.getObjectId());
81 assertNotSame(newid
, r
.getObjectId());
82 assertSame(ObjectId
.class, r
.getObjectId().getClass());
83 assertEquals(newid
.copy(), r
.getObjectId());
87 * Delete a ref that is pointed to by HEAD
91 public void testDeleteHEADreferencedRef() throws IOException
{
92 ObjectId pid
= db
.resolve("refs/heads/master^");
93 RefUpdate updateRef
= db
.updateRef("refs/heads/master");
94 updateRef
.setNewObjectId(pid
);
95 updateRef
.setForceUpdate(true);
96 Result update
= updateRef
.update();
97 assertEquals(Result
.FORCED
, update
); // internal
99 RefUpdate updateRef2
= db
.updateRef("refs/heads/master");
100 Result delete
= updateRef2
.delete();
101 assertEquals(Result
.REJECTED_CURRENT_BRANCH
, delete
);
102 assertEquals(pid
, db
.resolve("refs/heads/master"));
105 public void testLooseDelete() throws IOException
{
106 final String newRef
= "refs/heads/abc";
107 RefUpdate ref
= updateRef(newRef
);
108 ref
.update(); // create loose ref
109 ref
= updateRef(newRef
); // refresh
110 delete(ref
, Result
.NO_CHANGE
);
113 public void testDeleteHead() throws IOException
{
114 final RefUpdate ref
= updateRef(Constants
.HEAD
);
115 delete(ref
, Result
.REJECTED_CURRENT_BRANCH
, true, false);
119 * Delete a loose ref and make sure the directory in refs is deleted too,
120 * and the reflog dir too
122 * @throws IOException
124 public void testDeleteLooseAndItsDirectory() throws IOException
{
125 ObjectId pid
= db
.resolve("refs/heads/c^");
126 RefUpdate updateRef
= db
.updateRef("refs/heads/z/c");
127 updateRef
.setNewObjectId(pid
);
128 updateRef
.setForceUpdate(true);
129 Result update
= updateRef
.update();
130 assertEquals(Result
.NEW
, update
); // internal
131 assertTrue(new File(db
.getDirectory(), Constants
.R_HEADS
+ "z")
133 assertTrue(new File(db
.getDirectory(), "logs/refs/heads/z").exists());
135 // The real test here
136 RefUpdate updateRef2
= db
.updateRef("refs/heads/z/c");
137 updateRef2
.setForceUpdate(true);
138 Result delete
= updateRef2
.delete();
139 assertEquals(Result
.FORCED
, delete
);
140 assertNull(db
.resolve("refs/heads/z/c"));
141 assertFalse(new File(db
.getDirectory(), Constants
.R_HEADS
+ "z")
143 assertFalse(new File(db
.getDirectory(), "logs/refs/heads/z").exists());
146 public void testDeleteNotFound() throws IOException
{
147 final RefUpdate ref
= updateRef("refs/heads/xyz");
148 delete(ref
, Result
.NEW
, false, true);
151 public void testDeleteFastForward() throws IOException
{
152 final RefUpdate ref
= updateRef("refs/heads/a");
153 delete(ref
, Result
.FAST_FORWARD
);
156 public void testDeleteForce() throws IOException
{
157 final RefUpdate ref
= db
.updateRef("refs/heads/b");
158 ref
.setNewObjectId(db
.resolve("refs/heads/a"));
159 delete(ref
, Result
.REJECTED
, true, false);
160 ref
.setForceUpdate(true);
161 delete(ref
, Result
.FORCED
);
164 public void testRefKeySameAsOrigName() {
165 Map
<String
, Ref
> allRefs
= db
.getAllRefs();
166 for (Entry
<String
, Ref
> e
: allRefs
.entrySet()) {
167 assertEquals(e
.getKey(), e
.getValue().getOrigName());
173 * Try modify a ref forward, fast forward
175 * @throws IOException
177 public void testUpdateRefForward() throws IOException
{
178 ObjectId ppid
= db
.resolve("refs/heads/master^");
179 ObjectId pid
= db
.resolve("refs/heads/master");
181 RefUpdate updateRef
= db
.updateRef("refs/heads/master");
182 updateRef
.setNewObjectId(ppid
);
183 updateRef
.setForceUpdate(true);
184 Result update
= updateRef
.update();
185 assertEquals(Result
.FORCED
, update
);
186 assertEquals(ppid
, db
.resolve("refs/heads/master"));
189 RefUpdate updateRef2
= db
.updateRef("refs/heads/master");
190 updateRef2
.setNewObjectId(pid
);
191 Result update2
= updateRef2
.update();
192 assertEquals(Result
.FAST_FORWARD
, update2
);
193 assertEquals(pid
, db
.resolve("refs/heads/master"));
197 * Delete a ref that exists both as packed and loose. Make sure the ref
198 * cannot be resolved after delete.
200 * @throws IOException
202 public void testDeleteLoosePacked() throws IOException
{
203 ObjectId pid
= db
.resolve("refs/heads/c^");
204 RefUpdate updateRef
= db
.updateRef("refs/heads/c");
205 updateRef
.setNewObjectId(pid
);
206 updateRef
.setForceUpdate(true);
207 Result update
= updateRef
.update();
208 assertEquals(Result
.FORCED
, update
); // internal
210 // The real test here
211 RefUpdate updateRef2
= db
.updateRef("refs/heads/c");
212 updateRef2
.setForceUpdate(true);
213 Result delete
= updateRef2
.delete();
214 assertEquals(Result
.FORCED
, delete
);
215 assertNull(db
.resolve("refs/heads/c"));
219 * Try modify a ref to same
221 * @throws IOException
223 public void testUpdateRefNoChange() throws IOException
{
224 ObjectId pid
= db
.resolve("refs/heads/master");
225 RefUpdate updateRef
= db
.updateRef("refs/heads/master");
226 updateRef
.setNewObjectId(pid
);
227 Result update
= updateRef
.update();
228 assertEquals(Result
.NO_CHANGE
, update
);
229 assertEquals(pid
, db
.resolve("refs/heads/master"));
233 * Try modify a ref, but get wrong expected old value
235 * @throws IOException
237 public void testUpdateRefLockFailureWrongOldValue() throws IOException
{
238 ObjectId pid
= db
.resolve("refs/heads/master");
239 RefUpdate updateRef
= db
.updateRef("refs/heads/master");
240 updateRef
.setNewObjectId(pid
);
241 updateRef
.setExpectedOldObjectId(db
.resolve("refs/heads/master^"));
242 Result update
= updateRef
.update();
243 assertEquals(Result
.LOCK_FAILURE
, update
);
244 assertEquals(pid
, db
.resolve("refs/heads/master"));
248 * Try modify a ref that is locked
250 * @throws IOException
252 public void testUpdateRefLockFailureLocked() throws IOException
{
253 ObjectId opid
= db
.resolve("refs/heads/master");
254 ObjectId pid
= db
.resolve("refs/heads/master^");
255 RefUpdate updateRef
= db
.updateRef("refs/heads/master");
256 updateRef
.setNewObjectId(pid
);
257 LockFile lockFile1
= new LockFile(new File(db
.getDirectory(),"refs/heads/master"));
258 assertTrue(lockFile1
.lock()); // precondition to test
259 Result update
= updateRef
.update();
260 assertEquals(Result
.LOCK_FAILURE
, update
);
261 assertEquals(opid
, db
.resolve("refs/heads/master"));
262 LockFile lockFile2
= new LockFile(new File(db
.getDirectory(),"refs/heads/master"));
263 assertFalse(lockFile2
.lock()); // was locked, still is
267 * Try to delete a ref. Delete requires force.
269 * @throws IOException
271 public void testDeleteLoosePackedRejected() throws IOException
{
272 ObjectId pid
= db
.resolve("refs/heads/c^");
273 ObjectId oldpid
= db
.resolve("refs/heads/c");
274 RefUpdate updateRef
= db
.updateRef("refs/heads/c");
275 updateRef
.setNewObjectId(pid
);
276 Result update
= updateRef
.update();
277 assertEquals(Result
.REJECTED
, update
);
278 assertEquals(oldpid
, db
.resolve("refs/heads/c"));