1 19 20 package org.netbeans.modules.java.j2seproject.queries; 21 22 import java.io.IOException ; 23 import java.util.Properties ; 24 import javax.swing.event.ChangeEvent ; 25 import javax.swing.event.ChangeListener ; 26 import org.netbeans.api.java.queries.SourceForBinaryQuery; 27 import org.netbeans.api.project.Project; 28 import org.netbeans.api.project.ProjectManager; 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator; 31 import org.netbeans.modules.java.j2seproject.SourceRootsTest; 32 import org.openide.filesystems.FileObject; 33 import org.netbeans.api.project.TestUtil; 34 import org.netbeans.spi.project.support.ant.AntProjectHelper; 35 import org.netbeans.spi.project.support.ant.EditableProperties; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.modules.SpecificationVersion; 38 import org.openide.util.Lookup; 39 40 45 public class CompiledSourceForBinaryQueryTest extends NbTestCase { 46 47 public CompiledSourceForBinaryQueryTest(String testName) { 48 super(testName); 49 } 50 51 private FileObject scratch; 52 private FileObject projdir; 53 private FileObject sources; 54 private FileObject buildClasses; 55 private ProjectManager pm; 56 private Project pp; 57 AntProjectHelper helper; 58 59 protected void setUp() throws Exception { 60 super.setUp(); 61 TestUtil.setLookup(new Object [] { 62 new org.netbeans.modules.java.j2seproject.J2SEProjectType(), 63 new org.netbeans.modules.java.project.ProjectSourceForBinaryQuery(), 64 new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(), 65 }); 66 Properties p = System.getProperties(); 67 } 68 69 protected void tearDown() throws Exception { 70 scratch = null; 71 projdir = null; 72 pm = null; 73 TestUtil.setLookup(Lookup.EMPTY); 74 super.tearDown(); 75 } 76 77 78 private void prepareProject () throws IOException { 79 scratch = TestUtil.makeScratchDir(this); 80 projdir = scratch.createFolder("proj"); 81 J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"proj",null,null); 83 J2SEProjectGenerator.setDefaultSourceLevel(null); pm = ProjectManager.getDefault(); 85 pp = pm.findProject(projdir); 86 sources = projdir.getFileObject("src"); 87 FileObject fo = projdir.createFolder("build"); 88 buildClasses = fo.createFolder("classes"); 89 } 90 91 public void testSourceForBinaryQuery() throws Exception { 92 this.prepareProject(); 93 FileObject folder = scratch.createFolder("SomeFolder"); 94 SourceForBinaryQuery.Result result = SourceForBinaryQuery.findSourceRoots(folder.getURL()); 95 assertEquals("Non-project folder does not have any source folder", 0, result.getRoots().length); 96 folder = projdir.createFolder("SomeFolderInProject"); 97 result = SourceForBinaryQuery.findSourceRoots(folder.getURL()); 98 assertEquals("Project non build folder does not have any source folder", 0, result.getRoots().length); 99 result = SourceForBinaryQuery.findSourceRoots(buildClasses.getURL()); 100 assertEquals("Project build folder must have source folder", 1, result.getRoots().length); 101 assertEquals("Project build folder must have source folder",sources,result.getRoots()[0]); 102 } 103 104 105 public void testSourceForBinaryQueryListening () throws Exception { 106 this.prepareProject(); 107 SourceForBinaryQuery.Result result = SourceForBinaryQuery.findSourceRoots(buildClasses.getURL()); 108 assertEquals("Project build folder must have source folder", 1, result.getRoots().length); 109 assertEquals("Project build folder must have source folder",sources,result.getRoots()[0]); 110 TestListener tl = new TestListener (); 111 result.addChangeListener(tl); 112 EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 113 FileObject sources2 = projdir.createFolder("src2"); 114 props.put ("src.dir","src2"); 115 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 116 assertTrue (tl.wasEvent()); 117 assertEquals("Project build folder must have source folder", 1, result.getRoots().length); 118 assertEquals("Project build folder must have source folder",sources2,result.getRoots()[0]); 119 } 120 121 public void testSourceForBinaryQueryMultipleSourceRoots () throws Exception { 122 this.prepareProject(); 123 SourceForBinaryQuery.Result result = SourceForBinaryQuery.findSourceRoots(buildClasses.getURL()); 124 assertEquals("Project build folder must have source folder", 1, result.getRoots().length); 125 assertEquals("Project build folder must have source folder",sources,result.getRoots()[0]); 126 TestListener tl = new TestListener (); 127 result.addChangeListener(tl); 128 FileObject newRoot = SourceRootsTest.addSourceRoot(helper,projdir,"src.other.dir","other"); 129 assertTrue (tl.wasEvent()); 130 assertEquals("Project build folder must have 2 source folders", 2, result.getRoots().length); 131 assertEquals("Project build folder must have the first source folder",sources,result.getRoots()[0]); 132 assertEquals("Project build folder must have the second source folder",newRoot,result.getRoots()[1]); 133 } 134 135 private static class TestListener implements ChangeListener { 136 137 private boolean gotEvent; 138 139 public void stateChanged(ChangeEvent changeEvent) { 140 this.gotEvent = true; 141 } 142 143 public void reset () { 144 this.gotEvent = false; 145 } 146 147 public boolean wasEvent () { 148 return this.gotEvent; 149 } 150 } 151 152 } 153 | Popular Tags |