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.

FileTreeIteratorTest.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2008, 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.treewalk;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import java.io.File;
  48. import java.security.MessageDigest;
  49. import org.eclipse.jgit.api.Git;
  50. import org.eclipse.jgit.dircache.DirCacheCheckout;
  51. import org.eclipse.jgit.dircache.DirCacheEntry;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.FileMode;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.ObjectReader;
  56. import org.eclipse.jgit.lib.RepositoryTestCase;
  57. import org.eclipse.jgit.util.FileUtils;
  58. import org.eclipse.jgit.util.RawParseUtils;
  59. import org.junit.Before;
  60. import org.junit.Test;
  61. public class FileTreeIteratorTest extends RepositoryTestCase {
  62. private final String[] paths = { "a,", "a,b", "a/b", "a0b" };
  63. private long[] mtime;
  64. @Before
  65. public void setUp() throws Exception {
  66. super.setUp();
  67. // We build the entries backwards so that on POSIX systems we
  68. // are likely to get the entries in the trash directory in the
  69. // opposite order of what they should be in for the iteration.
  70. // This should stress the sorting code better than doing it in
  71. // the correct order.
  72. //
  73. mtime = new long[paths.length];
  74. for (int i = paths.length - 1; i >= 0; i--) {
  75. final String s = paths[i];
  76. writeTrashFile(s, s);
  77. mtime[i] = new File(trash, s).lastModified();
  78. }
  79. }
  80. @Test
  81. public void testEmptyIfRootIsFile() throws Exception {
  82. final File r = new File(trash, paths[0]);
  83. assertTrue(r.isFile());
  84. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS(),
  85. db.getConfig().get(WorkingTreeOptions.KEY));
  86. assertTrue(fti.first());
  87. assertTrue(fti.eof());
  88. }
  89. @Test
  90. public void testEmptyIfRootDoesNotExist() throws Exception {
  91. final File r = new File(trash, "not-existing-file");
  92. assertFalse(r.exists());
  93. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS(),
  94. db.getConfig().get(WorkingTreeOptions.KEY));
  95. assertTrue(fti.first());
  96. assertTrue(fti.eof());
  97. }
  98. @Test
  99. public void testEmptyIfRootIsEmpty() throws Exception {
  100. final File r = new File(trash, "not-existing-file");
  101. assertFalse(r.exists());
  102. FileUtils.mkdir(r);
  103. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS(),
  104. db.getConfig().get(WorkingTreeOptions.KEY));
  105. assertTrue(fti.first());
  106. assertTrue(fti.eof());
  107. }
  108. @Test
  109. public void testSimpleIterate() throws Exception {
  110. final FileTreeIterator top = new FileTreeIterator(trash, db.getFS(),
  111. db.getConfig().get(WorkingTreeOptions.KEY));
  112. assertTrue(top.first());
  113. assertFalse(top.eof());
  114. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  115. assertEquals(paths[0], nameOf(top));
  116. assertEquals(paths[0].length(), top.getEntryLength());
  117. assertEquals(mtime[0], top.getEntryLastModified());
  118. top.next(1);
  119. assertFalse(top.first());
  120. assertFalse(top.eof());
  121. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  122. assertEquals(paths[1], nameOf(top));
  123. assertEquals(paths[1].length(), top.getEntryLength());
  124. assertEquals(mtime[1], top.getEntryLastModified());
  125. top.next(1);
  126. assertFalse(top.first());
  127. assertFalse(top.eof());
  128. assertEquals(FileMode.TREE.getBits(), top.mode);
  129. final ObjectReader reader = db.newObjectReader();
  130. final AbstractTreeIterator sub = top.createSubtreeIterator(reader);
  131. assertTrue(sub instanceof FileTreeIterator);
  132. final FileTreeIterator subfti = (FileTreeIterator) sub;
  133. assertTrue(sub.first());
  134. assertFalse(sub.eof());
  135. assertEquals(paths[2], nameOf(sub));
  136. assertEquals(paths[2].length(), subfti.getEntryLength());
  137. assertEquals(mtime[2], subfti.getEntryLastModified());
  138. sub.next(1);
  139. assertTrue(sub.eof());
  140. top.next(1);
  141. assertFalse(top.first());
  142. assertFalse(top.eof());
  143. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  144. assertEquals(paths[3], nameOf(top));
  145. assertEquals(paths[3].length(), top.getEntryLength());
  146. assertEquals(mtime[3], top.getEntryLastModified());
  147. top.next(1);
  148. assertTrue(top.eof());
  149. }
  150. @Test
  151. public void testComputeFileObjectId() throws Exception {
  152. final FileTreeIterator top = new FileTreeIterator(trash, db.getFS(),
  153. db.getConfig().get(WorkingTreeOptions.KEY));
  154. final MessageDigest md = Constants.newMessageDigest();
  155. md.update(Constants.encodeASCII(Constants.TYPE_BLOB));
  156. md.update((byte) ' ');
  157. md.update(Constants.encodeASCII(paths[0].length()));
  158. md.update((byte) 0);
  159. md.update(Constants.encode(paths[0]));
  160. final ObjectId expect = ObjectId.fromRaw(md.digest());
  161. assertEquals(expect, top.getEntryObjectId());
  162. // Verify it was cached by removing the file and getting it again.
  163. //
  164. FileUtils.delete(new File(trash, paths[0]));
  165. assertEquals(expect, top.getEntryObjectId());
  166. }
  167. @Test
  168. public void testIsModifiedSymlink() throws Exception {
  169. File f = writeTrashFile("symlink", "content");
  170. Git git = new Git(db);
  171. git.add().addFilepattern("symlink").call();
  172. git.commit().setMessage("commit").call();
  173. // Modify previously committed DirCacheEntry and write it back to disk
  174. DirCacheEntry dce = db.readDirCache().getEntry("symlink");
  175. dce.setFileMode(FileMode.SYMLINK);
  176. DirCacheCheckout.checkoutEntry(db, f, dce);
  177. FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
  178. .getConfig().get(WorkingTreeOptions.KEY));
  179. while (!fti.getEntryPathString().equals("symlink"))
  180. fti.next(1);
  181. assertFalse(fti.isModified(dce, false));
  182. }
  183. private static String nameOf(final AbstractTreeIterator i) {
  184. return RawParseUtils.decode(Constants.CHARSET, i.path, 0, i.pathLen);
  185. }
  186. }