View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2009, Johannes E. Schindelin <johannes.schindelin@gmx.de>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.diff;
46  
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.fail;
49  
50  import java.io.ByteArrayOutputStream;
51  import java.io.IOException;
52  import java.io.InputStream;
53  
54  import org.eclipse.jgit.junit.JGitTestUtil;
55  import org.eclipse.jgit.patch.FileHeader;
56  import org.eclipse.jgit.patch.Patch;
57  import org.eclipse.jgit.util.RawParseUtils;
58  import org.junit.Before;
59  import org.junit.Test;
60  
61  public class DiffFormatterReflowTest {
62  	private RawText a;
63  
64  	private RawText b;
65  
66  	private FileHeader file;
67  
68  	private ByteArrayOutputStream out;
69  
70  	private DiffFormatter fmt;
71  
72  	@Before
73  	public void setUp() throws Exception {
74  		out = new ByteArrayOutputStream();
75  		fmt = new DiffFormatter(out);
76  	}
77  
78  	@Test
79  	public void testNegativeContextFails() throws IOException {
80  		init("X");
81  		try {
82  			fmt.setContext(-1);
83  			fail("accepted negative context");
84  		} catch (IllegalArgumentException e) {
85  			// pass
86  		}
87  	}
88  
89  	@Test
90  	public void testContext0() throws IOException {
91  		init("X");
92  		fmt.setContext(0);
93  		assertFormatted();
94  	}
95  
96  	@Test
97  	public void testContext1() throws IOException {
98  		init("X");
99  		fmt.setContext(1);
100 		assertFormatted();
101 	}
102 
103 	@Test
104 	public void testContext3() throws IOException {
105 		init("X");
106 		fmt.setContext(3);
107 		assertFormatted();
108 	}
109 
110 	@Test
111 	public void testContext5() throws IOException {
112 		init("X");
113 		fmt.setContext(5);
114 		assertFormatted();
115 	}
116 
117 	@Test
118 	public void testContext10() throws IOException {
119 		init("X");
120 		fmt.setContext(10);
121 		assertFormatted();
122 	}
123 
124 	@Test
125 	public void testContext100() throws IOException {
126 		init("X");
127 		fmt.setContext(100);
128 		assertFormatted();
129 	}
130 
131 	@Test
132 	public void testEmpty1() throws IOException {
133 		init("E");
134 		assertFormatted("E.patch");
135 	}
136 
137 	@Test
138 	public void testNoNewLine1() throws IOException {
139 		init("Y");
140 		assertFormatted("Y.patch");
141 	}
142 
143 	@Test
144 	public void testNoNewLine2() throws IOException {
145 		init("Z");
146 		assertFormatted("Z.patch");
147 	}
148 
149 	private void init(String name) throws IOException {
150 		a = new RawText(readFile(name + "_PreImage"));
151 		b = new RawText(readFile(name + "_PostImage"));
152 		file = parseTestPatchFile(name + ".patch").getFiles().get(0);
153 	}
154 
155 	private void assertFormatted() throws IOException {
156 		assertFormatted(JGitTestUtil.getName() + ".out");
157 	}
158 
159 	private void assertFormatted(String name) throws IOException {
160 		fmt.format(file, a, b);
161 		final String exp = RawParseUtils.decode(readFile(name));
162 		assertEquals(exp, RawParseUtils.decode(out.toByteArray()));
163 	}
164 
165 	private byte[] readFile(String patchFile) throws IOException {
166 		final InputStream in = getClass().getResourceAsStream(patchFile);
167 		if (in == null) {
168 			fail("No " + patchFile + " test vector");
169 			return null; // Never happens
170 		}
171 		try {
172 			final byte[] buf = new byte[1024];
173 			final ByteArrayOutputStream temp = new ByteArrayOutputStream();
174 			int n;
175 			while ((n = in.read(buf)) > 0)
176 				temp.write(buf, 0, n);
177 			return temp.toByteArray();
178 		} finally {
179 			in.close();
180 		}
181 	}
182 
183 	private Patch parseTestPatchFile(String patchFile) throws IOException {
184 		try (InputStream in = getClass().getResourceAsStream(patchFile)) {
185 			if (in == null) {
186 				fail("No " + patchFile + " test vector");
187 				return null; // Never happens
188 			}
189 			final Patch p = new Patch();
190 			p.parse(in);
191 			return p;
192 		}
193 	}
194 }