001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.test.j2ee;
042:
043: import java.io.BufferedReader;
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.io.InputStreamReader;
047: import java.net.URI;
048: import java.net.URLConnection;
049: import junit.textui.TestRunner;
050: import org.netbeans.jellytools.Bundle;
051: import org.netbeans.jellytools.EditorOperator;
052: import org.netbeans.jellytools.JellyTestCase;
053: import org.netbeans.jellytools.MainWindowOperator;
054: import org.netbeans.jellytools.NbDialogOperator;
055: import org.netbeans.jellytools.NewProjectNameLocationStepOperator;
056: import org.netbeans.jellytools.NewProjectWizardOperator;
057: import org.netbeans.jellytools.OutputTabOperator;
058: import org.netbeans.jellytools.ProjectsTabOperator;
059: import org.netbeans.jellytools.actions.Action;
060: import org.netbeans.jellytools.actions.CompileAction;
061: import org.netbeans.jellytools.modules.j2ee.nodes.J2eeServerNode;
062: import org.netbeans.jellytools.nodes.Node;
063:
064: import org.netbeans.jemmy.JemmyException;
065: import org.netbeans.jemmy.JemmyProperties;
066: import org.netbeans.jemmy.Waitable;
067: import org.netbeans.jemmy.Waiter;
068: import org.netbeans.jemmy.operators.JCheckBoxOperator;
069: import org.netbeans.jemmy.operators.JTreeOperator;
070:
071: import org.netbeans.junit.NbTestSuite;
072: import org.netbeans.junit.ide.ProjectSupport;
073:
074: /**
075: * Overall validation suite for j2ee cluster.
076: *
077: * @author Jiri.Skrivanek@sun.com
078: */
079: public class J2EEValidation extends JellyTestCase {
080:
081: /** Need to be defined because of JUnit */
082: public J2EEValidation(String name) {
083: super (name);
084: }
085:
086: public static NbTestSuite suite() {
087: NbTestSuite suite = new NbTestSuite();
088: suite.addTest(new J2EEValidation("testWebApplication"));
089: return suite;
090: }
091:
092: /** Use for execution inside IDE */
093: public static void main(java.lang.String[] args) {
094: // run whole suite
095: TestRunner.run(suite());
096: // run only selected test case
097: //junit.textui.TestRunner.run(new IDEValidation("testMainMenu"));
098: }
099:
100: @Override
101: public void setUp() {
102: System.out.println("######## " + getName() + " #######");
103: }
104:
105: // name of sample web application project
106: private static final String SAMPLE_WEB_PROJECT_NAME = "SampleWebProject"; //NOI18N
107:
108: /** Test Web Application
109: * - create new Web Application project
110: * - wait until project is in Projects view
111: * - wait classpath scanning finished
112: * - set option to not open browser at run
113: * - insert error statement into index.jsp, compile it and verify it failed
114: * - correct error in index.jsp, compile it and verify it succeded
115: * - run project from context menu on project's root node
116: * - wait until JSP Page is accessible through HTTP connection
117: * - stop application server
118: */
119: public void testWebApplication() throws Exception {
120: // create new web application project
121: NewProjectWizardOperator npwo = NewProjectWizardOperator
122: .invoke();
123: // "Web"
124: String webLabel = Bundle.getString(
125: "org.netbeans.modules.web.core.Bundle",
126: "Templates/JSP_Servlet");
127: npwo.selectCategory(webLabel);
128: // "Web Application"
129: String webApplicationLabel = org.netbeans.jellytools.Bundle
130: .getString(
131: "org.netbeans.modules.web.project.ui.wizards.Bundle",
132: "Templates/Project/Web/emptyWeb.xml");
133: npwo.selectProject(webApplicationLabel);
134: npwo.next();
135: NewProjectNameLocationStepOperator npnlso = new NewProjectNameLocationStepOperator();
136: npnlso.txtProjectName().setText(SAMPLE_WEB_PROJECT_NAME);
137: npnlso.txtProjectLocation().setText(
138: System.getProperty("netbeans.user")); // NOI18N
139: npnlso.next();
140: npnlso.finish();
141: // wait project appear in projects view
142: // wait 30 second
143: JemmyProperties.setCurrentTimeout(
144: "JTreeOperator.WaitNextNodeTimeout", 30000); // NOI18N
145: new ProjectsTabOperator()
146: .getProjectRootNode(SAMPLE_WEB_PROJECT_NAME);
147: // wait index.jsp is opened in editor
148: EditorOperator editor = new EditorOperator("index.jsp"); // NOI18N
149: // wait classpath scanning finished
150: ProjectSupport.waitScanFinished();
151:
152: // not display browser on run
153:
154: // open project properties
155: new ProjectsTabOperator().getProjectRootNode(
156: SAMPLE_WEB_PROJECT_NAME).properties();
157: // "Project Properties"
158: String projectPropertiesTitle = Bundle
159: .getStringTrimmed(
160: "org.netbeans.modules.web.project.ui.customizer.Bundle",
161: "LBL_Customizer_Title");
162: NbDialogOperator propertiesDialogOper = new NbDialogOperator(
163: projectPropertiesTitle);
164: // "Run"
165: String runLabel = Bundle
166: .getString(
167: "org.netbeans.modules.web.project.ui.customizer.Bundle",
168: "LBL_Config_Run");
169: // select "Run" category
170: new Node(new JTreeOperator(propertiesDialogOper), runLabel)
171: .select();
172: String displayBrowserLabel = Bundle
173: .getStringTrimmed(
174: "org.netbeans.modules.web.project.ui.customizer.Bundle",
175: "LBL_CustomizeRun_DisplayBrowser_JCheckBox");
176: new JCheckBoxOperator(propertiesDialogOper, displayBrowserLabel)
177: .setSelected(false);
178: // confirm properties dialog
179: propertiesDialogOper.ok();
180:
181: // Compile JSP
182:
183: Node projectRootNode = new ProjectsTabOperator()
184: .getProjectRootNode(SAMPLE_WEB_PROJECT_NAME);
185: // "Web Pages"
186: String webPagesLabel = Bundle.getString(
187: "org.netbeans.modules.web.project.ui.Bundle",
188: "LBL_Node_DocBase");
189: Node jspNode = new Node(projectRootNode, webPagesLabel
190: + "|index.jsp"); // NOI18N
191: // insert error statement
192: editor.insert("<%= nonExistentVar %>", 12, 1);
193:
194: CompileAction compileAction = new CompileAction();
195: compileAction.perform(jspNode);
196: // "SampleWebProject (compile-single-jsp)"
197: String outputTarget = Bundle.getString(
198: "org.apache.tools.ant.module.run.Bundle",
199: "TITLE_output_target", new Object[] {
200: SAMPLE_WEB_PROJECT_NAME, null,
201: "compile-single-jsp" }); // NOI18N
202: // "Build of SampleWebProject (compile-single-jsp) failed."
203: String failedMessage = Bundle.getString(
204: "org.apache.tools.ant.module.run.Bundle",
205: "FMT_target_failed_status",
206: new Object[] { outputTarget });
207: // "Finished building SampleWebProject (compile-single-jsp)"
208: String finishedMessage = Bundle.getString(
209: "org.apache.tools.ant.module.run.Bundle",
210: "FMT_finished_target_status",
211: new Object[] { outputTarget });
212: MainWindowOperator.getDefault().waitStatusText(failedMessage);
213: // check error message is printed
214: new OutputTabOperator("compile-single-jsp")
215: .waitText("nonExistentVar"); // NOI18N
216: // correct JSP file
217: editor.replace("<%= nonExistentVar %>", "");
218: // compile again
219: compileAction.perform(jspNode);
220: MainWindowOperator.getDefault().waitStatusText(finishedMessage);
221:
222: // Run project
223: try {
224: // "Run Project"
225: String runProjectItem = Bundle.getString(
226: "org.netbeans.modules.web.project.ui.Bundle",
227: "LBL_RunAction_Name");
228: new Action(null, runProjectItem)
229: .perform(new ProjectsTabOperator()
230: .getProjectRootNode(SAMPLE_WEB_PROJECT_NAME));
231: waitText(SAMPLE_WEB_PROJECT_NAME, 240000, "JSP Page");
232: } finally {
233: // log messages from output
234: getLog("RunOutput").print(
235: new OutputTabOperator(SAMPLE_WEB_PROJECT_NAME)
236: .getText()); // NOI18N
237: getLog("ServerLog").print(
238: new OutputTabOperator("GlassFish").getText());
239: // stop server
240: try {
241: J2eeServerNode serverNode = new J2eeServerNode(
242: "GlassFish");
243: serverNode.stop();
244: } catch (JemmyException e) {
245: // ignore it
246: }
247: }
248: }
249:
250: /** Opens URL connection and waits for given text. It thows TimeoutExpiredException
251: * if timeout expires.
252: * @param urlSuffix suffix added to server URL
253: * @param timeout time to wait
254: * @param text text to be found
255: */
256: public static void waitText(final String urlSuffix,
257: final long timeout, final String text) {
258: Waitable waitable = new Waitable() {
259: public Object actionProduced(Object obj) {
260: InputStream is = null;
261: try {
262: URLConnection connection = new URI(
263: "http://localhost:8080/" + urlSuffix)
264: .toURL().openConnection();
265: connection.setReadTimeout(Long.valueOf(timeout)
266: .intValue());
267: is = connection.getInputStream();
268: BufferedReader br = new BufferedReader(
269: new InputStreamReader(is));
270: String line = br.readLine();
271: while (line != null) {
272: if (line.indexOf(text) > -1) {
273: return Boolean.TRUE;
274: }
275: line = br.readLine();
276: }
277: is.close();
278: } catch (Exception e) {
279: //e.printStackTrace();
280: return null;
281: } finally {
282: if (is != null) {
283: try {
284: is.close();
285: } catch (IOException e) {
286: // ignore
287: }
288: }
289: }
290: return null;
291: }
292:
293: public String getDescription() {
294: return ("Text \"" + text
295: + "\" at http://localhost:8080/" + urlSuffix);
296: }
297: };
298: Waiter waiter = new Waiter(waitable);
299: waiter.getTimeouts().setTimeout("Waiter.WaitingTime", timeout);
300: try {
301: waiter.waitAction(null);
302: } catch (InterruptedException e) {
303: throw new JemmyException(
304: "Exception while waiting for connection.", e);
305: }
306: }
307: }
|