1 33 34 package edu.rice.cs.util.docnavigation; 35 36 import edu.rice.cs.util.UnexpectedException; 37 import edu.rice.cs.util.swing.Utilities; 38 import edu.rice.cs.drjava.DrJavaTestCase; 39 40 import javax.swing.tree.DefaultMutableTreeNode ; 41 import javax.swing.tree.TreePath ; 42 import java.io.File ; 43 import java.util.HashSet ; 44 45 public class JTreeSortNavigatorTest extends DrJavaTestCase { 46 47 protected JTreeSortNavigator<DummyINavigatorItem> tree; 48 protected DefaultMutableTreeNode root; 49 protected DefaultMutableTreeNode source; 50 protected DefaultMutableTreeNode folder1; 51 protected DefaultMutableTreeNode folder2; 52 protected String projName; 53 DummyINavigatorItem i1, i2, i3, i4; 54 55 public void setUp() throws Exception { 56 super.setUp(); 57 58 Utilities.invokeAndWait(new Runnable () { 59 public void run() { 60 61 try { 62 File f = File.createTempFile("project-",".pjt").getCanonicalFile(); 63 tree = new JTreeSortNavigator<DummyINavigatorItem>(f.getCanonicalPath()); 64 65 tree.addTopLevelGroup("[ Source Files ]", new INavigatorItemFilter<INavigatorItem>(){ 66 public boolean accept(INavigatorItem n) { return true; } 67 }); 68 i1 = new DummyINavigatorItem("item1"); 69 i2 = new DummyINavigatorItem("item2"); 70 i3 = new DummyINavigatorItem("item1"); 71 i4 = new DummyINavigatorItem("item2"); 72 tree.addDocument(i1, "folder1"); 73 tree.addDocument(i2, "folder1"); 74 tree.addDocument(i3, "folder2"); 75 tree.addDocument(i4, "folder2"); 76 77 root = (DefaultMutableTreeNode )tree.getModel().getRoot(); 78 source = (DefaultMutableTreeNode )root.getChildAt(0); 79 folder1 = (DefaultMutableTreeNode )source.getChildAt(0); 80 folder2 = (DefaultMutableTreeNode )source.getChildAt(1); 81 82 projName = root.toString(); 83 } 84 catch(Exception e) { throw new UnexpectedException(e); } 85 } 86 }); 87 } 88 89 public void testTraversalOps() { 90 assertEquals("doc count test", 4, tree.getDocumentCount()); 91 assertSame("getFirst test", i1, tree.getFirst()); 92 assertSame("getLast test", i4, tree.getLast()); 93 94 Utilities.invokeAndWait(new Runnable () { 95 public void run() { 96 tree.setNextChangeModelInitiated(true); 97 tree.setActiveDoc(i1); 98 } 99 }); 100 assertSame("getCurrent test", i1, tree.getCurrent()); 101 assertSame("getNext test 1", i2, tree.getNext(i1)); 102 assertSame("getNext test 2", i3, tree.getNext(i2)); 103 assertSame("getNext test 3", i4, tree.getNext(i3)); 104 105 assertSame("getPrevious test 1", i3, tree.getPrevious(i4)); 106 assertSame("getPrevious test 2", i2, tree.getPrevious(i3)); 107 assertSame("getPrevious test 3", i1, tree.getPrevious(i2)); 108 } 109 110 public void testGeneratePathString() { 111 TreePath tp = new TreePath (root.getPath()); 112 assertEquals("Path String for Root", "./", tree.generatePathString(tp)); 113 114 tp = new TreePath (source.getPath()); 115 assertEquals("Path String for source", "./[ Source Files ]/", tree.generatePathString(tp)); 116 117 tp = new TreePath (folder1.getPath()); 118 assertEquals("Path String for folder1", "./[ Source Files ]/folder1/", tree.generatePathString(tp)); 120 121 tp = new TreePath (folder2.getPath()); 122 assertEquals("Path String for folder2", "./[ Source Files ]/folder2/", tree.generatePathString(tp)); 123 } 124 125 public void testCollapsedPaths() { 126 HashSet <String > set = new HashSet <String >(); 127 set.add("./[ Source Files ]/folder1/"); 128 set.add("./[ Source Files ]/"); 129 131 TreePath tp1 = new TreePath (source.getPath()); 132 TreePath tp2 = new TreePath (folder1.getPath()); 133 TreePath tp3 = new TreePath (folder2.getPath()); 134 135 tree.collapsePaths(set); 136 assertTrue("Source should be collapsed.", tree.isCollapsed(tp1)); 137 assertTrue("Source InnerNode should say it is collapsed.", 138 ((InnerNode<?, ?>)tp1.getLastPathComponent()).isCollapsed()); 139 assertTrue("Folder1 should be collapsed.", tree.isCollapsed(tp2)); 140 assertTrue("folder1 InnerNode should say it is collapsed.", 143 ((InnerNode<?, ?>)tp2.getLastPathComponent()).isCollapsed()); 144 assertTrue("Tree should say Folder2 is collapsed.", tree.isCollapsed(tp3)); 145 assertFalse("folder2 InnerNode should not say it is collapsed.", 146 ((InnerNode<?, ?>)tp3.getLastPathComponent()).isCollapsed()); 147 148 HashSet <String > tmp = new HashSet <String >(); 149 for(String s : tree.getCollapsedPaths()) { 150 tmp.add(s); 151 } 152 assertEquals("Collapsed paths given should be collapsed paths received.", set, tmp); 153 154 } 155 156 161 public void testRenameDocument() { 162 final String name = "MyTest.dj0"; 163 final String newName = "MyTest.dj0*"; 164 final DummyINavigatorItem item = new DummyINavigatorItem(name); 165 final DummyINavigatorItem newItem = new DummyINavigatorItem(newName); 166 Utilities.invokeAndWait(new Runnable () { public void run() { tree.addDocument(item, "folder3"); } }); 167 InnerNode folder3 = (InnerNode)source.getChildAt(2); 168 assertEquals("folder3 should have 1 children", 1, folder3.getChildCount()); 169 Utilities.invokeAndWait(new Runnable () { public void run() { tree.refreshDocument(item, "folder3"); } }); 170 assertEquals("folder3 should have 1 children", 1, folder3.getChildCount()); 171 LeafNode<?> node = (LeafNode<?>)folder3.getChildAt(0); 172 assertEquals("node should have correct name", name, node.toString()); 173 Utilities.invokeAndWait(new Runnable () { 174 public void run() { 175 tree.removeDocument(item); 176 tree.addDocument(newItem, "folder3"); 177 } 178 }); 179 folder3 = (InnerNode)source.getChildAt(2); 180 LeafNode<?> newNode = (LeafNode<?>)folder3.getChildAt(0); 181 182 assertEquals("should have been renamed", newName, newNode.toString()); 184 assertEquals("node should have same parent", folder3, newNode.getParent()); 185 Utilities.invokeAndWait(new Runnable () { public void run() { tree.removeDocument(newItem); } }); 186 } 187 } | Popular Tags |