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.test.gui.web.servletexec.servlet23.listeners.creation; 



import org.netbeans.jellytools.Bundle;
import org.netbeans.jellytools.nodes.FilesystemNode;
import org.netbeans.jellytools.nodes.JavaNode;
import org.netbeans.jellytools.nodes.Node;

import org.netbeans.jellytools.JellyTestCase;


import org.netbeans.jellytools.ChooseTemplateStepOperator;
import org.netbeans.jellytools.TargetLocationStepOperator;
import org.netbeans.jellytools.NewObjectNameStepOperator;
import org.netbeans.jellytools.NewWizardOperator;
import org.netbeans.jellytools.EditorWindowOperator;
import org.netbeans.jellytools.EditorOperator;
import org.netbeans.jellytools.NbDialogOperator;
import org.netbeans.jellytools.actions.NewTemplateAction;


import org.netbeans.web.test.util.RepositoryNodeWaitable;
import org.netbeans.web.test.util.Utils;

import org.netbeans.test.gui.web.util.EditorPageWaitable;

import org.netbeans.jemmy.Timeouts;
import org.netbeans.jemmy.Waitable;
import org.netbeans.jemmy.Waiter;

import org.netbeans.junit.NbTestSuite;



public class ListenersCreation extends JellyTestCase { 

    private static String compileListenerMenuPath = "Compile";
    private static String wmName = "wm1";
    private static String webInf = "WEB-INF";
    private static String classes = "Classes";
    private static String popupDefaultPackage = "listeners|creation";
    private static String mainDefaultPackage = "listeners|creationMain";
    private static String existing = "existing";
    private static String nonexisting = "nonexisting";
    private static String fSep = System.getProperty("file.separator");
    private static String iSep = "|";
    private static String workDir = null;
    private static String webModule = null;
    private static String pkg = null;
    private static String pkgMain = null;
    private static boolean first = true;
    
    private static String calViaMain = "CALViaMainMenu";
    private static String clViaMain  = "CLViaMainMenu";
    private static String salViaMain = "SALViaMainMenu";
    private static String slViaMain  = "SLViaMainMenu";

    private static String calViaPopup = "CALViaPopup";
    private static String clViaPopup  = "CLViaPopup";
    private static String salViaPopup = "SALViaPopup";
    private static String slViaPopup  = "SLViaPopup";

    private static String popularCALViaPopup = "PopularCALViaPopup";
    private static String popularCLViaPopup  = "PopularCLViaPopup";
    private static String popularSALViaPopup = "PopularSALViaPopup";
    private static String popularSLViaPopup  = "PopularSLViaPopup";


    private static String popularCALViaMain = "PopularCALViaMain";
    private static String popularCLViaMain  = "PopularCLViaMain";
    private static String popularSALViaMain = "PopularSALViaMain";
    private static String popularSLViaMain  = "PopularSLViaMain";

    private static String calDefaultName = "ContextAttributeListener";
    private static String clDefaultName  = "ContextListener";
    private static String salDefaultName = "SessionAttributeListener";
    private static String slDefaultName  = "SessionListener";
    
    
    private static String calTreePath = null;
    private static String clTreePath  = null;
    private static String salTreePath = null;
    private static String slTreePath  = null;
    

    private static String jspServlet = null;
    private static String servlet = null;
    private static String listeners = null;
    private static String calName = null;
    private static String clName  = null;
    private static String salName = null;
    private static String slName  = null;



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


    /** Use for execution inside IDE */ 
    public static void main(java.lang.String[] args) { 
        junit.textui.TestRunner.run(suite()); 
    }

    public static junit.framework.Test suite() {
	
	workDir = System.getProperty("servletexec.workdir").replace('/', fSep.charAt(0));
	webModule = workDir + fSep + wmName;
	pkg = webModule + iSep + webInf + iSep + classes + iSep + popupDefaultPackage;
	pkgMain = webModule + iSep + webInf + iSep + classes + iSep + mainDefaultPackage;

	
	calTreePath = jspServlet + iSep + listeners + iSep +  calName;
	clTreePath = jspServlet + iSep + listeners + iSep +  clName;
	salTreePath = jspServlet + iSep + listeners + iSep +  salName;
	slTreePath = jspServlet + iSep + listeners + iSep +  slName;

	String wmc = System.getProperty("servletexec.mountcount");
	int count = 0;
	if(wmc != null) {
	    count = new Integer(wmc).intValue();
	}
	if(first) {
	    while(count >0) {
		Utils.handleDialogAfterNewWebModule();
		count--;
	    }
	    first = false;
	}
        return  new NbTestSuite(ListenersCreation.class); 
        
    }


