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:
007: The contents of this file are subject to the terms of either the GNU
008: General Public License Version 2 only ("GPL") or the Common
009: Development and Distribution License("CDDL") (collectively, the
010: "License"). You may not use this file except in compliance with the
011: License. You can obtain a copy of the License at
012: http://www.netbeans.org/cddl-gplv2.html
013: or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
014: specific language governing permissions and limitations under the
015: License. When distributing the software, include this License Header
016: Notice in each file and include the License file at
017: nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
018: particular file as subject to the "Classpath" exception as provided
019: by Sun in the GPL Version 2 section of the License file that
020: accompanied this code. If applicable, add the following below the
021: License Header, with the fields enclosed by brackets [] replaced by
022: your own identifying information:
023: "Portions Copyrighted [year] [name of copyright owner]"
024:
025: Contributor(s):
026:
027: The Original Software is NetBeans. The Initial Developer of the Original
028: Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
029: Microsystems, Inc. All Rights Reserved.
030:
031: If you wish your version of this file to be governed by only the CDDL
032: or only the GPL Version 2, indicate your decision by adding
033: "[Contributor] elects to include this software in this distribution
034: under the [CDDL or GPL Version 2] license." If you do not indicate a
035: single choice of license, a recipient has the option to distribute
036: your version of this file under either the CDDL, the GPL Version 2 or
037: to extend the choice of license to its licensees as provided above.
038: However, if you add GPL Version 2 code and therefore, elected the GPL
039: Version 2 license, then the option applies only if the new code is
040: made subject to such option by the copyright holder.
041: */
042: package org.netbeans.test.dataprovider.common;
043:
044: import javax.swing.*;
045: import javax.swing.tree.*;
046: import javax.swing.text.*;
047: import java.util.regex.*;
048: import org.netbeans.modules.visualweb.gravy.*;
049: import org.netbeans.jemmy.*;
050: import com.meterware.httpunit.*;
051: import org.netbeans.modules.visualweb.gravy.dataconnectivity.ServerNavigatorOperator;
052:
053: public class WebUtils implements Constants {
054: public static void runProject(String prjName) {
055: Utils.callPopupMenuOnProjectsTreeNode(prjName,
056: POPUP_MENU_ITEM_RUN);
057: if (isDeploymentSuccessful(prjName)) {
058: Utils.logMsg("+++ Project [" + prjName
059: + "] has been deployed successfully");
060: } else {
061: throw new RuntimeException("Project [" + prjName
062: + "] wasn't deployed successfully");
063: }
064: }
065:
066: private static boolean isDeploymentSuccessful(String prjName) {
067: String serverType = TestPropertiesHandler
068: .getServerProperty("Application_Server_Type");
069: if (Pattern.matches("Sun" + PATTERN_ANY_CHARS
070: + "Application Server", serverType)) {
071: return (isDeploymentSuccessful_SunAppServer(prjName));
072: }
073: throw new RuntimeException(
074: "Method isn't defined for an application server type ["
075: + serverType + "]");
076: }
077:
078: private static boolean isDeploymentSuccessful_SunAppServer(
079: String prjName) {
080: TreePath treePath = null;
081: for (int i = 0; i < 30; ++i) {
082: try {
083: Utils.putFocusOnWindowServices();
084: Utils.callPopupMenuOnServicesTreeNode(
085: getWebApplicationsTreeNodeName_SunAppServer(),
086: POPUP_MENU_ITEM_REFRESH);
087: //treePath = Utils.findServicesTreeNode(getDeployedApplicationTreeNodeName_SunAppServer(prjName),
088: // 2000, true);
089: String appServerName = TestPropertiesHandler
090: .getServerProperty("Application_Server_Name");
091: treePath = findDeployedProjectNode(new String[] {
092: appServerName, SERVICES_TREE_NODE_APPLICATIONS,
093: SERVICES_TREE_NODE_WEB_APPLICATIONS }, prjName);
094: if (treePath != null) {
095: Util.wait(1500);
096: new QueueTool().waitEmpty();
097: return true;
098: }
099: } catch (Exception e) {
100: e.printStackTrace(Utils.logStream);
101: }
102: }
103: return false;
104: }
105:
106: public static void undeployProject(String prjName) {
107: String serverType = TestPropertiesHandler
108: .getServerProperty("Application_Server_Type");
109: if (Pattern.matches("Sun" + PATTERN_ANY_CHARS
110: + "Application Server", serverType)) {
111: undeployProject_SunAppServer(prjName);
112: return;
113: }
114: throw new RuntimeException(
115: "Method isn't defined for an application server type of ["
116: + serverType + "]");
117: }
118:
119: private static void undeployProject_SunAppServer(String prjName) {
120: TreePath treePath = null;
121: String deployedAppTreeNodeName = getDeployedApplicationTreeNodeName_SunAppServer(prjName);
122: boolean isUndeployActionPerformed = false;
123: for (int i = 0; i < 30; ++i) {
124: try {
125: Utils.putFocusOnWindowServices();
126: Utils.callPopupMenuOnServicesTreeNode(
127: getWebApplicationsTreeNodeName_SunAppServer(),
128: POPUP_MENU_ITEM_REFRESH);
129:
130: String appServerName = TestPropertiesHandler
131: .getServerProperty("Application_Server_Name");
132: treePath = findDeployedProjectNode(new String[] {
133: appServerName, SERVICES_TREE_NODE_APPLICATIONS,
134: SERVICES_TREE_NODE_WEB_APPLICATIONS }, prjName);
135: if (treePath == null) {
136: if (isUndeployActionPerformed) {
137: Utils.logMsg("+++ Project [" + prjName
138: + "] has been undeployed successfully");
139: }
140: return;
141: } else {
142: if (!isUndeployActionPerformed) {
143: Utils.callPopupMenuOnServicesTreeNode(
144: deployedAppTreeNodeName,
145: POPUP_MENU_ITEM_UNDEPLOY);
146: Util.wait(2000);
147: new QueueTool().waitEmpty();
148: isUndeployActionPerformed = true;
149: }
150: }
151: } catch (Exception e) {
152: e.printStackTrace(Utils.logStream);
153: }
154: }
155: throw new RuntimeException(
156: "Problem with undeployment of project [" + prjName
157: + "]");
158: }
159:
160: private static TreePath findDeployedProjectNode(
161: String[] parentNodeNames, String prjName) {
162: //String deployedAppTreeNodeName = getDeployedApplicationTreeNodeName_SunAppServer(prjName);
163: //treePath = Utils.findServicesTreeNode(deployedAppTreeNodeName, 2000, false);
164: JTree jTree = (JTree) new ServerNavigatorOperator().getTree()
165: .getSource();
166: int startTreeRow = 0;
167: TreePath treePath = null;
168: for (String parentNodeName : parentNodeNames) {
169: treePath = expandTreePath(jTree, parentNodeName,
170: startTreeRow);
171: startTreeRow = jTree.getRowForPath(treePath);
172: }
173: TreeModel treeModel = jTree.getModel();
174: int deployedProjectCount = treeModel.getChildCount(treePath
175: .getLastPathComponent());
176: for (int i = 0; i < deployedProjectCount; i++) {
177: Object nodeChild = treeModel.getChild(treePath
178: .getLastPathComponent(), i);
179: TreePath deployedProjectTreePath = treePath
180: .pathByAddingChild(nodeChild);
181: Utils.logMsg("+++ Deployed project treePath = "
182: + deployedProjectTreePath.toString());
183:
184: Util.wait(800);
185: new QueueTool().waitEmpty();
186: if (isTreeNodeLabelEqualsPrjName(deployedProjectTreePath,
187: prjName)) {
188: Utils.logMsg("+++ TreePath "
189: + deployedProjectTreePath.toString()
190: + " contains the project name [" + prjName
191: + "]");
192: return treePath;
193: }
194: }
195: Utils.logMsg("+++ Deployed project with name [" + prjName
196: + "] is not found");
197: return null;
198: }
199:
200: private static TreePath expandTreePath(JTree jTree,
201: String treeNodeLabel, int startTreeRow) {
202: TreePath treePath = jTree.getNextMatch(treeNodeLabel,
203: startTreeRow, Position.Bias.Forward);
204: Util.wait(800);
205: new QueueTool().waitEmpty();
206:
207: jTree.expandPath(treePath);
208: Util.wait(800);
209: new QueueTool().waitEmpty();
210:
211: Utils.logMsg("+++ Expanded treePath = " + treePath.toString());
212: return treePath;
213: }
214:
215: private static boolean isTreeNodeLabelEqualsPrjName(
216: TreePath treePath, String prjName) {
217: // after prjName shouldn't be a word character [a-zA-Z_0-9], spaces, -
218: boolean result = Pattern.matches(PATTERN_ANY_CHARS + prjName
219: + "[^\\w\\s\\-]?", treePath.toString());
220: //Utils.logMsg("+++ TreePath " + treePath.toString() +
221: // " contains the project name [" + prjName + "]: " + result);
222: return result;
223: }
224:
225: public static WebResponse getWebResponseAfterDeployment(
226: String webAppURL) throws Throwable {
227: //HttpUnitOptions.setExceptionsThrownOnScriptError(false);
228: HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
229: HttpUnitOptions.setScriptingEnabled(false);
230:
231: WebConversation conversation = new WebConversation();
232: WebResponse response = null;
233: for (int i = 0; i < 8; ++i) {
234: try {
235: response = conversation.getResponse(webAppURL);
236: Util.wait(2000);
237: } catch (HttpNotFoundException e) {
238: if (i == 8) {
239: throw new RuntimeException(e);
240: }
241: }
242: }
243: return response;
244: }
245:
246: public static boolean isWebResponseOK(WebResponse response) {
247: if ((response != null)
248: && (response.getResponseCode() == WEB_RESPONSE_CODE_OK)) {
249: return true;
250: }
251: return false;
252: }
253:
254: private static String getWebApplicationsTreeNodeName_SunAppServer() {
255: String serverName = TestPropertiesHandler
256: .getServerProperty("Application_Server_Name");
257: return (SERVICES_TREE_NODE_SERVERS + "|" + serverName + "|"
258: + SERVICES_TREE_NODE_APPLICATIONS + "|" + SERVICES_TREE_NODE_WEB_APPLICATIONS);
259: }
260:
261: private static String getDeployedApplicationTreeNodeName_SunAppServer(
262: String prjName) {
263: return (getWebApplicationsTreeNodeName_SunAppServer() + "|" + prjName);
264: }
265: }
|