1 19 20 package org.netbeans.modules.ant.freeform.ui; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.io.File ; 24 import java.util.HashSet ; 25 import java.util.Set ; 26 import java.util.SortedSet ; 27 import java.util.TreeSet ; 28 import org.netbeans.api.project.Project; 29 import org.netbeans.api.project.ProjectManager; 30 import org.netbeans.junit.NbTestCase; 31 import org.netbeans.modules.ant.freeform.FreeformProjectGenerator; 32 import org.netbeans.modules.ant.freeform.FreeformProjectType; 33 import org.netbeans.modules.ant.freeform.TestBase; 34 import org.netbeans.modules.ant.freeform.spi.support.Util; 35 import org.netbeans.spi.project.support.ant.AntProjectHelper; 36 import org.netbeans.spi.project.support.ant.EditableProperties; 37 import org.netbeans.spi.project.ui.LogicalViewProvider; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.loaders.DataObject; 41 import org.openide.modules.ModuleInfo; 42 import org.openide.nodes.Children; 43 import org.openide.nodes.Node; 44 import org.openide.nodes.NodeEvent; 45 import org.openide.nodes.NodeListener; 46 import org.openide.nodes.NodeMemberEvent; 47 import org.openide.nodes.NodeReorderEvent; 48 import org.w3c.dom.Document ; 49 import org.openide.util.Lookup; 50 import org.w3c.dom.Element ; 51 import org.w3c.dom.NodeList ; 52 53 55 59 public class ViewTest extends TestBase { 60 61 public ViewTest(String name) { 62 super(name); 63 } 64 65 private LogicalViewProvider lvp; 66 67 protected void setUp() throws Exception { 68 super.setUp(); 69 ModuleInfo info = Lookup.getDefault().lookup(ModuleInfo.class); 70 lvp = extsrcroot.getLookup().lookup(LogicalViewProvider.class); 71 assertNotNull("found a LogicalViewProvider", lvp); 72 } 73 74 public void testViewItemBasic() throws Exception { 75 Node root = lvp.createLogicalView(); 76 assertEquals("lookup has project", extsrcroot, root.getLookup().lookup(Project.class)); 77 Children ch = root.getChildren(); 78 Node[] kids = ch.getNodes(true); 79 assertEquals("two child nodes", 2, kids.length); 80 assertEquals("correct code name #2", "nbproject/project.xml", kids[1].getName()); 82 assertEquals("correct display name #2", "project.xml", kids[1].getDisplayName()); 83 assertEquals("correct cookie #2", 84 DataObject.find(egdirFO.getFileObject("extsrcroot/proj/nbproject/project.xml")), 85 kids[1].getLookup().lookup(DataObject.class)); 86 } 87 88 public void testViewItemChanges() throws Exception { 89 Node root = lvp.createLogicalView(); 90 Children ch = root.getChildren(); 91 Node[] kids = ch.getNodes(true); 92 assertEquals("two child nodes", 2, kids.length); 93 assertEquals("correct code name #1", "../src", kids[0].getName()); 94 assertEquals("correct code name #2", "nbproject/project.xml", kids[1].getName()); 95 TestNL l = new TestNL(); 96 root.addNodeListener(l); 97 Element data = extsrcroot.getPrimaryConfigurationData(); 98 Element view = Util.findElement(data, "view", FreeformProjectType.NS_GENERAL); 99 assertNotNull("have <view>", view); 100 Element items = Util.findElement(view, "items", FreeformProjectType.NS_GENERAL); 101 assertNotNull("have <items>", items); 102 Element sourceFolder = Util.findElement(items, "source-folder", FreeformProjectType.NS_GENERAL); 103 assertNotNull("have <source-folder>", sourceFolder); 104 Element location = Util.findElement(sourceFolder, "location", FreeformProjectType.NS_GENERAL); 105 assertNotNull("have <location>", location); 106 NodeList nl = location.getChildNodes(); 107 assertEquals("one child", 1, nl.getLength()); 108 location.removeChild(nl.item(0)); 109 location.appendChild(location.getOwnerDocument().createTextNode("../src2")); 110 Element sourceFile = Util.findElement(items, "source-file", FreeformProjectType.NS_GENERAL); 111 assertNotNull("have <source-file>", sourceFile); 112 items.removeChild(sourceFile); 113 extsrcroot.putPrimaryConfigurationData(data); 114 Thread.sleep(500); 116 assertFalse("got some changes in children", l.probeChanges().isEmpty()); 117 kids = ch.getNodes(true); 118 assertEquals("one child node", 1, kids.length); 119 assertEquals("correct code name #1", "../src2", kids[0].getName()); 120 assertEquals("correct display name #1", "External Sources", kids[0].getDisplayName()); 121 assertEquals("correct cookie #1", 122 DataObject.find(egdirFO.getFileObject("extsrcroot/src2")), 123 kids[0].getLookup().lookup(DataObject.class)); 124 } 125 126 public void testFindPath() throws Exception { 127 LogicalViewProvider lvp2 = simple.getLookup().lookup(LogicalViewProvider.class); 129 assertNotNull(lvp2); 130 Node root = lvp2.createLogicalView(); 131 doTestFindPathPositive(lvp2, root, simple, "xdocs/foo.xml"); 132 doTestFindPathPositive(lvp2, root, simple, "xdocs"); 133 doTestFindPathPositive(lvp2, root, simple, "build.properties"); 134 doTestFindPathPositive(lvp2, root, simple, "build.xml"); 135 doTestFindPathNegative(lvp2, root, simple, "nbproject/project.xml"); 136 doTestFindPathNegative(lvp2, root, simple, "nbproject"); 137 } 138 139 public static void doTestFindPathPositive(LogicalViewProvider lvp, Node root, Project project, String path) throws Exception { 140 FileObject file = project.getProjectDirectory().getFileObject(path); 141 assertNotNull("found " + path, file); 142 DataObject d = DataObject.find(file); 143 Node nDO = lvp.findPath(root, d); 144 Node nFO = lvp.findPath(root, file); 145 assertNotNull("found node for " + path, nDO); 146 assertNotNull("found node for " + path, nFO); 147 assertEquals("correct node", d, nDO.getLookup().lookup(DataObject.class)); 148 assertEquals("correct node", d, nFO.getLookup().lookup(DataObject.class)); 150 } 151 152 public static void doTestFindPathNegative(LogicalViewProvider lvp, Node root, Project project, String path) throws Exception { 153 FileObject file = project.getProjectDirectory().getFileObject(path); 154 assertNotNull("found " + path, file); 155 DataObject d = DataObject.find(file); 156 Node n = lvp.findPath(root, d); 157 assertNull("did not find node for " + path, n); 158 } 159 160 public static void doTestIncludesExcludes(NbTestCase test, String style, String appearanceEverything, String appearanceIncludesExcludes, String appearanceExcludes, String appearanceFloating) throws Exception { 161 FolderNodeFactory.synchronous = true; 162 test.clearWorkDir(); 163 File d = test.getWorkDir(); 164 AntProjectHelper helper = FreeformProjectGenerator.createProject(d, d, "prj", null); 165 Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory()); 166 FileUtil.createData(new File (d, "s/relevant/included/file")); 167 FileUtil.createData(new File (d, "s/relevant/excluded/file")); 168 FileUtil.createData(new File (d, "s/ignored/file")); 169 Element data = Util.getPrimaryConfigurationData(helper); 170 Document doc = data.getOwnerDocument(); 171 Element items = (Element ) data.getElementsByTagName("items").item(0); 172 items.removeChild(items.getElementsByTagName("source-file").item(0)); Element sf = (Element ) items.appendChild(doc.createElementNS(Util.NAMESPACE, "source-folder")); 174 sf.setAttribute("style", style); 175 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "location")).appendChild(doc.createTextNode("s")); 176 Util.putPrimaryConfigurationData(helper, data); 177 ProjectManager.getDefault().saveProject(p); 178 Node r = p.getLookup().lookup(LogicalViewProvider.class).createLogicalView(); 179 assertEquals(appearanceEverything, expand(r)); 180 EditableProperties ep = new EditableProperties(); 182 ep.put("includes", "relevant/"); 183 ep.put("excludes", "**/excluded/"); 184 helper.putProperties("config.properties", ep); 185 data = Util.getPrimaryConfigurationData(helper); 186 doc = data.getOwnerDocument(); 187 data.getElementsByTagName("properties").item(0). 188 appendChild(doc.createElementNS(Util.NAMESPACE, "property-file")). 189 appendChild(doc.createTextNode("config.properties")); 190 Util.putPrimaryConfigurationData(helper, data); 191 ProjectManager.getDefault().saveProject(p); 192 data = Util.getPrimaryConfigurationData(helper); 193 doc = data.getOwnerDocument(); 194 sf = (Element ) data.getElementsByTagName("source-folder").item(0); 195 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "includes")). 196 appendChild(doc.createTextNode("${includes}")); 197 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "excludes")). 198 appendChild(doc.createTextNode("${excludes}")); 199 Util.putPrimaryConfigurationData(helper, data); 200 ProjectManager.getDefault().saveProject(p); 201 assertEquals(appearanceIncludesExcludes, expand(r)); 202 ep = helper.getProperties("config.properties"); 204 ep.remove("includes"); 205 helper.putProperties("config.properties", ep); 206 ProjectManager.getDefault().saveProject(p); 207 assertEquals(appearanceExcludes, expand(r)); 208 ep = helper.getProperties("config.properties"); 210 ep.put("includes", "relevant/included/"); 211 helper.putProperties("config.properties", ep); 212 ProjectManager.getDefault().saveProject(p); 213 assertEquals(appearanceFloating, expand(r)); 214 } 215 public void testIncludesExcludes() throws Exception { 216 doTestIncludesExcludes(this, "tree", 217 "prj{s{ignored{file} relevant{excluded{file} included{file}}}}", 218 "prj{s{relevant{included{file}}}}", 219 "prj{s{ignored{file} relevant{included{file}}}}", 220 "prj{s{relevant{included{file}}}}"); 221 } 222 private static String expand(Node n) { 223 Node[] kids = n.getChildren().getNodes(true); 224 String nm = n.getDisplayName(); 225 if (kids.length == 0) { 226 return nm; 227 } else { 228 SortedSet <String > under = new TreeSet <String >(); 229 for (Node kid : kids) { 230 under.add(expand(kid)); 231 } 232 StringBuilder b = new StringBuilder (nm).append('{'); 233 boolean first = true; 234 for (String s : under) { 235 if (first) { 236 first = false; 237 } else { 238 b.append(' '); 239 } 240 b.append(s); 241 } 242 return b.append('}').toString(); 243 } 244 } 245 246 private static final class TestNL implements NodeListener { 247 private final Set <String > changes = new HashSet <String >(); 248 public TestNL() {} 249 public synchronized void childrenRemoved(NodeMemberEvent ev) { 250 changes.add("childrenRemoved"); 251 } 252 public synchronized void childrenAdded(NodeMemberEvent ev) { 253 changes.add("childrenAdded"); 254 } 255 public synchronized void childrenReordered(NodeReorderEvent ev) { 256 changes.add("childrenReordered"); 257 } 258 public synchronized void nodeDestroyed(NodeEvent ev) { 259 changes.add("nodeDestroyed"); 260 } 261 public synchronized void propertyChange(PropertyChangeEvent propertyChangeEvent) { 262 changes.add(propertyChangeEvent.getPropertyName()); 263 } 264 265 public synchronized Set <String > probeChanges() { 266 Set <String > _changes = new HashSet <String >(changes); 267 changes.clear(); 268 return _changes; 269 } 270 } 271 272 } 273 | Popular Tags |