1 19 20 package org.netbeans.modules.project.ui.api; 21 22 import java.beans.PropertyChangeListener ; 23 import java.net.URL ; 24 import java.util.List ; 25 import javax.swing.Icon ; 26 import javax.swing.ImageIcon ; 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 43 public class RecentProjectsTest extends NbTestCase { 44 45 Project[] testProjects = new Project[15]; 46 String [] tpDisplayNames = new String [15]; 47 URL [] tpURLs = new URL [15]; 48 49 public static final ImageIcon icon = new ImageIcon (RecentProjectsTest.class.getResource("testimage.png")); 50 public static final String PRJ_NAME_PREFIX = "Project"; 51 52 public RecentProjectsTest(java.lang.String testName) { 53 super(testName); 54 } 55 56 protected void setUp() throws Exception { 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 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 [] { 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 { 73 } 74 75 public void testGetRecentProjectsInformation() { 76 77 List pil; 78 79 for (int i = 0; i < testProjects.length; i++) { 80 OpenProjectList.getDefault().open(testProjects[i], false); 81 } 82 83 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 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 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 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 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 193 private static class TestProjectInfo implements ProjectInformation { 194 195 private String displayName; 196 197 public TestProjectInfo(String dname) { 198 displayName = dname; 199 } 200 public String getName() { 201 return displayName; 202 } 203 public String getDisplayName() { 204 return displayName; 205 } 206 public Icon getIcon() { 207 return icon; 208 } 209 public Project getProject() { 210 return null; 211 } 212 public void addPropertyChangeListener(PropertyChangeListener listener) {} 213 public void removePropertyChangeListener(PropertyChangeListener listener) {} 214 215 } 216 217 } 218 | Popular Tags |