KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > docnavigation > JTreeSortNavigatorTest


1  /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

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 JavaDoc;
41 import javax.swing.tree.TreePath JavaDoc;
42 import java.io.File JavaDoc;
43 import java.util.HashSet JavaDoc;
44
45 public class JTreeSortNavigatorTest extends DrJavaTestCase {
46   
47   protected JTreeSortNavigator<DummyINavigatorItem> tree;
48   protected DefaultMutableTreeNode JavaDoc root;
49   protected DefaultMutableTreeNode JavaDoc source;
50   protected DefaultMutableTreeNode JavaDoc folder1;
51   protected DefaultMutableTreeNode JavaDoc folder2;
52   protected String JavaDoc projName;
53   DummyINavigatorItem i1, i2, i3, i4;
54   
55   public void setUp() throws Exception JavaDoc {
56     super.setUp();
57     
58     Utilities.invokeAndWait(new Runnable JavaDoc() {
59       public void run() {
60
61         try {
62           File JavaDoc 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 JavaDoc)tree.getModel().getRoot();
78           source = (DefaultMutableTreeNode JavaDoc)root.getChildAt(0);
79           folder1 = (DefaultMutableTreeNode JavaDoc)source.getChildAt(0);
80           folder2 = (DefaultMutableTreeNode JavaDoc)source.getChildAt(1);
81           
82           projName = root.toString();
83         }
84         catch(Exception JavaDoc 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 JavaDoc() {
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 JavaDoc tp = new TreePath JavaDoc(root.getPath());
112     assertEquals("Path String for Root", "./", tree.generatePathString(tp));
113     
114     tp = new TreePath JavaDoc(source.getPath());
115     assertEquals("Path String for source", "./[ Source Files ]/", tree.generatePathString(tp));
116     
117     tp = new TreePath JavaDoc(folder1.getPath());
118 // System.out.println(tree.generatePathString(tp));
119
assertEquals("Path String for folder1", "./[ Source Files ]/folder1/", tree.generatePathString(tp));
120     
121     tp = new TreePath JavaDoc(folder2.getPath());
122     assertEquals("Path String for folder2", "./[ Source Files ]/folder2/", tree.generatePathString(tp));
123   }
124   
125   public void testCollapsedPaths() {
126     HashSet JavaDoc<String JavaDoc> set = new HashSet JavaDoc<String JavaDoc>();
127     set.add("./[ Source Files ]/folder1/");
128     set.add("./[ Source Files ]/");
129     // folder2 & root should not be collapsed
130

131     TreePath JavaDoc tp1 = new TreePath JavaDoc(source.getPath());
132     TreePath JavaDoc tp2 = new TreePath JavaDoc(folder1.getPath());
133     TreePath JavaDoc tp3 = new TreePath JavaDoc(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 // System.out.println(((InnerNode)tp2.getLastPathComponent()).isCollapsed());
141
// System.out.println(tree.isCollapsed(tp3));
142
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 JavaDoc<String JavaDoc> tmp = new HashSet JavaDoc<String JavaDoc>();
149     for(String JavaDoc s : tree.getCollapsedPaths()) {
150       tmp.add(s);
151     }
152     assertEquals("Collapsed paths given should be collapsed paths received.", set, tmp);
153     
154   }
155   
156 // private String _name;
157
/**
158    * When the node is the only child of its parent and it is refreshed it should not
159    * try to delete the parent.
160    */

161   public void testRenameDocument() {
162     final String JavaDoc name = "MyTest.dj0";
163     final String JavaDoc newName = "MyTest.dj0*";
164     final DummyINavigatorItem item = new DummyINavigatorItem(name);
165     final DummyINavigatorItem newItem = new DummyINavigatorItem(newName);
166     Utilities.invokeAndWait(new Runnable JavaDoc() { 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 JavaDoc() { 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 JavaDoc() {
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 // tree.refreshDocument(newItem, "folder3")
183
assertEquals("should have been renamed", newName, newNode.toString());
184     assertEquals("node should have same parent", folder3, newNode.getParent());
185     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { tree.removeDocument(newItem); } });
186   }
187 }
Popular Tags