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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. TreeWalk tw = new TreeWalk(db);
  73. tw.addTree(new DirCacheIterator(dc1));
  74. tw.addTree(new DirCacheIterator(dc2));
  75. assertFalse(tw.next());
  76. }
  77. static final class AddEdit extends PathEdit {
  78. private final ObjectId data;
  79. private final long length;
  80. private boolean assumeValid;
  81. private FileMode type;
  82. public AddEdit(String entryPath, FileMode type, ObjectId data,
  83. long length,
  84. boolean assumeValid) {
  85. super(entryPath);
  86. this.type = type;
  87. this.data = data;
  88. this.length = length;
  89. this.assumeValid = assumeValid;
  90. }
  91. @Override
  92. public void apply(DirCacheEntry ent) {
  93. ent.setFileMode(type);
  94. ent.setLength(length);
  95. ent.setObjectId(data);
  96. ent.setAssumeValid(assumeValid);
  97. }
  98. }
  99. private ObjectId id(String data) {
  100. byte[] bytes = data.getBytes();
  101. return db.newObjectInserter().idFor(Constants.OBJ_BLOB, bytes);
  102. }
  103. @Test
  104. public void testOneOnly() throws IOException {
  105. DirCache dc1 = DirCache.newInCore();
  106. DirCache dc2 = DirCache.newInCore();
  107. DirCacheEditor editor = dc1.editor();
  108. editor.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, false));
  109. editor.finish();
  110. TreeWalk tw = new TreeWalk(db);
  111. tw.setRecursive(true);
  112. tw.addTree(new DirCacheIterator(dc1));
  113. tw.addTree(new DirCacheIterator(dc2));
  114. tw.setFilter(InterIndexDiffFilter.INSTANCE);
  115. assertTrue(tw.next());
  116. assertEquals("a/a", tw.getPathString());
  117. assertFalse(tw.next());
  118. }
  119. @Test
  120. public void testTwoSame() throws IOException {
  121. DirCache dc1 = DirCache.newInCore();
  122. DirCache dc2 = DirCache.newInCore();
  123. DirCacheEditor ed1 = dc1.editor();
  124. ed1.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, false));
  125. ed1.finish();
  126. DirCacheEditor ed2 = dc2.editor();
  127. ed2.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, false));
  128. ed2.finish();
  129. TreeWalk tw = new TreeWalk(db);
  130. tw.setRecursive(true);
  131. tw.addTree(new DirCacheIterator(dc1));
  132. tw.addTree(new DirCacheIterator(dc2));
  133. tw.setFilter(InterIndexDiffFilter.INSTANCE);
  134. assertFalse(tw.next());
  135. }
  136. @Test
  137. public void testTwoSameDifferByAssumeValid() throws IOException {
  138. DirCache dc1 = DirCache.newInCore();
  139. DirCache dc2 = DirCache.newInCore();
  140. DirCacheEditor ed1 = dc1.editor();
  141. ed1.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, false));
  142. ed1.finish();
  143. DirCacheEditor ed2 = dc2.editor();
  144. ed2.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, true));
  145. ed2.finish();
  146. TreeWalk tw = new TreeWalk(db);
  147. tw.setRecursive(true);
  148. tw.addTree(new DirCacheIterator(dc1));
  149. tw.addTree(new DirCacheIterator(dc2));
  150. tw.setFilter(InterIndexDiffFilter.INSTANCE);
  151. assertTrue(tw.next());
  152. assertEquals("a/a", tw.getPathString());
  153. assertFalse(tw.next());
  154. }
  155. @Test
  156. public void testTwoSameSameAssumeValidDifferentContent()
  157. throws IOException {
  158. DirCache dc1 = DirCache.newInCore();
  159. DirCache dc2 = DirCache.newInCore();
  160. DirCacheEditor ed1 = dc1.editor();
  161. ed1.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("a"), 1, true));
  162. ed1.finish();
  163. DirCacheEditor ed2 = dc2.editor();
  164. ed2.add(new AddEdit("a/a", FileMode.REGULAR_FILE, id("b"), 1, true));
  165. ed2.finish();
  166. TreeWalk tw = new TreeWalk(db);
  167. tw.setRecursive(true);
  168. tw.addTree(new DirCacheIterator(dc1));
  169. tw.addTree(new DirCacheIterator(dc2));
  170. tw.setFilter(InterIndexDiffFilter.INSTANCE);
  171. assertFalse(tw.next());
  172. }
  173. }