diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/MockSystemReader.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/MockSystemReader.java index 65551d657..d24dd44ff 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/MockSystemReader.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/MockSystemReader.java @@ -47,6 +47,7 @@ package org.eclipse.jgit.junit; import java.io.File; import java.io.IOException; +import java.lang.reflect.Field; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.HashMap; @@ -170,6 +171,7 @@ public class MockSystemReader extends SystemReader { * Assign some properties for the currently executing platform */ public void setCurrentPlatform() { + resetOsNames(); setProperty("os.name", System.getProperty("os.name")); setProperty("file.separator", System.getProperty("file.separator")); setProperty("path.separator", System.getProperty("path.separator")); @@ -180,6 +182,7 @@ public class MockSystemReader extends SystemReader { * Emulate Windows */ public void setWindows() { + resetOsNames(); setProperty("os.name", "Windows"); setProperty("file.separator", "\\"); setProperty("path.separator", ";"); @@ -191,10 +194,25 @@ public class MockSystemReader extends SystemReader { * Emulate Unix */ public void setUnix() { + resetOsNames(); setProperty("os.name", "*nix"); // Essentially anything but Windows setProperty("file.separator", "/"); setProperty("path.separator", ":"); setProperty("line.separator", "\n"); setPlatformChecker(); } + + private void resetOsNames() { + Field field; + try { + field = SystemReader.class.getDeclaredField("isWindows"); + field.setAccessible(true); + field.set(null, null); + field = SystemReader.class.getDeclaredField("isMacOS"); + field.setAccessible(true); + field.set(null, null); + } catch (Exception e) { + e.printStackTrace(); + } + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java index b4233b6cc..4795c89e7 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java @@ -71,6 +71,11 @@ import org.eclipse.jgit.lib.ObjectChecker; */ public abstract class SystemReader { private static final SystemReader DEFAULT; + + private static Boolean isMacOS; + + private static Boolean isWindows; + static { SystemReader r = new Default(); r.init(); @@ -148,6 +153,8 @@ public abstract class SystemReader { * the default instance. */ public static void setInstance(SystemReader newReader) { + isMacOS = null; + isWindows = null; if (newReader == null) INSTANCE = DEFAULT; else { @@ -293,26 +300,31 @@ public abstract class SystemReader { * @return true if we are running on a Windows. */ public boolean isWindows() { - String osDotName = AccessController - .doPrivileged(new PrivilegedAction() { - public String run() { - return getProperty("os.name"); //$NON-NLS-1$ - } - }); - return osDotName.startsWith("Windows"); //$NON-NLS-1$ + if (isWindows == null) { + String osDotName = getOsName(); + isWindows = Boolean.valueOf(osDotName.startsWith("Windows")); //$NON-NLS-1$ + } + return isWindows.booleanValue(); } /** * @return true if we are running on Mac OS X */ public boolean isMacOS() { - String osDotName = AccessController - .doPrivileged(new PrivilegedAction() { - public String run() { - return getProperty("os.name"); //$NON-NLS-1$ - } - }); - return "Mac OS X".equals(osDotName) || "Darwin".equals(osDotName); //$NON-NLS-1$ //$NON-NLS-2$ + if (isMacOS == null) { + String osDotName = getOsName(); + isMacOS = Boolean.valueOf( + "Mac OS X".equals(osDotName) || "Darwin".equals(osDotName)); //$NON-NLS-1$ //$NON-NLS-2$ + } + return isMacOS.booleanValue(); + } + + private String getOsName() { + return AccessController.doPrivileged(new PrivilegedAction() { + public String run() { + return getProperty("os.name"); //$NON-NLS-1$ + } + }); } /**