2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
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]"
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.
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.
41 package org.netbeans.test.j2ee;
43 import java.io.BufferedReader;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.io.InputStreamReader;
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;
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;
71 import org.netbeans.junit.NbTestSuite;
72 import org.netbeans.junit.NbModuleSuite;
73 import org.netbeans.test.ide.WatchProjects;
76 * Overall validation suite for j2ee cluster.
78 * @author Jiri.Skrivanek@sun.com
80 public class J2EEValidation extends JellyTestCase {
82 static final String [] tests = {
86 /** Need to be defined because of JUnit */
87 public J2EEValidation(String name) {
91 // public static NbTestSuite suite() {
92 // NbTestSuite suite = new NbTestSuite();
93 // suite.addTest(new J2EEValidation("testWebApplication"));
96 public static junit.framework.Test suite() {
97 return NbModuleSuite.create(
98 NbModuleSuite.createConfiguration(J2EEValidation.class)
107 /** Use for execution inside IDE */
108 public static void main(java.lang.String[] args) {
110 TestRunner.run(suite());
111 // run only selected test case
112 //junit.textui.TestRunner.run(new IDEValidation("testMainMenu"));
116 public void setUp() {
117 System.out.println("######## "+getName()+" #######");
120 // name of sample web application project
121 private static final String SAMPLE_WEB_PROJECT_NAME = "SampleWebProject"; //NOI18N
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
134 public void testWebApplication() throws Exception {
135 // create new web application project
136 NewProjectWizardOperator npwo = NewProjectWizardOperator.invoke();
138 String webLabel = Bundle.getString("org.netbeans.modules.web.core.Bundle", "Templates/JSP_Servlet");
139 npwo.selectCategory(webLabel);
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);
144 NewProjectNameLocationStepOperator npnlso = new NewProjectNameLocationStepOperator();
145 npnlso.txtProjectName().setText(SAMPLE_WEB_PROJECT_NAME);
146 npnlso.txtProjectLocation().setText(System.getProperty("netbeans.user")); // NOI18N
149 // wait project appear in projects view
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();
158 // not display browser on run
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);
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();
176 Node projectRootNode = new ProjectsTabOperator().getProjectRootNode(SAMPLE_WEB_PROJECT_NAME);
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);
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
202 editor.replace("<%= nonExistentVar %>", "");
204 compileAction.perform(jspNode);
205 MainWindowOperator.getDefault().waitStatusText(finishedMessage);
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");
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());
219 J2eeServerNode serverNode = new J2eeServerNode("GlassFish");
221 } catch (JemmyException e) {
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
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;
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) {
247 line = br.readLine();
250 } catch (Exception e) {
251 //e.printStackTrace();
257 } catch (IOException e) {
264 public String getDescription() {
265 return("Text \""+text+"\" at http://localhost:8090/"+urlSuffix);
268 Waiter waiter = new Waiter(waitable);
269 waiter.getTimeouts().setTimeout("Waiter.WaitingTime", timeout);
271 waiter.waitAction(null);
272 } catch (InterruptedException e) {
273 throw new JemmyException("Exception while waiting for connection.", e);