KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > api > RecentProjectsTest


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.project.ui.api;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.Icon JavaDoc;
26 import javax.swing.ImageIcon JavaDoc;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.api.project.ProjectInformation;
29 import org.netbeans.api.project.ProjectManager;
30 import org.netbeans.api.project.ProjectUtils;
31 import org.netbeans.junit.MockServices;
32 import org.netbeans.junit.NbTestCase;
33 import org.netbeans.modules.project.ui.OpenProjectList;
34 import org.netbeans.modules.project.ui.actions.TestSupport;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.util.lookup.Lookups;
38
39 /**
40  * Tests for RecentProjects class
41  * @author Milan Kubec
42  */

43 public class RecentProjectsTest extends NbTestCase {
44     
45     Project[] testProjects = new Project[15];
46     String JavaDoc[] tpDisplayNames = new String JavaDoc[15];
47     URL JavaDoc[] tpURLs = new URL JavaDoc[15];
48     
49     public static final ImageIcon JavaDoc icon = new ImageIcon JavaDoc(RecentProjectsTest.class.getResource("testimage.png"));
50     public static final String JavaDoc PRJ_NAME_PREFIX = "Project";
51     
52     public RecentProjectsTest(java.lang.String JavaDoc testName) {
53         super(testName);
54     }
55     
56     protected void setUp() throws Exception JavaDoc {
57         super.setUp();
58         MockServices.setServices(TestSupport.TestProjectFactory.class);
59         clearWorkDir();
60         FileObject workDirFO = FileUtil.toFileObject(getWorkDir());
61         for (int i = 0; i < testProjects.length; i++) {
62             String JavaDoc prjName = PRJ_NAME_PREFIX + (i + 1);
63             FileObject p = TestSupport.createTestProject(workDirFO, prjName);
64             TestSupport.TestProject tp = (TestSupport.TestProject) ProjectManager.getDefault().findProject(p);
65             tp.setLookup(Lookups.fixed(new Object JavaDoc[] { new TestProjectInfo(prjName) }));
66             testProjects[i] = tp;
67             tpDisplayNames[i] = ((ProjectInformation) ProjectUtils.getInformation(tp)).getDisplayName();
68             tpURLs[i] = tp.getProjectDirectory().getURL();
69         }
70     }
71     
72     protected void tearDown() throws Exception JavaDoc {
73     }
74     
75     public void testGetRecentProjectsInformation() {
76         
77         List JavaDoc pil;
78         
79         for (int i = 0; i < testProjects.length; i++) {
80             OpenProjectList.getDefault().open(testProjects[i], false);
81         }
82         
83         // Close all projects in the list one by one
84
for (int j = 0; j < testProjects.length; j++) {
85             OpenProjectList.getDefault().close(new Project[] {testProjects[j]}, false);
86             pil = RecentProjects.getDefault().getRecentProjectInformation();
87             assertEquals(1, RecentProjects.getDefault().getRecentProjectInformation().size());
88             assertEquals(tpDisplayNames[j], ((UnloadedProjectInformation) pil.get(0)).getDisplayName());
89             assertEquals(tpURLs[j], ((UnloadedProjectInformation) pil.get(0)).getURL());
90             assertEquals(icon, ((UnloadedProjectInformation) pil.get(0)).getIcon());
91             OpenProjectList.getDefault().open(testProjects[j], false);
92         }
93         
94         assertEquals(0, RecentProjects.getDefault().getRecentProjectInformation().size());
95         
96         // Close rand number of rand modules
97
OpenProjectList.getDefault().close(new Project[] {testProjects[3]}, false);
98         OpenProjectList.getDefault().close(new Project[] {testProjects[4]}, false);
99         OpenProjectList.getDefault().close(new Project[] {testProjects[6]}, false);
100         OpenProjectList.getDefault().close(new Project[] {testProjects[10]}, false);
101         OpenProjectList.getDefault().close(new Project[] {testProjects[12]}, false);
102         
103         pil = RecentProjects.getDefault().getRecentProjectInformation();
104         assertEquals(5, RecentProjects.getDefault().getRecentProjectInformation().size());
105         
106         assertEquals(tpDisplayNames[12], ((UnloadedProjectInformation) pil.get(0)).getDisplayName());
107         assertEquals(tpURLs[12], ((UnloadedProjectInformation) pil.get(0)).getURL());
108         assertEquals(icon, ((UnloadedProjectInformation) pil.get(0)).getIcon());
109         
110         assertEquals(tpDisplayNames[10], ((UnloadedProjectInformation) pil.get(1)).getDisplayName());
111         assertEquals(tpURLs[10], ((UnloadedProjectInformation) pil.get(1)).getURL());
112         assertEquals(icon, ((UnloadedProjectInformation) pil.get(1)).getIcon());
113         
114         assertEquals(tpDisplayNames[6], ((UnloadedProjectInformation) pil.get(2)).getDisplayName());
115         assertEquals(tpURLs[6], ((UnloadedProjectInformation) pil.get(2)).getURL());
116         assertEquals(icon, ((UnloadedProjectInformation) pil.get(2)).getIcon());
117         
118         assertEquals(tpDisplayNames[4], ((UnloadedProjectInformation) pil.get(3)).getDisplayName());
119         assertEquals(tpURLs[4], ((UnloadedProjectInformation) pil.get(3)).getURL());
120         assertEquals(icon, ((UnloadedProjectInformation) pil.get(3)).getIcon());
121         
122         assertEquals(tpDisplayNames[3], ((UnloadedProjectInformation) pil.get(4)).getDisplayName());
123         assertEquals(tpURLs[3], ((UnloadedProjectInformation) pil.get(4)).getURL());
124         assertEquals(icon, ((UnloadedProjectInformation) pil.get(4)).getIcon());
125         
126         OpenProjectList.getDefault().open(testProjects[3], false);
127         OpenProjectList.getDefault().open(testProjects[4], false);
128         OpenProjectList.getDefault().open(testProjects[6], false);
129         OpenProjectList.getDefault().open(testProjects[10], false);
130         OpenProjectList.getDefault().open(testProjects[12], false);
131         
132         assertEquals(0, RecentProjects.getDefault().getRecentProjectInformation().size());
133         
134         // Close ten projects
135
for (int k = 3; k < 13; k++) {
136             OpenProjectList.getDefault().close(new Project[] {testProjects[k]}, false);
137         }
138         pil = RecentProjects.getDefault().getRecentProjectInformation();
139         assertEquals(10, RecentProjects.getDefault().getRecentProjectInformation().size());
140         for (int l = 0; l > 10; l++) {
141             assertEquals(tpDisplayNames[12 - l], ((UnloadedProjectInformation) pil.get(l)).getDisplayName());
142             assertEquals(tpURLs[12 - l], ((UnloadedProjectInformation) pil.get(l)).getURL());
143             assertEquals(icon, ((UnloadedProjectInformation) pil.get(l)).getIcon());
144         }
145         for (int m = 3; m < 13; m++) {
146             OpenProjectList.getDefault().open(testProjects[m], false);
147         }
148         
149         assertEquals(0, RecentProjects.getDefault().getRecentProjectInformation().size());
150         
151         // Open and close more than ten projects
152
for (int n = 0; n < testProjects.length; n++) {
153             OpenProjectList.getDefault().close(new Project[] {testProjects[n]}, false);
154         }
155         pil = RecentProjects.getDefault().getRecentProjectInformation();
156         assertEquals(10, RecentProjects.getDefault().getRecentProjectInformation().size());
157         for (int p = 0; p > 10; p++) {
158             assertEquals(tpDisplayNames[testProjects.length - p], ((UnloadedProjectInformation) pil.get(p)).getDisplayName());
159             assertEquals(tpURLs[testProjects.length - p], ((UnloadedProjectInformation) pil.get(p)).getURL());
160             assertEquals(icon, ((UnloadedProjectInformation) pil.get(p)).getIcon());
161         }
162         for (int q = 0; q < testProjects.length; q++) {
163             OpenProjectList.getDefault().open(testProjects[q], false);
164         }
165         
166         assertEquals(0, RecentProjects.getDefault().getRecentProjectInformation().size());
167         
168         // close array of projects
169
OpenProjectList.getDefault().close(new Project[] {testProjects[2], testProjects[5], testProjects[9], testProjects[11]}, false);
170         pil = RecentProjects.getDefault().getRecentProjectInformation();
171         assertEquals(4, RecentProjects.getDefault().getRecentProjectInformation().size());
172         
173         assertEquals(tpDisplayNames[11], ((UnloadedProjectInformation) pil.get(0)).getDisplayName());
174         assertEquals(tpURLs[11], ((UnloadedProjectInformation) pil.get(0)).getURL());
175         assertEquals(icon, ((UnloadedProjectInformation) pil.get(0)).getIcon());
176         
177         assertEquals(tpDisplayNames[9], ((UnloadedProjectInformation) pil.get(1)).getDisplayName());
178         assertEquals(tpURLs[9], ((UnloadedProjectInformation) pil.get(1)).getURL());
179         assertEquals(icon, ((UnloadedProjectInformation) pil.get(1)).getIcon());
180         
181         assertEquals(tpDisplayNames[5], ((UnloadedProjectInformation) pil.get(2)).getDisplayName());
182         assertEquals(tpURLs[5], ((UnloadedProjectInformation) pil.get(2)).getURL());
183         assertEquals(icon, ((UnloadedProjectInformation) pil.get(2)).getIcon());
184         
185         assertEquals(tpDisplayNames[2], ((UnloadedProjectInformation) pil.get(3)).getDisplayName());
186         assertEquals(tpURLs[2], ((UnloadedProjectInformation) pil.get(3)).getURL());
187         assertEquals(icon, ((UnloadedProjectInformation) pil.get(3)).getIcon());
188         
189     }
190     
191     // -------------------------------------------------------------------------
192

193     private static class TestProjectInfo implements ProjectInformation {
194         
195         private String JavaDoc displayName;
196         
197         public TestProjectInfo(String JavaDoc dname) {
198             displayName = dname;
199         }
200         public String JavaDoc getName() {
201             return displayName;
202         }
203         public String JavaDoc getDisplayName() {
204             return displayName;
205         }
206         public Icon JavaDoc getIcon() {
207             return icon;
208         }
209         public Project getProject() {
210             return null;
211         }
212         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {}
213         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {}
214         
215     }
216     
217 }
218
Popular Tags