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.model.repl;
038:
039: import edu.rice.cs.drjava.DrJava;
040: import edu.rice.cs.drjava.DrJavaTestCase;
041: import edu.rice.cs.drjava.config.OptionConstants;
042: import edu.rice.cs.drjava.model.GlobalModelTestCase.OverwriteException;
043: import edu.rice.cs.drjava.model.GlobalModelTestCase.WarningFileSelector;
044: import edu.rice.cs.plt.io.IOUtil;
045: import edu.rice.cs.util.StringOps;
046: import edu.rice.cs.util.swing.Utilities;
047:
048: import java.io.File;
049: import java.io.IOException;
050:
051: /** Tests the functionality of the repl History.
052: * @version $Id: HistoryTest.java 4255 2007-08-28 19:17:37Z mgricken $
053: */
054: public final class HistoryTest extends DrJavaTestCase implements
055: OptionConstants {
056: private History _history;
057: private File _tempDir;
058:
059: /**
060: * Initialize fields for each test.
061: */
062: public void setUp() throws Exception {
063: super .setUp();
064: DrJava.getConfig().resetToDefaults();
065: String user = System.getProperty("user.name");
066: _tempDir = IOUtil.createAndMarkTempDirectory("DrJava-test-"
067: + user, "");
068: _history = new History();
069: }
070:
071: /**
072: * Cleans up temporary files and tries to free used variables after each test.
073: */
074: public void tearDown() throws Exception {
075: boolean ret = IOUtil.deleteRecursively(_tempDir);
076: assertTrue("delete temp directory " + _tempDir, ret);
077: _tempDir = null;
078: _history = null;
079: super .tearDown();
080: }
081:
082: /**
083: * Tests that the history doesn't overwrite files without prompting.
084: */
085: public void testSaveAsExistsForOverwrite() throws IOException {
086: _history.add("some text");
087: final File file1 = File.createTempFile("DrJava-test", ".hist",
088: _tempDir).getCanonicalFile();
089: file1.deleteOnExit();
090: try {
091: _history.writeToFile(new WarningFileSelector(file1));
092: fail("Did not ask to verify overwrite as expected");
093: } catch (OverwriteException e1) {
094: // good behavior for file saving...
095: }
096: }
097:
098: public void testMultipleInsert() {
099: _history.add("new Object()");
100: _history.add("new Object()");
101: assertEquals("Duplicate elements inserted", 2, _history.size());
102: }
103:
104: public void testCanMoveToEmptyAtEnd() {
105: String entry = "some text";
106: _history.add(entry);
107:
108: _history.movePrevious("");
109: assertEquals("Prev did not move to correct item", entry,
110: _history.getCurrent());
111:
112: _history.moveNext(entry);
113: assertEquals("Can't move to blank line at end", "", _history
114: .getCurrent());
115: }
116:
117: /**
118: * Ensures that Histories are bound to 500 entries.
119: */
120: public void testHistoryIsBounded() {
121: int maxLength = 500;
122: DrJava.getConfig().setSetting(HISTORY_MAX_SIZE,
123: new Integer(maxLength));
124:
125: int i;
126: for (i = 0; i < maxLength + 100; i++) {
127: _history.add("testing " + i);
128: }
129: for (; _history.hasPrevious(); i--) {
130: _history.movePrevious("testing " + i);
131: }
132:
133: assertEquals("History length is not bound to " + maxLength,
134: maxLength, _history.size());
135: assertEquals("History elements are not removed in FILO order",
136: "testing 100", _history.getCurrent());
137: }
138:
139: /** Tests that the history size can be updated, both through the config framework and the setMaxSize method.
140: */
141: public void testLiveUpdateOfHistoryMaxSize() {
142: int maxLength = 20;
143: DrJava.getConfig()
144: .setSetting(HISTORY_MAX_SIZE, new Integer(20));
145:
146: Utilities.clearEventQueue();
147:
148: for (int i = 0; i < maxLength; i++) {
149: _history.add("testing " + i);
150: }
151:
152: assertEquals("History size should be 20", 20, _history.size());
153:
154: DrJava.getConfig()
155: .setSetting(HISTORY_MAX_SIZE, new Integer(10));
156:
157: Utilities.clearEventQueue();
158: assertEquals("History size should be 10", 10, _history.size());
159: _history.setMaxSize(100);
160:
161: assertEquals("History size should still be 10", 10, _history
162: .size());
163:
164: _history.setMaxSize(0);
165: assertEquals("History size should be 0", 0, _history.size());
166:
167: DrJava.getConfig()
168: .setSetting(HISTORY_MAX_SIZE, new Integer(-1));
169:
170: Utilities.clearEventQueue();
171: assertEquals("History size should still be 0", 0, _history
172: .size());
173: }
174:
175: /**
176: * Tests the getHistoryAsString() method
177: */
178: public void testGetHistoryAsString() {
179: DrJava.getConfig()
180: .setSetting(HISTORY_MAX_SIZE, new Integer(20));
181:
182: Utilities.clearEventQueue();
183: assertEquals("testGetHistoryAsString:", "", _history
184: .getHistoryAsString());
185:
186: String newLine = StringOps.EOL;
187:
188: _history.add("some text");
189: assertEquals("testGetHistoryAsString:", "some text" + newLine,
190: _history.getHistoryAsString());
191:
192: _history.add("some more text");
193: _history.add("some text followed by a newline" + newLine);
194: String str = "some text" + newLine + "some more text" + newLine
195: + "some text followed by a newline" + newLine + newLine;
196: assertEquals("testGetHistoryAsString:", str, _history
197: .getHistoryAsString());
198: }
199:
200: /** Tests that the history remembers one edited entry for the given command. */
201: public void testRemembersOneEditedEntry() {
202: _history.add("some text");
203: _history.movePrevious("");
204:
205: String newEntry = "some different text";
206:
207: _history.moveNext(newEntry);
208: _history.movePrevious("");
209:
210: Utilities.clearEventQueue();
211: assertEquals("Did not remember the edited entry correctly.",
212: newEntry, _history.getCurrent());
213: }
214:
215: /** Tests that the history remembers multiple edited entries for the given command. */
216: public void testRemembersMultipleEditedEntries() {
217: _history.add("some text");
218: _history.add("some more text");
219: _history.movePrevious("");
220:
221: String newEntry1 = "some more different text";
222: String newEntry2 = "some different text";
223:
224: _history.movePrevious(newEntry1);
225: _history.moveNext(newEntry2);
226:
227: Utilities.clearEventQueue();
228: assertEquals("Did not remember the edited entry correctly.",
229: newEntry1, _history.getCurrent());
230:
231: _history.movePrevious(newEntry1);
232:
233: Utilities.clearEventQueue();
234: assertEquals("Did not remember the edited entry correctly.",
235: newEntry2, _history.getCurrent());
236: }
237:
238: /** Tests that the original copy of an edited entry remains in the history. */
239: public void testOriginalCopyRemains() {
240: String entry = "some text";
241: String newEntry = "some different text";
242:
243: _history.add(entry);
244: _history.movePrevious("");
245: _history.moveNext(newEntry);
246: _history.movePrevious("");
247: _history.add(newEntry);
248:
249: _history.movePrevious("");
250:
251: Utilities.clearEventQueue();
252: assertEquals("Did not add edited entry to end of history.",
253: newEntry, _history.getCurrent());
254:
255: _history.movePrevious(newEntry);
256:
257: Utilities.clearEventQueue();
258: assertEquals("Did not keep a copy of the original entry.",
259: entry, _history.getCurrent());
260: }
261:
262: /** Tests that the tab completion of the most recent entry is correct. */
263: public void testSearchHistory() {
264: String entry1 = "some text";
265: String entry2 = "blah";
266:
267: _history.add(entry1);
268: _history.add(entry2);
269:
270: _history.reverseSearch("s");
271:
272: Utilities.clearEventQueue();
273: assertEquals("Did not find the correct entry in history.",
274: entry1, _history.getCurrent());
275:
276: _history.forwardSearch("b");
277:
278: Utilities.clearEventQueue();
279: assertEquals("Did not find the correct entry in history.",
280: entry2, _history.getCurrent());
281: }
282:
283: /** Tests that if "tab completion" does not find a match, then cursor goes back to "end". */
284: public void testNoMatch() {
285: String entry1 = "some text";
286: String entry2 = "blah";
287:
288: _history.add(entry1);
289: _history.add(entry2);
290:
291: _history.reverseSearch("a");
292:
293: Utilities.clearEventQueue();
294: assertEquals("Did not reset cursor correctly.", "a", _history
295: .getCurrent());
296: }
297:
298: /** Tests reverse searching twice. */
299: public void testReverseSearchTwice() {
300: String entry1 = "same";
301: String entry2 = "some";
302:
303: _history.add(entry1);
304: _history.add(entry2);
305:
306: _history.reverseSearch("s");
307:
308: Utilities.clearEventQueue();
309: assertEquals("Did not reset cursor correctly.", entry2,
310: _history.getCurrent());
311:
312: _history.reverseSearch(_history.getCurrent());
313:
314: Utilities.clearEventQueue();
315: assertEquals("Did not reset cursor correctly.", entry1,
316: _history.getCurrent());
317: }
318: }
|