? ant/external/ant-api-1.6.1.zip ? ant/external/ant-docs-1.6.1.zip ? ant/external/ant-libs-1.6.1.zip Index: ant/project/test/unit/src/org/netbeans/spi/project/support/ant/PropertyUtilsTest.java =================================================================== RCS file: /cvs/ant/project/test/unit/src/org/netbeans/spi/project/support/ant/PropertyUtilsTest.java,v retrieving revision 1.13 diff -u -r1.13 PropertyUtilsTest.java --- ant/project/test/unit/src/org/netbeans/spi/project/support/ant/PropertyUtilsTest.java 8 Sep 2004 21:59:13 -0000 1.13 +++ ant/project/test/unit/src/org/netbeans/spi/project/support/ant/PropertyUtilsTest.java 12 Oct 2004 16:11:21 -0000 @@ -13,6 +13,8 @@ package org.netbeans.spi.project.support.ant; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; @@ -30,11 +32,15 @@ import java.util.Properties; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; +import org.netbeans.api.project.ProjectManager; import org.netbeans.junit.NbTestCase; +import org.openide.filesystems.FileChangeAdapter; import org.openide.filesystems.FileLock; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileSystem; import org.openide.filesystems.FileUtil; +import org.openide.util.Mutex; +import org.openide.util.RequestProcessor; import org.openide.util.Utilities; /** @@ -544,6 +550,109 @@ assertEquals("right main-2-b", "main-2-b-val+main-1-b-val+pre-b-val", defs.get("main-2-b")); } + + public void testNoFiringUnderWriteAccess() throws Exception { + File propFile = FileUtil.normalizeFile(new File(System.getProperty("java.io.tmpdir"), "build.properties")); + final FileObject propFileObject = FileUtil.toFileObject( propFile ); + + class TestFileObjectListener extends FileChangeAdapter { + + public int fileChangeCount; + + public synchronized void fileChanged(org.openide.filesystems.FileEvent fe) { + fileChangeCount++; + propFileObject.removeFileChangeListener( this ); + assertFalse( "Don't fire events under write access", ProjectManager.mutex().isWriteAccess() ); + } + } + + TestFileObjectListener tfol = new TestFileObjectListener(); + propFileObject.addFileChangeListener( tfol ); + + EditableProperties p = new EditableProperties(); + p.setProperty("key1", "val1"); + p.setProperty("key2", "val2"); + PropertyUtils.putGlobalProperties(p); + + assertEquals( "File listener call count", 1, tfol.fileChangeCount ); + + } + + + public void testFiringInRightThread() throws Exception { + File propFile = FileUtil.normalizeFile(new File(System.getProperty("java.io.tmpdir"), "build.properties")); + FileObject propFileObject = FileUtil.toFileObject( propFile ); + + final EditableProperties p = new EditableProperties(); + final Thread current = Thread.currentThread(); + + class TestFileObjectListener extends FileChangeAdapter implements Runnable, PropertyChangeListener { + + public int fileChangeCount; + public int stateChangeCount; + public Error error; + public PropertyEvaluator peval; + + + public synchronized void fileChanged(org.openide.filesystems.FileEvent fe) { + fileChangeCount++; + RequestProcessor.getDefault().post( this ); + try { + wait(); + } + catch( InterruptedException e ) { + fail( "nasty exception" ); + } + } + + public void propertyChange( PropertyChangeEvent evt ) { + if ( Thread.currentThread() != current ) { + fail( "Bad thread, expected: " + current + " real " + Thread.currentThread() ); + } + stateChangeCount++; + } + + public void run() { + try { + assertEquals( null, peval.getProperty( "key1") ); + assertEquals( null, peval.getProperty( "key2") ); + } + catch( Error e ) { + error = e; + } + finally { + synchronized ( this ) { + notify(); + } + } + } + } + + TestFileObjectListener tfol = new TestFileObjectListener(); + propFileObject.getFileSystem().addFileChangeListener( tfol ); + final PropertyProvider pp = PropertyUtils.propertiesFilePropertyProvider( propFile ); + tfol.peval = PropertyUtils.sequentialPropertyEvaluator(pp, new PropertyProvider[0]); + tfol.peval.addPropertyChangeListener( tfol ); + + + //ProjectManager.mutex().writeAccess( new Mutex.ExceptionAction() { + // public Object run() throws IOException { + p.setProperty("key1", "val1"); + p.setProperty("key2", "val2"); + PropertyUtils.putGlobalProperties(p); + // return null; + //} + //} ); + + assertEquals( "File listener call count", 1, tfol.fileChangeCount ); + + if ( tfol.error != null ) { + throw tfol.error; + } + + assertEquals( "StateChange listener call count", 2, tfol.stateChangeCount ); + } + private static final class TestMutablePropertyProvider implements PropertyProvider { public final Map/**/ defs; @@ -574,5 +683,6 @@ } } + }