xml.schema/test/qa-functional/src/org/netbeans/test/xml/schema/AcceptanceTestCase.java
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28 * Microsystems, Inc. All Rights Reserved.
30 * If you wish your version of this file to be governed by only the CDDL
31 * or only the GPL Version 2, indicate your decision by adding
32 * "[Contributor] elects to include this software in this distribution
33 * under the [CDDL or GPL Version 2] license." If you do not indicate a
34 * single choice of license, a recipient has the option to distribute
35 * your version of this file under either the CDDL, the GPL Version 2 or
36 * to extend the choice of license to its licensees as provided above.
37 * However, if you add GPL Version 2 code and therefore, elected the GPL
38 * Version 2 license, then the option applies only if the new code is
39 * made subject to such option by the copyright holder.
42 package org.netbeans.test.xml.schema;
44 import java.awt.Point;
45 import java.util.zip.CRC32;
46 import javax.swing.tree.TreePath;
47 import junit.framework.TestSuite;
48 import org.netbeans.jellytools.EditorOperator;
49 import org.netbeans.jellytools.JellyTestCase;
50 import org.netbeans.jellytools.NewFileNameLocationStepOperator;
51 import org.netbeans.jellytools.NewFileWizardOperator;
52 import org.netbeans.jellytools.OutputOperator;
53 import org.netbeans.jellytools.ProjectsTabOperator;
54 import org.netbeans.jellytools.TopComponentOperator;
55 import org.netbeans.jellytools.WizardOperator;
56 import org.netbeans.jellytools.actions.SaveAllAction;
57 import org.netbeans.jellytools.nodes.Node;
58 import org.netbeans.jellytools.nodes.ProjectRootNode;
59 import org.netbeans.jemmy.operators.JButtonOperator;
60 import org.netbeans.jemmy.operators.JDialogOperator;
61 import org.netbeans.jemmy.operators.JListOperator;
62 import org.netbeans.jemmy.operators.JPopupMenuOperator;
63 import org.netbeans.jemmy.operators.JRadioButtonOperator;
64 import org.netbeans.jemmy.operators.JTextFieldOperator;
65 import org.netbeans.jemmy.operators.JTreeOperator;
66 import org.netbeans.test.xml.schema.lib.SchemaMultiView;
67 import org.netbeans.test.xml.schema.lib.util.Helpers;
69 import org.netbeans.junit.NbModuleSuite;
70 import junit.framework.Test;
72 import java.util.zip.ZipFile;
73 import java.util.zip.ZipEntry;
74 import java.util.Enumeration;
75 import java.io.InputStream;
76 import java.io.OutputStream;
77 import java.io.FileOutputStream;
79 import java.io.IOException;
81 import java.util.Properties;
85 * @author ca@netbeans.org
88 public class AcceptanceTestCase extends JellyTestCase {
90 static final String [] m_aTestMethods = {
92 "createSchemaComponents",
95 //"refactorComplexType",
96 //"applyDesignPattern"
99 static final String TEST_SCHEMA_NAME = "testSchema";
100 static final String SCHEMA_EXTENSION = ".xsd";
102 public AcceptanceTestCase(String arg0) {
107 public static TestSuite suite() {
108 TestSuite testSuite = new TestSuite(AcceptanceTestCase.class.getName());
110 for (String strMethodName : m_aTestMethods) {
111 testSuite.addTest(new AcceptanceTestCase(strMethodName));
118 public static Test suite( )
120 return NbModuleSuite.create(
121 NbModuleSuite.createConfiguration( AcceptanceTestCase.class ).addTest(
123 "createSchemaComponents",
126 "refactorComplexType",
129 .enableModules( ".*" )
139 String sBase = System.getProperty( "nbjunit.workdir" ) + File.separator + ".." + File.separator + "data" + File.separator;
141 ZipFile zf = new ZipFile( sBase + "projects.zip" );
142 Enumeration<? extends ZipEntry> ent = zf.entries( );
143 while( ent.hasMoreElements( ) )
145 ZipEntry e = ent.nextElement( );
146 String name = e.getName( );
147 if( e.isDirectory( ) )
149 ( new File( sBase + name ) ).mkdirs( );
153 InputStream is = zf.getInputStream( e );
154 //File f = new File( name );
155 //System.out.println( "-->" + f.getPath( ) );
156 OutputStream os = new FileOutputStream( sBase + name );
158 byte[] b = new byte[ 1024 ];
159 while( -1 != ( r = is.read( b ) ) )
169 openDataProjects( "XSDTestProject" );
171 catch( IOException ex )
173 System.out.println( "+++ Projects failed +++" );
177 public void createNewSchema() {
180 NewFileWizardOperator opNewFileWizard = NewFileWizardOperator.invoke();
181 opNewFileWizard.selectCategory("XML");
182 opNewFileWizard.selectFileType("XML Schema");
183 opNewFileWizard.next();
185 NewFileNameLocationStepOperator opNewFileNameLocationStep = new NewFileNameLocationStepOperator();
186 opNewFileNameLocationStep.setObjectName(TEST_SCHEMA_NAME);
187 opNewFileWizard.finish();
189 TopComponentOperator opTopComponent = new TopComponentOperator(TEST_SCHEMA_NAME + SCHEMA_EXTENSION);
194 public void createSchemaComponents() {
197 String[][] aComponentsMenu = {
198 {"Complex Type...", "CT"},
199 {"Simple Type...", "ST"},
201 {"Attribute...", "A"}
204 SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
205 JListOperator opList = opMultiView.getColumnListOperator(0);
207 for (int i = 0; i <aComponentsMenu.length; i++) {
208 Point p = opList.getClickPoint(0);
209 opList.clickForPopup(p.x, p.y);
210 new JPopupMenuOperator().pushMenuNoBlock("Add|" + aComponentsMenu[i][0]);
211 Helpers.waitNoEvent();
213 JDialogOperator opCustomizer = new JDialogOperator();
214 new JTextFieldOperator(opCustomizer, 0).setText(aComponentsMenu[i][1]);
215 new JButtonOperator(opCustomizer, "OK").pushNoBlock();
216 Helpers.waitNoEvent();
222 public void customizeSchema() {
225 SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
228 JListOperator opList0 = opMultiView.getColumnListOperator(0);
229 opList0.selectItem("Elements");
231 JListOperator opList1 = opMultiView.getColumnListOperator(1);
232 callPopupOnListItem(opList1, "E", "Customize");
234 JDialogOperator opCustomizer = new JDialogOperator();
235 new JRadioButtonOperator(opCustomizer, "Use Existing Type").pushNoBlock();
236 Helpers.waitNoEvent();
238 JTreeOperator opTree = new JTreeOperator(opCustomizer);
239 TreePath treePath = opTree.findPath("Complex Types|CT");
240 opTree.selectPath(treePath);
242 new JButtonOperator(opCustomizer, "OK").pushNoBlock();
243 Helpers.waitNoEvent();
245 // Customize Attribute
246 opList0.selectItem("Attributes");
248 opList1 = opMultiView.getColumnListOperator(1);
249 callPopupOnListItem(opList1, "A", "Customize");
251 opCustomizer = new JDialogOperator();
252 new JRadioButtonOperator(opCustomizer, "Use Existing Type").pushNoBlock();
253 Helpers.waitNoEvent();
255 opTree = new JTreeOperator(opCustomizer);
256 treePath = opTree.findPath("Simple Types|ST");
257 opTree.selectPath(treePath);
259 new JButtonOperator(opCustomizer, "OK").pushNoBlock();
260 Helpers.waitNoEvent();
262 // Customize Complex Type
263 opList0.selectItem("Complex Types");
265 opList1 = opMultiView.getColumnListOperator(1);
266 callPopupOnListItem(opList1, "CT", "Add|Attribute Reference");
268 opCustomizer = new JDialogOperator();
270 opTree = new JTreeOperator(opCustomizer);
271 treePath = opTree.findPath("Attributes|A");
272 opTree.selectPath(treePath);
274 new JButtonOperator(opCustomizer, "OK").pushNoBlock();
275 Helpers.waitNoEvent();
280 public void checkSourceCRC() {
283 final long goldenCRC32 = 3610664692L;
285 SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
286 opMultiView.switchToSource();
288 EditorOperator opEditor = new EditorOperator(TEST_SCHEMA_NAME);
289 String strText = opEditor.getText();
291 opMultiView.switchToSchema();
293 strText = strText.replaceAll("[ \t\f\r]", "");
294 Helpers.writeJemmyLog("{" + strText + "}");
296 CRC32 crc32 = new CRC32();
297 crc32.update(strText.getBytes());
298 long checkSum = crc32.getValue();
299 Helpers.writeJemmyLog("CRC32=" + checkSum);
300 if ( checkSum != goldenCRC32) {
301 fail("Schema source check sum doesn't match golden value. Required: " + goldenCRC32 + ", calculated: " + checkSum );
307 public void refactorComplexType() {
310 SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
312 JListOperator opList0 = opMultiView.getColumnListOperator(0);
313 opList0.selectItem("Complex Types");
315 JListOperator opList1 = opMultiView.getColumnListOperator(1);
316 callPopupOnListItem(opList1, "CT", "Refactor|Rename...");
318 JDialogOperator opDialog = new JDialogOperator();
319 new JTextFieldOperator(opDialog).setText("CT1");
320 new JButtonOperator(opDialog, "Refactor").pushNoBlock();
321 opDialog.waitClosed();
323 opList0 = opMultiView.getColumnListOperator(0);
324 opList0.selectItem("Elements");
325 Helpers.waitNoEvent();
327 opList1 = opMultiView.getColumnListOperator(1);
328 opList1.selectItem("E");
329 Helpers.waitNoEvent();
331 JListOperator opList2 = opMultiView.getColumnListOperator(2);
332 opList2.selectItem("CT1");
334 opMultiView.switchToSource();
335 boolean bValid = isSchemaValid(TEST_SCHEMA_NAME);
336 opMultiView.switchToSchema();
345 public void applyDesignPattern() {
348 ProjectsTabOperator pto = new ProjectsTabOperator();
350 JTreeOperator opTree = pto.tree();
353 ProjectRootNode prn = pto.getProjectRootNode("XSDTestProject");
354 Node node = new Node(prn, "Source Packages|<default package>|" + TEST_SCHEMA_NAME + SCHEMA_EXTENSION);
356 node.callPopup().pushMenuNoBlock("Apply Design Pattern...");
357 Helpers.waitNoEvent();
359 WizardOperator opWizard = new WizardOperator("Apply Design Pattern");
360 new JRadioButtonOperator(opWizard, "Create a Single Global Element").pushNoBlock();
361 Helpers.waitNoEvent();
363 new JRadioButtonOperator(opWizard, "Do not Create Type(s)").pushNoBlock();
364 Helpers.waitNoEvent();
367 Helpers.waitNoEvent();
369 SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
370 opMultiView.switchToSource();
371 boolean bValid = isSchemaValid(TEST_SCHEMA_NAME);
372 opMultiView.switchToSchema();
381 private boolean isSchemaValid(String strSchemaName) {
382 boolean bValid = true;
384 EditorOperator opEditor = new EditorOperator(strSchemaName);
386 opEditor.clickForPopup();
387 new JPopupMenuOperator().pushMenu("Validate XML");
388 Helpers.waitNoEvent();
390 OutputOperator opOutput = new OutputOperator();
391 String strOutput = opOutput.getText();
393 if (!strOutput.matches("\\D*0 Error\\(s\\), 0 Warning\\(s\\)\\.\\D*")) {
394 Helpers.writeJemmyLog("Validate XML output:\n" + strOutput);
401 private void failInvalidSchema() {
402 fail("Schema validation failed.");
405 private void callPopupOnListItem(JListOperator opList, String strItem, String strMenuPath) {
406 opList.selectItem(strItem);
408 int index = opList.getSelectedIndex();
409 Point p = opList.getClickPoint(index);
410 opList.clickForPopup(p.x, p.y);
411 new JPopupMenuOperator().pushMenuNoBlock(strMenuPath);
414 public void tearDown() {
415 new SaveAllAction().performAPI();
418 protected void startTest(){
420 Helpers.closeUMLWarningIfOpened();