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.

InterIndexDiffFilterTest.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 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.filter;
  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.IOException;
  48. import org.eclipse.jgit.dircache.DirCache;
  49. import org.eclipse.jgit.dircache.DirCacheEditor;
  50. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  51. import org.eclipse.jgit.dircache.DirCacheEntry;
  52. import org.eclipse.jgit.dircache.DirCacheIterator;
  53. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.FileMode;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.eclipse.jgit.lib.Repository;
  58. import org.eclipse.jgit.treewalk.TreeWalk;
  59. import org.junit.Before;
  60. import org.junit.Test;
  61. public class InterIndexDiffFilterTest extends LocalDiskRepositoryTestCase {
  62. private Repository db;
  63. @Before
  64. public void setUp() throws Exception {
  65. super.setUp();
  66. db = createWorkRepository();
  67. }
  68. @Test
  69. public void testEmpty() throws IOException {
  70. DirCache dc1 = DirCache.newInCore();
  71. DirCache dc2 = DirCache.newInCore();
  72. try (TreeWalk tw = new TreeWalk(db)) {
  73. tw.addTree(new DirCacheIterator(dc1));
  74. tw.addTree(new DirCacheIterator(dc2));
  75. assertFalse(tw.next());
  76. }
  77. }
  78. static final class AddEdit extends PathEdit {
  79. private final ObjectId data;
  80. private final long length;
  81. private boolean assumeValid;
  82. private FileMode type;
  83. public AddEdit(String entryPath, FileMode type, ObjectId data,
  84. long length,
  85. boolean assumeValid) {
  86. super(entryPath);
  87. this.type = type;
  88. this.data = data;
  89. this.length = length;
  90. this.assumeValid = assumeValid;
  91. }
  92. @Override
  93. public void apply(DirCacheEntry ent) {
  94. ent.setFileMode(type);
  95. ent.setLength(length);
  96. ent.setObjectId(data);
  97. ent.setAssumeValid(assumeValid);
  98. }
  99. }
  100. private ObjectId id(String data) {
  101. byte[] bytes = data.getBytes();
  102. return db.newObjectInserter().idFor(Constants.OBJ_BLOB, bytes);
  103. }
  104. @Test
  105. public void testOneOnly() throws IOException {
  106. DirCache dc1 = DirCache.newInCore();
  107. DirCache dc2 = DirCache.newInCore();
  108. DirCacheEditor editor = dc1.editor();
  109. editor.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, false));
  110. editor.finish();
  111. try (TreeWalk tw = new TreeWalk(db)) {
  112. tw.setRecursive(true);
  113. tw.addTree(new DirCacheIterator(dc1));
  114. tw.addTree(new DirCacheIterator(dc2));
  115. tw.setFilter(InterIndexDiffFilter.INSTANCE);
  116. assertTrue(tw.next());
  117. assertEquals("a/a", tw.getPathString());
  118. assertFalse(tw.next());
  119. }
  120. }
  121. @Test
  122. public void testTwoSame() throws IOException {
  123. DirCache dc1 = DirCache.newInCore();
  124. DirCache dc2 = DirCache.newInCore();
  125. DirCacheEditor ed1 = dc1.editor();
  126. ed1.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, false));
  127. ed1.finish();
  128. DirCacheEditor ed2 = dc2.editor();
  129. ed2.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, false));
  130. ed2.finish();
  131. try (TreeWalk tw = new TreeWalk(db)) {
  132. tw.setRecursive(true);
  133. tw.addTree(new DirCacheIterator(dc1));
  134. tw.addTree(new DirCacheIterator(dc2));
  135. tw.setFilter(InterIndexDiffFilter.INSTANCE);
  136. assertFalse(tw.next());
  137. }
  138. }
  139. @Test
  140. public void testTwoSameDifferByAssumeValid() throws IOException {
  141. DirCache dc1 = DirCache.newInCore();
  142. DirCache dc2 = DirCache.newInCore();
  143. DirCacheEditor ed1 = dc1.editor();
  144. ed1.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, false));
  145. ed1.finish();
  146. DirCacheEditor ed2 = dc2.editor();
  147. ed2.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, true));
  148. ed2.finish();
  149. try (TreeWalk tw = new TreeWalk(db)) {
  150. tw.setRecursive(true);
  151. tw.addTree(new DirCacheIterator(dc1));
  152. tw.addTree(new DirCacheIterator(dc2));
  153. tw.setFilter(InterIndexDiffFilter.INSTANCE);
  154. assertTrue(tw.next());
  155. assertEquals("a/a", tw.getPathString());
  156. assertFalse(tw.next());
  157. }
  158. }
  159. @Test
  160. public void testTwoSameSameAssumeValidDifferentContent()
  161. throws IOException {
  162. DirCache dc1 = DirCache.newInCore();
  163. DirCache dc2 = DirCache.newInCore();
  164. DirCacheEditor ed1 = dc1.editor();
  165. ed1.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, true));
  166. ed1.finish();
  167. DirCacheEditor ed2 = dc2.editor();
  168. ed2.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("b"), 1, true));
  169. ed2.finish();
  170. try (TreeWalk tw = new TreeWalk(db)) {
  171. tw.setRecursive(true);
  172. tw.addTree(new DirCacheIterator(dc1));
  173. tw.addTree(new DirCacheIterator(dc2));
  174. tw.setFilter(InterIndexDiffFilter.INSTANCE);
  175. assertFalse(tw.next());
  176. }
  177. }
  178. }