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

package org.netbeans.test.gui.web.util;


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

import org.netbeans.core.ui.SwingBrowser;

import org.netbeans.jellytools.Bundle;
import org.netbeans.jellytools.EditorOperator; 
import org.netbeans.jellytools.EditorWindowOperator; 
import org.netbeans.jellytools.ExplorerOperator;
import org.netbeans.jellytools.MainWindowOperator; 
import org.netbeans.jellytools.NbDialogOperator;
import org.netbeans.jellytools.NbFrameOperator;
import org.netbeans.jellytools.OptionsOperator;
import org.netbeans.jellytools.OutputWindowOperator;
import org.netbeans.jellytools.TreeTableOperator;

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

import org.netbeans.jellytools.properties.ComboBoxProperty;
import org.netbeans.jellytools.properties.PropertySheetOperator;
import org.netbeans.jellytools.properties.PropertySheetTabOperator;
import org.netbeans.jellytools.properties.TextFieldProperty;

import org.netbeans.jemmy.Timeouts;
import org.netbeans.jemmy.TimeoutExpiredException;
import org.netbeans.jemmy.Waiter;
import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JPopupMenuOperator;
import org.netbeans.jemmy.operators.JTextComponentOperator;
import org.netbeans.jemmy.operators.JTreeOperator;

import org.netbeans.web.test.actions.ToggleBreakpointAction;
import org.netbeans.web.test.util.Utils;
import org.netbeans.web.test.util.StatusWaitable;

/**
 * Common Utilites for JSP/Servlet Debug tests.
 * @author Vladimir Strigun
 */
public class CommonUtils {
    //private static String alternateViewTitle =  "Mount Web Module";
    private static String finishDebugDialogTitle = "Finish Debugging Sessions";
    private static String informationDialogTitle = "Information";
    private static String port = "8081";
    private static String fSep = "/";
    private static String breakpointsPath = "Debugger|Breakpoint";
    private static String delim = "|";

    public CommonUtils() {
    }

