KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > xml > schema > core > AcceptanceTestCase


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.xml.schema.core;
21
22 import java.awt.Point JavaDoc;
23 import java.util.zip.CRC32 JavaDoc;
24 import javax.swing.tree.TreePath JavaDoc;
25 import org.netbeans.jellytools.EditorOperator;
26 import org.netbeans.jellytools.JellyTestCase;
27 import org.netbeans.jellytools.NewFileNameLocationStepOperator;
28 import org.netbeans.jellytools.NewFileWizardOperator;
29 import org.netbeans.jellytools.OutputOperator;
30 import org.netbeans.jellytools.ProjectsTabOperator;
31 import org.netbeans.jellytools.TopComponentOperator;
32 import org.netbeans.jellytools.WizardOperator;
33 import org.netbeans.jellytools.actions.SaveAllAction;
34 import org.netbeans.jellytools.nodes.Node;
35 import org.netbeans.jellytools.nodes.ProjectRootNode;
36 import org.netbeans.jemmy.operators.JButtonOperator;
37 import org.netbeans.jemmy.operators.JCheckBoxOperator;
38 import org.netbeans.jemmy.operators.JDialogOperator;
39 import org.netbeans.jemmy.operators.JListOperator;
40 import org.netbeans.jemmy.operators.JPopupMenuOperator;
41 import org.netbeans.jemmy.operators.JRadioButtonOperator;
42 import org.netbeans.jemmy.operators.JTextFieldOperator;
43 import org.netbeans.jemmy.operators.JTreeOperator;
44 import org.netbeans.test.xml.schema.core.lib.SchemaMultiView;
45 import org.netbeans.test.xml.schema.core.lib.util.Helpers;
46
47 /**
48  *
49  * @author ca@netbeans.org
50  */

