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.designpatterns;
043:
044: //import com.embarcadero.uml.ui.controls.projecttree.IProjectTreeItem;
045: import java.awt.event.InputEvent;
046: import java.awt.event.KeyEvent;
047: import javax.swing.JComboBox;
048: import javax.swing.JTextField;
049: import javax.swing.tree.TreePath;
050: import org.netbeans.jellytools.MainWindowOperator;
051: import org.netbeans.jellytools.ProjectsTabOperator;
052: import org.netbeans.jellytools.TopComponentOperator;
053: import org.netbeans.jellytools.actions.Action;
054: import org.netbeans.jellytools.nodes.Node;
055: import org.netbeans.jellytools.nodes.ProjectRootNode;
056: import org.netbeans.jemmy.EventTool;
057: import org.netbeans.jemmy.JemmyProperties;
058: import org.netbeans.jemmy.operators.JButtonOperator;
059: import org.netbeans.jemmy.operators.JComboBoxOperator;
060: import org.netbeans.jemmy.operators.JDialogOperator;
061: import org.netbeans.jemmy.operators.JLabelOperator;
062: import org.netbeans.jemmy.operators.JPopupMenuOperator;
063: import org.netbeans.jemmy.operators.JTextFieldOperator;
064: import org.netbeans.jemmy.operators.JTreeOperator;
065:
066: import org.netbeans.junit.NbTestSuite; //import org.netbeans.test.umllib.UMLClassOperator;
067: import org.netbeans.test.umllib.CompartmentOperator;
068: import org.netbeans.test.umllib.CompartmentTypes;
069: import org.netbeans.test.umllib.DiagramElementOperator;
070: import org.netbeans.test.umllib.DiagramOperator;
071: import org.netbeans.test.umllib.DrawingAreaOperator;
072: import org.netbeans.test.umllib.ExpandedElementTypes;
073: import org.netbeans.test.umllib.ExpandedElementTypes;
074: import org.netbeans.test.umllib.LinkTypes;
075: import org.netbeans.test.umllib.UMLPaletteOperator;
076: import org.netbeans.test.umllib.exceptions.NotFoundException;
077: import org.netbeans.test.umllib.testcases.UMLTestCase;
078: import org.netbeans.test.umllib.util.*;
079:
080: /**
081: *
082: * @author psb
083: * @spec uml/DesignPatterns
084: */
085: public class PromoteDesignPattern extends UMLTestCase {
086:
087: //some system properties
088: private static String contextPropItemName = "Properties";
089: private static String umlPropertyWindowTitle = "Project Properties";
090: private static String mainTreeTabName = "Projects";
091: private static String sourcePackGroup = "Source Packages";
092: private static String defPackageName = "newpackage";
093: private static String defClassName = "NewClass";
094: //common test properties
095: private static String prName = "PromotePatternsProject";
096: private static String project = prName + "|Model";
097: private static boolean codeSync = false;
098: private static String workdir = System.getProperty("xtest.workdir");
099: private static String defaultNewElementName = org.netbeans.test.uml.designpatterns.utils.Utils.defaultNewElementName;
100: private static String defaultReturnType = org.netbeans.test.uml.designpatterns.utils.Utils.defaultReturnType;
101: private static String defaultAttributeType = org.netbeans.test.uml.designpatterns.utils.Utils.defaultAttributeType;
102: private static String defaultAttributeVisibility = org.netbeans.test.uml.designpatterns.utils.Utils.defaultAttributeVisibility;
103: private static String defaultOperationVisibility = org.netbeans.test.uml.designpatterns.utils.Utils.defaultOperationVisibility;
104: private static String defaultClassVisibility = org.netbeans.test.uml.designpatterns.utils.Utils.defaultClassVisibility;
105: ProjectsTabOperator pto = null;
106: JTreeOperator prTree = null;
107: private Node lastDiagramNode = null;
108: private String lastTestCase = null;
109: private final long MINWAIT = 500;
110: ProjectRootNode prRoot = null;
111: private static int pkgCounter = 0;
112: //
113: ExpandedElementTypes allRoles[] = { ExpandedElementTypes.ROLE,
114: ExpandedElementTypes.INTERFACE_ROLE,
115: ExpandedElementTypes.ACTOR_ROLE,
116: ExpandedElementTypes.CLASS_ROLE,
117: ExpandedElementTypes.USE_CASE_ROLE };
118: ExpandedElementTypes allApplied[] = { ExpandedElementTypes.CLASS,
119: ExpandedElementTypes.INTERFACE, ExpandedElementTypes.ACTOR,
120: ExpandedElementTypes.CLASS, ExpandedElementTypes.USE_CASE };
121: LinkTypes allLinks[] = { LinkTypes.DEPENDENCY };
122: private static boolean makeScreen = false;
123:
124: /** Need to be defined because of JUnit */
125: public PromoteDesignPattern(String name) {
126: super (name);
127: }
128:
129: public static NbTestSuite suite() {
130: NbTestSuite suite = new NbTestSuite(
131: org.netbeans.test.uml.designpatterns.PromoteDesignPattern.class);
132:
133: return suite;
134: }
135:
136: private DiagramOperator createOrOpenDiagram(String project,
137: String workPkg, String diagram) {
138: org.netbeans.test.umllib.Utils.RetAll rt = org.netbeans.test.umllib.Utils
139: .createOrOpenDiagram(
140: project,
141: workPkg,
142: diagram,
143: org.netbeans.test.umllib.NewDiagramWizardOperator.CLASS_DIAGRAM);
144: pto = rt.pto;
145: prTree = new JTreeOperator(pto);
146: lastDiagramNode = rt.lastDiagramNode;
147: return rt.dOp;
148: }
149:
150: public void setUp() {
151: makeScreen = true;
152: System.out.println("######## " + getName() + " #######");
153: long tmp = JemmyProperties
154: .getCurrentTimeout("DialogWaiter.WaitDialogTimeout");
155: JemmyProperties.setCurrentTimeout(
156: "DialogWaiter.WaitDialogTimeout", 1000);
157: try {
158: JDialogOperator svD = new JDialogOperator("Exception");
159: new JButtonOperator(svD, "OK").push();
160: } catch (Exception ex) {
161: }
162: closeAllModal();
163: JemmyProperties.setCurrentTimeout(
164: "DialogWaiter.WaitDialogTimeout", tmp);
165: pto = ProjectsTabOperator.invoke();
166: prTree = new JTreeOperator(pto);
167: if (!codeSync) {
168: //org.netbeans.test.uml.designpatterns.utils.Utils.setDefaultPreferences();
169: org.netbeans.test.uml.designpatterns.utils.Utils
170: .prepareProjects(workdir, prName);
171: addProject();
172: //close unnecessary windows
173: TopComponentOperator doc = null;
174: try {
175: doc = new TopComponentOperator("Documentation");
176: doc.close();
177: } catch (Exception ex) {
178: }
179: codeSync = true;
180: }
181: prRoot = new ProjectRootNode(prTree, project);
182: //close design center
183: long oldwait = JemmyProperties
184: .getCurrentTimeout("ComponentOperator.WaitComponentTimeout");
185: JemmyProperties.setCurrentTimeout(
186: "ComponentOperator.WaitComponentTimeout", 3000);
187: try {
188: new TopComponentOperator(
189: LabelsAndTitles.DESIGN_CENTER_TITLE).closeWindow();
190: } catch (Exception ex) {
191:
192: }
193: JemmyProperties.setCurrentTimeout(
194: "ComponentOperator.WaitComponentTimeout", oldwait);
195:
196: }
197:
198: public void commonPreparePattern(String diagramType) {
199: pkgCounter++;
200: String curPkg = "pkg" + pkgCounter;
201: DiagramOperator dgr = org.netbeans.test.umllib.Utils
202: .createOrOpenDiagram(prName, curPkg,
203: "dgr" + pkgCounter, diagramType).dOp;
204: lastDiagramNode = new Node(prRoot, curPkg + "|" + "dgr"
205: + pkgCounter);
206: DrawingAreaOperator drA = dgr.getDrawingArea();
207: int numChild = new Node(prTree, lastDiagramNode.getParentPath())
208: .getChildren().length;
209: DiagramElementOperator dpE = null;
210: try {
211: dpE = dgr.putElementOnDiagram("DP" + pkgCounter,
212: ExpandedElementTypes.DESIGN_PATTERN);
213: } catch (NotFoundException ex) {
214: fail("Can't find design pattern: " + ex);
215: }
216: //
217: Node dpNode = new Node(prTree, lastDiagramNode.getParentPath()
218: + "|" + "DP" + pkgCounter);
219: //
220: UMLPaletteOperator pl = new UMLPaletteOperator();
221: //
222: java.awt.Point tmp = null;
223: for (int i = 1; i < (allRoles.length - 1); i++) {
224: ExpandedElementTypes it = allRoles[i];
225: try {
226: pl.selectTool(LibProperties.getProperties()
227: .getToolName(it));
228: } catch (NotFoundException ex) {
229: fail("BLOCKING: Can't find '" + it + "' in paletter");
230: }
231: drA.clickMouse(dpE.getCenterPoint().x,
232: dpE.getCenterPoint().y, 1);
233: tmp = drA.getFreePoint(80);
234: drA.clickMouse(tmp.x, tmp.y, 1);
235: //
236: try {
237: new DiagramElementOperator(dgr, defaultNewElementName,
238: it, 0);
239: } catch (NotFoundException ex) {
240: fail("Role " + it + " wasn't added to diagram");
241: }
242: }
243:
244: tmp = drA.getFreePoint(10);
245: drA.clickMouse(tmp.x, tmp.y, 1, InputEvent.BUTTON3_MASK);
246: drA.pushKey(KeyEvent.VK_ESCAPE);
247: new EventTool().waitNoEvent(500);
248: assertTrue("There should be " + (allRoles.length - 2)
249: + " binding links from design pattern to roles, now: "
250: + dpE.getLinks().size(), (allRoles.length - 2) == dpE
251: .getLinks().size());
252: assertTrue(
253: "There should be "
254: + (allRoles.length - 2)
255: + " roles within design pattern node in project tree, now: "
256: + dpNode.getChildren().length,
257: (allRoles.length - 2) == dpNode.getChildren().length);
258: //name roles
259: for (int i = 1; i < (allRoles.length - 1); i++) {
260: ExpandedElementTypes it = allRoles[i];
261: DiagramElementOperator curRole = null;
262: //
263: try {
264: curRole = new DiagramElementOperator(dgr,
265: defaultNewElementName, it, 0);
266: } catch (NotFoundException ex) {
267: fail("Can't find " + it + " on diagram");
268: }
269: curRole.select(true);
270: curRole.waitSelection(true);
271: for (int j = 0; j < ("name" + pkgCounter + "_" + i)
272: .length(); j++) {
273: drA.typeKey(("name" + pkgCounter + "_" + i).charAt(j));
274: }
275: tmp = drA.getFreePoint();
276: drA.clickMouse(tmp.x, tmp.y, 1);
277: try {
278: curRole = new DiagramElementOperator(dgr, "name"
279: + pkgCounter + "_" + i, it, 0);
280: } catch (NotFoundException ex) {
281: fail("Can't find named " + it + " on diagram");
282: }
283: new Node(dpNode, "name" + pkgCounter + "_" + i);
284: }
285: CompartmentOperator dpPC = null;
286: try {
287: dpPC = new CompartmentOperator(dpE,
288: CompartmentTypes.TEMPLATE_PARAMETERS_COMPARTMENT);
289: } catch (Exception ex) {
290: fail("can't find parameters comparment in design pattern");
291: }
292: String allRolesInDPStr = dpPC.getName();
293: for (int i = 1; i < (allRoles.length - 1); i++) {
294: assertTrue(
295: "there is no 'name"
296: + pkgCounter
297: + "_"
298: + i
299: + "' role in parameters comnpartment for design pattern",
300: allRolesInDPStr.indexOf("name" + pkgCounter + "_"
301: + i) > -1);
302: assertTrue("there is no 'name" + pkgCounter + "_" + i
303: + "' node within design pattern node", dpNode
304: .isChildPresent("name" + pkgCounter + "_" + i));
305: }
306: assertTrue("There should be " + (allRoles.length - 2)
307: + " names in parameters compartment, now:"
308: + allRolesInDPStr.split(",").length, allRolesInDPStr
309: .split(",").length == (allRoles.length - 2));
310: }
311:
312: /**
313: * @caseblock Promote design pattern to Design Center
314: * @usecase Promote a pattern, leaving in local project
315: */
316: public void testPromoteWORemove() {
317: lastTestCase = getCurrentTestMethodName();
318: commonPreparePattern("Component Diagram");
319: //
320: String curPkg = "pkg" + pkgCounter;
321: Node dpNode = new Node(prTree, lastDiagramNode.getParentPath()
322: + "|" + "DP");
323: dpNode.performPopupActionNoBlock("Promote Design Pattern...");
324: JDialogOperator prD = new JDialogOperator(
325: "Promote Pattern to Design Center");
326: JLabelOperator prL = new JLabelOperator(prD, "Project:");
327: JComboBoxOperator prC = new JComboBoxOperator((JComboBox) (prL
328: .getLabelFor()));
329: prC.selectItem("projectForPromotions");
330: //JLabelOperator nspl=new JLabelOperator(prD,"Namespace:");
331: //nspl.waitComponentShowing(true);
332: //JComboBoxOperator nsCb=new JComboBoxOperator((JComboBox)(nspl.getLabelFor()));
333: //nsCb.selectItem();
334: //nsCb.waitItemSelected(curPkg);
335: new JButtonOperator(prD, "OK").pushNoBlock();
336: prD.waitClosed();
337: //check results
338: //remains in model
339: DiagramOperator dgr = new DiagramOperator("dgr" + pkgCounter);
340: DrawingAreaOperator drA = dgr.getDrawingArea();
341: DiagramElementOperator dpE = null;
342: try {
343: dpE = new DiagramElementOperator(dgr, "DP" + pkgCounter);
344: } catch (Exception ex) {
345: fail("Can't find design pattern on diagram after promotion.");
346: }
347: assertTrue("There should be " + (allRoles.length - 2)
348: + " binding links from design pattern to roles, now: "
349: + dpE.getLinks().size(), (allRoles.length - 2) == dpE
350: .getLinks().size());
351: assertTrue(
352: "There should be "
353: + (allRoles.length - 2)
354: + " roles within design pattern node in project tree, now: "
355: + dpNode.getChildren().length,
356: (allRoles.length - 2) == dpNode.getChildren().length);
357: //roles
358: for (int i = 1; i < (allRoles.length - 1); i++) {
359: ExpandedElementTypes it = allRoles[i];
360: DiagramElementOperator curRole = null;
361: //
362: try {
363: curRole = new DiagramElementOperator(dgr, "name"
364: + pkgCounter + "_" + i, it, 0);
365: } catch (NotFoundException ex) {
366: fail("Can't find " + it + " on diagram");
367: }
368: new Node(dpNode, "name" + pkgCounter + "_" + i);
369: }
370: CompartmentOperator dpPC = null;
371: try {
372: dpPC = new CompartmentOperator(dpE,
373: CompartmentTypes.TEMPLATE_PARAMETERS_COMPARTMENT);
374: } catch (Exception ex) {
375: fail("can't find parameters comparment in design pattern");
376: }
377: String allRolesInDPStr = dpPC.getName();
378: for (int i = 1; i < (allRoles.length - 1); i++) {
379: assertTrue(
380: "there is no 'name"
381: + pkgCounter
382: + "_"
383: + i
384: + "' role in parameters comnpartment for design pattern",
385: allRolesInDPStr.indexOf("name" + pkgCounter + "_"
386: + i) > -1);
387: assertTrue("there is no 'name" + pkgCounter + "_" + i
388: + "' node within design pattern node", dpNode
389: .isChildPresent("name" + pkgCounter + "_" + i));
390: }
391: assertTrue("There should be " + (allRoles.length - 2)
392: + " names in parameters compartment, now:"
393: + allRolesInDPStr.split(",").length, allRolesInDPStr
394: .split(",").length == (allRoles.length - 2));
395: //appears in design center
396: Action open = new Action("Window|Other|UML Design Center", null);
397: open.performMenu();
398: TopComponentOperator dc = null;
399: try {
400: dc = new TopComponentOperator(
401: LabelsAndTitles.DESIGN_CENTER_TITLE);
402: } catch (Exception ex) {
403: fail("Design Center wasn't opend via menu");
404: }
405: JTreeOperator tr = new JTreeOperator(dc);
406: TreePath tmpP = null;
407: tmpP = tr.findPath("DesignPatternCatalog|"
408: + "projectForPromotions");
409: tr.expandPath(tmpP);
410: tr.waitExpanded(tmpP);
411: tmpP = tr.findPath("DesignPatternCatalog|"
412: + "projectForPromotions|" + "DP" + pkgCounter);
413: tr.expandPath(tmpP);
414: tr.waitExpanded(tmpP);
415: //roles
416: for (int i = 1; i < (allRoles.length - 1); i++) {
417: tr.findPath("DesignPatternCatalog|"
418: + "projectForPromotions|" + "DP" + pkgCounter + "|"
419: + "name" + pkgCounter + "_" + i);
420: }
421:
422: makeScreen = false;
423: }
424:
425: //
426: public void tearDown() {
427: if (makeScreen)
428: Utils.makeScreenShot(lastTestCase);
429: //popup protection
430: MainWindowOperator.getDefault().pushKey(KeyEvent.VK_ESCAPE);
431: //
432: closeAllModal();
433: org.netbeans.test.umllib.util.Utils.saveAll();
434: if (lastDiagramNode != null)
435: new Node(prTree, lastDiagramNode.getParentPath())
436: .collapse();
437: //
438: long oldwait = JemmyProperties
439: .getCurrentTimeout("ComponentOperator.WaitComponentTimeout");
440: JemmyProperties.setCurrentTimeout(
441: "ComponentOperator.WaitComponentTimeout", 5000);
442: try {
443: DiagramOperator d = new DiagramOperator("dgr");
444: DrawingAreaOperator drAr = d.getDrawingArea();
445: drAr.pushKey(KeyEvent.VK_ESCAPE);
446: java.awt.Point a = drAr.getFreePoint();
447: drAr.clickMouse(a.x, a.y, 1, InputEvent.BUTTON3_MASK);
448: drAr.pushKey(KeyEvent.VK_ESCAPE);
449: new Thread() {
450: public void run() {
451: try {
452: Thread.sleep(2000);
453: } catch (Exception ex) {
454: }
455: new JButtonOperator(new JDialogOperator(
456: "Save Diagram"), "Discard").push();
457: }
458: }.start();
459: d.closeAllDocuments();
460: } catch (Exception ex) {
461:
462: }
463: JemmyProperties.setCurrentTimeout(
464: "ComponentOperator.WaitComponentTimeout", oldwait);
465: //save
466: org.netbeans.test.umllib.util.Utils.tearDown();
467: }
468:
469: public boolean compareWithoutExtraSpaceChars(String s1, String s2) {
470: return org.netbeans.test.uml.designpatterns.utils.Utils
471: .compareWithoutExtraSpaceChars(s1, s2);
472: }
473:
474: public void addProject() {
475: Action open = new Action("Window|Other|UML Design Center", null);
476: open.performMenu();
477: TopComponentOperator dc = null;
478: try {
479: dc = new TopComponentOperator(
480: LabelsAndTitles.DESIGN_CENTER_TITLE);
481: } catch (Exception ex) {
482: fail("Design Center wasn't opend via menu");
483: }
484: //
485: JTreeOperator tr = new JTreeOperator(dc);
486: //
487: assertTrue(
488: "There is no Design Center root node or invisible, current root:"
489: + tr.getRoot(), "Design Center".equals(tr
490: .getRoot().toString())
491: && tr.isRootVisible());
492: tr.findPath("DesignPatternCatalog");
493: new JPopupMenuOperator(tr.callPopupOnPath(tr
494: .findPath("DesignPatternCatalog")))
495: .pushMenuNoBlock("New|Project...");
496: JDialogOperator nw = new JDialogOperator(
497: org.netbeans.test.uml.designpatterns.utils.Utils.createDesignCenterProjectTitle);
498: JLabelOperator pn = new JLabelOperator(
499: nw,
500: org.netbeans.test.uml.designpatterns.utils.Utils.createDesignCenterProjectNameLabel);
501: JLabelOperator lo = new JLabelOperator(
502: nw,
503: org.netbeans.test.uml.designpatterns.utils.Utils.createDesignCenterProjectLocationLabel);
504: JTextFieldOperator pnT = new JTextFieldOperator(
505: (JTextField) (pn.getLabelFor()));
506: JTextFieldOperator loT = new JTextFieldOperator(
507: (JTextField) (lo.getLabelFor()));
508: pnT.typeText("projectForPromotions");
509: loT.clearText();
510: loT.typeText(workdir);
511: new JButtonOperator(nw, "OK").push();
512: nw.waitClosed();
513: new EventTool().waitNoEvent(500);
514: //CAN'T REPRODUCE IN TESTS
515: // nw=new JDialogOperator("Create New Diagram");
516: // new JTextFieldOperator(nw).typeText("diagram");
517: // new JButtonOperator(nw,"OK").push();
518: tr.findPath("DesignPatternCatalog|" + "projectForPromotions");
519: // tr.findPath("DesignPatternCatalog|"+"projectForDelete"+"|"+"diagram");
520: }
521:
522: }
|