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

import java.io.File;
import java.awt.event.InputEvent;

import org.netbeans.junit.NbTestSuite; 
import org.netbeans.junit.NbTestCase; 
import junit.framework.Test;
import junit.framework.TestSuite;

import org.netbeans.jellytools.ExplorerOperator; 
import org.netbeans.jellytools.EditorOperator; 
import org.netbeans.jellytools.EditorWindowOperator; 
import org.netbeans.jellytools.JellyTestCase;
import org.netbeans.jellytools.MainWindowOperator;
import org.netbeans.jellytools.NbFrameOperator;

import org.netbeans.jellytools.actions.CompileAllAction;

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

import org.netbeans.jemmy.operators.JPopupMenuOperator;

import org.netbeans.web.test.actions.AddBreakpointAction;
import org.netbeans.web.test.actions.ContinueDebugAction;
import org.netbeans.web.test.actions.StartDebugAction;

import org.netbeans.test.gui.web.util.CommonUtils;
import org.netbeans.test.gui.web.util.CompilationUtils;

import org.netbeans.web.test.util.Utils;
import org.netbeans.web.test.util.StatusWaitable;
import org.netbeans.web.test.nodes.JSPNode;

/**
 * Collection of tests corresponding "Debugging Process with Java-Breakpoints" part of JSP/Servlet Debug specification.
 * @author Vladimir Strigun
 */
public class JavaDebugging extends JellyTestCase { 

    private static ExplorerOperator explorer= null;
    private EditorOperator editor = null;
    private static String workDir = null;
    private static String webModule = null;
    private static String wmName = "wm";
    private static boolean informDialogsAlreadyOpened = false;
    private static boolean compilationDialogAlreadyOpened = false;
    private static String testWMDir = "javadebug";
    public static final String delim = "|";

