1 19 20 package org.netbeans.spi.project.support.ant; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.OutputStream ; 25 import org.netbeans.api.project.ProjectManager; 26 import org.netbeans.api.project.TestUtil; 27 import org.netbeans.api.queries.FileBuiltQuery; 28 import org.netbeans.junit.NbTestCase; 29 import org.netbeans.spi.queries.FileBuiltQueryImplementation; 30 import org.openide.filesystems.FileLock; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileUtil; 33 import org.openide.loaders.DataObject; 34 35 37 41 public class GlobFileBuiltQueryTest extends NbTestCase { 42 43 static { 44 TestUtil.setLookup(new Object [] { 45 AntBasedTestUtil.testAntBasedProjectType(), 46 }); 47 } 48 49 public GlobFileBuiltQueryTest(String name) { 50 super(name); 51 } 52 53 private FileObject scratch; 54 private FileObject prj; 55 private FileObject extsrc; 56 private FileObject extbuild; 57 private AntProjectHelper h; 58 private FileBuiltQueryImplementation fbqi; 59 private FileObject foo, bar, fooTest, baz, nonsense; 60 private FileBuiltQuery.Status fooStatus, barStatus, fooTestStatus, bazStatus; 61 62 protected void setUp() throws Exception { 63 super.setUp(); 64 scratch = TestUtil.makeScratchDir(this); 65 prj = scratch.createFolder("prj"); 66 h = ProjectGenerator.createProject(prj, "test"); 67 extsrc = scratch.createFolder("extsrc"); 68 extbuild = scratch.createFolder("extbuild"); 69 EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 70 ep.setProperty("src.dir", "src"); 71 ep.setProperty("test.src.dir", "test/src"); 72 ep.setProperty("ext.src.dir", "../extsrc"); 73 ep.setProperty("build.classes.dir", "build/classes"); 74 ep.setProperty("test.build.classes.dir", "build/test/classes"); 75 ep.setProperty("ext.build.classes.dir", "../extbuild/classes"); 76 h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 77 ProjectManager.getDefault().saveProject(ProjectManager.getDefault().findProject(prj)); 78 foo = TestUtil.createFileFromContent(null, prj, "src/pkg/Foo.java"); 79 bar = TestUtil.createFileFromContent(null, prj, "src/pkg/Bar.java"); 80 fooTest = TestUtil.createFileFromContent(null, prj, "test/src/pkg/FooTest.java"); 81 baz = TestUtil.createFileFromContent(null, extsrc, "pkg2/Baz.java"); 82 nonsense = TestUtil.createFileFromContent(null, prj, "misc-src/whatever/Nonsense.java"); 83 fbqi = h.createGlobFileBuiltQuery(h.getStandardPropertyEvaluator(), new String [] { 84 "${src.dir}/*.java", 85 "${test.src.dir}/*.java", 86 "${ext.src.dir}/*.java", 87 }, new String [] { 88 "${build.classes.dir}/*.class", 89 "${test.build.classes.dir}/*.class", 90 "${ext.build.classes.dir}/*.class", 91 }); 92 fooStatus = fbqi.getStatus(foo); 93 barStatus = fbqi.getStatus(bar); 94 fooTestStatus = fbqi.getStatus(fooTest); 95 bazStatus = fbqi.getStatus(baz); 96 } 97 98 99 private static final long PAUSE = 1500; 100 101 public void testBasicFunctionality() throws Exception { 102 assertNotNull("have status for Foo.java", fooStatus); 103 assertNotNull("have status for Bar.java", barStatus); 104 assertNotNull("have status for FooTest.java", fooTestStatus); 105 assertNull("non-matching file ignored", fbqi.getStatus(nonsense)); 106 assertFalse("Foo.java not built", fooStatus.isBuilt()); 107 assertFalse("Bar.java not built", barStatus.isBuilt()); 108 assertFalse("FooTest.java not built", fooTestStatus.isBuilt()); 109 FileObject fooClass = TestUtil.createFileFromContent(null, prj, "build/classes/pkg/Foo.class"); 110 assertTrue("Foo.java now built", fooStatus.isBuilt()); 111 Thread.sleep(PAUSE); 112 TestUtil.createFileFromContent(null, prj, "src/pkg/Foo.java"); 113 assertFalse("Foo.class out of date", fooStatus.isBuilt()); 114 TestUtil.createFileFromContent(null, prj, "build/classes/pkg/Foo.class"); 115 assertTrue("Foo.class rebuilt", fooStatus.isBuilt()); 116 fooClass.delete(); 117 assertFalse("Foo.class deleted", fooStatus.isBuilt()); 118 TestUtil.createFileFromContent(null, prj, "build/test/classes/pkg/FooTest.class"); 119 assertTrue("FooTest.java now built", fooTestStatus.isBuilt()); 120 assertFalse("Bar.java still not built", barStatus.isBuilt()); 121 TestUtil.createFileFromContent(null, prj, "build/classes/pkg/Foo.class"); 122 assertTrue("Foo.java built again", fooStatus.isBuilt()); 123 DataObject.find(foo).setModified(true); 124 assertFalse("Foo.java modified", fooStatus.isBuilt()); 125 DataObject.find(foo).setModified(false); 126 assertTrue("Foo.java unmodified again", fooStatus.isBuilt()); 127 FileObject buildDir = prj.getFileObject("build"); 128 assertNotNull("build dir exists", buildDir); 129 buildDir.delete(); 130 assertFalse("Foo.java not built (build dir gone)", fooStatus.isBuilt()); 131 assertFalse("Bar.java still not built", barStatus.isBuilt()); 132 assertFalse("FooTest.java not built (build dir gone)", fooTestStatus.isBuilt()); 133 bar.delete(); 135 barStatus.isBuilt(); 136 } 137 138 139 private static final long WAIT = 10000; 140 141 private static final long QUICK_WAIT = 500; 142 143 public void testChangeFiring() throws Exception { 144 AntBasedTestUtil.TestCL fooL = new AntBasedTestUtil.TestCL(); 145 fooStatus.addChangeListener(fooL); 146 AntBasedTestUtil.TestCL barL = new AntBasedTestUtil.TestCL(); 147 barStatus.addChangeListener(barL); 148 AntBasedTestUtil.TestCL fooTestL = new AntBasedTestUtil.TestCL(); 149 fooTestStatus.addChangeListener(fooTestL); 150 assertFalse("Foo.java not built", fooStatus.isBuilt()); 151 FileObject fooClass = TestUtil.createFileFromContent(null, prj, "build/classes/pkg/Foo.class"); 152 assertTrue("change in Foo.java", fooL.expect(WAIT)); 153 assertTrue("Foo.java now built", fooStatus.isBuilt()); 154 assertFalse("no more changes in Foo.java", fooL.expect(QUICK_WAIT)); 155 fooClass.delete(); 156 assertTrue("change in Foo.java", fooL.expect(WAIT)); 157 assertFalse("Foo.java no longer built", fooStatus.isBuilt()); 158 assertFalse("no changes yet in FooTest.java", fooTestL.expect(QUICK_WAIT)); 159 assertFalse("FooTest.java not yet built", fooTestStatus.isBuilt()); 160 FileObject fooTestClass = TestUtil.createFileFromContent(null, prj, "build/test/classes/pkg/FooTest.class"); 161 assertTrue("change in FooTest.java", fooTestL.expect(WAIT)); 162 assertTrue("FooTest.java now built", fooTestStatus.isBuilt()); 163 FileObject buildDir = prj.getFileObject("build"); 164 assertNotNull("build dir exists", buildDir); 165 buildDir.delete(); 166 assertFalse("no change in Foo.java (still not built)", fooL.expect(QUICK_WAIT)); 167 assertFalse("Foo.java not built (build dir gone)", fooStatus.isBuilt()); 168 assertTrue("got change in FooTest.java (build dir gone)", fooTestL.expect(WAIT)); 169 assertFalse("FooTest.java not built (build dir gone)", fooTestStatus.isBuilt()); 170 assertFalse("never got changes in Bar.java (never built)", barL.expect(QUICK_WAIT)); 171 TestUtil.createFileFromContent(null, prj, "build/classes/pkg/Foo.class"); 172 assertTrue("change in Foo.class", fooL.expect(WAIT)); 173 assertTrue("Foo.class created", fooStatus.isBuilt()); 174 Thread.sleep(PAUSE); 175 TestUtil.createFileFromContent(null, prj, "src/pkg/Foo.java"); 176 assertTrue("change in Foo.java", fooL.expect(WAIT)); 177 assertFalse("Foo.class out of date", fooStatus.isBuilt()); 178 TestUtil.createFileFromContent(null, prj, "build/classes/pkg/Foo.class"); 179 assertTrue("touched Foo.class", fooL.expect(WAIT)); 180 assertTrue("Foo.class touched", fooStatus.isBuilt()); 181 DataObject.find(foo).setModified(true); 182 assertTrue("Foo.java modified in memory", fooL.expect(WAIT)); 183 assertFalse("Foo.java modified in memory", fooStatus.isBuilt()); 184 DataObject.find(foo).setModified(false); 185 assertTrue("Foo.java unmodified in memory", fooL.expect(WAIT)); 186 assertTrue("Foo.java unmodified again", fooStatus.isBuilt()); 187 File buildF = new File (FileUtil.toFile(prj), "build"); 188 assertTrue("build dir exists", buildF.isDirectory()); 189 TestUtil.deleteRec(buildF); 190 assertFalse(buildF.getAbsolutePath() + " is gone", buildF.exists()); 191 prj.getFileSystem().refresh(false); 192 assertTrue("build dir deleted", fooL.expect(WAIT)); 193 assertFalse("Foo.class gone (no build dir)", fooStatus.isBuilt()); 194 File pkg = new File (buildF, "classes/pkg".replace('/', File.separatorChar)); 195 File fooClassF = new File (pkg, "Foo.class"); 196 assertTrue("created " + pkg, pkg.mkdirs()); 198 assertFalse("no such file yet: " + fooClassF, fooClassF.exists()); 199 OutputStream os = new FileOutputStream (fooClassF); 200 os.close(); 201 prj.getFileSystem().refresh(false); 202 assertTrue(fooClassF.getAbsolutePath() + " created on disk", fooL.expect(WAIT)); 203 assertTrue("Foo.class back", fooStatus.isBuilt()); 204 Thread.sleep(PAUSE); 205 TestUtil.createFileFromContent(null, prj, "src/pkg/Foo.java"); 206 assertTrue("change in Foo.java", fooL.expect(WAIT)); 207 assertFalse("Foo.class out of date", fooStatus.isBuilt()); 208 os = new FileOutputStream (fooClassF); 209 os.write(69); os.close(); 211 prj.getFileSystem().refresh(false); 212 assertTrue("Foo.class recreated on disk", fooL.expect(WAIT)); 213 assertTrue("Foo.class touched", fooStatus.isBuilt()); 214 } 215 216 public void testExternalSourceRoots() throws Exception { 217 assertNotNull("have status for Baz.java", bazStatus); 219 AntBasedTestUtil.TestCL bazL = new AntBasedTestUtil.TestCL(); 220 bazStatus.addChangeListener(bazL); 221 assertFalse("Baz.java not built", bazStatus.isBuilt()); 222 FileObject bazClass = TestUtil.createFileFromContent(null, extbuild, "classes/pkg2/Baz.class"); 223 assertTrue("got change", bazL.expect(WAIT)); 224 assertTrue("Baz.java now built", bazStatus.isBuilt()); 225 Thread.sleep(PAUSE); 226 TestUtil.createFileFromContent(null, extsrc, "pkg2/Baz.java"); 227 assertTrue("got change", bazL.expect(WAIT)); 228 assertFalse("Baz.class out of date", bazStatus.isBuilt()); 229 TestUtil.createFileFromContent(null, extbuild, "classes/pkg2/Baz.class"); 230 assertTrue("got change", bazL.expect(WAIT)); 231 assertTrue("Baz.class rebuilt", bazStatus.isBuilt()); 232 bazClass.delete(); 233 assertTrue("got change", bazL.expect(WAIT)); 234 assertFalse("Baz.class deleted", bazStatus.isBuilt()); 235 TestUtil.createFileFromContent(null, extbuild, "classes/pkg2/Baz.class"); 236 assertTrue("got change", bazL.expect(WAIT)); 237 assertTrue("Baz.java built again", bazStatus.isBuilt()); 238 DataObject.find(baz).setModified(true); 239 assertTrue("got change", bazL.expect(WAIT)); 240 assertFalse("Baz.java modified", bazStatus.isBuilt()); 241 DataObject.find(baz).setModified(false); 242 assertTrue("got change", bazL.expect(WAIT)); 243 assertTrue("Baz.java unmodified again", bazStatus.isBuilt()); 244 extbuild.delete(); 245 assertTrue("got change", bazL.expect(WAIT)); 246 assertFalse("Baz.java not built (build dir gone)", bazStatus.isBuilt()); 247 } 248 249 public void testFileRenames() throws Exception { 250 assertNotNull("have status for Foo.java", fooStatus); 252 AntBasedTestUtil.TestCL fooL = new AntBasedTestUtil.TestCL(); 253 fooStatus.addChangeListener(fooL); 254 assertFalse("Foo.java not built", fooStatus.isBuilt()); 255 FileObject fooClass = TestUtil.createFileFromContent(null, prj, "build/classes/pkg/Foo.class"); 256 assertTrue("got change", fooL.expect(WAIT)); 257 assertTrue("Foo.java now built", fooStatus.isBuilt()); 258 FileLock lock = foo.lock(); 259 try { 260 foo.rename(lock, "Foo2", "java"); 261 } finally { 262 lock.releaseLock(); 263 } 264 assertTrue("got change", fooL.expect(WAIT)); 265 assertFalse("Foo2.java no longer built", fooStatus.isBuilt()); 266 fooClass = TestUtil.createFileFromContent(null, prj, "build/classes/pkg/Foo2.class"); 267 assertTrue("got change", fooL.expect(WAIT)); 268 assertTrue("Now Foo2.java is built", fooStatus.isBuilt()); 269 } 270 271 273 public void testInvalidFile() throws Exception { 274 FileObject baz = TestUtil.createFileFromContent(null, prj, "src/pkg/Baz.java"); 275 276 baz.delete(); 277 assertNull(fbqi.getStatus(baz)); 278 } 279 280 } 281 | Popular Tags |