complete TODO: getFileExtension handle dot in query part

+ testcase
pull/14/head
reger 9 years ago
parent 87e4abe393
commit 4cf875336c

@ -838,7 +838,7 @@ public class MultiProtocolURL implements Serializable, Comparable<MultiProtocolU
* @return extension or ""
*/
public static String getFileExtension(final String fileName) {
final int p = fileName.lastIndexOf('.');
int p = fileName.lastIndexOf('.');
if (p < 0) return "";
final int q = fileName.lastIndexOf('?');
if (q < 0) {
@ -846,7 +846,8 @@ public class MultiProtocolURL implements Serializable, Comparable<MultiProtocolU
}
// check last dot in query part
if (p > q) {
return ""; // TODO: last . after ? (file.ext?param=one.txt)
p = fileName.lastIndexOf('.', q);
if (p < 0) return "";
}
return fileName.substring(p + 1, q).toLowerCase();
}

@ -3,6 +3,7 @@ package net.yacy.cora.document.id;
import static org.junit.Assert.*;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeSet;
@ -192,6 +193,26 @@ public class MultiProtocolURLTest {
System.out.println(" -> " + resultUrl.toNormalform(false));
}
}
/**
* Test of getFileExtension method, of class MultiProtocolURL.
*/
@Test
public void testGetFileExtension() throws MalformedURLException {
Map<String, String> testurls = new HashMap<String, String>();
// key=testurl, value=result
testurls.put("path/file.xml","xml"); // easiest
testurls.put("path/file?h.pdf",""); // file w/o extension
testurls.put("file.html?param=h.pdf","html"); // dot in query part
testurls.put("url?param=h.pdf",""); // dot in query part
testurls.put("file.html?param", "html");
testurls.put("/path/","");
for (String s : testurls.keySet()) {
System.out.println("test getFileExtension: " + s + " -> " + testurls.get(s));
String result = MultiProtocolURL.getFileExtension(s);
assertEquals(testurls.get(s),result);
}
}
}

Loading…
Cancel
Save