1 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 ; 44 import java.io.IOException ; 45 import java.util.Vector ; 46 47 50 public final class RecentFileManagerTest extends DrJavaTestCase { 51 52 protected static final String FOO_TEXT = "class DrJavaTestFoo {}"; 53 protected static final String BAR_TEXT = "class DrJavaTestBar {}"; 54 private RecentFileManager _rfm; 55 private JMenu _menu; 56 protected File _tempDir; 57 58 61 public static Test suite() { return new TestSuite(RecentFileManagerTest.class); } 62 63 64 public void setUp() throws Exception { 65 super.setUp(); 66 _menu = new JMenu(); 67 _rfm = new RecentFileManager(0, _menu, null, OptionConstants.RECENT_FILES); 68 String user = System.getProperty("user.name"); 69 _tempDir = IOUtil.createAndMarkTempDirectory("DrJava-test-" + user, ""); 70 } 71 72 public void tearDown() throws Exception { 73 _menu = null; 74 _rfm = null; 75 IOUtil.deleteRecursively(_tempDir); 76 _tempDir = null; 77 super.tearDown(); 78 } 79 80 83 protected File tempFile() throws IOException { 84 return File.createTempFile("DrJava-test", ".java", _tempDir).getCanonicalFile(); 85 } 86 87 88 protected File writeToNewTempFile(String text) throws IOException { 89 File temp = tempFile(); 90 IOUtil.writeStringToFile(temp, text); 91 return temp; 92 } 93 94 95 public void testAddMoreThanMaxSize() throws IOException { 96 97 final File tempFile = writeToNewTempFile(BAR_TEXT); 98 final File tempFile2 = writeToNewTempFile(FOO_TEXT); 99 _rfm.updateMax(1); 100 _rfm.updateOpenFiles(tempFile); 101 _rfm.updateOpenFiles(tempFile2); 102 Vector <File > 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 108 public void testShrinksToMaxSize() throws IOException { 109 110 final File tempFile = writeToNewTempFile(BAR_TEXT); 111 final File tempFile2 = writeToNewTempFile(FOO_TEXT); 112 _rfm.updateMax(2); 113 114 _rfm.updateOpenFiles(tempFile); 115 _rfm.updateOpenFiles(tempFile2); 116 Vector <File > 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 128 public void testRemoveFile() throws Exception { 129 final File tempFile = writeToNewTempFile(BAR_TEXT); 131 final File tempFile2 = writeToNewTempFile(FOO_TEXT); 132 _rfm.updateMax(2); 133 _rfm.updateOpenFiles(tempFile); 134 _rfm.updateOpenFiles(tempFile2); 135 Vector <File > vector = _rfm.getFileVector(); 136 assertEquals("tempFile2 should be at top", vector.get(0), tempFile2); 137 138 _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 _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 _rfm.removeIfInList(tempFile); 150 assertEquals("number of recent files", 0, vector.size()); 151 } 152 153 154 public void testReopenFiles() throws Exception { 155 final File tempFile = writeToNewTempFile(BAR_TEXT); 156 final File tempFile2 = writeToNewTempFile(FOO_TEXT); 157 158 _rfm.updateMax(2); 159 _rfm.updateOpenFiles(tempFile2); 160 _rfm.updateOpenFiles(tempFile); 161 Vector <File > vector = _rfm.getFileVector(); 162 163 assertEquals("tempFile should be at top", vector.get(0), tempFile); 164 165 _rfm.updateOpenFiles(tempFile2); 167 vector = _rfm.getFileVector(); 168 assertEquals("tempFile2 should be at top", vector.get(0), tempFile2); 169 170 File parent = tempFile.getParentFile(); 172 String dotSlash = "." + System.getProperty("file.separator"); 173 parent = new File (parent, dotSlash); 174 File sameFile = new File (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 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 |