KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openfile > RecentFilesTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.openfile;
21
22 import java.net.URL JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
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 /**
38  * Tests for RecentFiles support, tests list modification policy.
39  *
40  * @author Dafe Simonek
41  */

42 public class RecentFilesTest extends TestCase {
43     
44     public RecentFilesTest(String JavaDoc testName) {
45         super(testName);
46     }
47
48     public static void main(java.lang.String JavaDoc[] 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 JavaDoc {
57     }
58
59     protected void tearDown() throws Exception JavaDoc {
60     }
61
62     public void testGetRecentFiles () throws Exception JavaDoc {
63         System.out.println("Testing RecentFiles.getRecentFiles...");
64         URL JavaDoc 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 JavaDoc<EditorLikeTC> tcs = new ArrayList JavaDoc<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         // close top components and check if they were added correctly to
81
// recent files list
82
for (EditorLikeTC curTC : tcs) {
83             curTC.close();
84         }
85         int i = 0;
86         List JavaDoc<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         // reopen first component again and check that it was removed from
94
// recent files list
95
tcs.get(0).open();
96         recentFiles = RecentFiles.getRecentFiles();
97         assertTrue(files.length == (recentFiles.size() + 1));
98         
99     }
100     
101     public void testPersistence () throws Exception JavaDoc {
102         System.out.println("Testing perfistence of recent files history...");
103         URL JavaDoc 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         // store, load and check for equality
112
for (FileObject file : files) {
113             HistoryItem hItem = new HistoryItem(file, System.currentTimeMillis());
114             RecentFiles.storeAdded(hItem);
115             Thread.sleep(100);
116         }
117         List JavaDoc<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 JavaDoc {
126         System.out.println("Testing fix for 87252...");
127         URL JavaDoc 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         // delete file and check that recent files *doesn't* contain the file
141
fo.delete();
142         List JavaDoc<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     /** Special TopComponent subclass which imitates TopComponents used for documents, editors */
155     private static class EditorLikeTC extends CloneableTopComponent {
156         
157         public EditorLikeTC (FileObject fo) throws Exception JavaDoc {
158             DataObject dObj = DataObject.find(fo);
159             associateLookup(Lookups.singleton(dObj));
160         }
161         
162     }
163
164 }
165
Popular Tags