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