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

package org.netbeans.test.web;

import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLConnection;
import junit.framework.AssertionFailedError;
import org.netbeans.jellytools.nodes.SourcePackagesNode;
import org.netbeans.jellytools.properties.PropertySheetOperator;
import org.netbeans.jemmy.EventTool;
import org.netbeans.jemmy.JemmyProperties;
import org.netbeans.jemmy.TimeoutExpiredException;
import org.netbeans.jemmy.Waitable;
import org.netbeans.jemmy.Waiter;
import org.netbeans.jemmy.util.PNGEncoder;
import org.netbeans.junit.NbTestSuite;
import org.netbeans.jemmy.operators.*;
import org.netbeans.jellytools.WizardOperator;
import org.netbeans.jellytools.nodes.Node;
import org.netbeans.jellytools.actions.ActionNoBlock;
import org.netbeans.jellytools.actions.Action;
import org.netbeans.jellytools.*;
import org.netbeans.junit.ide.ProjectSupport;
import org.netbeans.test.web.Util;

/**
 */
public class WebProjectValidation extends JellyTestCase {
    
    // name of sample project
    protected String SAMPLE_PROJECT_NAME = "SampleProject"; // NOI18N
    
    protected static final String SAMPLE_PROJECT_FOLDER = System.getProperty("netbeans.user");
    //protected static final String SAMPLE_PROJECT_FOLDER = "d:\\tmp";
    
    private static final String runItem = Bundle.getStringTrimmed("org.netbeans.modules.project.ui.Bundle", "Menu/RunProject");
    private static final String toggleBrakpointItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_Toggle_breakpoint");

    private Throwable exception = null;
    
    /** Need to be defined because of JUnit */
    public WebProjectValidation(String name) { 
        super(name);
    }

    public static NbTestSuite suite() {
        NbTestSuite suite = new NbTestSuite();
        suite.addTest(new WebProjectValidation("testNewWebProject"));
        suite.addTest(new WebProjectValidation("testNewJSP"));
        suite.addTest(new WebProjectValidation("testNewJSP2"));
        suite.addTest(new WebProjectValidation("testNewServlet"));
        suite.addTest(new WebProjectValidation("testNewServlet2"));
        suite.addTest(new WebProjectValidation("testBuildProject"));
        suite.addTest(new WebProjectValidation("testCompileAllJSP"));
        suite.addTest(new WebProjectValidation("testCompileJSP"));
        suite.addTest(new WebProjectValidation("testCleanProject"));
        suite.addTest(new WebProjectValidation("testRunProject"));
        suite.addTest(new WebProjectValidation("testRunJSP"));
        suite.addTest(new WebProjectValidation("testRunServlet"));
        suite.addTest(new WebProjectValidation("testDebugProject"));
        suite.addTest(new WebProjectValidation("testRunToCursor"));
        suite.addTest(new WebProjectValidation("testDebugServlet"));
        suite.addTest(new WebProjectValidation("testCreateTLD"));
        suite.addTest(new WebProjectValidation("testCreateTagHandler"));
        suite.addTest(new WebProjectValidation("testRunTag"));
        suite.addTest(new WebProjectValidation("testDebugTag"));
        suite.addTest(new WebProjectValidation("testNewHTML"));
        suite.addTest(new WebProjectValidation("testRunHTML"));
        suite.addTest(new WebProjectValidation("testNewSegment"));
        suite.addTest(new WebProjectValidation("testNewDocument"));  
        suite.addTest(new WebProjectValidation("testStopTomcat"));
        suite.addTest(new WebProjectValidation("testStartTomcat"));
        suite.addTest(new WebProjectValidation("testBrowserSettings"));
        suite.addTest(new WebProjectValidation("testFinish"));
        return suite;     
    }
    
    /** Use for execution inside IDE */
    public static void main(java.lang.String[] args) {
        // run whole suite
        junit.textui.TestRunner.run(suite());
        // run only selected test case
        //junit.textui.TestRunner.run(new MyModuleValidation("testT2"));
    }
    
    public void setUp() {
        System.out.println("########  "+getName()+"  #######");
        JemmyProperties.setCurrentTimeout("ComponentOperator.WaitComponentTimeout", 180000);
        JemmyProperties.setCurrentTimeout("FrameWaiter.WaitFrameTimeout", 180000);
        exception = null;
    }
    
    public void tearDown() {
    }
    
