View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2009, Johannes E. Schindelin <johannes.schindelin@gmx.de> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.diff;
13  
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.fail;
16  
17  import java.io.ByteArrayOutputStream;
18  import java.io.IOException;
19  import java.io.InputStream;
20  
21  import org.eclipse.jgit.junit.JGitTestUtil;
22  import org.eclipse.jgit.patch.FileHeader;
23  import org.eclipse.jgit.patch.Patch;
24  import org.eclipse.jgit.util.RawParseUtils;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  public class DiffFormatterReflowTest {
29  	private RawText a;
30  
31  	private RawText b;
32  
33  	private FileHeader file;
34  
35  	private ByteArrayOutputStream out;
36  
37  	private DiffFormatter fmt;
38  
39  	@Before
40  	public void setUp() throws Exception {
41  		out = new ByteArrayOutputStream();
42  		fmt = new DiffFormatter(out);
43  	}
44  
45  	@Test
46  	public void testNegativeContextFails() throws IOException {
47  		init("X");
48  		try {
49  			fmt.setContext(-1);
50  			fail("accepted negative context");
51  		} catch (IllegalArgumentException e) {
52  			// pass
53  		}
54  	}
55  
56  	@Test
57  	public void testContext0() throws IOException {
58  		init("X");
59  		fmt.setContext(0);
60  		assertFormatted();
61  	}
62  
63  	@Test
64  	public void testContext1() throws IOException {
65  		init("X");
66  		fmt.setContext(1);
67  		assertFormatted();
68  	}
69  
70  	@Test
71  	public void testContext3() throws IOException {
72  		init("X");
73  		fmt.setContext(3);
74  		assertFormatted();
75  	}
76  
77  	@Test
78  	public void testContext5() throws IOException {
79  		init("X");
80  		fmt.setContext(5);
81  		assertFormatted();
82  	}
83  
84  	@Test
85  	public void testContext10() throws IOException {
86  		init("X");
87  		fmt.setContext(10);
88  		assertFormatted();
89  	}
90  
91  	@Test
92  	public void testContext100() throws IOException {
93  		init("X");
94  		fmt.setContext(100);
95  		assertFormatted();
96  	}
97  
98  	@Test
99  	public void testEmpty1() throws IOException {
100 		init("E");
101 		assertFormatted("E.patch");
102 	}
103 
104 	@Test
105 	public void testNoNewLine1() throws IOException {
106 		init("Y");
107 		assertFormatted("Y.patch");
108 	}
109 
110 	@Test
111 	public void testNoNewLine2() throws IOException {
112 		init("Z");
113 		assertFormatted("Z.patch");
114 	}
115 
116 	private void init(String name) throws IOException {
117 		a = new RawText(readFile(name + "_PreImage"));
118 		b = new RawText(readFile(name + "_PostImage"));
119 		file = parseTestPatchFile(name + ".patch").getFiles().get(0);
120 	}
121 
122 	private void assertFormatted() throws IOException {
123 		assertFormatted(JGitTestUtil.getName() + ".out");
124 	}
125 
126 	private void assertFormatted(String name) throws IOException {
127 		fmt.format(file, a, b);
128 		final String exp = RawParseUtils.decode(readFile(name));
129 		assertEquals(exp, RawParseUtils.decode(out.toByteArray()));
130 	}
131 
132 	private byte[] readFile(String patchFile) throws IOException {
133 		final InputStream in = getClass().getResourceAsStream(patchFile);
134 		if (in == null) {
135 			fail("No " + patchFile + " test vector");
136 			return null; // Never happens
137 		}
138 		try {
139 			final byte[] buf = new byte[1024];
140 			final ByteArrayOutputStream temp = new ByteArrayOutputStream();
141 			int n;
142 			while ((n = in.read(buf)) > 0)
143 				temp.write(buf, 0, n);
144 			return temp.toByteArray();
145 		} finally {
146 			in.close();
147 		}
148 	}
149 
150 	private Patch parseTestPatchFile(String patchFile) throws IOException {
151 		try (InputStream in = getClass().getResourceAsStream(patchFile)) {
152 			if (in == null) {
153 				fail("No " + patchFile + " test vector");
154 				return null; // Never happens
155 			}
156 			final Patch p = new Patch();
157 			p.parse(in);
158 			return p;
159 		}
160 	}
161 }