ide.kit/test/qa-functional/src/org/netbeans/test/j2ee/J2EEValidation.java
author Ivan Sidorkin <ivansidorkin@netbeans.org>
Sun Jun 29 23:31:47 2008 +0400 (4 days ago)
changeset 86933 12393ff38fda
parent 869297b1d25417369
permissions -rw-r--r--
Changed port of glassfich back to 8080
        1 /*
        2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
        3  *
        4  * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
        5  *
        6  * The contents of this file are subject to the terms of either the GNU
        7  * General Public License Version 2 only ("GPL") or the Common
        8  * Development and Distribution License("CDDL") (collectively, the
        9  * "License"). You may not use this file except in compliance with the
       10  * License. You can obtain a copy of the License at
       11  * http://www.netbeans.org/cddl-gplv2.html
       12  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
       13  * specific language governing permissions and limitations under the
       14  * License.  When distributing the software, include this License Header
       15  * Notice in each file and include the License file at
       16  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
       17  * particular file as subject to the "Classpath" exception as provided
       18  * by Sun in the GPL Version 2 section of the License file that
       19  * accompanied this code. If applicable, add the following below the
       20  * License Header, with the fields enclosed by brackets [] replaced by
       21  * your own identifying information:
       22  * "Portions Copyrighted [year] [name of copyright owner]"
       23  *
       24  * Contributor(s):
       25  *
       26  * The Original Software is NetBeans. The Initial Developer of the Original
       27  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
       28  * Microsystems, Inc. All Rights Reserved.
       29  *
       30  * If you wish your version of this file to be governed by only the CDDL
       31  * or only the GPL Version 2, indicate your decision by adding
       32  * "[Contributor] elects to include this software in this distribution
       33  * under the [CDDL or GPL Version 2] license." If you do not indicate a
       34  * single choice of license, a recipient has the option to distribute
       35  * your version of this file under either the CDDL, the GPL Version 2 or
       36  * to extend the choice of license to its licensees as provided above.
       37  * However, if you add GPL Version 2 code and therefore, elected the GPL
       38  * Version 2 license, then the option applies only if the new code is
       39  * made subject to such option by the copyright holder.
       40  */
       41 package org.netbeans.test.j2ee;
       42 
       43 import java.io.BufferedReader;
       44 import java.io.IOException;
       45 import java.io.InputStream;
       46 import java.io.InputStreamReader;
       47 import java.net.URI;
       48 import java.net.URLConnection;
       49 import junit.textui.TestRunner;
       50 import org.netbeans.jellytools.Bundle;
       51 import org.netbeans.jellytools.EditorOperator;
       52 import org.netbeans.jellytools.JellyTestCase;
       53 import org.netbeans.jellytools.MainWindowOperator;
       54 import org.netbeans.jellytools.NbDialogOperator;
       55 import org.netbeans.jellytools.NewProjectNameLocationStepOperator;
       56 import org.netbeans.jellytools.NewProjectWizardOperator;
       57 import org.netbeans.jellytools.OutputTabOperator;
       58 import org.netbeans.jellytools.ProjectsTabOperator;
       59 import org.netbeans.jellytools.actions.Action;
       60 import org.netbeans.jellytools.actions.CompileAction;
       61 import org.netbeans.jellytools.modules.j2ee.nodes.J2eeServerNode;
       62 import org.netbeans.jellytools.nodes.Node;
       63 
       64 import org.netbeans.jemmy.JemmyException;
       65 import org.netbeans.jemmy.JemmyProperties;
       66 import org.netbeans.jemmy.Waitable;
       67 import org.netbeans.jemmy.Waiter;
       68 import org.netbeans.jemmy.operators.JCheckBoxOperator;
       69 import org.netbeans.jemmy.operators.JTreeOperator;
       70 
       71 import org.netbeans.junit.NbTestSuite;
       72 import org.netbeans.junit.NbModuleSuite;
       73 import org.netbeans.test.ide.WatchProjects;
       74 
       75 /**
       76  * Overall validation suite for j2ee cluster.
       77  *
       78  * @author Jiri.Skrivanek@sun.com
       79  */
       80 public class J2EEValidation extends JellyTestCase {
       81 
       82     static final String [] tests = {
       83                 "testWebApplication"
       84     };
       85     
       86     /** Need to be defined because of JUnit */
       87     public J2EEValidation(String name) {
       88         super(name);
       89     }
       90 
       91 //    public static NbTestSuite suite() {
       92 //        NbTestSuite suite = new NbTestSuite();
       93 //        suite.addTest(new J2EEValidation("testWebApplication"));
       94 //        return suite;
       95 //    }
       96     public static junit.framework.Test suite() {
       97         return NbModuleSuite.create(
       98                 NbModuleSuite.createConfiguration(J2EEValidation.class)
       99                 .addTest(tests)
      100                 .clusters(".*")
      101                 .enableModules(".*")
      102                 .gui(true)
      103                 );
      104     }
      105 
      106 
      107     /** Use for execution inside IDE */
      108     public static void main(java.lang.String[] args) {
      109         // run whole suite
      110         TestRunner.run(suite());
      111         // run only selected test case
      112         //junit.textui.TestRunner.run(new IDEValidation("testMainMenu"));
      113     }
      114     
      115     @Override
      116     public void setUp() {
      117         System.out.println("########  "+getName()+"  #######");
      118     }
      119     
      120     // name of sample web application project
      121     private static final String SAMPLE_WEB_PROJECT_NAME = "SampleWebProject";  //NOI18N
      122 
      123     /** Test Web Application
      124      * - create new Web Application project
      125      * - wait until project is in Projects view
      126      * - wait classpath scanning finished
      127      * - set option to not open browser at run
      128      * - insert error statement into index.jsp, compile it and verify it failed
      129      * - correct error in index.jsp, compile it and verify it succeded
      130      * - run project from context menu on project's root node
      131      * - wait until JSP Page is accessible through HTTP connection
      132      * - stop application server
      133      */
      134     public void testWebApplication() throws Exception {
      135         // create new web application project
      136         NewProjectWizardOperator npwo = NewProjectWizardOperator.invoke();
      137         // "Web"
      138         String webLabel = Bundle.getString("org.netbeans.modules.web.core.Bundle", "Templates/JSP_Servlet");
      139         npwo.selectCategory(webLabel);
      140         // "Web Application"
      141         String webApplicationLabel = org.netbeans.jellytools.Bundle.getString("org.netbeans.modules.web.project.ui.wizards.Bundle", "Templates/Project/Web/emptyWeb.xml");
      142         npwo.selectProject(webApplicationLabel);
      143         npwo.next();
      144         NewProjectNameLocationStepOperator npnlso = new NewProjectNameLocationStepOperator();
      145         npnlso.txtProjectName().setText(SAMPLE_WEB_PROJECT_NAME);
      146         npnlso.txtProjectLocation().setText(System.getProperty("netbeans.user")); // NOI18N
      147         npnlso.next();
      148         npnlso.finish();
      149         // wait project appear in projects view
      150         // wait 30 second
      151         JemmyProperties.setCurrentTimeout("JTreeOperator.WaitNextNodeTimeout", 30000); // NOI18N
      152         new ProjectsTabOperator().getProjectRootNode(SAMPLE_WEB_PROJECT_NAME);
      153         // wait index.jsp is opened in editor
      154         EditorOperator editor = new EditorOperator("index.jsp"); // NOI18N
      155         // wait classpath scanning finished
      156         WatchProjects.waitScanFinished();
      157         
      158         // not display browser on run
      159         
      160         // open project properties
      161         new ProjectsTabOperator().getProjectRootNode(SAMPLE_WEB_PROJECT_NAME).properties();
      162         // "Project Properties"
      163         String projectPropertiesTitle = Bundle.getStringTrimmed("org.netbeans.modules.web.project.ui.customizer.Bundle", "LBL_Customizer_Title");
      164         NbDialogOperator propertiesDialogOper = new NbDialogOperator(projectPropertiesTitle);
      165         // "Run"
      166         String runLabel = Bundle.getString("org.netbeans.modules.web.project.ui.customizer.Bundle", "LBL_Config_Run");
      167         // select "Run" category
      168         new Node(new JTreeOperator(propertiesDialogOper), runLabel).select();
      169         String displayBrowserLabel = Bundle.getStringTrimmed("org.netbeans.modules.web.project.ui.customizer.Bundle", "LBL_CustomizeRun_DisplayBrowser_JCheckBox");
      170         new JCheckBoxOperator(propertiesDialogOper, displayBrowserLabel).setSelected(false);
      171         // confirm properties dialog
      172         propertiesDialogOper.ok();
      173 
      174         // Compile JSP
      175         
      176         Node projectRootNode = new ProjectsTabOperator().getProjectRootNode(SAMPLE_WEB_PROJECT_NAME);
      177         // "Web Pages"
      178         String webPagesLabel = Bundle.getString(
      179                 "org.netbeans.modules.web.project.ui.Bundle", "LBL_Node_DocBase");
      180         Node jspNode = new Node(projectRootNode, webPagesLabel+"|index.jsp"); // NOI18N
      181         // insert error statement
      182         editor.insert("<%= nonExistentVar %>", 12, 1);
      183         
      184         CompileAction compileAction = new CompileAction();
      185         compileAction.perform(jspNode);
      186         // "SampleWebProject (compile-single-jsp)"
      187         String outputTarget = Bundle.getString(
      188                 "org.apache.tools.ant.module.run.Bundle", "TITLE_output_target", 
      189                 new Object[] {SAMPLE_WEB_PROJECT_NAME, null, "compile-single-jsp"});  // NOI18N
      190         // "Build of SampleWebProject (compile-single-jsp) failed."
      191         String failedMessage = Bundle.getString(
      192                 "org.apache.tools.ant.module.run.Bundle", "FMT_target_failed_status",
      193                 new Object[] {outputTarget});
      194         // "Finished building SampleWebProject (compile-single-jsp)"
      195         String finishedMessage = Bundle.getString(
      196                 "org.apache.tools.ant.module.run.Bundle", "FMT_finished_target_status", 
      197                 new Object[] {outputTarget});
      198         MainWindowOperator.getDefault().waitStatusText(failedMessage);
      199         // check error message is printed
      200         new OutputTabOperator("compile-single-jsp").waitText("nonExistentVar"); // NOI18N
      201         // correct JSP file
      202         editor.replace("<%= nonExistentVar %>", "");
      203         // compile again
      204         compileAction.perform(jspNode);
      205         MainWindowOperator.getDefault().waitStatusText(finishedMessage);
      206 
      207         // Run project
      208         try {
      209             // "Run Project"
      210             String runProjectItem = Bundle.getString("org.netbeans.modules.web.project.ui.Bundle", "LBL_RunAction_Name");
      211             new Action(null, runProjectItem).perform(new ProjectsTabOperator().getProjectRootNode(SAMPLE_WEB_PROJECT_NAME));
      212             waitText(SAMPLE_WEB_PROJECT_NAME, 240000, "JSP Page");
      213         } finally {
      214             // log messages from output
      215             getLog("RunOutput").print(new OutputTabOperator(SAMPLE_WEB_PROJECT_NAME).getText()); // NOI18N
      216             getLog("ServerLog").print(new OutputTabOperator("GlassFish").getText());
      217             // stop server
      218             try {
      219                 J2eeServerNode serverNode = new J2eeServerNode("GlassFish");
      220                 serverNode.stop();
      221             } catch (JemmyException e) {
      222                 // ignore it
      223             }
      224         }
      225     }
      226     
      227     /** Opens URL connection and waits for given text. It thows TimeoutExpiredException
      228      * if timeout expires.
      229      * @param urlSuffix suffix added to server URL
      230      * @param timeout time to wait
      231      * @param text text to be found
      232      */
      233     public static void waitText(final String urlSuffix, final long timeout, final String text) {
      234         Waitable waitable = new Waitable() {
      235             public Object actionProduced(Object obj) {
      236                 InputStream is = null;
      237                 try {
      238                     URLConnection connection = new URI("http://localhost:8080/"+urlSuffix).toURL().openConnection();
      239                     connection.setReadTimeout(Long.valueOf(timeout).intValue());
      240                     is = connection.getInputStream();
      241                     BufferedReader br = new BufferedReader(new InputStreamReader(is));
      242                     String line = br.readLine();
      243                     while(line != null) {
      244                         if(line.indexOf(text) > -1) {
      245                             return Boolean.TRUE;
      246                         }
      247                         line = br.readLine();
      248                     }
      249                     is.close();
      250                 } catch (Exception e) {
      251                     //e.printStackTrace();
      252                     return null;
      253                 } finally {
      254                     if(is != null) {
      255                         try {
      256                             is.close();
      257                         } catch (IOException e) {
      258                             // ignore
      259                         }
      260                     }
      261                 }
      262                 return null;
      263             }
      264             public String getDescription() {
      265                 return("Text \""+text+"\" at http://localhost:8090/"+urlSuffix);
      266             }
      267         };
      268         Waiter waiter = new Waiter(waitable);
      269         waiter.getTimeouts().setTimeout("Waiter.WaitingTime", timeout);
      270         try {
      271             waiter.waitAction(null);
      272         } catch (InterruptedException e) {
      273             throw new JemmyException("Exception while waiting for connection.", e);
      274         }
      275     }
      276 }