    /* Tests section */
   
    /*Section 1.1 CAL via MAIN*/

    public void testCALViaMain() {
	testLLSViaMain(calTreePath, calViaMain);
    }
    
    public void testSameNameCALViaMain() {
	testSameNameLLSViaMain(calTreePath, popularCALViaMain);
    }

    public void testDefaultNameCALViaMain() {
	testDefaultNameLLSViaMain(calTreePath, calDefaultName);
    }

    public void testSecondDefaultNameCALViaMain() {
	testSecondDefaultNameLLSViaMain(calTreePath, calDefaultName);
    }

    /*Section 1.2 CAL via Popup*/

    public void testCALViaPopup() {
	testLLSViaPopup(calTreePath, calName, calViaPopup);
    }
    
    public void testSameNameCALViaPopup() {
	testSameNameLLSViaPopup(calTreePath, calName, popularCALViaPopup);
    }
    
    public void testDefaultNameCALViaPopup() {
	testDefaultNameLLSViaPopup(calTreePath, calName, calDefaultName);
    }

    public void testSecondDefaultNameCALViaPopup() {
	testSecondDefaultNameLLSViaPopup(calTreePath, calName, calDefaultName);
    }


    /*Section 2.1 CL via MAIN*/

    public void testCLViaMain() {
	testLLSViaMain(clTreePath, clViaMain);
    }
    
    public void testSameNameCLViaMain() {
	testSameNameLLSViaMain(clTreePath, popularCLViaMain);
    }

    public void testDefaultNameCLViaMain() {
	testDefaultNameLLSViaMain(clTreePath, clDefaultName);
    }

    public void testSecondDefaultNameCLViaMain() {
	testSecondDefaultNameLLSViaMain(clTreePath, clDefaultName);
    }

    /*Section 2.2 CL via Popup*/

    public void testCLViaPopup() {
	testLLSViaPopup(clTreePath, clName, clViaPopup);
    }
    
    public void testSameNameCLViaPopup() {
	testSameNameLLSViaPopup(clTreePath, clName, popularCLViaPopup);
    }
    
    public void testDefaultNameCLViaPopup() {
	testDefaultNameLLSViaPopup(clTreePath, clName, clDefaultName);
    }

    public void testSecondDefaultNameCLViaPopup() {
	testSecondDefaultNameLLSViaPopup(clTreePath, clName, clDefaultName);
    }



    /*Section 3.1 SAL via MAIN*/

    public void testSALViaMain() {
	testLLSViaMain(salTreePath, salViaMain);
    }
    
    public void testSameNameSALViaMain() {
	testSameNameLLSViaMain(salTreePath, popularSALViaMain);
    }

    public void testDefaultNameSALViaMain() {
	testDefaultNameLLSViaMain(salTreePath, salDefaultName);
    }

    public void testSecondDefaultNameSALViaMain() {
	testSecondDefaultNameLLSViaMain(salTreePath, salDefaultName);
    }

    /*Section 3.2 SAL via Popup*/

    public void testSALViaPopup() {
	testLLSViaPopup(salTreePath, salName, salViaPopup);
    }
    
    public void testSameNameSALViaPopup() {
	testSameNameLLSViaPopup(salTreePath, salName, popularSALViaPopup);
    }
    
    public void testDefaultNameSALViaPopup() {
	testDefaultNameLLSViaPopup(salTreePath, salName, salDefaultName);
    }

    public void testSecondDefaultNameSALViaPopup() {
	testSecondDefaultNameLLSViaPopup(salTreePath, salName, salDefaultName);
    }



    /*Section 4.1 SL via MAIN*/