    /**
     * This tests close dialogs that appears before debugger start.
     * Two dialogs are handled: 
     * 
    *
  1. "Internal HTTP server not running" dialog. It's informed user that * internal server not running yet. *
  2. *
  3. "Information" dialog. It's informed user that internal web browser * do not support some functions. *
  4. *
      * @return null if no exception occured while waiting dialogs. Othrwise return non-empty string. */ public static String handleDialogsBeforeDebuggerStart(){ try { NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.modules.web.monitor.client.Bundle", "MON_NoServerTitle")); dialog.ok(); }catch(Exception e) { if(e instanceof org.netbeans.jemmy.TimeoutExpiredException){ System.out.println("HTTP server dialog do not opened"); } else { System.out.println("Debugging: Exception while waiting information dialog: "+e); return "Exception occured while waiting information dialog"; } } try { NbDialogOperator dialog = new NbDialogOperator(informationDialogTitle); dialog.ok(); } catch(Exception e) { if(e instanceof org.netbeans.jemmy.TimeoutExpiredException){ return("Information dialog do not opened"); } else { System.out.println("Debugging: Exception while waiting information dialog: "+e); return "Exception occured while waiting information dialog"; } } return null; } public static String handleJSPCompilationDialog(){ try { NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.netbeans.modules.web.core.Bundle", "CTL_Ask_if_compile_Title")); String alwaysCompileCaption = Bundle.getStringTrimmed("org.netbeans.modules.web.core.Bundle", "CTL_DLG_DebugCompile_AlwaysCompile"); JButtonOperator btAlwaysCompile = new JButtonOperator(dialog, alwaysCompileCaption); btAlwaysCompile.push(); }catch(Exception e) { if(e instanceof org.netbeans.jemmy.TimeoutExpiredException){ System.out.println("Compilation dialog do not opened"); return "not opened"; } else { System.out.println("Debugging: Exception while waiting compilation dialog: "+e); return "Exception occured while waiting compilation dialog"; } } return null; } //Begin of part of service functions to manipulate breakpoints /** * This method check whether servlet breakpoint exist for specified * file name and line. * Method return true if breakpoint is exist in Debugger->Breakpoints * tree, otherwise methos return false. */ public static boolean isServletBreakpointExist(String fileName, String servletName, int jspLineNumber, int servletLineNumber, boolean isDeferred) { boolean result = true; ExplorerOperator explorer = ExplorerOperator.invoke(); explorer.selectPageRuntime(); try { String path = null; if(!isDeferred) path = breakpointsPath+delim+"JSP "+fileName+".jsp:"+jspLineNumber+delim+servletName+":"+servletLineNumber; else path = breakpointsPath+delim+"JSP "+fileName+".jsp:"+jspLineNumber+" deferred"+delim+servletName+":"+servletLineNumber; JTreeOperator operator = explorer.runtimeTab().tree(); System.out.println("isServletBreakpointExist:: full path is:"+path); operator.findPath(path,delim); }catch(TimeoutExpiredException tee) { result = false; } explorer.selectPageFilesystems(); return result; } /** * This method remove all Breakpoints. It's invoke popup menu on Debugger->Breakpoints * 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"), "|"); }catch(Exception e){ e.printStackTrace(); result = false; } return result; } /** * This method check whether all breakpoints removed. * It's try to open Runtime Tab of explorer and check that Breakpoints node * do not have any leafs. * @return true if all breakpoints removed, otherwise return false. */ public static boolean isAllBreakpointsRemoved(){ ExplorerOperator explorer = ExplorerOperator.invoke(); explorer.selectPageRuntime(); try{ JTreeOperator operator = explorer.runtimeTab().tree(); TreePath treePath = operator.findPath(breakpointsPath, delim); int childCount = operator.getChildCount(treePath); if(childCount==0) return true; else return false; }catch(Exception e){ System.out.println("Exception occured:"+e.toString()); return false; } } /** * This method open breakpoint properties window through Runtime * tab of explorer. * @param fileName defines file name (use "jsp1" name format instead of "jsp1.jsp"). * @param lineNumber defines line number of breakpoint. * @param isDeferred boolean value defines whether breakpoint deferred or not. * @return true if breakpoint if correct breakpoint exist in Debugger->Breakpoints * tree, otherwise methos return false. */ public static boolean getPropertiesWindowForBreakpoint(String fileName, int lineNumber, boolean isDeferred){ boolean result = true; String fullName = null; fullName = "JSP "+fileName+".jsp:"+lineNumber; if(isDeferred){ fullName = fullName+" deferred"; } ExplorerOperator explorer = ExplorerOperator.invoke(); JTreeOperator jTreeOper = explorer.runtimeTab().tree(); Timeouts.initDefault("JTreeOperator.WaitNodeExpandedTimeout", 3000); try{ //System.out.println("SVA DEBUG:: getPropertiesWindow for breakpoint="+fullName); TreePath treePath = jTreeOper.findPath(breakpointsPath+delim+fullName, delim); JPopupMenu popupMenu = jTreeOper.callPopupOnPath(treePath); JPopupMenuOperator popupOper = new JPopupMenuOperator(popupMenu); popupOper.pushMenu("Properties", "|"); PropertySheetOperator properties = PropertySheetOperator.invoke(); PropertySheetTabOperator propertiesSheet = new PropertySheetTabOperator(properties, "Properties"); TextFieldProperty lineProperty = new TextFieldProperty(propertiesSheet, "Line Number"); ComboBoxProperty stateProperty = new ComboBoxProperty(propertiesSheet, "Enabled"); String line = lineProperty.getValue(); String state = stateProperty.getValue(); if(state.equals("False") | !line.equals((new Integer(lineNumber)).toString())){ result = false; } properties.close(); }catch(Exception e){ e.printStackTrace(); result = false; } return result; } /** * This method delete breakpoint through Runtime Tab of Explorer. * Method delete jsp breakpoint (with correspondent servlet breakpoint) if * servletLine != 0, otherwise it delete only servlet breakpoint for current * jsp breakpoint. * @param fileName defines file name (use "jsp1" name format instead of "jsp1.jsp"). * @param servletName defines servlet name. * @param lineNumber defines line number of breakpoint. * @param servletLine defines line of breakpoint in generated servlet. * @param isDeferred boolean value defines whether breakpoint deferred or not. * @return true if breakpoint removed and false otherwise. */ public static boolean deleteBreakpoint(String fileName, String servletName, int lineNumber, int servletLine, boolean isDeferred) { boolean result = true; String fullName = null; fullName = "JSP "+fileName+".jsp:"+lineNumber; if(isDeferred){ fullName = fullName+" deferred"; } ExplorerOperator explorer = ExplorerOperator.invoke(); JTreeOperator jTreeOper = explorer.runtimeTab().tree(); try{ String breakpointNode = breakpointsPath+delim+fullName; if(servletLine!=0) breakpointNode += delim+servletName+":"+servletLine; TreePath treePath = jTreeOper.findPath(breakpointNode, delim); JPopupMenu popupMenu = jTreeOper.callPopupOnPath(treePath); JPopupMenuOperator popupOper = new JPopupMenuOperator(popupMenu); popupOper.pushMenu("Delete", "|"); NbDialogOperator dialog = new NbDialogOperator(Bundle.getStringTrimmed("org.openide.explorer.Bundle", "MSG_ConfirmDeleteObjectTitle")); dialog.yes(); }catch(Exception e){ e.printStackTrace(); result = false; } return result; } /** * This method check whether breakpoint exist. If breakpoint do not exist, * it's add breakpoint by ToggleBreakpoint action. * This method can add deferred breakpoint. * @param name defines file name (use "jsp1" name format instead of "jsp1.jsp"). * @param breakpointFileName defines full name of jsp file (with directory). * @param lineNumber defines line number of breakpoint. * @param servletLine defines line of breakpoint in generated servlet. * @param isDeferred boolean value defines whether breakpoint deferred or not. If this parameter true, method will add deferred breakpoint. * @param workDir defines working directory for current jsp file. */ public static void checkBreakpointExist(String name, String breakpointFileName, int line, int servletLine, boolean isDeferred, String workDir) { String fullName = null; fullName = "JSP "+breakpointFileName+".jsp:"+line; if(isDeferred){ fullName = fullName+" deferred"; } if(!Utils.isExplorerItemExist(breakpointsPath+delim+fullName)){ EditorOperator editor = Utils.openFile(workDir, name); if(isDeferred) { editor.clickForPopup(100, 100); new JPopupMenuOperator().pushMenu("Show code in Servlet", "|"); EditorWindowOperator editorWindow = new EditorWindowOperator("Servlet for "+name); editor = editorWindow.selectPage("Servlet for "+name); editor.setCaretPositionToLine(servletLine); (new ToggleBreakpointAction()).performShortcut(); } else { editor.setCaretPositionToLine(line); (new ToggleBreakpointAction()).performShortcut(); } return; } } /** * This method invoke popup menu on breakpoint specified by paramaters and * click on "Go to source" menu item. Also it's check that cursor placed on * correct line of JSP of generated servlet. * @param fileName defines file name (use "jsp1" name format instead of "jsp1.jsp"). * @param servletName defines servlet name. * @param lineNumber defines line number of breakpoint. * @param servletLine defines line of breakpoint in generated servlet. * @param isDeferred boolean value defines whether breakpoint deferred or not. * @param toServlet * @return true if no exception occured while execution. */ //Should be modified!!!!! public static boolean goToSource(String fileName, String servletName, int lineNumber, int servletLine, boolean isDeferred, boolean toServlet) { boolean result = true; String fullName = null; fullName = "JSP "+fileName+".jsp:"+lineNumber; if(isDeferred){ fullName = fullName+" deferred"; } ExplorerOperator explorer = ExplorerOperator.invoke(); JTreeOperator jTreeOper = explorer.runtimeTab().tree(); try{ String fullPath = breakpointsPath+delim+fullName; if(toServlet) fullPath += delim+servletName+":"+servletLine; TreePath treePath = jTreeOper.findPath(fullPath, delim); JPopupMenu popupMenu = jTreeOper.callPopupOnPath(treePath); JPopupMenuOperator popupOper = new JPopupMenuOperator(popupMenu); popupOper.pushMenu(Bundle.getStringTrimmed("org.netbeans.modules.debugger.support.actions.Bundle", "CTL_GoToSource"), "|"); }catch(Exception e){ e.printStackTrace(); result = false; } try{ EditorWindowOperator editorWindow = new EditorWindowOperator("Servlet for "+fileName.substring(fileName.indexOf("/")+1)); EditorOperator editor = editorWindow.selectPage("Servlet for "+fileName.substring(fileName.indexOf("/")+1)); int position = editor.getLineNumber(); if(position != servletLine) result = false; }catch(Exception e){ e.printStackTrace(); result = false; } return result; } //End of part of service functions to manipulate breakpoints /** * This method check whether compilation successful or not. * It's open Output Window, select compiler tab and check that * "Finished fileName" text is exist inside. * @param fileName defines file name (use "jsp1" name format instead of "jsp1.jsp"). * @return true if compilation successful and false otherwise. */ public static boolean isCompilationSuccessful(String fileName){ (new OutputWindowOperator()).selectCompilerPage(); String text = (new OutputWindowOperator()).getText(); if(text.indexOf("Finished "+fileName+".")!=-1){ return true; } return false; } /** * This method check whether compilation break successful or not. * It's open Output Window, select compiler tab and check that * "Compilation interrupted" text is exist inside. * @param fileName defines file name (use "jsp1" name format instead of "jsp1.jsp"). * @return true if compilation break successful and false otherwise. */ public static boolean isCompilationBreakSuccessful(String fileName){ (new OutputWindowOperator()).selectCompilerPage(); String text = (new OutputWindowOperator()).getText(); if(text.indexOf("Compilation Interrupted\nErrors compiling "+fileName+".")!=-1){ return true; } return false; } /** * Method wait while correct jsp will be loaded in internal web browser. * @param name specify jsp name (use "jsp1" name format instead of "jsp1.jsp"). * @param workDir specify working directory. * @return null if file loaded in browser correctly and non-empty string otherwise. */ public static String waitCorrectJSPLoaded(String name, String workDir){ String url = "http://localhost:"+port+"/"+workDir+"/"+name+".jsp"; URLLoadedWaitable urlLoader = new URLLoadedWaitable(url); Waiter w = new Waiter(urlLoader); try { w.waitAction(urlLoader); } catch (InterruptedException e) { e.printStackTrace(); return("Exception while waiting loading URL:" + e); } catch (TimeoutExpiredException e1) { e1.printStackTrace(); return("Exception while waiting loading URL:" + e1); } return null; } /** * Method check whethet correct content of jsp displayed in internal web browser. * @param name specify jsp name (use "jsp1" name format instead of "jsp1.jsp"). * @param correctOut specify a string that should be displayed in browser. * @param workDir specify working directory. * @return null if file correct content of jsp displayed in browser correctly and non-empty string otherwise. */ public static String checkCorrectJSPOutput(String fileName, String correctOut, String workDir){ String result = waitCorrectJSPLoaded(fileName, workDir); if(result!=null){ System.out.println("Waitable of JSP loaded return:"+result); Utils.finishDebugger(); return "Cannot load correct JSP in browser"; } NbFrameOperator browser=new NbFrameOperator("Web Browser"); Utils.wait(3000); String currOut = (new JTextComponentOperator(browser, 0)).getText(); if(currOut.indexOf(correctOut)==-1){ browser.close(); System.out.println("DEBUG:: checkCorrectJSPOutput procedure, correctOut="+correctOut); return "Web browser displays incorrect JSP file"; } //browser.close(); return null; } public static void pressRefreshInBrowser(){ NbFrameOperator browser=new NbFrameOperator("Web Browser"); Utils.wait(3000); browser.getFocus(); (new JButtonOperator(browser, 3)).push(); } /** * Method set "Skip Static Lines" option. * It's open Tools->Options , select "Skip Static Lines" options and sets * value defined by staticLines variable. * @param staticLines boolean variable to be set. */ public static void selectStaticLines(boolean staticLines) { MainWindowOperator.getDefault().switchToEditingWorkspace(); OptionsOperator optOper = OptionsOperator.invoke(); TreeTableOperator treetable = optOper.treeTable(); int i = optOper.selectOption("Debugging and Executing"); i = optOper.selectOption("Debugging and Executing|"+Bundle.getString("org.netbeans.modules.web.core.Bundle", "LBL_ServletSettingsNode")); Utils.wait(2000); PropertySheetOperator properties = PropertySheetOperator.invoke(); PropertySheetTabOperator propertiesSheet = new PropertySheetTabOperator(properties, "Properties"); ComboBoxProperty linesProperty = new ComboBoxProperty(propertiesSheet, "Skip Static Lines"); if(staticLines) linesProperty.setValue("True"); else linesProperty.setValue("False"); properties.close(); optOper.close(); } /** * Method set "Compile JSPs before debugging" option. * It's open Tools->Options , select "Compile JSPs before debugging" options and sets * value defined by value variable. * @param velue int variable to be set.
      * Please, use following items: *
        *
      • 0 - set "Compile JSPs before debugging to "Yes"
      • *
      • 1 - set "Compile JSPs before debugging to "No"
      • *
      • 2 - set "Compile JSPs before debugging to "Always ask"
      • *
      */ public static void setCompileJSPsBeforeDebug(int value) { OptionsOperator optOper = OptionsOperator.invoke(); TreeTableOperator treetable = optOper.treeTable(); int i = optOper.selectOption("Debugging and Executing"); i = optOper.selectOption("Debugging and Executing|"+Bundle.getString("org.netbeans.modules.web.core.Bundle", "LBL_ServletSettingsNode")); PropertySheetOperator properties = PropertySheetOperator.invoke(); PropertySheetTabOperator propertiesSheet = new PropertySheetTabOperator(properties, "Properties"); ComboBoxProperty compileProperty = new ComboBoxProperty(propertiesSheet, "Compile JSPs before debugging"); compileProperty.setValue(value); //this waiter should be removed!!! Utils.wait(2000); optOper.close(); } // begin a part of waiters /** * Wait while debugger stop when breakpoint reached. * @param name specify jsp name (use "jsp1" name format instead of "jsp1.jsp"). * @param line specify line number to stop on. * @return null if debugger stops on breakpoint or non-empty string otherwise. */ //TODO: add check of line coloring public static String waitDebuggerStoppedOnBreakpoint(String name, int line) { String className = "org.apache.jsp."+name+"$jsp"; String[] variants = {"Breakpoint reached at line " + line + " in class "+className}; StatusWaitable csw = new StatusWaitable(variants, false); String result = Utils.waitDebuggerStop(csw); (new OutputWindowOperator()).selectPage("Debugger Console"); String text = (new OutputWindowOperator()).getText(); System.out.println("SVA DEBUG:: text="+text); if(text.indexOf("Breakpoint reached at line " + line + " in class "+className)!=-1){ return null; } return result; } /** * Wait while debugger stop when method reached. * @param jspName specify jsp name (use "jsp1" name format instead of "jsp1.jsp"). * @param methodName specify a method name. * @param line specify line number to stop on. * @return null if debugger stops on method or non-empty string otherwise. */ public static String waitDebuggerStoppedOnMethod(String jspName, String methodName, int line){ String className = "org.apache.jsp."+jspName+"$jsp"; String[] variants = {"Method "+methodName+" reached at line " + line + " in class "+className}; StatusWaitable csw = new StatusWaitable(variants, false); return Utils.waitDebuggerStop(csw); } /** * Wait while debugger stop when exception caught. * @param jspName specify jsp name (use "jsp1" name format instead of "jsp1.jsp"). * @param exceptionName specify a exception name. * @param line specify line number to stop on. * @return null if debugger stops on "exception caught" or non-empty string otherwise. */ public static String waitDebuggerStoppedOnExceptionCaught(String name, String exceptionName, String line) { String className = "org.apache.jsp."+name+"$jsp"; String[] variants = {"Exception "+exceptionName+" reached at line " + line + " in class "+className}; StatusWaitable csw = new StatusWaitable(variants, false); return Utils.waitDebuggerStop(csw); } // end a part of waiters }
... 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.