runaction.java.svn-base

来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 485 行 · 第 1/2 页

SVN-BASE
485
字号
/*
 * $Id$
 *
 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 2 for more details (a copy is
 * included at /legal/license.txt).
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions.
 *
 */

package com.sun.testme.actions;

import com.sun.javatest.TestDescription;
import com.sun.testme.jtharness.testsuite.JTharnessSuiteUtil;
import com.sun.testme.jtviewer.JTViewerTopComponent;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Properties;
import javax.swing.SwingUtilities;
import org.apache.tools.ant.module.api.support.ActionUtils;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ui.OpenProjects;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.execution.ExecutionEngine;
import org.openide.execution.ExecutorTask;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.nodes.Node;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CookieAction;
import org.openide.windows.IOProvider;
import org.openide.windows.InputOutput;

public final class RunAction extends CookieAction {
    
    private static String getRelativePath(File file) {
        Project project = OpenProjects.getDefault().getMainProject();
        JTharnessSuiteUtil testSuiteInfo = (JTharnessSuiteUtil)project.getLookup().lookup(JTharnessSuiteUtil.class);
        
        FileObject rootFO = FileUtil.toFileObject(testSuiteInfo.getWorkDirectory0().getTestSuite().getTestFinder().getRootDir());
        
        File targetFile = (file.isDirectory()) ? file : file.getParentFile();
        FileObject targetFO = FileUtil.toFileObject(targetFile);
        return FileUtil.getRelativePath(rootFO, targetFO);
    }
    
    protected void performAction(Node[] activatedNodes) {
        
        Runnable r = new Runnable() {
            public void run() {
                runTestAndBuild(null);
            }
        };
                
        new Thread(r).start();
/*        
        DataObject c = (DataObject) activatedNodes[0].getCookie(DataObject.class);
        String tests = null;
        OpenProjects openProjects = OpenProjects.getDefault();
        final Project mainProject = openProjects.getMainProject();
        System.setProperty("javatestClassDir", mainProject.getProjectDirectory().getPath() + "/lib/javatest.jar");
        
        System.out.println("Run test " + tests);
        ArrayList<String> argsList = new ArrayList();
        argsList.add("-config");
        argsList.add(JThanessUtils.getInterviewFile().getPath());
        argsList.add("-tests");
        argsList.add("com");
        argsList.add("-runtests");
        final String [] args = new String[argsList.size()];
        argsList.toArray(args);
        
        final com.sun.javatest.tool.Main javatestMain = new com.sun.javatest.tool.Main();

        Runnable r = new Runnable() {
            public void run() {
                try {
                    Runnable r = new Runnable() {
                        public void run() {
                            try {
                                FileObject fob = mainProject.getProjectDirectory().getFileObject("build/plugin.properties");
                                InputStream in = fob.getInputStream();
                                Properties prop = new Properties();
                                prop.load(in);
                                in.close();
                                String timeOut = (String)prop.get("riTimeout");
                                String riCmd = (String)prop.get("riCmd");
                                if ((timeOut == null) || (riCmd == null)) {
                                    return;
                                }
                                Thread.sleep(Integer.valueOf(timeOut).intValue());
                                Exec.execPrint(riCmd);
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    };
                    new Thread(r).start();
                    javatestMain.run(args, new PrintWriter(System.out));
                    JTViewerTopComponent win = (JTViewerTopComponent)JTViewerTopComponent.findInstance();
                    win.initWD();
                    if (!win.isOpened()) {
                        win.open();
                    }
                    win.requestActive();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }        
            }
        };
        new Thread(r).start();
 */
    }
    
    public static void runTestAndBuild(Object obj) {
        Project prj = OpenProjects.getDefault().getMainProject();
        FileObject buildFile = prj.getProjectDirectory().getFileObject("build/build.xml");

        try {
            ExecutorTask task = ActionUtils.runTarget(buildFile, new String[]{"build"}, null);
            task.waitFinished();
        } catch (Exception e) {
            e.printStackTrace();
        }

        String tests = null;
        if (obj == null) {
        } else if (obj instanceof TestDescription) {
            tests = ((TestDescription) obj).getRootRelativePath();
        } else if (obj instanceof File) {
            tests = getRelativePath((File) obj);
        } else {
            return;
        }

        OpenProjects openProjects = OpenProjects.getDefault();
        final Project mainProject = openProjects.getMainProject();
        System.setProperty("javatestClassDir", FileUtil.toFile(mainProject.getProjectDirectory()).getPath() + "/lib/javatest.jar");
        
        System.out.println("Run test " + tests);
        ArrayList<String> argsList = new ArrayList();
        argsList.add("-config");
        Project project = OpenProjects.getDefault().getMainProject();
        JTharnessSuiteUtil testSuiteInfo = (JTharnessSuiteUtil)project.getLookup().lookup(JTharnessSuiteUtil.class);
        
        if (testSuiteInfo.getInterviewFile() == null){
            DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message("You need to pass the configuration interview."));
            return;
        }
        
        argsList.add(testSuiteInfo.getInterviewFile().getPath());
        if (obj != null) {
            argsList.add("-tests");
            argsList.add(tests);
        }
        
        final Properties prop;
        try {
            FileObject fob = mainProject.getProjectDirectory().getFileObject("build/plugin.properties");
            InputStream in = fob.getInputStream();
            prop = new Properties();
            prop.load(in);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        
        argsList.add((String)prop.get("jtParams"));
        
        argsList.add("-runtests");
        final String [] args = new String[argsList.size()];
        argsList.toArray(args);
        
        final String timeOut = (String)prop.get("riTimeout");
        final String riCmd = (String)prop.get("riCmd");
        if ((timeOut == null) || ("".equals(timeOut.trim())) 
          || (riCmd == null)  || ("".equals(riCmd.trim()))) {
            DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message("You need to define the following properties: \n" +
                    "  - riCmd\n" +
                    "  - riTimeout \n" +
                    "in the plugin.properties file"));
            return;
        }
        final int intTimeout;
        try {
            intTimeout = Integer.valueOf(timeOut).intValue();
        } catch (Throwable e) {
            DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message("riTimeout property defined in the plugin.properties file specifies the delay in milliseconds before starting of an agent." +
                    "It should be integer."));
            return;
        }
        
        Runnable r = new Runnable() {
            public void run() {
                try {
                    Thread.sleep(intTimeout);
                    Exec.execPrint(riCmd);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        };

        Runnable r2 = new Runnable() {
            public void run() {
//                com.sun.javatest.tool.Main javatestMain = new com.sun.javatest.tool.Main();
                String sArgs = "";
                for (int i = 0; i < args.length; i++) {
                    sArgs += " " + args[i];
                }
                Exec.execPrint("java -cp " + System.getProperty("javatestClassDir") + " com.sun.javatest.tool.Main " + sArgs);
//                try {
//                    javatestMain.run(args, new PrintWriter(System.out));
//                } catch (CommandContext.Fault ex) {
//                    ex.printStackTrace();
//                } catch (Command.Fault ex) {
//                    ex.printStackTrace();
//                } catch (CommandParser.Fault ex) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?