1 19 20 package org.netbeans.modules.project.ant; 21 22 27 import java.io.File ; 28 import java.io.FileOutputStream ; 29 import java.util.ArrayList ; 30 import java.util.Collections ; 31 import java.util.List ; 32 import org.netbeans.api.project.TestUtil; 33 import org.netbeans.junit.NbTestCase; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 37 41 public class FileChangeSupportTest extends NbTestCase { 42 43 static { 44 FileChangeSupportTest.class.getClassLoader().setDefaultAssertionStatus(true); 45 } 46 47 public FileChangeSupportTest(String testName) { 48 super(testName); 49 } 50 51 private static final char SEP = File.separatorChar; 52 53 private static final long SLEEP = 1000; 55 private FileObject scratch; 56 private String scratchPath; 57 58 protected void setUp() throws Exception { 59 super.setUp(); 60 scratch = TestUtil.makeScratchDir(this); 61 scratchPath = FileUtil.toFile(scratch).getAbsolutePath(); 62 } 63 64 public void testSimpleModification() throws Exception { 65 FileObject dir = scratch.createFolder("dir"); 66 FileObject file = dir.createData("file"); 67 File fileF = FileUtil.toFile(file); 68 L l = new L(); 69 FileChangeSupport.DEFAULT.addListener(l, fileF); 70 TestUtil.createFileFromContent(null, dir, "file"); 71 assertEquals("one mod in file", Collections.singletonList("M:" + fileF), l.check()); 72 assertEquals("that's all", Collections.EMPTY_LIST, l.check()); 73 TestUtil.createFileFromContent(null, dir, "file"); 74 assertEquals("another mod in file", Collections.singletonList("M:" + fileF), l.check()); 75 dir.createData("bogus"); 76 assertEquals("nothing from a different file", Collections.EMPTY_LIST, l.check()); 77 TestUtil.createFileFromContent(null, dir, "bogus"); 78 assertEquals("even after touching the other file", Collections.EMPTY_LIST, l.check()); 79 } 80 81 public void testCreation() throws Exception { 82 File fileF = new File (scratchPath + SEP + "dir" + SEP + "file2"); 83 L l = new L(); 84 FileChangeSupport.DEFAULT.addListener(l, fileF); 85 FileObject dir = scratch.createFolder("dir"); 86 assertEquals("no mods yet, just made parent dir", Collections.EMPTY_LIST, l.check()); 87 FileObject file = dir.createData("file2"); 88 assertEquals("got file creation event", Collections.singletonList("C:" + fileF), l.check()); 89 TestUtil.createFileFromContent(null, dir, "file2"); 90 assertEquals("and then a mod in file", Collections.singletonList("M:" + fileF), l.check()); 91 dir.createData("file2a"); 92 assertEquals("nothing from a different file", Collections.EMPTY_LIST, l.check()); 93 } 94 95 public void testDeletion() throws Exception { 96 File fileF = new File (scratchPath + SEP + "dir" + SEP + "file3"); 97 L l = new L(); 98 FileChangeSupport.DEFAULT.addListener(l, fileF); 99 FileObject dir = scratch.createFolder("dir"); 100 assertEquals("no mods yet, just made parent dir", Collections.EMPTY_LIST, l.check()); 101 FileObject file = dir.createData("file3"); 102 assertEquals("got file creation event", Collections.singletonList("C:" + fileF), l.check()); 103 file.delete(); 104 assertEquals("got file deletion event", Collections.singletonList("D:" + fileF), l.check()); 105 dir.delete(); 106 assertEquals("nothing from deleting containing dir when file already deleted", Collections.EMPTY_LIST, l.check()); 107 dir = scratch.createFolder("dir"); 108 assertEquals("remade parent dir", Collections.EMPTY_LIST, l.check()); 109 file = dir.createData("file3"); 110 assertEquals("recreated file", Collections.singletonList("C:" + fileF), l.check()); 111 dir.delete(); 112 assertEquals("got file deletion event after dir deleted", Collections.singletonList("D:" + fileF), l.check()); 113 } 114 115 public void testDiskChanges() throws Exception { 116 File fileF = new File (scratchPath + SEP + "dir" + SEP + "file2"); 117 L l = new L(); 118 FileChangeSupport.DEFAULT.addListener(l, fileF); 119 File dirF = new File (scratchPath + SEP + "dir"); 120 dirF.mkdir(); 121 new FileOutputStream (fileF).close(); 122 scratch.getFileSystem().refresh(false); 123 assertEquals("got file creation event", Collections.singletonList("C:" + fileF), l.check()); 124 Thread.sleep(SLEEP); new FileOutputStream (fileF).close(); 126 scratch.getFileSystem().refresh(false); 127 assertEquals("and then a mod in file", Collections.singletonList("M:" + fileF), l.check()); 128 fileF.delete(); 129 dirF.delete(); 130 scratch.getFileSystem().refresh(false); 131 assertEquals("and then a file deletion event", Collections.singletonList("D:" + fileF), l.check()); 132 } 133 134 public void test66444() throws Exception { 135 File fileF = new File (scratchPath + SEP + "dir" + SEP + "file2"); 136 L l = new L(); 137 FileChangeSupport.DEFAULT.addListener(l, fileF); 138 File dirF = new File (scratchPath + SEP + "dir"); 139 140 for (int cntr = 0; cntr < 50; cntr++) { 141 dirF.mkdir(); 142 new FileOutputStream (fileF).close(); 143 scratch.getFileSystem().refresh(false); 144 assertEquals("got file creation event, count=" + cntr, Collections.singletonList("C:" + fileF), l.check()); 145 fileF.delete(); 146 dirF.delete(); 147 scratch.getFileSystem().refresh(false); 148 assertEquals("and then a file deletion event, count=" + cntr, Collections.singletonList("D:" + fileF), l.check()); 149 } 150 } 151 152 private static final class L implements FileChangeSupportListener { 153 154 private final List <String > events = new ArrayList <String >(); 155 156 public L() {} 157 158 public List <String > check() { 159 List <String > toret = new ArrayList <String >(events); 160 events.clear(); 161 return toret; 162 } 163 164 public void fileCreated(FileChangeSupportEvent event) { 165 events.add("C:" + event.getPath()); 166 } 167 168 public void fileDeleted(FileChangeSupportEvent event) { 169 events.add("D:" + event.getPath()); 170 } 171 172 public void fileModified(FileChangeSupportEvent event) { 173 events.add("M:" + event.getPath()); 174 } 175 176 } 177 178 } 179 | Popular Tags |