websvc.restkit/test/qa-functional/src/org/netbeans/modules/ws/qaf/rest/RestCStubsTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun Nov 22 11:35:02 2009 +0100 (10 hours ago)
changeset 154033 fa8b12981306
parent 1161322e479dce2e1d
permissions -rw-r--r--
Merge RP javadoc improvements
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2009 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  * If you wish your version of this file to be governed by only the CDDL
    25  * or only the GPL Version 2, indicate your decision by adding
    26  * "[Contributor] elects to include this software in this distribution
    27  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    28  * single choice of license, a recipient has the option to distribute
    29  * your version of this file under either the CDDL, the GPL Version 2 or
    30  * to extend the choice of license to its licensees as provided above.
    31  * However, if you add GPL Version 2 code and therefore, elected the GPL
    32  * Version 2 license, then the option applies only if the new code is
    33  * made subject to such option by the copyright holder.
    34  *
    35  * Contributor(s):
    36  *
    37  * Portions Copyrighted 2007 Sun Microsystems, Inc.
    38  */
    39 package org.netbeans.modules.ws.qaf.rest;
    40 
    41 import java.io.File;
    42 import java.io.IOException;
    43 import javax.swing.JButton;
    44 import javax.swing.ListModel;
    45 import junit.framework.Test;
    46 import org.netbeans.api.project.Project;
    47 import org.netbeans.api.project.ProjectManager;
    48 import org.netbeans.jellytools.Bundle;
    49 import org.netbeans.jellytools.NbDialogOperator;
    50 import org.netbeans.jellytools.ProjectsTabOperator;
    51 import org.netbeans.jellytools.WizardOperator;
    52 import org.netbeans.jellytools.nodes.ProjectRootNode;
    53 import org.netbeans.jemmy.operators.JButtonOperator;
    54 import org.netbeans.jemmy.operators.JCheckBoxOperator;
    55 import org.netbeans.jemmy.operators.JListOperator;
    56 import org.netbeans.jemmy.operators.JRadioButtonOperator;
    57 import org.netbeans.jemmy.operators.JTextFieldOperator;
    58 import org.netbeans.jemmy.operators.JTreeOperator;
    59 import org.netbeans.junit.NbModuleSuite;
    60 import org.netbeans.modules.project.ui.OpenProjectList;
    61 import org.openide.filesystems.FileUtil;
    62 import org.openide.nodes.Node;
    63 
    64 /**
    65  * Tests for New REST service client stub wizard
    66  *
    67  * @author lukas
    68  */
    69 public class RestCStubsTest extends RestTestBase {
    70 
    71     private static boolean haveProjects = false;
    72 
    73     public RestCStubsTest(String name) {
    74         super(name);
    75     }
    76 
    77     @Override
    78     public void setUp() throws Exception {
    79         super.setUp();
    80         if (!haveProjects) {
    81             File f = new File(getProjectsRootDir(), "FromEntities"); //NOI18N
    82             assertTrue("dependent project not found", f.exists() && f.isDirectory());
    83             Project p = ProjectManager.getDefault().findProject(FileUtil.toFileObject(f));
    84             assertNotNull(p);
    85             if (!OpenProjectList.getDefault().isOpen(p)) {
    86                 openProjects(f.getAbsolutePath());
    87             }
    88             f = new File(getProjectsRootDir(), "FromPatterns"); //NOI18N
    89             assertTrue("dependent project not found", f.exists() && f.isDirectory());
    90             p = ProjectManager.getDefault().findProject(FileUtil.toFileObject(f));
    91             assertNotNull(p);
    92             if (!OpenProjectList.getDefault().isOpen(p)) {
    93                 openProjects(f.getAbsolutePath());
    94             }
    95             haveProjects = true;
    96         }
    97     }
    98 
    99     @Override
   100     protected String getProjectName() {
   101         return "RESTClient"; //NOI18N
   102     }
   103 
   104     /**
   105      * Test the wizard:
   106      * - select target folder using browse... (browse folders dialog)
   107      * - add 2 projects
   108      * - remove 1 project
   109      * - then Cancel the wizard
   110      */
   111     public void testWizard() {
   112         //invoke the wizard
   113         //RESTful Web Service Client Stubs
   114         String cStubsLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "Templates/WebServices/RestClientStubs");
   115         String path = FileUtil.toFile(getProject("FromEntities").getProjectDirectory()).getAbsolutePath(); //NOI18N
   116         String path2 = FileUtil.toFile(getProject("FromPatterns").getProjectDirectory()).getAbsolutePath(); //NOI18N
   117         createNewWSFile(getProject(), cStubsLabel);
   118         WizardOperator wo = new WizardOperator(cStubsLabel);
   119         //browse to set target folder
   120         String browseLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_BrowseFolder");
   121         JButtonOperator jbo = new JButtonOperator(wo, browseLabel);
   122         jbo.push();
   123         String browseFoldersLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_BrowseFolders");
   124         NbDialogOperator ndo = new NbDialogOperator(browseFoldersLabel);
   125         JTreeOperator jto = new JTreeOperator(ndo);
   126         new org.netbeans.jellytools.nodes.Node(jto, jto.findPath("Source Packages")).select(); //NOI18N
   127         ndo.ok();
   128         assertEquals("browse selection not propagated", "", new JTextFieldOperator(wo, 2).getText().trim()); //NOI18N
   129         jbo.push();
   130         ndo = new NbDialogOperator(browseFoldersLabel);
   131         jto = new JTreeOperator(ndo);
   132         new org.netbeans.jellytools.nodes.Node(jto, "Web Pages|WEB-INF").select(); //NOI18N
   133         ndo.ok();
   134         assertEquals("browse selection not propagated", "WEB-INF", new JTextFieldOperator(wo, 2).getText().trim()); //NOI18N
   135         //add project
   136         addProject(wo, path);
   137         JListOperator jlo = new JListOperator(wo, 1);
   138         ListModel lm = jlo.getModel();
   139         try {
   140             Thread.sleep(1000);
   141         } catch (InterruptedException ie) {
   142         }
   143         assertEquals(1, lm.getSize());
   144         //add second project
   145         addProject(wo, path2);
   146         try {
   147             Thread.sleep(1000);
   148         } catch (InterruptedException ie) {
   149         }
   150         assertEquals(2, lm.getSize());
   151         //select first project
   152         jlo.selectItem(0);
   153         //remove it
   154         String removeLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_RemoveProject");
   155         new JButtonOperator(wo, removeLabel).push();
   156         try {
   157             Thread.sleep(1000);
   158         } catch (InterruptedException ie) {
   159         }
   160         assertEquals(1, lm.getSize());
   161         //cancel/close the wizard
   162         wo.cancel();
   163     }
   164 
   165     /**
   166      * Test stubs creation from a foreign project
   167      */
   168     public void testCreateSimpleStubs() {
   169         createStubs("FromEntities"); //NOI18N
   170     }
   171 
   172     /**
   173      * Test stubs creation from a local WADL file
   174      */
   175     public void testFromWADL() throws IOException {
   176         //RESTful Web Service Client Stubs
   177         String cStubsLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "Templates/WebServices/RestClientStubs");
   178         createNewWSFile(getProject(), cStubsLabel);
   179         WizardOperator wo = new WizardOperator(cStubsLabel);
   180         String browseLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_BrowseFolder");
   181         String browseFoldersLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_BrowseFolders");
   182         JButtonOperator jbo = new JButtonOperator(wo, browseLabel);
   183         jbo.push();
   184         new NbDialogOperator(browseFoldersLabel).cancel();
   185         assertEquals("browse selection propagated", "rest", new JTextFieldOperator(wo, 2).getText().trim()); //NOI18N
   186         //click on the use wadl button
   187         JRadioButtonOperator jrbo = new JRadioButtonOperator(wo, 1);
   188         jrbo.clickMouse();
   189         JTextFieldOperator jtfo = new JTextFieldOperator(wo, 0);
   190         jtfo.clearText();
   191         jtfo.typeText(new File(getRestDataDir(), "testApplication.wadl").getCanonicalFile().getAbsolutePath()); //NOI18N
   192         if (useJMaki()) {
   193             new JCheckBoxOperator(wo, 0).setSelected(true);
   194         }
   195         //click on the radio button again to force the wizard to revalidate itself
   196         //WA for: http://www.netbeans.org/issues/show_bug.cgi?id=128445
   197         jrbo.clickMouse();
   198         wo.finish();
   199         //Generating Client Stubs From RESTful Web Services...
   200         String progressLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ClientStubsProgress");
   201         waitDialogClosed(progressLabel);
   202     }
   203 
   204     protected void createStubs(String sourceProject) {
   205         String sourcePath = FileUtil.toFile(getProject(sourceProject).getProjectDirectory()).getAbsolutePath();
   206         //RESTful Web Service Client Stubs
   207         String cStubsLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "Templates/WebServices/RestClientStubs");
   208         createNewWSFile(getProject(), cStubsLabel);
   209         WizardOperator wo = new WizardOperator(cStubsLabel);
   210         addProject(wo, sourcePath);
   211         if (useJMaki()) {
   212             new JCheckBoxOperator(wo, 0).setSelected(true);
   213         }
   214         wo.finish();
   215         //Generating Client Stubs From RESTful Web Services...
   216         String progressLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ClientStubsProgress");
   217         waitDialogClosed(progressLabel);
   218     }
   219 
   220     protected boolean useJMaki() {
   221         return false;
   222     }
   223 
   224     private void addProject(WizardOperator wo, String path) {
   225         //Add Project...
   226         String addProjectLabel = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_AddProject");
   227         new JButtonOperator(wo, addProjectLabel).pushNoBlock();
   228         //Select Project
   229         String prjDlgTitle = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ProjectChooserTitle");
   230         NbDialogOperator ndo = new NbDialogOperator(prjDlgTitle);
   231         JTextFieldOperator jtfo = new JTextFieldOperator(ndo, 0);
   232         jtfo.clearText();
   233         jtfo.typeText(path);
   234         //Open
   235         JButton jb = JButtonOperator.findJButton(ndo.getContentPane(), "Open", false, false); //NOI18N
   236         if (jb != null) {
   237             JButtonOperator jbo = new JButtonOperator(jb);
   238             jbo.pushNoBlock();
   239         } else {
   240             fail("Open button not found...."); //NOI18N
   241         }
   242     }
   243 
   244     private Project getProject(String name) {
   245         ProjectRootNode n = ProjectsTabOperator.invoke().getProjectRootNode(name);
   246         return ((Node) n.getOpenideNode()).getLookup().lookup(Project.class);
   247     }
   248 
   249     /**
   250      * Creates suite from particular test cases. You can define order of testcases here.
   251      */
   252     public static Test suite() {
   253         return NbModuleSuite.create(addServerTests(Server.GLASSFISH, NbModuleSuite.createConfiguration(RestCStubsTest.class),
   254                 "testWizard", //NOI18N
   255                 "testCreateSimpleStubs", //NOI18N
   256                 "testFromWADL", //NOI18N
   257                 "testCloseProject" //NOI18N
   258                 ).enableModules(".*").clusters(".*")); //NOI18N
   259     }
   260 }