1 19 20 package org.netbeans.modules.search.project; 21 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.io.IOException ; 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 46 public class OpenProjectCLITest extends NbTestCase { 47 File dir; 48 FileObject fo; 49 50 public OpenProjectCLITest(String testName) { 51 super(testName); 52 } 53 54 protected boolean runInEQ() { 55 return true; 56 } 57 58 protected void setUp() throws Exception { 59 dir = new File (getWorkDir(), "tstdir"); 60 dir.mkdirs(); 61 File nb = new File (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 { 73 } 74 75 public void testOpenProjectFolder() throws Exception { 76 CommandLine.getDefault().process(new String [] { "--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 title, String rootTitle, Node root, NodeAcceptor acceptor, Component 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 { 122 return new MockProject(projectDirectory); 123 } 124 125 public void saveProject(Project project) throws IOException , ClassCastException { 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 |