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-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.web.test.util;

import java.io.File;
import java.util.HashSet;

import javax.swing.tree.TreePath;
import javax.swing.JPopupMenu;

import org.netbeans.jellytools.actions.ActionNoBlock;
import org.netbeans.jellytools.Bundle;
import org.netbeans.jellytools.EditorOperator; 
import org.netbeans.jellytools.EditorWindowOperator; 
import org.netbeans.jellytools.OutputWindowOperator;
import org.netbeans.jellytools.ExplorerOperator;
import org.netbeans.jellytools.MainWindowOperator;
import org.netbeans.jellytools.NbDialogOperator;

import org.netbeans.jellytools.nodes.FilesystemNode;
import org.netbeans.jellytools.nodes.FormNode;

import org.netbeans.jemmy.TimeoutExpiredException;
import org.netbeans.jemmy.Timeouts;
import org.netbeans.jemmy.Waiter;
import org.netbeans.jemmy.operators.DialogOperator;
import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JComboBoxOperator;
import org.netbeans.jemmy.operators.JPopupMenuOperator;
import org.netbeans.jemmy.operators.JTextFieldOperator;
import org.netbeans.jemmy.operators.JCheckBoxOperator;
import org.netbeans.jemmy.operators.JTreeOperator;

import org.netbeans.web.test.actions.FinishDebugAction;

public class Utils {

    private static String breakpointsPath = "Debugger|Breakpoint";
    private static String delim = "|";
    private static String fSep = System.getProperty("file.separator");

    public Utils() {
    }

    public static EditorOperator openFile(String workDir, String file) {
       MainWindowOperator.getDefault().switchToEditingWorkspace();
       ExplorerOperator explorer = ExplorerOperator.invoke();
       explorer.selectPageFilesystems();
       FilesystemNode tmpNode = null;
       tmpNode = new FilesystemNode(workDir);
       FormNode fNode = new FormNode(tmpNode, file);
       fNode.open();
       EditorWindowOperator editorWindow = new EditorWindowOperator(file);
       EditorOperator editor = editorWindow.selectPage(file);
       editor.requestFocus();
       return editor;
    } 

    public static boolean isExplorerItemExist(String path) {
        ExplorerOperator explorer = ExplorerOperator.invoke();
        explorer.selectPageRuntime();
        try {
            JTreeOperator jTreeOper = explorer.runtimeTab().tree();
            Timeouts.initDefault("JTreeOperator.WaitNodeExpandedTimeout", 3000);
	    jTreeOper.findPath(path,delim);
	}catch(TimeoutExpiredException tee) {
	    return false;
	}
	return true;
    }

    public static void handleDialogAfterNewWebModule(){
	try {
	    NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.modules.web.context.Bundle", "CTL_TITLE"));
	    dialog.ok();
	}catch(Exception e) {
	    System.out.println("Alternate dialog not found");
	}
    }


    /**
     * This method checks results of compilation of .java file
     * @param node full explorer path to node
     * @return null in case of success and the reason of failure in other case.
     */

    public static String checkResultOfJavaFileCompilation(String node) {
	String name = node.substring(node.lastIndexOf(delim)+1);
	String file = toFSPath(node) + ".class";

	String[] variants = {"Finished " + name + ".","Errors compiling " + name + "."}; //NOI18N
	
	StatusWaitable csw = new StatusWaitable(variants);	
	Waiter w = new Waiter(csw);
	try {
            w.waitAction(csw);
        } catch (InterruptedException e) {
            e.printStackTrace();
	    return("Exception while waiting compilation results:" + e);
        } catch (TimeoutExpiredException e1) {
            e1.printStackTrace();
	    return("Exception while waiting compilation results:" + e1);
        }
	OutputWindowOperator owo = OutputWindowOperator.invoke();
	owo.selectCompilerPage();
	String text = owo.getText();
	if(csw.getStatus().indexOf("Error")!=-1) {
	    return("ST: " + csw.getStatus()+ " OUT: " + text);
	}
	if(!(new File(file).exists())) {
	    return("Class " + file + " is NOT appears after compilation ");
	}
	return null;
    }
    

