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.project.ui.api;
043:
044: import java.beans.PropertyChangeListener;
045: import java.net.URL;
046: import java.util.List;
047: import javax.swing.Icon;
048: import javax.swing.ImageIcon;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.api.project.ProjectInformation;
051: import org.netbeans.api.project.ProjectManager;
052: import org.netbeans.api.project.ProjectUtils;
053: import org.netbeans.junit.MockServices;
054: import org.netbeans.junit.NbTestCase;
055: import org.netbeans.modules.project.ui.OpenProjectList;
056: import org.netbeans.modules.project.ui.actions.TestSupport;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.FileUtil;
059: import org.openide.util.lookup.Lookups;
060:
061: /**
062: * Tests for RecentProjects class
063: * @author Milan Kubec
064: */
065: public class RecentProjectsTest extends NbTestCase {
066:
067: private Project[] testProjects = new Project[15];
068: private String[] tpDisplayNames = new String[15];
069: private URL[] tpURLs = new URL[15];
070:
071: public static final ImageIcon icon = new ImageIcon(
072: RecentProjectsTest.class.getResource("testimage.png"));
073: public static final String PRJ_NAME_PREFIX = "Project";
074:
075: public RecentProjectsTest(String testName) {
076: super (testName);
077: }
078:
079: @Override
080: protected void setUp() throws Exception {
081: super .setUp();
082: MockServices.setServices(TestSupport.TestProjectFactory.class);
083: clearWorkDir();
084: FileObject workDirFO = FileUtil.toFileObject(getWorkDir());
085: for (int i = 0; i < testProjects.length; i++) {
086: String prjName = PRJ_NAME_PREFIX + (i + 1);
087: FileObject p = TestSupport.createTestProject(workDirFO,
088: prjName);
089: TestSupport.TestProject tp = (TestSupport.TestProject) ProjectManager
090: .getDefault().findProject(p);
091: tp.setLookup(Lookups.fixed(new TestProjectInfo(prjName)));
092: testProjects[i] = tp;
093: tpDisplayNames[i] = ProjectUtils.getInformation(tp)
094: .getDisplayName();
095: tpURLs[i] = tp.getProjectDirectory().getURL();
096: }
097: }
098:
099: public void testGetRecentProjectsInformation() {
100:
101: List<UnloadedProjectInformation> pil;
102:
103: for (Project p : testProjects) {
104: OpenProjectList.getDefault().open(p, false);
105: }
106:
107: // Close all projects in the list one by one
108: for (int j = 0; j < testProjects.length; j++) {
109: OpenProjectList.getDefault().close(
110: new Project[] { testProjects[j] }, false);
111: pil = RecentProjects.getDefault()
112: .getRecentProjectInformation();
113: assertEquals(1, RecentProjects.getDefault()
114: .getRecentProjectInformation().size());
115: assertEquals(tpDisplayNames[j], pil.get(0).getDisplayName());
116: assertEquals(tpURLs[j], pil.get(0).getURL());
117: assertEquals(icon, pil.get(0).getIcon());
118: OpenProjectList.getDefault().open(testProjects[j], false);
119: }
120:
121: assertEquals(0, RecentProjects.getDefault()
122: .getRecentProjectInformation().size());
123:
124: // Close rand number of rand modules
125: OpenProjectList.getDefault().close(
126: new Project[] { testProjects[3] }, false);
127: OpenProjectList.getDefault().close(
128: new Project[] { testProjects[4] }, false);
129: OpenProjectList.getDefault().close(
130: new Project[] { testProjects[6] }, false);
131: OpenProjectList.getDefault().close(
132: new Project[] { testProjects[10] }, false);
133: OpenProjectList.getDefault().close(
134: new Project[] { testProjects[12] }, false);
135:
136: pil = RecentProjects.getDefault().getRecentProjectInformation();
137: assertEquals(5, RecentProjects.getDefault()
138: .getRecentProjectInformation().size());
139:
140: assertEquals(tpDisplayNames[12], pil.get(0).getDisplayName());
141: assertEquals(tpURLs[12], pil.get(0).getURL());
142: assertEquals(icon, pil.get(0).getIcon());
143:
144: assertEquals(tpDisplayNames[10], pil.get(1).getDisplayName());
145: assertEquals(tpURLs[10], pil.get(1).getURL());
146: assertEquals(icon, pil.get(1).getIcon());
147:
148: assertEquals(tpDisplayNames[6], pil.get(2).getDisplayName());
149: assertEquals(tpURLs[6], pil.get(2).getURL());
150: assertEquals(icon, pil.get(2).getIcon());
151:
152: assertEquals(tpDisplayNames[4], pil.get(3).getDisplayName());
153: assertEquals(tpURLs[4], pil.get(3).getURL());
154: assertEquals(icon, pil.get(3).getIcon());
155:
156: assertEquals(tpDisplayNames[3], pil.get(4).getDisplayName());
157: assertEquals(tpURLs[3], pil.get(4).getURL());
158: assertEquals(icon, pil.get(4).getIcon());
159:
160: OpenProjectList.getDefault().open(testProjects[3], false);
161: OpenProjectList.getDefault().open(testProjects[4], false);
162: OpenProjectList.getDefault().open(testProjects[6], false);
163: OpenProjectList.getDefault().open(testProjects[10], false);
164: OpenProjectList.getDefault().open(testProjects[12], false);
165:
166: assertEquals(0, RecentProjects.getDefault()
167: .getRecentProjectInformation().size());
168:
169: // Close ten projects
170: for (int k = 3; k < 13; k++) {
171: OpenProjectList.getDefault().close(
172: new Project[] { testProjects[k] }, false);
173: }
174: pil = RecentProjects.getDefault().getRecentProjectInformation();
175: assertEquals(10, RecentProjects.getDefault()
176: .getRecentProjectInformation().size());
177: for (int l = 0; l > 10; l++) {
178: assertEquals(tpDisplayNames[12 - l], pil.get(l)
179: .getDisplayName());
180: assertEquals(tpURLs[12 - l], pil.get(l).getURL());
181: assertEquals(icon, pil.get(l).getIcon());
182: }
183: for (int m = 3; m < 13; m++) {
184: OpenProjectList.getDefault().open(testProjects[m], false);
185: }
186:
187: assertEquals(0, RecentProjects.getDefault()
188: .getRecentProjectInformation().size());
189:
190: // Open and close more than ten projects
191: for (Project p : testProjects) {
192: OpenProjectList.getDefault().close(new Project[] { p },
193: false);
194: }
195: pil = RecentProjects.getDefault().getRecentProjectInformation();
196: assertEquals(10, RecentProjects.getDefault()
197: .getRecentProjectInformation().size());
198: for (int p = 0; p > 10; p++) {
199: assertEquals(tpDisplayNames[testProjects.length - p], pil
200: .get(p).getDisplayName());
201: assertEquals(tpURLs[testProjects.length - p], pil.get(p)
202: .getURL());
203: assertEquals(icon, pil.get(p).getIcon());
204: }
205: for (Project p : testProjects) {
206: OpenProjectList.getDefault().open(p, false);
207: }
208:
209: assertEquals(0, RecentProjects.getDefault()
210: .getRecentProjectInformation().size());
211:
212: // close array of projects
213: OpenProjectList.getDefault().close(
214: new Project[] { testProjects[2], testProjects[5],
215: testProjects[9], testProjects[11] }, false);
216: pil = RecentProjects.getDefault().getRecentProjectInformation();
217: assertEquals(4, RecentProjects.getDefault()
218: .getRecentProjectInformation().size());
219:
220: assertEquals(tpDisplayNames[11], pil.get(0).getDisplayName());
221: assertEquals(tpURLs[11], pil.get(0).getURL());
222: assertEquals(icon, pil.get(0).getIcon());
223:
224: assertEquals(tpDisplayNames[9], pil.get(1).getDisplayName());
225: assertEquals(tpURLs[9], pil.get(1).getURL());
226: assertEquals(icon, pil.get(1).getIcon());
227:
228: assertEquals(tpDisplayNames[5], pil.get(2).getDisplayName());
229: assertEquals(tpURLs[5], pil.get(2).getURL());
230: assertEquals(icon, pil.get(2).getIcon());
231:
232: assertEquals(tpDisplayNames[2], pil.get(3).getDisplayName());
233: assertEquals(tpURLs[2], pil.get(3).getURL());
234: assertEquals(icon, pil.get(3).getIcon());
235:
236: }
237:
238: // -------------------------------------------------------------------------
239:
240: private static class TestProjectInfo implements ProjectInformation {
241:
242: private String displayName;
243:
244: public TestProjectInfo(String dname) {
245: displayName = dname;
246: }
247:
248: public String getName() {
249: return displayName;
250: }
251:
252: public String getDisplayName() {
253: return displayName;
254: }
255:
256: public Icon getIcon() {
257: return icon;
258: }
259:
260: public Project getProject() {
261: return null;
262: }
263:
264: public void addPropertyChangeListener(
265: PropertyChangeListener listener) {
266: }
267:
268: public void removePropertyChangeListener(
269: PropertyChangeListener listener) {
270: }
271:
272: }
273:
274: }
|