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-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.test.debugger.Util;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.SwingUtilities;
import org.openide.TopManager;
import org.openide.src.MethodElement;
import org.openide.src.ClassElement;
import org.openide.nodes.Node;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.cookies.LineCookie;
import org.openide.cookies.CloseCookie;
import org.openide.filesystems.FileObject;
import org.openide.text.Line;

/**
 * Class containing common methods used in automated tests.
 *
 * @author  Jan Stola
 */
public class Common extends Object {
    /** Message printed when breakpoint cannot be created. */
    public static String addBug="Breakpoint cannot be created.";
    /** Message printed when added breakpoint could not be found. */
    public static String addFindBug="Breakpoint add/find does not work!";
    /** Message printed when debugger miss the breakpoint. */
    public static String stopBug="Debugger didn't stop on breakpoint.";
    /** Message printed when debugger have problems during start. */
    public static String startBug="Problem found when debugger started. ";
    /** Message printed when debugger is stopped instead of finished. */
    public static String finishBug="Debugger stopped instead of finished.";
    /** Message printed when some breakpoint was not removed correctly. */
    public static String removeBug="Breakpoint was not removed correctly.";
    /** Error message of the last error. */
    public static String error="";
    
    /**
     * Looks for Line object for given className and
     * lineNumber.
     *
     * @param className name of the class the desired line belongs to.
     * @param lineNumber line number of the desired line.
     * @return Line object for given className and
     * lineNumber or null if such Line
     * cannot be created.
     */
    public static Line getLineFor(String className, int lineNumber) {
        LineCookie lc = (LineCookie) getCookieFor(className, LineCookie.class);
        if (lc == null) {
            error+="Class "+className+" does not have LineCookie.";
            return null;
        }
        Line.Set ls = lc.getLineSet();
        Line l = ls.getOriginal(lineNumber - 1);
        if (l == null) {
            error="Cannot find Line for class "+className+" line number "+lineNumber+".";
            return null;
        }
        return l;
    }
    
    /**
     * Checks whether class with given className has specified
     * cookie.
     *
     * @param className class name of the checked class.
     * @param cookie Class of the desired cookie.
     * @return desired cookie if the node representing the specified
     * className really has such cookie. Otherwise returns
     * null.
     */
    public static Node.Cookie getCookieFor(String className, Class cookie) {
        Node n = getNodeFor(className);
        if (n == null) {
            error+="Cannot obtain node for "+className+".";
            return null;
        }
        Node.Cookie c = (Node.Cookie) n.getCookie(cookie);
        if (c == null) {
            error="Cannot find "+cookie+" cookie for class "+className+".";
            return null;
        }
        return c;
    }
    
    /**
     * Looks for Node representing class of the given
     * className.
     *
     * @param className name of the desired class.
     * @return Node representing class of the given
     * className or null if such node
     * doesn't exist.
     */
    public static Node getNodeFor(String className) {
        DataObject doo = getDataObjectFor(className);
        if (doo == null) {
            error+="Cannot obtain data object for class "+className+".";
            return null;
        }
        Node n = doo.getNodeDelegate();
        if (n == null) {
            error="Cannot find NodeDelegate for class "+className+".";
            return null;
        }
        return n;
    }
    
    /**
     * Looks for DataObject representing class of the given
     * className.
     *
     * @param className name of the desired class.
     * @return DataObject representing class of the given
     * className or null if such DataObject
     * doesn't exist.
     */
    public static DataObject getDataObjectFor(String className) {
        String sourceName = className.replace('.','/') + ".java";
        FileObject fo = TopManager.getDefault().getRepository().findResource(sourceName);
        if (fo == null) {
            error="Cannot find source "+sourceName+".";
            return null;
        }
        try {
            return DataObject.find(fo);
        } catch (DataObjectNotFoundException e) {
            error="Cannot find DataObject for file "+className+". "+getString(e)+".";
            return null;
        }
    }
    
    /**
     * Looks for MethodElement for the given className
     * and methodName.
     *
     * @param className name of the class the MethodElement
     * belongs to.
     * @param methodName name of the method the desired MethodElement
     * should represent.
     * @return MethodElement for the given className
     * and methodName or null if such
     * MethodElement doesn't exist.
     */
    public static MethodElement getMethodElement(String className, String methodName) {
        ClassElement ce = ClassElement.forName(className);
        if (ce == null) {
            error="Can not find ClassElement for "+className+".";
            return null;
        }
        MethodElement[] ms = ce.getMethods();
        int i, k = ms.length;
        for (i = 0; i < k; i++)
            if (ms [i].getName().getName().equals(methodName))
                break;
        if (i == k) {
            error="Can not find method "+methodName+" in ClassElement for "+className+".";
            return null;
        }
        return ms [i];
    }
    
    /**
     * Closes editor where the source code of the class with given
     * className is opened.
     *
     * @param className name of the closed class.
     * @return true if there was such editor pane opened
     * and was closed successfully. Returns false otherwise.
     */
    public static boolean closeEditor(String className) {
        // PENDING
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
        }
        Node node=getNodeFor(className);
        if (node==null) {
            error+="Cannot find node for class "+className+".";
            return false;
        }
        final CloseCookie cc=(CloseCookie)node.getCookie(CloseCookie.class);
        if (cc==null) {
            error="Node for class "+className+" does not have CloseCookie.";
            return false;
        }
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    cc.close();
                }
            });
        } catch (InterruptedException e) {
        } catch (InvocationTargetException e) {}
        return true;
    }
    
    /**
     * Checks whether it runs on Linux machine.
     *
     * @return true if this test is running on Linux machine.
     * Returns false otherwise.
     */
    public static boolean isLinux() {
        String osname=System.getProperty("os.name");
        return osname.equals("Linux");
    }
    
    /**
     * Checks whether it runs on Windows machine.
     *
     * @return true if this test is running on Windows machine.
     * Returns false otherwise.
     */
    public static boolean isWindows() {
        String osname=System.getProperty("os.name");
        return osname.equals("Windows NT")||osname.equals("Windows 95")||
        osname.equals("Windows 98")||osname.equals("Windows 2000");
    }
    
    /**
     * Checks whether it runs on Solaris.
     *
     * @return true if this test is running on Solaris.
     * Returns false otherwise.
     */
    public static boolean isSolaris() {
        String osname=System.getProperty("os.name");
        return osname.equals("Solaris")||osname.equals("SunOS");
    }
    
    public static String getString(Throwable exception) {
        StringWriter sw=new StringWriter();
        try {            
            PrintWriter pw=new PrintWriter(sw);
            exception.printStackTrace(pw);
            pw.close();
        } catch (Exception e) {
            return null;
        }
        return sw.toString();
    }
    
}
... 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.