1 19 20 package org.netbeans.modules.java.j2seproject; 21 22 import java.io.File ; 23 import org.netbeans.api.project.ProjectUtils; 24 import org.openide.modules.SpecificationVersion; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.NodeList ; 27 import org.w3c.dom.Document ; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileUtil; 30 import org.openide.util.Lookup; 31 import org.netbeans.junit.NbTestCase; 32 import org.netbeans.api.project.ProjectManager; 33 import org.netbeans.api.project.Project; 34 import org.netbeans.api.project.TestUtil; 35 import org.netbeans.api.project.Sources; 36 import org.netbeans.api.queries.SharabilityQuery; 37 import org.netbeans.spi.project.support.ant.AntProjectHelper; 38 import org.netbeans.spi.project.support.ant.EditableProperties; 39 40 public class J2SESharabilityQueryTest extends NbTestCase { 41 42 public J2SESharabilityQueryTest(String testName) { 43 super(testName); 44 } 45 46 private FileObject scratch; 47 private FileObject projdir; 48 private FileObject sources; 49 private FileObject tests; 50 private FileObject dist; 51 private FileObject build; 52 private ProjectManager pm; 53 private J2SEProject pp; 54 private AntProjectHelper helper; 55 56 protected void setUp() throws Exception { 57 super.setUp(); 58 TestUtil.setLookup(new Object [] { 59 new J2SEProjectType(), 60 new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation() 61 }); 62 scratch = TestUtil.makeScratchDir(this); 63 projdir = scratch.createFolder("proj"); 64 J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"proj",null,null); J2SEProjectGenerator.setDefaultSourceLevel(null); 67 sources = projdir.getFileObject("src"); 68 tests = projdir.getFileObject("test"); 69 dist = FileUtil.createFolder(projdir,"dist"); 70 build = FileUtil.createFolder(projdir,"build"); 71 pm = ProjectManager.getDefault(); 72 Project p = pm.findProject(projdir); 73 assertTrue("Invalid project type",p instanceof J2SEProject); 74 pp = (J2SEProject) p; 75 } 76 77 protected void tearDown() throws Exception { 78 scratch = null; 79 projdir = null; 80 sources = null; 81 tests = null; 82 pm = null; 83 pp = null; 84 helper = null; 85 TestUtil.setLookup(Lookup.EMPTY); 86 super.tearDown(); 87 } 88 89 public void testSharability () throws Exception { 90 File f = FileUtil.toFile (this.sources); 91 int res = SharabilityQuery.getSharability(f); 92 assertEquals("Sources must be sharable", SharabilityQuery.SHARABLE, res); 93 f = FileUtil.toFile (this.tests); 94 res = SharabilityQuery.getSharability(f); 95 assertEquals("Tests must be sharable", SharabilityQuery.SHARABLE, res); 96 f = FileUtil.toFile (this.build); 97 res = SharabilityQuery.getSharability(f); 98 assertEquals("Build can't be sharable", SharabilityQuery.NOT_SHARABLE, res); 99 f = FileUtil.toFile (this.dist); 100 res = SharabilityQuery.getSharability(f); 101 assertEquals("Dist can't be sharable", SharabilityQuery.NOT_SHARABLE, res); 102 FileObject newSourceRoot = addSourceRoot(helper, projdir, "src2.dir",new File (FileUtil.toFile(scratch),"sources2")); 103 ProjectUtils.getSources(pp).getSourceGroups(Sources.TYPE_GENERIC); 104 f = FileUtil.toFile (newSourceRoot); 105 res = SharabilityQuery.getSharability(f); 106 assertEquals("Sources2 must be sharable", SharabilityQuery.SHARABLE, res); 107 FileObject newSourceRoot2 = changeSourceRoot(helper, projdir, "src2.dir", new File (FileUtil.toFile(scratch),"sources3")); 108 f = FileUtil.toFile (newSourceRoot2); 109 res = SharabilityQuery.getSharability(f); 110 assertEquals("Sources3 must be sharable", SharabilityQuery.SHARABLE, res); 111 f = FileUtil.toFile (newSourceRoot); 112 res = SharabilityQuery.getSharability(f); 113 assertEquals("Sources2 must be unknown", SharabilityQuery.UNKNOWN, res); 114 } 115 116 public static FileObject addSourceRoot (AntProjectHelper helper, FileObject projdir, 117 String propName, File folder) throws Exception { 118 if (!folder.exists()) { 119 folder.mkdirs(); 120 } 121 Element data = helper.getPrimaryConfigurationData(true); 122 NodeList nl = data.getElementsByTagNameNS (J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); 123 assert nl.getLength() == 1; 124 Element roots = (Element ) nl.item(0); 125 Document doc = roots.getOwnerDocument(); 126 Element root = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); 127 root.setAttribute("id", propName); 128 roots.appendChild (root); 129 helper.putPrimaryConfigurationData (data,true); 130 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 131 props.put (propName,folder.getAbsolutePath()); 132 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,props); 133 return FileUtil.toFileObject(folder); 134 } 135 136 public static FileObject changeSourceRoot (AntProjectHelper helper, FileObject projdir, 137 String propName, File folder) throws Exception { 138 if (!folder.exists()) { 139 folder.mkdirs(); 140 } 141 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 142 assert props.containsKey(propName); 143 props.put (propName,folder.getAbsolutePath()); 144 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,props); 145 return FileUtil.toFileObject(folder); 146 } 147 148 } 149 | Popular Tags |