View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
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  
44  package org.eclipse.jgit.patch;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertNull;
48  import static org.junit.Assert.assertSame;
49  import static org.junit.Assert.assertTrue;
50  import static org.junit.Assert.fail;
51  
52  import java.io.IOException;
53  import java.io.InputStream;
54  
55  import org.eclipse.jgit.junit.JGitTestUtil;
56  import org.junit.Test;
57  
58  public class PatchErrorTest {
59  	@Test
60  	public void testError_DisconnectedHunk() throws IOException {
61  		final Patch p = parseTestPatchFile();
62  		assertEquals(1, p.getFiles().size());
63  		{
64  			final FileHeader fh = p.getFiles().get(0);
65  			assertEquals(
66  					"org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java",
67  					fh.getNewPath());
68  			assertEquals(1, fh.getHunks().size());
69  		}
70  
71  		assertEquals(1, p.getErrors().size());
72  		final FormatError e = p.getErrors().get(0);
73  		assertSame(FormatError.Severity.ERROR, e.getSeverity());
74  		assertEquals("Hunk disconnected from file", e.getMessage());
75  		assertEquals(18, e.getOffset());
76  		assertTrue(e.getLineText().startsWith("@@ -109,4 +109,11 @@ assert"));
77  	}
78  
79  	@Test
80  	public void testError_TruncatedOld() throws IOException {
81  		final Patch p = parseTestPatchFile();
82  		assertEquals(1, p.getFiles().size());
83  		assertEquals(1, p.getErrors().size());
84  
85  		final FormatError e = p.getErrors().get(0);
86  		assertSame(FormatError.Severity.ERROR, e.getSeverity());
87  		assertEquals("Truncated hunk, at least 1 old lines is missing", e
88  				.getMessage());
89  		assertEquals(313, e.getOffset());
90  		assertTrue(e.getLineText().startsWith("@@ -236,9 +236,9 @@ protected "));
91  	}
92  
93  	@Test
94  	public void testError_TruncatedNew() throws IOException {
95  		final Patch p = parseTestPatchFile();
96  		assertEquals(1, p.getFiles().size());
97  		assertEquals(1, p.getErrors().size());
98  
99  		final FormatError e = p.getErrors().get(0);
100 		assertSame(FormatError.Severity.ERROR, e.getSeverity());
101 		assertEquals("Truncated hunk, at least 1 new lines is missing", e
102 				.getMessage());
103 		assertEquals(313, e.getOffset());
104 		assertTrue(e.getLineText().startsWith("@@ -236,9 +236,9 @@ protected "));
105 	}
106 
107 	@Test
108 	public void testError_BodyTooLong() throws IOException {
109 		final Patch p = parseTestPatchFile();
110 		assertEquals(1, p.getFiles().size());
111 		assertEquals(1, p.getErrors().size());
112 
113 		final FormatError e = p.getErrors().get(0);
114 		assertSame(FormatError.Severity.WARNING, e.getSeverity());
115 		assertEquals("Hunk header 4:11 does not match body line count of 4:12",
116 				e.getMessage());
117 		assertEquals(349, e.getOffset());
118 		assertTrue(e.getLineText().startsWith("@@ -109,4 +109,11 @@ assert"));
119 	}
120 
121 	@Test
122 	public void testError_GarbageBetweenFiles() throws IOException {
123 		final Patch p = parseTestPatchFile();
124 		assertEquals(2, p.getFiles().size());
125 		{
126 			final FileHeader fh = p.getFiles().get(0);
127 			assertEquals(
128 					"org.eclipse.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java",
129 					fh.getNewPath());
130 			assertEquals(1, fh.getHunks().size());
131 		}
132 		{
133 			final FileHeader fh = p.getFiles().get(1);
134 			assertEquals(
135 					"org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java",
136 					fh.getNewPath());
137 			assertEquals(1, fh.getHunks().size());
138 		}
139 
140 		assertEquals(1, p.getErrors().size());
141 		final FormatError e = p.getErrors().get(0);
142 		assertSame(FormatError.Severity.WARNING, e.getSeverity());
143 		assertEquals("Unexpected hunk trailer", e.getMessage());
144 		assertEquals(926, e.getOffset());
145 		assertEquals("I AM NOT HERE\n", e.getLineText());
146 	}
147 
148 	@Test
149 	public void testError_GitBinaryNoForwardHunk() throws IOException {
150 		final Patch p = parseTestPatchFile();
151 		assertEquals(2, p.getFiles().size());
152 		{
153 			final FileHeader fh = p.getFiles().get(0);
154 			assertEquals("org.spearce.egit.ui/icons/toolbar/fetchd.png", fh
155 					.getNewPath());
156 			assertSame(FileHeader.PatchType.GIT_BINARY, fh.getPatchType());
157 			assertTrue(fh.getHunks().isEmpty());
158 			assertNull(fh.getForwardBinaryHunk());
159 		}
160 		{
161 			final FileHeader fh = p.getFiles().get(1);
162 			assertEquals("org.spearce.egit.ui/icons/toolbar/fetche.png", fh
163 					.getNewPath());
164 			assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
165 			assertTrue(fh.getHunks().isEmpty());
166 			assertNull(fh.getForwardBinaryHunk());
167 		}
168 
169 		assertEquals(1, p.getErrors().size());
170 		final FormatError e = p.getErrors().get(0);
171 		assertSame(FormatError.Severity.ERROR, e.getSeverity());
172 		assertEquals("Missing forward-image in GIT binary patch", e
173 				.getMessage());
174 		assertEquals(297, e.getOffset());
175 		assertEquals("\n", e.getLineText());
176 	}
177 
178 	private Patch parseTestPatchFile() throws IOException {
179 		final String patchFile = JGitTestUtil.getName() + ".patch";
180 		try (InputStream in = getClass().getResourceAsStream(patchFile)) {
181 			if (in == null) {
182 				fail("No " + patchFile + " test vector");
183 				return null; // Never happens
184 			}
185 			final Patch p = new Patch();
186 			p.parse(in);
187 			return p;
188 		}
189 	}
190 }