exportedbundler.java

来自「cqME :java framework for TCK test.」· Java 代码 · 共 314 行

JAVA
314
字号
/* * $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.export.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.communication.TestResultListener;import com.sun.tck.cldc.javatest.TestBundler;import com.sun.tck.cldc.javatest.TestResultDispatcher;import com.sun.tck.cldc.javatest.util.UTFConverter;import com.sun.tck.j2me.javatest.TestExportInfo;import java.io.DataInputStream;import java.io.File;import java.io.IOException;import java.util.HashMap;import java.util.LinkedList;import java.util.Map;import java.util.Properties;import java.util.jar.JarFile;import java.util.zip.ZipEntry;/** * TestBundler for bundling previously bundled (exported) tests. This bundler * is used in &quot;Run exported test&quot; mode. *<p> * This bundler doesn't create test bundles, it only checks that the bundle * specified by user in answer to 'Exported test' question is valid, and * Javatest harness is actually executing tests from the specified bundle. * * It's also responsible for right ordering of executed tests: they should * be registered at the Test Provider in the same order as bundled in the * specified bundle. */public class ExportedBundler extends TestBundler {        /**     * Creates a new instance of <code>ExportedBundler</code>.     *     * @param   testExportInfo  <code>TestExportInfo</code> instance to be used     *          in this test run.     */    public ExportedBundler(TestExportInfo testExportInfo) {        this.testExportInfo = testExportInfo;        tests = new HashMap<String, byte[]>();        testsInOrder = new LinkedList<String>();    }        /**     * Overrides the super implementation, because most of usual initializations     * aren't needed. Does only what is required in &quot;Run exported      * tests&quot; mode.     *     * @param   tp  <code>GenericTestProvider</code> instance to be used as test     *          provider in this test run.     */    public void reset(GenericTestProvider tp, int jarFileSizeLimit,            String testPath, int maxTests, String agentJar, String[] agentArgs,            String clientImplJar, String clientJar) {        setTestProvider(tp);        reset();    }    /**     * Checks that number of tests found by Test Finder equals to number of     * tests contained in the exported bundle.     *     * @exception   RuntimeException    if wrong number of tests were found.     */    public void notifyFinderDone(int testsFound) {        if (testsFound != tests.size()) {            throw new RuntimeException("Wrong number of tests.");        }    }    /**      * Does the bundling job specific for &quot;Run exported test&quot; mode by     * invoking {@link #addTest(String[] args, TestResultListener l)} and     * {@link #checkFinished()}.     *     * @return  <code>TestBundle</code> instance of current bundle. Always     *          returns the same value.     */    public TestBundle dispatchTest(String[] args, TestResultListener l) {        synchronized (TestBundler.class) {            setTestsDispatched(getTestsDispatched() + 1);            try {                addTest(args, l);            } finally {                checkFinished();            }        }        return getCurrentBundle();    }        /**     * Does nothing, because in &quot;Run exported tests&quot; there are no need     * to add extra JAR files to bundles, exported bundle is ready for     * execution.     */    public void registerExtraJars(String testID, String[] jarNames) {        // do nothing    }        /**     * Checks that specified test description relates to test contained in the     * exported bundle.     *     * @param   td  test description to check.     *     * @exception   RuntimeException if test description defines unexpected     *              test.     */    public void registerTestDescription(TestDescription td) {        String testId = td.getRootRelativeURL();                if (!tests.containsKey(testId)) {            throw new RuntimeException("Unexpected test ID");        }    }    /**     * Removes the request for specified finished test from {@link #tests}     * table.     * Invokes the super implementation.     */    public void finishedTest(String testID) {        super.finishedTest(testID);        tests.put(testID, null);    }    /**     * Returns empty <code>GenericTestBundle</code> instance.     *     * @return  <code>GenericTestBundle</code> instance.     */    protected GenericTestBundle createBundle() {        return new GenericTestBundle();    }    /**     * Checks that specified test is contained in the exported bundle. Generates     * the request from the specified arguments and store it in      * {@link #tests} table.     *     * @param   args   Test arguments. <code>args[0]</code> must be the test ID.     * @param   l   <code>TestResultListener</code>  for this test.     */    protected void addTest(String[] args, TestResultListener l) {        String testId = args[0];        byte[] req = UTFConverter.stringsToBytes(args);                if (!tests.containsKey(testId) || tests.get(testId) != null) {            throw new RuntimeException("Unexpected test ID: " + testId);        }        tests.put(testId, req);        if (getCurrentBundle() == null) {            setCurrentBundle(createBundle());        }        testResultDispatcher.addTestResultListener(req, l);    }        /**     * Checks whether all tests are dispatched. If yes, finalizes current     * bundle:     * <ul>     *     <li>Adds all requests from {@link #tests} table to current bundle     *      in order specified by {@link #testsInOrder} list.</li>     *     <li>Invokes {@link #flush()} and {@link #allTestsDispatched()}</li>     * </ul>     */    public void checkFinished() {        if (tests.size() == getTestsDispatched()) {            // need to make a final flush            if (getCurrentBundle() != null) {                while (testsInOrder.size() > 0) {                    getCurrentBundle().addTest(                            tests.get(testsInOrder.removeFirst()));                }                flush();            }            allTestsDispatched();        }    }        /**     * Finalizes current bundle and registers it at the test provider.     */     protected void flush() {        String app = new File(testExportInfo.getExportedTest()).getName();                getCurrentBundle().setApp(app);        checkOutBundle(getCurrentBundle());        getCurrentBundle().setTestResultListener(testResultDispatcher);        getTestProvider().dispatchTestBundle(getCurrentBundle());    }        /**     * Does initializations specific for Run exported test mode including     * loading bundle properties from &quot;test.properties&quot; file and     * initialization of {@link #tests} map.     */    private void reset() {        initTests();        setTestsDispatched(0);    }    /**     * Initializes the {@link #tests} table. Assures that {@link #tests}     * contains all tests from the exported bundle with <code>null     * </code> values.     */    private void initTests() {        if (tests.size() == 0) {            loadTests();        } else {            for (Map.Entry<String, byte[]> e : tests.entrySet()) {                e.setValue(null);            }        }    }        /**     * Reads the content of the exported test bundle and adds the test IDs to     * {@link #tests}.     * <p>     * Also adds the test IDs to {@link #testsInOrder} list in the same order     * as encountered in the exported test bundle.     */    private void loadTests() {        JarFile jarFile = null;        ZipEntry zipEntry = null;                try {            jarFile = new JarFile(new File(testExportInfo.getExportedTest()));            for (   int i = 0;                    (zipEntry = jarFile.getEntry("getNextTest" + i)) != null;                    i++) {                DataInputStream in = new DataInputStream(                        jarFile.getInputStream(zipEntry));                String testId = in.readUTF();                tests.put(testId, null);                testsInOrder.add(testId);                in.close();            }        } catch (IOException e) {            throw new RuntimeException("Cannot read JAR bundle: "                    + testExportInfo.getExportedTest(), e);        } finally {            if (jarFile != null) {                try {                    jarFile.close();                } catch (IOException e) {                    throw new RuntimeException("Unexpected I/O error while "                            + "closing JAR file: " + jarFile.getName(), e);                }            }        }    }        /**     * <code>TestExportInfo</code> instance to use for obtaining all information     * related to test export.     */    protected TestExportInfo testExportInfo;        /**     * Collection of test requests in the exported bundle.     */    protected Map<String, byte[]> tests;        /**     * Ordered list of tests in the exported bundle. Tests should be executed     * in the order defined by this list.     */    protected LinkedList<String> testsInOrder;        /**     * Properties of the exported bundle.     */    protected Properties bundleProperties;        /**     * <code>TestResultListener</code> to be used for running exported tests.     */    protected TestResultDispatcher testResultDispatcher            = new TestResultDispatcher();}

⌨️ 快捷键说明

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