FileDocCategorySizeDatePackage
FormatterElement.javaAPI DocApache Ant 1.709416Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.taskdefs.optional.junit

FormatterElement

public class FormatterElement extends Object

A wrapper for the implementations of JUnitResultFormatter. In particular, used as a nested <formatter> element in a <junit> task.

For example,

<junit printsummary="no" haltonfailure="yes" fork="false">
<formatter type="plain" usefile="false" />
<test name="org.apache.ecs.InternationalCharTest" />
</junit>
adds a plain type implementation (PlainJUnitResultFormatter) to display the results of the test.

Either the type or the classname attribute must be set.

see
JUnitTask
see
XMLJUnitResultFormatter
see
BriefJUnitResultFormatter
see
PlainJUnitResultFormatter
see
JUnitResultFormatter

Fields Summary
private String
classname
private String
extension
private OutputStream
out
private File
outFile
private boolean
useFile
private String
ifProperty
private String
unlessProperty
public static final String
XML_FORMATTER_CLASS_NAME
xml formatter class
public static final String
BRIEF_FORMATTER_CLASS_NAME
brief formatter class
public static final String
PLAIN_FORMATTER_CLASS_NAME
plain formatter class
Constructors Summary
Methods Summary
JUnitTaskMirror.JUnitResultFormatterMirrorcreateFormatter()

since
Ant 1.2

        return createFormatter(null);
    
JUnitTaskMirror.JUnitResultFormatterMirrorcreateFormatter(java.lang.ClassLoader loader)

since
Ant 1.6


        if (classname == null) {
            throw new BuildException("you must specify type or classname");
        }
        //although this code appears to duplicate that of ClasspathUtils.newInstance,
        //we cannot use that because this formatter may run in a forked process,
        //without that class.
        Class f = null;
        try {
            if (loader == null) {
                f = Class.forName(classname);
            } else {
                f = Class.forName(classname, true, loader);
            }
        } catch (ClassNotFoundException e) {
            throw new BuildException(
                "Using loader " + loader + " on class " + classname
                + ": " + e, e);
        } catch (NoClassDefFoundError e) {
            throw new BuildException(
                "Using loader " + loader + " on class " + classname
                + ": " + e, e);
        }

        Object o = null;
        try {
            o = f.newInstance();
        } catch (InstantiationException e) {
            throw new BuildException(e);
        } catch (IllegalAccessException e) {
            throw new BuildException(e);
        }

        if (!(o instanceof JUnitTaskMirror.JUnitResultFormatterMirror)) {
            throw new BuildException(classname
                + " is not a JUnitResultFormatter");
        }
        JUnitTaskMirror.JUnitResultFormatterMirror r =
            (JUnitTaskMirror.JUnitResultFormatterMirror) o;
        if (useFile && outFile != null) {
            try {
                out = new BufferedOutputStream(new FileOutputStream(outFile));
            } catch (java.io.IOException e) {
                throw new BuildException("Unable to open file " + outFile, e);
            }
        }
        r.setOutput(out);
        return r;
    
public java.lang.StringgetClassname()
Get name of class to be used as the formatter.

return
the name of the class.

        return classname;
    
public java.lang.StringgetExtension()
Get the extension used for the report file.

return
the extension.

        return extension;
    
booleangetUseFile()
Get whether the formatter should log to file.

        return useFile;
    
public voidsetClassname(java.lang.String classname)

Set name of class to be used as the formatter.

This class must implement JUnitResultFormatter

param
classname the name of the formatter class.

        this.classname = classname;
        if (XML_FORMATTER_CLASS_NAME.equals(classname)) {
           setExtension(".xml");
        } else if (PLAIN_FORMATTER_CLASS_NAME.equals(classname)) {
           setExtension(".txt");
        } else if (BRIEF_FORMATTER_CLASS_NAME.equals(classname)) {
           setExtension(".txt");
        }
    
public voidsetExtension(java.lang.String ext)
Set the extension to use for the report file.

param
ext the extension to use.

        this.extension = ext;
    
public voidsetIf(java.lang.String ifProperty)
Set whether this formatter should be used. It will be used if the property has been set, otherwise it won't.

param
ifProperty name of property

        this.ifProperty = ifProperty;
    
voidsetOutfile(java.io.File out)

Set the file which the formatte should log to.

Note that logging to file must be enabled .

        this.outFile = out;
    
public voidsetOutput(java.io.OutputStream out)

Set output stream for formatter to use.

Defaults to standard out.

param
out the output stream to use.

        this.out = out;
    
public voidsetType(org.apache.tools.ant.taskdefs.optional.junit.FormatterElement$TypeAttribute type)

Quick way to use a standard formatter.

At the moment, there are three supported standard formatters.

  • The xml type uses a XMLJUnitResultFormatter.
  • The brief type uses a BriefJUnitResultFormatter.
  • The plain type (the default) uses a PlainJUnitResultFormatter.

Sets classname attribute - so you can't use that attribute if you use this one.

param
type the enumerated value to use.


                                                                           
        
        if ("xml".equals(type.getValue())) {
            setClassname(XML_FORMATTER_CLASS_NAME);
        } else {
            if ("brief".equals(type.getValue())) {
                setClassname(BRIEF_FORMATTER_CLASS_NAME);
            } else { // must be plain, ensured by TypeAttribute
                setClassname(PLAIN_FORMATTER_CLASS_NAME);
            }
        }
    
public voidsetUnless(java.lang.String unlessProperty)
Set whether this formatter should NOT be used. It will not be used if the property has been set, orthwise it will be used.

param
unlessProperty name of property

        this.unlessProperty = unlessProperty;
    
public voidsetUseFile(boolean useFile)
Set whether the formatter should log to file.

param
useFile if true use a file, if false send to standard out.

        this.useFile = useFile;
    
public booleanshouldUse(org.apache.tools.ant.Task t)
Ensures that the selector passes the conditions placed on it with if and unless properties.

param
t the task the this formatter is used in.
return
true if the formatter should be used.

        if (ifProperty != null && t.getProject().getProperty(ifProperty) == null) {
            return false;
        } else if (unlessProperty != null
                    && t.getProject().getProperty(unlessProperty) != null) {
            return false;
        }

        return true;