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.search.project;
043:
044: import java.awt.Component;
045: import java.awt.Dialog;
046: import java.io.File;
047: import java.io.IOException;
048: import javax.swing.SwingUtilities;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.api.project.ProjectManager;
051: import org.netbeans.api.project.ui.OpenProjects;
052: import org.netbeans.api.sendopts.CommandException;
053: import org.netbeans.api.sendopts.CommandLine;
054: import org.netbeans.junit.MockServices;
055: import org.netbeans.junit.NbTestCase;
056: import org.netbeans.spi.project.ProjectFactory;
057: import org.netbeans.spi.project.ProjectState;
058: import org.openide.DialogDescriptor;
059: import org.openide.DialogDisplayer;
060: import org.openide.NotifyDescriptor;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.nodes.Node;
064: import org.openide.nodes.NodeAcceptor;
065: import org.openide.nodes.NodeOperation;
066: import org.openide.util.Lookup;
067: import org.openide.util.UserCancelException;
068: import org.openide.util.lookup.Lookups;
069:
070: /**
071: *
072: * @author Jaroslav Tulach
073: */
074: public class OpenProjectCLITest extends NbTestCase {
075: File dir;
076: FileObject fo;
077:
078: public OpenProjectCLITest(String testName) {
079: super (testName);
080: }
081:
082: protected boolean runInEQ() {
083: return false;
084: }
085:
086: protected void setUp() throws Exception {
087: dir = new File(getWorkDir(), "tstdir");
088: dir.mkdirs();
089: File nb = new File(dir, "nbproject");
090: nb.mkdirs();
091:
092: MockServices.setServices(DD.class, MockNodeOperation.class,
093: MockProjectFactory.class);
094: MockNodeOperation.explored = null;
095:
096: fo = FileUtil.toFileObject(dir);
097: assertTrue("This is a project folder", ProjectManager
098: .getDefault().isProject(fo));
099:
100: OpenProjects.getDefault().close(
101: OpenProjects.getDefault().getOpenProjects());
102: assertEquals("No open projects", 0, OpenProjects.getDefault()
103: .getOpenProjects().length);
104: DD.prev = null;
105: DD.cnt = 0;
106: }
107:
108: protected void tearDown() throws Exception {
109: }
110:
111: public void testOpenProjectFolder() throws Exception {
112: CommandLine.getDefault().process(
113: new String[] { "--open", dir.getPath() });
114: assertNull("No explorer called", MockNodeOperation.explored);
115:
116: Project p = OpenProjects.getDefault().getMainProject();
117: assertNotNull("There is a main project", p);
118: if (!(p instanceof MockProject)) {
119: fail("Wrong project: " + p);
120: }
121: MockProject mp = (MockProject) p;
122:
123: assertEquals("It is our dir", fo, mp.p);
124: }
125:
126: public void testOpenBrokenAndProjectFolder() throws Exception {
127: File nonExist = new File(dir, "IDoNotExist");
128: assertFalse(nonExist.exists());
129: File nonExist2 = new File(dir, "IDoNotExistEither");
130: assertFalse(nonExist2.exists());
131:
132: try {
133: CommandLine.getDefault().process(
134: new String[] { "--open", nonExist2.getPath(),
135: nonExist.getPath(), dir.getPath() });
136: fail("One project does not exists, should fail");
137: } catch (CommandException ex) {
138: if (ex.getLocalizedMessage().indexOf(nonExist.getName()) == -1) {
139: fail("Messages shall contain " + nonExist + "\n"
140: + ex.getMessage());
141: }
142: }
143: assertNull("No explorer called", MockNodeOperation.explored);
144:
145: Project p = OpenProjects.getDefault().getMainProject();
146: assertNotNull("There is a main project", p);
147: if (!(p instanceof MockProject)) {
148: fail("Wrong project: " + p);
149: }
150: MockProject mp = (MockProject) p;
151:
152: assertEquals("It is our dir", fo, mp.p);
153:
154: SwingUtilities.invokeAndWait(new Runnable() {
155: public void run() {
156: }
157: });
158:
159: assertNotNull("Failure notified", DD.prev);
160: assertEquals("Just once", 1, DD.cnt);
161: }
162:
163: public static final class DD extends DialogDisplayer {
164: static int cnt;
165: static NotifyDescriptor prev;
166:
167: public Object notify(NotifyDescriptor descriptor) {
168: cnt++;
169: prev = descriptor;
170: return NotifyDescriptor.OK_OPTION;
171: }
172:
173: public Dialog createDialog(DialogDescriptor descriptor) {
174: throw new UnsupportedOperationException(
175: "Not supported yet.");
176: }
177:
178: }
179:
180: public static final class MockNodeOperation extends NodeOperation {
181: public static Node explored;
182:
183: public boolean customize(Node n) {
184: fail("No customize");
185: return false;
186: }
187:
188: public void explore(Node n) {
189: assertNull("No explore before", explored);
190: explored = n;
191: }
192:
193: public void showProperties(Node n) {
194: fail("no props");
195: }
196:
197: public void showProperties(Node[] n) {
198: fail("no props");
199: }
200:
201: public Node[] select(String title, String rootTitle, Node root,
202: NodeAcceptor acceptor, Component top)
203: throws UserCancelException {
204: fail("no select");
205: return null;
206: }
207: }
208:
209: public static final class MockProjectFactory implements
210: ProjectFactory {
211: public boolean isProject(FileObject projectDirectory) {
212: return projectDirectory.isFolder();
213: }
214:
215: public Project loadProject(FileObject projectDirectory,
216: ProjectState state) throws IOException {
217: return new MockProject(projectDirectory);
218: }
219:
220: public void saveProject(Project project) throws IOException,
221: ClassCastException {
222: }
223: }
224:
225: private static final class MockProject implements Project {
226: private final FileObject p;
227:
228: public MockProject(FileObject p) {
229: this .p = p;
230: }
231:
232: public FileObject getProjectDirectory() {
233: return p;
234: }
235:
236: public Lookup getLookup() {
237: return Lookups.singleton(this);
238: }
239:
240: }
241: }
|