KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > layers > RemoveWritablesTest


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.layers;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.concurrent.Callable JavaDoc;
29 import java.util.jar.JarEntry JavaDoc;
30 import java.util.jar.JarOutputStream JavaDoc;
31 import java.util.jar.Manifest JavaDoc;
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 /**
41  *
42  * @author Stanislav Aubrecht
43  */

44 public class RemoveWritablesTest extends NbTestCase {
45     SystemFileSystem sfs;
46     Module myModule;
47     File JavaDoc configDir;
48     
49     private static final String JavaDoc 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 JavaDoc testName) {
58         super(testName);
59     }
60
61     protected void setUp() throws Exception JavaDoc {
62         clearWorkDir();
63         
64         File JavaDoc u = new File JavaDoc(getWorkDir(), "userdir");
65         File JavaDoc uc = new File JavaDoc(u, "config");
66         uc.mkdirs();
67         System.setProperty("netbeans.user", u.toString());
68         
69         File JavaDoc h = new File JavaDoc(getWorkDir(), "nb/installdir");
70         new File JavaDoc(h, "config").mkdirs();
71         System.setProperty("netbeans.home", h.toString());
72
73         File JavaDoc 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() );//new File( getWorkDir(), "userdir/config" );
82

83     }
84
85     protected void tearDown() throws Exception JavaDoc {
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 JavaDoc {
93         FileObject folder = sfs.findResource( "foo" );
94         FileObject newFile = folder.createData( "newFile", "ext" );
95         
96         File JavaDoc writableFile = new File JavaDoc( new File JavaDoc( configDir, "foo"), "newFile.ext" );
97         assertTrue( writableFile.exists() );
98         
99         Object JavaDoc writablesRemover = newFile.getAttribute( "removeWritables" );
100         
101         assertNotNull( writablesRemover );
102         assertTrue( writablesRemover instanceof Callable JavaDoc );
103         
104         ((Callable JavaDoc)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 JavaDoc {
111         FileObject folder = sfs.findResource( "foo" );
112         FileObject existingFile = sfs.findResource( "foo/test1" );
113         
114         assertNotNull( existingFile );
115         
116         existingFile.delete();
117         
118         File JavaDoc maskFile = new File JavaDoc( new File JavaDoc( configDir, "foo"), "test1_hidden" );
119         assertTrue( maskFile.exists() );
120         
121         Object JavaDoc writablesRemover = sfs.findResource( "foo" ).getAttribute( "removeWritables" );
122         
123         assertNotNull( writablesRemover );
124         assertTrue( writablesRemover instanceof Callable JavaDoc );
125         
126         ((Callable JavaDoc)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 JavaDoc {
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 JavaDoc maskFile = new File JavaDoc( new File JavaDoc( configDir, "foo"), "test1_hidden" );
145         assertTrue( maskFile.exists() );
146         
147         Object JavaDoc writablesRemover = sfs.findResource( "foo" ).getAttribute( "removeWritables" );
148         
149         assertNotNull( writablesRemover );
150         assertTrue( writablesRemover instanceof Callable JavaDoc );
151         
152         ((Callable JavaDoc)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 JavaDoc {
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 JavaDoc createModuleJar(String JavaDoc manifest) throws IOException JavaDoc {
172         File JavaDoc jarFile = new File JavaDoc( getWorkDir(), "mymodule.jar" );
173         
174         JarOutputStream JavaDoc os = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(jarFile), new Manifest JavaDoc(
175             new ByteArrayInputStream JavaDoc(manifest.getBytes())
176         ));
177         JarEntry JavaDoc entry = new JarEntry JavaDoc("mf-layer.xml");
178         os.putNextEntry( entry );
179         InputStream JavaDoc 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