Skip to content

Instantly share code, notes, and snippets.

@johnpaulett
Created February 19, 2010 20:26
Show Gist options
  • Save johnpaulett/309162 to your computer and use it in GitHub Desktop.
Save johnpaulett/309162 to your computer and use it in GitHub Desktop.
================java.net.URI================
passed java.net.URI /home
passed java.net.URI ../home
passed java.net.URI home
passed java.net.URI file:///home
passed java.net.URI file://../home
passed java.net.URI file://home
FAILED java.net.URI c:\ Illegal character in opaque part at index 2: c:\
passed java.net.URI c:/
passed java.net.URI file://c:/
FAILED java.net.URI file://c:\ Illegal character in authority at index 7: file://c:\
================java.net.URL================
FAILED java.net.URL /home no protocol: /home
FAILED java.net.URL ../home no protocol: ../home
FAILED java.net.URL home no protocol: home
passed java.net.URL file:///home
passed java.net.URL file://../home
passed java.net.URL file://home
FAILED java.net.URL c:\ unknown protocol: c
FAILED java.net.URL c:/ unknown protocol: c
passed java.net.URL file://c:/
FAILED java.net.URL file://c:\ For input string: "\"
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URL;
public class URITest {
public void run() throws Exception {
String[] paths = new String[] { "/home", "../home", "home",
"file:///home", "file://../home", "file://home", "c:\\", "c:/",
"file://c:/", "file://c:\\" };
constuct(URI.class, paths);
constuct(URL.class, paths);
}
private <T> void constuct(Class<T> clazz, String[] paths) throws Exception {
System.out.println(String.format("================%s================",
clazz.getCanonicalName()));
for (String path : paths) {
System.out.println(constuct(clazz, path));
}
System.out.println("");
}
private <T> String constuct(Class<T> clazz, String path) throws Exception {
Constructor<T> constructor = clazz.getConstructor(String.class);
try {
constructor.newInstance(path);
return status("passed", clazz, path);
} catch (InvocationTargetException e) {
Class<?> throwsType = constructor.getExceptionTypes()[0];
Throwable t = e.getTargetException();
if (t.getClass() == throwsType) {
return status("FAILED", clazz, path, t.getMessage());
} else {
throw e;
}
}
}
private <T> String status(String result, Class<T> clazz, String path) {
return status(result, clazz, path, null);
}
private <T> String status(String result, Class<T> clazz, String path,
String message) {
StringBuffer buf = new StringBuffer();
buf.append(result);
buf.append("\t");
buf.append(clazz.getCanonicalName());
buf.append("\t");
buf.append(path);
if (message != null) {
buf.append("\t");
buf.append(message);
}
return buf.toString();
}
public static void main(String[] args) throws Exception {
(new URITest()).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment