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 org.netbeans.test.uml.ejb.utils;
043:
044: import java.awt.Component;
045: import java.util.ArrayList;
046: import java.util.Date;
047: import javax.swing.JComboBox;
048: import javax.swing.JDialog;
049: import javax.swing.JTextArea;
050: import org.netbeans.jellytools.MainWindowOperator;
051: import org.netbeans.jellytools.ProjectsTabOperator;
052: import org.netbeans.jellytools.nodes.Node;
053: import org.netbeans.jellytools.nodes.ProjectRootNode;
054: import org.netbeans.jemmy.ComponentChooser;
055: import org.netbeans.jemmy.EventTool;
056: import org.netbeans.jemmy.Timeout;
057: import org.netbeans.jemmy.TimeoutExpiredException;
058: import org.netbeans.jemmy.drivers.DriverManager;
059: import org.netbeans.jemmy.drivers.input.KeyEventDriver;
060: import org.netbeans.jemmy.drivers.input.KeyRobotDriver;
061: import org.netbeans.jemmy.operators.JButtonOperator;
062: import org.netbeans.jemmy.operators.JCheckBoxOperator;
063: import org.netbeans.jemmy.operators.JComboBoxOperator;
064: import org.netbeans.jemmy.operators.JDialogOperator;
065: import org.netbeans.jemmy.operators.JLabelOperator;
066: import org.netbeans.jemmy.operators.JTextAreaOperator;
067: import org.netbeans.jemmy.operators.JTextComponentOperator;
068: import org.netbeans.test.umllib.DiagramOperator;
069: import org.netbeans.test.umllib.exceptions.KnownBugException;
070: import org.netbeans.test.umllib.exceptions.UMLCommonException;
071:
072: public class EJBCreator {
073:
074: public final static String EJB_1 = "EJB1.1";
075: public final static String EJB_2 = "EJB2.0";
076:
077: public final static String BMP = "BeanManaged";
078: public final static String CMP = "ContainerManaged";
079: public final static String MDB = "MessageDriven";
080: public final static String SESSION_STATELESS = "StatelessSession";
081: public final static String SESSION_STATEFUL = "StatefulSession";
082:
083: public final String APPLY_PATTERN_MENU = "Apply Design Pattern...";
084: public final String PATTERN_WIZARD_FIRST_STEP_TTL = "Design Pattern Apply Wizard";
085: public final String PATTERN_WIZARD_TTL = "Design Pattern Wizard";
086:
087: private final String NEXT_BTN = "Next";
088: private final String FINISH_BTN = "Finish";
089:
090: private ArrayList<Pair> pairs = null;
091: private EventTool eventTool = new EventTool();
092: private String projectName = null;
093:
094: private static boolean reported_85870 = false;
095:
096: /** Creates a new instance of BasicEJBPatternVerifier */
097: public EJBCreator(String project) {
098: this .projectName = project;
099: }
100:
101: public DiagramOperator create(String invocationPath,
102: String ejbType, String ejbName, String namespace,
103: boolean createDiagram, String diagramName) {
104: eventTool.waitNoEvent(1000);
105: openDesignPatternWizard(invocationPath);
106:
107: eventTool.waitNoEvent(1000);
108: handleStartPage();
109:
110: eventTool.waitNoEvent(1000);
111: handleSelectPattern(ejbType, ejbName);
112:
113: eventTool.waitNoEvent(1000);
114: handleSelectTargetScope(namespace);
115:
116: eventTool.waitNoEvent(1000);
117: handleChooseParticipants(pairs);
118:
119: eventTool.waitNoEvent(1000);
120: handleSetOptions(createDiagram, diagramName);
121:
122: eventTool.waitNoEvent(1000);
123: finishWizard();
124: //
125: eventTool.waitNoEvent(3000);
126: //
127: DiagramOperator dia = null;
128: try {
129: dia = new DiagramOperator(diagramName);
130: } catch (TimeoutExpiredException ex) {
131: //1st dialog
132: JDialog dlg = JDialogOperator.findJDialog(
133: "Invalid Property", true, true);
134: if (dlg != null) {
135: JTextArea lbl = JTextAreaOperator
136: .findJTextArea(
137: dlg,
138: "The operations of an interface must be abstract. The Abstract property for this operation will be set back to True.",
139: false, false);
140: if (lbl != null) {
141: if (!reported_85870) {
142: reported_85870 = true;
143: new Thread(new TerminationDialogHandler())
144: .start();
145: throw new KnownBugException(85870,
146: "Unexpected \"Invalid Property\" dialogs on pattern application");
147: } else
148: throw new KnownBugException(
149: 85870,
150: "Too long application and a lot of Unexpected \"Invalid Property\" dialogs on pattern application");
151: } else {
152: throw new UMLCommonException(
153: "Unknown Invalid property dialog.");
154: }
155: } else {
156: throw ex;
157: }
158: }
159: return dia;
160: }
161:
162: protected void openDesignPatternWizard(String pathToInvokationPoint) {
163: ProjectsTabOperator pto = ProjectsTabOperator.invoke();
164: ProjectRootNode root = new ProjectRootNode(pto.tree(),
165: projectName);
166:
167: Node node = new Node(root, pathToInvokationPoint);
168: node.performPopupActionNoBlock(APPLY_PATTERN_MENU);
169:
170: new JDialogOperator(PATTERN_WIZARD_FIRST_STEP_TTL);
171: }
172:
173: protected void handleStartPage() {
174: JDialogOperator dlg = new JDialogOperator(
175: PATTERN_WIZARD_FIRST_STEP_TTL);
176: new JButtonOperator(dlg, NEXT_BTN).push();
177: }
178:
179: protected void handleSelectPattern(String ejbType, String ejbName) {
180: JDialogOperator dlg = new JDialogOperator(PATTERN_WIZARD_TTL);
181: JComboBoxOperator cbProject = new JComboBoxOperator(dlg, 0);
182: cbProject.selectItem(ejbType);
183: eventTool.waitNoEvent(1000);
184: JComboBoxOperator cbEJB = new JComboBoxOperator(dlg, 1);
185: cbEJB.selectItem(ejbName);
186: eventTool.waitNoEvent(1000);
187:
188: new JButtonOperator(dlg, NEXT_BTN).push();
189: }
190:
191: protected void handleSelectTargetScope(String namespace) {
192: JDialogOperator dlg = new JDialogOperator(PATTERN_WIZARD_TTL);
193: if (namespace != null) {
194: //JComboBoxOperator cbNamespace = new JComboBoxOperator(dlg, 1);
195: JLabelOperator nspLbl = new JLabelOperator(dlg,
196: "Namespace:");
197: JComboBoxOperator cbNamespace = new JComboBoxOperator(
198: (JComboBox) (nspLbl.getLabelFor()));
199: cbNamespace.selectItem(namespace);
200: cbNamespace.waitItemSelected(namespace);
201: eventTool.waitNoEvent(1000);
202: }
203: new JButtonOperator(dlg, NEXT_BTN).push();
204: }
205:
206: protected void handleChooseParticipants(ArrayList<Pair> pairs) {
207: if (pairs != null) {
208: //to be implemented in the descendants or later
209: }
210: JDialogOperator dlg = new JDialogOperator(PATTERN_WIZARD_TTL);
211: new JButtonOperator(dlg, NEXT_BTN).push();
212: }
213:
214: protected void handleSetOptions(boolean createDiagram,
215: String diagramName) {
216: JDialogOperator dlg = new JDialogOperator(PATTERN_WIZARD_TTL);
217:
218: new JCheckBoxOperator(dlg).clickMouse();
219: new JCheckBoxOperator(dlg).waitSelected(true);
220: eventTool.waitNoEvent(1000);
221: DriverManager.setKeyDriver(new KeyEventDriver());
222: new JTextComponentOperator(dlg, 1).clearText();
223: new JTextComponentOperator(dlg, 1).waitText("");
224: new JTextComponentOperator(dlg, 1).typeText(diagramName);
225: eventTool.waitNoEvent(1000);
226: DriverManager.setKeyDriver(new KeyRobotDriver(new Timeout(
227: "autoDelay", 50)));
228: new JButtonOperator(dlg, NEXT_BTN).push();
229: JComboBox g;
230: }
231:
232: protected void finishWizard() {
233: JDialogOperator dlg = new JDialogOperator(
234: PATTERN_WIZARD_FIRST_STEP_TTL);
235: new JButtonOperator(dlg, FINISH_BTN).push();
236: dlg.waitClosed();
237: }
238:
239: //***************************************************************
240: //WORKAROUND FOR 85870
241: //***************************************************************
242: /**
243: * class works until MainWindow is showing and wait for different times of dialogs
244: */
245: static private class TerminationDialogHandler implements Runnable {
246: private long startTime;
247: private long timeout;
248: private long prevTime;
249: //
250: private int lastBtnIndex = 0;
251: protected JDialogOperator tDlg = null;
252: //
253: private String dialogTitle;
254: private String buttonTitle[];
255: //
256: final private long EXEC_TIMEOUT = 60 * 60 * 6 * 1000;//6 hours to exit thread
257: final private long DEFAULT_TIMEOUT = 60 * 1000;//wait 60 seconds and close unused dialog
258: //
259: final static public String DIALOG_TITLE_85870 = "Invalid Property";
260: final static public String BUTTON_TITLE_85870 = "OK";
261: final static public String TEXT_85870 = "The operations of an interface must be abstract. The Abstract property for this operation will be set back to True.";
262:
263: TerminationDialogHandler() {
264: startTime = new Date().getTime();
265: }
266:
267: public void run() {
268: MainWindowOperator mw = null;
269: while (true) {
270: //try dialog every 2 second
271: try {
272: Thread.sleep(2000);
273: } catch (Exception ex) {
274: }
275: ;
276: mw = MainWindowOperator.getDefault();
277: if (!(mw != null && mw.isShowing() && mw.isVisible())) {
278: break;
279: }
280: if ((new Date().getTime() - startTime) > EXEC_TIMEOUT) {
281: break;
282: }
283: //check if dialog was found before and still exists
284: tDlg = null;
285: //try to find dialog
286: try {
287: tDlg = new JDialogOperator(
288: new ChooseDialogByTitleAndButtonAndText(
289: DIALOG_TITLE_85870,
290: BUTTON_TITLE_85870, TEXT_85870));
291: new JButtonOperator(tDlg, BUTTON_TITLE_85870)
292: .push();
293: tDlg.waitClosed();
294: } catch (Exception ex) {
295: //no dialog
296: }
297: }
298: }
299: }
300:
301: /**
302: * choose JDialog with appropriate exaqct title and appropriate exact button within
303: */
304: static private class ChooseDialogByTitleAndButtonAndText implements
305: ComponentChooser {
306: private String title;
307: private String btn;
308: private String txt;
309:
310: ChooseDialogByTitleAndButtonAndText(String ttl, String bt,
311: String text) {
312: title = ttl;
313: btn = bt;
314: txt = text;
315: }
316:
317: public String getDescription() {
318: return "find dialog with " + title + " title";
319: }
320:
321: public boolean checkComponent(Component component) {
322: if (component instanceof java.awt.Dialog) {
323: if (((java.awt.Dialog) component).getTitle().equals(
324: title)) {
325: return JButtonOperator.findJButton(
326: (java.awt.Dialog) component, btn, true,
327: true) != null
328: && JTextAreaOperator.findJTextArea(
329: (java.awt.Dialog) component, txt,
330: true, true) != null;
331: }
332: }
333: return false;
334: }
335: }
336:
337: }
|