View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc. 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.patch;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.fail;
15  
16  import java.io.IOException;
17  import java.io.InputStream;
18  
19  import org.eclipse.jgit.diff.Edit;
20  import org.eclipse.jgit.diff.EditList;
21  import org.junit.Test;
22  
23  public class EditListTest {
24  	@Test
25  	public void testHunkHeader() throws IOException {
26  		final Patch p = parseTestPatchFile("testGetText_BothISO88591.patch");
27  		final FileHeader fh = p.getFiles().get(0);
28  
29  		final EditList list0 = fh.getHunks().get(0).toEditList();
30  		assertEquals(1, list0.size());
31  		assertEquals(new Edit(4 - 1, 5 - 1, 4 - 1, 5 - 1), list0.get(0));
32  
33  		final EditList list1 = fh.getHunks().get(1).toEditList();
34  		assertEquals(1, list1.size());
35  		assertEquals(new Edit(16 - 1, 17 - 1, 16 - 1, 17 - 1), list1.get(0));
36  	}
37  
38  	@Test
39  	public void testFileHeader() throws IOException {
40  		final Patch p = parseTestPatchFile("testGetText_BothISO88591.patch");
41  		final FileHeader fh = p.getFiles().get(0);
42  		final EditList e = fh.toEditList();
43  		assertEquals(2, e.size());
44  		assertEquals(new Edit(4 - 1, 5 - 1, 4 - 1, 5 - 1), e.get(0));
45  		assertEquals(new Edit(16 - 1, 17 - 1, 16 - 1, 17 - 1), e.get(1));
46  	}
47  
48  	@Test
49  	public void testTypes() throws IOException {
50  		final Patch p = parseTestPatchFile("testEditList_Types.patch");
51  		final FileHeader fh = p.getFiles().get(0);
52  		final EditList e = fh.toEditList();
53  		assertEquals(3, e.size());
54  		assertEquals(new Edit(3 - 1, 3 - 1, 3 - 1, 4 - 1), e.get(0));
55  		assertEquals(new Edit(17 - 1, 19 - 1, 18 - 1, 18 - 1), e.get(1));
56  		assertEquals(new Edit(23 - 1, 25 - 1, 22 - 1, 28 - 1), e.get(2));
57  	}
58  
59  	private Patch parseTestPatchFile(String patchFile) throws IOException {
60  		try (InputStream in = getClass().getResourceAsStream(patchFile)) {
61  			if (in == null) {
62  				fail("No " + patchFile + " test vector");
63  				return null; // Never happens
64  			}
65  			final Patch p = new Patch();
66  			p.parse(in);
67  			return p;
68  		}
69  	}
70  }