websvc.restkit/test/qa-functional/src/org/netbeans/modules/ws/qaf/rest/PatternsTest.java
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2009 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]"
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.
37 * Portions Copyrighted 2009 Sun Microsystems, Inc.
39 package org.netbeans.modules.ws.qaf.rest;
42 import java.util.HashSet;
44 import junit.framework.Test;
45 import org.netbeans.jellytools.Bundle;
46 import org.netbeans.jellytools.EditorOperator;
47 import org.netbeans.jellytools.NbDialogOperator;
48 import org.netbeans.jellytools.WizardOperator;
49 import org.netbeans.jellytools.nodes.Node;
50 import org.netbeans.jemmy.operators.JButtonOperator;
51 import org.netbeans.jemmy.operators.JComboBoxOperator;
52 import org.netbeans.jemmy.operators.JRadioButtonOperator;
53 import org.netbeans.jemmy.operators.JTextFieldOperator;
54 import org.netbeans.junit.NbModuleSuite;
55 import org.netbeans.modules.ws.qaf.utilities.RestWizardOperator;
56 import org.openide.filesystems.FileObject;
57 import org.openide.filesystems.FileUtil;
60 * Tests for New REST from Patterns wizard
64 public class PatternsTest extends RestTestBase {
66 private enum Pattern {
73 public String toString() {
77 return Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_SingletonResource");
80 return Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ContainerItem");
82 //Client-Controlled Container-Item
83 return Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ClientControl");
85 throw new AssertionError("Unknown type: " + this); //NOI18N
89 * Method for getting correct index of the container resource class select
90 * button in the new RESTful web service from patterns wizard for given type
93 * @return index of the container resource class select button
95 public int getRepresentationClassSelectIndex() {
100 case CcContainerItem:
103 throw new AssertionError("Unknown type: " + this); //NOI18N
107 * Method for getting correct index of the container resource representation
108 * class select button in the new RESTful web service from patterns wizard
109 * for given type of the resource
111 * @return index of the container resource representation class select button
113 public int getContainerRepresentationClassSelectIndex() {
118 case CcContainerItem:
121 throw new AssertionError("Unknown type: " + this); //NOI18N
128 * @param testName name of particular test case
130 public PatternsTest(String name) {
135 public String getProjectName() {
136 return "FromPatterns"; //NOI18N
139 protected String getRestPackage() {
140 return "o.n.m.ws.qaf.rest.patterns"; //NOI18N
144 * Test default setting for Singleton pattern
146 public void testSingletonDef() {
147 Set<File> files = createWsFromPatterns(null, Pattern.Singleton, null);
151 * Test application/json mime setting for Singleton pattern
153 public void testSingleton1() {
154 String name = "Singleton1"; //NOI18N
155 Set<File> files = createWsFromPatterns(name, Pattern.Singleton, MimeType.APPLICATION_JSON);
159 * Test text/plain mime setting for Singleton pattern
161 public void testSingleton2() {
162 String name = "Singleton2"; //NOI18N
163 Set<File> files = createWsFromPatterns(name, Pattern.Singleton, MimeType.TEXT_PLAIN);
167 * Test text/html mime setting for Singleton pattern
169 public void testSingleton3() {
170 String name = "Singleton3"; //NOI18N
171 Set<File> files = createWsFromPatterns(name, Pattern.Singleton, MimeType.TEXT_HTML);
175 * Test default setting for Container Item pattern
177 public void testContainerIDef() {
178 Set<File> files = createWsFromPatterns(null, Pattern.ContainerItem, null);
182 * Test application/json mime setting for Container Item pattern
184 public void testContainerI1() {
185 String name = "CI1"; //NOI18N
186 Set<File> files = createWsFromPatterns(name, Pattern.ContainerItem, MimeType.APPLICATION_JSON);
190 * Test text/plain mime setting for Container Item pattern
192 public void testContainerI2() {
193 String name = "CI2"; //NOI18N
194 Set<File> files = createWsFromPatterns(name, Pattern.ContainerItem, MimeType.TEXT_PLAIN);
198 * Test text/html mime setting for Container Item pattern
200 public void testContainerI3() {
201 String name = "CI3"; //NOI18N
202 Set<File> files = createWsFromPatterns(name, Pattern.ContainerItem, MimeType.TEXT_HTML);
206 * Test default setting for Client Controlled Container Item pattern
208 public void testCcContainerIDef() {
209 String name = "Item1"; //NOI18N
210 Set<File> files = createWsFromPatterns(null, Pattern.CcContainerItem, null);
214 * Test application/json mime setting for Client Controlled Container Item pattern
216 public void testCcContainerI1() {
217 String name = "CcCI1"; //NOI18N
218 Set<File> files = createWsFromPatterns(name, Pattern.CcContainerItem, MimeType.APPLICATION_JSON);
222 * Test text/plain mime setting for Client Controlled Container Item pattern
224 public void testCcContainerI2() {
225 String name = "CcCI2"; //NOI18N
226 Set<File> files = createWsFromPatterns(name, Pattern.CcContainerItem, MimeType.TEXT_PLAIN);
230 * Test text/html mime setting for Client Controlled Container Item pattern
232 public void testCcContainerI3() {
233 String name = "CcCI3"; //NOI18N
234 Set<File> files = createWsFromPatterns(name, Pattern.CcContainerItem, MimeType.TEXT_HTML);
238 * Make sure all REST services nodes are visible in project log. view
240 public void testNodes() {
241 Node restNode = getRestNode();
242 assertEquals("missing nodes?", 20, restNode.getChildren().length); //NOI18N
243 restNode.tree().clickOnPath(restNode.getTreePath(), 2);
244 assertTrue("Node not collapsed", restNode.isCollapsed());
247 private Set<File> createWsFromPatterns(String name, Pattern pattern, MimeType mimeType) {
248 //RESTful Web Services from Patterns
249 String patternsTypeName = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "Templates/WebServices/RestServicesFromPatterns");
250 createNewWSFile(getProject(), patternsTypeName);
251 WizardOperator wo = new WizardOperator(patternsTypeName);
252 new JRadioButtonOperator(wo, pattern.ordinal()).clickMouse();
254 wo = new RestWizardOperator(patternsTypeName);
255 //set resource package
256 JComboBoxOperator jcbo = new JComboBoxOperator(wo, new Pkg());
259 jcbo.typeText(getRestPackage());
261 //we're not using Defs when name != null !!!
262 //set resource class name
263 JTextFieldOperator jtfo = new JTextFieldOperator(wo, new ClsName());
265 jtfo.typeText(name + "Cl"); //NOI18N
267 if (mimeType != null) {
268 jcbo = new JComboBoxOperator(wo, new Mime());
269 jcbo.selectItem(mimeType.toString());
271 //set resource representation class
272 if (MimeType.APPLICATION_JSON.equals(mimeType)) {
273 jtfo = new JTextFieldOperator(wo, new RCls());
275 jtfo.typeText("org.codehaus.jettison.json.JSONString"); //NOI18N
276 } else if (MimeType.TEXT_PLAIN.equals(mimeType)) {
277 new JButtonOperator(wo, pattern.getRepresentationClassSelectIndex()).pushNoBlock();
279 String fTypeLbl = Bundle.getStringTrimmed("org.netbeans.modules.java.source.ui.Bundle", "DLG_FindType");
280 NbDialogOperator nbo = new NbDialogOperator(fTypeLbl);
281 new JTextFieldOperator(nbo, 0).typeText("Level"); //NOI18N
284 if (Pattern.Singleton.equals(pattern)) {
286 jtfo = new JTextFieldOperator(wo, new Path());
288 jtfo.typeText(name + "URI"); //NOI18N
291 jtfo = new JTextFieldOperator(wo, new Path());
293 jtfo.typeText("{" + name + "URI}"); //NOI18N
294 //set container resource class name
295 jtfo = new JTextFieldOperator(wo, new CClsName());
297 jtfo.typeText(name + "CClass"); //NOI18N
298 //set container resource Path
299 jtfo = new JTextFieldOperator(wo, new CPath());
301 jtfo.typeText("/" + name + "ContainerURI"); //NOI18N
302 //set container resource representation class
303 if (MimeType.APPLICATION_JSON.equals(mimeType)) {
304 jtfo = new JTextFieldOperator(wo, new CRCls());
306 jtfo.typeText("org.codehaus.jettison.json.JSONObject"); //NOI18N
307 } else if (MimeType.TEXT_PLAIN.equals(mimeType)) {
308 new JButtonOperator(wo, pattern.getContainerRepresentationClassSelectIndex()).pushNoBlock();
310 String fTypeLbl = Bundle.getStringTrimmed("org.netbeans.modules.java.source.ui.Bundle", "DLG_FindType");
311 NbDialogOperator nbo = new NbDialogOperator(fTypeLbl);
312 new JTextFieldOperator(nbo, 0).typeText("Preferences"); //NOI18N
318 String progressDialogTitle = Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_RestServicesFromPatternsProgress");
319 waitDialogClosed(progressDialogTitle);
320 Set<File> createdFiles = new HashSet<File>();
324 createdFiles.add(getFileFromProject(name + "Cl")); //NOI18N
326 createdFiles.add(getFileFromProject("GenericResource")); //NOI18N
331 createdFiles.add(getFileFromProject(name + "Cl")); //NOI18N
332 createdFiles.add(getFileFromProject(name + "CClass")); //NOI18N
334 createdFiles.add(getFileFromProject("ItemResource")); //NOI18N
335 createdFiles.add(getFileFromProject("ItemsResource")); //NOI18N
338 case CcContainerItem:
340 createdFiles.add(getFileFromProject(name + "Cl")); //NOI18N
341 createdFiles.add(getFileFromProject(name + "CClass")); //NOI18N
343 createdFiles.add(getFileFromProject("ItemResource_1")); //NOI18N
344 createdFiles.add(getFileFromProject("ItemsResource_1")); //NOI18N
348 closeCreatedFiles(createdFiles);
349 checkFiles(createdFiles);
353 private File getFileFromProject(String fileName) {
354 FileObject fo = getProject().getProjectDirectory().getFileObject("src/java"); //NOI18N
355 String location = getRestPackage().replace('.', '/') + "/" + fileName + ".java"; //NOI18N
356 FileObject file = fo.getFileObject(location);
357 assertNotNull(fileName + " not found at " + FileUtil.toFile(fo).getAbsolutePath() + File.separator + location, file); //NOI18N
358 return FileUtil.toFile(file);
361 private void closeCreatedFiles(Set<File> files) {
362 for (File f : files) {
363 EditorOperator eo = new EditorOperator(f.getName());
369 * Creates suite from particular test cases. You can define order of testcases here.
371 public static Test suite() {
372 return NbModuleSuite.create(addServerTests(Server.GLASSFISH_V3, NbModuleSuite.createConfiguration(PatternsTest.class),
373 "testSingletonDef", //NOI18N
374 "testContainerIDef", //NOI18N
375 "testCcContainerIDef", //NOI18N
376 "testSingleton1", //NOI18N
377 "testCcContainerI1", //NOI18N
378 "testSingleton2", //NOI18N
379 "testContainerI1", //NOI18N
380 "testContainerI2", //NOI18N
381 "testSingleton3", //NOI18N
382 "testContainerI3", //NOI18N
383 "testCcContainerI2", //NOI18N
384 "testCcContainerI3", //NOI18N
385 "testNodes", //NOI18N
386 "testDeploy", //NOI18N
387 "testUndeploy" //NOI18N
388 ).enableModules(".*").clusters(".*")); //NOI18N
391 static class Pkg extends JComponentByLabelFinder {
395 super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_Package"));
399 static class ClsName extends JComponentByLabelFinder {
403 super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ClassName"));
407 static class Mime extends JComponentByLabelFinder {
411 super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_MimeType"));
415 static class RCls extends JComponentByLabelFinder {
418 //Representation Class:
419 super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_RepresentationClass"));
423 static class Path extends JComponentByLabelFinder {
427 super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_UriTemplate"));
431 static class CClsName extends JComponentByLabelFinder {
434 //Container Class Name:
435 super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ContainerClass"));
439 static class CPath extends JComponentByLabelFinder {
443 super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ContainerUriTemplate"));
447 static class CRCls extends JComponentByLabelFinder {
450 //Container Representation Class:
451 super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_ContainerRepresentationClass"));
455 static class Loc extends JComponentByLabelFinder {
459 super(Bundle.getStringTrimmed("org.netbeans.modules.websvc.rest.wizard.Bundle", "LBL_SrcLocation"));