View Javadoc
1   /*
2    * Copyright (C) 2008, 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 java.lang.Integer.valueOf;
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertSame;
16  import static org.junit.Assert.assertTrue;
17  import static org.junit.Assert.fail;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.text.MessageFormat;
22  
23  import org.eclipse.jgit.internal.JGitText;
24  import org.eclipse.jgit.junit.JGitTestUtil;
25  import org.junit.Test;
26  
27  public class PatchCcErrorTest {
28  	@Test
29  	public void testError_CcTruncatedOld() throws IOException {
30  		final Patch p = parseTestPatchFile();
31  		assertEquals(1, p.getFiles().size());
32  		assertEquals(3, p.getErrors().size());
33  		{
34  			final FormatError e = p.getErrors().get(0);
35  			assertSame(FormatError.Severity.ERROR, e.getSeverity());
36  			assertEquals(MessageFormat.format(
37  					JGitText.get().truncatedHunkLinesMissingForAncestor,
38  					valueOf(1), valueOf(1)), e.getMessage());
39  			assertEquals(346, e.getOffset());
40  			assertTrue(e.getLineText().startsWith(
41  					"@@@ -55,12 -163,13 +163,15 @@@ public "));
42  		}
43  		{
44  			final FormatError e = p.getErrors().get(1);
45  			assertSame(FormatError.Severity.ERROR, e.getSeverity());
46  			assertEquals(MessageFormat.format(
47  					JGitText.get().truncatedHunkLinesMissingForAncestor,
48  					valueOf(2), valueOf(2)), e.getMessage());
49  			assertEquals(346, e.getOffset());
50  			assertTrue(e.getLineText().startsWith(
51  					"@@@ -55,12 -163,13 +163,15 @@@ public "));
52  		}
53  		{
54  			final FormatError e = p.getErrors().get(2);
55  			assertSame(FormatError.Severity.ERROR, e.getSeverity());
56  			assertEquals("Truncated hunk, at least 3 new lines is missing", e
57  					.getMessage());
58  			assertEquals(346, e.getOffset());
59  			assertTrue(e.getLineText().startsWith(
60  					"@@@ -55,12 -163,13 +163,15 @@@ public "));
61  		}
62  	}
63  
64  	private Patch parseTestPatchFile() throws IOException {
65  		final String patchFile = JGitTestUtil.getName() + ".patch";
66  		try (InputStream in = getClass().getResourceAsStream(patchFile)) {
67  			if (in == null) {
68  				fail("No " + patchFile + " test vector");
69  				return null; // Never happens
70  			}
71  			final Patch p = new Patch();
72  			p.parse(in);
73  			return p;
74  		}
75  	}
76  
77  }