001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.ant;
043:
044: // XXX testRenames
045: // XXX testRemoveListener
046: // XXX testMultipleListenersOnSameFile
047: // XXX testSameListenerOnMultipleFiles
048:
049: import java.io.File;
050: import java.io.FileOutputStream;
051: import java.util.ArrayList;
052: import java.util.Collections;
053: import java.util.List;
054: import org.netbeans.api.project.TestUtil;
055: import org.netbeans.junit.NbTestCase;
056: import org.openide.filesystems.FileObject;
057: import org.openide.filesystems.FileUtil;
058:
059: /**
060: * Test {@link FileChangeSupport}.
061: * @author Jesse Glick
062: */
063: public class FileChangeSupportTest extends NbTestCase {
064:
065: static {
066: FileChangeSupportTest.class.getClassLoader()
067: .setDefaultAssertionStatus(true);
068: }
069:
070: public FileChangeSupportTest(String testName) {
071: super (testName);
072: }
073:
074: private static final char SEP = File.separatorChar;
075:
076: private static final long SLEEP = 1000; // msec to sleep before touching a file again
077:
078: private FileObject scratch;
079: private String scratchPath;
080:
081: protected void setUp() throws Exception {
082: super .setUp();
083: scratch = TestUtil.makeScratchDir(this );
084: scratchPath = FileUtil.toFile(scratch).getAbsolutePath();
085: }
086:
087: public void testSimpleModification() throws Exception {
088: FileObject dir = scratch.createFolder("dir");
089: FileObject file = dir.createData("file");
090: File fileF = FileUtil.toFile(file);
091: L l = new L();
092: FileChangeSupport.DEFAULT.addListener(l, fileF);
093: TestUtil.createFileFromContent(null, dir, "file");
094: assertEquals("one mod in file", Collections.singletonList("M:"
095: + fileF), l.check());
096: assertEquals("that's all", Collections.EMPTY_LIST, l.check());
097: TestUtil.createFileFromContent(null, dir, "file");
098: assertEquals("another mod in file", Collections
099: .singletonList("M:" + fileF), l.check());
100: dir.createData("bogus");
101: assertEquals("nothing from a different file",
102: Collections.EMPTY_LIST, l.check());
103: TestUtil.createFileFromContent(null, dir, "bogus");
104: assertEquals("even after touching the other file",
105: Collections.EMPTY_LIST, l.check());
106: }
107:
108: public void testCreation() throws Exception {
109: File fileF = new File(scratchPath + SEP + "dir" + SEP + "file2");
110: L l = new L();
111: FileChangeSupport.DEFAULT.addListener(l, fileF);
112: FileObject dir = scratch.createFolder("dir");
113: assertEquals("no mods yet, just made parent dir",
114: Collections.EMPTY_LIST, l.check());
115: FileObject file = dir.createData("file2");
116: assertEquals("got file creation event", Collections
117: .singletonList("C:" + fileF), l.check());
118: TestUtil.createFileFromContent(null, dir, "file2");
119: assertEquals("and then a mod in file", Collections
120: .singletonList("M:" + fileF), l.check());
121: dir.createData("file2a");
122: assertEquals("nothing from a different file",
123: Collections.EMPTY_LIST, l.check());
124: }
125:
126: public void testDeletion() throws Exception {
127: File fileF = new File(scratchPath + SEP + "dir" + SEP + "file3");
128: L l = new L();
129: FileChangeSupport.DEFAULT.addListener(l, fileF);
130: FileObject dir = scratch.createFolder("dir");
131: assertEquals("no mods yet, just made parent dir",
132: Collections.EMPTY_LIST, l.check());
133: FileObject file = dir.createData("file3");
134: assertEquals("got file creation event", Collections
135: .singletonList("C:" + fileF), l.check());
136: file.delete();
137: assertEquals("got file deletion event", Collections
138: .singletonList("D:" + fileF), l.check());
139: dir.delete();
140: assertEquals(
141: "nothing from deleting containing dir when file already deleted",
142: Collections.EMPTY_LIST, l.check());
143: dir = scratch.createFolder("dir");
144: assertEquals("remade parent dir", Collections.EMPTY_LIST, l
145: .check());
146: file = dir.createData("file3");
147: assertEquals("recreated file", Collections.singletonList("C:"
148: + fileF), l.check());
149: dir.delete();
150: assertEquals("got file deletion event after dir deleted",
151: Collections.singletonList("D:" + fileF), l.check());
152: }
153:
154: public void testDiskChanges() throws Exception {
155: File fileF = new File(scratchPath + SEP + "dir" + SEP + "file2");
156: L l = new L();
157: FileChangeSupport.DEFAULT.addListener(l, fileF);
158: File dirF = new File(scratchPath + SEP + "dir");
159: dirF.mkdir();
160: new FileOutputStream(fileF).close();
161: scratch.getFileSystem().refresh(false);
162: assertEquals("got file creation event", Collections
163: .singletonList("C:" + fileF), l.check());
164: Thread.sleep(SLEEP); // make sure timestamp changes
165: new FileOutputStream(fileF).close();
166: scratch.getFileSystem().refresh(false);
167: assertEquals("and then a mod in file", Collections
168: .singletonList("M:" + fileF), l.check());
169: fileF.delete();
170: dirF.delete();
171: scratch.getFileSystem().refresh(false);
172: assertEquals("and then a file deletion event", Collections
173: .singletonList("D:" + fileF), l.check());
174: }
175:
176: public void test66444() throws Exception {
177: File fileF = new File(scratchPath + SEP + "dir" + SEP + "file2");
178: L l = new L();
179: FileChangeSupport.DEFAULT.addListener(l, fileF);
180: File dirF = new File(scratchPath + SEP + "dir");
181:
182: for (int cntr = 0; cntr < 50; cntr++) {
183: dirF.mkdir();
184: new FileOutputStream(fileF).close();
185: scratch.getFileSystem().refresh(false);
186: assertEquals("got file creation event, count=" + cntr,
187: Collections.singletonList("C:" + fileF), l.check());
188: fileF.delete();
189: dirF.delete();
190: scratch.getFileSystem().refresh(false);
191: assertEquals("and then a file deletion event, count="
192: + cntr, Collections.singletonList("D:" + fileF), l
193: .check());
194: }
195: }
196:
197: private static final class L implements FileChangeSupportListener {
198:
199: private final List<String> events = new ArrayList<String>();
200:
201: public L() {
202: }
203:
204: public List<String> check() {
205: List<String> toret = new ArrayList<String>(events);
206: events.clear();
207: return toret;
208: }
209:
210: public void fileCreated(FileChangeSupportEvent event) {
211: events.add("C:" + event.getPath());
212: }
213:
214: public void fileDeleted(FileChangeSupportEvent event) {
215: events.add("D:" + event.getPath());
216: }
217:
218: public void fileModified(FileChangeSupportEvent event) {
219: events.add("M:" + event.getPath());
220: }
221:
222: }
223:
224: }
|