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