RefUpdateTest: Refactor to not use deprecated Repository#getAllRefs

Change-Id: I157e62a0e1479d4bc95ef867e616ca8a30041759
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
This commit is contained in:
David Pursehouse 2018-05-23 13:54:52 +09:00 committed by Matthias Sohn
parent f7fbc7fcd7
commit 5ff56d2ed1
1 changed files with 27 additions and 17 deletions

View File

@ -62,6 +62,7 @@
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Optional;
import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
@ -110,11 +111,21 @@ private void delete(final RefUpdate ref, final Result expected,
delete(db, ref, expected, exists, removed); delete(db, ref, expected, exists, removed);
} }
private void delete(Repository repo, final RefUpdate ref, final Result expected, private void delete(Repository repo, final RefUpdate ref,
final boolean exists, final boolean removed) throws IOException { final Result expected, final boolean exists, final boolean removed)
assertEquals(exists, repo.getAllRefs().containsKey(ref.getName())); throws IOException {
assertEquals(exists, getRef(repo, ref.getName()).isPresent());
assertEquals(expected, ref.delete()); assertEquals(expected, ref.delete());
assertEquals(!removed, repo.getAllRefs().containsKey(ref.getName())); assertEquals(!removed, getRef(repo, ref.getName()).isPresent());
}
private Optional<Ref> getRef(Repository repo, String name)
throws IOException {
return getRef(repo.getRefDatabase().getRefs(), name);
}
private Optional<Ref> getRef(List<Ref> refs, String name) {
return refs.stream().filter(r -> r.getName().equals(name)).findAny();
} }
@Test @Test
@ -125,8 +136,7 @@ public void testNoCacheObjectIdSubclass() throws IOException {
ru.setNewObjectId(newid); ru.setNewObjectId(newid);
Result update = ru.update(); Result update = ru.update();
assertEquals(Result.NEW, update); assertEquals(Result.NEW, update);
final Ref r = db.getAllRefs().get(newRef); final Ref r = getRef(db, newRef).get();
assertNotNull(r);
assertEquals(newRef, r.getName()); assertEquals(newRef, r.getName());
assertNotNull(r.getObjectId()); assertNotNull(r.getObjectId());
assertNotSame(newid, r.getObjectId()); assertNotSame(newid, r.getObjectId());
@ -378,10 +388,10 @@ public void testDeleteWithoutHead() throws IOException {
@Test @Test
public void testRefKeySameAsName() { public void testRefKeySameAsName() {
@SuppressWarnings("deprecation")
Map<String, Ref> allRefs = db.getAllRefs(); Map<String, Ref> allRefs = db.getAllRefs();
for (Entry<String, Ref> e : allRefs.entrySet()) { for (Entry<String, Ref> e : allRefs.entrySet()) {
assertEquals(e.getKey(), e.getValue().getName()); assertEquals(e.getKey(), e.getValue().getName());
} }
} }
@ -520,8 +530,8 @@ public void testUpdateRefNoChange() throws IOException {
*/ */
@Test @Test
public void testRefsCacheAfterUpdate() throws Exception { public void testRefsCacheAfterUpdate() throws Exception {
// Do not use the defalt repo for this case. // Do not use the default repo for this case.
Map<String, Ref> allRefs = db.getAllRefs(); List<Ref> allRefs = db.getRefDatabase().getRefs();
ObjectId oldValue = db.resolve("HEAD"); ObjectId oldValue = db.resolve("HEAD");
ObjectId newValue = db.resolve("HEAD^"); ObjectId newValue = db.resolve("HEAD^");
// first make HEAD refer to loose ref // first make HEAD refer to loose ref
@ -537,9 +547,9 @@ public void testRefsCacheAfterUpdate() throws Exception {
update = updateRef.update(); update = updateRef.update();
assertEquals(Result.FAST_FORWARD, update); assertEquals(Result.FAST_FORWARD, update);
allRefs = db.getAllRefs(); allRefs = db.getRefDatabase().getRefs();
Ref master = allRefs.get("refs/heads/master"); Ref master = getRef(allRefs, "refs/heads/master").get();
Ref head = allRefs.get("HEAD"); Ref head = getRef(allRefs, "HEAD").get();
assertEquals("refs/heads/master", master.getName()); assertEquals("refs/heads/master", master.getName());
assertEquals("HEAD", head.getName()); assertEquals("HEAD", head.getName());
assertTrue("is symbolic reference", head.isSymbolic()); assertTrue("is symbolic reference", head.isSymbolic());
@ -557,8 +567,8 @@ public void testRefsCacheAfterUpdate() throws Exception {
*/ */
@Test @Test
public void testRefsCacheAfterUpdateLooseOnly() throws Exception { public void testRefsCacheAfterUpdateLooseOnly() throws Exception {
// Do not use the defalt repo for this case. // Do not use the default repo for this case.
Map<String, Ref> allRefs = db.getAllRefs(); List<Ref> allRefs = db.getRefDatabase().getRefs();
ObjectId oldValue = db.resolve("HEAD"); ObjectId oldValue = db.resolve("HEAD");
writeSymref(Constants.HEAD, "refs/heads/newref"); writeSymref(Constants.HEAD, "refs/heads/newref");
RefUpdate updateRef = db.updateRef(Constants.HEAD); RefUpdate updateRef = db.updateRef(Constants.HEAD);
@ -567,9 +577,9 @@ public void testRefsCacheAfterUpdateLooseOnly() throws Exception {
Result update = updateRef.update(); Result update = updateRef.update();
assertEquals(Result.NEW, update); assertEquals(Result.NEW, update);
allRefs = db.getAllRefs(); allRefs = db.getRefDatabase().getRefs();
Ref head = allRefs.get("HEAD"); Ref head = getRef(allRefs, "HEAD").get();
Ref newref = allRefs.get("refs/heads/newref"); Ref newref = getRef(allRefs, "refs/heads/newref").get();
assertEquals("refs/heads/newref", newref.getName()); assertEquals("refs/heads/newref", newref.getName());
assertEquals("HEAD", head.getName()); assertEquals("HEAD", head.getName());
assertTrue("is symbolic reference", head.isSymbolic()); assertTrue("is symbolic reference", head.isSymbolic());