KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > MasterFileSystemTest


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.modules.masterfs;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 import junit.framework.Test;
26 import org.netbeans.junit.NbTestSuite;
27 import org.netbeans.modules.masterfs.providers.AutoMountProvider;
28 import org.netbeans.modules.masterfs.providers.FileSystemProvider;
29 import org.netbeans.modules.masterfs.providers.ProvidedExtensionsTest;
30 import org.netbeans.modules.masterfs.providers.MountSupport;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileObjectTestHid;
33 import org.openide.filesystems.FileSystem;
34 import org.openide.filesystems.FileSystemFactoryHid;
35 import org.openide.filesystems.FileSystemTestHid;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.filesystems.FileUtilTestHidden;
38 import org.openide.filesystems.URLMapperTestHidden;
39 import org.openide.util.Lookup;
40 import org.openide.util.lookup.AbstractLookup;
41 import org.openide.util.lookup.InstanceContent;
42 import org.openide.util.lookup.Lookups;
43 import org.openide.util.lookup.ProxyLookup;
44
45 /**
46  * @author rm111737
47  */

48 public class MasterFileSystemTest extends FileSystemFactoryHid {
49     private static MasterFileSystem masterfs;
50
51     
52     static {
53         System.setProperty("org.openide.util.Lookup", TestLookup.class.getName());
54     }
55
56     /** Creates new MasterFileSystemTest */
57     public MasterFileSystemTest(Test test) {
58         super(test);
59         ProvidedExtensionsTest.ProvidedExtensionsImpl.setImplsMoveRetVal(false);
60         ProvidedExtensionsTest.ProvidedExtensionsImpl.setImplsRenameRetVal(false);
61         ProvidedExtensionsTest.ProvidedExtensionsImpl.setImplsDeleteRetVal(false);
62     }
63
64
65     public static void main(String JavaDoc args[]) {
66         junit.textui.TestRunner.run(suite());
67     }
68
69
70     public static Test suite() {
71         NbTestSuite suite = new NbTestSuite();
72         suite.addTestSuite(FileSystemTestHid.class);
73         suite.addTestSuite(FileObjectTestHid.class);
74         suite.addTestSuite(MasterFileObjectTestHid.class);
75         suite.addTestSuite(URLMapperTestHidden.class);
76         suite.addTestSuite(FileUtilTestHidden.class);
77         
78          
79         return new MasterFileSystemTest(suite);
80     }
81
82
83     private File JavaDoc getWorkDir() {
84         String JavaDoc workDirProperty = System.getProperty("workdir");//NOI18N
85
workDirProperty = (workDirProperty != null) ? workDirProperty : System.getProperty("java.io.tmpdir");//NOI18N
86
return new File JavaDoc(workDirProperty);
87     }
88
89     protected String JavaDoc getResourcePrefix(String JavaDoc testName, String JavaDoc[] resources) {
90        try {
91            masterfs = MasterFileSystem.getDefault();
92            File JavaDoc testDir = getWorkDir();
93            createResources(testDir, resources);
94            return FileUtil.normalizeFile(testDir).getAbsolutePath().replace('\\','/');
95         } catch (IOException JavaDoc e) {
96             throw new IllegalStateException JavaDoc(e.getLocalizedMessage());
97         }
98     }
99
100     private void createResources(File JavaDoc testDir, String JavaDoc[] resources) throws IOException JavaDoc {
101         for (int i = 0; i < resources.length; i++) {
102             File JavaDoc resourceFile = FileUtil.normalizeFile(new File JavaDoc (testDir, resources[i]));
103             String JavaDoc resourcePath = resourceFile.getAbsolutePath().replace('\\','/');
104             if (masterfs.getRoot().getFileObject(resourcePath) == null) {
105                 boolean isFolder = resources[i].endsWith("/");
106                 try {
107                     createOneResource(isFolder, resourcePath);
108                 } catch (IOException JavaDoc e) {
109                     /** this piece of code handles clearWorkDir in TetsBaseHid.setUp. MasterFileSystem is singleton
110                      * shared in one JVM for all tests and clearWorkDir means external modification here.*/

111                     StringTokenizer JavaDoc en = new StringTokenizer JavaDoc(resourcePath, "/");
112                     FileObject lastFo = masterfs.getRoot();
113                     while (en.hasMoreElements() && lastFo != null) {
114                         String JavaDoc filename = (String JavaDoc) en.nextElement();
115                         if (lastFo.getFileObject(filename) == null) {
116                             lastFo.refresh();
117                         }
118                         lastFo = lastFo.getFileObject(filename);
119                     }
120                     createOneResource(isFolder, resourcePath);
121                 }
122             }
123         }
124     }
125
126     private void createOneResource(boolean folder, String JavaDoc resourcePath) throws IOException JavaDoc {
127         if (folder) {
128             FileUtil.createFolder(masterfs.getRoot(), resourcePath);
129         } else {
130             FileUtil.createData(masterfs.getRoot(), resourcePath);
131         }
132     }
133
134     protected FileSystem[] createFileSystem(String JavaDoc testName, String JavaDoc[] resources) throws IOException JavaDoc {
135         return new FileSystem[]{masterfs};
136     }
137
138     protected void destroyFileSystem(String JavaDoc testName) throws IOException JavaDoc {}
139     
140     static class TestFSProvider implements FileSystemProvider {
141         static TestFSProvider DEFAULT = new TestFSProvider();
142         private boolean init = false;
143         
144         private TestFSProvider () {};
145         
146         public AutoMountProvider initialize(MountSupport mSupport) {
147             init = true;
148             return null;
149         }
150         
151         boolean isInitailized () {
152             return init;
153         }
154     }
155     
156     public static class TestLookup extends ProxyLookup {
157         public TestLookup() {
158             super ();
159             setLookups(new Lookup[] {getMetaInfLookup(), getInstanceLookup()});
160         }
161
162         private Lookup getInstanceLookup() {
163             InstanceContent instanceContent = new InstanceContent ();
164             instanceContent.add(TestFSProvider.DEFAULT);
165             Lookup instanceLookup = new AbstractLookup (instanceContent);
166             return instanceLookup;
167         }
168
169         private Lookup getMetaInfLookup() {
170             return Lookups.metaInfServices(Thread.currentThread().getContextClassLoader());
171         }
172     }
173     
174 }
175
Popular Tags