    public void testSLViaMain() {
	testLLSViaMain(slTreePath, slViaMain);
    }
    
    public void testSameNameSLViaMain() {
	testSameNameLLSViaMain(slTreePath, popularSLViaMain);
    }

    public void testDefaultNameSLViaMain() {
	testDefaultNameLLSViaMain(slTreePath, slDefaultName);
    }

    public void testSecondDefaultNameSLViaMain() {
	testSecondDefaultNameLLSViaMain(slTreePath, slDefaultName);
    }

    /*Section 4.2 SL via Popup*/

    public void testSLViaPopup() {
	testLLSViaPopup(slTreePath, slName, slViaPopup);
    }
    
    public void testSameNameSLViaPopup() {
	testSameNameLLSViaPopup(slTreePath, slName, popularSLViaPopup);
    }
    
    public void testDefaultNameSLViaPopup() {
	testDefaultNameLLSViaPopup(slTreePath, slName, slDefaultName);
    }

    public void testSecondDefaultNameSLViaPopup() {
	testSecondDefaultNameLLSViaPopup(slTreePath, slName, slDefaultName);
    }




    //Private methods

    private void checkResultOfListenerCreation(String explorerPath) {
	String name = explorerPath.substring(explorerPath.lastIndexOf(iSep)+1);
	//1)Check that it is created

	RepositoryNodeWaitable rnw = new RepositoryNodeWaitable(explorerPath);
	Waiter w1 = new Waiter(rnw);
	try {
	    w1.waitAction(rnw);
	} catch (Exception e) {
	    e.printStackTrace();
	    fail("Listener node not created in repository");
	}
	//2)Check that it is opened in the editor

	EditorPageWaitable epw = new EditorPageWaitable(name);
	Waiter w = new Waiter(epw);
	try {
	    w.waitAction(epw);
	} catch (Exception e) {
	    e.printStackTrace();
	    fail("Not opened in editor");
	} 
	epw.getEditorOperator().close(false); //Close opened file
	//3)Check that code is "compilable"

	JavaNode node1 = new JavaNode(explorerPath);
	node1.compile();
	
	isFail(Utils.checkResultOfJavaFileCompilation(explorerPath));
    }

   
    private void isFail(String res) {
	if(res != null) {
	    fail(res);
	}
    } 



     /* Tests section. Private methods. Used for all kinds of listeners*/
   
    /*Section 1.1 LISTENER via MAIN*/
   private void testLLSViaMain(String llsTreePath, String llsViaMain) {
	FilesystemNode node1 = null;
	NewWizardOperator wizard = null;
	try {
	    node1 = new FilesystemNode(pkgMain);
	    node1.select();
	}catch(Exception e) {
	    e.printStackTrace();
	    fail("Please check, that all mounts are done properly");
	}
	try {
	    new NewTemplateAction().performMenu();
	    wizard = new NewWizardOperator();
	    new ChooseTemplateStepOperator().selectTemplate(llsTreePath);
	    wizard.next();
	    new TargetLocationStepOperator().setName(llsViaMain);
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during listener creation");
	}
	checkResultOfListenerCreation(pkgMain + iSep + llsViaMain);
	
    }
    
   private void testSameNameLLSViaMain(String llsTreePath, String popularLLSViaMain) {
	FilesystemNode node1 = null;
	NewWizardOperator wizard = null;
	try {
	    node1 = new FilesystemNode(pkgMain);
	    node1.select();
	}catch(Exception e) {
	    e.printStackTrace();
	    fail("Please check, that all mounts are done properly");
	}
	try {
	    new NewTemplateAction().performMenu();
	    wizard = new NewWizardOperator();
	    new ChooseTemplateStepOperator().selectTemplate(llsTreePath);
	    wizard.next();
	    new TargetLocationStepOperator().setName(popularLLSViaMain);
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during listener creation");
	}

	checkResultOfListenerCreation(pkgMain + iSep + popularLLSViaMain);
	
	try {
	    node1.select();
	    new NewTemplateAction().performMenu();
	    wizard = new NewWizardOperator();
	    new ChooseTemplateStepOperator().selectTemplate(llsTreePath);
	    wizard.next();
	    new TargetLocationStepOperator().setName(popularLLSViaMain);
	    if(wizard.btFinish().isEnabled()) {
		wizard.cancel();
		fail("Finish button isn't disabled, if LLS with same name already exist");
	    }
	    wizard.cancel();
	}catch(Exception e2) {
	    if(wizard != null)
		wizard.cancel();
	    e2.printStackTrace();
	    fail("Exception during listener creation");
	}
    }