    public JavaDebugging(String testName) { 
        super(testName); 
    } 

    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }

    public static Test suite(){
        TestSuite suite = new NbTestSuite(JavaDebugging.class);
	workDir = System.getProperty("jspservletdebug.workdir").replace('/', File.separatorChar);
	webModule = workDir + File.separatorChar + wmName;
        workDir = webModule+delim+testWMDir;
        explorer = ExplorerOperator.invoke();
        explorer.selectPageFilesystems();
        FilesystemNode tmpNode = null;
        Utils.handleDialogAfterNewWebModule();
        tmpNode = new FilesystemNode(workDir);
        FormNode fNode = new FormNode(tmpNode, "jsp1");
        (new CompileAllAction()).performMenu();
        String res = CompilationUtils.checkResultOfBuildAll(workDir, testWMDir);
        if(res!=null)
            System.out.println("FAILED: cannot compile all jsp's!!! "+res);
        explorer = ExplorerOperator.invoke();
        explorer.selectPageFilesystems();
        Utils.setSwingBrowser();
        return suite;
    }

    /** 
        Check debugger stopped on "one method breakpoint"
    */

    public void testStopsOnOneMethodBreakpoint() {
        editor = Utils.openFile(workDir, "jsp1");
        JSPNode jspNode = new JSPNode(workDir+delim+"jsp1");
        jspNode.viewServlet();

        EditorWindowOperator editorWindow = new EditorWindowOperator("Servlet for jsp1");
        editor = editorWindow.selectPage("Servlet for jsp1");
        editor.setCaretPositionToLine(56);
        (new AddBreakpointAction()).performMenu(); 
        Utils.fillAddBreakpointMethod(null, null, null, false);
        if(!Utils.isExplorerItemExist("Debugger|Breakpoints|Method org.apache.jsp.jsp1$jsp._jspService")){
            fail("Breakpoint do not added");
        }
        editor = Utils.openFile(workDir, "jsp1");
        (new StartDebugAction()).performMenu(); 
        handleDialogsBeforeDebuggerStart();
        checkDebuggerStoppedOnMethod("jsp1", "_jspService", 25);
        (MainWindowOperator.getDefault()).getToolbarButton((MainWindowOperator.getDefault()).getToolbar("Debug"), "Continue").push();
        String res = CommonUtils.checkCorrectJSPOutput("jsp1", "Just a simple JSP file 1. Test passed.", testWMDir);
        Utils.finishDebugger();
        if(res!=null) fail(res);
    }


    /** 
        Check debugger stopped on "all methods breakpoint"
    */

    public void testStopsOnAllMethodsBreakpoint() {
        editor = Utils.openFile(workDir, "jsp2");
        JSPNode jspNode = new JSPNode(workDir+delim+"jsp2");
        jspNode.viewServlet();

        EditorWindowOperator editorWindow = new EditorWindowOperator("Servlet for jsp2");
        editor = editorWindow.selectPage("Servlet for jsp2");
        editor.setCaretPositionToLine(56);
        (new AddBreakpointAction()).performMenu(); 
        Utils.fillAddBreakpointMethod("org.apache.jsp", null, null, true);
        if(!Utils.isExplorerItemExist("Debugger|Breakpoints|Methods in org.apache.jsp.jsp2$jsp")){
            fail("Breakpoint do not added");
        }
        editor = Utils.openFile(workDir, "jsp2");
        (new StartDebugAction()).performMenu(); 
        handleDialogsBeforeDebuggerStart();
        checkDebuggerStoppedOnMethod("jsp2", "", 17);
        (MainWindowOperator.getDefault()).getToolbarButton((MainWindowOperator.getDefault()).getToolbar("Debug"), "Continue").push();
        checkDebuggerStoppedOnMethod("jsp2", "", 14);
        (MainWindowOperator.getDefault()).getToolbarButton((MainWindowOperator.getDefault()).getToolbar("Debug"), "Continue").push();
        checkDebuggerStoppedOnMethod("jsp2", "_jspService", 25);
        (MainWindowOperator.getDefault()).getToolbarButton((MainWindowOperator.getDefault()).getToolbar("Debug"), "Continue").push();
        checkDebuggerStoppedOnMethod("jsp2", "_jspx_init", 20);
        (MainWindowOperator.getDefault()).getToolbarButton((MainWindowOperator.getDefault()).getToolbar("Debug"), "Continue").push();
        String res = CommonUtils.checkCorrectJSPOutput("jsp2", "Just a simple JSP file 2. Test passed.", testWMDir);
        Utils.finishDebugger();
        if(res!=null) fail(res);
    }


    /** 
        Check debugger stopped on "exception caught breakpoint"
    */

    public void testStopsOnExceptionCaughtBreakpoint() {
        editor = Utils.openFile(workDir, "jsp3");
        JSPNode jspNode = new JSPNode(workDir+delim+"jsp3");
        jspNode.viewServlet();
        EditorWindowOperator editorWindow = new EditorWindowOperator("Servlet for jsp3");
        editor = editorWindow.selectPage("Servlet for jsp3");
        editor.setCaretPositionToLine(56);
        (new AddBreakpointAction()).performMenu(); 
        Utils.fillAddBreakpointException(null, "IndexOutOfBoundsException", 0);
        if(!Utils.isExplorerItemExist("Debugger|Breakpoints|Exception java.lang.IndexOutOfBoundsException caught")){
            fail("Breakpoint do not added");
        }
        editor = Utils.openFile(workDir, "jsp3");
        (new StartDebugAction()).performMenu(); 
        handleDialogsBeforeDebuggerStart();
        for(int j=0; j<3; j++){
            if(checkDebuggerStoppedOnExceptionCaught("org.apache.xerces", "java.lang.ArrayIndexOutOfBoundsException", "?", false)!=null)
                (MainWindowOperator.getDefault()).getToolbarButton((MainWindowOperator.getDefault()).getToolbar("Debug"), "Continue").push();
        }
        checkDebuggerStoppedOnExceptionCaught("org.apache.jsp.jsp3$jsp", "java.lang.IndexOutOfBoundsException", "71", true);
        //String res = CommonUtils.checkCorrectJSPOutput("jsp2", "Just a simple JSP file 2. Test passed.", testWMDir);
        //if(res!=null) fail(res);
        Utils.finishDebugger();
    }


    /** 
        Check debugger stopped on "variable access breakpoint"
    */

    public void testStopsOnVariableAccessBreakpoint() {
        Utils.deleteAllBreakpoints();
        editor = Utils.openFile(workDir, "jsp4");
        JSPNode jspNode = new JSPNode(workDir+delim+"jsp4");
        jspNode.viewServlet();
        EditorWindowOperator editorWindow = new EditorWindowOperator("Servlet for jsp4");
        editor = editorWindow.selectPage("Servlet for jsp4");
        editor.setCaretPositionToLine(12);
        editor.select(12, 13, 13);
        (new AddBreakpointAction()).performMenu(); 
        Utils.fillAddBreakpointVariable("org.apache.jsp", null, null, 0);
        if(!Utils.isExplorerItemExist("Debugger|Breakpoints|Variable org.apache.jsp.jsp4$jsp.j access")){
            fail("Breakpoint do not added");
        }
        editor = Utils.openFile(workDir, "jsp4");
        (new StartDebugAction()).performMenu(); 
        handleDialogsBeforeDebuggerStart();
        checkDebuggerStoppedOnVariable("Variable j access (?).");
        (MainWindowOperator.getDefault()).getToolbarButton((MainWindowOperator.getDefault()).getToolbar("Debug"), "Continue").push();

        String res = CommonUtils.checkCorrectJSPOutput("jsp4", "Just a simple JSP file 4. Test passed.", testWMDir);
        Utils.finishDebugger();
        if(res!=null) fail(res);
    }


    /** 
        Check debugger stopped on "variable modification breakpoint"
    */

    public void testStopsOnVariableModificationBreakpoint() {
        Utils.deleteAllBreakpoints();
        editor = Utils.openFile(workDir, "jsp5");
        JSPNode jspNode = new JSPNode(workDir+delim+"jsp5");
        jspNode.viewServlet();
        EditorWindowOperator editorWindow = new EditorWindowOperator("Servlet for jsp5");
        editor = editorWindow.selectPage("Servlet for jsp5");
        editor.setCaretPositionToLine(68);
        editor.select(68, 17, 17);
        (new AddBreakpointAction()).performMenu(); 
        Utils.fillAddBreakpointVariable("org.apache.jsp", null, null, -1);
        if(!Utils.isExplorerItemExist("Debugger|Breakpoints|Variable org.apache.jsp.jsp5$jsp.j modification")){
            fail("Breakpoint do not added");
        }
        editor = Utils.openFile(workDir, "jsp5");
        (new StartDebugAction()).performMenu(); 
        handleDialogsBeforeDebuggerStart();

            checkDebuggerStoppedOnVariable("Variable j modification (?).");
            (MainWindowOperator.getDefault()).getToolbarButton((MainWindowOperator.getDefault()).getToolbar("Debug"), "Continue").push();
            checkDebuggerStoppedOnVariable("Variable j modification (0).");
            (new ContinueDebugAction()).performMenu();

        String res = CommonUtils.checkCorrectJSPOutput("jsp5", "Just a simple JSP file 5. Test passed.", testWMDir);
        Utils.finishDebugger();
        if(res!=null) fail(res);
    }


    /** 
        Check debugger stopped on "variable modification&access breakpoints"
    */

    public void testStopsOnVariableModificationAccessBreakpoints() {
        editor = Utils.openFile(workDir, "jsp5");
        boolean addBreakpoint = false;
        if(!Utils.isExplorerItemExist("Debugger|Breakpoints|Variable org.apache.jsp.jsp5$jsp.j modification")){
            addBreakpoint = true;
        }
        editor = Utils.openFile(workDir, "jsp5");
        JSPNode jspNode = new JSPNode(workDir+delim+"jsp5");
        jspNode.viewServlet();
        EditorWindowOperator editorWindow = new EditorWindowOperator("Servlet for jsp5");
        editor = editorWindow.selectPage("Servlet for jsp5");
        editor.setCaretPositionToLine(68);
        editor.select(68, 17, 17);
        (new AddBreakpointAction()).performMenu(); 
        Utils.fillAddBreakpointVariable("org.apache.jsp", null, null, 0);
        if(!Utils.isExplorerItemExist("Debugger|Breakpoints|Variable org.apache.jsp.jsp5$jsp.j access")){
            fail("Breakpoint do not added");
        }
        if(addBreakpoint){
            editorWindow = new EditorWindowOperator("Servlet for jsp5");
            editor = editorWindow.selectPage("Servlet for jsp5");
            editor.setCaretPositionToLine(68);
            editor.select(68, 17, 17);
            (new AddBreakpointAction()).performMenu(); 
            Utils.fillAddBreakpointVariable("org.apache.jsp", null, null, -1);
            if(!Utils.isExplorerItemExist("Debugger|Breakpoints|Variable org.apache.jsp.jsp5$jsp.j modification")){
                fail("Breakpoint do not added");
            }
        }
        editor = Utils.openFile(workDir, "jsp5");
        (new StartDebugAction()).performMenu(); 
        try{
            handleDialogsBeforeDebuggerStart();

            checkDebuggerStoppedOnVariable("Variable j modification (?).");
            (new ContinueDebugAction()).performMenu();
            //(MainWindowOperator.getDefault()).getToolbarButton((MainWindowOperator.getDefault()).getToolbar("Debug"), "Continue").push();

            checkDebuggerStoppedOnVariable("Variable j access (0).");
            (new ContinueDebugAction()).performMenu();

            checkDebuggerStoppedOnVariable("Variable j modification (0).");
            (new ContinueDebugAction()).performMenu();
            //(MainWindowOperator.getDefault()).getToolbarButton((MainWindowOperator.getDefault()).getToolbar("Debug"), "Continue").push();

            String res = CommonUtils.checkCorrectJSPOutput("jsp5", "Just a simple JSP file 5. Test passed.", testWMDir);
            Utils.finishDebugger();
            if(res!=null) fail(res);
        }catch(Exception e){
            System.out.println("Exception occured during execution of testStopsOnVariableModificationAccessBreakpoints  Exception is:"+e);
            e.printStackTrace();
            Utils.finishDebugger();
            fail("Exception occured during execution. See log for details.");
        }
    }


    /** 
        Check debugger stopped on "thread start breakpoint"
    */

    public void testStopsOnThreadStartBreakpoint() {
        Utils.deleteAllBreakpoints();
        editor = Utils.openFile(workDir, "jsp6");
        JSPNode jspNode = new JSPNode(workDir+delim+"jsp6");
        jspNode.viewServlet();
        EditorWindowOperator editorWindow = new EditorWindowOperator("Servlet for jsp6");
        editor = editorWindow.selectPage("Servlet for jsp6");
        editor.setCaretPositionToLine(56);
        (new AddBreakpointAction()).performMenu(); 
        Utils.fillAddBreakpointThread(0);
        if(!Utils.isExplorerItemExist("Debugger|Breakpoints|Thread start")){
            fail("Breakpoint do not added");
        }
        editor = Utils.openFile(workDir, "jsp6");
        (new StartDebugAction()).performMenu(); 
        handleDialogsBeforeDebuggerStart();
        StatusWaitable csw = new StatusWaitable("Thread", "started.");
        String message = "start breakpoint.";
        waitDebuggerStopOnThread(csw, message, "jsp6", "Just a simple JSP file 6. Test passed.");
        Utils.finishDebugger();
    }


    /** 
        Check debugger stopped on "thread death breakpoint"
    */

    public void testStopsOnThreadDeathBreakpoint() {
        Utils.deleteAllBreakpoints();
        editor = Utils.openFile(workDir, "jsp6");
        JSPNode jspNode = new JSPNode(workDir+delim+"jsp6");
        jspNode.viewServlet();
        EditorWindowOperator editorWindow = new EditorWindowOperator("Servlet for jsp6");
        editor = editorWindow.selectPage("Servlet for jsp6");
        editor.setCaretPositionToLine(56);
        (new AddBreakpointAction()).performMenu(); 
        Utils.fillAddBreakpointThread(1);
        if(!Utils.isExplorerItemExist("Debugger|Breakpoints|Thread death")){
            fail("Breakpoint do not added");
        }
        editor = Utils.openFile(workDir, "jsp6");
        (new StartDebugAction()).performMenu(); 
        handleDialogsBeforeDebuggerStart();
        StatusWaitable csw = new StatusWaitable("Thread", "death.");
        String message = "death breakpoint.";
        waitDebuggerStopOnThread(csw, message, "jsp6", "Just a simple JSP file 6. Test passed.");
        Utils.finishDebugger();
    }


    /** 
        Check debugger stopped on "thread start / death breakpoint"
    */

    public void testStopsOnThreadStartDeathBreakpoint() {
        Utils.deleteAllBreakpoints();
        editor = Utils.openFile(workDir, "jsp7");
        JSPNode jspNode = new JSPNode(workDir+delim+"jsp7");
        jspNode.viewServlet();
        EditorWindowOperator editorWindow = new EditorWindowOperator("Servlet for jsp7");
        editor = editorWindow.selectPage("Servlet for jsp7");
        editor.setCaretPositionToLine(56);
        (new AddBreakpointAction()).performMenu(); 
        Utils.fillAddBreakpointThread(2);
        if(!Utils.isExplorerItemExist("Debugger|Breakpoints|Thread start / death")){
            fail("Breakpoint do not added");
        }
        editor = Utils.openFile(workDir, "jsp7");
        (new StartDebugAction()).performMenu(); 
        handleDialogsBeforeDebuggerStart();
        StatusWaitable csw = new StatusWaitable("Thread", "started.", "death.");
        String message = "start / death breakpoint.";
        waitDebuggerStopOnThread(csw, message, "jsp7", "Just a simple JSP file 7. Test passed.");
        Utils.finishDebugger();

    }


    /*
      The part of private function needed for this testbag
    */

    private void handleDialogsBeforeDebuggerStart(){
        if(!compilationDialogAlreadyOpened) {
            String res = CommonUtils.handleJSPCompilationDialog();
            if(res!=null){ 
                if(res.indexOf("not opened")==-1) fail(res);
            }else{
                compilationDialogAlreadyOpened = true;
            }
        }
        if(!informDialogsAlreadyOpened){
            String res = CommonUtils.handleDialogsBeforeDebuggerStart();
            if(res!=null){ 
                if(res.indexOf("not opened")==-1) fail(res);
            }else{
                informDialogsAlreadyOpened = true;
            }
        }
    }

    private void waitDebuggerStopOnThread(StatusWaitable sw, String message, String fileName, String fileContent){
        boolean debuggerStop = false;
        boolean jspLoaded = false;
        int counter = 0;
        StatusWaitable csw = sw;
        while (!jspLoaded){
            String result = Utils.waitDebuggerStop(csw);
            if(result==null){
                debuggerStop = true;
                try{
                    (new ContinueDebugAction()).performMenu();
                }catch(Exception e){
                  if(e instanceof org.netbeans.jemmy.TimeoutExpiredException){
                      System.out.println("Cannot find \"Continue\" item in menu.");
                  }else{ 
                      fail("Exception occured while pressing \"Continue\" menu item. Exception is: "+e);
                  }
                }
            }else{
                String res = CommonUtils.checkCorrectJSPOutput(fileName, fileContent, testWMDir);
                if(res==null){ 
                    jspLoaded = true;  
                }else{ 
                   if(counter>10){ System.out.println("SVA DEBUG:: Finished when counter>10");Utils.finishDebugger(); fail(res);  } 
                }
            }
            counter++;
        }
        if(!debuggerStop){ 
            Utils.finishDebugger(); 
            fail("Debugger do not stopped on thread "+message); 
        }
    }


    private void checkDebuggerStoppedOnVariable(String str){
            String[] variants = {str};
            StatusWaitable csw = new StatusWaitable(variants, true);	
            String result = Utils.waitDebuggerStop(csw);
            if(result!=null){
                System.out.println("Error while waiting of debugger stop: "+result);
                Utils.finishDebugger();
                fail("Debugger do not stopped on variable access or modification. Wait for \""+str+"\" failed.");
            }
    }

    private void checkDebuggerStoppedOnMethod(String name, String methodName, int line) {
        String result = CommonUtils.waitDebuggerStoppedOnMethod(name, methodName, line);
        if(result!=null){
            System.out.println("Error while waiting of debugger stop: "+result);
            Utils.finishDebugger();
            fail("Debugger do not stopped on "+methodName+" in file "+name);
        }
    }

    private String checkDebuggerStoppedOnExceptionCaught(String name, String exceptionName, String line, boolean always) {
	String[] variants = {"Exception "+exceptionName+" reached at line " + line + " in class "+name};
   	StatusWaitable csw = new StatusWaitable(variants, false);	
        String result = Utils.waitDebuggerStop(csw);
        if(result!=null && always){
            System.out.println("Error while waiting of debugger stop: "+result);
            Utils.finishDebugger();
            fail("Debugger do not stopped on "+exceptionName+" in file "+name);
            return result;
        }
        return null;
    }

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