1 19 20 package org.netbeans.modules.openfile; 21 22 import java.net.URL ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import junit.framework.Test; 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 import org.netbeans.modules.openfile.RecentFiles.HistoryItem; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.URLMapper; 32 import org.openide.loaders.DataObject; 33 import org.openide.util.lookup.Lookups; 34 import org.openide.windows.CloneableTopComponent; 35 36 37 42 public class RecentFilesTest extends TestCase { 43 44 public RecentFilesTest(String testName) { 45 super(testName); 46 } 47 48 public static void main(java.lang.String [] args) { 49 junit.textui.TestRunner.run(suite()); 50 } 51 52 public static Test suite() { 53 return new TestSuite(RecentFilesTest.class); 54 } 55 56 protected void setUp() throws Exception { 57 } 58 59 protected void tearDown() throws Exception { 60 } 61 62 public void testGetRecentFiles () throws Exception { 63 System.out.println("Testing RecentFiles.getRecentFiles..."); 64 URL url = RecentFilesTest.class.getResource("resources/recent_files/"); 65 assertNotNull("url not found.", url); 66 67 FileObject folder = URLMapper.findFileObject(url); 68 FileObject[] files = folder.getChildren(); 69 List <EditorLikeTC> tcs = new ArrayList <EditorLikeTC>(); 70 71 RecentFiles.getPrefs().clear(); 72 RecentFiles.init(); 73 74 for (FileObject curFo : files) { 75 EditorLikeTC curTC = new EditorLikeTC(curFo); 76 tcs.add(0, curTC); 77 curTC.open(); 78 } 79 80 for (EditorLikeTC curTC : tcs) { 83 curTC.close(); 84 } 85 int i = 0; 86 List <HistoryItem> recentFiles = RecentFiles.getRecentFiles(); 87 assertTrue("Expected " + files.length + " recent files, got " + recentFiles.size(), files.length == recentFiles.size()); 88 for (FileObject fo : files) { 89 assertEquals(fo, recentFiles.get(i).getFile()); 90 i++; 91 } 92 93 tcs.get(0).open(); 96 recentFiles = RecentFiles.getRecentFiles(); 97 assertTrue(files.length == (recentFiles.size() + 1)); 98 99 } 100 101 public void testPersistence () throws Exception { 102 System.out.println("Testing perfistence of recent files history..."); 103 URL url = RecentFilesTest.class.getResource("resources/recent_files/"); 104 assertNotNull("url not found.", url); 105 106 FileObject folder = URLMapper.findFileObject(url); 107 FileObject[] files = folder.getChildren(); 108 109 RecentFiles.getPrefs().clear(); 110 111 for (FileObject file : files) { 113 HistoryItem hItem = new HistoryItem(file, System.currentTimeMillis()); 114 RecentFiles.storeAdded(hItem); 115 Thread.sleep(100); 116 } 117 List <HistoryItem> loaded = RecentFiles.load(); 118 assertTrue("Persistence failed, " + files.length + " stored items, " + loaded.size() + " loaded.", files.length == loaded.size()); 119 int i = files.length - 1; 120 for (FileObject fileObject : files) { 121 assertTrue("File #" + (i + 1) + " differs", fileObject.equals(loaded.get(i--).getFile())); 122 } 123 } 124 125 public void test87252 () throws Exception { 126 System.out.println("Testing fix for 87252..."); 127 URL url = RecentFilesTest.class.getResource("resources/recent_files/"); 128 assertNotNull("url not found.", url); 129 130 FileObject folder = URLMapper.findFileObject(url); 131 FileObject fo = folder.createData("ToBeDeleted.txt"); 132 133 RecentFiles.getPrefs().clear(); 134 RecentFiles.init(); 135 136 EditorLikeTC tc = new EditorLikeTC(fo); 137 tc.open(); 138 tc.close(); 139 140 fo.delete(); 142 List <HistoryItem> recentFiles = RecentFiles.getRecentFiles(); 143 boolean contained = false; 144 for (HistoryItem historyItem : recentFiles) { 145 if (fo.equals(historyItem.getFile())) { 146 contained = true; 147 break; 148 } 149 } 150 assertFalse("Deleted file should not be in recent files", contained); 151 } 152 153 154 155 private static class EditorLikeTC extends CloneableTopComponent { 156 157 public EditorLikeTC (FileObject fo) throws Exception { 158 DataObject dObj = DataObject.find(fo); 159 associateLookup(Lookups.singleton(dObj)); 160 } 161 162 } 163 164 } 165 | Popular Tags |