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