    /** Test creation of web project. 
     * - open New Project wizard from main menu (File|New Project)
     * - select Web|Web Application
     * - in the next panel type project name and project location
     * - finish the wizard
     * - wait until scanning of java files is finished
     * - check index.jsp is opened
     */
    public void testNewWebProject() throws IOException {
        new ActionNoBlock("File|New Project", null).perform(); // TODO - use appropriate action
        WizardOperator newProjectWizard = new WizardOperator("New Project"); // TODO - use wizard appropriate operator (also in next lines
        JTreeOperator tree = new JTreeOperator(newProjectWizard);
        tree.getTimeouts().setTimeout("JTreeOperator.WaitNodeVisibleTimeout",30000);
        new Node(tree, "Web").select();
        new JListOperator(newProjectWizard, 1).selectItem("Web Application");
        newProjectWizard.next();
        new JTextFieldOperator(newProjectWizard, 0).typeText(SAMPLE_PROJECT_NAME);
        JTextFieldOperator txtProjectLocation = new JTextFieldOperator(newProjectWizard, 1);
        // clear text field
        txtProjectLocation.setText("");
        txtProjectLocation.typeText(SAMPLE_PROJECT_FOLDER); // NOI18N
        newProjectWizard.finish();
        Util.waitForScan();
        new EditorWindowOperator().getEditor("index.jsp");
        // HACK
        Node webPages = new Node(new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME),"Web Pages");
        new Node(webPages,"index.jsp");
        new Node(webPages,"WEB-INF|web.xml");
        new Node(webPages,"META-INF|context.xml");
        ref(Util.dumpProjectView(SAMPLE_PROJECT_NAME));
        compareReferenceFiles();
        //compareDD();
    }
    
    /** Test new JSP wizard. 
     * - open New File wizard from main menu (File|New File)
     * - select sample project as target
     * - select Web|JSP file type
     * - in the next panel type name
     * - finish the wizard
     * - check file is open in editor and close all opened documents
     */
    public void testNewJSP() throws IOException {
        // workaround due to issue #46073
        new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME).select();

        new ActionNoBlock("File|New File", null).perform();
        // WORKAROUND
        new EventTool().waitNoEvent(1000);
        WizardOperator newFileWizard = new WizardOperator("New File");
        new JComboBoxOperator(newFileWizard).selectItem(SAMPLE_PROJECT_NAME);
        // workaround due to issue #46007
        new Node(new JTreeOperator(newFileWizard), "Java Classes").select();
        new Node(new JTreeOperator(newFileWizard), "Web").select();
        new JListOperator(newFileWizard, 1).selectItem("JSP");
        newFileWizard.next();
        JTextFieldOperator txtPackageName = new JTextFieldOperator(newFileWizard);
        // clear text field
        txtPackageName.setText("");
        txtPackageName.typeText("page1");
        newFileWizard.finish();
        // check class is opened in Editor and then close it
        new EditorOperator("page1.jsp").close();
        ref(Util.dumpProjectView(SAMPLE_PROJECT_NAME));
        compareReferenceFiles();
        //compareDD();
    }
    
    /** Test new JSP wizard. 
     * - open New File wizard from context menu on Web Pages (New|File)
     * - select JSP file type
     * - in the next panel type name in
     * - finish the wizard
     * - check file is open in editor and close all opened documents
     */
    public void testNewJSP2() throws IOException {
        Node sample1Node = new Node(new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME),"Web Pages");
        // create a new class
        new ActionNoBlock(null, "New|JSP").perform(sample1Node);
        // WORKAROUND
        new EventTool().waitNoEvent(1000);
        WizardOperator newFileWizard = new WizardOperator("New JSP File");
        new JTextFieldOperator(newFileWizard).typeText("page2");
        newFileWizard.finish();
        // check class is opened in Editor and then close all documents
        new EditorOperator("page2.jsp").close();
        ref(Util.dumpProjectView(SAMPLE_PROJECT_NAME));
        compareReferenceFiles();
        //compareDD();
    }
    
    /** Test new servlet wizard. 
     * - open New File wizard from main menu (File|New File)
     * - select sample project as target
     * - select Web|Servlet file type
     * - in the next panel type name
     * - finish the wizard
     * - check file is open in editor and close it
     */
    public void testNewServlet() throws IOException {
        // create a new package
        new ActionNoBlock("File|New File", null).perform();
        // WORKAROUND
        new EventTool().waitNoEvent(1000);
        WizardOperator newFileWizard = new WizardOperator("New File");
        new JComboBoxOperator(newFileWizard).selectItem(SAMPLE_PROJECT_NAME);
        // workaround due to issue #46007
        new Node(new JTreeOperator(newFileWizard), "Java Classes").select();
        new Node(new JTreeOperator(newFileWizard), "Web").select();
        new JListOperator(newFileWizard, 1).selectItem("Servlet");
        newFileWizard.next();
        JTextFieldOperator txtPackageName = new JTextFieldOperator(newFileWizard);
        // clear text field
        txtPackageName.setText("");
        txtPackageName.typeText("Servlet1");
        JComboBoxOperator txtPackage = new JComboBoxOperator(newFileWizard,1);
        // clear text field
        txtPackage.clearText();
        txtPackage.typeText("test1");
        newFileWizard.next();
        newFileWizard.finish();
        // check class is opened in Editor and then close it
        new EditorOperator("Servlet1.java").close();
        // HACK
        new Node (new SourcePackagesNode(SAMPLE_PROJECT_NAME), "test1|Servlet1");
        ref(Util.dumpProjectView(SAMPLE_PROJECT_NAME));
        compareReferenceFiles();
        //compareDD();
    }
    
    /** Test new Servlet wizard. 
     * - open New File wizard from main menu (File|New File)
     * - select sample project as target
     * - select Java Classes|Package file type
     * - in the next panel type package name in
     * - finish the wizard
     * - open New File wizard from context menu on created package node (New|File)
     * - select Web|Servlet file type
     * - in the next panel type class name in
     * - finish the wizard
     * - check class is open in editor and close it
     */
    public void testNewServlet2() throws IOException {
        // create a new package
        new ActionNoBlock("File|New File", null).perform();
        // WORKAROUND
        new EventTool().waitNoEvent(1000);
        WizardOperator newFileWizard = new WizardOperator("New File");
        new JComboBoxOperator(newFileWizard).selectItem(SAMPLE_PROJECT_NAME);
        new Node(new JTreeOperator(newFileWizard), "Java Classes").select();
        new JListOperator(newFileWizard, 1).selectItem("Package");
        newFileWizard.next();
        JTextFieldOperator txtPackageName = new JTextFieldOperator(newFileWizard);
        // clear text field
        txtPackageName.setText("");
        txtPackageName.typeText("test2");
        newFileWizard.finish();
        Node sample1Node = new Node(new SourcePackagesNode(SAMPLE_PROJECT_NAME), "test2");
        // create a new class
        new ActionNoBlock(null, "New|Servlet").perform(sample1Node);
        newFileWizard = new WizardOperator("New Servlet");
        new JTextFieldOperator(newFileWizard).typeText("Servlet2");
        newFileWizard.next();
        new JTextFieldOperator(newFileWizard,1).setText("");
        new JTextFieldOperator(newFileWizard,1).typeText("Servlet2Name");
        new JTextFieldOperator(newFileWizard,2).setText("");
        new JTextFieldOperator(newFileWizard,2).typeText("/Servlet2URL");
        newFileWizard.finish();
        // check class is opened in Editor and then close it
        new EditorOperator("Servlet2.java").close();
        // HACK
        new Node (new SourcePackagesNode(SAMPLE_PROJECT_NAME), "test2|Servlet2");
        ref(Util.dumpProjectView(SAMPLE_PROJECT_NAME));
        compareReferenceFiles();
        //compareDD();
    }
    
    public void testBuildProject() {
        Node rootNode = new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME);
        new Action(null,"Clean and Build Project").perform(rootNode);
        // wait message "Finished building SampleProject (clean, dist)"
        MainWindowOperator.getDefault().waitStatusText("Finished building "+SAMPLE_PROJECT_NAME+" (clean, dist)");

        ref(Util.dumpFiles(new File(SAMPLE_PROJECT_FOLDER, SAMPLE_PROJECT_NAME)));
        compareReferenceFiles();
    }
    
    public void testCompileAllJSP() {
        Node rootNode = new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME);
        new Action(null,"Properties").perform(rootNode);
        NbDialogOperator properties = new NbDialogOperator("Project Properties");
        new Node(new JTreeOperator(properties), "Build|Compiling Sources").select();
        new JCheckBoxOperator(properties,2).changeSelection(true);
        properties.ok();
