testbundler.java

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

JAVA
664
字号
    protected void undoAddTest() {        builder.undoLastAdded();    }    protected void checkOutBundle (GenericTestBundle bundle) {    }    protected TestBuilder getTestBuilder () {        return builder;    }    /**     * Determines whether the test must be bundled separately from other     * tests.     * <p>     * This method may be overridden by subclasses that wish to provide     * customized behavior, but such overriding is discouraged. Use     * {@link #setSeparateBundlingPolicy(SeparateBundlingPolicy)} instead.     * <p>     * This implementation delegates the decision to the current     * {@link SeparateBundlingPolicy separate bundling policy}.     *     * @param testID     *            The test name.     * @return <code>true</code> if the test should be bundled separately,     *         <code>false</code> otherwise.     * @see #setSeparateBundlingPolicy(SeparateBundlingPolicy)     * @see SeparateBundlingPolicy#mustBundleSeparately(TestDescription)     */    protected boolean mustRunSeparately(String testID) {            return getSeparateBundlingPolicy().mustBundleSeparately(                    getTestDescription(testID));    }    /**     * Returns <code>GenericTestProvider</code> instance which is used as     * test provider in this test run.     *     * @return  instance used as test provider in this test run.     */    protected GenericTestProvider getTestProvider() {        return provider;    }    /**     * Sets the instance of <code>GenericTestProvider</code> to be used in this     * test run.     * <p>     * <code>TestBundler</code> descendants should avoid to override or use     * this method, because <code>TestBundler</code> is aware of setting     * test provider when needed.     * <p>     * To specify test provider for this test run, use {@link #reset(     * GenericTestProvider tp, int jarFileSizeLimit, String testPath,     * int maxTests, String agentJar, String[] agentArgs, String clientImplJar,     * String clientJar)} method.     *     * @param   provider new <code>GenericTestProvider</code> instance for this     *          test run.     */    protected void setTestProvider(GenericTestProvider provider) {        this.provider = provider;    }    /**     * Returns the <code>GenericTestBundle</code> instance representing current     * bundle.     *     * @return  instance representing current bundle.     */    protected GenericTestBundle getCurrentBundle() {        return currentBundle;    }    /**     * Sets current bundle to the specified instance.     * <p>     * <code>TestBundler</code> descendants should avoid to override or use     * this method, because <code>TestBundler</code> is aware of setting     * current bundle instance when needed.     *     * @param   currentBundle   GenericTestBundle instance to be set as current     *          bundle.     */    protected void setCurrentBundle(GenericTestBundle currentBundle) {        this.currentBundle = currentBundle;    }    /**     * Returns the name of current bundle.     *     * @return Name of current bundle.     */    protected String getAppName() {        return app;    }    /**     * Sets the name of current bundle.     * <p>     * <code>TestBundler</code> descendants should avoid to override or use     * this method, because <code>TestBundler</code> is aware of setting     * the name of current bundle when needed.     *     * @param app   new name of current bundle.     */    protected void setAppName(String app) {        this.app = app;    }    /**     * Returns the arguments for Agent in current bundle.     *     * @return  array of arguments.     */    protected String[] getAgentArgs() {        return agentArgs;    }    /**     * Sets the arguments for Agent in current bundle.     * <p>     * <code>TestBundler</code> descendants should avoid to override or use     * this method, because <code>TestBundler</code> is aware of setting     * the arguments for Agent in current bundle when needed.     *     * @param   agentArgs   new array of arguments.     */    protected void setAgentArgs(String[] agentArgs) {        this.agentArgs = agentArgs;    }    /**     * Returns the pathname of the Agent JAR file bundled in current bundle.     *     * @return  Pathname of the Agent JAR file.     */    protected String getAgentJar() {        return agentJar;    }    protected Manifest createManifest(String agentJar, String clientImplJar) {        return addManifest(addManifest(new Manifest(), agentJar), clientImplJar);    }        public int getTestsDispatched() {        return tb.testsDispatched;    }    public void setTestsDispatched(int num) {        tb.testsDispatched = num;    }     public void checkFinished() {        if (tb.testsFound == tb.testsDispatched) {            // need to make a final flush            if (tb.currentBundle != null) {                tb.flush();            }            allTestsDispatched();        }    }// --- no more public stuff below this line --------------    private static TestBundler tb = null;    private GenericTestProvider provider = null;    private String[] agentArgs = null;    private String clientImplJar = null;    private String agentJar = null;    private GenericTestBundle currentBundle = null;    private GenericTestBundle lastBundle = null;    private TestBuilder builder = null;    private TestResultDispatcher testResultDispatcher =             new TestResultDispatcher();    private int testsStored = 0;    private int testsDispatched = 0;    private int testsFound = -1;    private Hashtable pathmap = new Hashtable(50);    // currently registered test descriptions    private Map descriptions = new Hashtable();    // Default policy that determines which tests to be bundled separately.    // See setSeparateBundlingPolicy().    private SeparateBundlingPolicy sepBundlingPolicy = new SeparateBundlingPolicy() {        public boolean mustBundleSeparately(TestDescription td) {            Set keywords = td.getKeywordTable();            return (keywords.contains("negative")                    || keywords.contains("cldc_typechecker_specific")                    || keywords.contains("single"));        }    };    private int maxTests = -1;    private String app = null;        private void addTest(String[] args, TestResultListener l) {        // initialize first if needed        if (currentBundle == null) {            if (!initBundle()) {                currentBundle = null;                builder.drop();                throw new RuntimeException (                         "Agent + Client themselves do not fit into the limit");            }        } else if (mustRunSeparately(args[0])) { // some tests must run alone            flush();            addTest(args, l);            return;        }        String classpath = (String) pathmap.get(args[0]);        // add if possible        if ( builder.addTest(args[0], classpath) && checkBundle(currentBundle, args)) {            byte[] req = UTFConverter.stringsToBytes(args);            testResultDispatcher.addTestResultListener(req, l);            currentBundle.addTest(req);            if (currentBundle.getTestCount() == maxTests ||                     mustRunSeparately(args[0])) {                flush();            }        } else {            undoAddTest();            if (currentBundle.getTestCount() == 0) {                currentBundle = null;                builder.drop();                throw new RuntimeException (                         "Test does not fit into the limit: " + args[0]);            } else {                flush();                addTest(args, l);            }        }    }        /**     * Makes initialization of current bundle before adding tests.     *     * @return  <code>true</code> if the current bundle has been successfully     *          initialized, <code>false</code> otherwise.     */    protected boolean initBundle() {        currentBundle = createBundle();        app = generateName();        agentArgs[1] = app;        return builder.createJar(agentJar) &&                builder.addByteArray("agent.dat",                UTFConverter.stringsToBytes(agentArgs));    }    private void flush() {        currentBundle.setApp(app);        checkOutBundle(currentBundle);        builder.checkOut(app);        currentBundle.setTestResultListener(testResultDispatcher);        provider.dispatchTestBundle(currentBundle);        lastBundle = currentBundle;        currentBundle = null;    }     /**     * Generates new unique bundle name.     *     * @return  String name of bundle.     */    protected static synchronized String generateName() {        return name + (++instanceNumber) +  ".jar";    }        private static synchronized void reset(String s) {        name = s;        reset();    }        private static synchronized void reset() {        instanceNumber = 0;    }        private static int instanceNumber = 0;    private static String name = null;    private static Manifest addManifest(Manifest old, String new_name) {        old = (old == null) ? new Manifest() : old;        if (new_name == null) {            return old;        }        JarFile jf = null;        InputStream in = null;        try {            jf = new JarFile(new_name);            ZipEntry e = jf.getEntry("META-INF/MANIFEST.MF");            if (e != null) {                in = jf.getInputStream(e);                old.read(in);            }            return old;        } catch (IOException e) {            e.printStackTrace();            return old;        } finally {            try {                if (in != null) {                    in.close();                }            } catch (IOException e1) {            }            try {                   if (jf != null) {                    jf.close();                }            } catch (IOException e1) {            }        }    }}class CldcTCKTestBundle extends GenericTestBundle {    /**     * Notifies this bundle that it is finished     */    public void finishing() {        // System.out.println("finishing "+getApp());        new File(TestBundler.getTestBundler().getJarSourceDirectory(), getApp()).delete();    }}

⌨️ 快捷键说明

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