1 19 20 package org.apache.tools.ant.module.xml; 21 22 import java.io.File ; 23 import java.io.OutputStream ; 24 import javax.swing.event.ChangeEvent ; 25 import javax.swing.event.ChangeListener ; 26 import org.apache.tools.ant.module.api.AntProjectCookie; 27 import org.apache.tools.ant.module.loader.AntProjectDataLoader; 28 import org.apache.tools.ant.module.loader.AntProjectDataObject; 29 import org.netbeans.junit.MockServices; 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.filesystems.FileLock; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.loaders.DataObject; 35 import org.w3c.dom.Document ; 36 37 40 44 public class AntProjectSupportTest extends NbTestCase { 45 46 public AntProjectSupportTest(String name) { 47 super(name); 48 } 49 50 private FileObject scratch; 51 52 @Override 53 protected void setUp() throws Exception { 54 super.setUp(); 55 clearWorkDir(); 56 File scratchF = getWorkDir(); 57 scratch = FileUtil.toFileObject(scratchF); 58 assertNotNull("FO for " + scratchF, scratch); 59 MockServices.setServices(AntProjectDataLoader.class); 60 } 61 62 public void testInitiallyInvalidScript() throws Exception { 63 FileObject fo = scratch.createData("build.xml"); 64 assertEquals("it is an APDO", AntProjectDataObject.class, DataObject.find(fo).getClass()); 65 AntProjectCookie apc = new AntProjectSupport(fo); 66 TestCL l = new TestCL(); 67 apc.addChangeListener(l); 68 assertNull("invalid", apc.getDocument()); 69 assertNotNull("invalid", apc.getParseException()); 70 FileLock lock = fo.lock(); 71 try { 72 OutputStream os = fo.getOutputStream(lock); 73 try { 74 os.write("<project default='x'><target name='x'/></project>".getBytes("UTF-8")); 75 } finally { 76 os.close(); 77 } 78 } finally { 79 lock.releaseLock(); 80 } 81 assertTrue("got a change", l.expect(5000)); 82 Thread.sleep(1000); assertEquals("now valid (no exc)", null, apc.getParseException()); 84 Document doc = apc.getDocument(); 85 assertNotNull("now valid (have doc)", doc); 86 assertEquals("one target", 1, doc.getElementsByTagName("target").getLength()); 87 } 88 89 93 private static final class TestCL implements ChangeListener { 94 95 private boolean fired; 96 97 public TestCL() {} 98 99 public synchronized void stateChanged(ChangeEvent e) { 100 fired = true; 101 notify(); 102 } 103 104 109 public synchronized boolean expect() { 110 boolean f = fired; 111 fired = false; 112 return f; 113 } 114 115 121 public synchronized boolean expect(long timeout) throws InterruptedException { 122 if (!fired) { 123 wait(timeout); 124 } 125 return expect(); 126 } 127 128 } 129 130 } 131 | Popular Tags |