View Javadoc
1   /*
2    * Copyright (C) 2011, GitHub 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.api;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertNotNull;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.util.Collection;
17  import java.util.Iterator;
18  
19  import org.eclipse.jgit.junit.RepositoryTestCase;
20  import org.eclipse.jgit.lib.Constants;
21  import org.eclipse.jgit.lib.ObjectId;
22  import org.eclipse.jgit.lib.RefUpdate;
23  import org.eclipse.jgit.lib.RefUpdate.Result;
24  import org.eclipse.jgit.revwalk.RevCommit;
25  import org.junit.Test;
26  
27  /**
28   * Unit tests of {@link StashListCommand}
29   */
30  public class StashListCommandTest extends RepositoryTestCase {
31  
32  	@Test
33  	public void noStashRef() throws Exception {
34  		StashListCommand command = Git.wrap(db).stashList();
35  		Collection<RevCommit> stashed = command.call();
36  		assertNotNull(stashed);
37  		assertTrue(stashed.isEmpty());
38  	}
39  
40  	@Test
41  	public void emptyStashReflog() throws Exception {
42  		Git git = Git.wrap(db);
43  		writeTrashFile("file.txt", "content");
44  		git.add().addFilepattern("file.txt").call();
45  		RevCommit commit = git.commit().setMessage("create file").call();
46  
47  		RefUpdate update = db.updateRef(Constants.R_STASH);
48  		update.setNewObjectId(commit);
49  		update.disableRefLog();
50  		assertEquals(Result.NEW, update.update());
51  
52  		StashListCommand command = Git.wrap(db).stashList();
53  		Collection<RevCommit> stashed = command.call();
54  		assertNotNull(stashed);
55  		assertTrue(stashed.isEmpty());
56  	}
57  
58  	@Test
59  	public void singleStashedCommit() throws Exception {
60  		Git git = Git.wrap(db);
61  		writeTrashFile("file.txt", "content");
62  		git.add().addFilepattern("file.txt").call();
63  		RevCommit commit = git.commit().setMessage("create file").call();
64  
65  		assertEquals(Result.NEW, newStashUpdate(commit).update());
66  
67  		StashListCommand command = git.stashList();
68  		Collection<RevCommit> stashed = command.call();
69  		assertNotNull(stashed);
70  		assertEquals(1, stashed.size());
71  		assertEquals(commit, stashed.iterator().next());
72  	}
73  
74  	@Test
75  	public void multipleStashedCommits() throws Exception {
76  		Git git = Git.wrap(db);
77  
78  		writeTrashFile("file.txt", "content");
79  		git.add().addFilepattern("file.txt").call();
80  		RevCommit commit1 = git.commit().setMessage("create file").call();
81  
82  		writeTrashFile("file.txt", "content2");
83  		git.add().addFilepattern("file.txt").call();
84  		RevCommit commit2 = git.commit().setMessage("edit file").call();
85  
86  		assertEquals(Result.NEW, newStashUpdate(commit1).update());
87  		assertEquals(Result.FAST_FORWARD, newStashUpdate(commit2).update());
88  
89  		StashListCommand command = git.stashList();
90  		Collection<RevCommit> stashed = command.call();
91  		assertNotNull(stashed);
92  		assertEquals(2, stashed.size());
93  		Iterator<RevCommit> iter = stashed.iterator();
94  		assertEquals(commit2, iter.next());
95  		assertEquals(commit1, iter.next());
96  	}
97  
98  	private RefUpdate newStashUpdate(ObjectId newId) throws Exception {
99  		RefUpdate ru = db.updateRef(Constants.R_STASH);
100 		ru.setNewObjectId(newId);
101 		ru.setForceRefLog(true);
102 		return ru;
103 	}
104 }