1 19 20 package org.netbeans.core.startup.layers; 21 22 import java.io.File ; 23 import java.io.FileWriter ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import org.netbeans.core.startup.layers.ModuleLayeredFileSystem; 28 import org.netbeans.core.startup.layers.SystemFileSystem; 29 import org.netbeans.junit.NbTestCase; 30 import org.openide.filesystems.FileAttributeEvent; 31 import org.openide.filesystems.FileChangeAdapter; 32 import org.openide.filesystems.FileEvent; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileRenameEvent; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.filesystems.Repository; 37 38 42 public class AttributeChangeIsNotifiedTest extends NbTestCase { 43 SystemFileSystem sfs; 44 45 public AttributeChangeIsNotifiedTest(String testName) { 46 super(testName); 47 } 48 49 protected void setUp() throws Exception { 50 clearWorkDir(); 51 52 File u = new File (getWorkDir(), "userdir"); 53 File uc = new File (u, "config"); 54 uc.mkdirs(); 55 System.setProperty("netbeans.user", u.toString()); 56 File h = new File (getWorkDir(), "nb/installdir"); 57 new File (h, "config").mkdirs(); 58 System.setProperty("netbeans.home", h.toString()); 59 60 61 sfs = (SystemFileSystem)Repository.getDefault().getDefaultFileSystem(); 62 63 File f = FileUtil.toFile(sfs.getRoot()); 64 65 assertEquals("Root is really on disk", uc, f); 66 67 } 68 69 protected void tearDown() throws Exception { 70 } 71 72 protected ModuleLayeredFileSystem getTheLayer(SystemFileSystem sfs) { 73 return sfs.getUserLayer(); 74 } 75 76 public void testChangeOfAnAttributeInLayerIsFired() throws Exception { 77 doChangeOfAnAttributeInLayerIsFired(getTheLayer(sfs)); 78 } 79 80 private void doChangeOfAnAttributeInLayerIsFired(ModuleLayeredFileSystem fs) throws Exception { 81 File f1 = changeOfAnAttributeInLayerIsFiredgenerateLayer("Folder", "java.awt.List"); 82 File f2 = changeOfAnAttributeInLayerIsFiredgenerateLayer("Folder", "java.awt.Button"); 83 File f3 = changeOfAnAttributeInLayerIsFiredgenerateLayer("NoChange", "nochange"); 84 85 { 86 ArrayList list = new ArrayList (); 87 list.add(f1.toURL()); 88 list.add(f3.toURL()); 89 fs.setURLs (list); 90 } 91 92 FileObject file = sfs.findResource("Folder/empty.xml"); 93 assertNotNull("File found in layer", file); 94 95 FSListener l = new FSListener(); 96 file.addFileChangeListener(l); 97 98 FileObject nochange = sfs.findResource("NoChange/empty.xml"); 99 assertNotNull("File found in layer", nochange); 100 FSListener no = new FSListener(); 101 nochange.addFileChangeListener(no); 102 103 assertAttr("The first value is list", file, "value", "java.awt.List"); 104 assertAttr("Imutable value is nochange", nochange, "value", "nochange"); 105 106 { 107 ArrayList list = new ArrayList (); 108 list.add(f2.toURL()); 109 list.add(f3.toURL()); 110 fs.setURLs (list); 111 } 112 String v2 = (String ) file.getAttribute("value"); 113 assertEquals("The second value is button", "java.awt.Button", v2); 114 115 assertEquals("One change: " + l.events, 1, l.events.size()); 116 117 if (!(l.events.get(0) instanceof FileAttributeEvent)) { 118 fail("Wrong event: " + l.events); 119 } 120 121 assertAttr("Imutable value is still nochange", nochange, "value", "nochange"); 122 assertEquals("No change in this attribute: " + no.events, 0, no.events.size()); 123 } 124 125 private static void assertAttr(String msg, FileObject fo, String attr, String value) throws IOException { 126 Object v = fo.getAttribute(attr); 127 assertEquals(msg + "[" + fo + "]", value, v); 128 } 129 130 int cnt; 131 private File changeOfAnAttributeInLayerIsFiredgenerateLayer(String folderName, String string) throws IOException { 132 File f = new File (getWorkDir(), "layer" + (cnt++) + ".xml"); 133 FileWriter w = new FileWriter (f); 134 w.write( 135 "<filesystem>" + 136 "<folder name='" + folderName + "'>" + 137 " <file name='empty.xml' >" + 138 " <attr name='value' stringvalue='" + string + "' />" + 139 " </file>" + 140 "</folder>" + 141 "</filesystem>" 142 ); 143 w.close(); 144 return f; 145 } 146 147 private static class FSListener extends FileChangeAdapter { 148 public List events = new ArrayList (); 149 public List change = new ArrayList (); 150 151 152 public void fileRenamed(FileRenameEvent fe) { 153 events.add(fe); 154 } 155 156 public void fileAttributeChanged(FileAttributeEvent fe) { 157 events.add(fe); 158 } 159 160 public void fileFolderCreated(FileEvent fe) { 161 events.add(fe); 162 } 163 164 public void fileDeleted(FileEvent fe) { 165 events.add(fe); 166 } 167 168 public void fileDataCreated(FileEvent fe) { 169 events.add(fe); 170 } 171 172 public void fileChanged(FileEvent fe) { 173 change.add(fe); 174 } 175 176 } 177 } 178 | Popular Tags |