KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > InstalledFileLocatorImplTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.startup;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.Locale JavaDoc;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.modules.InstalledFileLocator;
29 import org.openide.util.NbBundle;
30 /**
31  * Test functionality of InstalledFileLocatorImpl.
32  * @author Jesse Glick
33  */

34 public class InstalledFileLocatorImplTest extends NbTestCase {
35
36     static {
37         InstalledFileLocatorImplTest.class.getClassLoader().setDefaultAssertionStatus(true);
38     }
39     
40     public InstalledFileLocatorImplTest(String JavaDoc name) {
41         super(name);
42     }
43     
44     private File JavaDoc scratch, nbhome, nbuser, nbdir1, nbdir2;
45     private InstalledFileLocator ifl;
46     protected void setUp() throws Exception JavaDoc {
47         super.setUp();
48         clearWorkDir();
49         scratch = getWorkDir();
50         nbhome = new File JavaDoc(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 JavaDoc(scratch, "nbuser");
62         touch(file(nbuser, "a/b"));
63         nbdir1 = new File JavaDoc(scratch, "nbdir1");
64         touch(file(nbdir1, "e/f/g"));
65         nbdir2 = new File JavaDoc(scratch, "nbdir2");
66         touch(file(nbdir2, "h"));
67         touch(file(nbdir2, "loc/y_foo.html"));
68         File JavaDoc nbdirx = new File JavaDoc(scratch, "nbdirx"); // nonexistent dir
69
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             // Useless trailing separator intentional:
75
nbdirx.getAbsolutePath() + File.pathSeparatorChar);
76         NbBundle.setBranding("foo");
77         Locale.setDefault(Locale.JAPAN);
78         ifl = new InstalledFileLocatorImpl();
79     }
80     private static File JavaDoc file(File JavaDoc dir, String JavaDoc path) {
81         return new File JavaDoc(dir, path.replace('/', File.separatorChar));
82     }
83     private static void touch(File JavaDoc f) throws IOException JavaDoc {
84         File JavaDoc p = f.getParentFile();
85         if (!p.exists()) {
86             if (!p.mkdirs()) {
87                 throw new IOException JavaDoc(p.getAbsolutePath());
88             }
89         }
90         OutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
91         os.close();
92     }
93
94     /**
95      * Test file locating.
96      * Note that there can be only one test method because the IFLI static
97      * block initializes the dir list immediately with the given system
98      * properties, so another test in the same VM would not be able to change them.
99      */

100     public void testLocate() throws Exception JavaDoc {
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