testbundler.java

来自「cqME :java framework for TCK test.」· Java 代码 · 共 664 行 · 第 1/2 页

JAVA
664
字号
/* * $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.cldc.javatest;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.util.Hashtable;import java.util.Map;import java.util.Set;import java.util.jar.JarFile;import java.util.jar.Manifest;import java.util.logging.Level;import java.util.logging.Logger;import java.util.zip.ZipEntry;import com.sun.javatest.TestDescription;import com.sun.tck.cldc.communication.GenericTestProvider;import com.sun.tck.cldc.communication.TestResultListener;import com.sun.tck.cldc.communication.GenericTestBundle;import com.sun.tck.cldc.communication.TestBundle;import com.sun.tck.cldc.javatest.util.UTFConverter;public class TestBundler {    static {        // Eliminate warnings from java.util.jar.Attributes.        // They happen when duplicate manifest entries are encountered        // while merging manifests. See addManifest().        Logger.getLogger("java.util.jar").setLevel(Level.SEVERE);    }    /**     * Creates a <code>TestBundler</code> instance.     */    public TestBundler() {        // default constructor.    }    // allow custom testBundler    public static void initTestBundler(String tb_classname) {        try {            Class tb_class = Class.forName(tb_classname);            tb = (TestBundler) tb_class.newInstance();        } catch (LinkageError le) {            le.printStackTrace();            throw new IllegalArgumentException("Unexpected: "+le);        } catch (ClassNotFoundException cnfe) {            cnfe.printStackTrace();            throw new IllegalArgumentException("Unexpected: "+cnfe);        } catch (InstantiationException ie) {            ie.printStackTrace();            throw new IllegalArgumentException("Unexpected: "+ie);        } catch (IllegalAccessException iae) {            iae.printStackTrace();            throw new IllegalArgumentException("Unexpected: "+iae);        } catch (ClassCastException cce) {            cce.printStackTrace();            throw new IllegalArgumentException("Unexpected: "+cce);        }    }    // allow custom testBundler    public static void setTestBundler(TestBundler tbundler) {        tb = tbundler;    }    // allow custom testBundler    public static TestBundler getTestBundler() {        if(tb == null) {            throw new IllegalStateException("No test bundler");        }        return tb;    }    public synchronized void registerResources(String testID, String resources){    	builder.registerResources( testID, resources );    }    /**     * Registers with the current TestBuilder a list of additional jar     * files needed by the test for execution. The content of these jar     * files (not jars themselves) will be added to the bundle     * along with the test classes.     * @param testID - test that requires additional jar files.     * @param jarNames - list of jar files whose content to be bundled with the test     */    public synchronized void registerExtraJars(String testID, String[] jarNames){        builder.registerExtraJars(testID, jarNames);    }    /**     * Subclasses may override this method to obtain test's keywords.     * <p>     * Default implementation does nothing.     *     * @deprecated Use {@link #getTestDescription(String)}.     * @param testID     *            The test name.     * @param keywords     *            The set of keywords.     */    public synchronized void registerKeywords(String testID, Set keywords){        // this method is not used anymore, replaced by registerTestDescription    }    /**     * Registers the given test description with this bundler. The bundler may     * use information stored in the description while making decisions on how     * to bundle the test: what additional files to bundle with test, should     * the test be bundled separately, etc.     * <p>     * Invokes {@link #registerKeywords(String, Set)} and     * {@link #registerResources(String, String)} with data obtained from the     * test description.     *     * @param td     *            The test description.     */    public synchronized void registerTestDescription(TestDescription td) {        String testId = td.getRootRelativeURL();        descriptions.put(testId, td);        registerKeywords(testId, td.getKeywordTable());        if (td.getParameter("resources") != null) {            registerResources(testId, td.getParameter("resources"));        }                String executeClass = td.getParameter("executeClass");        builder.registerExecuteClass(testId, executeClass);    }    /**     * Returns the registered test description for the given test name.     * <p>     * May return <code>null</code>, if no test description with the     * provided test name has been registered.     *     * @param testId     *            The test name.     * @return The registered test description, or <code>null</code>.     * @see #registerTestDescription(TestDescription)     */    protected synchronized TestDescription getTestDescription(String testId) {        return (TestDescription) descriptions.get(testId);    }    public synchronized void registerClasspath(String testID, String classpath){        pathmap.put(testID, classpath);    }    // remove what we can from heap as soon as a test is finished    public synchronized void finishedTest(String testID) {        descriptions.remove(testID);        pathmap.remove(testID);    }    // called each time the test run starts    public void reset(GenericTestProvider tp, int jarFileSizeLimit,            String testPath, int maxTests,            String agentJar, String[] agentArgs, String clientImplJar) {        reset(tp, jarFileSizeLimit, testPath, maxTests,              agentJar, agentArgs, clientImplJar, null);    }        public void reset(GenericTestProvider tp, int jarFileSizeLimit,            String testPath, int maxTests,            String agentJar, String[] agentArgs, String clientImplJar,             String clientJar) {        // each time creating a new GenericTestProvider to reset its internal buffers        tb.provider = tp;        // make sure that there are no leftover descriptions from previous runs        synchronized (this) {            descriptions.clear();        }        String jarSourceDirectory = tp.getJarSourceDirectory();        tb.builder = createTestBuilder(jarSourceDirectory, jarFileSizeLimit,                testPath);        Manifest manifest = createManifest(agentJar, clientImplJar);        if (! (tb.builder.createJar(agentJar, manifest) &&                tb.builder.addJarFileContent(clientImplJar) &&                tb.builder.addJarFileContent(clientJar)) ) {            throw new RuntimeException (                         "Jar File Size Limit too small to fit framework classes");        }        tb.builder.checkOut("agent_client.jar");        tb.maxTests = maxTests;        tb.agentJar = new File(jarSourceDirectory, "agent_client.jar").getPath();        String[] newArgs = new String[agentArgs.length + 2];         newArgs[0] = "-bundleId";        System.arraycopy(agentArgs, 0, newArgs, 2, agentArgs.length);        tb.agentArgs = newArgs;        tb.clientImplJar = clientImplJar;        reset("test");    }        /**     * Create initialized TestBuilder instance to be used for creating all test     * in this test run. This method can be overriden if need a special     * TestBuilder.     *      * @param   jarSourceDirectory  Pathname of directory where tests will be     *          built.     * @param   jarFileSizeLimit    Maximum size of a JAR file in bytes.     * @param   testPath     *      * @return  Initialized TestBuilder instance.     */    protected TestBuilder createTestBuilder(String jarSourceDirectory,            int jarFileSizeLimit, String testPath) {        return new TestBuilder(jarSourceDirectory, jarFileSizeLimit, testPath);    }            // called for each test by CLDCCommand    public synchronized TestBundle dispatchTest(String[] args,            TestResultListener l) {        synchronized (TestBundler.class) {            setTestsDispatched(getTestsDispatched() + 1);            try {                tb.addTest(args, l);            } finally {                tb.checkFinished();            }        }        if (currentBundle == null)            return lastBundle;        else            return currentBundle;    }    // should be called by TestFinder when the run ends    // it's a hook, but how else?    public synchronized void notifyFinderDone(int testsFound) {        tb.testsFound = testsFound;    }    /**     * Notifies the bundler that one special test has been handled, and there is     * no need to bundle it. There are such special tests that should not be     * bundled, they are executed by other means. The components that execute     * such tests should invoke this method so that the bundler would keep its     * records up-to-date.     */    public synchronized void testSkippedForBundling() {        setTestsDispatched(getTestsDispatched() + 1);        checkFinished();    }    // And this one is called by CldcTCKTestBundle    public String getJarSourceDirectory() {         return tb.provider.getJarSourceDirectory();    }    /**     * Sets the policy that determines what tests should be bundled separately     * from others, as one test per bundle. If the policy has not been set     * explicitly, the default one is used.     * <p>     * The default policy is to treat tests with keywords "negative", "single"     * and "cldc_typechecker_specific" as the ones that need to be bundled     * separately.     *     * @param sepBundlingPolicy     *            The policy to set.     */    public final void setSeparateBundlingPolicy(            SeparateBundlingPolicy sepBundlingPolicy) {        this.sepBundlingPolicy = sepBundlingPolicy;    }    /**     * Returns the current policy that determines what tests should be bundled     * separately from others.     *     * @return The separate bundling policy.     */    public final SeparateBundlingPolicy getSeparateBundlingPolicy() {        return sepBundlingPolicy;    }    /**     * This method is invoked from within the last call of      * {@link #dispatchTest(String[] args, TestResultListener l)}     * during one test run.     */    protected void allTestsDispatched() {        tb.provider.dispatchTestBundle(null);    }        /**     * Creates new instance of <code>GenericTestBundle</code>.     * This method can be called by <code>TestBundler</code> when it needs a new     * instance of <code>GenericTestBundle</code>. Usually it happens before     * <code>TestBundler</code> starts to add tests to new bundle.     * <p>     * This method can be overriden in order to use more specific types of     * test bundles.     *     * @return  new instance of <code>GenericTestBundle</code>.     */    protected GenericTestBundle createBundle () {        return new CldcTCKTestBundle();    }    protected boolean checkBundle (GenericTestBundle bundle, String[] test) {        return true;    }

⌨️ 快捷键说明

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