alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.project.ant;

// XXX testRenames
// XXX testRemoveListener
// XXX testMultipleListenersOnSameFile
// XXX testSameListenerOnMultipleFiles

import org.netbeans.junit.NbTestCase;
import org.netbeans.api.project.TestUtil;
import org.openide.filesystems.FileObject;
import java.util.ArrayList;
import java.util.List;
import org.openide.filesystems.FileUtil;
import java.io.File;
import java.util.Collections;

import java.io.FileOutputStream;

/**
 * Test {@link FileChangeSupport}.
 * @author Jesse Glick
 */
public class FileChangeSupportTest extends NbTestCase {
    
    static {
        FileChangeSupportTest.class.getClassLoader().setDefaultAssertionStatus(true);
    }
    
    public FileChangeSupportTest(String testName) {
        super(testName);
    }

    private static final char SEP = File.separatorChar;
    
    private static final long SLEEP = 1000; // msec to sleep before touching a file again
    
    private FileObject scratch;
    private String scratchPath;
    
    protected void setUp() throws Exception {
        super.setUp();
        scratch = TestUtil.makeScratchDir(this);
        scratchPath = FileUtil.toFile(scratch).getAbsolutePath();
    }
    
    public void testSimpleModification() throws Exception {
        FileObject dir = scratch.createFolder("dir");
        FileObject file = dir.createData("file");
        File fileF = FileUtil.toFile(file);
        L l = new L();
        FileChangeSupport.DEFAULT.addListener(l, fileF);
        TestUtil.createFileFromContent(null, dir, "file");
        assertEquals("one mod in file", Collections.singletonList("M:" + fileF), l.check());
        assertEquals("that's all", Collections.EMPTY_LIST, l.check());
        TestUtil.createFileFromContent(null, dir, "file");
        assertEquals("another mod in file", Collections.singletonList("M:" + fileF), l.check());
        dir.createData("bogus");
        assertEquals("nothing from a different file", Collections.EMPTY_LIST, l.check());
        TestUtil.createFileFromContent(null, dir, "bogus");
        assertEquals("even after touching the other file", Collections.EMPTY_LIST, l.check());
    }
    
    public void testCreation() throws Exception {
        File fileF = new File(scratchPath + SEP + "dir" + SEP + "file2");
        L l = new L();
        FileChangeSupport.DEFAULT.addListener(l, fileF);
        FileObject dir = scratch.createFolder("dir");
        assertEquals("no mods yet, just made parent dir", Collections.EMPTY_LIST, l.check());
        FileObject file = dir.createData("file2");
        assertEquals("got file creation event", Collections.singletonList("C:" + fileF), l.check());
        TestUtil.createFileFromContent(null, dir, "file2");
        assertEquals("and then a mod in file", Collections.singletonList("M:" + fileF), l.check());
        dir.createData("file2a");
        assertEquals("nothing from a different file", Collections.EMPTY_LIST, l.check());
    }
    
    public void testDeletion() throws Exception {
        File fileF = new File(scratchPath + SEP + "dir" + SEP + "file3");
        L l = new L();
        FileChangeSupport.DEFAULT.addListener(l, fileF);
        FileObject dir = scratch.createFolder("dir");
        assertEquals("no mods yet, just made parent dir", Collections.EMPTY_LIST, l.check());
        FileObject file = dir.createData("file3");
        assertEquals("got file creation event", Collections.singletonList("C:" + fileF), l.check());
        file.delete();
        assertEquals("got file deletion event", Collections.singletonList("D:" + fileF), l.check());
        dir.delete();
        assertEquals("nothing from deleting containing dir when file already deleted", Collections.EMPTY_LIST, l.check());
        dir = scratch.createFolder("dir");
        assertEquals("remade parent dir", Collections.EMPTY_LIST, l.check());
        file = dir.createData("file3");
        assertEquals("recreated file", Collections.singletonList("C:" + fileF), l.check());
        dir.delete();
        assertEquals("got file deletion event after dir deleted", Collections.singletonList("D:" + fileF), l.check());
    }
    
    public void testDiskChanges() throws Exception {
        File fileF = new File(scratchPath + SEP + "dir" + SEP + "file2");
        L l = new L();
        FileChangeSupport.DEFAULT.addListener(l, fileF);
        File dirF = new File(scratchPath + SEP + "dir");
        dirF.mkdir();
        new FileOutputStream(fileF).close();
        scratch.getFileSystem().refresh(false);
        assertEquals("got file creation event", Collections.singletonList("C:" + fileF), l.check());
        Thread.sleep(SLEEP); // make sure timestamp changes
        new FileOutputStream(fileF).close();
        scratch.getFileSystem().refresh(false);
        assertEquals("and then a mod in file", Collections.singletonList("M:" + fileF), l.check());
        fileF.delete();
        dirF.delete();
        scratch.getFileSystem().refresh(false);
        assertEquals("and then a file deletion event", Collections.singletonList("D:" + fileF), l.check());
    }
    
    private static final class L implements FileChangeSupportListener {
        
        private final List/**/ events = new ArrayList();
        
        public L() {}
        
        public List/**/ check() {
            List toret = new ArrayList(events);
            events.clear();
            return toret;
        }
        
        public void fileCreated(FileChangeSupportEvent event) {
            events.add("C:" + event.getPath());
        }

        public void fileDeleted(FileChangeSupportEvent event) {
            events.add("D:" + event.getPath());
        }

        public void fileModified(FileChangeSupportEvent event) {
            events.add("M:" + event.getPath());
        }
        
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.