   private void testDefaultNameLLSViaMain(String llsTreePath, String llsDefaultName) {
	FilesystemNode node1 = null;
	NewWizardOperator wizard = null;
	try {
	    node1 = new FilesystemNode(pkgMain);
	    node1.select();
	}catch(Exception e) {
	    e.printStackTrace();
	    fail("Please check, that all mounts are done properly");
	}
	try {
	    new NewTemplateAction().performMenu();
	    wizard = new NewWizardOperator();
	    new ChooseTemplateStepOperator().selectTemplate(llsTreePath);
	    wizard.next();
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during listener creation");
	}
	checkResultOfListenerCreation(pkgMain + iSep + llsDefaultName);
    }

   private void testSecondDefaultNameLLSViaMain(String llsTreePath, String llsDefaultName) {
	FilesystemNode node1 = null;
	NewWizardOperator wizard = null;
	try {
	    node1 = new FilesystemNode(pkgMain);
	    node1.select();
	}catch(Exception e) {
	    e.printStackTrace();
	    fail("Please check, that all mounts are done properly");
	}
	try {
	    new JavaNode(pkgMain + iSep + llsDefaultName);
	}catch(Exception e1) {
	    try {
		new NewTemplateAction().performMenu();
		wizard = new NewWizardOperator();
		new ChooseTemplateStepOperator().selectTemplate(llsTreePath);
		wizard.next();
		wizard.finish();
	    }catch(Exception e2) {
		if(wizard != null)
		    wizard.cancel();
		e2.printStackTrace();
		fail("Exception during listener creation");
	    }
	    checkResultOfListenerCreation(pkgMain + iSep + llsDefaultName);
	}
	try {
	    new NewTemplateAction().performMenu();
	    wizard = new NewWizardOperator();
	    new ChooseTemplateStepOperator().selectTemplate(llsTreePath);
	    wizard.next();
	    wizard.finish();
	}catch(Exception e3) {
	    if(wizard != null)
		wizard.cancel();
	    e3.printStackTrace();
	    fail("Exception during listener creation");
	}
	checkResultOfListenerCreation(pkgMain + iSep + llsDefaultName + "_1");
	
    }



    /*Section 1.2 LLS via Popup*/



   private void testLLSViaPopup(String llsTreePath, String llsName, String llsViaPopup) {
	FilesystemNode node1 = null;
	NewWizardOperator wizard = null;
	try {
	    node1 = new FilesystemNode(pkg);
	    node1.select();
	}catch(Exception e) {
	    e.printStackTrace();
	    fail("Please check, that all mounts are done properly");
	}
	try {
	    createPopup(llsTreePath);
	    node1.newFromTemplate(llsName);
	    wizard = new NewWizardOperator();
	    new NewObjectNameStepOperator().setName(llsViaPopup);
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during listener creation");
	}
	checkResultOfListenerCreation(pkg + iSep + llsViaPopup);
	
    }
    
   private void testSameNameLLSViaPopup(String llsTreePath, String llsName, String popularLLSViaPopup) {
	FilesystemNode node1 = null;
	NewWizardOperator wizard = null;
	try {
	    node1 = new FilesystemNode(pkg);
	    node1.select();
	}catch(Exception e) {
	    e.printStackTrace();
	    fail("Please check, that all mounts are done properly");
	}
	createPopup(llsTreePath);
	try {
	    node1.newFromTemplate(llsName);
	    wizard = new NewWizardOperator();
	    new NewObjectNameStepOperator().setName(popularLLSViaPopup);
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during listener creation");
	}
	checkResultOfListenerCreation(pkg + iSep + popularLLSViaPopup);
	try {
	    node1.select();
	    node1.newFromTemplate(llsName);
	    wizard = new NewWizardOperator();
	    new NewObjectNameStepOperator().setName(popularLLSViaPopup);
	    if(wizard.btFinish().isEnabled()) {
		wizard.cancel();
		fail("Finish button isn't disabled, if LLS with same name already exist");
	    }
	    wizard.cancel();
	}catch(Exception e2) {
	    if(wizard != null)
		wizard.cancel();
	    e2.printStackTrace();
	    fail("Exception during listener creation");
	}
    }

