PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/jgit-2.0.0.201206130900-r/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashApplyCommandTest.java

#
Java | 456 lines | 335 code | 76 blank | 45 comment | 0 complexity | cc611695240efda30cb591bb2d4b29ce MD5 | raw file
Possible License(s): BSD-3-Clause
  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. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.File;
  50. import java.text.MessageFormat;
  51. import org.eclipse.jgit.api.errors.InvalidRefNameException;
  52. import org.eclipse.jgit.api.errors.JGitInternalException;
  53. import org.eclipse.jgit.api.errors.NoHeadException;
  54. import org.eclipse.jgit.errors.CheckoutConflictException;
  55. import org.eclipse.jgit.internal.JGitText;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.eclipse.jgit.lib.Repository;
  58. import org.eclipse.jgit.lib.RepositoryTestCase;
  59. import org.eclipse.jgit.revwalk.RevCommit;
  60. import org.eclipse.jgit.util.FileUtils;
  61. import org.junit.Before;
  62. import org.junit.Test;
  63. /**
  64. * Unit tests of {@link StashApplyCommand}
  65. */
  66. public class StashApplyCommandTest extends RepositoryTestCase {
  67. private static final String PATH = "file.txt";
  68. private RevCommit head;
  69. private Git git;
  70. private File committedFile;
  71. @Before
  72. public void setUp() throws Exception {
  73. super.setUp();
  74. git = Git.wrap(db);
  75. committedFile = writeTrashFile(PATH, "content");
  76. git.add().addFilepattern(PATH).call();
  77. head = git.commit().setMessage("add file").call();
  78. assertNotNull(head);
  79. }
  80. @Test
  81. public void workingDirectoryDelete() throws Exception {
  82. deleteTrashFile(PATH);
  83. assertFalse(committedFile.exists());
  84. RevCommit stashed = git.stashCreate().call();
  85. assertNotNull(stashed);
  86. assertEquals("content", read(committedFile));
  87. ObjectId unstashed = git.stashApply().call();
  88. assertEquals(stashed, unstashed);
  89. assertFalse(committedFile.exists());
  90. Status status = git.status().call();
  91. assertTrue(status.getAdded().isEmpty());
  92. assertTrue(status.getChanged().isEmpty());
  93. assertTrue(status.getConflicting().isEmpty());
  94. assertTrue(status.getModified().isEmpty());
  95. assertTrue(status.getUntracked().isEmpty());
  96. assertTrue(status.getRemoved().isEmpty());
  97. assertEquals(1, status.getMissing().size());
  98. assertTrue(status.getMissing().contains(PATH));
  99. }
  100. @Test
  101. public void indexAdd() throws Exception {
  102. String addedPath = "file2.txt";
  103. File addedFile = writeTrashFile(addedPath, "content2");
  104. git.add().addFilepattern(addedPath).call();
  105. RevCommit stashed = git.stashCreate().call();
  106. assertNotNull(stashed);
  107. assertFalse(addedFile.exists());
  108. ObjectId unstashed = git.stashApply().call();
  109. assertEquals(stashed, unstashed);
  110. assertTrue(addedFile.exists());
  111. assertEquals("content2", read(addedFile));
  112. Status status = git.status().call();
  113. assertTrue(status.getChanged().isEmpty());
  114. assertTrue(status.getConflicting().isEmpty());
  115. assertTrue(status.getMissing().isEmpty());
  116. assertTrue(status.getModified().isEmpty());
  117. assertTrue(status.getRemoved().isEmpty());
  118. assertTrue(status.getUntracked().isEmpty());
  119. assertEquals(1, status.getAdded().size());
  120. assertTrue(status.getAdded().contains(addedPath));
  121. }
  122. @Test
  123. public void indexDelete() throws Exception {
  124. git.rm().addFilepattern("file.txt").call();
  125. RevCommit stashed = git.stashCreate().call();
  126. assertNotNull(stashed);
  127. assertEquals("content", read(committedFile));
  128. ObjectId unstashed = git.stashApply().call();
  129. assertEquals(stashed, unstashed);
  130. assertFalse(committedFile.exists());
  131. Status status = git.status().call();
  132. assertTrue(status.getAdded().isEmpty());
  133. assertTrue(status.getChanged().isEmpty());
  134. assertTrue(status.getConflicting().isEmpty());
  135. assertTrue(status.getModified().isEmpty());
  136. assertTrue(status.getMissing().isEmpty());
  137. assertTrue(status.getUntracked().isEmpty());
  138. assertEquals(1, status.getRemoved().size());
  139. assertTrue(status.getRemoved().contains(PATH));
  140. }
  141. @Test
  142. public void workingDirectoryModify() throws Exception {
  143. writeTrashFile("file.txt", "content2");
  144. RevCommit stashed = git.stashCreate().call();
  145. assertNotNull(stashed);
  146. assertEquals("content", read(committedFile));
  147. ObjectId unstashed = git.stashApply().call();
  148. assertEquals(stashed, unstashed);
  149. assertEquals("content2", read(committedFile));
  150. Status status = git.status().call();
  151. assertTrue(status.getAdded().isEmpty());
  152. assertTrue(status.getChanged().isEmpty());
  153. assertTrue(status.getConflicting().isEmpty());
  154. assertTrue(status.getMissing().isEmpty());
  155. assertTrue(status.getRemoved().isEmpty());
  156. assertTrue(status.getUntracked().isEmpty());
  157. assertEquals(1, status.getModified().size());
  158. assertTrue(status.getModified().contains(PATH));
  159. }
  160. @Test
  161. public void workingDirectoryModifyInSubfolder() throws Exception {
  162. String path = "d1/d2/f.txt";
  163. File subfolderFile = writeTrashFile(path, "content");
  164. git.add().addFilepattern(path).call();
  165. head = git.commit().setMessage("add file").call();
  166. writeTrashFile(path, "content2");
  167. RevCommit stashed = git.stashCreate().call();
  168. assertNotNull(stashed);
  169. assertEquals("content", read(subfolderFile));
  170. ObjectId unstashed = git.stashApply().call();
  171. assertEquals(stashed, unstashed);
  172. assertEquals("content2", read(subfolderFile));
  173. Status status = git.status().call();
  174. assertTrue(status.getAdded().isEmpty());
  175. assertTrue(status.getChanged().isEmpty());
  176. assertTrue(status.getConflicting().isEmpty());
  177. assertTrue(status.getMissing().isEmpty());
  178. assertTrue(status.getRemoved().isEmpty());
  179. assertTrue(status.getUntracked().isEmpty());
  180. assertEquals(1, status.getModified().size());
  181. assertTrue(status.getModified().contains(path));
  182. }
  183. @Test
  184. public void workingDirectoryModifyIndexChanged() throws Exception {
  185. writeTrashFile("file.txt", "content2");
  186. git.add().addFilepattern("file.txt").call();
  187. writeTrashFile("file.txt", "content3");
  188. RevCommit stashed = git.stashCreate().call();
  189. assertNotNull(stashed);
  190. assertEquals("content", read(committedFile));
  191. ObjectId unstashed = git.stashApply().call();
  192. assertEquals(stashed, unstashed);
  193. assertEquals("content3", read(committedFile));
  194. Status status = git.status().call();
  195. assertTrue(status.getAdded().isEmpty());
  196. assertTrue(status.getConflicting().isEmpty());
  197. assertTrue(status.getMissing().isEmpty());
  198. assertTrue(status.getRemoved().isEmpty());
  199. assertTrue(status.getUntracked().isEmpty());
  200. assertEquals(1, status.getChanged().size());
  201. assertTrue(status.getChanged().contains(PATH));
  202. assertEquals(1, status.getModified().size());
  203. assertTrue(status.getModified().contains(PATH));
  204. }
  205. @Test
  206. public void workingDirectoryCleanIndexModify() throws Exception {
  207. writeTrashFile("file.txt", "content2");
  208. git.add().addFilepattern("file.txt").call();
  209. writeTrashFile("file.txt", "content");
  210. RevCommit stashed = git.stashCreate().call();
  211. assertNotNull(stashed);
  212. assertEquals("content", read(committedFile));
  213. ObjectId unstashed = git.stashApply().call();
  214. assertEquals(stashed, unstashed);
  215. assertEquals("content2", read(committedFile));
  216. Status status = git.status().call();
  217. assertTrue(status.getAdded().isEmpty());
  218. assertTrue(status.getConflicting().isEmpty());
  219. assertTrue(status.getMissing().isEmpty());
  220. assertTrue(status.getModified().isEmpty());
  221. assertTrue(status.getRemoved().isEmpty());
  222. assertTrue(status.getUntracked().isEmpty());
  223. assertEquals(1, status.getChanged().size());
  224. assertTrue(status.getChanged().contains(PATH));
  225. }
  226. @Test
  227. public void workingDirectoryDeleteIndexAdd() throws Exception {
  228. String path = "file2.txt";
  229. File added = writeTrashFile(path, "content2");
  230. assertTrue(added.exists());
  231. git.add().addFilepattern(path).call();
  232. FileUtils.delete(added);
  233. assertFalse(added.exists());
  234. RevCommit stashed = git.stashCreate().call();
  235. assertNotNull(stashed);
  236. assertFalse(added.exists());
  237. ObjectId unstashed = git.stashApply().call();
  238. assertEquals(stashed, unstashed);
  239. assertEquals("content2", read(added));
  240. Status status = git.status().call();
  241. assertTrue(status.getChanged().isEmpty());
  242. assertTrue(status.getConflicting().isEmpty());
  243. assertTrue(status.getMissing().isEmpty());
  244. assertTrue(status.getModified().isEmpty());
  245. assertTrue(status.getRemoved().isEmpty());
  246. assertTrue(status.getUntracked().isEmpty());
  247. assertEquals(1, status.getAdded().size());
  248. assertTrue(status.getAdded().contains(path));
  249. }
  250. @Test
  251. public void workingDirectoryDeleteIndexEdit() throws Exception {
  252. writeTrashFile(PATH, "content2");
  253. git.add().addFilepattern(PATH).call();
  254. FileUtils.delete(committedFile);
  255. assertFalse(committedFile.exists());
  256. RevCommit stashed = git.stashCreate().call();
  257. assertNotNull(stashed);
  258. assertEquals("content", read(committedFile));
  259. ObjectId unstashed = git.stashApply().call();
  260. assertEquals(stashed, unstashed);
  261. assertFalse(committedFile.exists());
  262. Status status = git.status().call();
  263. assertTrue(status.getAdded().isEmpty());
  264. assertTrue(status.getChanged().isEmpty());
  265. assertTrue(status.getConflicting().isEmpty());
  266. assertTrue(status.getMissing().isEmpty());
  267. assertTrue(status.getModified().isEmpty());
  268. assertTrue(status.getUntracked().isEmpty());
  269. assertEquals(1, status.getRemoved().size());
  270. assertTrue(status.getRemoved().contains(PATH));
  271. }
  272. @Test
  273. public void multipleEdits() throws Exception {
  274. String addedPath = "file2.txt";
  275. git.rm().addFilepattern(PATH).call();
  276. File addedFile = writeTrashFile(addedPath, "content2");
  277. git.add().addFilepattern(addedPath).call();
  278. RevCommit stashed = git.stashCreate().call();
  279. assertNotNull(stashed);
  280. assertTrue(committedFile.exists());
  281. assertFalse(addedFile.exists());
  282. ObjectId unstashed = git.stashApply().call();
  283. assertEquals(stashed, unstashed);
  284. Status status = git.status().call();
  285. assertTrue(status.getChanged().isEmpty());
  286. assertTrue(status.getConflicting().isEmpty());
  287. assertTrue(status.getMissing().isEmpty());
  288. assertTrue(status.getModified().isEmpty());
  289. assertTrue(status.getUntracked().isEmpty());
  290. assertEquals(1, status.getRemoved().size());
  291. assertTrue(status.getRemoved().contains(PATH));
  292. assertEquals(1, status.getAdded().size());
  293. assertTrue(status.getAdded().contains(addedPath));
  294. }
  295. @Test
  296. public void workingDirectoryContentConflict() throws Exception {
  297. writeTrashFile(PATH, "content2");
  298. RevCommit stashed = git.stashCreate().call();
  299. assertNotNull(stashed);
  300. assertEquals("content", read(committedFile));
  301. assertTrue(git.status().call().isClean());
  302. writeTrashFile(PATH, "content3");
  303. try {
  304. git.stashApply().call();
  305. fail("Exception not thrown");
  306. } catch (JGitInternalException e) {
  307. assertTrue(e.getCause() instanceof CheckoutConflictException);
  308. }
  309. }
  310. @Test
  311. public void indexContentConflict() throws Exception {
  312. writeTrashFile(PATH, "content2");
  313. RevCommit stashed = git.stashCreate().call();
  314. assertNotNull(stashed);
  315. assertEquals("content", read(committedFile));
  316. assertTrue(git.status().call().isClean());
  317. writeTrashFile(PATH, "content3");
  318. git.add().addFilepattern(PATH).call();
  319. writeTrashFile(PATH, "content2");
  320. try {
  321. git.stashApply().call();
  322. fail("Exception not thrown");
  323. } catch (JGitInternalException e) {
  324. assertTrue(e.getCause() instanceof CheckoutConflictException);
  325. }
  326. }
  327. @Test
  328. public void workingDirectoryEditPreCommit() throws Exception {
  329. writeTrashFile(PATH, "content2");
  330. RevCommit stashed = git.stashCreate().call();
  331. assertNotNull(stashed);
  332. assertEquals("content", read(committedFile));
  333. assertTrue(git.status().call().isClean());
  334. String path2 = "file2.txt";
  335. writeTrashFile(path2, "content3");
  336. git.add().addFilepattern(path2).call();
  337. assertNotNull(git.commit().setMessage("adding file").call());
  338. ObjectId unstashed = git.stashApply().call();
  339. assertEquals(stashed, unstashed);
  340. Status status = git.status().call();
  341. assertTrue(status.getAdded().isEmpty());
  342. assertTrue(status.getChanged().isEmpty());
  343. assertTrue(status.getConflicting().isEmpty());
  344. assertTrue(status.getMissing().isEmpty());
  345. assertTrue(status.getRemoved().isEmpty());
  346. assertTrue(status.getUntracked().isEmpty());
  347. assertEquals(1, status.getModified().size());
  348. assertTrue(status.getModified().contains(PATH));
  349. }
  350. @Test
  351. public void unstashNonStashCommit() throws Exception {
  352. try {
  353. git.stashApply().setStashRef(head.name()).call();
  354. fail("Exception not thrown");
  355. } catch (JGitInternalException e) {
  356. assertEquals(MessageFormat.format(
  357. JGitText.get().stashCommitMissingTwoParents, head.name()),
  358. e.getMessage());
  359. }
  360. }
  361. @Test
  362. public void unstashNoHead() throws Exception {
  363. Repository repo = createWorkRepository();
  364. try {
  365. Git.wrap(repo).stashApply().call();
  366. fail("Exception not thrown");
  367. } catch (NoHeadException e) {
  368. assertNotNull(e.getMessage());
  369. }
  370. }
  371. @Test
  372. public void noStashedCommits() throws Exception {
  373. try {
  374. git.stashApply().call();
  375. fail("Exception not thrown");
  376. } catch (InvalidRefNameException e) {
  377. assertNotNull(e.getMessage());
  378. }
  379. }
  380. }