1 19 20 package org.netbeans.core.startup; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.util.Locale ; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.modules.InstalledFileLocator; 29 import org.openide.util.NbBundle; 30 34 public class InstalledFileLocatorImplTest extends NbTestCase { 35 36 static { 37 InstalledFileLocatorImplTest.class.getClassLoader().setDefaultAssertionStatus(true); 38 } 39 40 public InstalledFileLocatorImplTest(String name) { 41 super(name); 42 } 43 44 private File scratch, nbhome, nbuser, nbdir1, nbdir2; 45 private InstalledFileLocator ifl; 46 protected void setUp() throws Exception { 47 super.setUp(); 48 clearWorkDir(); 49 scratch = getWorkDir(); 50 nbhome = new File (scratch, "nbhome"); 51 touch(file(nbhome, "a/b")); 52 touch(file(nbhome, "a/c")); 53 touch(file(nbhome, "d")); 54 touch(file(nbhome, "e/f/g")); 55 touch(file(nbhome, "loc/x.html")); 56 touch(file(nbhome, "loc/x_ja.html")); 57 touch(file(nbhome, "loc/x_foo.html")); 58 touch(file(nbhome, "loc/x_foo_ja.html")); 59 touch(file(nbhome, "loc/y.html")); 60 touch(file(nbhome, "h_ja")); 61 nbuser = new File (scratch, "nbuser"); 62 touch(file(nbuser, "a/b")); 63 nbdir1 = new File (scratch, "nbdir1"); 64 touch(file(nbdir1, "e/f/g")); 65 nbdir2 = new File (scratch, "nbdir2"); 66 touch(file(nbdir2, "h")); 67 touch(file(nbdir2, "loc/y_foo.html")); 68 File nbdirx = new File (scratch, "nbdirx"); System.setProperty("netbeans.home", nbhome.getAbsolutePath()); 70 System.setProperty("netbeans.user", nbuser.getAbsolutePath()); 71 System.setProperty("netbeans.dirs", 72 nbdir1.getAbsolutePath() + File.pathSeparatorChar + 73 nbdir2.getAbsolutePath() + File.pathSeparatorChar + 74 nbdirx.getAbsolutePath() + File.pathSeparatorChar); 76 NbBundle.setBranding("foo"); 77 Locale.setDefault(Locale.JAPAN); 78 ifl = new InstalledFileLocatorImpl(); 79 } 80 private static File file(File dir, String path) { 81 return new File (dir, path.replace('/', File.separatorChar)); 82 } 83 private static void touch(File f) throws IOException { 84 File p = f.getParentFile(); 85 if (!p.exists()) { 86 if (!p.mkdirs()) { 87 throw new IOException (p.getAbsolutePath()); 88 } 89 } 90 OutputStream os = new FileOutputStream (f); 91 os.close(); 92 } 93 94 100 public void testLocate() throws Exception { 101 InstalledFileLocatorImpl.prepareCache(); 102 assertEquals("[cache] found a simple file", file(nbhome, "d"), ifl.locate("d", null, false)); 103 assertEquals("[cache] did not find a nonexistent file", null, ifl.locate("d2", null, false)); 104 assertEquals("[cache] found an override in nbuser", file(nbuser, "a/b"), ifl.locate("a/b", null, false)); 105 assertEquals("[cache] found a non-override in nbhome", file(nbhome, "a/c"), ifl.locate("a/c", null, false)); 106 assertEquals("[cache] found an overridden dir in nbuser", file(nbuser, "a"), ifl.locate("a", null, false)); 107 assertEquals("[cache] did not find a nonexistent file in an existing dir", null, ifl.locate("a/x", null, false)); 108 assertEquals("[cache] found a multilevel override in an nbdirs component", file(nbdir1, "e/f/g"), ifl.locate("e/f/g", null, false)); 109 assertEquals("[cache] all nbdirs components scanned", file(nbdir2, "h"), ifl.locate("h", null, false)); 110 assertEquals("[cache] localized and branded resource can be found", file(nbhome, "loc/x_foo_ja.html"), ifl.locate("loc/x.html", null, true)); 111 assertEquals("[cache] nbdirs can override location of a branded resource", file(nbdir2, "loc/y_foo.html"), ifl.locate("loc/y.html", null, true)); 112 assertEquals("[cache] but look in all dirs for most specific resource first", file(nbhome, "h_ja"), ifl.locate("h", null, true)); 113 assertEquals("[cache] localized lookup a no-op for nonlocalized files", file(nbuser, "a/b"), ifl.locate("a/b", null, true)); 114 InstalledFileLocatorImpl.discardCache(); 115 assertEquals("[no cache] found a simple file", file(nbhome, "d"), ifl.locate("d", null, false)); 116 assertEquals("[no cache] did not find a nonexistent file", null, ifl.locate("d2", null, false)); 117 touch(file(nbhome, "d2")); 118 assertEquals("[no cache] but did find a newly added file", file(nbhome, "d2"), ifl.locate("d2", null, false)); 119 assertEquals("[no cache] found an override in nbuser", file(nbuser, "a/b"), ifl.locate("a/b", null, false)); 120 assertEquals("[no cache] found a non-override in nbhome", file(nbhome, "a/c"), ifl.locate("a/c", null, false)); 121 assertEquals("[no cache] found an overridden dir in nbuser", file(nbuser, "a"), ifl.locate("a", null, false)); 122 assertEquals("[no cache] did not find a nonexistent file in an existing dir", null, ifl.locate("a/x", null, false)); 123 assertEquals("[no cache] found a multilevel override in an nbdirs component", file(nbdir1, "e/f/g"), ifl.locate("e/f/g", null, false)); 124 assertEquals("[no cache] all nbdirs components scanned", file(nbdir2, "h"), ifl.locate("h", null, false)); 125 assertEquals("[no cache] localized and branded resource can be found", file(nbhome, "loc/x_foo_ja.html"), ifl.locate("loc/x.html", null, true)); 126 assertEquals("[no cache] nbdirs can override location of a branded resource", file(nbdir2, "loc/y_foo.html"), ifl.locate("loc/y.html", null, true)); 127 assertEquals("[no cache] but look in all dirs for most specific resource first", file(nbhome, "h_ja"), ifl.locate("h", null, true)); 128 assertEquals("[no cache] localized lookup a no-op for nonlocalized files", file(nbuser, "a/b"), ifl.locate("a/b", null, true)); 129 } 130 131 } 132 | Popular Tags |