KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > project > ProjectsSearchActionTest


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.search.project;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Iterator JavaDoc;
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 /**
45  *
46  * @author Marian Petras
47  * @author Jaroslav Tulach
48  */

49 public class ProjectsSearchActionTest extends NbTestCase {
50     
51     private static final String JavaDoc PRJ_WITH_SEARCH_INFO = "prjWithSearchInfo";
52     private static final String JavaDoc PRJ_WITHOUT_SEARCH_INFO = "prjWithoutSearchInfo";
53     
54     private WorkDirCleaner workDirCleaner;
55     private File JavaDoc prjRootDir;
56     private FileObject prjRootFileObj;
57     
58     public ProjectsSearchActionTest(String JavaDoc testName) {
59         super(testName);
60     }
61     
62     @Override JavaDoc
63     protected boolean runInEQ() {
64         return true;
65     }
66     
67     /**
68      * Mostly copied from <code>OpenProjectCLITest.java</code>.
69      *
70      * @author Jaroslav Tulach
71      * @author Marian Petras
72      */

73     protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
92         private IOException JavaDoc ioExc;
93         public void run() {
94             try {
95                 clearWorkDir();
96             } catch (IOException JavaDoc ex) {
97                 this.ioExc = ex;
98             }
99         }
100     }
101     
102     protected void tearDown() throws Exception JavaDoc {
103         super.tearDown();
104     }
105     
106     /**
107      * Tests that when action <em>Find in Projects</em> is invoked and there is
108      * a project with {@code SearchInfo} in its {@code Lookup}, that
109      * {@code SearchInfo} is used by the action.
110      */

111     public void testProjectWithSearchInfo() throws IOException JavaDoc {
112         checkWorkDirCleaner();
113         
114         openProject(PRJ_WITH_SEARCH_INFO);
115         
116         Collection JavaDoc<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     /**
123      * Tests that when action <em>Find in Projects</em> is invoked, there is
124      * an project opened and the project does not contain {@code SearchInfo}
125      * in its {@code Lookup}, a default {@code SearchInfo} is used.
126      */

127     public void testProjectWithoutSearchInfo() throws IOException JavaDoc {
128         checkWorkDirCleaner();
129         
130         openProject(PRJ_WITHOUT_SEARCH_INFO);
131         
132         Collection JavaDoc<SearchInfo> searchInfos = getSearchInfoToBeUsed();
133         assertFalse("No SearchInfo used by ProjectsSearchAction", searchInfos.isEmpty());
134         
135         for (SearchInfo searchInfo : searchInfos) {
136             String JavaDoc searchInfoClassName = searchInfo.getClass().getName();
137             if (!searchInfoClassName.startsWith("org.openidex.search.")) {
138                 fail("Unexpected SearchInfo used by ProjectsSearchAction: " + searchInfoClassName);
139             }
140         }
141     }
142     
143     /**
144      */

145     private void checkWorkDirCleaner() {
146         if ((workDirCleaner != null) && (workDirCleaner.ioExc != null)) {
147             fail("WorkDirCleaner thrown an exception: " + workDirCleaner.ioExc);
148         }
149     }
150     
151     /**
152      */

153     private Collection JavaDoc<SearchInfo> getSearchInfoToBeUsed() {
154         ProjectsSearchAction prjSearchAction = SystemAction.get(ProjectsSearchAction.class);
155         assertNotNull("ProjectsSearchAction not found", prjSearchAction);
156         
157         prjSearchAction.isEnabled(); //the first call of 'isEnabled()' always returns 'false'
158
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 JavaDoc<SearchInfo> result = new ArrayList JavaDoc<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     /**
175      *
176      */

177     private void openProject(String JavaDoc prjDirName) throws IOException JavaDoc {
178         assert EventQueue.isDispatchThread();
179         
180         prjRootDir = new File JavaDoc(getWorkDir(), prjDirName);
181         prjRootDir.mkdirs();
182         
183         new File JavaDoc(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 JavaDoc 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     /**
204      * Copied from <code>OpenProjectCLITest.java</code>.
205      *
206      * @author Jaroslav Tulach
207      */

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 JavaDoc {
215             return new MockProject(projectDirectory);
216         }
217
218         public void saveProject(Project project) throws IOException JavaDoc, ClassCastException JavaDoc {
219         }
220         
221     }
222     
223     /**
224      * Copied from <code>OpenProjectCLITest.java</code>.
225      *
226      * @author Jaroslav Tulach
227      */

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     /**
249      *
250      * @author Marian Petras
251      */

252     private static final class MockSearchInfo implements SearchInfo {
253         
254         public boolean canSearch() {
255             return true;
256         }
257
258         public Iterator JavaDoc objectsToSearch() {
259             return Collections.emptyList().iterator();
260         }
261 }
262     
263 }
Popular Tags