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.

FileTreeIteratorJava7Test.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2012-2013, Robin Rosenberg <robin.rosenberg@dewire.com>
  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.io.IOException;
  49. import org.eclipse.jgit.api.Git;
  50. import org.eclipse.jgit.api.ResetCommand.ResetType;
  51. import org.eclipse.jgit.dircache.DirCache;
  52. import org.eclipse.jgit.dircache.DirCacheEditor;
  53. import org.eclipse.jgit.dircache.DirCacheEntry;
  54. import org.eclipse.jgit.dircache.DirCacheIterator;
  55. import org.eclipse.jgit.junit.RepositoryTestCase;
  56. import org.eclipse.jgit.lib.Constants;
  57. import org.eclipse.jgit.lib.FileMode;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.ObjectInserter;
  60. import org.eclipse.jgit.util.FS;
  61. import org.eclipse.jgit.util.FileUtils;
  62. import org.junit.Test;
  63. public class FileTreeIteratorJava7Test extends RepositoryTestCase {
  64. @Test
  65. public void testFileModeSymLinkIsNotATree() throws IOException {
  66. FS fs = db.getFS();
  67. // mål = target in swedish, just to get som unicode in here
  68. writeTrashFile("mål/data", "targetdata");
  69. fs.createSymLink(new File(trash, "länk"), "mål");
  70. FileTreeIterator fti = new FileTreeIterator(db);
  71. assertFalse(fti.eof());
  72. assertEquals("länk", fti.getEntryPathString());
  73. assertEquals(FileMode.SYMLINK, fti.getEntryFileMode());
  74. fti.next(1);
  75. assertFalse(fti.eof());
  76. assertEquals("mål", fti.getEntryPathString());
  77. assertEquals(FileMode.TREE, fti.getEntryFileMode());
  78. fti.next(1);
  79. assertTrue(fti.eof());
  80. }
  81. @Test
  82. public void testSymlinkNotModifiedThoughNormalized() throws Exception {
  83. DirCache dc = db.lockDirCache();
  84. DirCacheEditor dce = dc.editor();
  85. final String UNNORMALIZED = "target/";
  86. final byte[] UNNORMALIZED_BYTES = Constants.encode(UNNORMALIZED);
  87. ObjectInserter oi = db.newObjectInserter();
  88. final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
  89. UNNORMALIZED_BYTES, 0,
  90. UNNORMALIZED_BYTES.length);
  91. oi.release();
  92. dce.add(new DirCacheEditor.PathEdit("link") {
  93. @Override
  94. public void apply(DirCacheEntry ent) {
  95. ent.setFileMode(FileMode.SYMLINK);
  96. ent.setObjectId(linkid);
  97. ent.setLength(UNNORMALIZED_BYTES.length);
  98. }
  99. });
  100. assertTrue(dce.commit());
  101. new Git(db).commit().setMessage("Adding link").call();
  102. new Git(db).reset().setMode(ResetType.HARD).call();
  103. DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
  104. FileTreeIterator fti = new FileTreeIterator(db);
  105. fti.next(1); // Skips .git
  106. // self-check
  107. assertEquals("link", fti.getEntryPathString());
  108. assertEquals("link", dci.getEntryPathString());
  109. // test
  110. assertFalse(fti.isModified(dci.getDirCacheEntry(), true,
  111. db.newObjectReader()));
  112. }
  113. /**
  114. * Like #testSymlinkNotModifiedThoughNormalized but there is no
  115. * normalization being done.
  116. *
  117. * @throws Exception
  118. */
  119. @Test
  120. public void testSymlinkModifiedNotNormalized() throws Exception {
  121. DirCache dc = db.lockDirCache();
  122. DirCacheEditor dce = dc.editor();
  123. final String NORMALIZED = "target";
  124. final byte[] NORMALIZED_BYTES = Constants.encode(NORMALIZED);
  125. ObjectInserter oi = db.newObjectInserter();
  126. final ObjectId linkid = oi.insert(Constants.OBJ_BLOB, NORMALIZED_BYTES,
  127. 0, NORMALIZED_BYTES.length);
  128. oi.release();
  129. dce.add(new DirCacheEditor.PathEdit("link") {
  130. @Override
  131. public void apply(DirCacheEntry ent) {
  132. ent.setFileMode(FileMode.SYMLINK);
  133. ent.setObjectId(linkid);
  134. ent.setLength(NORMALIZED_BYTES.length);
  135. }
  136. });
  137. assertTrue(dce.commit());
  138. new Git(db).commit().setMessage("Adding link").call();
  139. new Git(db).reset().setMode(ResetType.HARD).call();
  140. DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
  141. FileTreeIterator fti = new FileTreeIterator(db);
  142. fti.next(1); // Skips .git
  143. // self-check
  144. assertEquals("link", fti.getEntryPathString());
  145. assertEquals("link", dci.getEntryPathString());
  146. // test
  147. assertFalse(fti.isModified(dci.getDirCacheEntry(), true));
  148. }
  149. /**
  150. * Like #testSymlinkNotModifiedThoughNormalized but here the link is
  151. * modified.
  152. *
  153. * @throws Exception
  154. */
  155. @Test
  156. public void testSymlinkActuallyModified() throws Exception {
  157. final String NORMALIZED = "target";
  158. final byte[] NORMALIZED_BYTES = Constants.encode(NORMALIZED);
  159. ObjectInserter oi = db.newObjectInserter();
  160. final ObjectId linkid = oi.insert(Constants.OBJ_BLOB, NORMALIZED_BYTES,
  161. 0, NORMALIZED_BYTES.length);
  162. oi.release();
  163. DirCache dc = db.lockDirCache();
  164. DirCacheEditor dce = dc.editor();
  165. dce.add(new DirCacheEditor.PathEdit("link") {
  166. @Override
  167. public void apply(DirCacheEntry ent) {
  168. ent.setFileMode(FileMode.SYMLINK);
  169. ent.setObjectId(linkid);
  170. ent.setLength(NORMALIZED_BYTES.length);
  171. }
  172. });
  173. assertTrue(dce.commit());
  174. new Git(db).commit().setMessage("Adding link").call();
  175. new Git(db).reset().setMode(ResetType.HARD).call();
  176. FileUtils.delete(new File(trash, "link"), FileUtils.NONE);
  177. FS.DETECTED.createSymLink(new File(trash, "link"), "newtarget");
  178. DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
  179. FileTreeIterator fti = new FileTreeIterator(db);
  180. fti.next(1); // Skips .git
  181. // self-check
  182. assertEquals("link", fti.getEntryPathString());
  183. assertEquals("link", dci.getEntryPathString());
  184. // test
  185. assertTrue(fti.isModified(dci.getDirCacheEntry(), true,
  186. db.newObjectReader()));
  187. }
  188. }