//        Util.waitForScan();
        
        testBuildProject();
        testCleanProject();

        new Action(null,"Properties").perform(rootNode);
        properties = new NbDialogOperator("Project Properties");
        new Node(new JTreeOperator(properties), "Build|Compiling Sources").select();
        new JCheckBoxOperator(properties,2).changeSelection(false);
        properties.ok();
//        Util.waitForScan();
    }
    
    public void testCompileJSP() {
        Node rootNode = new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME);
        Node jspNode = new Node(rootNode,"Web Pages|page2.jsp");
        new Action(null,"Compile File").perform(jspNode);
        // wait message "Finished building SampleProject (clean, dist)"
        MainWindowOperator.getDefault().waitStatusText("Finished building "+SAMPLE_PROJECT_NAME+" (compile-single-jsp)");
        ref(Util.dumpFiles(new File(SAMPLE_PROJECT_FOLDER, SAMPLE_PROJECT_NAME)));
        compareReferenceFiles();
    }
    
    public void testCleanProject() {
        Node rootNode = new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME);
        new Action(null,"Clean Project").perform(rootNode);
        // wait message "Finished building SampleProject (clean, dist)"
        MainWindowOperator.getDefault().waitStatusText("Finished building "+SAMPLE_PROJECT_NAME+" (clean)");
        
        ref(Util.dumpFiles(new File(SAMPLE_PROJECT_FOLDER, SAMPLE_PROJECT_NAME)));
        compareReferenceFiles();
    }
    
    public void testRunProject() {
        Util.setSwingBrowser();
        Node rootNode = new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME);
        new Node(rootNode,"Web Pages|index.jsp").performPopupAction("Open");
        new EditorOperator("index.jsp").replace("JSP Page",
                                                "SampleProject Index Page");
        new Action(null,"Run Project").perform(rootNode);
        Util.waitForProgressMonitor(true);
        new EditorOperator("index.jsp").close();
        new TopComponentOperator("SampleProject Index Page").close();
        grabOutput();
    }

    public void testRunJSP() {
        Node rootNode = new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME);
        new Node(rootNode,"Web Pages|page1.jsp").performPopupAction("Open");
        new EditorOperator("page1.jsp").replace("JSP Page",
                                                "Page 1");
        new Action("Run|Run Other|Run \"page1.jsp\"",null).perform();
        Util.waitForProgressMonitor(false);
        new EditorOperator("page1.jsp").close();
        new TopComponentOperator("Page 1").close();
        grabOutput();
    }
    
    public void testRunServlet() {
        Node sourceNode = new SourcePackagesNode(SAMPLE_PROJECT_NAME);
        new Node(sourceNode,"test1|Servlet1.java").performPopupAction("Open");
        EditorOperator editor = new EditorOperator("Servlet1.java");
        if (!editor.getText(43).equals("        /* TODO output your page here\n"))
            throw new AssertionFailedError("Servlet1.java:43 Other content than expected.\n"+
            "was >"+editor.getText(43)+"<, expected >        /* TODO output your page here\n<");
        if (!editor.getText(52).equals("         */\n"))
            throw new AssertionFailedError("Servlet1.java:52 Other content than expected.\n"+
            "was >"+editor.getText(52)+"<, expected >         */\n<");
        editor.deleteLine(52);
        editor.deleteLine(43);
        editor.replace("out.println(\"Servlet\");",
                       "out.println(\"Servlet name=\"+request.getParameter(\"name\")+\"\");");
        
        new ActionNoBlock("Run|Run Other|Run \"Servlet1.java\"",null).perform();
        NbDialogOperator dialog = new NbDialogOperator("Set Servlet Execution Uri");
        new JComboBoxOperator(dialog).typeText("?name=Servlet1\n");
        dialog.ok();
        //new JButtonOperator(dialog,"OK").push();
        
        Util.waitForProgressMonitor(false);
        new EditorOperator("Servlet1.java").close();
        new TopComponentOperator("Servlet name=Servlet1").close();
        grabOutput();
    }
    
    public void testDebugProject() throws Throwable {
      try {  
        Node rootNode = new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME);
        new Node(rootNode,"Web Pages|index.jsp").performPopupAction("Open");
        EditorOperator editor = new EditorOperator("index.jsp");
        
        editor.insert("Debugging",6,1);
        editor.setCaretPositionToLine(6);
        new Action(runItem+"|"+toggleBrakpointItem,null).perform();
        // wait breakpoint established
        new Waiter(new Waitable() {
            public Object actionProduced(Object editorOper) {
                return ((EditorOperator)editorOper).getAnnotations(6).length == 1 ? Boolean.TRUE : null;
            }
            public String getDescription() {
                return("Wait breakpoint established on line 6"); // NOI18N
            }
        }).waitAction(editor);
        
        editor.setCaretPositionToLine(9);
        editor.pushKey(KeyEvent.VK_F8, KeyEvent.CTRL_MASK);
        // wait breakpoint established
        new Waiter(new Waitable() {
            public Object actionProduced(Object editorOper) {
                return ((EditorOperator)editorOper).getAnnotations(9).length == 1 ? Boolean.TRUE : null;
            }
            public String getDescription() {
                return("Wait breakpoint established on line 9"); // NOI18N
            }
        }).waitAction(editor);
            
        
        // start to track Main Window status bar
//        MainWindowOperator.StatusTextTracer stt = MainWindowOperator.getDefault().getStatusTextTracer();
//        stt.start();
//        MainWindowOperator.getDefault().getTimeouts().setTimeout("Waiter.WaitingTime", 180000);
        
        new Action(null,"Debug Project").perform(rootNode);
        Util.waitForProgressMonitor(false);

        OutputTabOperator console = new OutputTabOperator("Debugger Console");
        console.getTimeouts().setTimeout("ComponentOperator.WaitStateTimeout", 180000);
        
//        stt.waitText("Breakpoint reached at line 6 in index.jsp");
        console.waitText("stopped at index.jsp:6");

        // Step Over
        new Action(runItem+"|Step Over", null).perform();
//        stt.waitText("stopped at index.jsp:7"); // NOI18N
        console.waitText("stopped at index.jsp:7");
        
        // continue debugging
        // "Continue"
        String continueItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_Continue_action_name");
        new Action(runItem+"|"+continueItem, null).perform();
        
        // check the second breakpoint reached
        // wait status text "Thread main stopped at SampleClass1.java:24"
//        stt.waitText("Breakpoint reached at line 9 in index.jsp"); // NOI18N
        console.waitText("stopped at index.jsp:9"); 
//        stt.stop();  
        
      } catch (Throwable th) {
            exception = th;
            captureScreen();
            grabOutput();
            throw th;
      } finally {
          try {
            finishDebugger();

            new EditorOperator("index.jsp").close();
            new TopComponentOperator("SampleProject Index Page").close();
            closeDebuggerConsole();
            Util.deleteAllBreakpoints();
            Util.stopTomcat(); 
          } catch (Throwable e) {
              handleCleanupException(e);
          }
      }
    }
    
    public void testRunToCursor() throws Throwable {
      try {
        Node rootNode = new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME);
        new Node(rootNode,"Web Pages|index.jsp").performPopupAction("Open");
        EditorOperator editor = new EditorOperator("index.jsp");
        editor.insert("Run To Cursor ",6,1);
        editor.setCaretPositionToLine(4);
        new Action("Run|Run to Cursor",null).perform();
        Util.waitForProgressMonitor(true);
        OutputTabOperator console = new OutputTabOperator("Debugger Console");
        console.waitText("stopped at index.jsp:4");
        // Step Over
        new Action("Run|Step Over", null).perform();
        console.waitText("stopped at index.jsp:5");
        
      } catch (Throwable th) {
            exception = th;
            captureScreen();
            grabOutput();
            throw th;
      } finally {
          try {
            finishDebugger();
        
            new EditorOperator("index.jsp").close();
            new TopComponentOperator("SampleProject Index Page").close();
            closeDebuggerConsole();
            //Util.stopTomcat(); 
          } catch (Throwable e) {
              handleCleanupException(e);
          }
      }
    }
    
    public void testDebugServlet() throws Throwable {
      try {
        Node sourceNode = new SourcePackagesNode(SAMPLE_PROJECT_NAME);
        Node servletNode = new Node(sourceNode,"test1|Servlet1.java");
        servletNode.performPopupAction("Open");
        
        EditorOperator editor = new EditorOperator("Servlet1.java");
        
        editor.setCaretPositionToLine(45);
        new Action(runItem+"|"+toggleBrakpointItem,null).perform();
        // wait breakpoint established
        new Waiter(new Waitable() {
            public Object actionProduced(Object editorOper) {
                return ((EditorOperator)editorOper).getAnnotations(45).length == 1 ? Boolean.TRUE : null;
            }
            public String getDescription() {
                return("Wait breakpoint established on line 45"); // NOI18N
            }
        }).waitAction(editor);
        
        editor.setCaretPositionToLine(50);
        editor.pushKey(KeyEvent.VK_F8, KeyEvent.CTRL_MASK);
        // wait breakpoint established
        new Waiter(new Waitable() {
            public Object actionProduced(Object editorOper) {
                return ((EditorOperator)editorOper).getAnnotations(50).length == 1 ? Boolean.TRUE : null;
            }
            public String getDescription() {
                return("Wait breakpoint established on line 50"); // NOI18N
            }
        }).waitAction(editor);
            
        new Action(null,"Debug File").perform(servletNode);
        Util.waitForProgressMonitor(false);

        OutputTabOperator console = new OutputTabOperator("Debugger Console");
        console.waitText("stopped at Servlet1.java:45");

        // Step Over
        new Action(runItem+"|Step Over", null).perform();
        console.waitText("stopped at Servlet1.java:46");
        
        // continue debugging
        // "Continue"
        String continueItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_Continue_action_name");
        new Action(runItem+"|"+continueItem, null).perform();
        
        // check the second breakpoint reached
        console.waitText("stopped at Servlet1.java:50"); 
        
      } catch (Throwable th) {
            exception = th;
            captureScreen();
            grabOutput();
            throw th;
      } finally {
          try {
            finishDebugger();
        
            new EditorOperator("Servlet1.java").close();
            new TopComponentOperator("Servlet name=Servlet1").close();
            closeDebuggerConsole();
            Util.deleteAllBreakpoints();
          } catch (Throwable e) {
              handleCleanupException(e);
          }
      }
    }
    
    public void testCreateTLD() {
        //HACK
        new Node (new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME), "Web Pages|WEB-INF").expand();

        // workaround due to issue #46073
        new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME).select();

        new ActionNoBlock("File|New File", null).perform();
        // WORKAROUND
        new EventTool().waitNoEvent(1000);
        WizardOperator newFileWizard = new WizardOperator("New File");
        new JComboBoxOperator(newFileWizard).selectItem(SAMPLE_PROJECT_NAME);
        // workaround due to issue #46007
        new Node(new JTreeOperator(newFileWizard), "Java Classes").select();
        new Node(new JTreeOperator(newFileWizard), "Web").select();
        new JListOperator(newFileWizard, 1).selectItem("Tag Library Descriptor");
        newFileWizard.next();
        JTextFieldOperator txtName = new JTextFieldOperator(newFileWizard);
        // clear text field
        txtName.setText("");
        txtName.typeText("MyTags");
        newFileWizard.finish();
        try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) {}
        // check class is opened in Editor and then close it
        new EditorOperator("MyTags.tld").close();
        // HACK
        new Node (new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME), "Web Pages|WEB-INF|tlds|MyTags.tld");
        ref(Util.dumpProjectView(SAMPLE_PROJECT_NAME));
        compareReferenceFiles();
    }
    
    public void testCreateTagHandler() {
        // workaround due to issue #46073
        new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME).select();

        new ActionNoBlock("File|New File", null).perform();
        // WORKAROUND
        new EventTool().waitNoEvent(1000);
        WizardOperator newFileWizard = new WizardOperator("New File");
        new JComboBoxOperator(newFileWizard).selectItem(SAMPLE_PROJECT_NAME);
        // workaround due to issue #46007
        new Node(new JTreeOperator(newFileWizard), "Java Classes").select();
        new Node(new JTreeOperator(newFileWizard), "Web").select();
        new JListOperator(newFileWizard, 1).selectItem("Tag Handler");
        newFileWizard.next();
        JTextFieldOperator txtName = new JTextFieldOperator(newFileWizard);
        // clear text field
        txtName.setText("");
        txtName.typeText("MyTag");
        JComboBoxOperator pkg = new JComboBoxOperator(newFileWizard,1);
        pkg.clearText();
        pkg.typeText("tags");
        newFileWizard.next();
        new JButtonOperator(newFileWizard).push();
        NbDialogOperator dialog = new NbDialogOperator("Browse Files");
        new Node(new JTreeOperator(dialog),"Web Pages|WEB-INF|tlds|MyTags.tld").select();
        new JButtonOperator(dialog,"Select File").push();
        newFileWizard.finish();
        // check class is opened in Editor and then close it
        EditorOperator editor = new EditorOperator("MyTag.java");
        editor.insert(getTagHandlerCode(),43,1);
        editor.saveDocument();
        editor.close();
        // HACK
        new Node (new SourcePackagesNode(SAMPLE_PROJECT_NAME), "tags|MyTag.java");
        ref(Util.dumpProjectView(SAMPLE_PROJECT_NAME));
        compareReferenceFiles();
    }
    
    protected String getTagHandlerCode() {
        return "out.println(\"TagOutput\"); \n";
    }
    
    public void testRunTag() throws Throwable {
        Node rootNode = new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME);
        Node jspNode = new Node(rootNode,"Web Pages|page2.jsp");
        jspNode.performPopupAction("Open");
        EditorOperator editor = new EditorOperator("page2.jsp");
        editor.replace("JSP Page",
                      "<my:MyTag/>");
        editor.insert("<%@taglib prefix=\"my\" uri=\"/WEB-INF/tlds/MyTags\"%>\n", 3, 1);
        new Action(null,"Run File").perform(jspNode);
        Util.waitForProgressMonitor(false);
        editor.close();
        new TopComponentOperator("TagOutput").close();
        grabOutput();
    }

    public void testDebugTag() throws Throwable {
      EditorOperator editor = null;
      try {
        Node sourceNode = new SourcePackagesNode(SAMPLE_PROJECT_NAME);
        Node tagNode = new Node(sourceNode,"tags|MyTag.java");
        tagNode.performPopupAction("Open");
        
        editor = new EditorOperator("MyTag.java");
        
        editor.setCaretPositionToLine(43);
        new Action(runItem+"|"+toggleBrakpointItem,null).perform();
        // wait breakpoint established
        new Waiter(new Waitable() {
            public Object actionProduced(Object editorOper) {
                return ((EditorOperator)editorOper).getAnnotations(43).length == 1 ? Boolean.TRUE : null;
            }
            public String getDescription() {
                return("Wait breakpoint established on line 43"); // NOI18N
            }
        }).waitAction(editor);
        
        Node rootNode = new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME);
        Node jspNode = new Node(rootNode,"Web Pages|page2.jsp");
        new Action(null,"Debug File").perform(jspNode);
        Util.waitForProgressMonitor(false);

        OutputTabOperator console = new OutputTabOperator("Debugger Console");
        console.waitText("stopped at MyTag.java:43");
        
        // continue debugging
        // "Continue"
        String continueItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_Continue_action_name");
        new Action(runItem+"|"+continueItem, null).perform();
        MainWindowOperator.getDefault().waitStatusText("User program running");
        
      } catch (Throwable th) {
            exception = th;
            captureScreen();
            grabOutput();
            throw th;
      } finally {
          try {
            finishDebugger();
        
            if (editor != null)
                editor.close();
            new TopComponentOperator("TagOutput").close();
            closeDebuggerConsole();
            Util.deleteAllBreakpoints(); 
          } catch (Throwable e) {
              handleCleanupException(e);
          }
      }        
    }
    
    public void testNewHTML() throws IOException {
        // workaround due to issue #46073
        new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME).select();

        new ActionNoBlock("File|New File", null).perform();
        // WORKAROUND
        new EventTool().waitNoEvent(1000);
        WizardOperator newFileWizard = new WizardOperator("New File");
        new JComboBoxOperator(newFileWizard).selectItem(SAMPLE_PROJECT_NAME);
        // workaround due to issue #46007
        new Node(new JTreeOperator(newFileWizard), "Java Classes").select();
        new Node(new JTreeOperator(newFileWizard), "Web").select();
        new JListOperator(newFileWizard, 1).selectItem("HTML");
        newFileWizard.next();
        JTextFieldOperator txtPackageName = new JTextFieldOperator(newFileWizard);
        // clear text field
        txtPackageName.setText("");
        txtPackageName.typeText("HTML");
        newFileWizard.finish();
        // check class is opened in Editor and then close it
        new EditorOperator("HTML.html").close();
        ref(Util.dumpProjectView(SAMPLE_PROJECT_NAME));
        compareReferenceFiles();
        //compareDD();
    }        
    
    public void testRunHTML() {
        Node rootNode = new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME);
        new Node(rootNode,"Web Pages|HTML.html").performPopupAction("Open");
        new EditorOperator("HTML.html").replace("",
                                                "HTML Page");
        new Action("Run|Run Other|Run \"HTML.html\"",null).perform();
        Util.waitForProgressMonitor(false);
        new EditorOperator("HTML.html").close();
        new TopComponentOperator("HTML Page").close();
        grabOutput();
    }
    
    public void testNewSegment() throws IOException {
        Node sample1Node = new Node(new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME),"Web Pages");
        // create a new class
        new ActionNoBlock(null, "New|JSP").perform(sample1Node);
        // WORKAROUND
        new EventTool().waitNoEvent(1000);
        WizardOperator newFileWizard = new WizardOperator("New JSP File");
        new JTextFieldOperator(newFileWizard).typeText("segment");
        new JCheckBoxOperator(newFileWizard).changeSelection(true);
        newFileWizard.finish();
        // check class is opened in Editor and then close all documents
        new EditorOperator("segment.jspf").close();
        ref(Util.dumpProjectView(SAMPLE_PROJECT_NAME));
        compareReferenceFiles();
        //compareDD();
    }
    
    public void testNewDocument() throws IOException {
        Node sample1Node = new Node(new ProjectsTabOperator().getProjectRootNode(SAMPLE_PROJECT_NAME),"Web Pages");
        // create a new class
        new ActionNoBlock(null, "New|JSP").perform(sample1Node);
        // WORKAROUND
        new EventTool().waitNoEvent(1000);
        WizardOperator newFileWizard = new WizardOperator("New JSP File");
        new JTextFieldOperator(newFileWizard).typeText("document");
        new JRadioButtonOperator(newFileWizard,1).changeSelection(true);
        newFileWizard.finish();
        // check class is opened in Editor and then close all documents
        new EditorOperator("document.jspx").close();
        ref(Util.dumpProjectView(SAMPLE_PROJECT_NAME));
        compareReferenceFiles();
        //compareDD();
    }
    
    public void testStopTomcat() throws Exception {
        Util.startStopTomcat(false, true);
        try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) {}
        URL url = new URL("http://localhost:8084");
        URLConnection connection = url.openConnection();
        try {
            connection.connect();
            fail("Connection to http://localhost:8084 established, but tomcat should not be running.");
        } catch (ConnectException e) {  }
    }
    
    public void testStartTomcat() throws Exception {
        Util.startStopTomcat(true, true);
        URL url = new URL("http://localhost:8084");
        URLConnection connection = url.openConnection();
        connection.connect();
    }
    
    public void testBrowserSettings() {
        OptionsOperator optionsOper = OptionsOperator.invoke();
        // set exact comparator because in Japanese there is conflict with Filesystem settings
        optionsOper.treeTable().tree().setComparator(new Operator.DefaultStringComparator(true, true));
        // "IDE Configuration|System|System Settings"
        String systemSettings = Bundle.getString("org.netbeans.core.Bundle", "UI/Services/IDEConfiguration") + "|" +
                                //Bundle.getString("org.netbeans.core.Bundle", "UI/Services/IDEConfiguration/System") + "|" +
                                //Bundle.getString("org.netbeans.core.Bundle", "Services/org-netbeans-core-IDESettings.settings");
                                "Server and External Tool Settings|Web Browsers";
        optionsOper.selectOption(systemSettings);
        
        Node node = new Node(optionsOper.treeTable().tree(),systemSettings);
        node.expand();
        new Node(node,"Swing HTML Browser");
        String children[] = node.getChildren();
        for (int i=0; i") && 
                !children[i].equals("Mozilla") &&
                !children[i].equals("Netscape") &&
                !children[i].equals("Swing HTML Browser") &&
                !children[i].equals("Internet Explorer"))
                fail("Unrecognized settings in Web Browser Options");
        }
        optionsOper.close();
        
    }
    
    public void testFinish() {
        Util.stopTomcat();
        ProjectSupport.closeProject(SAMPLE_PROJECT_NAME);
    }
    
    //********************************************************
    
    private void compareDD() throws IOException {
        assertFile("web.xml differ", 
                   new File(SAMPLE_PROJECT_FOLDER+File.separator+SAMPLE_PROJECT_NAME+File.separator+"web"+File.separator+"WEB-INF"+File.separator+"web.xml"),
                   getGoldenFile(this.getName()+"_web.xml.pass"),
                   new File(getWorkDir(),this.getName()+"_web.xml.diff"));
    }
    
    private void finishDebugger() {
         // finish debugging
        // "Finish Debugger Session"
        String finishItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_KillAction_name");
        Action finishAction = new Action(runItem+"|"+finishItem, null);
        if (finishAction.isEnabled())
            finishAction.perform();
        // check status line
        // "Debugger finished"
        String finishedLabel = Bundle.getString("org.netbeans.modules.debugger.jpda.ui.Bundle", "CTL_Debugger_finished");;
        MainWindowOperator.getDefault().waitStatusText(finishedLabel);
    }
    
    private void closeDebuggerConsole() {
        long timeout = JemmyProperties.getCurrentTimeout("ComponentOperator.WaitComponentTimeout");
        JemmyProperties.setCurrentTimeout("ComponentOperator.WaitComponentTimeout", 3000);
        try {
            OutputTabOperator console = new OutputTabOperator("Debugger Console");
            console.makeComponentVisible();
            console.close();
        } catch (TimeoutExpiredException e) {}
        JemmyProperties.setCurrentTimeout("ComponentOperator.WaitComponentTimeout", timeout);
    }
    
    private void handleCleanupException(Throwable e) throws Throwable {
        if (exception == null)
          throw e;
        else {
          exception.printStackTrace(getLog());
          throw new Exception("Exception in clean-up, look at log for exception from test.", e);
        }
    }
    
    private void captureScreen() {
        try {
            // capture screen before cleanup in finally clause is completed
            PNGEncoder.captureScreen(getWorkDir().getAbsolutePath()+File.separator+"screenBeforeCleanup.png");
        } catch (Exception e1) {
            // ignore it
        }
    }
    
    private void grabOutput() {
        long timeout = JemmyProperties.getCurrentTimeout("ComponentOperator.WaitComponentTimeout");
        JemmyProperties.setCurrentTimeout("ComponentOperator.WaitComponentTimeout", 3000);
        try {
            for (int i=0; i<10; i++) {
                OutputTabOperator output = new OutputTabOperator("",i);
                log("Output"+i+".log",output.getName()+"\n-------------\n\n"+output.getText());
            }
        } catch (TimeoutExpiredException e) { }
        JemmyProperties.setCurrentTimeout("ComponentOperator.WaitComponentTimeout", timeout);
    }

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