Util { public static String expandUserHome(String text) { if (text.equals("~")) return getUserHome(); if (text.indexOf("~/") == 0 || text.indexOf("file:~/") == 0 || text.indexOf("jar:file:~/") == 0) return text.replaceFirst("~/", Matcher.quoteReplacement(getUserHome()) + "/"); if (text.indexOf("~\\") == 0 || text.indexOf("file:~\\") == 0 || text.indexOf("jar:file:~\\") == 0) return text.replaceFirst("~\\\\", Matcher.quoteReplacement(getUserHome()) + "\\\\"); return text; } private  Util(); static List<T> reverse(List<T> src); @SuppressWarnings("unchecked") static T[] reverse(T[] array); static String expandUserHome(String text); static String fixBackslashesToSlashes(String path); static String fixSpacesToPercentTwenty(String path); static T ignoreAndReturnNull(); static void ignore(); static boolean isFeatureDisabled(Method method, DisableableFeature feature); static UnsupportedOperationException unsupported(Throwable cause, String msg, Object... args); static UnsupportedOperationException unsupported(String msg, Object... args); static T unreachableButCompilerNeedsThis(); static String asString(Object result); static long now(); static File fileFromURI(URI uri); static File fileFromURI(String uriSpec); static boolean eq(Object o1, Object o2); static SystemProvider system(); static void save(File target, Properties p); static void delete(File target); static void saveJar(File target, String entryName, Properties props); static T newInstance(Class<T> clazz); static List<T> newInstance(Class<? extends T>[] classes, List<T> result);  }
@Test public void testExpandUserHomeOnUnix() { SystemProvider save = UtilTest.setSystem(new SystemProviderForTest( new Properties() {{ setProperty("user.home", "/home/john"); }}, new HashMap<String, String>() )); try { assertEquals("/home/john", Util.expandUserHome("~")); assertEquals("/home/john/foo/bar/", Util.expandUserHome("~/foo/bar/")); assertEquals("file:/home/john/foo/bar/", Util.expandUserHome("file:~/foo/bar/")); assertEquals("jar:file:/home/john/foo/bar/", Util.expandUserHome("jar:file:~/foo/bar/")); assertEquals("/home/john\\foo\\bar\\", Util.expandUserHome("~\\foo\\bar\\")); assertEquals("file:/home/john\\foo\\bar\\", Util.expandUserHome("file:~\\foo\\bar\\")); assertEquals("jar:file:/home/john\\foo\\bar\\", Util.expandUserHome("jar:file:~\\foo\\bar\\")); } finally { UtilTest.setSystem(save); } }
@Test public void testExpandUserHomeOnWindows() { SystemProvider save = UtilTest.setSystem(new SystemProviderForTest( new Properties() {{ setProperty("user.home", "C:\\Users\\John"); }}, new HashMap<String, String>() )); try { assertEquals("C:\\Users\\John", Util.expandUserHome("~")); assertEquals("C:\\Users\\John/foo/bar/", Util.expandUserHome("~/foo/bar/")); assertEquals("file:C:\\Users\\John/foo/bar/", Util.expandUserHome("file:~/foo/bar/")); assertEquals("jar:file:C:\\Users\\John/foo/bar/", Util.expandUserHome("jar:file:~/foo/bar/")); assertEquals("C:\\Users\\John\\foo\\bar\\", Util.expandUserHome("~\\foo\\bar\\")); assertEquals("file:C:\\Users\\John\\foo\\bar\\", Util.expandUserHome("file:~\\foo\\bar\\")); assertEquals("jar:file:C:\\Users\\John\\foo\\bar\\", Util.expandUserHome("jar:file:~\\foo\\bar\\")); } finally { UtilTest.setSystem(save); } }