51
52 public class AcceptanceTestCase extends JellyTestCase {
53     
54     static final String JavaDoc [] m_aTestMethods = {
55         "createNewSchema",
56                 "createSchemaComponents",
57                 "customizeSchema",
58                 "checkSourceCRC",
59                 "refactorComplexType",
60                 "applyDesignPattern"
61     };
62     
63     static final String JavaDoc TEST_SCHEMA_NAME = "testSchema";
64     static final String JavaDoc SCHEMA_EXTENSION = ".xsd";
65     
66     public AcceptanceTestCase(String JavaDoc arg0) {
67         super(arg0);
68     }
69     
70     public static junit.framework.TestSuite suite() {
71         junit.framework.TestSuite testSuite = new junit.framework.TestSuite("Acceptance suite");
72         
73         for (String JavaDoc strMethodName : m_aTestMethods) {
74             testSuite.addTest(new AcceptanceTestCase(strMethodName));
75         }
76         
77         return testSuite;
78     }
79     
80     public void createNewSchema() {
81         startTest();
82         
83         NewFileWizardOperator opNewFileWizard = NewFileWizardOperator.invoke();
84         opNewFileWizard.selectCategory("XML");
85         opNewFileWizard.selectFileType("XML Schema");
86         opNewFileWizard.next();
87         
88         NewFileNameLocationStepOperator opNewFileNameLocationStep = new NewFileNameLocationStepOperator();
89         opNewFileNameLocationStep.setObjectName(TEST_SCHEMA_NAME);
90         opNewFileWizard.finish();
91         
92         TopComponentOperator opTopComponent = new TopComponentOperator(TEST_SCHEMA_NAME + SCHEMA_EXTENSION);
93         
94         endTest();
95     }
96     
97     public void createSchemaComponents() {
98         startTest();
99         
100         String JavaDoc[][] aComponentsMenu = {
101             {"Complex Type...", "CT"},
102             {"Simple Type...", "ST"},
103             {"Element...", "E"},
104             {"Attribute...", "A"}
105         };
106         
107         SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
108         JListOperator opList = opMultiView.getColumnListOperator(0);
109         
110         for (int i = 0; i <aComponentsMenu.length; i++) {
111             Point JavaDoc p = opList.getClickPoint(0);
112             opList.clickForPopup(p.x, p.y);
113             new JPopupMenuOperator().pushMenuNoBlock("Add|" + aComponentsMenu[i][0]);
114             Helpers.waitNoEvent();
115             
116             JDialogOperator opCustomizer = new JDialogOperator();
117             new JTextFieldOperator(opCustomizer, 0).setText(aComponentsMenu[i][1]);
118             new JButtonOperator(opCustomizer, "OK").pushNoBlock();
119             Helpers.waitNoEvent();
120         }
121         
122         endTest();
123     }
124     
125     public void customizeSchema() {
126         startTest();
127
128         SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
129         
130         // Customize Element
131
JListOperator opList0 = opMultiView.getColumnListOperator(0);
132         opList0.selectItem("Elements");
133         
134         JListOperator opList1 = opMultiView.getColumnListOperator(1);
135         callPopupOnListItem(opList1, "E", "Customize");
136         
137         JDialogOperator opCustomizer = new JDialogOperator();
138         new JRadioButtonOperator(opCustomizer, "Use Existing Type").pushNoBlock();
139         Helpers.waitNoEvent();
140         
141         JTreeOperator opTree = new JTreeOperator(opCustomizer);
142         TreePath JavaDoc treePath = opTree.findPath("Complex Types|CT");
143         opTree.selectPath(treePath);
144         
145         new JButtonOperator(opCustomizer, "OK").pushNoBlock();
146         Helpers.waitNoEvent();
147         
148         // Customize Attribute
149
opList0.selectItem("Attributes");
150         
151         opList1 = opMultiView.getColumnListOperator(1);
152         callPopupOnListItem(opList1, "A", "Customize");
153         
154         opCustomizer = new JDialogOperator();
155         new JRadioButtonOperator(opCustomizer, "Use Existing Type").pushNoBlock();
156         Helpers.waitNoEvent();
157         
158         opTree = new JTreeOperator(opCustomizer);
159         treePath = opTree.findPath("Simple Types|ST");
160         opTree.selectPath(treePath);
161         
162         new JButtonOperator(opCustomizer, "OK").pushNoBlock();
163         Helpers.waitNoEvent();
164         
165         // Customize Complex Type
166
opList0.selectItem("Complex Types");
167         
168         opList1 = opMultiView.getColumnListOperator(1);
169         callPopupOnListItem(opList1, "CT", "Add|Attribute Reference");
170         
171         opCustomizer = new JDialogOperator();
172         
173         opTree = new JTreeOperator(opCustomizer);
174         treePath = opTree.findPath("Attributes|A");
175         opTree.selectPath(treePath);
176         
177         new JButtonOperator(opCustomizer, "OK").pushNoBlock();
178         Helpers.waitNoEvent();
179         
180         endTest();
181     }
182     
183     public void checkSourceCRC() {
184         startTest();
185         
186         final long goldenCRC32 = 2295334600L;
187         
188         SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
189         opMultiView.switchToSource();
190         
191         EditorOperator opEditor = new EditorOperator(TEST_SCHEMA_NAME);
192         String JavaDoc strText = opEditor.getText();
193         
194         opMultiView.switchToSchema();
195         
196         strText = strText.replaceAll("[ [\t\f\r]]", "");
197         Helpers.writeJemmyLog("{" + strText + "}");
198         
199         CRC32 JavaDoc crc32 = new CRC32 JavaDoc();
200         crc32.update(strText.getBytes());
201         long checkSum = crc32.getValue();
202         Helpers.writeJemmyLog("CRC32=" + checkSum);
203         if ( checkSum != goldenCRC32) {
204             fail("Schema source check sum doesn't match golden value");
205         }
206         
207         endTest();
208     }
209     
210     public void refactorComplexType() {
211         startTest();
212         
213         SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
214         
215         JListOperator opList0 = opMultiView.getColumnListOperator(0);
216         opList0.selectItem("Complex Types");
217         
218         JListOperator opList1 = opMultiView.getColumnListOperator(1);
219         callPopupOnListItem(opList1, "CT", "Refactor|Rename...");
220         
221         JDialogOperator opDialog = new JDialogOperator();
222         new JCheckBoxOperator(opDialog, "Preview All Changes").changeSelection(false);
223         Helpers.waitNoEvent();
224         new JTextFieldOperator(opDialog).setText("CT1");
225         new JButtonOperator(opDialog, "Next").pushNoBlock();
226         opDialog.waitClosed();
227         
228         opList0 = opMultiView.getColumnListOperator(0);
229         opList0.selectItem("Elements");
230         Helpers.waitNoEvent();
231         
232         opList1 = opMultiView.getColumnListOperator(1);
233         opList1.selectItem("E");
234         Helpers.waitNoEvent();
235         
236         JListOperator opList2 = opMultiView.getColumnListOperator(2);
237         opList2.selectItem("CT1");
238         
239         opMultiView.switchToSource();
240         boolean bValid = isSchemaValid(TEST_SCHEMA_NAME);
241         opMultiView.switchToSchema();
242         
243         if (!bValid) {
244             failInvalidSchema();
245         }
246         
247         endTest();
248     }
249     
250     public void applyDesignPattern() {
251         startTest();
252         
253         ProjectsTabOperator pto = new ProjectsTabOperator();
254         
255         JTreeOperator opTree = pto.tree();
256         
257         // work with nodes
258
ProjectRootNode prn = pto.getProjectRootNode("XSDTestProject");
259         Node node = new Node(prn, "Source Packages|<default package>|" + TEST_SCHEMA_NAME + SCHEMA_EXTENSION);
260         
261         node.callPopup().pushMenuNoBlock("Apply Design Pattern...");
262         Helpers.waitNoEvent();
263         
264         WizardOperator opWizard = new WizardOperator("Apply Design Pattern");
265         new JRadioButtonOperator(opWizard, "Create a Single Global Element").pushNoBlock();
266         Helpers.waitNoEvent();
267         
268         new JRadioButtonOperator(opWizard, "Do not Create Type(s)").pushNoBlock();
269         Helpers.waitNoEvent();
270         
271         opWizard.finish();
272         Helpers.waitNoEvent();
273         
274         SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
275         opMultiView.switchToSource();
276         boolean bValid = isSchemaValid(TEST_SCHEMA_NAME);
277         opMultiView.switchToSchema();
278         
279         if (!bValid) {
280             failInvalidSchema();
281         }
282         
283         endTest();
284     }
285     
286     private boolean isSchemaValid(String JavaDoc strSchemaName) {
287         boolean bValid = true;
288         
289         EditorOperator opEditor = new EditorOperator(strSchemaName);
290         
291         opEditor.clickForPopup();
292         new JPopupMenuOperator().pushMenu("Validate XML");
293         Helpers.waitNoEvent();
294         
295         OutputOperator opOutput = new OutputOperator();
296         String JavaDoc strOutput = opOutput.getText();
297         
298         if (!strOutput.matches("\\D*0 Error\\(s\\), 0 Warning\\(s\\)\\.\\D*")) {
299             Helpers.writeJemmyLog("Validate XML output:\n" + strOutput);
300             bValid = false;
301         }
302         
303         return bValid;
304     }
305     
306     private void failInvalidSchema() {
307         fail("Schema validation failed.");
308     }
309     
310     private void callPopupOnListItem(JListOperator opList, String JavaDoc strItem, String JavaDoc strMenuPath) {
311         opList.selectItem(strItem);
312         
313         int index = opList.getSelectedIndex();
314         Point JavaDoc p = opList.getClickPoint(index);
315         opList.clickForPopup(p.x, p.y);
316         new JPopupMenuOperator().pushMenuNoBlock(strMenuPath);
317     }
318     
319     public void tearDown() {
320         new SaveAllAction().performAPI();
321     }
322 }
323
Popular Tags