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.xml.schema;
043:
044: import java.awt.Point;
045: import java.util.zip.CRC32;
046: import javax.swing.tree.TreePath;
047: import junit.framework.TestSuite;
048: import org.netbeans.jellytools.EditorOperator;
049: import org.netbeans.jellytools.JellyTestCase;
050: import org.netbeans.jellytools.NewFileNameLocationStepOperator;
051: import org.netbeans.jellytools.NewFileWizardOperator;
052: import org.netbeans.jellytools.OutputOperator;
053: import org.netbeans.jellytools.ProjectsTabOperator;
054: import org.netbeans.jellytools.TopComponentOperator;
055: import org.netbeans.jellytools.WizardOperator;
056: import org.netbeans.jellytools.actions.SaveAllAction;
057: import org.netbeans.jellytools.nodes.Node;
058: import org.netbeans.jellytools.nodes.ProjectRootNode;
059: import org.netbeans.jemmy.operators.JButtonOperator;
060: import org.netbeans.jemmy.operators.JDialogOperator;
061: import org.netbeans.jemmy.operators.JListOperator;
062: import org.netbeans.jemmy.operators.JPopupMenuOperator;
063: import org.netbeans.jemmy.operators.JRadioButtonOperator;
064: import org.netbeans.jemmy.operators.JTextFieldOperator;
065: import org.netbeans.jemmy.operators.JTreeOperator;
066: import org.netbeans.test.xml.schema.lib.SchemaMultiView;
067: import org.netbeans.test.xml.schema.lib.util.Helpers;
068:
069: /**
070: *
071: * @author ca@netbeans.org
072: */
073:
074: public class AcceptanceTestCase extends JellyTestCase {
075:
076: static final String[] m_aTestMethods = { "createNewSchema",
077: "createSchemaComponents", "customizeSchema",
078: "checkSourceCRC", "refactorComplexType",
079: "applyDesignPattern" };
080:
081: static final String TEST_SCHEMA_NAME = "testSchema";
082: static final String SCHEMA_EXTENSION = ".xsd";
083:
084: public AcceptanceTestCase(String arg0) {
085: super (arg0);
086: }
087:
088: public static TestSuite suite() {
089: TestSuite testSuite = new TestSuite(AcceptanceTestCase.class
090: .getName());
091:
092: for (String strMethodName : m_aTestMethods) {
093: testSuite.addTest(new AcceptanceTestCase(strMethodName));
094: }
095:
096: return testSuite;
097: }
098:
099: public void createNewSchema() {
100: startTest();
101:
102: NewFileWizardOperator opNewFileWizard = NewFileWizardOperator
103: .invoke();
104: opNewFileWizard.selectCategory("XML");
105: opNewFileWizard.selectFileType("XML Schema");
106: opNewFileWizard.next();
107:
108: NewFileNameLocationStepOperator opNewFileNameLocationStep = new NewFileNameLocationStepOperator();
109: opNewFileNameLocationStep.setObjectName(TEST_SCHEMA_NAME);
110: opNewFileWizard.finish();
111:
112: TopComponentOperator opTopComponent = new TopComponentOperator(
113: TEST_SCHEMA_NAME + SCHEMA_EXTENSION);
114:
115: endTest();
116: }
117:
118: public void createSchemaComponents() {
119: startTest();
120:
121: String[][] aComponentsMenu = { { "Complex Type...", "CT" },
122: { "Simple Type...", "ST" }, { "Element...", "E" },
123: { "Attribute...", "A" } };
124:
125: SchemaMultiView opMultiView = new SchemaMultiView(
126: TEST_SCHEMA_NAME);
127: JListOperator opList = opMultiView.getColumnListOperator(0);
128:
129: for (int i = 0; i < aComponentsMenu.length; i++) {
130: Point p = opList.getClickPoint(0);
131: opList.clickForPopup(p.x, p.y);
132: new JPopupMenuOperator().pushMenuNoBlock("Add|"
133: + aComponentsMenu[i][0]);
134: Helpers.waitNoEvent();
135:
136: JDialogOperator opCustomizer = new JDialogOperator();
137: new JTextFieldOperator(opCustomizer, 0)
138: .setText(aComponentsMenu[i][1]);
139: new JButtonOperator(opCustomizer, "OK").pushNoBlock();
140: Helpers.waitNoEvent();
141: }
142:
143: endTest();
144: }
145:
146: public void customizeSchema() {
147: startTest();
148:
149: SchemaMultiView opMultiView = new SchemaMultiView(
150: TEST_SCHEMA_NAME);
151:
152: // Customize Element
153: JListOperator opList0 = opMultiView.getColumnListOperator(0);
154: opList0.selectItem("Elements");
155:
156: JListOperator opList1 = opMultiView.getColumnListOperator(1);
157: callPopupOnListItem(opList1, "E", "Customize");
158:
159: JDialogOperator opCustomizer = new JDialogOperator();
160: new JRadioButtonOperator(opCustomizer, "Use Existing Type")
161: .pushNoBlock();
162: Helpers.waitNoEvent();
163:
164: JTreeOperator opTree = new JTreeOperator(opCustomizer);
165: TreePath treePath = opTree.findPath("Complex Types|CT");
166: opTree.selectPath(treePath);
167:
168: new JButtonOperator(opCustomizer, "OK").pushNoBlock();
169: Helpers.waitNoEvent();
170:
171: // Customize Attribute
172: opList0.selectItem("Attributes");
173:
174: opList1 = opMultiView.getColumnListOperator(1);
175: callPopupOnListItem(opList1, "A", "Customize");
176:
177: opCustomizer = new JDialogOperator();
178: new JRadioButtonOperator(opCustomizer, "Use Existing Type")
179: .pushNoBlock();
180: Helpers.waitNoEvent();
181:
182: opTree = new JTreeOperator(opCustomizer);
183: treePath = opTree.findPath("Simple Types|ST");
184: opTree.selectPath(treePath);
185:
186: new JButtonOperator(opCustomizer, "OK").pushNoBlock();
187: Helpers.waitNoEvent();
188:
189: // Customize Complex Type
190: opList0.selectItem("Complex Types");
191:
192: opList1 = opMultiView.getColumnListOperator(1);
193: callPopupOnListItem(opList1, "CT", "Add|Attribute Reference");
194:
195: opCustomizer = new JDialogOperator();
196:
197: opTree = new JTreeOperator(opCustomizer);
198: treePath = opTree.findPath("Attributes|A");
199: opTree.selectPath(treePath);
200:
201: new JButtonOperator(opCustomizer, "OK").pushNoBlock();
202: Helpers.waitNoEvent();
203:
204: endTest();
205: }
206:
207: public void checkSourceCRC() {
208: startTest();
209:
210: final long goldenCRC32 = 2295334600L;
211:
212: SchemaMultiView opMultiView = new SchemaMultiView(
213: TEST_SCHEMA_NAME);
214: opMultiView.switchToSource();
215:
216: EditorOperator opEditor = new EditorOperator(TEST_SCHEMA_NAME);
217: String strText = opEditor.getText();
218:
219: opMultiView.switchToSchema();
220:
221: strText = strText.replaceAll("[ [\t\f\r]]", "");
222: Helpers.writeJemmyLog("{" + strText + "}");
223:
224: CRC32 crc32 = new CRC32();
225: crc32.update(strText.getBytes());
226: long checkSum = crc32.getValue();
227: Helpers.writeJemmyLog("CRC32=" + checkSum);
228: if (checkSum != goldenCRC32) {
229: fail("Schema source check sum doesn't match golden value");
230: }
231:
232: endTest();
233: }
234:
235: public void refactorComplexType() {
236: startTest();
237:
238: SchemaMultiView opMultiView = new SchemaMultiView(
239: TEST_SCHEMA_NAME);
240:
241: JListOperator opList0 = opMultiView.getColumnListOperator(0);
242: opList0.selectItem("Complex Types");
243:
244: JListOperator opList1 = opMultiView.getColumnListOperator(1);
245: callPopupOnListItem(opList1, "CT", "Refactor|Rename...");
246:
247: JDialogOperator opDialog = new JDialogOperator();
248: new JTextFieldOperator(opDialog).setText("CT1");
249: new JButtonOperator(opDialog, "Refactor").pushNoBlock();
250: opDialog.waitClosed();
251:
252: opList0 = opMultiView.getColumnListOperator(0);
253: opList0.selectItem("Elements");
254: Helpers.waitNoEvent();
255:
256: opList1 = opMultiView.getColumnListOperator(1);
257: opList1.selectItem("E");
258: Helpers.waitNoEvent();
259:
260: JListOperator opList2 = opMultiView.getColumnListOperator(2);
261: opList2.selectItem("CT1");
262:
263: opMultiView.switchToSource();
264: boolean bValid = isSchemaValid(TEST_SCHEMA_NAME);
265: opMultiView.switchToSchema();
266:
267: if (!bValid) {
268: failInvalidSchema();
269: }
270:
271: endTest();
272: }
273:
274: public void applyDesignPattern() {
275: startTest();
276:
277: ProjectsTabOperator pto = new ProjectsTabOperator();
278:
279: JTreeOperator opTree = pto.tree();
280:
281: // work with nodes
282: ProjectRootNode prn = pto.getProjectRootNode("XSDTestProject");
283: Node node = new Node(prn, "Source Packages|<default package>|"
284: + TEST_SCHEMA_NAME + SCHEMA_EXTENSION);
285:
286: node.callPopup().pushMenuNoBlock("Apply Design Pattern...");
287: Helpers.waitNoEvent();
288:
289: WizardOperator opWizard = new WizardOperator(
290: "Apply Design Pattern");
291: new JRadioButtonOperator(opWizard,
292: "Create a Single Global Element").pushNoBlock();
293: Helpers.waitNoEvent();
294:
295: new JRadioButtonOperator(opWizard, "Do not Create Type(s)")
296: .pushNoBlock();
297: Helpers.waitNoEvent();
298:
299: opWizard.finish();
300: Helpers.waitNoEvent();
301:
302: SchemaMultiView opMultiView = new SchemaMultiView(
303: TEST_SCHEMA_NAME);
304: opMultiView.switchToSource();
305: boolean bValid = isSchemaValid(TEST_SCHEMA_NAME);
306: opMultiView.switchToSchema();
307:
308: if (!bValid) {
309: failInvalidSchema();
310: }
311:
312: endTest();
313: }
314:
315: private boolean isSchemaValid(String strSchemaName) {
316: boolean bValid = true;
317:
318: EditorOperator opEditor = new EditorOperator(strSchemaName);
319:
320: opEditor.clickForPopup();
321: new JPopupMenuOperator().pushMenu("Validate XML");
322: Helpers.waitNoEvent();
323:
324: OutputOperator opOutput = new OutputOperator();
325: String strOutput = opOutput.getText();
326:
327: if (!strOutput
328: .matches("\\D*0 Error\\(s\\), 0 Warning\\(s\\)\\.\\D*")) {
329: Helpers.writeJemmyLog("Validate XML output:\n" + strOutput);
330: bValid = false;
331: }
332:
333: return bValid;
334: }
335:
336: private void failInvalidSchema() {
337: fail("Schema validation failed.");
338: }
339:
340: private void callPopupOnListItem(JListOperator opList,
341: String strItem, String strMenuPath) {
342: opList.selectItem(strItem);
343:
344: int index = opList.getSelectedIndex();
345: Point p = opList.getClickPoint(index);
346: opList.clickForPopup(p.x, p.y);
347: new JPopupMenuOperator().pushMenuNoBlock(strMenuPath);
348: }
349:
350: public void tearDown() {
351: new SaveAllAction().performAPI();
352: }
353:
354: protected void startTest() {
355: super.startTest();
356: Helpers.closeUMLWarningIfOpened();
357: }
358:
359: }
|