1 19 20 package org.netbeans.modules.junit; 21 22 import java.util.*; 23 import javax.swing.Action ; 24 import org.netbeans.api.java.classpath.ClassPath; 25 import org.netbeans.api.project.FileOwnerQuery; 26 import org.netbeans.api.project.Project; 27 import org.netbeans.api.project.SourceGroup; 28 import org.netbeans.modules.junit.plugin.JUnitPlugin; 29 import org.netbeans.modules.junit.plugin.JUnitPlugin.CreateTestParam; 30 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 31 import org.openide.DialogDisplayer; 32 import org.openide.NotifyDescriptor; 33 import org.openide.filesystems.FileObject; 34 import org.openide.loaders.DataObject; 35 import org.openide.loaders.DataObjectNotFoundException; 36 import org.openide.nodes.Node; 37 import org.openide.util.HelpCtx; 38 import org.openide.util.NbBundle; 39 import org.openide.cookies.EditorCookie; 40 41 47 public final class CreateTestAction extends TestAction { 48 49 public CreateTestAction() { 50 putValue("noIconInMenu", Boolean.TRUE); } 52 53 54 public String getName() { 55 return NbBundle.getMessage(CreateTestAction.class, 56 "LBL_Action_CreateTest"); } 58 59 public HelpCtx getHelpCtx() { 60 return new HelpCtx(CreateTestAction.class); 61 } 62 63 protected void initialize() { 64 super.initialize(); 65 putProperty(Action.SHORT_DESCRIPTION, 66 NbBundle.getMessage(CreateTestAction.class, 67 "HINT_Action_CreateTest")); } 69 70 protected String iconResource() { 71 return "org/netbeans/modules/junit/resources/" + "CreateTestActionIcon.gif"; } 74 75 80 private static String checkNodesValidity(Node[] nodes) { 81 FileObject[] files = getFiles(nodes); 82 83 Project project = getProject(files); 84 if (project == null) { 85 return NbBundle.getMessage(CreateTestAction.class, 86 "MSG_multiproject_selection"); } 88 89 if (!checkPackages(files)) { 90 return NbBundle.getMessage(CreateTestAction.class, 91 "MSG_invalid_packages"); } 93 94 return null; 95 } 96 97 102 private static boolean checkPackages(FileObject[] files) { 103 if (files.length == 0) { 104 return true; 105 } else { 106 Project project = FileOwnerQuery.getOwner(files[0]); 107 for (int i = 0 ; i < files.length; i++) { 108 String packageName = getPackage(project, files[i]); 109 if ((packageName == null) 110 || !TestUtil.isValidPackageName(packageName)) { 111 return false; 112 } 113 } 114 return true; 115 } 116 } 117 118 125 private static String getPackage(Project project, FileObject file) { 126 SourceGroup srcGrp = TestUtil.findSourceGroupOwner(project, file); 127 if (srcGrp!= null) { 128 ClassPath cp = ClassPathSupport.createClassPath( 129 new FileObject [] {srcGrp.getRootFolder()}); 130 return cp.getResourceName(file, '.', false); 131 } else { 132 return null; 133 } 134 } 135 136 137 private static FileObject[] getFiles(Node[] nodes) { 138 FileObject[] ret = new FileObject[nodes.length]; 139 for (int i = 0 ; i < nodes.length ; i++) { 140 ret[i] = TestUtil.getFileObjectFromNode(nodes[i]); 141 } 142 return ret; 143 } 144 145 150 private static Project getProject(FileObject[] files) { 151 Project project = null; 152 for (int i = 0 ; i < files.length; i++) { 153 Project nodeProject = FileOwnerQuery.getOwner(files[i]); 154 if (project == null) { 155 project = nodeProject; 156 } else if (project != nodeProject) { 157 return null; 158 } 159 } 160 return project; 161 } 162 163 @Override 164 protected void performAction(Node[] nodes) { 165 String problem; 166 if ((problem = checkNodesValidity(nodes)) != null) { 167 NotifyDescriptor msg = new NotifyDescriptor.Message( 169 problem, NotifyDescriptor.WARNING_MESSAGE); 170 DialogDisplayer.getDefault().notify(msg); 171 return; 172 } 173 174 JUnitCfgOfCreate cfg = new JUnitCfgOfCreate(nodes); 177 if (!cfg.configure()) { 178 return; 179 } 180 181 182 final boolean singleClass = cfg.isSingleClass(); 183 final Map<CreateTestParam, Object > params 184 = TestUtil.getSettingsMap(!singleClass); 185 if (singleClass) { 186 params.put(CreateTestParam.CLASS_NAME, cfg.getTestClassName()); 187 } 188 final FileObject targetFolder = cfg.getTargetFolder(); 189 cfg = null; 190 191 FileObject[] filesToTest = getFileObjectsFromNodes(nodes); 192 if (filesToTest == null) { 193 return; } 195 196 197 final JUnitPlugin plugin = TestUtil.getPluginForProject( 198 FileOwnerQuery.getOwner(filesToTest[0])); 199 200 201 final FileObject[] testFileObjects 202 = JUnitPluginTrampoline.DEFAULT.createTests( 203 plugin, 204 filesToTest, 205 targetFolder, 206 params); 207 208 209 if (testFileObjects.length == 1) { 210 try { 211 DataObject dobj = DataObject.find(testFileObjects[0]); 212 EditorCookie ec = dobj.getCookie(EditorCookie.class); 213 if (ec != null) { 214 ec.open(); 215 } 216 } catch (DataObjectNotFoundException ex) { 217 ex.printStackTrace(); 218 } 219 } 220 } 221 222 231 private static FileObject[] getFileObjectsFromNodes(final Node[] nodes){ 232 FileObject[] fileObjects = new FileObject[nodes.length]; 233 List<FileObject> fileObjectsList = null; 234 235 for (int i = 0; i < nodes.length; i++) { 236 final Node node = nodes[i]; 237 final FileObject fo; 238 if (!hasParentAmongNodes(nodes, i) 239 && ((fo = getTestFileObject(node)) != null)) { 240 if (fileObjects != null) { 241 fileObjects[i] = fo; 242 } else { 243 if (fileObjectsList == null) { 244 fileObjectsList = new ArrayList<FileObject>( 245 nodes.length - i); 246 } 247 fileObjectsList.add(fo); 248 } 249 } else { 250 fileObjects = null; } 252 } 253 if (fileObjects == null) { 254 if (fileObjectsList != null) { 255 fileObjects = fileObjectsList.toArray( 256 new FileObject[fileObjectsList.size()]); 257 fileObjectsList = null; 258 } 259 } 260 261 return fileObjects; 262 } 263 264 273 private static FileObject getTestFileObject(final Node node) { 274 final FileObject fo = TestUtil.getFileObjectFromNode(node); 275 if (fo == null) { 276 TestUtil.notifyUser(NbBundle.getMessage( 277 CreateTestAction.class, 278 "MSG_file_from_node_failed")); return null; 280 } 281 ClassPath cp = ClassPath.getClassPath(fo, ClassPath.SOURCE); 282 if (cp == null) { 283 TestUtil.notifyUser(NbBundle.getMessage( 284 CreateTestAction.class, 285 "MSG_no_project", fo)); 287 return null; 288 } 289 return fo; 290 } 291 292 private static boolean hasParentAmongNodes(final Node[] nodes, 293 final int idx) { 294 Node node; 295 296 node = nodes[idx].getParentNode(); 297 while (null != node) { 298 for (int i = 0; i < nodes.length; i++) { 299 if (i == idx) { 300 continue; 301 } 302 if (node == nodes[i]) { 303 return true; 304 } 305 } 306 node = node.getParentNode(); 307 } 308 return false; 309 } 310 311 } 312 | Popular Tags |