Improved support for non ASCII chars in local file system URLs

Creating a MultiProtocolURL instance from a File object and then
retrieving a File with getFSFile() was inconsistent with file paths
containing space or non ASCII chars.
pull/60/head^2
luccioman 8 years ago
parent 7edddd7b0d
commit 0bc868a819

@ -2147,7 +2147,7 @@ public class MultiProtocolURL implements Serializable, Comparable<MultiProtocolU
*/
public java.io.File getFSFile() throws MalformedURLException {
if (!isFile()) throw new MalformedURLException();
return new java.io.File(this.toNormalform(true).substring(5));
return new java.io.File(unescape(this.toNormalform(true)).substring("file://".length()));
}
/**

@ -1,8 +1,12 @@
package net.yacy.cora.document.id;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
@ -373,6 +377,29 @@ public class MultiProtocolURLTest {
assertEquals(testString[1], unescaped);
}
}
/**
* Unit tests for {@link MultiProtocolURL#MultiProtocolURL(java.io.File)}
* @throws MalformedURLException when an error occurred
* @throws URISyntaxException
*/
@Test
public void testFileConstructor() throws MalformedURLException, URISyntaxException {
File[] files = new File[] {
/* Simple file name */
new File(File.separator + "textFile.txt"),
/* File name with space */
new File(File.separator + "text file.txt"),
/* File name with non ASCII latin chars */
new File(File.separator + "fileéàè.txt"),
};
for(int i = 0; i < files.length; i++) {
MultiProtocolURL url = new MultiProtocolURL(files[i]);
assertTrue(url.isFile());
/* Check consistency when retrieving a File object with getFSFile() */
assertEquals(files[i], url.getFSFile());
}
}
}

Loading…
Cancel
Save