1 19 20 package org.netbeans.modules.masterfs; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.StringTokenizer ; 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 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 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 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 getWorkDir() { 84 String workDirProperty = System.getProperty("workdir"); workDirProperty = (workDirProperty != null) ? workDirProperty : System.getProperty("java.io.tmpdir"); return new File (workDirProperty); 87 } 88 89 protected String getResourcePrefix(String testName, String [] resources) { 90 try { 91 masterfs = MasterFileSystem.getDefault(); 92 File testDir = getWorkDir(); 93 createResources(testDir, resources); 94 return FileUtil.normalizeFile(testDir).getAbsolutePath().replace('\\','/'); 95 } catch (IOException e) { 96 throw new IllegalStateException (e.getLocalizedMessage()); 97 } 98 } 99 100 private void createResources(File testDir, String [] resources) throws IOException { 101 for (int i = 0; i < resources.length; i++) { 102 File resourceFile = FileUtil.normalizeFile(new File (testDir, resources[i])); 103 String 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 e) { 109 111 StringTokenizer en = new StringTokenizer (resourcePath, "/"); 112 FileObject lastFo = masterfs.getRoot(); 113 while (en.hasMoreElements() && lastFo != null) { 114 String filename = (String ) 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 resourcePath) throws IOException { 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 testName, String [] resources) throws IOException { 135 return new FileSystem[]{masterfs}; 136 } 137 138 protected void destroyFileSystem(String testName) throws IOException {} 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 |