websvc.kit/test/qa-functional/src/org/netbeans/modules/ws/qaf/editor/HintsTests.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]"
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Micro//Systems, Inc. Portions Copyright 1997-2008 Sun
28 * Micro//Systems, 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.
41 package org.netbeans.modules.ws.qaf.editor;
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.Comparator;
47 import java.util.List;
48 import java.util.logging.Handler;
49 import java.util.logging.Level;
50 import java.util.logging.LogRecord;
51 import java.util.logging.Logger;
52 import junit.framework.Test;
53 import org.netbeans.jellytools.EditorOperator;
54 import org.netbeans.jellytools.NbDialogOperator;
55 import org.netbeans.jemmy.JemmyException;
56 import org.netbeans.junit.NbModuleSuite;
57 import org.netbeans.modules.editor.hints.AnnotationHolder;
58 import org.netbeans.modules.ws.qaf.WebServicesTestBase;
59 import org.netbeans.spi.editor.hints.ErrorDescription;
60 import org.netbeans.spi.editor.hints.Fix;
61 import org.openide.cookies.EditorCookie;
62 import org.openide.filesystems.FileObject;
63 import org.openide.filesystems.FileUtil;
64 import org.openide.loaders.DataObject;
65 import org.openide.util.RequestProcessor;
68 * Duration of this test suite: aprox. 2min
70 * @author Jindrich Sedek
72 public class HintsTests extends WebServicesTestBase {
74 private List<Fix> fixes;
75 private List<ErrorDescription> problems;
78 * Creates a new instance of WebServices
80 public HintsTests(String S) {
84 public void testEndpointInterface() throws Exception {
85 hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/EndpointInterface.java"));
88 public void testExceptions() throws Exception {
89 hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/Exceptions.java"));
92 public void testHandlers() throws Exception {
93 hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/Handlers.java"));
96 public void testHandlers2() throws Exception {
97 hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/Handlers.java"), 1, null, 2);
100 public void testIOParameters() throws Exception {
101 hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/IOParametrs.java"));
104 public void testReturnValue() throws Exception {
105 hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/ReturnValue.java"));
108 public void testServiceName() throws Exception {
109 hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/ServiceName.java"));
112 private void hintTest(File file) throws Exception {
113 hintTest(file, 0, null, 1);
117 protected String getProjectName() {
121 protected void hintTest(File testedFile, int fixOrder, String captionDirToClose, int size) throws Exception {
122 String result = null;
124 log("STARTING HINT TEST");
125 FileObject fileToTest = FileUtil.toFileObject(testedFile);
126 DataObject dataToTest = DataObject.find(fileToTest);
127 EditorCookie editorCookie = dataToTest.getCookie(EditorCookie.class);
129 EditorOperator operator = new EditorOperator(testedFile.getName());
130 assertNotNull(operator);
131 String text = operator.getText();
133 assertFalse(text.length() == 0);
134 waitHintsShown(fileToTest, size);
135 for (ErrorDescription errorDescription : problems) {
136 ref(errorDescription.toString());
138 for (Fix fix : fixes) {
141 final Fix fix = fixes.get(fixOrder);
142 RequestProcessor.Task task = RequestProcessor.getDefault().post(new Runnable() {
147 } catch (Exception ex) {
148 ex.printStackTrace();
149 fail("IMPLEMENT" + ex.toString());
153 closeDialog(captionDirToClose);
154 task.waitFinished(1000);
156 while (!editorCookie.isModified()) {
157 log("WAITING FOR MODIFICATION:" + count);
160 throw new AssertionError("NO CODE EDITED");
163 ref("---------------------");
164 result = operator.getText();
165 assertFalse(text.equals(result));
166 } catch (Exception e) {
167 System.err.println(e);
171 EditorOperator.closeDiscardAll();
176 public List<ErrorDescription> getProblems(FileObject fileToTest) {
177 problems = AnnotationHolder.getInstance(fileToTest).getErrors();
178 Collections.sort(problems, new Comparator<ErrorDescription>() {
180 public int compare(ErrorDescription o1, ErrorDescription o2) {
181 return o1.toString().compareTo(o2.toString());
187 public static Test suite() {
188 return NbModuleSuite.create(addServerTests(NbModuleSuite.createConfiguration(HintsTests.class),
189 "testEndpointInterface",
196 ).enableModules(".*").clusters(".*"));
199 private static class HintsHandler extends Handler {
201 RequestProcessor.Task task;
204 public HintsHandler(int delay, RequestProcessor.Task task) {
210 public void publish(LogRecord record) {
211 if (record.getMessage().contains("updateAnnotations")) {
212 Logger.getLogger("TEST").info("RESCHEDULING");
213 task.schedule(delay);
218 public void flush() {
222 public void close() throws SecurityException {
226 protected void waitHintsShown(FileObject fileToTest, int size) {
227 final int delay = 1000;
229 final Object lock = new Object();
230 Runnable posted = new Runnable() {
233 synchronized (lock) {
238 RequestProcessor RP = new RequestProcessor("TEST REQUEST PROCESSOR");
239 final RequestProcessor.Task task = RP.create(posted);
240 HintsHandler handl = new HintsHandler(delay, task);
241 Logger logger = Logger.getLogger(AnnotationHolder.class.getName());
242 logger.setLevel(Level.FINE);
245 synchronized (lock) {
246 task.schedule(delay);
247 logger.addHandler(handl);
248 lock.wait(repeat * delay);
250 } while ((repeat-- > 0) && (getFixes(fileToTest).size() < size));
251 } catch (InterruptedException intExc) {
252 throw new JemmyException("REFRESH DID NOT FINISHED IN " + repeat * delay + " SECONDS", intExc);
254 logger.removeHandler(handl);
258 public List<Fix> getFixes(FileObject fileToTest) {
259 fixes = new ArrayList<Fix>();
260 for (ErrorDescription errorDescription : getProblems(fileToTest)) {
261 fixes.addAll(errorDescription.getFixes().getFixes());
263 Collections.sort(fixes, new Comparator<Fix>() {
265 public int compare(Fix o1, Fix o2) {
266 return o1.getText().compareTo(o2.getText());
272 private void closeDialog(String dialogName) {
273 if (dialogName == null) {
276 new NbDialogOperator(dialogName).ok();