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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 java.io.File;
  45. import java.security.MessageDigest;
  46. import org.eclipse.jgit.lib.Constants;
  47. import org.eclipse.jgit.lib.FileMode;
  48. import org.eclipse.jgit.lib.ObjectId;
  49. import org.eclipse.jgit.lib.RepositoryTestCase;
  50. import org.eclipse.jgit.util.RawParseUtils;
  51. public class FileTreeIteratorTest extends RepositoryTestCase {
  52. private final String[] paths = { "a,", "a,b", "a/b", "a0b" };
  53. private long[] mtime;
  54. public void setUp() throws Exception {
  55. super.setUp();
  56. // We build the entries backwards so that on POSIX systems we
  57. // are likely to get the entries in the trash directory in the
  58. // opposite order of what they should be in for the iteration.
  59. // This should stress the sorting code better than doing it in
  60. // the correct order.
  61. //
  62. mtime = new long[paths.length];
  63. for (int i = paths.length - 1; i >= 0; i--) {
  64. final String s = paths[i];
  65. writeTrashFile(s, s);
  66. mtime[i] = new File(trash, s).lastModified();
  67. }
  68. }
  69. public void testEmptyIfRootIsFile() throws Exception {
  70. final File r = new File(trash, paths[0]);
  71. assertTrue(r.isFile());
  72. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS());
  73. assertTrue(fti.first());
  74. assertTrue(fti.eof());
  75. }
  76. public void testEmptyIfRootDoesNotExist() throws Exception {
  77. final File r = new File(trash, "not-existing-file");
  78. assertFalse(r.exists());
  79. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS());
  80. assertTrue(fti.first());
  81. assertTrue(fti.eof());
  82. }
  83. public void testEmptyIfRootIsEmpty() throws Exception {
  84. final File r = new File(trash, "not-existing-file");
  85. assertFalse(r.exists());
  86. r.mkdir();
  87. assertTrue(r.isDirectory());
  88. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS());
  89. assertTrue(fti.first());
  90. assertTrue(fti.eof());
  91. }
  92. public void testSimpleIterate() throws Exception {
  93. final FileTreeIterator top = new FileTreeIterator(trash, db.getFS());
  94. assertTrue(top.first());
  95. assertFalse(top.eof());
  96. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  97. assertEquals(paths[0], nameOf(top));
  98. assertEquals(paths[0].length(), top.getEntryLength());
  99. assertEquals(mtime[0], top.getEntryLastModified());
  100. top.next(1);
  101. assertFalse(top.first());
  102. assertFalse(top.eof());
  103. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  104. assertEquals(paths[1], nameOf(top));
  105. assertEquals(paths[1].length(), top.getEntryLength());
  106. assertEquals(mtime[1], top.getEntryLastModified());
  107. top.next(1);
  108. assertFalse(top.first());
  109. assertFalse(top.eof());
  110. assertEquals(FileMode.TREE.getBits(), top.mode);
  111. final AbstractTreeIterator sub = top.createSubtreeIterator(db);
  112. assertTrue(sub instanceof FileTreeIterator);
  113. final FileTreeIterator subfti = (FileTreeIterator) sub;
  114. assertTrue(sub.first());
  115. assertFalse(sub.eof());
  116. assertEquals(paths[2], nameOf(sub));
  117. assertEquals(paths[2].length(), subfti.getEntryLength());
  118. assertEquals(mtime[2], subfti.getEntryLastModified());
  119. sub.next(1);
  120. assertTrue(sub.eof());
  121. top.next(1);
  122. assertFalse(top.first());
  123. assertFalse(top.eof());
  124. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  125. assertEquals(paths[3], nameOf(top));
  126. assertEquals(paths[3].length(), top.getEntryLength());
  127. assertEquals(mtime[3], top.getEntryLastModified());
  128. top.next(1);
  129. assertTrue(top.eof());
  130. }
  131. public void testComputeFileObjectId() throws Exception {
  132. final FileTreeIterator top = new FileTreeIterator(trash, db.getFS());
  133. final MessageDigest md = Constants.newMessageDigest();
  134. md.update(Constants.encodeASCII(Constants.TYPE_BLOB));
  135. md.update((byte) ' ');
  136. md.update(Constants.encodeASCII(paths[0].length()));
  137. md.update((byte) 0);
  138. md.update(Constants.encode(paths[0]));
  139. final ObjectId expect = ObjectId.fromRaw(md.digest());
  140. assertEquals(expect, top.getEntryObjectId());
  141. // Verify it was cached by removing the file and getting it again.
  142. //
  143. new File(trash, paths[0]).delete();
  144. assertEquals(expect, top.getEntryObjectId());
  145. }
  146. private static String nameOf(final AbstractTreeIterator i) {
  147. return RawParseUtils.decode(Constants.CHARSET, i.path, 0, i.pathLen);
  148. }
  149. }