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.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.Utils;
import org.netbeans.web.test.util.RepositoryNodeWaitable;

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.jemmy.operators.Operator;

import org.netbeans.junit.NbTestSuite;


public class NewServletFromTemplate extends JellyTestCase { 

    private static String servletTreePath = null;
    private static String wmName = "wm1";
    private static String webInf = "WEB-INF";
    private static String classes = "Classes";
    private static String popupDefaultPackage = "creation";
    private static String mainDefaultPackage = "creationMain";
    private static String existing = "existing";
    private static String nonexisting = "nonexisting";
    private static String servletViaMain = "NewViaMain";
    private static String servletViaPopup = "NewViaPopup";
    private static String popularServletViaMain = "NewWithPopularNameViaMain";
    private static String popularServletViaPopup = "NewWithPopularNameViaPopup";
    private static String existPkgTextFieldServlet = "NewInExistingPackageInTextField";
    private static String existPkgTreeServlet = "NewInExistingPackageInTree";
    private static String nonexistPkgServlet = "NewInNonexistingPackage";
    private static String outsideWMServlet = "NewOutsideOfWebModule";
    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 jspServlet = null;
    private static String servlet = null;


    public NewServletFromTemplate(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;
	servletTreePath = jspServlet + iSep + servlet;
	
	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(NewServletFromTemplate.class);
        
    }


    /* Tests section */
   

