Fix apply patch which did not work with non-ascii characters

Bug: 483943
Change-Id: If28f64053d20ab1bee54245f223e952dc2fe392c
Signed-off-by: XinTong Wang <xintong@ca.ibm.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
XinTong Wang 2015-12-08 15:03:26 -05:00 committed by Matthias Sohn
parent be8c525fa0
commit 770d36c8ba
16 changed files with 58 additions and 7 deletions

View File

@ -144,10 +144,10 @@ protected void deleteTrashFile(final String name) throws IOException {
protected static void checkFile(File f, final String checkData)
throws IOException {
Reader r = new InputStreamReader(new FileInputStream(f), "ISO-8859-1");
Reader r = new InputStreamReader(new FileInputStream(f), "UTF-8");
try {
char[] data = new char[(int) f.length()];
if (f.length() != r.read(data))
char[] data = new char[checkData.length()];
if (checkData.length() != r.read(data))
throw new IOException("Internal error reading file data from "+f);
assertEquals(checkData, new String(data));
} finally {

View File

@ -186,6 +186,55 @@ public void testModifyNL1() throws Exception {
b.getString(0, b.size(), false));
}
@Test
public void testNonASCII() throws Exception {
ApplyResult result = init("NonASCII");
assertEquals(1, result.getUpdatedFiles().size());
assertEquals(new File(db.getWorkTree(), "NonASCII"),
result.getUpdatedFiles().get(0));
checkFile(new File(db.getWorkTree(), "NonASCII"),
b.getString(0, b.size(), false));
}
@Test
public void testNonASCII2() throws Exception {
ApplyResult result = init("NonASCII2");
assertEquals(1, result.getUpdatedFiles().size());
assertEquals(new File(db.getWorkTree(), "NonASCII2"),
result.getUpdatedFiles().get(0));
checkFile(new File(db.getWorkTree(), "NonASCII2"),
b.getString(0, b.size(), false));
}
@Test
public void testNonASCIIAdd() throws Exception {
ApplyResult result = init("NonASCIIAdd");
assertEquals(1, result.getUpdatedFiles().size());
assertEquals(new File(db.getWorkTree(), "NonASCIIAdd"),
result.getUpdatedFiles().get(0));
checkFile(new File(db.getWorkTree(), "NonASCIIAdd"),
b.getString(0, b.size(), false));
}
@Test
public void testNonASCIIAdd2() throws Exception {
ApplyResult result = init("NonASCIIAdd2", false, true);
assertEquals(1, result.getUpdatedFiles().size());
assertEquals(new File(db.getWorkTree(), "NonASCIIAdd2"),
result.getUpdatedFiles().get(0));
checkFile(new File(db.getWorkTree(), "NonASCIIAdd2"),
b.getString(0, b.size(), false));
}
@Test
public void testNonASCIIDel() throws Exception {
ApplyResult result = init("NonASCIIDel", true, false);
assertEquals(1, result.getUpdatedFiles().size());
assertEquals(new File(db.getWorkTree(), "NonASCIIDel"),
result.getUpdatedFiles().get(0));
assertFalse(new File(db.getWorkTree(), "NonASCIIDel").exists());
}
private static byte[] readFile(final String patchFile) throws IOException {
final InputStream in = getTestResource(patchFile);
if (in == null) {

View File

@ -201,10 +201,12 @@ private void apply(File f, FileHeader fh)
oldLines.add(rt.getString(i));
List<String> newLines = new ArrayList<String>(oldLines);
for (HunkHeader hh : fh.getHunks()) {
StringBuilder hunk = new StringBuilder();
for (int j = hh.getStartOffset(); j < hh.getEndOffset(); j++)
hunk.append((char) hh.getBuffer()[j]);
RawText hrt = new RawText(hunk.toString().getBytes());
byte[] b = new byte[hh.getEndOffset() - hh.getStartOffset()];
System.arraycopy(hh.getBuffer(), hh.getStartOffset(), b, 0,
b.length);
RawText hrt = new RawText(b);
List<String> hunkLines = new ArrayList<String>(hrt.size());
for (int i = 0; i < hrt.size(); i++)
hunkLines.add(hrt.getString(i));