KickJava   Java API By Example, From Geeks To Geeks.

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


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.search.project;
21
22 import java.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import org.netbeans.api.project.Project;
26 import org.netbeans.api.project.ProjectManager;
27 import org.netbeans.api.project.ui.OpenProjects;
28 import org.netbeans.api.sendopts.CommandLine;
29 import org.netbeans.junit.MockServices;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.spi.project.ProjectFactory;
32 import org.netbeans.spi.project.ProjectState;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.nodes.Node;
36 import org.openide.nodes.NodeAcceptor;
37 import org.openide.nodes.NodeOperation;
38 import org.openide.util.Lookup;
39 import org.openide.util.UserCancelException;
40 import org.openide.util.lookup.Lookups;
41
42 /**
43  *
44  * @author Jaroslav Tulach
45  */

46 public class OpenProjectCLITest extends NbTestCase {
47     File JavaDoc dir;
48     FileObject fo;
49     
50     public OpenProjectCLITest(String JavaDoc testName) {
51         super(testName);
52     }
53     
54     protected boolean runInEQ() {
55         return true;
56     }
57     
58     protected void setUp() throws Exception JavaDoc {
59         dir = new File JavaDoc(getWorkDir(), "tstdir");
60         dir.mkdirs();
61         File JavaDoc nb = new File JavaDoc(dir, "nbproject");
62         nb.mkdirs();
63
64         MockServices.setServices(MockNodeOperation.class, MockProjectFactory.class);
65         MockNodeOperation.explored = null;
66         
67         fo = FileUtil.toFileObject(dir);
68         assertTrue("This is a project folder", ProjectManager.getDefault().isProject(fo));
69         
70     }
71
72     protected void tearDown() throws Exception JavaDoc {
73     }
74     
75     public void testOpenProjectFolder() throws Exception JavaDoc {
76         CommandLine.getDefault().process(new String JavaDoc[] { "--open", dir.getPath() });
77         assertNull("No explorer called", MockNodeOperation.explored);
78
79         Project p = OpenProjects.getDefault().getMainProject();
80         assertNotNull("There is a main project", p);
81         if (!(p instanceof MockProject)) {
82             fail("Wrong project: " + p);
83         }
84         MockProject mp = (MockProject)p;
85         
86         assertEquals("It is our dir", fo, mp.p);
87     }
88     
89     public static final class MockNodeOperation extends NodeOperation {
90         public static Node explored;
91         
92         public boolean customize(Node n) {
93             fail("No customize");
94             return false;
95         }
96
97         public void explore(Node n) {
98             assertNull("No explore before", explored);
99             explored = n;
100         }
101
102         public void showProperties(Node n) {
103             fail("no props");
104         }
105
106         public void showProperties(Node[] n) {
107             fail("no props");
108         }
109
110         public Node[] select(String JavaDoc title, String JavaDoc rootTitle, Node root, NodeAcceptor acceptor, Component JavaDoc top) throws UserCancelException {
111             fail("no select");
112             return null;
113         }
114     }
115     
116     public static final class MockProjectFactory implements ProjectFactory {
117         public boolean isProject(FileObject projectDirectory) {
118             return projectDirectory.isFolder();
119         }
120
121         public Project loadProject(FileObject projectDirectory, ProjectState state) throws IOException JavaDoc {
122             return new MockProject(projectDirectory);
123         }
124
125         public void saveProject(Project project) throws IOException JavaDoc, ClassCastException JavaDoc {
126         }
127     }
128     
129     private static final class MockProject implements Project {
130         private final FileObject p;
131         
132         public MockProject(FileObject p) {
133             this.p = p;
134         }
135         
136         public FileObject getProjectDirectory() {
137             return p;
138         }
139
140         public Lookup getLookup() {
141             return Lookups.singleton(this);
142         }
143         
144     }
145 }
146
Popular Tags