View Javadoc
1   /*
2    * Copyright (C) 2012, GitHub 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  package org.eclipse.jgit.api;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertNotNull;
47  import static org.junit.Assert.assertNull;
48  import static org.junit.Assert.assertTrue;
49  import static org.junit.Assert.fail;
50  
51  import java.io.File;
52  import java.util.List;
53  
54  import org.eclipse.jgit.api.errors.JGitInternalException;
55  import org.eclipse.jgit.junit.RepositoryTestCase;
56  import org.eclipse.jgit.lib.Constants;
57  import org.eclipse.jgit.lib.ObjectId;
58  import org.eclipse.jgit.lib.Ref;
59  import org.eclipse.jgit.lib.ReflogEntry;
60  import org.eclipse.jgit.lib.ReflogReader;
61  import org.eclipse.jgit.revwalk.RevCommit;
62  import org.junit.Before;
63  import org.junit.Test;
64  
65  /**
66   * Unit tests of {@link StashCreateCommand}
67   */
68  public class StashDropCommandTest extends RepositoryTestCase {
69  
70  	private RevCommit head;
71  
72  	private Git git;
73  
74  	private File committedFile;
75  
76  	@Before
77  	public void setUp() throws Exception {
78  		super.setUp();
79  		git = Git.wrap(db);
80  		committedFile = writeTrashFile("file.txt", "content");
81  		git.add().addFilepattern("file.txt").call();
82  		head = git.commit().setMessage("add file").call();
83  		assertNotNull(head);
84  	}
85  
86  	@Test(expected = IllegalArgumentException.class)
87  	public void dropNegativeRef() {
88  		git.stashDrop().setStashRef(-1);
89  	}
90  
91  	@Test
92  	public void dropWithNoStashedCommits() throws Exception {
93  		assertNull(git.stashDrop().call());
94  	}
95  
96  	@Test
97  	public void dropWithInvalidLogIndex() throws Exception {
98  		write(committedFile, "content2");
99  		Ref stashRef = git.getRepository().getRef(Constants.R_STASH);
100 		assertNull(stashRef);
101 		RevCommit stashed = git.stashCreate().call();
102 		assertNotNull(stashed);
103 		stashRef = git.getRepository().getRef(Constants.R_STASH);
104 		assertEquals(stashed, git.getRepository().getRef(Constants.R_STASH)
105 				.getObjectId());
106 		try {
107 			assertNull(git.stashDrop().setStashRef(100).call());
108 			fail("Exception not thrown");
109 		} catch (JGitInternalException e) {
110 			assertNotNull(e.getMessage());
111 			assertTrue(e.getMessage().length() > 0);
112 		}
113 	}
114 
115 	@Test
116 	public void dropSingleStashedCommit() throws Exception {
117 		write(committedFile, "content2");
118 		Ref stashRef = git.getRepository().getRef(Constants.R_STASH);
119 		assertNull(stashRef);
120 		RevCommit stashed = git.stashCreate().call();
121 		assertNotNull(stashed);
122 		stashRef = git.getRepository().getRef(Constants.R_STASH);
123 		assertEquals(stashed, git.getRepository().getRef(Constants.R_STASH)
124 				.getObjectId());
125 		assertNull(git.stashDrop().call());
126 		stashRef = git.getRepository().getRef(Constants.R_STASH);
127 		assertNull(stashRef);
128 
129 		ReflogReader reader = git.getRepository().getReflogReader(
130 				Constants.R_STASH);
131 		assertNull(reader);
132 	}
133 
134 	@Test
135 	public void dropAll() throws Exception {
136 		write(committedFile, "content2");
137 		Ref stashRef = git.getRepository().getRef(Constants.R_STASH);
138 		assertNull(stashRef);
139 		RevCommit firstStash = git.stashCreate().call();
140 		assertNotNull(firstStash);
141 		stashRef = git.getRepository().getRef(Constants.R_STASH);
142 		assertNotNull(stashRef);
143 		assertEquals(firstStash, git.getRepository().getRef(Constants.R_STASH)
144 				.getObjectId());
145 
146 		write(committedFile, "content3");
147 		RevCommit secondStash = git.stashCreate().call();
148 		assertNotNull(secondStash);
149 		stashRef = git.getRepository().getRef(Constants.R_STASH);
150 		assertNotNull(stashRef);
151 		assertEquals(secondStash, git.getRepository().getRef(Constants.R_STASH)
152 				.getObjectId());
153 
154 		assertNull(git.stashDrop().setAll(true).call());
155 		assertNull(git.getRepository().getRef(Constants.R_STASH));
156 
157 		ReflogReader reader = git.getRepository().getReflogReader(
158 				Constants.R_STASH);
159 		assertNull(reader);
160 	}
161 
162 	@Test
163 	public void dropFirstStashedCommit() throws Exception {
164 		write(committedFile, "content2");
165 		Ref stashRef = git.getRepository().getRef(Constants.R_STASH);
166 		assertNull(stashRef);
167 		RevCommit firstStash = git.stashCreate().call();
168 		assertNotNull(firstStash);
169 		stashRef = git.getRepository().getRef(Constants.R_STASH);
170 		assertNotNull(stashRef);
171 		assertEquals(firstStash, git.getRepository().getRef(Constants.R_STASH)
172 				.getObjectId());
173 
174 		write(committedFile, "content3");
175 		RevCommit secondStash = git.stashCreate().call();
176 		assertNotNull(secondStash);
177 		stashRef = git.getRepository().getRef(Constants.R_STASH);
178 		assertNotNull(stashRef);
179 		assertEquals(secondStash, git.getRepository().getRef(Constants.R_STASH)
180 				.getObjectId());
181 
182 		assertEquals(firstStash, git.stashDrop().call());
183 		stashRef = git.getRepository().getRef(Constants.R_STASH);
184 		assertNotNull(stashRef);
185 		assertEquals(firstStash, stashRef.getObjectId());
186 
187 		ReflogReader reader = git.getRepository().getReflogReader(
188 				Constants.R_STASH);
189 		List<ReflogEntry> entries = reader.getReverseEntries();
190 		assertEquals(1, entries.size());
191 		assertEquals(ObjectId.zeroId(), entries.get(0).getOldId());
192 		assertEquals(firstStash, entries.get(0).getNewId());
193 		assertTrue(entries.get(0).getComment().length() > 0);
194 	}
195 
196 	@Test
197 	public void dropMiddleStashCommit() throws Exception {
198 		write(committedFile, "content2");
199 		Ref stashRef = git.getRepository().getRef(Constants.R_STASH);
200 		assertNull(stashRef);
201 		RevCommit firstStash = git.stashCreate().call();
202 		assertNotNull(firstStash);
203 		stashRef = git.getRepository().getRef(Constants.R_STASH);
204 		assertNotNull(stashRef);
205 		assertEquals(firstStash, git.getRepository().getRef(Constants.R_STASH)
206 				.getObjectId());
207 
208 		write(committedFile, "content3");
209 		RevCommit secondStash = git.stashCreate().call();
210 		assertNotNull(secondStash);
211 		stashRef = git.getRepository().getRef(Constants.R_STASH);
212 		assertNotNull(stashRef);
213 		assertEquals(secondStash, git.getRepository().getRef(Constants.R_STASH)
214 				.getObjectId());
215 
216 		write(committedFile, "content4");
217 		RevCommit thirdStash = git.stashCreate().call();
218 		assertNotNull(thirdStash);
219 		stashRef = git.getRepository().getRef(Constants.R_STASH);
220 		assertNotNull(stashRef);
221 		assertEquals(thirdStash, git.getRepository().getRef(Constants.R_STASH)
222 				.getObjectId());
223 
224 		assertEquals(thirdStash, git.stashDrop().setStashRef(1).call());
225 		stashRef = git.getRepository().getRef(Constants.R_STASH);
226 		assertNotNull(stashRef);
227 		assertEquals(thirdStash, stashRef.getObjectId());
228 
229 		ReflogReader reader = git.getRepository().getReflogReader(
230 				Constants.R_STASH);
231 		List<ReflogEntry> entries = reader.getReverseEntries();
232 		assertEquals(2, entries.size());
233 		assertEquals(ObjectId.zeroId(), entries.get(1).getOldId());
234 		assertEquals(firstStash, entries.get(1).getNewId());
235 		assertTrue(entries.get(1).getComment().length() > 0);
236 		assertEquals(entries.get(0).getOldId(), entries.get(1).getNewId());
237 		assertEquals(thirdStash, entries.get(0).getNewId());
238 		assertTrue(entries.get(0).getComment().length() > 0);
239 	}
240 
241 	@Test
242 	public void dropBoundaryStashedCommits() throws Exception {
243 		write(committedFile, "content2");
244 		Ref stashRef = git.getRepository().getRef(Constants.R_STASH);
245 		assertNull(stashRef);
246 		RevCommit firstStash = git.stashCreate().call();
247 		assertNotNull(firstStash);
248 		stashRef = git.getRepository().getRef(Constants.R_STASH);
249 		assertNotNull(stashRef);
250 		assertEquals(firstStash, git.getRepository().getRef(Constants.R_STASH)
251 				.getObjectId());
252 
253 		write(committedFile, "content3");
254 		RevCommit secondStash = git.stashCreate().call();
255 		assertNotNull(secondStash);
256 		stashRef = git.getRepository().getRef(Constants.R_STASH);
257 		assertNotNull(stashRef);
258 		assertEquals(secondStash, git.getRepository().getRef(Constants.R_STASH)
259 				.getObjectId());
260 
261 		write(committedFile, "content4");
262 		RevCommit thirdStash = git.stashCreate().call();
263 		assertNotNull(thirdStash);
264 		stashRef = git.getRepository().getRef(Constants.R_STASH);
265 		assertNotNull(stashRef);
266 		assertEquals(thirdStash, git.getRepository().getRef(Constants.R_STASH)
267 				.getObjectId());
268 
269 		write(committedFile, "content5");
270 		RevCommit fourthStash = git.stashCreate().call();
271 		assertNotNull(fourthStash);
272 		stashRef = git.getRepository().getRef(Constants.R_STASH);
273 		assertNotNull(stashRef);
274 		assertEquals(fourthStash, git.getRepository().getRef(Constants.R_STASH)
275 				.getObjectId());
276 
277 		assertEquals(thirdStash, git.stashDrop().call());
278 		stashRef = git.getRepository().getRef(Constants.R_STASH);
279 		assertNotNull(stashRef);
280 		assertEquals(thirdStash, stashRef.getObjectId());
281 
282 		assertEquals(thirdStash, git.stashDrop().setStashRef(2).call());
283 		stashRef = git.getRepository().getRef(Constants.R_STASH);
284 		assertNotNull(stashRef);
285 		assertEquals(thirdStash, stashRef.getObjectId());
286 
287 		ReflogReader reader = git.getRepository().getReflogReader(
288 				Constants.R_STASH);
289 		List<ReflogEntry> entries = reader.getReverseEntries();
290 		assertEquals(2, entries.size());
291 		assertEquals(ObjectId.zeroId(), entries.get(1).getOldId());
292 		assertEquals(secondStash, entries.get(1).getNewId());
293 		assertTrue(entries.get(1).getComment().length() > 0);
294 		assertEquals(entries.get(0).getOldId(), entries.get(1).getNewId());
295 		assertEquals(thirdStash, entries.get(0).getNewId());
296 		assertTrue(entries.get(0).getComment().length() > 0);
297 	}
298 }