Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
|
42 |
package org.netbeans.core.startup.layers; |
43 |
|
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.io.OutputStream; |
47 |
import java.util.Arrays; |
48 |
import java.util.Iterator; |
49 |
import junit.framework.Test; |
50 |
import org.netbeans.core.startup.MainLookup; |
51 |
import org.netbeans.junit.NbModuleSuite; |
52 |
import org.netbeans.junit.NbTestCase; |
53 |
import org.openide.filesystems.FileObject; |
54 |
import org.openide.filesystems.FileSystem; |
55 |
import org.openide.filesystems.FileUtil; |
56 |
import org.openide.filesystems.LocalFileSystem; |
57 |
import org.openide.util.Lookup; |
58 |
import org.openide.util.lookup.InstanceContent; |
59 |
|
60 |
/** Test layering of filesystems installed via lookup. |
61 |
* |
62 |
* @author Jaroslav Tulach |
63 |
*/ |
64 |
public class DynamicSFSFallbackTest extends NbTestCase |
65 |
implements InstanceContent.Convertor<FileSystem,FileSystem> { |
66 |
FileSystem fs1; |
67 |
FileSystem fs2; |
68 |
|
69 |
public DynamicSFSFallbackTest(String testName) { |
70 |
super(testName); |
71 |
} |
72 |
|
73 |
public static Test suite() { |
74 |
return NbModuleSuite.create( |
75 |
NbModuleSuite.emptyConfiguration(). |
76 |
addTest(DynamicSFSFallbackTest.class). |
77 |
clusters("org-netbeans-core-ui.*") |
78 |
); |
79 |
} |
80 |
|
81 |
@Override |
82 |
protected void setUp() throws Exception { |
83 |
} |
84 |
|
85 |
@Override |
86 |
protected void tearDown() throws Exception { |
87 |
super.tearDown(); |
88 |
MainLookup.unregister(fs1, this); |
89 |
MainLookup.unregister(fs2, this); |
90 |
} |
91 |
|
92 |
|
93 |
|
94 |
public void testDynamicSystemsCanAlsoBeBehindLayers() throws Exception { |
95 |
FileObject global = FileUtil.getConfigFile("Toolbars/Standard.xml"); |
96 |
assertNotNull("File Object installed: " + global, global); |
97 |
if (global.asText().indexOf("<Toolbar name=") == -1) { |
98 |
fail("Expecting toolbar definition: " + global.asText()); |
99 |
} |
100 |
|
101 |
final LocalFileSystem lfs1 = new LocalFileSystem(); |
102 |
File dir1 = new File(getWorkDir(), "dir1"); |
103 |
dir1.mkdirs(); |
104 |
lfs1.setRootDirectory(dir1); |
105 |
lfs1.getRoot().setAttribute("fallback", Boolean.TRUE); |
106 |
assertEquals("Position attribute is there", Boolean.TRUE, lfs1.getRoot().getAttribute("fallback")); |
107 |
fs1 = lfs1; |
108 |
fs2 = FileUtil.createMemoryFileSystem(); |
109 |
|
110 |
FileObject fo1 = FileUtil.createData(fs1.getRoot(), global.getPath()); |
111 |
fo1.setAttribute("one", 1); |
112 |
write(fo1, "fileone"); |
113 |
|
114 |
FileObject fo11 = FileUtil.createData(fs1.getRoot(), "test-fs-is-there.txt"); |
115 |
write(fo11, "hereIam"); |
116 |
|
117 |
MainLookup.register(fs1, this); |
118 |
MainLookup.register(fs2, this); |
119 |
|
120 |
Iterator<? extends FileSystem> it = Lookup.getDefault().lookupAll(FileSystem.class).iterator(); |
121 |
assertTrue("At least One", it.hasNext()); |
122 |
assertEquals("first is fs1", fs1, it.next()); |
123 |
assertTrue("At least two ", it.hasNext()); |
124 |
assertEquals("first is fs2", fs2, it.next()); |
125 |
|
126 |
if (global.asText().indexOf("<Toolbar name=") == -1) { |
127 |
fail("Still Expecting toolbar definition: " + global.asText()); |
128 |
} |
129 |
assertTrue("Still valid", global.isValid()); |
130 |
|
131 |
FileObject fo = FileUtil.getConfigFile("test-fs-is-there.txt"); |
132 |
assertNotNull("File found: " + Arrays.toString(FileUtil.getConfigRoot().getChildren()), fo); |
133 |
assertEquals("Text is correct", "hereIam", fo.asText()); |
134 |
} |
135 |
|
136 |
private static void write(FileObject fo, String txt) throws IOException { |
137 |
OutputStream os = fo.getOutputStream(); |
138 |
os.write(txt.getBytes()); |
139 |
os.close(); |
140 |
} |
141 |
|
142 |
public FileSystem convert(FileSystem obj) { |
143 |
return obj; |
144 |
} |
145 |
|
146 |
public Class<? extends FileSystem> type(FileSystem obj) { |
147 |
return obj.getClass(); |
148 |
} |
149 |
|
150 |
public String id(FileSystem obj) { |
151 |
return obj.getDisplayName(); |
152 |
} |
153 |
|
154 |
public String displayName(FileSystem obj) { |
155 |
return obj.getDisplayName(); |
156 |
} |
157 |
} |