1 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 ; 45 import java.io.IOException ; 46 47 50 public final class HistoryTest extends DrJavaTestCase implements OptionConstants{ 51 private History _history; 52 private File _tempDir; 53 54 57 public void setUp() throws Exception { 58 super.setUp(); 59 DrJava.getConfig().resetToDefaults(); 60 String user = System.getProperty("user.name"); 61 _tempDir = IOUtil.createAndMarkTempDirectory("DrJava-test-" + user, ""); 62 _history = new History(); 63 } 64 65 68 public void tearDown() throws Exception { 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 79 public void testSaveAsExistsForOverwrite() throws IOException { 80 _history.add("some text"); 81 final File 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 } 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 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 116 public void testHistoryIsBounded() { 117 int maxLength = 500; 118 DrJava.getConfig().setSetting(HISTORY_MAX_SIZE, new Integer (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 134 public void testLiveUpdateOfHistoryMaxSize() { 135 int maxLength = 20; 136 DrJava.getConfig().setSetting(HISTORY_MAX_SIZE, new Integer (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 (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 (-1)); 158 159 Utilities.clearEventQueue(); 160 assertEquals("History size should still be 0", 0, _history.size()); 161 } 162 163 166 public void testGetHistoryAsString() { 167 DrJava.getConfig().setSetting(HISTORY_MAX_SIZE, new Integer (20)); 168 169 Utilities.clearEventQueue(); 170 assertEquals("testGetHistoryAsString:", "", _history.getHistoryAsString()); 171 172 String 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 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 185 public void testRemembersOneEditedEntry() { 186 _history.add("some text"); 187 _history.movePrevious(""); 188 189 String 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 199 public void testRemembersMultipleEditedEntries() { 200 _history.add("some text"); 201 _history.add("some more text"); 202 _history.movePrevious(""); 203 204 String newEntry1 = "some more different text"; 205 String 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 220 public void testOriginalCopyRemains() { 221 String entry = "some text"; 222 String 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 242 public void testSearchHistory() { 243 String entry1 = "some text"; 244 String 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 261 public void testNoMatch() { 262 String entry1 = "some text"; 263 String 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 275 public void testReverseSearchTwice() { 276 String entry1 = "same"; 277 String 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 |