xml.schema/test/qa-functional/src/org/netbeans/test/xml/schema/AcceptanceTestCase.java
author michaelnazarov@netbeans.org
Thu Jun 26 17:03:15 2008 +0400 (7 days ago)
changeset 86614 d64f5d1324bb
parent 8186080351c1ff463
permissions -rw-r--r--
XML automation
        1 /*
        2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
        3  *
        4  * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
        5  *
        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]"
       23  *
       24  * Contributor(s):
       25  *
       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.
       29  *
       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.
       40  */
       41 
       42 package org.netbeans.test.xml.schema;
       43 
       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;
       68 
       69 import org.netbeans.junit.NbModuleSuite;
       70 import junit.framework.Test;
       71 
       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;
       78 import java.io.File;
       79 import java.io.IOException;
       80 
       81 import java.util.Properties;
       82 
       83 /**
       84  *
       85  * @author ca@netbeans.org
       86  */
       87 
       88 public class AcceptanceTestCase extends JellyTestCase {
       89     
       90     static final String [] m_aTestMethods = {
       91         "createNewSchema",
       92                 "createSchemaComponents",
       93                 "customizeSchema"
       94                 //"checkSourceCRC",
       95                 //"refactorComplexType",
       96                 //"applyDesignPattern"
       97     };
       98     
       99     static final String TEST_SCHEMA_NAME = "testSchema";
      100     static final String SCHEMA_EXTENSION = ".xsd";
      101     
      102     public AcceptanceTestCase(String arg0) {
      103         super(arg0);
      104     }
      105 
      106     /*    
      107     public static TestSuite suite() {
      108         TestSuite testSuite = new TestSuite(AcceptanceTestCase.class.getName());
      109         
      110         for (String strMethodName : m_aTestMethods) {
      111             testSuite.addTest(new AcceptanceTestCase(strMethodName));
      112         }
      113         
      114         return testSuite;
      115     }
      116     */
      117 
      118     public static Test suite( )
      119     {
      120       return NbModuleSuite.create(
      121           NbModuleSuite.createConfiguration( AcceptanceTestCase.class ).addTest(
      122               "createNewSchema",
      123               "createSchemaComponents",
      124               "customizeSchema",
      125               "checkSourceCRC",
      126               "refactorComplexType",
      127               "applyDesignPattern"
      128            )
      129            .enableModules( ".*" )
      130            .clusters( ".*" )
      131            //.gui( true )
      132         );
      133     }
      134 
      135     public void setUp( )
      136     {
      137       try
      138       {
      139         String sBase = System.getProperty( "nbjunit.workdir" ) + File.separator + ".." + File.separator + "data" + File.separator;
      140         // Extract zip data
      141         ZipFile zf = new ZipFile( sBase + "projects.zip" );
      142         Enumeration<? extends ZipEntry> ent = zf.entries( );
      143         while( ent.hasMoreElements( ) )
      144         {
      145           ZipEntry e = ent.nextElement( );
      146           String name = e.getName( );
      147           if( e.isDirectory( ) )
      148           {
      149             ( new File( sBase + name ) ).mkdirs( );
      150           }
      151           else
      152           {
      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 );
      157             int r;
      158             byte[] b = new byte[ 1024 ];
      159             while( -1 != ( r = is.read( b ) ) )
      160               os.write( b, 0, r );
      161             is.close( );
      162             os.flush( );
      163             os.close( );
      164           }
      165         }
      166         zf.close( );
      167 
      168         // Open project
      169         openDataProjects( "XSDTestProject" );
      170       }
      171       catch( IOException ex )
      172       {
      173         System.out.println( "+++ Projects failed +++" );
      174       }
      175     }
      176     
      177     public void createNewSchema() {
      178         startTest();
      179         
      180         NewFileWizardOperator opNewFileWizard = NewFileWizardOperator.invoke();
      181         opNewFileWizard.selectCategory("XML");
      182         opNewFileWizard.selectFileType("XML Schema");
      183         opNewFileWizard.next();
      184         
      185         NewFileNameLocationStepOperator opNewFileNameLocationStep = new NewFileNameLocationStepOperator();
      186         opNewFileNameLocationStep.setObjectName(TEST_SCHEMA_NAME);
      187         opNewFileWizard.finish();
      188         
      189         TopComponentOperator opTopComponent = new TopComponentOperator(TEST_SCHEMA_NAME + SCHEMA_EXTENSION);
      190         
      191         endTest();
      192     }
      193     
      194     public void createSchemaComponents() {
      195         startTest();
      196         
      197         String[][] aComponentsMenu = {
      198             {"Complex Type...", "CT"},
      199             {"Simple Type...",  "ST"},
      200             {"Element...",      "E"},
      201             {"Attribute...",    "A"}
      202         };
      203         
      204         SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
      205         JListOperator opList = opMultiView.getColumnListOperator(0);
      206         
      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();
      212             
      213             JDialogOperator opCustomizer = new JDialogOperator();
      214             new JTextFieldOperator(opCustomizer, 0).setText(aComponentsMenu[i][1]);
      215             new JButtonOperator(opCustomizer, "OK").pushNoBlock();
      216             Helpers.waitNoEvent();
      217         }
      218         
      219         endTest();
      220     }
      221     
      222     public void customizeSchema() {
      223         startTest();
      224 
      225         SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
      226         
      227         //  Customize Element
      228         JListOperator opList0 = opMultiView.getColumnListOperator(0);
      229         opList0.selectItem("Elements");
      230         
      231         JListOperator opList1 = opMultiView.getColumnListOperator(1);
      232         callPopupOnListItem(opList1, "E", "Customize");
      233         
      234         JDialogOperator opCustomizer = new JDialogOperator();
      235         new JRadioButtonOperator(opCustomizer, "Use Existing Type").pushNoBlock();
      236         Helpers.waitNoEvent();
      237         
      238         JTreeOperator opTree = new JTreeOperator(opCustomizer);
      239         TreePath treePath = opTree.findPath("Complex Types|CT");
      240         opTree.selectPath(treePath);
      241         
      242         new JButtonOperator(opCustomizer, "OK").pushNoBlock();
      243         Helpers.waitNoEvent();
      244         
      245         // Customize Attribute
      246         opList0.selectItem("Attributes");
      247         
      248         opList1 = opMultiView.getColumnListOperator(1);
      249         callPopupOnListItem(opList1, "A", "Customize");
      250         
      251         opCustomizer = new JDialogOperator();
      252         new JRadioButtonOperator(opCustomizer, "Use Existing Type").pushNoBlock();
      253         Helpers.waitNoEvent();
      254         
      255         opTree = new JTreeOperator(opCustomizer);
      256         treePath = opTree.findPath("Simple Types|ST");
      257         opTree.selectPath(treePath);
      258         
      259         new JButtonOperator(opCustomizer, "OK").pushNoBlock();
      260         Helpers.waitNoEvent();
      261         
      262         // Customize Complex Type
      263         opList0.selectItem("Complex Types");
      264         
      265         opList1 = opMultiView.getColumnListOperator(1);
      266         callPopupOnListItem(opList1, "CT", "Add|Attribute Reference");
      267         
      268         opCustomizer = new JDialogOperator();
      269         
      270         opTree = new JTreeOperator(opCustomizer);
      271         treePath = opTree.findPath("Attributes|A");
      272         opTree.selectPath(treePath);
      273         
      274         new JButtonOperator(opCustomizer, "OK").pushNoBlock();
      275         Helpers.waitNoEvent();
      276         
      277         endTest();
      278     }
      279     
      280     public void checkSourceCRC() {
      281         startTest();
      282         
      283         final long goldenCRC32 = 3610664692L;
      284         
      285         SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
      286         opMultiView.switchToSource();
      287         
      288         EditorOperator opEditor = new EditorOperator(TEST_SCHEMA_NAME);
      289         String strText = opEditor.getText();
      290         
      291         opMultiView.switchToSchema();
      292         
      293         strText = strText.replaceAll("[ \t\f\r]", "");
      294         Helpers.writeJemmyLog("{" + strText + "}");
      295         
      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 );
      302         }
      303         
      304         endTest();
      305     }
      306     
      307     public void refactorComplexType() {
      308         startTest();
      309         
      310         SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
      311         
      312         JListOperator opList0 = opMultiView.getColumnListOperator(0);
      313         opList0.selectItem("Complex Types");
      314         
      315         JListOperator opList1 = opMultiView.getColumnListOperator(1);
      316         callPopupOnListItem(opList1, "CT", "Refactor|Rename...");
      317         
      318         JDialogOperator opDialog = new JDialogOperator();
      319         new JTextFieldOperator(opDialog).setText("CT1");
      320         new JButtonOperator(opDialog, "Refactor").pushNoBlock();
      321         opDialog.waitClosed();
      322         
      323         opList0 = opMultiView.getColumnListOperator(0);
      324         opList0.selectItem("Elements");
      325         Helpers.waitNoEvent();
      326         
      327         opList1 = opMultiView.getColumnListOperator(1);
      328         opList1.selectItem("E");
      329         Helpers.waitNoEvent();
      330         
      331         JListOperator opList2 = opMultiView.getColumnListOperator(2);
      332         opList2.selectItem("CT1");
      333         
      334         opMultiView.switchToSource();
      335         boolean bValid = isSchemaValid(TEST_SCHEMA_NAME);
      336         opMultiView.switchToSchema();
      337         
      338         if (!bValid) {
      339             failInvalidSchema();
      340         }
      341         
      342         endTest();
      343     }
      344     
      345     public void applyDesignPattern() {
      346         startTest();
      347         
      348         ProjectsTabOperator pto = new ProjectsTabOperator();
      349         
      350         JTreeOperator opTree = pto.tree();
      351         
      352         // work with nodes
      353         ProjectRootNode prn = pto.getProjectRootNode("XSDTestProject");
      354         Node node = new Node(prn, "Source Packages|<default package>|" + TEST_SCHEMA_NAME + SCHEMA_EXTENSION);
      355         
      356         node.callPopup().pushMenuNoBlock("Apply Design Pattern...");
      357         Helpers.waitNoEvent();
      358         
      359         WizardOperator opWizard = new WizardOperator("Apply Design Pattern");
      360         new JRadioButtonOperator(opWizard, "Create a Single Global Element").pushNoBlock();
      361         Helpers.waitNoEvent();
      362         
      363         new JRadioButtonOperator(opWizard, "Do not Create Type(s)").pushNoBlock();
      364         Helpers.waitNoEvent();
      365         
      366         opWizard.finish();
      367         Helpers.waitNoEvent();
      368         
      369         SchemaMultiView opMultiView = new SchemaMultiView(TEST_SCHEMA_NAME);
      370         opMultiView.switchToSource();
      371         boolean bValid = isSchemaValid(TEST_SCHEMA_NAME);
      372         opMultiView.switchToSchema();
      373         
      374         if (!bValid) {
      375             failInvalidSchema();
      376         }
      377         
      378         endTest();
      379     }
      380     
      381     private boolean isSchemaValid(String strSchemaName) {
      382         boolean bValid = true;
      383         
      384         EditorOperator opEditor = new EditorOperator(strSchemaName);
      385         
      386         opEditor.clickForPopup();
      387         new JPopupMenuOperator().pushMenu("Validate XML");
      388         Helpers.waitNoEvent();
      389         
      390         OutputOperator opOutput = new OutputOperator();
      391         String strOutput = opOutput.getText();
      392         
      393         if (!strOutput.matches("\\D*0 Error\\(s\\),  0 Warning\\(s\\)\\.\\D*")) {
      394             Helpers.writeJemmyLog("Validate XML output:\n" + strOutput);
      395             bValid = false;
      396         }
      397         
      398         return bValid;
      399     }
      400     
      401     private void failInvalidSchema() {
      402         fail("Schema validation failed.");
      403     }
      404     
      405     private void callPopupOnListItem(JListOperator opList, String strItem, String strMenuPath) {
      406         opList.selectItem(strItem);
      407         
      408         int index = opList.getSelectedIndex();
      409         Point p = opList.getClickPoint(index);
      410         opList.clickForPopup(p.x, p.y);
      411         new JPopupMenuOperator().pushMenuNoBlock(strMenuPath);
      412     }
      413     
      414     public void tearDown() {
      415         new SaveAllAction().performAPI();
      416     }
      417 
      418     protected void startTest(){
      419         super.startTest();
      420         Helpers.closeUMLWarningIfOpened();
      421     }
      422 
      423 }