1 19 20 package org.netbeans.core.startup.layers; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.util.concurrent.Callable ; 29 import java.util.jar.JarEntry ; 30 import java.util.jar.JarOutputStream ; 31 import java.util.jar.Manifest ; 32 import org.netbeans.Module; 33 import org.netbeans.core.startup.Main; 34 import org.netbeans.junit.NbTestCase; 35 import org.openide.filesystems.FileLock; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.filesystems.Repository; 39 40 44 public class RemoveWritablesTest extends NbTestCase { 45 SystemFileSystem sfs; 46 Module myModule; 47 File configDir; 48 49 private static final String manifest = "Manifest-Version: 1.0\n" 50 + "OpenIDE-Module: org.netbeans.modules.foo\n" 51 + "OpenIDE-Module-Specification-Version: 1.0\n" 52 + "OpenIDE-Module-Implementation-Version: today\n" 53 + "OpenIDE-Module-Layer: mf-layer.xml\n"; 54 55 56 57 public RemoveWritablesTest(String testName) { 58 super(testName); 59 } 60 61 protected void setUp() throws Exception { 62 clearWorkDir(); 63 64 File u = new File (getWorkDir(), "userdir"); 65 File uc = new File (u, "config"); 66 uc.mkdirs(); 67 System.setProperty("netbeans.user", u.toString()); 68 69 File h = new File (getWorkDir(), "nb/installdir"); 70 new File (h, "config").mkdirs(); 71 System.setProperty("netbeans.home", h.toString()); 72 73 File moduleJar = createModuleJar( manifest ); 74 myModule = Main.getModuleSystem().getManager().create( moduleJar, null, true, false, false ); 75 Main.getModuleSystem().getManager().enable( myModule ); 76 77 sfs = (SystemFileSystem)Repository.getDefault().getDefaultFileSystem(); 78 79 assertNotNull("Module layer is installed", sfs.findResource( "foo" ) ); 80 81 configDir = FileUtil.toFile( sfs.getRoot() ); 83 } 84 85 protected void tearDown() throws Exception { 86 if( null != myModule ) { 87 Main.getModuleSystem().getManager().disable( myModule ); 88 Main.getModuleSystem().getManager().delete( myModule ); 89 } 90 } 91 92 public void testAddedFile() throws Exception { 93 FileObject folder = sfs.findResource( "foo" ); 94 FileObject newFile = folder.createData( "newFile", "ext" ); 95 96 File writableFile = new File ( new File ( configDir, "foo"), "newFile.ext" ); 97 assertTrue( writableFile.exists() ); 98 99 Object writablesRemover = newFile.getAttribute( "removeWritables" ); 100 101 assertNotNull( writablesRemover ); 102 assertTrue( writablesRemover instanceof Callable ); 103 104 ((Callable )writablesRemover).call(); 105 106 assertFalse( "local file removed", writableFile.exists() ); 107 assertNull( "FileObject does not exist", sfs.findResource( "foo/newFile.ext" ) ); 108 } 109 110 public void testRemovedFile() throws Exception { 111 FileObject folder = sfs.findResource( "foo" ); 112 FileObject existingFile = sfs.findResource( "foo/test1" ); 113 114 assertNotNull( existingFile ); 115 116 existingFile.delete(); 117 118 File maskFile = new File ( new File ( configDir, "foo"), "test1_hidden" ); 119 assertTrue( maskFile.exists() ); 120 121 Object writablesRemover = sfs.findResource( "foo" ).getAttribute( "removeWritables" ); 122 123 assertNotNull( writablesRemover ); 124 assertTrue( writablesRemover instanceof Callable ); 125 126 ((Callable )writablesRemover).call(); 127 128 assertFalse( "local file removed", maskFile.exists() ); 129 assertNotNull( "FileObject exists again", sfs.findResource( "foo/test1" ) ); 130 } 131 132 public void testRenamedFile() throws Exception { 133 FileObject folder = sfs.findResource( "foo" ); 134 FileObject existingFile = sfs.findResource( "foo/test1" ); 135 136 assertNotNull( existingFile ); 137 138 FileLock lock = existingFile.lock(); 139 existingFile.rename( lock, "newName", "newExt" ); 140 lock.releaseLock(); 141 142 assertNotNull( sfs.findResource( "foo/newName.newExt" ) ); 143 144 File maskFile = new File ( new File ( configDir, "foo"), "test1_hidden" ); 145 assertTrue( maskFile.exists() ); 146 147 Object writablesRemover = sfs.findResource( "foo" ).getAttribute( "removeWritables" ); 148 149 assertNotNull( writablesRemover ); 150 assertTrue( writablesRemover instanceof Callable ); 151 152 ((Callable )writablesRemover).call(); 153 154 assertFalse( "local file removed", maskFile.exists() ); 155 assertNotNull( "FileObject exists again", sfs.findResource( "foo/test1" ) ); 156 assertNull( "renamed file is gone", sfs.findResource( "foo/newName.newExt" ) ); 157 } 158 159 public void testModifiedAttributesFile() throws Exception { 160 FileObject folder = sfs.findResource( "foo" ); 161 FileObject existingFile = sfs.findResource( "foo/test1" ); 162 163 assertNotNull( existingFile ); 164 165 existingFile.setAttribute( "myAttribute", "myAttributeValue" ); 166 167 assertNull( "removeWritables does not work for file attributes", sfs.findResource( "foo" ).getAttribute( "removeWritables" ) ); 168 } 169 170 171 private File createModuleJar(String manifest) throws IOException { 172 File jarFile = new File ( getWorkDir(), "mymodule.jar" ); 173 174 JarOutputStream os = new JarOutputStream (new FileOutputStream (jarFile), new Manifest ( 175 new ByteArrayInputStream (manifest.getBytes()) 176 )); 177 JarEntry entry = new JarEntry ("mf-layer.xml"); 178 os.putNextEntry( entry ); 179 InputStream is = RemoveWritablesTest.class.getResourceAsStream( "data/layer3.xml" ); 180 FileUtil.copy( is, os ); 181 is.close(); 182 os.close(); 183 184 return jarFile; 185 } 186 } 187 | Popular Tags |