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-2007 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:
042: package gui.actions;
043:
044: import javax.swing.tree.TreePath;
045:
046: import org.netbeans.jellytools.RuntimeTabOperator;
047: import org.netbeans.jellytools.nodes.Node;
048:
049: import org.netbeans.jemmy.TimeoutExpiredException;
050: import org.netbeans.jemmy.operators.ComponentOperator;
051: import org.netbeans.jemmy.operators.JPopupMenuOperator;
052: import org.netbeans.jemmy.EventTool;
053:
054: import org.netbeans.progress.module.Controller;
055: import org.netbeans.progress.spi.InternalHandle;
056: import org.netbeans.progress.spi.TaskModel;
057:
058: /**
059: * Measure application server Startup time via NetBeans TaskModel API.
060: *
061: * @author rashid@netbeans.org, mkhramov@netbeans.org, mmirilovic@netbeans.org
062: *
063: */
064: public class StartAppserver extends
065: org.netbeans.performance.test.utilities.PerformanceTestCase {
066:
067: private RuntimeTabOperator rto;
068: private Node asNode;
069:
070: /** Creates a new instance of StartAppserver
071: * @param testName
072: **/
073: public StartAppserver(String testName) {
074: super (testName);
075: expectedTime = 45000; //TODO: Adjust expectedTime value
076: WAIT_AFTER_OPEN = 4000;
077: }
078:
079: /** Creates a new instance of StartAppserver
080: * @param testName
081: * @param performanceDataName
082: **/
083: public StartAppserver(String testName, String performanceDataName) {
084: super (testName, performanceDataName);
085: expectedTime = 45000; //TODO: Adjust expectedTime value
086: WAIT_AFTER_OPEN = 4000;
087: }
088:
089: public void prepare() {
090: log(":: prepare");
091: rto = RuntimeTabOperator.invoke();
092: TreePath path = null;
093:
094: try {
095: path = rto.tree().findPath("Servers|GlassFish V2"); // NOI18N
096: } catch (TimeoutExpiredException exc) {
097: exc.printStackTrace(System.err);
098: throw new Error("Cannot find Application Server Node");
099: }
100:
101: asNode = new Node(rto.tree(), path);
102: asNode.select();
103: new EventTool().waitNoEvent(5000);
104: }
105:
106: public ComponentOperator open() {
107: log("::open");
108: String serverIDEName = asNode.getText();
109:
110: JPopupMenuOperator popup = asNode.callPopup();
111: if (popup == null) {
112: throw new Error(
113: "Cannot get context menu for Application server node ");
114: }
115:
116: boolean startEnabled = popup.showMenuItem("Start").isEnabled(); // NOI18N
117: if (startEnabled) {
118: popup.pushMenuNoBlock("Start"); // NOI18N
119: }
120:
121: waitForAppServerTask("Starting", serverIDEName);
122: return null;
123: }
124:
125: public void close() {
126: log("::close");
127: if (asNode != null) {
128: String serverIDEName = asNode.getText();
129:
130: JPopupMenuOperator popup = asNode.callPopup();
131: if (popup == null) {
132: throw new Error(
133: "Cannot get context menu for Application server node ");
134: }
135: boolean startEnabled = popup.showMenuItem("Stop")
136: .isEnabled(); // NOI18N
137: if (startEnabled) {
138: popup.pushMenuNoBlock("Stop"); // NOI18N
139: }
140: waitForAppServerTask("Stopping", serverIDEName);
141: }
142: }
143:
144: private void waitForAppServerTask(String taskName,
145: String serverIDEName) {
146: Controller controller = Controller.getDefault();
147: TaskModel model = controller.getModel();
148:
149: InternalHandle task = waitServerTaskHandle(model, taskName
150: + " " + serverIDEName);
151: long taskTimestamp = task.getTimeStampStarted();
152:
153: log("task started at : " + taskTimestamp);
154:
155: while (true) {
156: int state = task.getState();
157: if (state == task.STATE_FINISHED) {
158: return;
159: }
160: try {
161: Thread.sleep(50);
162: } catch (InterruptedException exc) {
163: exc.printStackTrace(System.err);
164: return;
165: }
166: }
167: }
168:
169: private InternalHandle waitServerTaskHandle(TaskModel model,
170: String serverIDEName) {
171: while (true) {
172: InternalHandle[] handles = model.getHandles();
173: InternalHandle serverTask = getServerTaskHandle(handles,
174: serverIDEName);
175: if (serverTask != null) {
176: log("Returning task handle");
177: return serverTask;
178: }
179:
180: try {
181: Thread.sleep(50);
182: } catch (InterruptedException exc) {
183: exc.printStackTrace(System.err);
184: }
185: }
186: }
187:
188: private InternalHandle getServerTaskHandle(
189: InternalHandle[] handles, String taskName) {
190: if (handles.length == 0) {
191: log("Empty tasks queue");
192: return null;
193: }
194:
195: for (InternalHandle internalHandle : handles) {
196: if (internalHandle.getDisplayName().equals(taskName)) {
197: log("Expected task found...");
198: return internalHandle;
199: }
200: }
201: return null;
202: }
203:
204: public static void main(java.lang.String[] args) {
205: repeat = 2;
206: junit.textui.TestRunner.run(new StartAppserver("measureTime"));
207: }
208:
209: }
|