You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReadTreeTest.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2008-2009, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.lib;
  47. import java.io.ByteArrayInputStream;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.util.ArrayList;
  53. import java.util.Arrays;
  54. import java.util.HashMap;
  55. import org.eclipse.jgit.errors.CheckoutConflictException;
  56. import org.eclipse.jgit.errors.CorruptObjectException;
  57. import org.eclipse.jgit.treewalk.FileTreeIterator;
  58. import org.eclipse.jgit.treewalk.TreeWalk;
  59. import org.eclipse.jgit.util.FS;
  60. public abstract class ReadTreeTest extends RepositoryTestCase {
  61. protected Tree theHead;
  62. protected Tree theMerge;
  63. // Each of these rules are from the read-tree manpage
  64. // go there to see what they mean.
  65. // Rule 0 is left out for obvious reasons :)
  66. public void testRules1thru3_NoIndexEntry() throws IOException {
  67. Tree head = new Tree(db);
  68. FileTreeEntry headFile = head.addFile("foo");
  69. ObjectId objectId = ObjectId.fromString("ba78e065e2c261d4f7b8f42107588051e87e18e9");
  70. headFile.setId(objectId);
  71. Tree merge = new Tree(db);
  72. prescanTwoTrees(head, merge);
  73. assertTrue(getRemoved().contains("foo"));
  74. prescanTwoTrees(merge, head);
  75. assertEquals(objectId, getUpdated().get("foo"));
  76. ObjectId anotherId = ObjectId.fromString("ba78e065e2c261d4f7b8f42107588051e87e18ee");
  77. merge.addFile("foo").setId(anotherId);
  78. prescanTwoTrees(head, merge);
  79. assertEquals(anotherId, getUpdated().get("foo"));
  80. }
  81. void setupCase(HashMap<String, String> headEntries,
  82. HashMap<String, String> mergeEntries,
  83. HashMap<String, String> indexEntries) throws IOException {
  84. theHead = buildTree(headEntries);
  85. theMerge = buildTree(mergeEntries);
  86. buildIndex(indexEntries);
  87. }
  88. private void buildIndex(HashMap<String, String> indexEntries) throws IOException {
  89. GitIndex index = new GitIndex(db);
  90. if (indexEntries != null) {
  91. for (java.util.Map.Entry<String,String> e : indexEntries.entrySet()) {
  92. index.add(trash, writeTrashFile(e.getKey(), e.getValue())).forceRecheck();
  93. }
  94. }
  95. index.write();
  96. db.getIndex().read();
  97. }
  98. private Tree buildTree(HashMap<String, String> headEntries) throws IOException {
  99. Tree tree = new Tree(db);
  100. ObjectWriter ow = new ObjectWriter(db);
  101. if (headEntries == null)
  102. return tree;
  103. FileTreeEntry fileEntry;
  104. Tree parent;
  105. for (java.util.Map.Entry<String, String> e : headEntries.entrySet()) {
  106. fileEntry = tree.addFile(e.getKey());
  107. fileEntry.setId(genSha1(e.getValue()));
  108. parent = fileEntry.getParent();
  109. while (parent != null) {
  110. parent.setId(ow.writeTree(parent));
  111. parent = parent.getParent();
  112. }
  113. }
  114. return tree;
  115. }
  116. ObjectId genSha1(String data) {
  117. InputStream is = new ByteArrayInputStream(data.getBytes());
  118. ObjectWriter objectWriter = new ObjectWriter(db);
  119. try {
  120. return objectWriter.writeObject(Constants.OBJ_BLOB, data
  121. .getBytes().length, is, true);
  122. } catch (IOException e) {
  123. fail(e.toString());
  124. }
  125. return null;
  126. }
  127. protected void go() throws IllegalStateException, IOException {
  128. prescanTwoTrees(theHead, theMerge);
  129. }
  130. // for these rules, they all have clean yes/no options
  131. // but it doesn't matter if the entry is clean or not
  132. // so we can just ignore the state in the filesystem entirely
  133. public void testRules4thru13_IndexEntryNotInHead() throws IOException {
  134. // rules 4 and 5
  135. HashMap<String, String> idxMap;
  136. idxMap = new HashMap<String, String>();
  137. idxMap.put("foo", "foo");
  138. setupCase(null, null, idxMap);
  139. go();
  140. assertTrue(getUpdated().isEmpty());
  141. assertTrue(getRemoved().isEmpty());
  142. assertTrue(getConflicts().isEmpty());
  143. // rules 6 and 7
  144. idxMap = new HashMap<String, String>();
  145. idxMap.put("foo", "foo");
  146. setupCase(null, idxMap, idxMap);
  147. go();
  148. assertAllEmpty();
  149. // rules 8 and 9
  150. HashMap<String, String> mergeMap;
  151. mergeMap = new HashMap<String, String>();
  152. mergeMap.put("foo", "merge");
  153. setupCase(null, mergeMap, idxMap);
  154. go();
  155. assertTrue(getUpdated().isEmpty());
  156. assertTrue(getRemoved().isEmpty());
  157. assertTrue(getConflicts().contains("foo"));
  158. // rule 10
  159. HashMap<String, String> headMap = new HashMap<String, String>();
  160. headMap.put("foo", "foo");
  161. setupCase(headMap, null, idxMap);
  162. go();
  163. assertTrue(getRemoved().contains("foo"));
  164. assertTrue(getUpdated().isEmpty());
  165. assertTrue(getConflicts().isEmpty());
  166. // rule 11
  167. setupCase(headMap, null, idxMap);
  168. new File(trash, "foo").delete();
  169. writeTrashFile("foo", "bar");
  170. db.getIndex().getMembers()[0].forceRecheck();
  171. go();
  172. assertTrue(getRemoved().isEmpty());
  173. assertTrue(getUpdated().isEmpty());
  174. assertTrue(getConflicts().contains("foo"));
  175. // rule 12 & 13
  176. headMap.put("foo", "head");
  177. setupCase(headMap, null, idxMap);
  178. go();
  179. assertTrue(getRemoved().isEmpty());
  180. assertTrue(getUpdated().isEmpty());
  181. assertTrue(getConflicts().contains("foo"));
  182. // rules 14 & 15
  183. setupCase(headMap, headMap, idxMap);
  184. go();
  185. assertAllEmpty();
  186. // rules 16 & 17
  187. setupCase(headMap, mergeMap, idxMap); go();
  188. assertTrue(getConflicts().contains("foo"));
  189. // rules 18 & 19
  190. setupCase(headMap, idxMap, idxMap); go();
  191. assertAllEmpty();
  192. // rule 20
  193. setupCase(idxMap, mergeMap, idxMap); go();
  194. assertTrue(getUpdated().containsKey("foo"));
  195. // rules 21
  196. setupCase(idxMap, mergeMap, idxMap);
  197. new File(trash, "foo").delete();
  198. writeTrashFile("foo", "bar");
  199. db.getIndex().getMembers()[0].forceRecheck();
  200. go();
  201. assertTrue(getConflicts().contains("foo"));
  202. }
  203. private void assertAllEmpty() {
  204. assertTrue(getRemoved().isEmpty());
  205. assertTrue(getUpdated().isEmpty());
  206. assertTrue(getConflicts().isEmpty());
  207. }
  208. public void testDirectoryFileSimple() throws IOException {
  209. Tree treeDF = buildTree(mkmap("DF", "DF"));
  210. Tree treeDFDF = buildTree(mkmap("DF/DF", "DF/DF"));
  211. buildIndex(mkmap("DF", "DF"));
  212. prescanTwoTrees(treeDF, treeDFDF);
  213. assertTrue(getRemoved().contains("DF"));
  214. assertTrue(getUpdated().containsKey("DF/DF"));
  215. recursiveDelete(new File(trash, "DF"));
  216. buildIndex(mkmap("DF/DF", "DF/DF"));
  217. prescanTwoTrees(treeDFDF, treeDF);
  218. assertTrue(getRemoved().contains("DF/DF"));
  219. assertTrue(getUpdated().containsKey("DF"));
  220. }
  221. /*
  222. * Directory/File Conflict cases:
  223. * It's entirely possible that in practice a number of these may be equivalent
  224. * to the cases described in git-read-tree.txt. As long as it does the right thing,
  225. * that's all I care about. These are basically reverse-engineered from
  226. * what git currently does. If there are tests for these in git, it's kind of
  227. * hard to track them all down...
  228. *
  229. * H I M Clean H==M H==I I==M Result
  230. * ------------------------------------------------------------------
  231. *1 D D F Y N Y N Update
  232. *2 D D F N N Y N Conflict
  233. *3 D F D Y N N Update
  234. *4 D F D N N N Update
  235. *5 D F F Y N N Y Keep
  236. *6 D F F N N N Y Keep
  237. *7 F D F Y Y N N Update
  238. *8 F D F N Y N N Conflict
  239. *9 F D F Y N N N Update
  240. *10 F D D N N Y Keep
  241. *11 F D D N N N Conflict
  242. *12 F F D Y N Y N Update
  243. *13 F F D N N Y N Conflict
  244. *14 F F D N N N Conflict
  245. *15 0 F D N N N Conflict
  246. *16 0 D F Y N N N Update
  247. *17 0 D F N N N Conflict
  248. *18 F 0 D Update
  249. *19 D 0 F Update
  250. */
  251. public void testDirectoryFileConflicts_1() throws Exception {
  252. // 1
  253. doit(mk("DF/DF"), mk("DF"), mk("DF/DF"));
  254. assertNoConflicts();
  255. assertUpdated("DF");
  256. assertRemoved("DF/DF");
  257. }
  258. public void testDirectoryFileConflicts_2() throws Exception {
  259. // 2
  260. setupCase(mk("DF/DF"), mk("DF"), mk("DF/DF"));
  261. writeTrashFile("DF/DF", "different");
  262. go();
  263. assertConflict("DF/DF");
  264. }
  265. public void testDirectoryFileConflicts_3() throws Exception {
  266. // 3 - the first to break!
  267. doit(mk("DF/DF"), mk("DF/DF"), mk("DF"));
  268. assertUpdated("DF/DF");
  269. assertRemoved("DF");
  270. }
  271. public void testDirectoryFileConflicts_4() throws Exception {
  272. // 4 (basically same as 3, just with H and M different)
  273. doit(mk("DF/DF"), mkmap("DF/DF", "foo"), mk("DF"));
  274. assertUpdated("DF/DF");
  275. assertRemoved("DF");
  276. }
  277. public void testDirectoryFileConflicts_5() throws Exception {
  278. // 5
  279. doit(mk("DF/DF"), mk("DF"), mk("DF"));
  280. assertRemoved("DF/DF");
  281. }
  282. public void testDirectoryFileConflicts_6() throws Exception {
  283. // 6
  284. setupCase(mk("DF/DF"), mk("DF"), mk("DF"));
  285. writeTrashFile("DF", "different");
  286. go();
  287. assertRemoved("DF/DF");
  288. }
  289. public void testDirectoryFileConflicts_7() throws Exception {
  290. // 7
  291. doit(mk("DF"), mk("DF"), mk("DF/DF"));
  292. assertUpdated("DF");
  293. assertRemoved("DF/DF");
  294. cleanUpDF();
  295. setupCase(mk("DF/DF"), mk("DF/DF"), mk("DF/DF/DF/DF/DF"));
  296. go();
  297. assertRemoved("DF/DF/DF/DF/DF");
  298. assertUpdated("DF/DF");
  299. cleanUpDF();
  300. setupCase(mk("DF/DF"), mk("DF/DF"), mk("DF/DF/DF/DF/DF"));
  301. writeTrashFile("DF/DF/DF/DF/DF", "diff");
  302. go();
  303. assertConflict("DF/DF/DF/DF/DF");
  304. assertUpdated("DF/DF");
  305. }
  306. // 8 ?
  307. public void testDirectoryFileConflicts_9() throws Exception {
  308. // 9
  309. doit(mk("DF"), mkmap("DF", "QP"), mk("DF/DF"));
  310. assertRemoved("DF/DF");
  311. assertUpdated("DF");
  312. }
  313. public void testDirectoryFileConflicts_10() throws Exception {
  314. // 10
  315. cleanUpDF();
  316. doit(mk("DF"), mk("DF/DF"), mk("DF/DF"));
  317. assertNoConflicts();
  318. }
  319. public void testDirectoryFileConflicts_11() throws Exception {
  320. // 11
  321. doit(mk("DF"), mk("DF/DF"), mkmap("DF/DF", "asdf"));
  322. assertConflict("DF/DF");
  323. }
  324. public void testDirectoryFileConflicts_12() throws Exception {
  325. // 12
  326. cleanUpDF();
  327. doit(mk("DF"), mk("DF/DF"), mk("DF"));
  328. assertRemoved("DF");
  329. assertUpdated("DF/DF");
  330. }
  331. public void testDirectoryFileConflicts_13() throws Exception {
  332. // 13
  333. cleanUpDF();
  334. setupCase(mk("DF"), mk("DF/DF"), mk("DF"));
  335. writeTrashFile("DF", "asdfsdf");
  336. go();
  337. assertConflict("DF");
  338. assertUpdated("DF/DF");
  339. }
  340. public void testDirectoryFileConflicts_14() throws Exception {
  341. // 14
  342. cleanUpDF();
  343. doit(mk("DF"), mk("DF/DF"), mkmap("DF", "Foo"));
  344. assertConflict("DF");
  345. assertUpdated("DF/DF");
  346. }
  347. public void testDirectoryFileConflicts_15() throws Exception {
  348. // 15
  349. doit(mkmap(), mk("DF/DF"), mk("DF"));
  350. assertRemoved("DF");
  351. assertUpdated("DF/DF");
  352. }
  353. public void testDirectoryFileConflicts_15b() throws Exception {
  354. // 15, take 2, just to check multi-leveled
  355. doit(mkmap(), mk("DF/DF/DF/DF"), mk("DF"));
  356. assertRemoved("DF");
  357. assertUpdated("DF/DF/DF/DF");
  358. }
  359. public void testDirectoryFileConflicts_16() throws Exception {
  360. // 16
  361. cleanUpDF();
  362. doit(mkmap(), mk("DF"), mk("DF/DF/DF"));
  363. assertRemoved("DF/DF/DF");
  364. assertUpdated("DF");
  365. }
  366. public void testDirectoryFileConflicts_17() throws Exception {
  367. // 17
  368. cleanUpDF();
  369. setupCase(mkmap(), mk("DF"), mk("DF/DF/DF"));
  370. writeTrashFile("DF/DF/DF", "asdf");
  371. go();
  372. assertConflict("DF/DF/DF");
  373. assertUpdated("DF");
  374. }
  375. public void testDirectoryFileConflicts_18() throws Exception {
  376. // 18
  377. cleanUpDF();
  378. doit(mk("DF/DF"), mk("DF/DF/DF/DF"), null);
  379. assertRemoved("DF/DF");
  380. assertUpdated("DF/DF/DF/DF");
  381. }
  382. public void testDirectoryFileConflicts_19() throws Exception {
  383. // 19
  384. cleanUpDF();
  385. doit(mk("DF/DF/DF/DF"), mk("DF/DF/DF"), null);
  386. assertRemoved("DF/DF/DF/DF");
  387. assertUpdated("DF/DF/DF");
  388. }
  389. private void cleanUpDF() throws Exception {
  390. tearDown();
  391. setUp();
  392. recursiveDelete(new File(trash, "DF"));
  393. }
  394. private void assertConflict(String s) {
  395. assertTrue(getConflicts().contains(s));
  396. }
  397. private void assertUpdated(String s) {
  398. assertTrue(getUpdated().containsKey(s));
  399. }
  400. private void assertRemoved(String s) {
  401. assertTrue(getRemoved().contains(s));
  402. }
  403. private void assertNoConflicts() {
  404. assertTrue(getConflicts().isEmpty());
  405. }
  406. private void doit(HashMap<String, String> h, HashMap<String, String> m,
  407. HashMap<String, String> i) throws IOException {
  408. setupCase(h, m, i);
  409. go();
  410. }
  411. protected static HashMap<String, String> mk(String a) {
  412. return mkmap(a, a);
  413. }
  414. protected static HashMap<String, String> mkmap(String... args) {
  415. if ((args.length % 2) > 0)
  416. throw new IllegalArgumentException("needs to be pairs");
  417. HashMap<String, String> map = new HashMap<String, String>();
  418. for (int i = 0; i < args.length; i += 2) {
  419. map.put(args[i], args[i+1]);
  420. }
  421. return map;
  422. }
  423. public void testUntrackedConflicts() throws IOException {
  424. setupCase(null, mk("foo"), null);
  425. writeTrashFile("foo", "foo");
  426. go();
  427. assertConflict("foo");
  428. recursiveDelete(new File(trash, "foo"));
  429. setupCase(null, mk("foo"), null);
  430. writeTrashFile("foo/bar/baz", "");
  431. writeTrashFile("foo/blahblah", "");
  432. go();
  433. assertConflict("foo/bar/baz");
  434. assertConflict("foo/blahblah");
  435. recursiveDelete(new File(trash, "foo"));
  436. setupCase(mkmap("foo/bar", "", "foo/baz", ""),
  437. mk("foo"), mkmap("foo/bar", "", "foo/baz", ""));
  438. assertTrue(new File(trash, "foo/bar").exists());
  439. go();
  440. assertNoConflicts();
  441. }
  442. public void testCloseNameConflictsX0() throws IOException {
  443. setupCase(mkmap("a/a", "a/a-c"), mkmap("a/a","a/a", "b.b/b.b","b.b/b.bs"), mkmap("a/a", "a/a-c") );
  444. checkout();
  445. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  446. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  447. go();
  448. assertIndex(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  449. assertWorkDir(mkmap("a/a", "a/a", "b.b/b.b", "b.b/b.bs"));
  450. assertNoConflicts();
  451. }
  452. public void testCloseNameConflicts1() throws IOException {
  453. setupCase(mkmap("a/a", "a/a-c"), mkmap("a/a","a/a", "a.a/a.a","a.a/a.a"), mkmap("a/a", "a/a-c") );
  454. checkout();
  455. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  456. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  457. go();
  458. assertIndex(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  459. assertWorkDir(mkmap("a/a", "a/a", "a.a/a.a", "a.a/a.a"));
  460. assertNoConflicts();
  461. }
  462. public void testCheckoutOutChanges() throws IOException {
  463. setupCase(mk("foo"), mk("foo/bar"), mk("foo"));
  464. checkout();
  465. assertIndex(mk("foo/bar"));
  466. assertWorkDir(mk("foo/bar"));
  467. assertFalse(new File(trash, "foo").isFile());
  468. assertTrue(new File(trash, "foo/bar").isFile());
  469. recursiveDelete(new File(trash, "foo"));
  470. assertWorkDir(mkmap());
  471. setupCase(mk("foo/bar"), mk("foo"), mk("foo/bar"));
  472. checkout();
  473. assertIndex(mk("foo"));
  474. assertWorkDir(mk("foo"));
  475. assertFalse(new File(trash, "foo/bar").isFile());
  476. assertTrue(new File(trash, "foo").isFile());
  477. setupCase(mk("foo"), mkmap("foo", "qux"), mkmap("foo", "bar"));
  478. assertIndex(mkmap("foo", "bar"));
  479. assertWorkDir(mkmap("foo", "bar"));
  480. try {
  481. checkout();
  482. fail("did not throw exception");
  483. } catch (CheckoutConflictException e) {
  484. assertIndex(mkmap("foo", "bar"));
  485. assertWorkDir(mkmap("foo", "bar"));
  486. }
  487. }
  488. public void testCheckoutUncachedChanges() throws IOException {
  489. setupCase(mk("foo"), mk("foo"), mk("foo"));
  490. writeTrashFile("foo", "otherData");
  491. checkout();
  492. assertIndex(mk("foo"));
  493. assertWorkDir(mkmap("foo", "otherData"));
  494. assertTrue(new File(trash, "foo").isFile());
  495. }
  496. /**
  497. * The interface these tests need from a class implementing a checkout
  498. */
  499. interface Checkout {
  500. HashMap<String, ObjectId> updated();
  501. ArrayList<String> conflicts();
  502. ArrayList<String> removed();
  503. void prescanTwoTrees() throws IOException;
  504. void checkout() throws IOException;
  505. }
  506. public void assertWorkDir(HashMap<String, String> i)
  507. throws CorruptObjectException, IOException {
  508. TreeWalk walk = new TreeWalk(db);
  509. walk.reset();
  510. walk.setRecursive(true);
  511. walk.addTree(new FileTreeIterator(db.getWorkDir(), FS.DETECTED));
  512. String expectedValue;
  513. String path;
  514. int nrFiles = 0;
  515. FileTreeIterator ft;
  516. while (walk.next()) {
  517. ft = walk.getTree(0, FileTreeIterator.class);
  518. path = ft.getEntryPathString();
  519. expectedValue = i.get(path);
  520. assertNotNull("found unexpected file for path "
  521. + path + " in workdir", expectedValue);
  522. File file = new File(db.getWorkDir(), path);
  523. assertTrue(file.exists());
  524. if (file.isFile()) {
  525. FileInputStream is = new FileInputStream(file);
  526. byte[] buffer = new byte[(int) file.length()];
  527. int offset = 0;
  528. int numRead = 0;
  529. while (offset < buffer.length
  530. && (numRead = is.read(buffer, offset, buffer.length
  531. - offset)) >= 0) {
  532. offset += numRead;
  533. }
  534. is.close();
  535. assertTrue("unexpected content for path " + path
  536. + " in workDir. Expected: <" + expectedValue + ">",
  537. Arrays.equals(buffer, i.get(path).getBytes()));
  538. nrFiles++;
  539. }
  540. }
  541. assertEquals("WorkDir has not the right size.", i.size(), nrFiles);
  542. }
  543. public void assertIndex(HashMap<String, String> i)
  544. throws CorruptObjectException, IOException {
  545. String expectedValue;
  546. String path;
  547. GitIndex theIndex=db.getIndex();
  548. assertEquals("Index has not the right size.", i.size(),
  549. theIndex.getMembers().length);
  550. for (int j = 0; j < theIndex.getMembers().length; j++) {
  551. path = theIndex.getMembers()[j].getName();
  552. expectedValue = i.get(path);
  553. assertNotNull("found unexpected entry for path " + path
  554. + " in index", expectedValue);
  555. assertTrue("unexpected content for path " + path
  556. + " in index. Expected: <" + expectedValue + ">",
  557. Arrays.equals(
  558. db.openBlob(theIndex.getMembers()[j].getObjectId())
  559. .getBytes(), i.get(path).getBytes()));
  560. }
  561. }
  562. public abstract void prescanTwoTrees(Tree head, Tree merge) throws IllegalStateException, IOException;
  563. public abstract void checkout() throws IOException;
  564. public abstract ArrayList<String> getRemoved();
  565. public abstract HashMap<String, ObjectId> getUpdated();
  566. public abstract ArrayList<String> getConflicts();
  567. }