alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.test.java.gui.errorannotations;

import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import junit.textui.TestRunner;
import org.netbeans.jellytools.*;
import org.netbeans.jellytools.properties.Property;
import org.netbeans.jellytools.properties.PropertySheetOperator;
import org.netbeans.jellytools.properties.PropertySheetTabOperator;
import org.netbeans.jellytools.properties.TextFieldProperty;
import org.netbeans.jemmy.EventTool;
import org.netbeans.junit.AssertionFailedErrorException;
import org.netbeans.junit.NbTestSuite;
import org.netbeans.modules.java.JavaEditor;
import org.netbeans.test.java.Utilities;
import org.openide.actions.SaveAllAction;
import org.openide.actions.UndoAction;
import org.openide.cookies.OpenCookie;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.JarFileSystem;
import org.openide.filesystems.Repository;
import org.openide.loaders.DataObject;

public class ErrorAnnotations extends JellyTestCase {
    
    
    private final String sampleDir = Utilities.getSampleDir().getDisplayName();
    private final String EDITING = Bundle.getString("org.netbeans.core.Bundle", "UI/Services/Editing");
    private final String JAVA_SOURCES = Bundle.getString("org.netbeans.modules.java.settings.Bundle", "CTL_Java_option");
    private static final String LIMIT = Bundle.getString("org.netbeans.modules.java.settings.Bundle", "PROP_PARSING_ERRORS");
    private static final String CONFIRM = Bundle.getString("org.openide.explorer.Bundle", "MSG_ConfirmDeleteObjectTitle");
    private static EditorOperator editor;
    private static int DELAY = 4000;

    private static DataObject dob;
    
    /** Need to be defined because of JUnit */
    public ErrorAnnotations(String name) {
        super(name);
    }
    
    public static NbTestSuite suite() {
        NbTestSuite suite = new NbTestSuite();
        suite.addTest(new ErrorAnnotations("test1_3"));
        suite.addTest(new ErrorAnnotations("test2"));
        suite.addTest(new ErrorAnnotations("test4"));
        suite.addTest(new ErrorAnnotations("test5"));
        return suite;
    }
    
    /** Use for execution inside IDE */
    public static void main(java.lang.String[] args) {
        TestRunner.run(suite());
    }
    
    static {
        try {
            dob = DataObject.find(Repository.getDefault().findResource("examples/colorpicker/ColorPreview.java"));        
        } catch (Exception e) {
            throw new AssertionFailedErrorException(e);
        }
    }
    
    public void setUp() {
        System.out.println("########  "+getName()+"  #######");
    }
    
    public void tearDown() {
        ((SaveAllAction) SaveAllAction.findObject(SaveAllAction.class, true)).performAction();
    }
    
    
    private Object[] getAnnotations() {
        JavaEditor edit = (JavaEditor) dob.getCookie(JavaEditor.class);
        ArrayList pa;
        try {
            Field annot = JavaEditor.class.getDeclaredField("annotations");
            annot.setAccessible(true);
            pa = (ArrayList) annot.get(edit);
        } catch (NoSuchFieldException e) {
            throw new AssertionFailedErrorException(e);
        } catch (IllegalAccessException e) {
            throw new AssertionFailedErrorException(e);
        } 
        return pa.toArray();
    }
    
    public void test1_3() { 
        ((OpenCookie) dob.getCookie(OpenCookie.class)).open();
         
        EditorWindowOperator ewo = new EditorWindowOperator();
        editor = ewo.getEditor("ColorPreview");
        editor.insert(" ", 19, 3);
        
        new EventTool().waitNoEvent(DELAY);

        Object[] annots = getAnnotations();
        assertNotNull(annots);
        assertEquals(1, annots.length);
        assertEquals(EditorOperator.getAnnotationType(annots[0]), "org-netbeans-modules-java-parser_annotation");
        assertEquals(EditorOperator.getAnnotationShortDescription(annots[0]), "'class' or 'interface' expected");
    }

    public void test2() { 
        
        //undo
        ((UndoAction) UndoAction.findObject(UndoAction.class, true)).performAction();

        new EventTool().waitNoEvent(DELAY);
        
        Object[] annots = getAnnotations();
        assertEquals(annots.length, 0);
    }

    public void test4() { 
        
        editor.replace("javax.swing.JPanel", "org.openide.windows.CloneableTopComponent");

        new EventTool().waitNoEvent(DELAY);
        
        Object annot = getAnnotations()[0];
        assertNotNull(annot);
        assertEquals(EditorOperator.getAnnotationType(annot), "org-netbeans-modules-java-parser_annotation");
        assertTrue(EditorOperator.getAnnotationShortDescription(annot).startsWith("cannot resolve symbol"));
        
        mountOpenideJar();
        new EventTool().waitNoEvent(DELAY);
        Object[] annots = getAnnotations();
        
        assertEquals(annots.length, 0);
    }
    
    private void mountOpenideJar() {
        String home = System.getProperty("netbeans.home"); // NOI18N
        String mountPoint = home + File.separator + "lib" + File.separator + "openide.jar"; // NOI18N
        mountPoint = mountPoint.replace('\\', '/');
        FileSystem jar = Repository.getDefault().findFileSystem(mountPoint); 
        if (jar == null) {
            try {
                JarFileSystem jfs= new JarFileSystem();
                jfs.setJarFile(new File(mountPoint));
                Repository.getDefault().addFileSystem(jfs);
            } catch (IOException ioe) {
                throw new AssertionFailedErrorException("Mounting FS: "+mountPoint+" failed.", ioe);
            } catch (PropertyVetoException pve) {
                throw new AssertionFailedErrorException("Mounting FS: "+mountPoint+" failed.", pve);
            }
        }
    }

    public void test5() { 
        setLimit(0);
        
        editor.insert(" ", 19, 3);
        
        new EventTool().waitNoEvent(DELAY);

        Object[] annots = getAnnotations();
        assertEquals(annots.length, 0);
        
        setLimit(10);
        editor.replace("pu blic class ColorPreview extends org.openide.windows.CloneableTopComponent", "public class ColorPreview extends javax.swing.JPanel");
    }
    
    private void setLimit(int limit) {
        OptionsOperator optionsOperator = OptionsOperator.invoke();
        PropertySheetOperator pso = optionsOperator.getPropertySheet(EDITING + "|" + JAVA_SOURCES);
        new Property(pso, LIMIT).setValue("" + limit);
        optionsOperator.close();
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.