alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
/*
 * OpenTestAction.java
 *
 * Created on February 28, 2001, 7:08 PM
 */

package org.netbeans.modules.junit;

import java.net.URL;

import java.util.ArrayList;
import javax.swing.Action;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.java.queries.UnitTestForSourceQuery;
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
import org.openide.ErrorManager;
import org.openide.cookies.EditorCookie;
import org.openide.cookies.SourceCookie;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.nodes.Node;
import org.openide.src.ClassElement;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CookieAction;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.ImageIcon;
import org.openide.util.Utilities;

/** Action sensitive to some DataFolder, SourceCookie or ClassElement cookie
 * which tries to open JUnit test corresponding to the selected soruce file.
 *
 * @author  Nathan W. Phelps, David Konecny
 * @ version 1.0
 */
public class OpenTestAction extends CookieAction {

    protected Class[] cookieClasses () {
        return new Class[] { DataFolder.class, SourceCookie.class, ClassElement.class };
    }

    public boolean asynchronous() {
        return false;
    }

    protected int mode () {
        return MODE_ALL;
    }

    protected void performAction (Node[] nodes) {
        FileObject selectedFO;
        
        for (int i = 0; i < nodes.length; i++) {
            // get test class or suite class file, if it was not such one pointed by the node
            selectedFO = TestUtil.getFileObjectFromNode(nodes[i]);
            if (selectedFO == null) {
                TestUtil.notifyUser(NbBundle.getMessage(OpenTestAction.class, "MSG_file_from_node_failed"));
                continue;
            }
            ClassPath cp = ClassPath.getClassPath(selectedFO, ClassPath.SOURCE);
            if (cp == null) {
                TestUtil.notifyUser(NbBundle.getMessage(OpenTestAction.class, 
                    "MSG_no_project", selectedFO));
                continue;
            }
            FileObject packageRoot = cp.findOwnerRoot(selectedFO);
            String resource = cp.getResourceName(selectedFO, '/', false);
            
            URL testRoot = UnitTestForSourceQuery.findUnitTest(packageRoot);
            ClassPath testClassPath = null;
            if (testRoot == null) {
                // no tests, use sources instead
                testClassPath = cp;
            } else {
                ArrayList cpItems = new ArrayList();
                cpItems.add(ClassPathSupport.createResource(testRoot));
                testClassPath = ClassPathSupport.createClassPath(cpItems);
            }
            String testName = null;
            if (selectedFO.isFolder()) {
                // find Suite for package
                testName = TestUtil.convertPackage2SuiteName(resource);
            } else {
                // find Test for class
                testName = TestUtil.convertClass2TestName(resource);
            }
            FileObject fileToOpen = testClassPath.findResource(testName+".java");
            if (fileToOpen != null) {
                openFile(fileToOpen);
            } else {
                TestUtil.notifyUser(NbBundle.getMessage(OpenTestAction.class,
                    selectedFO.isFolder() ? "MSG_testsuite_class_not_found" : 
                    "MSG_test_class_not_found", testName, resource));
                continue;
            }
        }
    }

    /**
     * Open given file in editor.
     * @return true if file was opened or false
     */
    private boolean openFile(FileObject fo) {
        DataObject dobj;
        try {
            dobj = DataObject.find(fo);
        } catch (DataObjectNotFoundException e) {
            ErrorManager.getDefault().log(ErrorManager.ERROR, e.toString());
            return false;
        }
        if (dobj == null) {
             return false;
         }
         EditorCookie cookie = (EditorCookie)dobj.getCookie(EditorCookie.class);
         if (cookie == null) {
             return false;
         }
         cookie.open();
         return true;
    }
    
    public String getName () {
        return NbBundle.getMessage (OpenTestAction.class, "LBL_Action_OpenTest");
    }

//     protected String iconResource () {
//         return "org/netbeans/modules/junit/resources/OpenTestActionIcon.gif";
//     }

    public HelpCtx getHelpCtx () {
        return new HelpCtx(OpenTestAction.class);
    }

    /** Perform special enablement check in addition to the normal one.
    protected boolean enable (Node[] nodes) {
	if (! super.enable (nodes)) return false;
	if (...) ...;
    }
    */

    protected void initialize () {
	super.initialize ();
        putProperty(Action.SHORT_DESCRIPTION, NbBundle.getMessage(OpenTestAction.class, "HINT_Action_OpenTest"));
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.