1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.io.File ; 23 import java.util.Collections ; 24 import java.util.List ; 25 import java.util.SortedSet ; 26 import java.util.TreeSet ; 27 import org.netbeans.api.project.FileOwnerQuery; 28 import org.netbeans.api.project.Project; 29 import org.netbeans.api.project.ProjectManager; 30 import org.netbeans.api.project.ProjectUtils; 31 import org.netbeans.api.project.SourceGroup; 32 import org.netbeans.api.project.Sources; 33 import org.netbeans.modules.ant.freeform.spi.support.Util; 34 import org.netbeans.spi.project.support.ant.AntProjectHelper; 35 import org.netbeans.spi.project.support.ant.EditableProperties; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.NodeList ; 41 42 45 49 public class FreeformSourcesTest extends TestBase { 50 51 public FreeformSourcesTest(String name) { 52 super(name); 53 } 54 55 public void testSources() throws Exception { 56 Sources s = ProjectUtils.getSources(simple); 57 SourceGroup[] groups = s.getSourceGroups(Sources.TYPE_GENERIC); 58 assertEquals("one generic group", 1, groups.length); 59 assertEquals("right root folder", simple.getProjectDirectory(), groups[0].getRootFolder()); 60 assertEquals("right display name", "Simple Freeform Project", groups[0].getDisplayName()); 61 groups = s.getSourceGroups("java"); 62 assertEquals("two Java groups", 2, groups.length); 63 assertEquals("right root folder #1", simple.getProjectDirectory().getFileObject("src"), groups[0].getRootFolder()); 64 assertEquals("right display name #1", "Main Sources", groups[0].getDisplayName()); 65 assertEquals("right root folder #2", simple.getProjectDirectory().getFileObject("antsrc"), groups[1].getRootFolder()); 66 assertEquals("right display name #2", "Ant Task Sources", groups[1].getDisplayName()); 67 } 68 69 public void testExternalSourceRoot() throws Exception { 70 Sources s = ProjectUtils.getSources(extsrcroot); 71 SourceGroup[] groups = s.getSourceGroups(Sources.TYPE_GENERIC); 72 assertEquals("one generic group", 1, groups.length); 73 assertEquals("right root folder", egdirFO.getFileObject("extsrcroot"), groups[0].getRootFolder()); 74 assertEquals("right display name", "Top-Level Dir", groups[0].getDisplayName()); 75 groups = s.getSourceGroups("java"); 76 assertEquals("one Java group", 1, groups.length); 77 assertEquals("right root folder", egdirFO.getFileObject("extsrcroot/src"), groups[0].getRootFolder()); 78 assertEquals("right display name", "External Sources", groups[0].getDisplayName()); 79 assertEquals("correct file owner", extsrcroot, FileOwnerQuery.getOwner(egdirFO.getFileObject("extsrcroot/src/org/foo/Foo.java"))); 80 } 81 82 public void testSourceRootChanges() throws Exception { 83 Sources s = ProjectUtils.getSources(extsrcroot); 84 SourceGroup[] groups = s.getSourceGroups("java"); 85 assertEquals("one Java group", 1, groups.length); 86 assertEquals("right root folder", egdirFO.getFileObject("extsrcroot/src"), groups[0].getRootFolder()); 87 TestCL l = new TestCL(); 88 s.addChangeListener(l); 89 Element data = extsrcroot.getPrimaryConfigurationData(); 90 Element folders = Util.findElement(data, "folders", FreeformProjectType.NS_GENERAL); 91 assertNotNull("have <folders>", folders); 92 List sourceFolders = Util.findSubElements(folders); 93 assertEquals("have 2 <source-folder>s", 2, sourceFolders.size()); 94 Element sourceFolder = (Element ) sourceFolders.get(1); 95 Element location = Util.findElement(sourceFolder, "location", FreeformProjectType.NS_GENERAL); 96 assertNotNull("have <location>", location); 97 NodeList nl = location.getChildNodes(); 98 assertEquals("one child (text)", 1, nl.getLength()); 99 location.removeChild(nl.item(0)); 100 location.appendChild(location.getOwnerDocument().createTextNode("../src2")); 101 extsrcroot.putPrimaryConfigurationData(data); 102 assertEquals("got a change in Sources", 1, l.changeCount()); 103 groups = s.getSourceGroups("java"); 104 assertEquals("one Java group", 1, groups.length); 105 assertEquals("right root folder", egdirFO.getFileObject("extsrcroot/src2"), groups[0].getRootFolder()); 106 } 107 108 public void testExternalBuildRoot() throws Exception { 109 FileObject builtFile = egdirFO.getFileObject("extbuildroot/build/built.file"); 111 assertNotNull("have built.file", builtFile); 112 assertEquals("owned by extbuildroot project", extbuildroot, FileOwnerQuery.getOwner(builtFile)); 113 } 114 115 public void testIncludesExcludes() throws Exception { 116 clearWorkDir(); 117 File d = getWorkDir(); 118 AntProjectHelper helper = FreeformProjectGenerator.createProject(d, d, "prj", null); 119 Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory()); 120 FileUtil.createData(new File (d, "s/relevant/included/file")); 121 FileUtil.createData(new File (d, "s/relevant/excluded/file")); 122 FileUtil.createData(new File (d, "s/ignored/file")); 123 Element data = Util.getPrimaryConfigurationData(helper); 124 Document doc = data.getOwnerDocument(); 125 Element sf = (Element ) data.insertBefore(doc.createElementNS(Util.NAMESPACE, "folders"), Util.findElement(data, "view", Util.NAMESPACE)). 126 appendChild(doc.createElementNS(Util.NAMESPACE, "source-folder")); 127 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "label")).appendChild(doc.createTextNode("Sources")); 128 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "type")).appendChild(doc.createTextNode("stuff")); 129 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "location")).appendChild(doc.createTextNode("s")); 130 Util.putPrimaryConfigurationData(helper, data); 131 ProjectManager.getDefault().saveProject(p); 132 Sources s = ProjectUtils.getSources(p); 133 SourceGroup[] gs = s.getSourceGroups("stuff"); 134 assertEquals(1, gs.length); 135 assertEquals(FileUtil.toFileObject(new File (d, "s")), gs[0].getRootFolder()); 136 assertEquals("Sources", gs[0].getDisplayName()); 137 assertEquals("ignored{file} relevant{excluded{file} included{file}}", expand(gs[0])); 138 EditableProperties ep = new EditableProperties(); 140 ep.put("includes", "relevant/"); 141 ep.put("excludes", "**/excluded/"); 142 helper.putProperties("config.properties", ep); 143 data = Util.getPrimaryConfigurationData(helper); 144 doc = data.getOwnerDocument(); 145 data.getElementsByTagName("properties").item(0). 146 appendChild(doc.createElementNS(Util.NAMESPACE, "property-file")). 147 appendChild(doc.createTextNode("config.properties")); 148 Util.putPrimaryConfigurationData(helper, data); 149 ProjectManager.getDefault().saveProject(p); 150 data = Util.getPrimaryConfigurationData(helper); 151 doc = data.getOwnerDocument(); 152 sf = (Element ) data.getElementsByTagName("source-folder").item(0); 153 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "includes")). 154 appendChild(doc.createTextNode("${includes}")); 155 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "excludes")). 156 appendChild(doc.createTextNode("${excludes}")); 157 Util.putPrimaryConfigurationData(helper, data); 158 ProjectManager.getDefault().saveProject(p); 159 gs = s.getSourceGroups("stuff"); 160 assertEquals("relevant{included{file}}", expand(gs[0])); 161 TestPCL l = new TestPCL(); 163 gs[0].addPropertyChangeListener(l); 164 ep = helper.getProperties("config.properties"); 165 ep.remove("includes"); 166 helper.putProperties("config.properties", ep); 167 ProjectManager.getDefault().saveProject(p); 168 assertEquals("ignored{file} relevant{included{file}}", expand(gs[0])); 169 assertEquals(Collections.singleton(SourceGroup.PROP_CONTAINERSHIP), l.changed); 170 } 171 private static String expand(SourceGroup g) { 172 return expand(g, g.getRootFolder()); 173 } 174 private static String expand(SourceGroup g, FileObject d) { 175 SortedSet <String > subs = new TreeSet <String >(); 176 for (FileObject kid : d.getChildren()) { 177 if (!g.contains(kid)) { 178 continue; 179 } 180 String sub = kid.getNameExt(); 181 if (kid.isFolder()) { 182 sub += '{' + expand(g, kid) + '}'; 183 } 184 subs.add(sub); 185 } 186 StringBuilder b = new StringBuilder (); 187 for (String sub : subs) { 188 if (b.length() > 0) { 189 b.append(' '); 190 } 191 b.append(sub); 192 } 193 return b.toString(); 194 } 195 196 } 197 | Popular Tags |