exportbundler.java.svn-base

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

SVN-BASE
1,261
字号
/* * $Id$ * * Copyright 1996-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.tck.j2me.javatest;import com.sun.javatest.TestDescription;import com.sun.tck.cldc.communication.GenericTestBundle;import com.sun.tck.cldc.communication.GenericTestProvider;import com.sun.tck.cldc.communication.TestBundle;import com.sun.tck.cldc.javatest.TestBuilder;import com.sun.tck.cldc.javatest.util.UTFConverter;import com.sun.tck.j2me.utils.FileUtils;import com.sun.tck.j2me.utils.javasourcemodel.CompilationUnit;import com.sun.tck.j2me.utils.javasourcemodel.JavaLanguage;import com.sun.tck.j2me.utils.javasourcemodel.ParseException;import com.sun.tck.midp.javatest.MidBundler;import java.io.File;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.LineNumberReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.MalformedURLException;import java.net.URI;import java.net.URISyntaxException;import java.net.URL;import java.util.ArrayList;import java.util.HashSet;import java.util.Hashtable;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;/** * <code>TestBundler</code> implementation, which stores test requests into * exported bundles generated in specified directory. * Requests will be stored in root resource files with names: * <ul> *   <li>getNextTest1</li> *   <li>getNextTest2</li> *   <li>getNextTest3</li> *   <li>...</li> * </ul> * <p> * Also defines functionality for exporting extra content which can be useful * for user: source files, classes, build scripts and other. * <p> * Created export directory contain subdirectories for each exported bundle  * named <code>test1</code>, <code>test2</code> and so on. These directories * include all bundle specific files. * <p> * At the root of the export directory an HTML index file is generated  * containing the list of all exported bundles and their content. */public class ExportBundler extends MidBundler {        /**     * The name of index HTML file which is created in the export directory     * and lists all exported tests.     */    static final String INDEX_FILE_NAME  = "index.html";            /**     * The name of Ant script file which is created in the export directory     * and contains targets for (re-)building exported tests.     */     static final String BUILD_SCRIPT_FILE_NAME = "build.xml";        /**     * The name of file which is created in the export directory     * and defines general properties applicable to all exported tests.     */    static final String BUILD_PROPERTIES_FILE_NAME = "build.properties";        /**     * The name of build script file which is imported in main build script     * when running by Ant from command line.     */    static final String BUILD_IMPL_FILE_NAME = "build-impl.xml";        /**     * The name of build script file which is imported in main build script     * when running from NetBeans IDE.     */    static final String NB_BUILD_IMPL_FILE_NAME = "nb-build-impl.xml";    /**     * The name of NetBeans project file.     */    static final String NB_PROJECT_FILE_NAME = "project.xml";    /**     * The name of NetBeans project properties file.     */    static final String NB_PROPERTIES_FILE_NAME = "project.properties";    /**     * Default port number for provisioning HTTP server for exported tests.     */    static int DEFAULT_PROVISIONING_HTTP_PORT = 8082;    private ExportBundler() {    }        /**     * Creates instance of <code>ExportBundler</code> with specified parameters.     *      * @param   tdm     <code>TestDescriptionModifier</code> for this test run.     * @param   testExportInfo  <code>TestExportInfo</code> instance containing     *      all test export related parameters for this test run.     */    public ExportBundler(TestDescriptionModifier tdm,            TestExportInfo testExportInfo) {        super(true,  tdm);        this.testExportInfo = testExportInfo;        distributedTests = new HashSet<String>();        bundleAgentArgsMap = new Hashtable<TestBundle, String[]>();    }    /**     * Overrides super implementation. This implementation doesn't add      * <code>agent.dat</code> entry to newly generated JAR file for current     * bundle. It's left for last moment when all client args will be known.     */    protected boolean initBundle() {        isCurrentBundleDistributed = false;        setCurrentBundle(createBundle());        setAppName(generateName());        getAgentArgs()[1] = getAppName();        return getTestBuilder().createJar(getAgentJar());    }    /**     * Adds <code>agent.dat</code> entry to current bundle containing     * arguments for agent returned by {@link #getAgentArgs()}.     */    private void storeAgentArgs() {        /* Note: getAgentArgs always returns the same instance of String[]         * which is reused for all bundles.         * So, the content of obtained instance should not be changed         */        String[] args = getAgentArgs();        if (isCurrentBundleDistributed) {            args = addClientArg(args, distributedClientArg);        }        bundleAgentArgsMap.put(getCurrentBundle(), args.clone());        getTestBuilder().addByteArray("agent.dat",                UTFConverter.stringsToBytes(args));    }        /**     * Writes agent arguments to agentArgs.txt file in the exported bundle     * directory.     */    void exportAgentArguments(TestBundle bundle, File bundleDir,            Properties bundleProps) {        PrintWriter out = null;        String[] args = bundleAgentArgsMap.get(bundle);        if (args != null) {            File agentArgsFile = new File(bundleDir, "agentArgs.txt");            try {                out = new PrintWriter(agentArgsFile);                for (String arg : args) {                    out.println(arg);                }                bundleProps.put("agent.args.source", "agentArgs.txt");                bundleProps.put("agent.args.file", "agent.dat");            } catch (IOException e) {                throw new RuntimeException("Failed to export agentArgs.txt: "                        + e.getMessage(), e);            } finally {                if (out != null) {                    out.close();                }            }        }    }        /**     * Inserts specified argument at the end of client's arguments inside     * agent arguments.     *     * @param   args    agent's arguments containing "-client" argument.     * @param   clientArg   client's argument to insert     *     * @return  agent's arguments with specified client's argument inserted.     */    private String[] addClientArg(String[] args, String clientArg) {        String[] newArgs = new String[args.length + 1];        for (int i = 0, j = 0; i < args.length; i++, j++) {            newArgs[j] = args[i];            if ("-client".equals(args[i])) {	        while (i + 1 < args.length &&	                ! args[i + 1].equals("-verbose") &&	                ! args[i + 1].equals("-bundleId") &&	                ! args[i + 1].equals("-timeout") &&	                ! args[i + 1].equals("-commClient") &&	                ! args[i + 1].equals("-commServiceAddress")) {                    newArgs[++j] = args[++i];                }                newArgs[++j] = clientArg;            }        }        return newArgs;    }        /**     * Overrides the <code>TestBundler.createTestBuilder()</code> method.     * Creates <code>MidExportBuilder</code> instance.     *     * @return  Initialized <code>MidExportBuilder</code> instance.     */    protected TestBuilder createTestBuilder(String jarSourceDirectory,            int jarFileSizeLimit, String testPath) {                ExportTestBuilder builder = new ExportTestBuilder(jarSourceDirectory,                jarFileSizeLimit, testPath, testExportInfo,                getTestProvider().getAppMainClass());        return builder;    }     /**     * <code>TestExportInfo</code> instance to be used for obtaining all     * information related to test export.     */    protected TestExportInfo testExportInfo;    /**     * Stores request if test can be successfully added to the bundle.     */    protected synchronized boolean checkBundle (GenericTestBundle bundle,            String[] test) {        if(!super.checkBundle(bundle,test)) {            return false;        } else {            storeRequest(bundle, test);            return true;        }    }    /**     * Overrides notification on successful dispatching all tests.     * This method is invoked after all tests have been exported and it's the     * time for exporting the things related to whole test suite.     * <p>     * This method invokes {@link #exportBundle(TestBundle bundle)} method     * for each exported bundle and also exports files which don't belong to     * any bundle.     */    protected void allTestsDispatched() {        for (TestBundle bundle : exportedBundles) {            exportBundle(bundle);        }        exportHtmlIndex();        super.allTestsDispatched();    }        /**     * Overrides the parent's method for adding the bundle to the collection     * of exported bundles.     */    public void checkOutBundle(GenericTestBundle bundle) {        storeAgentArgs();        exportedBundles.add(bundle);        super.checkOutBundle(bundle);    }        /**     * Exports specified test bundle.     * <p>     * First, it creates the directory for the bundle if it doesn't yet exist.     * Then it generates properties for this bundle and invokes all      * <code>exportXXX(TestBundle bundle, File bundleDir,     * Properties bundleProps)</code> methods defines in this class. All     * these methods generate specific content in the bundle directory     * and stores properties in the specified <code>Properties</code>     * instance.     * <p>     * {@link #exportProperties(TestBundle bundle, File bundleDir,     *      Properties bundleProps)} is invoked the last. It saves all defined      * properties in <code>test.properties</code> file.     *     * @param   bundle  test bundle to export.     */    protected void exportBundle(TestBundle bundle) {        File bundleDir = createBundleDir(bundle);

⌨️ 快捷键说明

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