   private void testDefaultNameLLSViaPopup(String llsTreePath, String llsName, String llsDefaultName) {
	FilesystemNode node1 = null;
	NewWizardOperator wizard = null;
	
	try {
	    node1 = new FilesystemNode(pkg);
	    node1.select();
	}catch(Exception e) {
	    e.printStackTrace();
	    fail("Please check, that all mounts are done properly");
	}
	createPopup(llsTreePath);
	try {
	    node1.newFromTemplate(llsName);
	    wizard = new NewWizardOperator();
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during listener creation");
	}
	checkResultOfListenerCreation(pkg + iSep + llsDefaultName);
    }


   private void testSecondDefaultNameLLSViaPopup(String llsTreePath, String llsName, String llsDefaultName) {
	FilesystemNode node1 = null;
	NewWizardOperator wizard = null;
	try {
	    node1 = new FilesystemNode(pkg);
	    node1.select();
	}catch(Exception e) {
	    e.printStackTrace();
	    fail("Please check, that all mounts are done properly");
	}
	createPopup(llsTreePath);

	try {
	    new JavaNode(pkg + iSep + llsDefaultName);
	}catch(Exception e) {
	    try {
		node1.newFromTemplate(llsName);
		wizard = new NewWizardOperator();
		wizard.finish();
	    }catch(Exception e1) {
		if(wizard != null)
		    wizard.cancel();
		e1.printStackTrace();
		fail("Exception during listener creation");
	    }
	    checkResultOfListenerCreation(pkg + iSep + llsDefaultName);
	    node1.select();
	}
	try {
	    node1.newFromTemplate(llsName);
	    wizard = new NewWizardOperator();
	    wizard.finish();
	}catch(Exception e2) {
	    if(wizard != null)
		wizard.cancel();
	    e2.printStackTrace();
	    fail("Exception during listener creation");
	}
	checkResultOfListenerCreation(pkg + iSep + llsDefaultName + "_1");
	
    }

    private void createPopup(String treePath) {
	NewWizardOperator wizard = null;
	try {
	    new NewTemplateAction().performMenu();
	    wizard = new NewWizardOperator();
	    new ChooseTemplateStepOperator().selectTemplate(treePath);
	    wizard.next();
	    wizard.cancel();
	}catch(Exception e) {
	    e.printStackTrace();
	    if(wizard != null)
		wizard.cancel(); //Just remove wrong wizard window
	    fail("Something was wrong at step1. New popup menu was not created");
	}
    }


    static {
	try {
	    jspServlet = Bundle.getString("org.netbeans.modules.web.core.Bundle", "Templates/JSP_Servlet");
	    servlet = Bundle.getString("org.netbeans.modules.web.core.Bundle", "Templates/JSP_Servlet/Servlet.java");
	    listeners = Bundle.getString("org.netbeans.modules.web.core.Bundle", "Templates/JSP_Servlet/Listeners");
	    calName = Bundle.getString("org.netbeans.modules.web.core.Bundle", "Templates/JSP_Servlet/Listeners/ContextAttributeListener.java");
	    clName  = Bundle.getString("org.netbeans.modules.web.core.Bundle", "Templates/JSP_Servlet/Listeners/ContextListener.java");
	    slName = Bundle.getString("org.netbeans.modules.web.core.Bundle", "Templates/JSP_Servlet/Listeners/SessionListener.java");
	    salName  = Bundle.getString("org.netbeans.modules.web.core.Bundle", "Templates/JSP_Servlet/Listeners/SessionAttributeListener.java");
	}catch(Exception e) {
	    e.printStackTrace();
	    fail("Error during static initialization");
	}
    }


}





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