1 19 20 package org.netbeans.modules.search.project; 21 22 import java.awt.EventQueue ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.Iterator ; 29 import org.netbeans.api.project.Project; 30 import org.netbeans.api.project.ProjectManager; 31 import org.netbeans.api.project.ui.OpenProjects; 32 import org.netbeans.junit.MockServices; 33 import org.netbeans.junit.NbTestCase; 34 import org.netbeans.spi.project.ProjectFactory; 35 import org.netbeans.spi.project.ProjectState; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.nodes.Node; 39 import org.openide.util.Lookup; 40 import org.openide.util.actions.SystemAction; 41 import org.openide.util.lookup.Lookups; 42 import org.openidex.search.SearchInfo; 43 44 49 public class ProjectsSearchActionTest extends NbTestCase { 50 51 private static final String PRJ_WITH_SEARCH_INFO = "prjWithSearchInfo"; 52 private static final String PRJ_WITHOUT_SEARCH_INFO = "prjWithoutSearchInfo"; 53 54 private WorkDirCleaner workDirCleaner; 55 private File prjRootDir; 56 private FileObject prjRootFileObj; 57 58 public ProjectsSearchActionTest(String testName) { 59 super(testName); 60 } 61 62 @Override 63 protected boolean runInEQ() { 64 return true; 65 } 66 67 73 protected void setUp() throws Exception { 74 workDirCleaner = null; 75 prjRootDir = null; 76 prjRootFileObj = null; 77 78 MockServices.setServices(MockProjectFactory.class); 79 80 OpenProjects openProjectsImpl = OpenProjects.getDefault(); 81 final Project[] openProjects = openProjectsImpl.getOpenProjects(); 82 if (openProjects.length == 0) { 83 clearWorkDir(); 84 } else { 85 openProjectsImpl.close(openProjects); 86 workDirCleaner = new WorkDirCleaner(); 87 EventQueue.invokeLater(workDirCleaner); 88 } 89 } 90 91 final class WorkDirCleaner implements Runnable { 92 private IOException ioExc; 93 public void run() { 94 try { 95 clearWorkDir(); 96 } catch (IOException ex) { 97 this.ioExc = ex; 98 } 99 } 100 } 101 102 protected void tearDown() throws Exception { 103 super.tearDown(); 104 } 105 106 111 public void testProjectWithSearchInfo() throws IOException { 112 checkWorkDirCleaner(); 113 114 openProject(PRJ_WITH_SEARCH_INFO); 115 116 Collection <SearchInfo> searchInfos = getSearchInfoToBeUsed(); 117 assertTrue("Single SearchInfo instance expected", searchInfos.size() == 1); 118 assertTrue("Unexpected SearchInfo used by ProjectsSearchAction", 119 searchInfos.iterator().next().getClass() == MockSearchInfo.class); 120 } 121 122 127 public void testProjectWithoutSearchInfo() throws IOException { 128 checkWorkDirCleaner(); 129 130 openProject(PRJ_WITHOUT_SEARCH_INFO); 131 132 Collection <SearchInfo> searchInfos = getSearchInfoToBeUsed(); 133 assertFalse("No SearchInfo used by ProjectsSearchAction", searchInfos.isEmpty()); 134 135 for (SearchInfo searchInfo : searchInfos) { 136 String searchInfoClassName = searchInfo.getClass().getName(); 137 if (!searchInfoClassName.startsWith("org.openidex.search.")) { 138 fail("Unexpected SearchInfo used by ProjectsSearchAction: " + searchInfoClassName); 139 } 140 } 141 } 142 143 145 private void checkWorkDirCleaner() { 146 if ((workDirCleaner != null) && (workDirCleaner.ioExc != null)) { 147 fail("WorkDirCleaner thrown an exception: " + workDirCleaner.ioExc); 148 } 149 } 150 151 153 private Collection <SearchInfo> getSearchInfoToBeUsed() { 154 ProjectsSearchAction prjSearchAction = SystemAction.get(ProjectsSearchAction.class); 155 assertNotNull("ProjectsSearchAction not found", prjSearchAction); 156 157 prjSearchAction.isEnabled(); boolean isEnabled = prjSearchAction.isEnabled(); 159 assertTrue("ProjectsSearchAction should be enabled", isEnabled); 160 161 Node[] nodesToSearch = prjSearchAction.getNodesToSearch(); 162 assertTrue("No nodes to search", (nodesToSearch != null) && (nodesToSearch.length != 0)); 163 164 Collection <SearchInfo> result = new ArrayList <SearchInfo>(nodesToSearch.length); 165 for (Node node : nodesToSearch) { 166 SearchInfo searchInfo = node.getLookup().lookup(SearchInfo.class); 167 if (searchInfo != null) { 168 result.add(searchInfo); 169 } 170 } 171 return result; 172 } 173 174 177 private void openProject(String prjDirName) throws IOException { 178 assert EventQueue.isDispatchThread(); 179 180 prjRootDir = new File (getWorkDir(), prjDirName); 181 prjRootDir.mkdirs(); 182 183 new File (prjRootDir, "nbproject").mkdirs(); 184 185 prjRootFileObj = FileUtil.toFileObject(prjRootDir); 186 assertTrue(prjRootDir.getParent() + " should be recognized as a project directory", 187 ProjectManager.getDefault().isProject(prjRootFileObj)); 188 189 Project project = null; 190 try { 191 project = ProjectManager.getDefault().findProject(prjRootFileObj); 192 } catch (IOException ex) { 193 fail("Could not load project " + prjRootFileObj); 194 } 195 assertNotNull("findProject(...) returned null", project); 196 197 OpenProjects.getDefault().open(new Project[] { project }, false); 198 Project[] openProjects = OpenProjects.getDefault().getOpenProjects(); 199 assertEquals("Exactly one open project should be open", 1, openProjects.length); 200 assertEquals("Another project is open", openProjects[0].getProjectDirectory(), prjRootFileObj); 201 } 202 203 208 public static final class MockProjectFactory implements ProjectFactory { 209 210 public boolean isProject(FileObject projectDirectory) { 211 return projectDirectory.isFolder(); 212 } 213 214 public Project loadProject(FileObject projectDirectory, ProjectState state) throws IOException { 215 return new MockProject(projectDirectory); 216 } 217 218 public void saveProject(Project project) throws IOException , ClassCastException { 219 } 220 221 } 222 223 228 private static final class MockProject implements Project { 229 230 private final FileObject p; 231 232 public MockProject(FileObject p) { 233 this.p = p; 234 } 235 236 public FileObject getProjectDirectory() { 237 return p; 238 } 239 240 public Lookup getLookup() { 241 return p.getName().equals(PRJ_WITH_SEARCH_INFO) 242 ? Lookups.fixed(this, new MockSearchInfo()) 243 : Lookups.singleton(this); 244 } 245 246 } 247 248 252 private static final class MockSearchInfo implements SearchInfo { 253 254 public boolean canSearch() { 255 return true; 256 } 257 258 public Iterator objectsToSearch() { 259 return Collections.emptyList().iterator(); 260 } 261 } 262 263 } | Popular Tags |