KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > repl > HistoryTest


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.model.repl;
35
36 import edu.rice.cs.drjava.DrJava;
37 import edu.rice.cs.drjava.DrJavaTestCase;
38 import edu.rice.cs.drjava.config.OptionConstants;
39 import edu.rice.cs.drjava.model.GlobalModelTestCase.OverwriteException;
40 import edu.rice.cs.drjava.model.GlobalModelTestCase.WarningFileSelector;
41 import edu.rice.cs.plt.io.IOUtil;
42 import edu.rice.cs.util.swing.Utilities;
43
44 import java.io.File JavaDoc;
45 import java.io.IOException JavaDoc;
46
47 /** Tests the functionality of the repl History.
48  * @version $Id: HistoryTest.java 4075 2007-01-19 21:35:50Z dlsmith $
49  */

50 public final class HistoryTest extends DrJavaTestCase implements OptionConstants{
51   private History _history;
52   private File JavaDoc _tempDir;
53
54   /**
55    * Initialize fields for each test.
56    */

57   public void setUp() throws Exception JavaDoc {
58     super.setUp();
59     DrJava.getConfig().resetToDefaults();
60     String JavaDoc user = System.getProperty("user.name");
61     _tempDir = IOUtil.createAndMarkTempDirectory("DrJava-test-" + user, "");
62     _history = new History();
63   }
64
65   /**
66    * Cleans up temporary files and tries to free used variables after each test.
67    */

68   public void tearDown() throws Exception JavaDoc {
69     boolean ret = IOUtil.deleteRecursively(_tempDir);
70     assertTrue("delete temp directory " + _tempDir, ret);
71     _tempDir = null;
72     _history = null;
73     super.tearDown();
74   }
75
76   /**
77    * Tests that the history doesn't overwrite files without prompting.
78    */

79   public void testSaveAsExistsForOverwrite() throws IOException JavaDoc {
80     _history.add("some text");
81     final File JavaDoc file1 = File.createTempFile("DrJava-test", ".hist", _tempDir).getCanonicalFile();
82     file1.deleteOnExit();
83     try {
84       _history.writeToFile(new WarningFileSelector(file1));
85       fail("Did not ask to verify overwrite as expected");
86     }
87     catch (OverwriteException e1) {
88       // good behavior for file saving...
89
}
90   }
91
92   public void testMultipleInsert() {
93     _history.add("new Object()");
94     _history.add("new Object()");
95     assertEquals("Duplicate elements inserted", 2, _history.size());
96   }
97
98   public void testCanMoveToEmptyAtEnd() {
99     String JavaDoc entry = "some text";
100     _history.add(entry);
101
102     _history.movePrevious("");
103     assertEquals("Prev did not move to correct item",
104                  entry,
105                  _history.getCurrent());
106
107     _history.moveNext(entry);
108     assertEquals("Can't move to blank line at end",
109                  "",
110                  _history.getCurrent());
111   }
112
113   /**
114    * Ensures that Histories are bound to 500 entries.
115    */

116   public void testHistoryIsBounded() {
117     int maxLength = 500;
118     DrJava.getConfig().setSetting(HISTORY_MAX_SIZE, new Integer JavaDoc(maxLength));
119
120     int i;
121     for (i = 0; i < maxLength + 100; i++) {
122       _history.add("testing " + i);
123     }
124     for (; _history.hasPrevious(); i--) {
125       _history.movePrevious("testing " + i);
126     }
127
128     assertEquals("History length is not bound to " + maxLength, maxLength, _history.size());
129     assertEquals("History elements are not removed in FILO order", "testing 100", _history.getCurrent());
130   }
131
132   /** Tests that the history size can be updated, both through the config framework and the setMaxSize method.
133    */

134   public void testLiveUpdateOfHistoryMaxSize() {
135     int maxLength = 20;
136     DrJava.getConfig().setSetting(HISTORY_MAX_SIZE, new Integer JavaDoc(20));
137     
138     Utilities.clearEventQueue();
139
140     for (int i = 0; i < maxLength; i++) {
141       _history.add("testing " + i);
142     }
143
144     assertEquals("History size should be 20", 20, _history.size());
145
146     DrJava.getConfig().setSetting(HISTORY_MAX_SIZE, new Integer JavaDoc(10));
147    
148     Utilities.clearEventQueue();
149     assertEquals("History size should be 10", 10, _history.size());
150     _history.setMaxSize(100);
151
152     assertEquals("History size should still be 10", 10, _history.size());
153
154     _history.setMaxSize(0);
155     assertEquals("History size should be 0", 0, _history.size());
156
157     DrJava.getConfig().setSetting(HISTORY_MAX_SIZE, new Integer JavaDoc(-1));
158
159     Utilities.clearEventQueue();
160     assertEquals("History size should still be 0", 0, _history.size());
161   }
162
163   /**
164    * Tests the getHistoryAsString() method
165    */

166   public void testGetHistoryAsString() {
167     DrJava.getConfig().setSetting(HISTORY_MAX_SIZE, new Integer JavaDoc(20));
168
169     Utilities.clearEventQueue();
170     assertEquals("testGetHistoryAsString:", "", _history.getHistoryAsString());
171
172     String JavaDoc newLine = System.getProperty("line.separator");
173
174     _history.add("some text");
175     assertEquals("testGetHistoryAsString:", "some text" + newLine, _history.getHistoryAsString());
176
177     _history.add("some more text");
178     _history.add("some text followed by a newline" + newLine);
179     String JavaDoc str =
180       "some text" + newLine + "some more text" + newLine + "some text followed by a newline" + newLine + newLine;
181     assertEquals("testGetHistoryAsString:", str, _history.getHistoryAsString());
182   }
183
184   /** Tests that the history remembers one edited entry for the given command. */
185   public void testRemembersOneEditedEntry() {
186     _history.add("some text");
187     _history.movePrevious("");
188
189     String JavaDoc newEntry = "some different text";
190
191     _history.moveNext(newEntry);
192     _history.movePrevious("");
193
194     Utilities.clearEventQueue();
195     assertEquals("Did not remember the edited entry correctly.", newEntry, _history.getCurrent());
196   }
197
198   /** Tests that the history remembers multiple edited entries for the given command. */
199   public void testRemembersMultipleEditedEntries() {
200     _history.add("some text");
201     _history.add("some more text");
202     _history.movePrevious("");
203
204     String JavaDoc newEntry1 = "some more different text";
205     String JavaDoc newEntry2 = "some different text";
206
207     _history.movePrevious(newEntry1);
208     _history.moveNext(newEntry2);
209
210     Utilities.clearEventQueue();
211     assertEquals("Did not remember the edited entry correctly.", newEntry1, _history.getCurrent());
212
213     _history.movePrevious(newEntry1);
214     
215     Utilities.clearEventQueue();
216     assertEquals("Did not remember the edited entry correctly.", newEntry2, _history.getCurrent());
217   }
218
219   /** Tests that the original copy of an edited entry remains in the history. */
220   public void testOriginalCopyRemains() {
221     String JavaDoc entry = "some text";
222     String JavaDoc newEntry = "some different text";
223
224     _history.add(entry);
225     _history.movePrevious("");
226     _history.moveNext(newEntry);
227     _history.movePrevious("");
228     _history.add(newEntry);
229
230     _history.movePrevious("");
231     
232     Utilities.clearEventQueue();
233     assertEquals("Did not add edited entry to end of history.", newEntry, _history.getCurrent());
234
235     _history.movePrevious(newEntry);
236     
237     Utilities.clearEventQueue();
238     assertEquals("Did not keep a copy of the original entry.", entry, _history.getCurrent());
239   }
240
241   /** Tests that the tab completion of the most recent entry is correct. */
242   public void testSearchHistory() {
243     String JavaDoc entry1 = "some text";
244     String JavaDoc entry2 = "blah";
245
246     _history.add(entry1);
247     _history.add(entry2);
248
249     _history.reverseSearch("s");
250     
251     Utilities.clearEventQueue();
252     assertEquals("Did not find the correct entry in history.", entry1, _history.getCurrent());
253
254     _history.forwardSearch("b");
255     
256     Utilities.clearEventQueue();
257     assertEquals("Did not find the correct entry in history.", entry2, _history.getCurrent());
258   }
259
260   /** Tests that if "tab completion" does not find a match, then cursor goes back to "end". */
261   public void testNoMatch() {
262     String JavaDoc entry1 = "some text";
263     String JavaDoc entry2 = "blah";
264
265     _history.add(entry1);
266     _history.add(entry2);
267
268     _history.reverseSearch("a");
269
270     Utilities.clearEventQueue();
271     assertEquals("Did not reset cursor correctly.", "a", _history.getCurrent());
272   }
273
274   /** Tests reverse searching twice. */
275   public void testReverseSearchTwice() {
276     String JavaDoc entry1 = "same";
277     String JavaDoc entry2 = "some";
278
279     _history.add(entry1);
280     _history.add(entry2);
281
282     _history.reverseSearch("s");
283     
284     Utilities.clearEventQueue();
285     assertEquals("Did not reset cursor correctly.", entry2, _history.getCurrent());
286
287     _history.reverseSearch(_history.getCurrent());
288     
289     Utilities.clearEventQueue();
290     assertEquals("Did not reset cursor correctly.", entry1, _history.getCurrent());
291   }
292 }
293
Popular Tags