FileDocCategorySizeDatePackage
JUnitPerfTagHandler.javaAPI DocExample7258Tue Nov 26 19:49:32 GMT 2002com.oreilly.javaxp.xdoclet.perf

JUnitPerfTagHandler.java

package com.oreilly.javaxp.xdoclet.perf;

import xdoclet.XDocletException;
import xdoclet.XDocletTagSupport;
import xdoclet.tagshandler.TypeTagsHandler;

import java.text.MessageFormat;

/**
 * Provides a window into the junitperf.xdt template file. The instance
 * methods can be used in the template file to retrieve or perform
 * logic. See the junitperf.xdt file for an example of these methods are used.
 *
 * @xdoclet.taghandler namespace="Perf"
 *
 * @author Brian M. Coyner
 * @version $version $Id: JUnitPerfTagHandler.java,v 1.11 2002/11/23 03:54:55 jepc Exp $
 */
public class JUnitPerfTagHandler extends XDocletTagSupport {

    public static final String TIMED_TEST = "junitperf.timedtest";
    public static final String LOAD_TEST = "junitperf.loadtest";
    public static final String WAIT_FOR_COMPLETION = "waitForCompletion";
    public static final String NUMBER_OF_USERS = "numberOfUsers";
    public static final String NUMBER_OF_ITERATIONS = "numberOfIterations";
    public static final String MAX_ELAPSED_TIME = "maxElapsedTime";

    /**
     * If the current class being evaluated extends <code>TestCase</code>
     * then add the data contained in the given parameter.
     *
     * @param template the current block of text to be parsed.
     */
    public void ifIsTestCase(String template) throws XDocletException {
        if (TypeTagsHandler.isOfType(getCurrentClass(),
                                     "junit.framework.TestCase",
                                     TypeTagsHandler.TYPE_HIERARCHY)) {
            generate(template);
        }
    }

    /**
     * This shows an example of a content-level method that returns
     * the name of the new name of the generated class. See the
     * junitperf.xdt file for an example of how to use this method.
     *
     * <p>
     * This method shows how to use the active Ant subtask to retrieve
     * attributes defined in the Ant buildfile. Here we extract out the
     * 'jUnitPerfPattern' attribute.
     * </p>
     *
     * @return the new name of the generated class.
     */
    public String className() throws XDocletException {
        JUnitPerfDocletSubTask task = (JUnitPerfDocletSubTask)
                getDocletContext().getActiveSubTask();

        String currentJUnitTest = getCurrentClass().getName();
        return MessageFormat.format(task.getJUnitPerfPattern(),
                                    new Object[]{currentJUnitTest});
    }

    /**
     * This method shows an example of how to use XDoclet to output
     * code that instantiates an object.
     *
     * @return a line of code that instantiates a new JUnitPerf <code>
     * TimedTest</code>.
     */
    public String timedTest() throws XDocletException {
        return "new TimedTest(" + getJUnitConstructor() +
                ", " + getMaxElapsedTime() +
                ", " + getWaitForCompletion() + ");";
    }

    /**
     * This method shows an example of how to use XDoclet to output
     * code that instantiates an object.
     *
     * @return a line of code that instantiates a new JUnitPerf <code>
     * LoadTest</code>.
     */
    public String loadTest() throws XDocletException {
        return "new LoadTest(" + getJUnitConstructor() +
                ", " + getNumberOfUsers() +
                ", " + getNumberOfIterations() + ");";
    }

    /**
     * Helper method that retrieves the current class being evaluated. This
     * class should be an instance of a JUnit <code>TestCase</code> and
     * therefore we pass in as a single parameter the current method name
     * being evaluated. This produces a new instance of a <code>TestCase</code>
     * that executes a single JUnit test method. For example:
     * <code>new TestExample("testExampleLoad");</code> might be
     * the output of the method.
     *
     * @return a line of code that constructs a new <code>TestCase</code>
     * method for executing a single JUnit test method.
     */
    private String getJUnitConstructor() {
        return "new " + getCurrentClass().getName() + "(\"" +
                getCurrentMethod().getName() + "\")";
    }

    /**
     * Helper method that retrieves the number of users to use for a
     * <code>LoadTest</code>. This method shows an example of how
     * to use the <code>xdoclet.XDocletTagSupport.getMethodTagValue()</code>
     * method.
     *
     * @return the number of users to use for a load test. This is a mandatory
     * XDoclet tag parameter (attribute).
     * @throws XDocletException if this attribute does not exist in the
     * source file; it's mandatory!
     */
    private String getNumberOfUsers() throws XDocletException {
        return getTagValue(XDocletTagSupport.FOR_METHOD,
                           LOAD_TEST,
                           NUMBER_OF_USERS,
                           null,
                           null,
                           false,
                           true);
    }

    /**
     * Helper method that retrieves the number of iterations each user
     * of a <code>LoadTest</code> must execute.
     *
     * @return the number of iterations to use for a load test. If the
     * value is not specified in the source file a default value of
     * 1 is used.
     */
    private String getNumberOfIterations() throws XDocletException {
        return getTagValue(XDocletTagSupport.FOR_METHOD,
                           LOAD_TEST,
                           NUMBER_OF_ITERATIONS,
                           null,
                           "1",
                           false,
                           false);
    }

    /**
     * Helper method that retrieves the max allowed time for a <code>
     * TimedTest</code> to execute. This is another example of a mandatory
     * attribute.
     *
     * @return the max allowed time for a test to execute.
     * @throws XDocletException
     */
    private String getMaxElapsedTime() throws XDocletException {
        return getTagValue(XDocletTagSupport.FOR_METHOD,
                           TIMED_TEST,
                           MAX_ELAPSED_TIME,
                           null,
                           null,
                           false,
                           true);
    }

    /**
     * Helper method that retrieves whether or not the <code>TimedTest</code>
     * should wait for the JUnit test method to complete before throwing
     * an exception if the time has elapsed. This method shows an example
     * of setting up two valid default values.
     *
     * @return 'true' if the timed test should wait for completion of the
     * JUnit test method before throwing an exception; 'false' if an
     * exception should be raised immediately.
     */
    private String getWaitForCompletion() throws XDocletException {
        return getTagValue(XDocletTagSupport.FOR_METHOD,
                           TIMED_TEST,
                           WAIT_FOR_COMPLETION,
                           "true,false",
                           "false",
                           false,
                           false);
    }
}