     /**
     * This method checks results of clean of .java file
     * @param node full explorer path to node
     * @return null in case of success and a reason of failure in other case.
     */

    public static String checkResultOfJavaFileClean(String node) {
	String name = node.substring(node.lastIndexOf(delim)+1);
	String file = toFSPath(node) + ".class";

	String[] variants = {"Finished " + name + ".","Errors compiling " + name + "."};

	StatusWaitable csw = new StatusWaitable(variants);	
	Waiter w = new Waiter(csw);
	try {
            w.waitAction(csw);
        } catch (InterruptedException e) {
            e.printStackTrace();
	    return("Exception while waiting compilation results:" + e);
        } catch (TimeoutExpiredException e1) {
            e1.printStackTrace();
	    return("Exception while waiting compilation results:" + e1);
        }
	OutputWindowOperator owo = OutputWindowOperator.invoke();
	owo.selectCompilerPage();
	String text = owo.getText();
		
	if(csw.getStatus().indexOf("Error")!=-1) {
	    return("ST: " + csw.getStatus()+ " OUT: " + text);
	}
	if(new File(file).exists()) {
	    return("File " + file + " is still present after clean ");
	}
	return null;
    }


    public static String checkResultOfCleanAll(String pkg) {
	String name = pkg.substring(pkg.lastIndexOf(delim)+1);
	String file = toFSPath(pkg);

	File dir = new File(file);
	if(!dir.isDirectory()) {
	    return("Wrong parameter for checkResultOfCleanAll: " + pkg);
	}
	String[] variants = {"Finished " + name + ".","Errors compiling " + name + "."};
	StatusWaitable csw = new StatusWaitable(variants);	
	Waiter w = new Waiter(csw);
	try {
            w.waitAction(csw);
        } catch (InterruptedException e) {
            e.printStackTrace();
	    return("Exception while waiting compilation results:" + e);
        } catch (TimeoutExpiredException e1) {
            e1.printStackTrace();
	    return("Exception while waiting compilation results:" + e1);
        }

	OutputWindowOperator owo = OutputWindowOperator.invoke();
	owo.selectCompilerPage();
	String text = owo.getText();

	if(csw.getStatus().indexOf("Error")!=-1) {
	    return("ST: " + csw.getStatus()+ " OUT: " + text);
	}
	if(dir.listFiles(new ClassFileFilter(true)).length != 0) {
	    String list  = "";
	    File[] fl = dir.listFiles(new ClassFileFilter(true));
	    for(int i=0;iBreakpoints
     * node of Runtime Tab of explorer and press "Delete All" popup menu item.
     * @return true if no any exception occured while executing, otherwise return false.
     */

    public static boolean deleteAllBreakpoints() {
       boolean result = true; 
       ExplorerOperator explorer = ExplorerOperator.invoke();
       JTreeOperator jTreeOper = explorer.runtimeTab().tree();
       try{ 
           TreePath treePath = jTreeOper.findPath(breakpointsPath, delim);
           JPopupMenu popupMenu = jTreeOper.callPopupOnPath(treePath);
           JPopupMenuOperator popupOper = new JPopupMenuOperator(popupMenu);
           popupOper.pushMenu(Bundle.getStringTrimmed("org.netbeans.modules.debugger.support.actions.Bundle", "CTL_DeleteAll"), "|");
           NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.modules.debugger.support.actions.Bundle", "MSG_ConfirmDeleteAllBreakpointsTitle"));
           dialog.yes();
       }catch(Exception e){
           e.printStackTrace();
           result = false;
       }
       //explorer.switchToFilesystemsTab();
       return result;
    }

    public static void fillAddBreakpointDialog(String name, int line, String type){
        NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.modules.debugger.support.actions.Bundle", "CTL_Breakpoint_Title"));  
        if(type=="jsp/servlet")
            (new JComboBoxOperator(dialog, 0)).selectItem(Bundle.getStringTrimmed("org.netbeans.modules.web.debug.Bundle", "CTL_JSP_SERVLET_DEBUGGER_NAME")); 
        else
            (new JComboBoxOperator(dialog, 0)).selectItem(Bundle.getStringTrimmed("org.netbeans.modules.web.debug.Bundle", "CTL_JSP_DEBUGGER_NAME")); 
        JTextFieldOperator lineField = new JTextFieldOperator(dialog, 0); 
        if(line!=0){
            lineField.clearText();
            lineField.typeText((new Integer(line)).toString());
        }
        if(name!=null){
            JTextFieldOperator fileField = new JTextFieldOperator(dialog, 1); 
            fileField.typeText(name);
        }
        //wait(3000);
        dialog.ok();
        //new JButtonOperator(dialog, "OK").doClick();
    }

    public static void fillAddBreakpointMethod(String packageName, String className, String methodName, boolean allMethods) {
        NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.modules.debugger.support.actions.Bundle", "CTL_Breakpoint_Title"));  
        (new JComboBoxOperator(dialog, 1)).selectItem(Bundle.getStringTrimmed("org.netbeans.modules.debugger.support.java.Bundle", "CTL_Method_event_type_name"));
        if(packageName!=null){
            JTextFieldOperator packageField = new JTextFieldOperator(dialog, 0); 
            packageField.typeText(packageName);
        }
        if(className!=null){
            JTextFieldOperator classField = new JTextFieldOperator(dialog, 1); 
            classField.typeText(className);
        }
        if(methodName!=null){
            JTextFieldOperator methodField = new JTextFieldOperator(dialog, 2); 
//            methodField.setText(methodName);
            methodField.typeText(methodName);
        }
        if(allMethods){
            JCheckBoxOperator allclassesField = new JCheckBoxOperator(dialog, 1); 
            allclassesField.push();
        }
        wait(3000);
        dialog.ok();
        //new JButtonOperator(dialog, "OK").doClick();
    }

    public static void fillAddBreakpointClass(String packageName, String className, int classPrepare) {
        NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.modules.debugger.support.actions.Bundle", "CTL_Breakpoint_Title"));  
        (new JComboBoxOperator(dialog, 1)).selectItem(Bundle.getStringTrimmed("org.netbeans.modules.debugger.jpda.Bundle", "CTL_Class_event_type_name"));
        if(packageName!=null){
            JTextFieldOperator packageField = new JTextFieldOperator(dialog, 0); 
            packageField.setText(packageName);
        }
        if(className!=null){
            JTextFieldOperator classField = new JTextFieldOperator(dialog, 1); 
            classField.setText(className);
        }
        if(classPrepare!=-1){
            (new JComboBoxOperator(dialog, 4)).selectItem(classPrepare); 
        }
        wait(3000);
        dialog.ok();
        //new JButtonOperator(dialog, "OK").doClick();
    }


    public static void fillAddBreakpointThread(int threadStart) {
        NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.modules.debugger.support.actions.Bundle", "CTL_Breakpoint_Title"));  
        (new JComboBoxOperator(dialog, 1)).selectItem(Bundle.getStringTrimmed("org.netbeans.modules.debugger.jpda.Bundle", "CTL_Thread_event_type_name"));
        if(threadStart!=-1){
            (new JComboBoxOperator(dialog, 2)).selectItem(threadStart); 
        }
        wait(3000);
        dialog.ok();
        //new JButtonOperator(dialog, "OK").doClick();
    }

    public static void fillAddBreakpointVariable(String packageName, String className, String variableName, int stopOn) {
        NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.modules.debugger.support.actions.Bundle", "CTL_Breakpoint_Title"));  
        if(!(packageName==null & className==null & variableName==null & stopOn ==-1 ))
        (new JComboBoxOperator(dialog, 1)).selectItem(Bundle.getStringTrimmed("org.netbeans.modules.debugger.jpda.Bundle", "CTL_Variable_event_type_name"));
        if(packageName!=null){
            JTextFieldOperator packageField = new JTextFieldOperator(dialog, 0); 
            packageField.typeText(packageName);
        }
        if(className!=null){
            JTextFieldOperator classField = new JTextFieldOperator(dialog, 1); 
            classField.typeText(className);
        }
        if(variableName!=null){
            JTextFieldOperator methodField = new JTextFieldOperator(dialog, 2); 
            methodField.typeText(variableName);
        }
        if(stopOn!=-1){
            (new JComboBoxOperator(dialog, 5)).selectItem(stopOn); 
        }
        wait(3000);
        dialog.ok();
        //new JButtonOperator(dialog, "OK").doClick();
    }

    public static void fillAddBreakpointException(String packageName, String exceptionName, int stopOn) {
        NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.modules.debugger.support.actions.Bundle", "CTL_Breakpoint_Title"));  
        (new JComboBoxOperator(dialog, 1)).selectItem(Bundle.getStringTrimmed("org.netbeans.modules.debugger.jpda.Bundle", "CTL_Exception_event_name_type_name"));
        if(packageName!=null){
            JTextFieldOperator packageField = new JTextFieldOperator(dialog, 0); 
            packageField.typeText(packageName);
        }
        if(exceptionName!=null){
            JTextFieldOperator exceptionField = new JTextFieldOperator(dialog, 1); 
            exceptionField.typeText(exceptionName);
        }
        if(stopOn!=-1){
            (new JComboBoxOperator(dialog, 4)).setSelectedIndex(stopOn); 
        }
        wait(5000);
        dialog.ok();
        //new JButtonOperator(dialog, "OK").doClick();
    }


    public static void finishDebugger(){
        try{
            (new FinishDebugAction()).performMenu();
            (new Thread () {
                 public void run() {
                     try {
                         NbDialogOperator nbDialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.modules.debugger.multisession.Bundle", "CTL_Finish_debugging_dialog"));
                         nbDialog.ok();
                     } catch (Exception e) {
                     }
                 }
            }).run();
        } catch(org.netbeans.jemmy.TimeoutExpiredException timeEx) {
            System.out.println("Catched TimeoutExpiredException when trying to finish debugger. May be correct???");
             timeEx.printStackTrace();
        } 
    }

    public static void setSwingBrowser(){
        new ActionNoBlock(Bundle.getStringTrimmed("org.netbeans.core.Bundle", "Menu/Tools")+ delim +Bundle.getStringTrimmed("org.netbeans.core.ui.Bundle", "CTL_SetupWizardTitle"), null).perform();
        wait(5000);
        NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.core.ui.Bundle", "CTL_SetupWizardTitle"));
        (new JComboBoxOperator(dialog, 0)).setSelectedItem(Bundle.getStringTrimmed("org.netbeans.beaninfo.Bundle", "CTL_SwingBrowser"));
        new JButtonOperator(dialog, Bundle.getStringTrimmed("org.openide.Bundle","CTL_FINISH")).doClick();
    } 

    public static String waitDebuggerStop(StatusWaitable csw){
	Waiter w = new Waiter(csw);
	try {
            w.waitAction(csw);
        } catch (InterruptedException e) {
            e.printStackTrace();
	    return("Exception while waiting debugger stop:" + e);
        } catch (TimeoutExpiredException e1) {
            e1.printStackTrace();
	    return("Exception while waiting debugger stop:" + e1);
        }
        return null;
    }

    public static void wait(int millisec) {
        try {
            Thread.sleep(millisec);
        } catch(Exception e) {}
    }
    
    public static String toFSPath(String explorerPath) {
	StringBuffer sb = new StringBuffer(explorerPath);
	int ind = explorerPath.indexOf("Classes"); //NOI18N
	if(ind != -1) {
	    sb.setCharAt(ind,'c'); //Explorer's "Classes" to FS "classes"
	}
	return sb.toString().replace(delim.charAt(0),fSep.charAt(0));
    }
}




... 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.