diff -r cc5360887ca1 core.startup/test/unit/src/org/netbeans/core/startup/layers/CachingPreventsFileTouchesTest.java --- a/core.startup/test/unit/src/org/netbeans/core/startup/layers/CachingPreventsFileTouchesTest.java Tue Feb 21 08:50:52 2012 +0100 +++ b/core.startup/test/unit/src/org/netbeans/core/startup/layers/CachingPreventsFileTouchesTest.java Wed Feb 22 14:15:49 2012 +0100 @@ -44,7 +44,11 @@ package org.netbeans.core.startup.layers; +import java.beans.PropertyVetoException; +import java.io.File; import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.Set; import java.util.logging.Level; @@ -55,6 +59,9 @@ import org.netbeans.junit.NbTestSuite; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; +import org.openide.filesystems.LocalFileSystem; +import org.openide.modules.InstalledFileLocator; +import org.openide.modules.Places; import org.openide.util.Lookup; /** @@ -94,10 +101,12 @@ CachingPreventsFileTouchesTest.class ).reuseUserDir(true).enableModules("platform\\d*", ".*").enableClasspathModules(false) .honorAutoloadEager(true); - conf = conf.addTest("testReadAccess").gui(false); + conf = conf.addTest("testReadAccess", "testRememberCacheDir").gui(false); suite.addTest(conf.suite()); } - + + suite.addTest(new CachingPreventsFileTouchesTest("testCachesDontUseAbsolutePaths")); + return suite; } @@ -110,8 +119,6 @@ } FileObject fo = FileUtil.getConfigFile("Services/Browsers"); fo.delete(); - // will be reset next time the system starts - System.getProperties().remove("netbeans.dirs"); // initializes counting, but waits till netbeans.dirs are provided // by NbModuleSuite initCheckReadAccess(); @@ -139,5 +146,46 @@ throw e; } } + + public void testRememberCacheDir() { + File cacheDir = Places.getCacheDirectory(); + assertTrue("It is a directory", cacheDir.isDirectory()); + System.setProperty("mycache", cacheDir.getPath()); + + File boot = InstalledFileLocator.getDefault().locate("lib/boot.jar", "org.netbeans.bootstrap", false); + assertNotNull("Boot.jar found", boot); + System.setProperty("myinstall", boot.getParentFile().getParentFile().getParentFile().getPath()); + } + public void testCachesDontUseAbsolutePaths() throws Exception { + String cache = System.getProperty("mycache"); + String install = System.getProperty("myinstall"); + + assertNotNull("Cache found", cache); + assertNotNull("Install found", install); + + File cacheDir = new File(cache); + assertTrue("Cache dir is dir", cacheDir.isDirectory()); + int cnt = 0; + final File[] arr = cacheDir.listFiles(); + Collections.shuffle(Arrays.asList(arr)); + for (File f : arr) { + if (!f.isDirectory()) { + cnt++; + assertFileDoesNotContain(f, install); + } + } + assertTrue("Some cache files found", cnt > 4); + } + + private static void assertFileDoesNotContain(File file, String text) throws IOException, PropertyVetoException { + LocalFileSystem lfs = new LocalFileSystem(); + lfs.setRootDirectory(file.getParentFile()); + FileObject fo = lfs.findResource(file.getName()); + assertNotNull("file object for " + file + " found", fo); + String content = fo.asText(); + if (content.contains(text)) { + fail("File " + file + " seems to contain '" + text + "'!"); + } + } } diff -r cc5360887ca1 o.n.bootstrap/src/org/netbeans/LocaleVariants.java --- a/o.n.bootstrap/src/org/netbeans/LocaleVariants.java Tue Feb 21 08:50:52 2012 +0100 +++ b/o.n.bootstrap/src/org/netbeans/LocaleVariants.java Wed Feb 22 14:15:49 2012 +0100 @@ -72,7 +72,7 @@ String locale = is.readUTF(); mapLocale = Locale.getDefault().toString().equals(locale) ? Locale.getDefault() : null; for (;;) { - String file = is.readUTF(); + String file = readRelativePath(is); if (file.length() == 0) { break; } @@ -100,7 +100,7 @@ synchronized (map) { os.writeUTF(mapLocale.toString()); for (Map.Entry> entry : map.entrySet()) { - os.writeUTF(entry.getKey().getPath()); + writeRelativePath(entry.getKey(), os); for (FileWithSuffix fws : entry.getValue()) { fws.write(os); } @@ -131,6 +131,53 @@ return l; } + private static void writeRelativePath(File path, DataOutputStream dos) + throws IOException { + if (testWritePath(path, System.getProperty("netbeans.user"), "user", dos)) { + return; + } + int cnt = 0; + for (String p : System.getProperty("netbeans.dirs").split(File.pathSeparator)) { + if (testWritePath(path, p, "" + cnt, dos)) { + return; + } + cnt++; + } + if (testWritePath(path, System.getProperty("netbeans.home"), "home", dos)) { + return; + } + throw new IOException("Can't find relative path for " + path); // NOI18N + } + + private static String readRelativePath(DataInputStream dis) throws IOException { + String index = dis.readUTF(); + if (index.isEmpty()) { + return index; + } + String relative = dis.readUTF(); + if ("user".equals(index)) { + return System.getProperty("netbeans.user").concat(relative); + } + if ("home".equals(index)) { + return System.getProperty("netbeans.home").concat(relative); + } + int indx = Integer.parseInt(index); + return System.getProperty("netbeans.dirs").split(File.pathSeparator)[indx].concat(relative); + } + + private static boolean testWritePath(File path, String prefix, String codeName, DataOutputStream dos) throws IOException { + if (prefix == null || prefix.isEmpty()) { + return false; + } + if (path.getPath().startsWith(prefix)) { + dos.writeUTF(codeName); + dos.writeUTF(path.getPath().substring(prefix.length())); + return true; + } + return false; + } + + static final class FileWithSuffix { public final File file; public final String suffix; @@ -140,7 +187,7 @@ } static FileWithSuffix read(DataInputStream is) throws IOException { - String path = is.readUTF(); + String path = readRelativePath(is); if (path.length() == 0) { return null; } @@ -149,7 +196,7 @@ } void write(DataOutputStream os) throws IOException { - os.writeUTF(file.getPath()); + writeRelativePath(file, os); os.writeUTF(suffix); } }