View Javadoc
1   /*
2    * Copyright (C) 2022, Simeon Andreev and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.diff;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertSame;
15  
16  import java.util.Arrays;
17  import java.util.List;
18  import org.eclipse.jgit.internal.diff.FilteredRenameDetector;
19  import org.eclipse.jgit.lib.ObjectId;
20  import org.eclipse.jgit.treewalk.filter.PathFilter;
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  public class FilteredRenameDetectorTest extends AbstractRenameDetectionTestCase {
25  
26  	private FilteredRenameDetector frd;
27  
28  	@Override
29  	@Before
30  	public void setUp() throws Exception {
31  		super.setUp();
32  		frd = new FilteredRenameDetector(db);
33  	}
34  
35  	@Test
36  	public void testExactRename() throws Exception {
37  		ObjectId foo = blob("foo");
38  		ObjectId bar = blob("bar");
39  
40  		DiffEntry a = DiffEntry.add(PATH_A, foo);
41  		DiffEntry b = DiffEntry.delete(PATH_Q, foo);
42  
43  		DiffEntry c = DiffEntry.add(PATH_H, bar);
44  		DiffEntry d = DiffEntry.delete(PATH_B, bar);
45  
46  		List<DiffEntry> changes = Arrays.asList(a, b, c, d);
47  		PathFilter filter = PathFilter.create(PATH_A);
48  		List<DiffEntry> entries = frd.compute(changes, filter);
49  		assertEquals("Unexpected entries in: " + entries, 1, entries.size());
50  		assertRename(b, a, 100, entries.get(0));
51  	}
52  
53  	@Test
54  	public void testExactRename_multipleFilters() throws Exception {
55  		ObjectId foo = blob("foo");
56  		ObjectId bar = blob("bar");
57  
58  		DiffEntry a = DiffEntry.add(PATH_A, foo);
59  		DiffEntry b = DiffEntry.delete(PATH_Q, foo);
60  
61  		DiffEntry c = DiffEntry.add(PATH_H, bar);
62  		DiffEntry d = DiffEntry.delete(PATH_B, bar);
63  
64  		List<DiffEntry> changes = Arrays.asList(a, b, c, d);
65  		List<PathFilter> filters = Arrays.asList(PathFilter.create(PATH_A),
66  				PathFilter.create(PATH_H));
67  		List<DiffEntry> entries = frd.compute(changes, filters);
68  		assertEquals("Unexpected entries in: " + entries, 2, entries.size());
69  		assertRename(b, a, 100, entries.get(0));
70  		assertRename(d, c, 100, entries.get(1));
71  	}
72  
73  	@Test
74  	public void testInexactRename() throws Exception {
75  		ObjectId aId = blob("foo\nbar\nbaz\nblarg\n");
76  		ObjectId bId = blob("foo\nbar\nbaz\nblah\n");
77  		DiffEntry a = DiffEntry.add(PATH_A, aId);
78  		DiffEntry b = DiffEntry.delete(PATH_Q, bId);
79  
80  		ObjectId cId = blob("some\nsort\nof\ntext\n");
81  		ObjectId dId = blob("completely\nunrelated\ntext\n");
82  		DiffEntry c = DiffEntry.add(PATH_B, cId);
83  		DiffEntry d = DiffEntry.delete(PATH_H, dId);
84  
85  		List<DiffEntry> changes = Arrays.asList(a, b, c, d);
86  		PathFilter filter = PathFilter.create(PATH_A);
87  		List<DiffEntry> entries = frd.compute(changes, filter);
88  		assertEquals("Unexpected entries: " + entries, 1, entries.size());
89  		assertRename(b, a, 66, entries.get(0));
90  	}
91  
92  	@Test
93  	public void testInexactRename_multipleFilters() throws Exception {
94  		ObjectId aId = blob("foo\nbar\nbaz\nblarg\n");
95  		ObjectId bId = blob("foo\nbar\nbaz\nblah\n");
96  		DiffEntry a = DiffEntry.add(PATH_A, aId);
97  		DiffEntry b = DiffEntry.delete(PATH_Q, bId);
98  
99  		ObjectId cId = blob("some\nsort\nof\ntext\n");
100 		ObjectId dId = blob("completely\nunrelated\ntext\n");
101 		DiffEntry c = DiffEntry.add(PATH_B, cId);
102 		DiffEntry d = DiffEntry.delete(PATH_H, dId);
103 
104 		List<DiffEntry> changes = Arrays.asList(a, b, c, d);
105 		List<PathFilter> filters = Arrays.asList(PathFilter.create(PATH_A),
106 				PathFilter.create(PATH_H));
107 		List<DiffEntry> entries = frd.compute(changes, filters);
108 		assertEquals("Unexpected entries: " + entries, 2, entries.size());
109 		assertRename(b, a, 66, entries.get(0));
110 		assertSame(d, entries.get(1));
111 	}
112 
113 	@Test
114 	public void testNoRenames() throws Exception {
115 		ObjectId aId = blob("");
116 		ObjectId bId = blob("blah1");
117 		ObjectId cId = blob("");
118 		ObjectId dId = blob("blah2");
119 
120 		DiffEntry a = DiffEntry.add(PATH_A, aId);
121 		DiffEntry b = DiffEntry.delete(PATH_Q, bId);
122 
123 		DiffEntry c = DiffEntry.add(PATH_H, cId);
124 		DiffEntry d = DiffEntry.delete(PATH_B, dId);
125 
126 		List<DiffEntry> changes = Arrays.asList(a, b, c, d);
127 		PathFilter filter = PathFilter.create(PATH_A);
128 		List<DiffEntry> entries = frd.compute(changes, filter);
129 		assertEquals("Unexpected entries in: " + entries, 1, entries.size());
130 		assertSame(a, entries.get(0));
131 	}
132 
133 	@Test
134 	public void testNoRenames_multipleFilters() throws Exception {
135 		ObjectId aId = blob("");
136 		ObjectId bId = blob("blah1");
137 		ObjectId cId = blob("");
138 		ObjectId dId = blob("blah2");
139 
140 		DiffEntry a = DiffEntry.add(PATH_A, aId);
141 		DiffEntry b = DiffEntry.delete(PATH_Q, bId);
142 
143 		DiffEntry c = DiffEntry.add(PATH_H, cId);
144 		DiffEntry d = DiffEntry.delete(PATH_B, dId);
145 
146 		List<DiffEntry> changes = Arrays.asList(a, b, c, d);
147 		List<PathFilter> filters = Arrays.asList(PathFilter.create(PATH_A),
148 				PathFilter.create(PATH_H));
149 		List<DiffEntry> entries = frd.compute(changes, filters);
150 		assertEquals("Unexpected entries in: " + entries, 2, entries.size());
151 		assertSame(a, entries.get(0));
152 		assertSame(c, entries.get(1));
153 	}
154 }