1 19 20 package org.netbeans.modules.java.freeform; 21 22 import java.io.FileOutputStream ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.FileUtil; 29 import org.netbeans.api.project.Project; 30 import org.netbeans.api.project.ProjectManager; 31 import org.netbeans.api.queries.FileBuiltQuery; 32 import org.netbeans.modules.ant.freeform.TestBase; 33 import org.netbeans.spi.queries.FileBuiltQueryImplementation; 34 35 39 public class JavaFreeformFileBuiltQueryTest extends TestBase { 40 41 public JavaFreeformFileBuiltQueryTest(String testName) { 42 super(testName); 43 } 44 45 private Project simpleProject; 46 47 private void deepCopy(FileObject source, FileObject target) throws IOException { 48 List toCopy = new LinkedList (); 49 50 FileObject[] children = source.getChildren(); 51 52 for (int cntr = 0; cntr < children.length; cntr++) { 53 toCopy.add(children[cntr].getNameExt()); 54 } 55 56 while (toCopy.size() > 0) { 57 String copying = (String ) toCopy.remove(0); 58 FileObject toCopyFO = source.getFileObject(copying); 59 FileObject targetParent = target.getFileObject(FileUtil.getRelativePath(source, toCopyFO.getParent())); 60 61 if (toCopyFO.isFolder()) { 62 targetParent.createFolder(toCopyFO.getNameExt()); 63 64 children = toCopyFO.getChildren(); 65 66 for (int cntr = 0; cntr < children.length; cntr++) { 67 toCopy.add(FileUtil.getRelativePath(source, children[cntr])); 68 } 69 } else { 70 FileUtil.copyFile(toCopyFO, targetParent, toCopyFO.getName(), toCopyFO.getExt()); 71 } 72 } 73 } 74 75 protected void setUp() throws Exception { 76 super.setUp(); 77 assertNotNull("Must have built ant/freeform unit tests first, INCLUDING copying non-*.java resources to the classes build directory", 78 JavaFreeformFileBuiltQueryTest.class.getResource("/META-INF/services/org.openide.modules.InstalledFileLocator")); 79 80 FileObject root = simple.getProjectDirectory(); 81 clearWorkDir(); 82 File workdir = getWorkDir(); 83 FileObject workdirFO = FileUtil.toFileObject(workdir); 84 FileObject simpleFO = workdirFO.createFolder("simple"); 85 86 deepCopy(root, simpleFO); 87 88 simpleProject = ProjectManager.getDefault().findProject(simpleFO); 89 } 90 91 private void createEmptyFile(File f) throws IOException { 92 f.getParentFile().mkdirs(); 93 new FileOutputStream (f).close(); 94 } 95 96 private void refreshRecursively(FileObject of) { 97 List toRefresh = new LinkedList (); 98 99 toRefresh.add(of); 100 101 while (toRefresh.size() > 0) { 102 FileObject f = (FileObject) toRefresh.remove(0); 103 104 f.refresh(); 105 106 FileObject[] children = f.getChildren(); 107 108 for (int cntr = 0; cntr < children.length; cntr++) { 109 toRefresh.add(children[cntr]); 110 } 111 } 112 } 113 114 public void testStatusUpdated() throws Exception { 115 FileObject src = simpleProject.getProjectDirectory().getFileObject("src"); 121 FileObject antsrc = simpleProject.getProjectDirectory().getFileObject("antsrc"); 122 123 FileObject MyAppJava = src.getFileObject("org/foo/myapp/MyApp.java"); 124 FileObject SomeFileJava = src.getFileObject("org/foo/myapp/SomeFile.java"); 125 FileObject SpecialTaskJava = antsrc.getFileObject("org/foo/ant/SpecialTask.java"); 126 127 File MyAppClass = new File (FileUtil.toFile(simpleProject.getProjectDirectory()), "build/classes/org/foo/myapp/MyApp.class".replace('/', File.separatorChar)); 128 File SomeFileClass = new File (FileUtil.toFile(simpleProject.getProjectDirectory()), "build/classes/org/foo/myapp/SomeFile.class".replace('/', File.separatorChar)); 129 File SpecialTaskClass = new File (FileUtil.toFile(simpleProject.getProjectDirectory()), "build/antclasses/org/foo/ant/SpecialTask.class".replace('/', File.separatorChar)); 130 131 FileBuiltQueryImplementation fbqi = simpleProject.getLookup().lookup(FileBuiltQueryImplementation.class); 132 133 assertNotNull("have FileBuiltQueryImplementation in lookup", fbqi); 134 135 FileBuiltQuery.Status MyAppStatus = fbqi.getStatus(MyAppJava); 136 FileBuiltQuery.Status SomeFileStatus = fbqi.getStatus(SomeFileJava); 137 FileBuiltQuery.Status SpecialTaskStatus = fbqi.getStatus(SpecialTaskJava); 138 139 assertFalse("MyApp.java is not built", MyAppStatus.isBuilt()); 140 assertFalse("SomeFile.java is not built", SomeFileStatus.isBuilt()); 141 assertFalse("SpecialTask.java is not built", SpecialTaskStatus.isBuilt()); 142 143 createEmptyFile(MyAppClass); 144 createEmptyFile(SpecialTaskClass); 145 146 refreshRecursively(simpleProject.getProjectDirectory()); 147 148 assertTrue("MyApp.java is built", MyAppStatus.isBuilt()); 149 assertFalse("SomeFile.java is not built", SomeFileStatus.isBuilt()); 150 assertTrue("SpecialTask.java is built", SpecialTaskStatus.isBuilt()); 151 152 MyAppClass.delete(); 153 154 refreshRecursively(simpleProject.getProjectDirectory()); 155 156 assertFalse("MyApp.java is built", MyAppStatus.isBuilt()); 157 assertFalse("SomeFile.java is not built", SomeFileStatus.isBuilt()); 158 assertTrue("SpecialTask.java is built", SpecialTaskStatus.isBuilt()); 159 160 SpecialTaskClass.delete(); 161 createEmptyFile(MyAppClass); 162 createEmptyFile(SomeFileClass); 163 164 refreshRecursively(simpleProject.getProjectDirectory()); 165 166 assertTrue("MyApp.java is built", MyAppStatus.isBuilt()); 167 assertTrue("SomeFile.java is not built", SomeFileStatus.isBuilt()); 168 assertFalse("SpecialTask.java is built", SpecialTaskStatus.isBuilt()); 169 } 170 } 171 | Popular Tags |