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