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 java.nio.charset.StandardCharsets.ISO_8859_1;
47  import static java.nio.charset.StandardCharsets.UTF_8;
48  import static org.junit.Assert.assertEquals;
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  import java.io.InputStreamReader;
55  import java.nio.charset.Charset;
56  
57  import org.eclipse.jgit.junit.JGitTestUtil;
58  import org.junit.Test;
59  
60  public class GetTextTest {
61  	@Test
62  	public void testGetText_BothISO88591() throws IOException {
63  		final Charset cs = ISO_8859_1;
64  		final Patch p = parseTestPatchFile();
65  		assertTrue(p.getErrors().isEmpty());
66  		assertEquals(1, p.getFiles().size());
67  		final FileHeader fh = p.getFiles().get(0);
68  		assertEquals(2, fh.getHunks().size());
69  		assertEquals(readTestPatchFile(cs), fh.getScriptText(cs, cs));
70  	}
71  
72  	@Test
73  	public void testGetText_NoBinary() throws IOException {
74  		final Charset cs = ISO_8859_1;
75  		final Patch p = parseTestPatchFile();
76  		assertTrue(p.getErrors().isEmpty());
77  		assertEquals(1, p.getFiles().size());
78  		final FileHeader fh = p.getFiles().get(0);
79  		assertEquals(0, fh.getHunks().size());
80  		assertEquals(readTestPatchFile(cs), fh.getScriptText(cs, cs));
81  	}
82  
83  	@Test
84  	public void testGetText_Convert() throws IOException {
85  		final Charset csOld = ISO_8859_1;
86  		final Charset csNew = UTF_8;
87  		final Patch p = parseTestPatchFile();
88  		assertTrue(p.getErrors().isEmpty());
89  		assertEquals(1, p.getFiles().size());
90  		final FileHeader fh = p.getFiles().get(0);
91  		assertEquals(2, fh.getHunks().size());
92  
93  		// Read the original file as ISO-8859-1 and fix up the one place
94  		// where we changed the character encoding. That makes the exp
95  		// string match what we really expect to get back.
96  		//
97  		String exp = readTestPatchFile(csOld);
98  		exp = exp.replace("\303\205ngstr\303\266m", "\u00c5ngstr\u00f6m");
99  
100 		assertEquals(exp, fh.getScriptText(csOld, csNew));
101 	}
102 
103 	@Test
104 	public void testGetText_DiffCc() throws IOException {
105 		final Charset csOld = ISO_8859_1;
106 		final Charset csNew = UTF_8;
107 		final Patch p = parseTestPatchFile();
108 		assertTrue(p.getErrors().isEmpty());
109 		assertEquals(1, p.getFiles().size());
110 		final CombinedFileHeader fh = (CombinedFileHeader) p.getFiles().get(0);
111 		assertEquals(1, fh.getHunks().size());
112 
113 		// Read the original file as ISO-8859-1 and fix up the one place
114 		// where we changed the character encoding. That makes the exp
115 		// string match what we really expect to get back.
116 		//
117 		String exp = readTestPatchFile(csOld);
118 		exp = exp.replace("\303\205ngstr\303\266m", "\u00c5ngstr\u00f6m");
119 
120 		assertEquals(exp, fh
121 				.getScriptText(new Charset[] { csNew, csOld, csNew }));
122 	}
123 
124 	private Patch parseTestPatchFile() throws IOException {
125 		final String patchFile = JGitTestUtil.getName() + ".patch";
126 		try (InputStream in = getClass().getResourceAsStream(patchFile)) {
127 			if (in == null) {
128 				fail("No " + patchFile + " test vector");
129 				return null; // Never happens
130 			}
131 			final Patch p = new Patch();
132 			p.parse(in);
133 			return p;
134 		}
135 	}
136 
137 	private String readTestPatchFile(Charset cs) throws IOException {
138 		final String patchFile = JGitTestUtil.getName() + ".patch";
139 		try (InputStream in = getClass().getResourceAsStream(patchFile)) {
140 			if (in == null) {
141 				fail("No " + patchFile + " test vector");
142 				return null; // Never happens
143 			}
144 			final InputStreamReader r = new InputStreamReader(in, cs);
145 			char[] tmp = new char[2048];
146 			final StringBuilder s = new StringBuilder();
147 			int n;
148 			while ((n = r.read(tmp)) > 0)
149 				s.append(tmp, 0, n);
150 			return s.toString();
151 		}
152 	}
153 }