KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > RecentFileManagerTest


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-2005 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.drjava.ui;
35
36 import edu.rice.cs.drjava.DrJavaTestCase;
37 import edu.rice.cs.drjava.config.*;
38 import edu.rice.cs.plt.io.IOUtil;
39 import junit.framework.Test;
40 import junit.framework.TestSuite;
41
42 import javax.swing.*;
43 import java.io.File JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.util.Vector JavaDoc;
46
47 /** Test functions of RecentFileManager.
48  * @version $Id: RecentFileManagerTest.java 4075 2007-01-19 21:35:50Z dlsmith $
49  */

50 public final class RecentFileManagerTest extends DrJavaTestCase {
51
52   protected static final String JavaDoc FOO_TEXT = "class DrJavaTestFoo {}";
53   protected static final String JavaDoc BAR_TEXT = "class DrJavaTestBar {}";
54   private RecentFileManager _rfm;
55   private JMenu _menu;
56   protected File JavaDoc _tempDir;
57
58   /** Creates a test suite for JUnit to run.
59    * @return a test suite based on the methods in this class
60    */

61   public static Test suite() { return new TestSuite(RecentFileManagerTest.class); }
62
63   /** Setup method for each JUnit test case. */
64   public void setUp() throws Exception JavaDoc {
65     super.setUp();
66     _menu = new JMenu();
67     _rfm = new RecentFileManager(0, _menu, null, OptionConstants.RECENT_FILES);
68     String JavaDoc user = System.getProperty("user.name");
69     _tempDir = IOUtil.createAndMarkTempDirectory("DrJava-test-" + user, "");
70   }
71
72   public void tearDown() throws Exception JavaDoc {
73     _menu = null;
74     _rfm = null;
75     IOUtil.deleteRecursively(_tempDir);
76     _tempDir = null;
77     super.tearDown();
78   }
79
80   /** Create a new temporary file in _tempDir. Calls with the same int will return the same filename, while calls with
81    * different ints will return different filenames.
82    */

83   protected File JavaDoc tempFile() throws IOException JavaDoc {
84     return File.createTempFile("DrJava-test", ".java", _tempDir).getCanonicalFile();
85   }
86
87   /** Creates a new temporary file and writes the given text to it. The File object for the new file is returned. */
88   protected File JavaDoc writeToNewTempFile(String JavaDoc text) throws IOException JavaDoc {
89     File JavaDoc temp = tempFile();
90     IOUtil.writeStringToFile(temp, text);
91     return temp;
92   }
93
94   /** Tests that the size of the recent files list doesn't get bigger than the maximum size. */
95   public void testAddMoreThanMaxSize() throws IOException JavaDoc {
96
97     final File JavaDoc tempFile = writeToNewTempFile(BAR_TEXT);
98     final File JavaDoc tempFile2 = writeToNewTempFile(FOO_TEXT);
99     _rfm.updateMax(1);
100     _rfm.updateOpenFiles(tempFile);
101     _rfm.updateOpenFiles(tempFile2);
102     Vector JavaDoc<File JavaDoc> vector = _rfm.getFileVector();
103     assertEquals("number of recent files", 1, vector.size());
104     assertEquals("text of recent file", FOO_TEXT, IOUtil.toString(vector.get(0)));
105   }
106
107   /** Tests that the size of the recent files list is reduced in response to a decrease in max size. */
108   public void testShrinksToMaxSize() throws IOException JavaDoc {
109
110     final File JavaDoc tempFile = writeToNewTempFile(BAR_TEXT);
111     final File JavaDoc tempFile2 = writeToNewTempFile(FOO_TEXT);
112     _rfm.updateMax(2);
113
114     _rfm.updateOpenFiles(tempFile);
115     _rfm.updateOpenFiles(tempFile2);
116     Vector JavaDoc<File JavaDoc> vector = _rfm.getFileVector();
117     assertEquals("number of recent files", 2, vector.size());
118     assertEquals("text of most-recent file", FOO_TEXT, IOUtil.toString(vector.get(0)));
119     assertEquals("text of second-most recent file", BAR_TEXT, IOUtil.toString(vector.get(1)));
120     _rfm.updateMax(1);
121     _rfm.numberItems();
122     vector = _rfm.getFileVector();
123     assertEquals("number of recent files", 1, vector.size());
124     assertEquals("text of recent file", FOO_TEXT, IOUtil.toString(vector.get(0)));
125   }
126
127   /** Tests that files are removed correctly from the list. */
128   public void testRemoveFile() throws Exception JavaDoc {
129     // Open two files
130
final File JavaDoc tempFile = writeToNewTempFile(BAR_TEXT);
131     final File JavaDoc tempFile2 = writeToNewTempFile(FOO_TEXT);
132     _rfm.updateMax(2);
133     _rfm.updateOpenFiles(tempFile);
134     _rfm.updateOpenFiles(tempFile2);
135     Vector JavaDoc<File JavaDoc> vector = _rfm.getFileVector();
136     assertEquals("tempFile2 should be at top", vector.get(0), tempFile2);
137
138     // Remove top
139
_rfm.removeIfInList(tempFile2);
140     assertEquals("number of recent files", 1, vector.size());
141     assertEquals("tempFile should be at top", vector.get(0), tempFile);
142
143     // Remove non-existant entry
144
_rfm.removeIfInList(tempFile2);
145     assertEquals("number of recent files", 1, vector.size());
146     assertEquals("tempFile should still be at top", vector.get(0), tempFile);
147
148     // Remove top again
149
_rfm.removeIfInList(tempFile);
150     assertEquals("number of recent files", 0, vector.size());
151   }
152
153   /** Tests that the list is re-ordered correctly after a file is re-opened, even if it has a different path. */
154   public void testReopenFiles() throws Exception JavaDoc {
155     final File JavaDoc tempFile = writeToNewTempFile(BAR_TEXT);
156     final File JavaDoc tempFile2 = writeToNewTempFile(FOO_TEXT);
157
158     _rfm.updateMax(2);
159     _rfm.updateOpenFiles(tempFile2);
160     _rfm.updateOpenFiles(tempFile);
161     Vector JavaDoc<File JavaDoc> vector = _rfm.getFileVector();
162
163     assertEquals("tempFile should be at top", vector.get(0), tempFile);
164
165     // Re-open tempFile2
166
_rfm.updateOpenFiles(tempFile2);
167     vector = _rfm.getFileVector();
168     assertEquals("tempFile2 should be at top", vector.get(0), tempFile2);
169
170     // Re-open tempFile with a different path, e.g. /tmp/MyFile -> /tmp/./MyFile
171
File JavaDoc parent = tempFile.getParentFile();
172     String JavaDoc dotSlash = "." + System.getProperty("file.separator");
173     parent = new File JavaDoc(parent, dotSlash);
174     File JavaDoc sameFile = new File JavaDoc(parent, tempFile.getName());
175
176     _rfm.updateOpenFiles(sameFile);
177     vector = _rfm.getFileVector();
178     assertEquals("sameFile should be at top", vector.get(0), sameFile);
179     assertEquals("should only have two files", 2, vector.size());
180     assertTrue("should not contain tempFile", !(vector.contains(tempFile)));
181   }
182
183   /** Verifies that the presentation names for the directory filter are correct. */
184   public void testDirectoryFilterDescription() {
185     DirectoryFilter f = new DirectoryFilter();
186     assertEquals("Should have the correct description.", "Directories", f.getDescription());
187     f = new DirectoryFilter("Other directories");
188     assertEquals("Should have allowed an alternate description.", "Other directories", f.getDescription());
189   }
190 }
191
Popular Tags