1 19 20 package org.netbeans.modules.java.j2seproject; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.lang.reflect.Method ; 25 import org.netbeans.api.java.project.JavaProjectConstants; 26 import org.netbeans.api.java.queries.SourceForBinaryQuery; 27 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileUtil; 30 import org.openide.modules.SpecificationVersion; 31 import org.openide.util.Lookup; 32 import org.netbeans.junit.NbTestCase; 33 import org.netbeans.api.project.ProjectManager; 34 import org.netbeans.api.project.Project; 35 import org.netbeans.api.project.ProjectUtils; 36 import org.netbeans.api.project.SourceGroup; 37 import org.netbeans.api.project.TestUtil; 38 import org.netbeans.spi.project.support.ant.AntProjectHelper; 39 import org.netbeans.spi.project.support.ant.EditableProperties; 40 import org.netbeans.spi.project.ui.ProjectOpenedHook; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.NodeList ; 43 import org.w3c.dom.Document ; 44 45 51 public class J2SESourcesTest extends NbTestCase { 52 53 public J2SESourcesTest(String testName) { 54 super(testName); 55 } 56 57 private FileObject scratch; 58 private FileObject projdir; 59 private FileObject sources; 60 private FileObject build; 61 private FileObject classes; 62 private ProjectManager pm; 63 private Project project; 64 private AntProjectHelper helper; 65 66 protected void setUp() throws Exception { 67 super.setUp(); 68 TestUtil.setLookup(new Object [] { 69 new org.netbeans.modules.java.j2seproject.J2SEProjectType(), 70 new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation() 71 }); 72 scratch = TestUtil.makeScratchDir(this); 73 projdir = scratch.createFolder("proj"); 74 J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"proj",null,null); J2SEProjectGenerator.setDefaultSourceLevel(null); 77 sources = this.getFileObject(projdir, "src"); 78 build = this.getFileObject (scratch, "build"); 79 classes = this.getFileObject(build,"classes"); 80 File f = FileUtil.normalizeFile (FileUtil.toFile(build)); 81 String path = f.getAbsolutePath (); 82 EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 86 props.setProperty(J2SEProjectProperties.BUILD_DIR, path); 87 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 88 pm = ProjectManager.getDefault(); 89 project = pm.findProject(projdir); 90 assertTrue("Invalid project type", project instanceof J2SEProject); 91 } 92 93 protected void tearDown() throws Exception { 94 scratch = null; 95 projdir = null; 96 sources = null; 97 build = null; 98 classes = null; 99 pm = null; 100 project = null; 101 helper = null; 102 TestUtil.setLookup(Lookup.EMPTY); 103 super.tearDown(); 104 } 105 106 public void testSourceRoots () throws Exception { 107 FileObject[] roots = SourceForBinaryQuery.findSourceRoots(classes.getURL()).getRoots(); 108 assertNotNull (roots); 109 assertEquals("There should be 1 src root",1,roots.length); 110 assertTrue ("The source root is not valid", sources.isValid()); 111 assertEquals("Invalid src root", sources, roots[0]); 112 FileObject src2 = projdir.createFolder("src2"); 113 addSourceRoot (helper, src2, "src2"); 114 roots = SourceForBinaryQuery.findSourceRoots(classes.getURL()).getRoots(); 115 assertNotNull (roots); 116 assertEquals("There should be 2 src roots", 2, roots.length); 117 assertTrue ("The source root is not valid", sources.isValid()); 118 assertEquals("Invalid src root", sources, roots[0]); 119 assertTrue ("The source root 2 is not valid", src2.isValid()); 120 assertEquals("Invalid src2 root", src2, roots[1]); 121 } 122 123 public void testIncludesExcludes() throws Exception { 124 SourceGroup g = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA)[0]; 125 assertEquals(sources, g.getRootFolder()); 126 FileObject objectJava = FileUtil.createData(sources, "java/lang/Object.java"); 127 FileObject jcJava = FileUtil.createData(sources, "javax/swing/JComponent.java"); 128 FileObject doc = FileUtil.createData(sources, "javax/swing/doc-files/index.html"); 129 assertTrue(g.contains(objectJava)); 130 assertTrue(g.contains(jcJava)); 131 assertTrue(g.contains(doc)); 132 Method projectOpened = ProjectOpenedHook.class.getDeclaredMethod("projectOpened"); 133 projectOpened.setAccessible(true); 134 projectOpened.invoke(project.getLookup().lookup(ProjectOpenedHook.class)); 135 EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 136 assertEquals("includes/excludes were initialized to defaults", "**", ep.getProperty(J2SEProjectProperties.INCLUDES)); 137 assertEquals("includes/excludes were initialized to defaults", "", ep.getProperty(J2SEProjectProperties.EXCLUDES)); 138 ep.setProperty(J2SEProjectProperties.INCLUDES, "javax/swing/"); 139 ep.setProperty(J2SEProjectProperties.EXCLUDES, "**/doc-files/"); 140 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 141 pm.saveProject(project); 142 assertFalse(g.contains(objectJava)); 143 assertTrue(g.contains(jcJava)); 144 assertFalse(g.contains(doc)); 145 } 146 147 private static FileObject getFileObject (FileObject parent, String name) throws IOException { 148 FileObject result = parent.getFileObject(name); 149 if (result == null) { 150 result = parent.createFolder(name); 151 } 152 return result; 153 } 154 155 156 private static void addSourceRoot (AntProjectHelper helper, FileObject sourceFolder, String propName) throws Exception { 157 Element data = helper.getPrimaryConfigurationData(true); 158 NodeList nl = data.getElementsByTagNameNS (J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); 159 assert nl.getLength() == 1; 160 Element roots = (Element ) nl.item(0); 161 Document doc = roots.getOwnerDocument(); 162 Element root = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); 163 root.setAttribute("id", propName); 164 roots.appendChild (root); 165 helper.putPrimaryConfigurationData (data,true); 166 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 167 File f = FileUtil.normalizeFile(FileUtil.toFile(sourceFolder)); 168 props.put (propName, f.getAbsolutePath()); 169 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,props); 170 } 171 172 173 } 174 | Popular Tags |