001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.ui;
038:
039: import edu.rice.cs.drjava.DrJavaTestCase;
040: import edu.rice.cs.drjava.config.*;
041: import edu.rice.cs.plt.io.IOUtil;
042: import junit.framework.Test;
043: import junit.framework.TestSuite;
044:
045: import javax.swing.*;
046: import java.io.File;
047: import java.io.IOException;
048: import java.util.Vector;
049:
050: /** Test functions of RecentFileManager.
051: * @version $Id: RecentFileManagerTest.java 4255 2007-08-28 19:17:37Z mgricken $
052: */
053: public final class RecentFileManagerTest extends DrJavaTestCase {
054:
055: protected static final String FOO_TEXT = "class DrJavaTestFoo {}";
056: protected static final String BAR_TEXT = "class DrJavaTestBar {}";
057: private RecentFileManager _rfm;
058: private JMenu _menu;
059: protected File _tempDir;
060:
061: /** Creates a test suite for JUnit to run.
062: * @return a test suite based on the methods in this class
063: */
064: public static Test suite() {
065: return new TestSuite(RecentFileManagerTest.class);
066: }
067:
068: /** Setup method for each JUnit test case. */
069: public void setUp() throws Exception {
070: super .setUp();
071: _menu = new JMenu();
072: _rfm = new RecentFileManager(0, _menu, null,
073: OptionConstants.RECENT_FILES);
074: String user = System.getProperty("user.name");
075: _tempDir = IOUtil.createAndMarkTempDirectory("DrJava-test-"
076: + user, "");
077: }
078:
079: public void tearDown() throws Exception {
080: _menu = null;
081: _rfm = null;
082: IOUtil.deleteRecursively(_tempDir);
083: _tempDir = null;
084: super .tearDown();
085: }
086:
087: /** Create a new temporary file in _tempDir. Calls with the same int will return the same filename, while calls with
088: * different ints will return different filenames.
089: */
090: protected File tempFile() throws IOException {
091: return File.createTempFile("DrJava-test", ".java", _tempDir)
092: .getCanonicalFile();
093: }
094:
095: /** Creates a new temporary file and writes the given text to it. The File object for the new file is returned. */
096: protected File writeToNewTempFile(String text) throws IOException {
097: File temp = tempFile();
098: IOUtil.writeStringToFile(temp, text);
099: return temp;
100: }
101:
102: /** Tests that the size of the recent files list doesn't get bigger than the maximum size. */
103: public void testAddMoreThanMaxSize() throws IOException {
104:
105: final File tempFile = writeToNewTempFile(BAR_TEXT);
106: final File tempFile2 = writeToNewTempFile(FOO_TEXT);
107: _rfm.updateMax(1);
108: _rfm.updateOpenFiles(tempFile);
109: _rfm.updateOpenFiles(tempFile2);
110: Vector<File> vector = _rfm.getFileVector();
111: assertEquals("number of recent files", 1, vector.size());
112: assertEquals("text of recent file", FOO_TEXT, IOUtil
113: .toString(vector.get(0)));
114: }
115:
116: /** Tests that the size of the recent files list is reduced in response to a decrease in max size. */
117: public void testShrinksToMaxSize() throws IOException {
118:
119: final File tempFile = writeToNewTempFile(BAR_TEXT);
120: final File tempFile2 = writeToNewTempFile(FOO_TEXT);
121: _rfm.updateMax(2);
122:
123: _rfm.updateOpenFiles(tempFile);
124: _rfm.updateOpenFiles(tempFile2);
125: Vector<File> vector = _rfm.getFileVector();
126: assertEquals("number of recent files", 2, vector.size());
127: assertEquals("text of most-recent file", FOO_TEXT, IOUtil
128: .toString(vector.get(0)));
129: assertEquals("text of second-most recent file", BAR_TEXT,
130: IOUtil.toString(vector.get(1)));
131: _rfm.updateMax(1);
132: _rfm.numberItems();
133: vector = _rfm.getFileVector();
134: assertEquals("number of recent files", 1, vector.size());
135: assertEquals("text of recent file", FOO_TEXT, IOUtil
136: .toString(vector.get(0)));
137: }
138:
139: /** Tests that files are removed correctly from the list. */
140: public void testRemoveFile() throws Exception {
141: // Open two files
142: final File tempFile = writeToNewTempFile(BAR_TEXT);
143: final File tempFile2 = writeToNewTempFile(FOO_TEXT);
144: _rfm.updateMax(2);
145: _rfm.updateOpenFiles(tempFile);
146: _rfm.updateOpenFiles(tempFile2);
147: Vector<File> vector = _rfm.getFileVector();
148: assertEquals("tempFile2 should be at top", vector.get(0),
149: tempFile2);
150:
151: // Remove top
152: _rfm.removeIfInList(tempFile2);
153: assertEquals("number of recent files", 1, vector.size());
154: assertEquals("tempFile should be at top", vector.get(0),
155: tempFile);
156:
157: // Remove non-existant entry
158: _rfm.removeIfInList(tempFile2);
159: assertEquals("number of recent files", 1, vector.size());
160: assertEquals("tempFile should still be at top", vector.get(0),
161: tempFile);
162:
163: // Remove top again
164: _rfm.removeIfInList(tempFile);
165: assertEquals("number of recent files", 0, vector.size());
166: }
167:
168: /** Tests that the list is re-ordered correctly after a file is re-opened, even if it has a different path. */
169: public void testReopenFiles() throws Exception {
170: final File tempFile = writeToNewTempFile(BAR_TEXT);
171: final File tempFile2 = writeToNewTempFile(FOO_TEXT);
172:
173: _rfm.updateMax(2);
174: _rfm.updateOpenFiles(tempFile2);
175: _rfm.updateOpenFiles(tempFile);
176: Vector<File> vector = _rfm.getFileVector();
177:
178: assertEquals("tempFile should be at top", vector.get(0),
179: tempFile);
180:
181: // Re-open tempFile2
182: _rfm.updateOpenFiles(tempFile2);
183: vector = _rfm.getFileVector();
184: assertEquals("tempFile2 should be at top", vector.get(0),
185: tempFile2);
186:
187: // Re-open tempFile with a different path, e.g. /tmp/MyFile -> /tmp/./MyFile
188: File parent = tempFile.getParentFile();
189: String dotSlash = "." + System.getProperty("file.separator");
190: parent = new File(parent, dotSlash);
191: File sameFile = new File(parent, tempFile.getName());
192:
193: _rfm.updateOpenFiles(sameFile);
194: vector = _rfm.getFileVector();
195: assertEquals("sameFile should be at top", vector.get(0),
196: sameFile);
197: assertEquals("should only have two files", 2, vector.size());
198: assertTrue("should not contain tempFile", !(vector
199: .contains(tempFile)));
200: }
201:
202: /** Verifies that the presentation names for the directory filter are correct. */
203: public void testDirectoryFilterDescription() {
204: DirectoryFilter f = new DirectoryFilter();
205: assertEquals("Should have the correct description.",
206: "Directories", f.getDescription());
207: f = new DirectoryFilter("Other directories");
208: assertEquals("Should have allowed an alternate description.",
209: "Other directories", f.getDescription());
210: }
211: }
|