View Javadoc
1   /*
2    * Copyright (C) 2008, 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.assertSame;
48  import static org.junit.Assert.assertTrue;
49  import static org.junit.Assert.fail;
50  
51  import java.io.IOException;
52  import java.io.InputStream;
53  
54  import org.eclipse.jgit.diff.DiffEntry;
55  import org.eclipse.jgit.junit.JGitTestUtil;
56  import org.eclipse.jgit.lib.FileMode;
57  import org.junit.Test;
58  
59  public class PatchCcTest {
60  	@Test
61  	public void testParse_OneFileCc() throws IOException {
62  		final Patch p = parseTestPatchFile();
63  		assertEquals(1, p.getFiles().size());
64  		assertTrue(p.getErrors().isEmpty());
65  
66  		final CombinedFileHeader cfh = (CombinedFileHeader) p.getFiles().get(0);
67  
68  		assertEquals("org.spearce.egit.ui/src/org/spearce/egit/ui/UIText.java",
69  				cfh.getNewPath());
70  		assertEquals(cfh.getNewPath(), cfh.getOldPath());
71  
72  		assertEquals(98, cfh.startOffset);
73  
74  		assertEquals(2, cfh.getParentCount());
75  		assertSame(cfh.getOldId(0), cfh.getOldId());
76  		assertEquals("169356b", cfh.getOldId(0).name());
77  		assertEquals("dd8c317", cfh.getOldId(1).name());
78  		assertEquals("fd85931", cfh.getNewId().name());
79  
80  		assertSame(cfh.getOldMode(0), cfh.getOldMode());
81  		assertSame(FileMode.REGULAR_FILE, cfh.getOldMode(0));
82  		assertSame(FileMode.REGULAR_FILE, cfh.getOldMode(1));
83  		assertSame(FileMode.EXECUTABLE_FILE, cfh.getNewMode());
84  		assertSame(FileHeader.ChangeType.MODIFY, cfh.getChangeType());
85  		assertSame(FileHeader.PatchType.UNIFIED, cfh.getPatchType());
86  
87  		assertEquals(1, cfh.getHunks().size());
88  		{
89  			final CombinedHunkHeader h = cfh.getHunks().get(0);
90  
91  			assertSame(cfh, h.getFileHeader());
92  			assertEquals(346, h.startOffset);
93  			assertEquals(764, h.endOffset);
94  
95  			assertSame(h.getOldImage(0), h.getOldImage());
96  			assertSame(cfh.getOldId(0), h.getOldImage(0).getId());
97  			assertSame(cfh.getOldId(1), h.getOldImage(1).getId());
98  
99  			assertEquals(55, h.getOldImage(0).getStartLine());
100 			assertEquals(12, h.getOldImage(0).getLineCount());
101 			assertEquals(3, h.getOldImage(0).getLinesAdded());
102 			assertEquals(0, h.getOldImage(0).getLinesDeleted());
103 
104 			assertEquals(163, h.getOldImage(1).getStartLine());
105 			assertEquals(13, h.getOldImage(1).getLineCount());
106 			assertEquals(2, h.getOldImage(1).getLinesAdded());
107 			assertEquals(0, h.getOldImage(1).getLinesDeleted());
108 
109 			assertEquals(163, h.getNewStartLine());
110 			assertEquals(15, h.getNewLineCount());
111 
112 			assertEquals(10, h.getLinesContext());
113 		}
114 	}
115 
116 	@Test
117 	public void testParse_CcNewFile() throws IOException {
118 		final Patch p = parseTestPatchFile();
119 		assertEquals(1, p.getFiles().size());
120 		assertTrue(p.getErrors().isEmpty());
121 
122 		final CombinedFileHeader cfh = (CombinedFileHeader) p.getFiles().get(0);
123 
124 		assertSame(DiffEntry.DEV_NULL, cfh.getOldPath());
125 		assertEquals("d", cfh.getNewPath());
126 
127 		assertEquals(187, cfh.startOffset);
128 
129 		assertEquals(2, cfh.getParentCount());
130 		assertSame(cfh.getOldId(0), cfh.getOldId());
131 		assertEquals("0000000", cfh.getOldId(0).name());
132 		assertEquals("0000000", cfh.getOldId(1).name());
133 		assertEquals("4bcfe98", cfh.getNewId().name());
134 
135 		assertSame(cfh.getOldMode(0), cfh.getOldMode());
136 		assertSame(FileMode.MISSING, cfh.getOldMode(0));
137 		assertSame(FileMode.MISSING, cfh.getOldMode(1));
138 		assertSame(FileMode.REGULAR_FILE, cfh.getNewMode());
139 		assertSame(FileHeader.ChangeType.ADD, cfh.getChangeType());
140 		assertSame(FileHeader.PatchType.UNIFIED, cfh.getPatchType());
141 
142 		assertEquals(1, cfh.getHunks().size());
143 		{
144 			final CombinedHunkHeader h = cfh.getHunks().get(0);
145 
146 			assertSame(cfh, h.getFileHeader());
147 			assertEquals(273, h.startOffset);
148 			assertEquals(300, h.endOffset);
149 
150 			assertSame(h.getOldImage(0), h.getOldImage());
151 			assertSame(cfh.getOldId(0), h.getOldImage(0).getId());
152 			assertSame(cfh.getOldId(1), h.getOldImage(1).getId());
153 
154 			assertEquals(1, h.getOldImage(0).getStartLine());
155 			assertEquals(0, h.getOldImage(0).getLineCount());
156 			assertEquals(1, h.getOldImage(0).getLinesAdded());
157 			assertEquals(0, h.getOldImage(0).getLinesDeleted());
158 
159 			assertEquals(1, h.getOldImage(1).getStartLine());
160 			assertEquals(0, h.getOldImage(1).getLineCount());
161 			assertEquals(1, h.getOldImage(1).getLinesAdded());
162 			assertEquals(0, h.getOldImage(1).getLinesDeleted());
163 
164 			assertEquals(1, h.getNewStartLine());
165 			assertEquals(1, h.getNewLineCount());
166 
167 			assertEquals(0, h.getLinesContext());
168 		}
169 	}
170 
171 	@Test
172 	public void testParse_CcDeleteFile() throws IOException {
173 		final Patch p = parseTestPatchFile();
174 		assertEquals(1, p.getFiles().size());
175 		assertTrue(p.getErrors().isEmpty());
176 
177 		final CombinedFileHeader cfh = (CombinedFileHeader) p.getFiles().get(0);
178 
179 		assertEquals("a", cfh.getOldPath());
180 		assertSame(DiffEntry.DEV_NULL, cfh.getNewPath());
181 
182 		assertEquals(187, cfh.startOffset);
183 
184 		assertEquals(2, cfh.getParentCount());
185 		assertSame(cfh.getOldId(0), cfh.getOldId());
186 		assertEquals("7898192", cfh.getOldId(0).name());
187 		assertEquals("2e65efe", cfh.getOldId(1).name());
188 		assertEquals("0000000", cfh.getNewId().name());
189 
190 		assertSame(cfh.getOldMode(0), cfh.getOldMode());
191 		assertSame(FileMode.REGULAR_FILE, cfh.getOldMode(0));
192 		assertSame(FileMode.REGULAR_FILE, cfh.getOldMode(1));
193 		assertSame(FileMode.MISSING, cfh.getNewMode());
194 		assertSame(FileHeader.ChangeType.DELETE, cfh.getChangeType());
195 		assertSame(FileHeader.PatchType.UNIFIED, cfh.getPatchType());
196 
197 		assertTrue(cfh.getHunks().isEmpty());
198 	}
199 
200 	private Patch parseTestPatchFile() throws IOException {
201 		final String patchFile = JGitTestUtil.getName() + ".patch";
202 		try (InputStream in = getClass().getResourceAsStream(patchFile)) {
203 			if (in == null) {
204 				fail("No " + patchFile + " test vector");
205 				return null; // Never happens
206 			}
207 			final Patch p = new Patch();
208 			p.parse(in);
209 			return p;
210 		}
211 	}
212 }