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.

DirCacheCGitCompatabilityTest.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 2008-2010, Google 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.dircache;
  44. import java.io.BufferedReader;
  45. import java.io.File;
  46. import java.io.FileInputStream;
  47. import java.io.InputStreamReader;
  48. import java.util.ArrayList;
  49. import java.util.Iterator;
  50. import java.util.LinkedHashMap;
  51. import java.util.Map;
  52. import org.eclipse.jgit.errors.CorruptObjectException;
  53. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  54. import org.eclipse.jgit.lib.FileMode;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.treewalk.TreeWalk;
  58. import org.eclipse.jgit.util.JGitTestUtil;
  59. public class DirCacheCGitCompatabilityTest extends LocalDiskRepositoryTestCase {
  60. private final File index = pathOf("gitgit.index");
  61. public void testReadIndex_LsFiles() throws Exception {
  62. final Map<String, CGitIndexRecord> ls = readLsFiles();
  63. final DirCache dc = new DirCache(index);
  64. assertEquals(0, dc.getEntryCount());
  65. dc.read();
  66. assertEquals(ls.size(), dc.getEntryCount());
  67. {
  68. final Iterator<CGitIndexRecord> rItr = ls.values().iterator();
  69. for (int i = 0; rItr.hasNext(); i++)
  70. assertEqual(rItr.next(), dc.getEntry(i));
  71. }
  72. }
  73. public void testTreeWalk_LsFiles() throws Exception {
  74. final Repository db = createBareRepository();
  75. final Map<String, CGitIndexRecord> ls = readLsFiles();
  76. final DirCache dc = new DirCache(index);
  77. assertEquals(0, dc.getEntryCount());
  78. dc.read();
  79. assertEquals(ls.size(), dc.getEntryCount());
  80. {
  81. final Iterator<CGitIndexRecord> rItr = ls.values().iterator();
  82. final TreeWalk tw = new TreeWalk(db);
  83. tw.reset();
  84. tw.setRecursive(true);
  85. tw.addTree(new DirCacheIterator(dc));
  86. while (rItr.hasNext()) {
  87. final DirCacheIterator dcItr;
  88. assertTrue(tw.next());
  89. dcItr = tw.getTree(0, DirCacheIterator.class);
  90. assertNotNull(dcItr);
  91. assertEqual(rItr.next(), dcItr.getDirCacheEntry());
  92. }
  93. }
  94. }
  95. public void testUnsupportedOptionalExtension() throws Exception {
  96. final DirCache dc = new DirCache(pathOf("gitgit.index.ZZZZ"));
  97. dc.read();
  98. assertEquals(1, dc.getEntryCount());
  99. assertEquals("A", dc.getEntry(0).getPathString());
  100. }
  101. public void testUnsupportedRequiredExtension() throws Exception {
  102. final DirCache dc = new DirCache(pathOf("gitgit.index.aaaa"));
  103. try {
  104. dc.read();
  105. fail("Cache loaded an unsupported extension");
  106. } catch (CorruptObjectException err) {
  107. assertEquals("DIRC extension 'aaaa'"
  108. + " not supported by this version.", err.getMessage());
  109. }
  110. }
  111. public void testCorruptChecksumAtFooter() throws Exception {
  112. final DirCache dc = new DirCache(pathOf("gitgit.index.badchecksum"));
  113. try {
  114. dc.read();
  115. fail("Cache loaded despite corrupt checksum");
  116. } catch (CorruptObjectException err) {
  117. assertEquals("DIRC checksum mismatch", err.getMessage());
  118. }
  119. }
  120. private static void assertEqual(final CGitIndexRecord c,
  121. final DirCacheEntry j) {
  122. assertNotNull(c);
  123. assertNotNull(j);
  124. assertEquals(c.path, j.getPathString());
  125. assertEquals(c.id, j.getObjectId());
  126. assertEquals(c.mode, j.getRawMode());
  127. assertEquals(c.stage, j.getStage());
  128. }
  129. public void testReadIndex_DirCacheTree() throws Exception {
  130. final Map<String, CGitIndexRecord> cList = readLsFiles();
  131. final Map<String, CGitLsTreeRecord> cTree = readLsTree();
  132. final DirCache dc = new DirCache(index);
  133. assertEquals(0, dc.getEntryCount());
  134. dc.read();
  135. assertEquals(cList.size(), dc.getEntryCount());
  136. final DirCacheTree jTree = dc.getCacheTree(false);
  137. assertNotNull(jTree);
  138. assertEquals("", jTree.getNameString());
  139. assertEquals("", jTree.getPathString());
  140. assertTrue(jTree.isValid());
  141. assertEquals(ObjectId
  142. .fromString("698dd0b8d0c299f080559a1cffc7fe029479a408"), jTree
  143. .getObjectId());
  144. assertEquals(cList.size(), jTree.getEntrySpan());
  145. final ArrayList<CGitLsTreeRecord> subtrees = new ArrayList<CGitLsTreeRecord>();
  146. for (final CGitLsTreeRecord r : cTree.values()) {
  147. if (FileMode.TREE.equals(r.mode))
  148. subtrees.add(r);
  149. }
  150. assertEquals(subtrees.size(), jTree.getChildCount());
  151. for (int i = 0; i < jTree.getChildCount(); i++) {
  152. final DirCacheTree sj = jTree.getChild(i);
  153. final CGitLsTreeRecord sc = subtrees.get(i);
  154. assertEquals(sc.path, sj.getNameString());
  155. assertEquals(sc.path + "/", sj.getPathString());
  156. assertTrue(sj.isValid());
  157. assertEquals(sc.id, sj.getObjectId());
  158. }
  159. }
  160. private File pathOf(final String name) {
  161. return JGitTestUtil.getTestResourceFile(name);
  162. }
  163. private Map<String, CGitIndexRecord> readLsFiles() throws Exception {
  164. final LinkedHashMap<String, CGitIndexRecord> r = new LinkedHashMap<String, CGitIndexRecord>();
  165. final BufferedReader br = new BufferedReader(new InputStreamReader(
  166. new FileInputStream(pathOf("gitgit.lsfiles")), "UTF-8"));
  167. try {
  168. String line;
  169. while ((line = br.readLine()) != null) {
  170. final CGitIndexRecord cr = new CGitIndexRecord(line);
  171. r.put(cr.path, cr);
  172. }
  173. } finally {
  174. br.close();
  175. }
  176. return r;
  177. }
  178. private Map<String, CGitLsTreeRecord> readLsTree() throws Exception {
  179. final LinkedHashMap<String, CGitLsTreeRecord> r = new LinkedHashMap<String, CGitLsTreeRecord>();
  180. final BufferedReader br = new BufferedReader(new InputStreamReader(
  181. new FileInputStream(pathOf("gitgit.lstree")), "UTF-8"));
  182. try {
  183. String line;
  184. while ((line = br.readLine()) != null) {
  185. final CGitLsTreeRecord cr = new CGitLsTreeRecord(line);
  186. r.put(cr.path, cr);
  187. }
  188. } finally {
  189. br.close();
  190. }
  191. return r;
  192. }
  193. private static class CGitIndexRecord {
  194. final int mode;
  195. final ObjectId id;
  196. final int stage;
  197. final String path;
  198. CGitIndexRecord(final String line) {
  199. final int tab = line.indexOf('\t');
  200. final int sp1 = line.indexOf(' ');
  201. final int sp2 = line.indexOf(' ', sp1 + 1);
  202. mode = Integer.parseInt(line.substring(0, sp1), 8);
  203. id = ObjectId.fromString(line.substring(sp1 + 1, sp2));
  204. stage = Integer.parseInt(line.substring(sp2 + 1, tab));
  205. path = line.substring(tab + 1);
  206. }
  207. }
  208. private static class CGitLsTreeRecord {
  209. final int mode;
  210. final ObjectId id;
  211. final String path;
  212. CGitLsTreeRecord(final String line) {
  213. final int tab = line.indexOf('\t');
  214. final int sp1 = line.indexOf(' ');
  215. final int sp2 = line.indexOf(' ', sp1 + 1);
  216. mode = Integer.parseInt(line.substring(0, sp1), 8);
  217. id = ObjectId.fromString(line.substring(sp2 + 1, tab));
  218. path = line.substring(tab + 1);
  219. }
  220. }
  221. }