    public void testSelectedPackageViaMain() {
	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(servletTreePath);
	    wizard.next();
	    new TargetLocationStepOperator().setName(servletViaMain);
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during servlet creation");
	}
	checkResultOfServletCreation(pkgMain + iSep + servletViaMain);
    }

    public void testSameNameInSelectedPackageViaMain() {
	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(servletTreePath);
	    wizard.next();
	    new TargetLocationStepOperator().setName(popularServletViaMain);
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during servlet creation");
	}

	checkResultOfServletCreation(pkgMain + iSep + popularServletViaMain);

	try {
	    node1.select();
	    new NewTemplateAction().performMenu();
	    wizard = new NewWizardOperator();
	    new ChooseTemplateStepOperator().selectTemplate(servletTreePath);
	    wizard.next();
	    new TargetLocationStepOperator().setName(popularServletViaMain);
	    if(wizard.btFinish().isEnabled()) {
		wizard.cancel();
		fail("Finish button isn't disabled, if Servlet with same name already exist");
	    }
	    wizard.cancel();
	}catch(Exception e2) {
	    if(wizard != null)
		wizard.cancel();
	    e2.printStackTrace();
	    fail("Exception during servlet creation");
	}
    }

    public void testDefaultNameInSelectedPackageViaMain() {
	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(servletTreePath);
	    wizard.next();
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during servlet creation");
	}
	checkResultOfServletCreation(pkgMain + iSep + "Servlet");
	
    }

    public void testSecondDefaultNameInSelectedPackageViaMain() {
	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 {
	    JavaNode node2 = new JavaNode(pkgMain + iSep + "Servlet");
	}catch(Exception e) {
	    try {
		new NewTemplateAction().performMenu();
		wizard = new NewWizardOperator();
		new ChooseTemplateStepOperator().selectTemplate(servletTreePath);
		wizard.next();
		wizard.finish();
	    }catch(Exception e1) {
		if(wizard != null)
		    wizard.cancel();
		e1.printStackTrace();
		fail("Exception during servlet creation");
	    }
	    checkResultOfServletCreation(pkgMain + iSep + "Servlet");
	}
	try {
	    new NewTemplateAction().performMenu();
	    wizard = new NewWizardOperator();
	    new ChooseTemplateStepOperator().selectTemplate(servletTreePath);
	    wizard.next();
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during servlet creation");
	}
	checkResultOfServletCreation(pkgMain + iSep + "Servlet_1");
    }

    public void testInExistingNonSelectedPackageInTextField() {
	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(servletTreePath);
	    wizard.next();
	    TargetLocationStepOperator tlso = new TargetLocationStepOperator();
	    tlso.txtPackage().setText("creationMain/existing");
	    tlso.create();
	    tlso.setName(existPkgTextFieldServlet);
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during servlet creation");
	}
	checkResultOfServletCreation(pkgMain + iSep + "existing" + iSep + existPkgTextFieldServlet);
    }

    public void testInExistingNonSelectedPackageInTree() {
	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(servletTreePath);
	    wizard.next();
	    TargetLocationStepOperator tlso = new TargetLocationStepOperator();
	    tlso.setName(existPkgTreeServlet);
	    tlso.selectLocation(pkgMain + iSep + existing);
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during servlet creation");
	}
	checkResultOfServletCreation(pkgMain + iSep + existing + iSep + existPkgTreeServlet);
    }

    public void testInNonexistingPackage() {
	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(servletTreePath);
	    wizard.next();
	    TargetLocationStepOperator tlso = new TargetLocationStepOperator();
	    tlso.setName(nonexistPkgServlet);
	    tlso.txtPackage().setText("creationMain/nonexisting");
	    tlso.create();
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during servlet creation");
	}
	checkResultOfServletCreation(pkgMain + iSep + nonexisting + iSep + nonexistPkgServlet);
	
    }

    public void testOutsideOfWebModule() {
	FilesystemNode node1 = null;
	NewWizardOperator wizard = null;
	try {
	    node1 = new FilesystemNode(webModule);
	    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(servletTreePath);
	    wizard.next();
	    TargetLocationStepOperator tlso = new TargetLocationStepOperator();
	    tlso.setName(outsideWMServlet);
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during servlet creation");
	}
	checkResultOfServletCreation(webModule + iSep + outsideWMServlet);
	
    }
    



    //PopupSection

    public void testSelectedPackageViaPopup() {
	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(servletTreePath);
	try {
	    node1.newFromTemplate(servlet);
	    wizard = new NewWizardOperator();
	    new NewObjectNameStepOperator().setName(servletViaPopup);
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during servlet creation");
	}
	checkResultOfServletCreation(pkg + iSep + servletViaPopup);
    }

    public void testSameNameInSelectedPackageViaPopup() {
	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(servletTreePath);
	try {
	    node1.newFromTemplate(servlet);
	    wizard = new NewWizardOperator();
	    new NewObjectNameStepOperator().setName(popularServletViaPopup);
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during servlet creation");
	}

	checkResultOfServletCreation(pkg + iSep + popularServletViaPopup);

	node1.select();
	node1.newFromTemplate(servlet);
        wizard = new NewWizardOperator();
	new NewObjectNameStepOperator().setName(popularServletViaPopup);
	if(wizard.btFinish().isEnabled()) {
	    wizard.cancel();
	    fail("Finish button isn't disabled, if Servlet with same name already exist");
	}
	wizard.cancel();
    }

    public void testDefaultNameInSelectedPackageViaPopup() {
	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(servletTreePath);
	try {
	    node1.newFromTemplate(servlet);
	    wizard = new NewWizardOperator();
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during servlet creation");
	}
	checkResultOfServletCreation(pkg + iSep + "Servlet");
	
    }

    public void testSecondDefaultNameInSelectedPackageViaPopup() {
	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(servletTreePath);
	try {
	    JavaNode node2 = new JavaNode(pkg + iSep + "Servlet");
	}catch(Exception e) {
	    try {
		node1.newFromTemplate(servlet);
		wizard = new NewWizardOperator();
		wizard.finish();
	    }catch(Exception e1) {
		if(wizard != null)
		    wizard.cancel();
		e1.printStackTrace();
		fail("Exception during servlet creation");
	    }
	    checkResultOfServletCreation(pkg + iSep + "Servlet");
	    node1.select();
	}
	try {
	    node1.newFromTemplate(servlet);
	    wizard = new NewWizardOperator();
	    wizard.finish();
	}catch(Exception e1) {
	    if(wizard != null)
		wizard.cancel();
	    e1.printStackTrace();
	    fail("Exception during servlet creation");
	}
	checkResultOfServletCreation(pkg + iSep + "Servlet_1");
    }


    /**/
    


    //Private methods


    private void checkResultOfServletCreation(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("Servlet 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);
	}
    }
   
    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");
	}
    }


    //This static block used to init variables, common for all tests



    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");
	}catch(Exception e) {
	    e.printStackTrace();
	    fail("Error during static initialization. Check Bundle paths.");
	}
    }
} 
... 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.