websvc.kit/test/qa-functional/src/org/netbeans/modules/ws/qaf/editor/HintsTests.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun Nov 22 11:35:02 2009 +0100 (10 hours ago)
changeset 154033 fa8b12981306
parent 107555605d721410b0
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  * Contributor(s):
    25  *
    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.
    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 package org.netbeans.modules.ws.qaf.editor;
    42 
    43 import java.io.File;
    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;
    66 
    67 /**
    68  *  Duration of this test suite: aprox. 2min
    69  *
    70  * @author Jindrich Sedek
    71  */
    72 public class HintsTests extends WebServicesTestBase {
    73 
    74     private List<Fix> fixes;
    75     private List<ErrorDescription> problems;
    76 
    77     /**
    78      * Creates a new instance of WebServices
    79      */
    80     public HintsTests(String S) {
    81         super(S);
    82     }
    83 
    84     public void testEndpointInterface() throws Exception {
    85         hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/EndpointInterface.java"));
    86     }
    87 
    88     public void testExceptions() throws Exception {
    89         hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/Exceptions.java"));
    90     }
    91 
    92     public void testHandlers() throws Exception {
    93         hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/Handlers.java"));
    94     }
    95 
    96     public void testHandlers2() throws Exception {
    97         hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/Handlers.java"), 1, null, 2);
    98     }
    99 
   100     public void testIOParameters() throws Exception {
   101         hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/IOParametrs.java"));
   102     }
   103 
   104     public void testReturnValue() throws Exception {
   105         hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/ReturnValue.java"));
   106     }
   107 
   108     public void testServiceName() throws Exception {
   109         hintTest(new File(getDataDir(), "projects/Hints/src/java/hints/ServiceName.java"));
   110     }
   111 
   112     private void hintTest(File file) throws Exception {
   113         hintTest(file, 0, null, 1);
   114     }
   115 
   116     @Override
   117     protected String getProjectName() {
   118         return "Hints";
   119     }
   120 
   121     protected void hintTest(File testedFile, int fixOrder, String captionDirToClose, int size) throws Exception {
   122         String result = null;
   123         try {
   124             log("STARTING HINT TEST");
   125             FileObject fileToTest = FileUtil.toFileObject(testedFile);
   126             DataObject dataToTest = DataObject.find(fileToTest);
   127             EditorCookie editorCookie = dataToTest.getCookie(EditorCookie.class);
   128             editorCookie.open();
   129             EditorOperator operator = new EditorOperator(testedFile.getName());
   130             assertNotNull(operator);
   131             String text = operator.getText();
   132             assertNotNull(text);
   133             assertFalse(text.length() == 0);
   134             waitHintsShown(fileToTest, size);
   135             for (ErrorDescription errorDescription : problems) {
   136                 ref(errorDescription.toString());
   137             }
   138             for (Fix fix : fixes) {
   139                 ref(fix.getText());
   140             }
   141             final Fix fix = fixes.get(fixOrder);
   142             RequestProcessor.Task task = RequestProcessor.getDefault().post(new Runnable() {
   143 
   144                 public void run() {
   145                     try {
   146                         fix.implement();
   147                     } catch (Exception ex) {
   148                         ex.printStackTrace();
   149                         fail("IMPLEMENT" + ex.toString());
   150                     }
   151                 }
   152             });
   153             closeDialog(captionDirToClose);
   154             task.waitFinished(1000);
   155             int count = 0;
   156             while (!editorCookie.isModified()) {
   157                 log("WAITING FOR MODIFICATION:" + count);
   158                 Thread.sleep(1000);
   159                 if (++count == 10) {
   160                     throw new AssertionError("NO CODE EDITED");
   161                 }
   162             }
   163             ref("---------------------");
   164             result = operator.getText();
   165             assertFalse(text.equals(result));
   166         } catch (Exception e) {
   167             System.err.println(e);
   168             e.printStackTrace();
   169         } finally {
   170             ref(result);
   171             EditorOperator.closeDiscardAll();
   172             Thread.sleep(1000);
   173         }
   174     }
   175 
   176     public List<ErrorDescription> getProblems(FileObject fileToTest) {
   177         problems = AnnotationHolder.getInstance(fileToTest).getErrors();
   178         Collections.sort(problems, new Comparator<ErrorDescription>() {
   179 
   180             public int compare(ErrorDescription o1, ErrorDescription o2) {
   181                 return o1.toString().compareTo(o2.toString());
   182             }
   183         });
   184         return problems;
   185     }
   186     
   187     public static Test suite() {
   188         return NbModuleSuite.create(addServerTests(NbModuleSuite.createConfiguration(HintsTests.class),
   189                 "testEndpointInterface",
   190                 "testExceptions", 
   191                 "testHandlers",
   192                 "testHandlers2",
   193                 "testIOParameters",
   194                 "testReturnValue",
   195                 "testServiceName"
   196                 ).enableModules(".*").clusters(".*"));
   197     }
   198 
   199     private static class HintsHandler extends Handler {
   200 
   201         RequestProcessor.Task task;
   202         int delay;
   203 
   204         public HintsHandler(int delay, RequestProcessor.Task task) {
   205             this.task = task;
   206             this.delay = delay;
   207         }
   208 
   209         @Override
   210         public void publish(LogRecord record) {
   211             if (record.getMessage().contains("updateAnnotations")) {
   212                 Logger.getLogger("TEST").info("RESCHEDULING");
   213                 task.schedule(delay);
   214             }
   215         }
   216 
   217         @Override
   218         public void flush() {
   219         }
   220 
   221         @Override
   222         public void close() throws SecurityException {
   223         }
   224     }
   225 
   226     protected void waitHintsShown(FileObject fileToTest, int size) {
   227         final int delay = 1000;
   228         int repeat = 20;
   229         final Object lock = new Object();
   230         Runnable posted = new Runnable() {
   231 
   232             public void run() {
   233                 synchronized (lock) {
   234                     lock.notifyAll();
   235                 }
   236             }
   237         };
   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);
   243         try {
   244             do {
   245                 synchronized (lock) {
   246                     task.schedule(delay);
   247                     logger.addHandler(handl);
   248                     lock.wait(repeat * delay);
   249                 }
   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);
   253         } finally {
   254             logger.removeHandler(handl);
   255         }
   256     }
   257 
   258     public List<Fix> getFixes(FileObject fileToTest) {
   259         fixes = new ArrayList<Fix>();
   260         for (ErrorDescription errorDescription : getProblems(fileToTest)) {
   261             fixes.addAll(errorDescription.getFixes().getFixes());
   262         }
   263         Collections.sort(fixes, new Comparator<Fix>() {
   264 
   265             public int compare(Fix o1, Fix o2) {
   266                 return o1.getText().compareTo(o2.getText());
   267             }
   268         });
   269         return fixes;
   270     }
   271 
   272     private void closeDialog(String dialogName) {
   273         if (dialogName == null) {
   274             return;
   275         }
   276         new NbDialogOperator(dialogName).ok();
   277     }
   278 
   279 }