001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.openfile;
043:
044: import java.net.URL;
045: import java.util.ArrayList;
046: import java.util.List;
047: import junit.framework.Test;
048: import junit.framework.TestCase;
049: import junit.framework.TestSuite;
050: import org.netbeans.modules.openfile.RecentFiles.HistoryItem;
051: import org.openide.filesystems.FileObject;
052: import org.openide.filesystems.FileObject;
053: import org.openide.filesystems.URLMapper;
054: import org.openide.loaders.DataObject;
055: import org.openide.util.lookup.Lookups;
056: import org.openide.windows.CloneableTopComponent;
057:
058: /**
059: * Tests for RecentFiles support, tests list modification policy.
060: *
061: * @author Dafe Simonek
062: */
063: public class RecentFilesTest extends TestCase {
064:
065: public RecentFilesTest(String testName) {
066: super (testName);
067: }
068:
069: public static void main(java.lang.String[] args) {
070: junit.textui.TestRunner.run(suite());
071: }
072:
073: public static Test suite() {
074: return new TestSuite(RecentFilesTest.class);
075: }
076:
077: protected void setUp() throws Exception {
078: }
079:
080: protected void tearDown() throws Exception {
081: }
082:
083: public void testGetRecentFiles() throws Exception {
084: System.out.println("Testing RecentFiles.getRecentFiles...");
085: URL url = RecentFilesTest.class
086: .getResource("resources/recent_files/");
087: assertNotNull("url not found.", url);
088:
089: FileObject folder = URLMapper.findFileObject(url);
090: FileObject[] files = folder.getChildren();
091: List<EditorLikeTC> tcs = new ArrayList<EditorLikeTC>();
092:
093: RecentFiles.getPrefs().clear();
094: RecentFiles.init();
095:
096: for (FileObject curFo : files) {
097: EditorLikeTC curTC = new EditorLikeTC(curFo);
098: tcs.add(0, curTC);
099: curTC.open();
100: }
101:
102: // close top components and check if they were added correctly to
103: // recent files list
104: for (EditorLikeTC curTC : tcs) {
105: curTC.close();
106: }
107: int i = 0;
108: List<HistoryItem> recentFiles = RecentFiles.getRecentFiles();
109: assertTrue("Expected " + files.length + " recent files, got "
110: + recentFiles.size(), files.length == recentFiles
111: .size());
112: for (FileObject fo : files) {
113: assertEquals(RecentFiles.convertFile2URL(fo), recentFiles
114: .get(i).getURL());
115: i++;
116: }
117:
118: // reopen first component again and check that it was removed from
119: // recent files list
120: tcs.get(0).open();
121: recentFiles = RecentFiles.getRecentFiles();
122: assertTrue(files.length == (recentFiles.size() + 1));
123:
124: }
125:
126: public void testPersistence() throws Exception {
127: System.out
128: .println("Testing perfistence of recent files history...");
129: URL url = RecentFilesTest.class
130: .getResource("resources/recent_files/");
131: assertNotNull("url not found.", url);
132:
133: FileObject folder = URLMapper.findFileObject(url);
134: FileObject[] files = folder.getChildren();
135:
136: RecentFiles.getPrefs().clear();
137:
138: // store, load and check for equality
139: for (FileObject file : files) {
140: HistoryItem hItem = new HistoryItem(RecentFiles
141: .convertFile2URL(file), System.currentTimeMillis());
142: RecentFiles.storeAdded(hItem);
143: Thread.sleep(100);
144: }
145: List<HistoryItem> loaded = RecentFiles.load();
146: assertTrue("Persistence failed, " + files.length
147: + " stored items, " + loaded.size() + " loaded.",
148: files.length == loaded.size());
149: int i = files.length - 1;
150: for (FileObject fileObject : files) {
151: assertTrue("File #" + (i + 1) + " differs", fileObject
152: .equals(RecentFiles.convertURL2File(loaded.get(i--)
153: .getURL())));
154: }
155: }
156:
157: public void test87252() throws Exception {
158: System.out.println("Testing fix for 87252...");
159: URL url = RecentFilesTest.class
160: .getResource("resources/recent_files/");
161: assertNotNull("url not found.", url);
162:
163: FileObject folder = URLMapper.findFileObject(url);
164: FileObject fo = folder.createData("ToBeDeleted.txt");
165:
166: RecentFiles.getPrefs().clear();
167: RecentFiles.init();
168:
169: EditorLikeTC tc = new EditorLikeTC(fo);
170: tc.open();
171: tc.close();
172:
173: // delete file and check that recent files *doesn't* contain the file
174: fo.delete();
175: List<HistoryItem> recentFiles = RecentFiles.getRecentFiles();
176: boolean contained = false;
177: for (HistoryItem historyItem : recentFiles) {
178: if (fo.equals(RecentFiles.convertURL2File(historyItem
179: .getURL()))) {
180: contained = true;
181: break;
182: }
183: }
184: assertFalse("Deleted file should not be in recent files",
185: contained);
186: }
187:
188: /** Special TopComponent subclass which imitates TopComponents used for documents, editors */
189: private static class EditorLikeTC extends CloneableTopComponent {
190:
191: public EditorLikeTC(FileObject fo) throws Exception {
192: DataObject dObj = DataObject.find(fo);
193: associateLookup(Lookups.singleton(dObj));
194: }
195:
196: }
197:
198: }
|