001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Micro//Systems, Inc. Portions Copyright 1997-2006 Sun
028: * Micro//Systems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.ws.qaf.editor;
042:
043: import java.io.File;
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.Comparator;
047: import java.util.List;
048: import java.util.logging.Handler;
049: import java.util.logging.Level;
050: import java.util.logging.LogRecord;
051: import java.util.logging.Logger;
052: import junit.framework.TestSuite;
053: import org.netbeans.jellytools.EditorOperator;
054: import org.netbeans.jellytools.NbDialogOperator;
055: import org.netbeans.jemmy.JemmyException;
056: import org.netbeans.junit.NbTestSuite;
057: import org.netbeans.modules.editor.hints.AnnotationHolder;
058: import org.netbeans.modules.ws.qaf.WebServicesTestBase;
059: import org.netbeans.spi.editor.hints.ErrorDescription;
060: import org.netbeans.spi.editor.hints.Fix;
061: import org.openide.cookies.EditorCookie;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.FileUtil;
064: import org.openide.loaders.DataObject;
065: import org.openide.util.RequestProcessor;
066:
067: /**
068: *
069: * @author Jindrich Sedek
070: */
071: public class HintsTests extends WebServicesTestBase {
072:
073: private List<Fix> fixes;
074: private List<ErrorDescription> problems;
075:
076: /**
077: * Creates a new instance of WebServices
078: */
079: public HintsTests(String S) {
080: super (S);
081: }
082:
083: public void testEndpointInterface() throws Exception {
084: hintTest(new File(getDataDir(),
085: "projects/Hints/src/java/hints/EndpointInterface.java"));
086: }
087:
088: public void testExceptions() throws Exception {
089: hintTest(new File(getDataDir(),
090: "projects/Hints/src/java/hints/Exceptions.java"));
091: }
092:
093: public void testHandlers() throws Exception {
094: hintTest(new File(getDataDir(),
095: "projects/Hints/src/java/hints/Handlers.java"));
096: }
097:
098: public void testHandlers2() throws Exception {
099: hintTest(new File(getDataDir(),
100: "projects/Hints/src/java/hints/Handlers.java"), 1,
101: null, 2);
102: }
103:
104: public void testIOParameters() throws Exception {
105: hintTest(new File(getDataDir(),
106: "projects/Hints/src/java/hints/IOParametrs.java"));
107: }
108:
109: public void testReturnValue() throws Exception {
110: hintTest(new File(getDataDir(),
111: "projects/Hints/src/java/hints/ReturnValue.java"));
112: }
113:
114: public void testServiceName() throws Exception {
115: hintTest(new File(getDataDir(),
116: "projects/Hints/src/java/hints/ServiceName.java"));
117: }
118:
119: private void hintTest(File file) throws Exception {
120: hintTest(file, 0, null, 1);
121: }
122:
123: @Override
124: protected String getProjectName() {
125: return "Hints";
126: }
127:
128: protected void hintTest(File testedFile, int fixOrder,
129: String captionDirToClose, int size) throws Exception {
130: String result = null;
131: try {
132: log("STARTING HINT TEST");
133: FileObject fileToTest = FileUtil.toFileObject(testedFile);
134: DataObject dataToTest = DataObject.find(fileToTest);
135: EditorCookie editorCookie = dataToTest
136: .getCookie(EditorCookie.class);
137: editorCookie.open();
138: EditorOperator operator = new EditorOperator(testedFile
139: .getName());
140: assertNotNull(operator);
141: String text = operator.getText();
142: assertNotNull(text);
143: assertFalse(text.length() == 0);
144: waitHintsShown(fileToTest, size);
145: for (ErrorDescription errorDescription : problems) {
146: ref(errorDescription.toString());
147: }
148: for (Fix fix : fixes) {
149: ref(fix.getText());
150: }
151: final Fix fix = fixes.get(fixOrder);
152: RequestProcessor.Task task = RequestProcessor.getDefault()
153: .post(new Runnable() {
154:
155: public void run() {
156: try {
157: fix.implement();
158: } catch (Exception ex) {
159: ex.printStackTrace();
160: fail("IMPLEMENT" + ex.toString());
161: }
162: }
163: });
164: closeDialog(captionDirToClose);
165: task.waitFinished(1000);
166: int count = 0;
167: while (!editorCookie.isModified()) {
168: log("WAITING FOR MODIFICATION:" + count);
169: Thread.sleep(1000);
170: if (++count == 10) {
171: throw new AssertionError("NO CODE EDITED");
172: }
173: }
174: ref("---------------------");
175: result = operator.getText();
176: assertFalse(text.equals(result));
177: } catch (Exception e) {
178: System.err.println(e);
179: e.printStackTrace();
180: } finally {
181: ref(result);
182: EditorOperator.closeDiscardAll();
183: Thread.sleep(1000);
184: }
185: }
186:
187: public List<ErrorDescription> getProblems(FileObject fileToTest) {
188: problems = AnnotationHolder.getInstance(fileToTest).getErrors();
189: Collections.sort(problems, new Comparator<ErrorDescription>() {
190:
191: public int compare(ErrorDescription o1, ErrorDescription o2) {
192: return o1.toString().compareTo(o2.toString());
193: }
194: });
195: return problems;
196: }
197:
198: public static TestSuite suite() {
199: TestSuite suite = new NbTestSuite();
200: suite.addTest(new HintsTests("testEndpointInterface")); //NOI18N
201: suite.addTest(new HintsTests("testExceptions")); //NOI18N
202: suite.addTest(new HintsTests("testHandlers")); //NOI18N
203: suite.addTest(new HintsTests("testHandlers2")); //NOI18N
204: suite.addTest(new HintsTests("testIOParameters")); //NOI18N
205: suite.addTest(new HintsTests("testReturnValue")); //NOI18N
206: suite.addTest(new HintsTests("testServiceName")); //NOI18N
207: return suite;
208: }
209:
210: private static class HintsHandler extends Handler {
211:
212: RequestProcessor.Task task;
213: int delay;
214:
215: public HintsHandler(int delay, RequestProcessor.Task task) {
216: this .task = task;
217: this .delay = delay;
218: }
219:
220: @Override
221: public void publish(LogRecord record) {
222: if (record.getMessage().contains("updateAnnotations")) {
223: Logger.getLogger("TEST").info("RESCHEDULING");
224: task.schedule(delay);
225: }
226: }
227:
228: @Override
229: public void flush() {
230: }
231:
232: @Override
233: public void close() throws SecurityException {
234: }
235: }
236:
237: protected void waitHintsShown(FileObject fileToTest, int size) {
238: final int delay = 1000;
239: int repeat = 20;
240: final Object lock = new Object();
241: Runnable posted = new Runnable() {
242:
243: public void run() {
244: synchronized (lock) {
245: lock.notifyAll();
246: }
247: }
248: };
249: RequestProcessor RP = new RequestProcessor(
250: "TEST REQUEST PROCESSOR");
251: final RequestProcessor.Task task = RP.create(posted);
252: HintsHandler handl = new HintsHandler(delay, task);
253: Logger logger = Logger.getLogger(AnnotationHolder.class
254: .getName());
255: logger.setLevel(Level.FINE);
256: try {
257: do {
258: synchronized (lock) {
259: task.schedule(delay);
260: logger.addHandler(handl);
261: lock.wait(repeat * delay);
262: }
263: } while ((repeat-- > 0)
264: && (getFixes(fileToTest).size() < size));
265: } catch (InterruptedException intExc) {
266: throw new JemmyException("REFRESH DID NOT FINISHED IN "
267: + repeat * delay + " SECONDS", intExc);
268: } finally {
269: logger.removeHandler(handl);
270: }
271: }
272:
273: public List<Fix> getFixes(FileObject fileToTest) {
274: fixes = new ArrayList<Fix>();
275: for (ErrorDescription errorDescription : getProblems(fileToTest)) {
276: fixes.addAll(errorDescription.getFixes().getFixes());
277: }
278: Collections.sort(fixes, new Comparator<Fix>() {
279:
280: public int compare(Fix o1, Fix o2) {
281: return o1.getText().compareTo(o2.getText());
282: }
283: });
284: return fixes;
285: }
286:
287: private void closeDialog(String dialogName) {
288: if (dialogName == null) {
289: return;
290: }
291: new NbDialogOperator(dialogName).ok();
292: }
293:
294: }
|