KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ant > FileChangeSupportTest


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.modules.project.ant;
21
22 // XXX testRenames
23
// XXX testRemoveListener
24
// XXX testMultipleListenersOnSameFile
25
// XXX testSameListenerOnMultipleFiles
26

27 import java.io.File JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.List JavaDoc;
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 /**
38  * Test {@link FileChangeSupport}.
39  * @author Jesse Glick
40  */

41 public class FileChangeSupportTest extends NbTestCase {
42     
43     static {
44         FileChangeSupportTest.class.getClassLoader().setDefaultAssertionStatus(true);
45     }
46     
47     public FileChangeSupportTest(String JavaDoc testName) {
48         super(testName);
49     }
50
51     private static final char SEP = File.separatorChar;
52     
53     private static final long SLEEP = 1000; // msec to sleep before touching a file again
54

55     private FileObject scratch;
56     private String JavaDoc scratchPath;
57     
58     protected void setUp() throws Exception JavaDoc {
59         super.setUp();
60         scratch = TestUtil.makeScratchDir(this);
61         scratchPath = FileUtil.toFile(scratch).getAbsolutePath();
62     }
63     
64     public void testSimpleModification() throws Exception JavaDoc {
65         FileObject dir = scratch.createFolder("dir");
66         FileObject file = dir.createData("file");
67         File JavaDoc 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 JavaDoc {
82         File JavaDoc fileF = new File JavaDoc(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 JavaDoc {
96         File JavaDoc fileF = new File JavaDoc(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 JavaDoc {
116         File JavaDoc fileF = new File JavaDoc(scratchPath + SEP + "dir" + SEP + "file2");
117         L l = new L();
118         FileChangeSupport.DEFAULT.addListener(l, fileF);
119         File JavaDoc dirF = new File JavaDoc(scratchPath + SEP + "dir");
120         dirF.mkdir();
121         new FileOutputStream JavaDoc(fileF).close();
122         scratch.getFileSystem().refresh(false);
123         assertEquals("got file creation event", Collections.singletonList("C:" + fileF), l.check());
124         Thread.sleep(SLEEP); // make sure timestamp changes
125
new FileOutputStream JavaDoc(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 JavaDoc {
135         File JavaDoc fileF = new File JavaDoc(scratchPath + SEP + "dir" + SEP + "file2");
136         L l = new L();
137         FileChangeSupport.DEFAULT.addListener(l, fileF);
138         File JavaDoc dirF = new File JavaDoc(scratchPath + SEP + "dir");
139         
140         for (int cntr = 0; cntr < 50; cntr++) {
141             dirF.mkdir();
142             new FileOutputStream JavaDoc(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 JavaDoc<String JavaDoc> events = new ArrayList JavaDoc<String JavaDoc>();
155         
156         public L() {}
157         
158         public List JavaDoc<String JavaDoc> check() {
159             List JavaDoc<String JavaDoc> toret = new ArrayList JavaDoc<String JavaDoc>(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