formatterelement.java

来自「MoMEUnit是一个单元测试的J2ME的应用程序xUnit架构实例。这是来自J」· Java 代码 · 共 282 行

JAVA
282
字号
package org.momeunit.ant.taskdefs;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.OutputStream;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Task;import org.momeunit.ant.core.Utility;import org.momeunit.ant.formatter.UnitResultFormatter;/** * MoMEUnit nested tag intended to specify and configure formatters used to * process results of tests run. Contains method * {@link #createFormatter(File, String, Task, ClassLoader)} for creating the * configured formatter. *  * @author Sergio Morozov * @version 1.1.2 */public class FormatterElement extends ShouldUseElement{  /**   * Type of formatter.   */  private FormatterType type = null;  /**   * Classname of formatter.   */  private String classname = null;  /**   * Does formmater send output to file.   */  private boolean useFile = true;  /**   * Extension of report file.   */  private String ext = null;  /**   * Does formatter filter stacktrace.   */  private boolean filterTrace = true;  /**   * Name of task owning this tag.   */  private Task task = null;  /**   * Encoding used in writing report files.   */  private String encoding = null;  /**   * Sets owning task.   *    * @param task   *          the the owning task to set.   * @since 1.1   */  public void setTask(Task task)  {    this.task = task;  }  /**   * Returns owning task.   *    * @return the owning task.   * @since 1.1   */  public Task getTask()  {    return this.task;  }  /**   * Sets encoding of report file to create.   *    * @param encoding   *          the encoding to set.   * @since 1.1   */  public void setEncoding(String encoding)  {    Utility.assertNotEmpty(encoding, "encoding", "formatter");    this.encoding = encoding;  }  /**   * Returns encoding of report file to create.   *    * @return the encoding of report file to create.   * @since 1.1.2   */  public String getEncoding()  {    return this.encoding;  }  /**   * Sets flag that indicates whether to send output to external file or ant log   * system.   *    * @param useFile   *          the useFile to set.   * @since 1.1   */  public void setUseFile(boolean useFile)  {    this.useFile = useFile;  }  /**   * Sets the extension of report file.   *    * @param ext   *          the ext to set   * @since 1.1   */  public void setExtension(String ext)  {    if (ext != null)    {      this.ext = ext.trim();      if (this.ext.length() > 0 && this.ext.charAt(0) != '.') this.ext = '.' + this.ext;    }  }  /**   * Returns extension of report file to create.   *    * @return explicitly set extension of report file or default one based on   *         type or classname or an empty string, if not found.   * @since 1.1   */  public String getExtension()  {    if (this.ext == null && this.type != null) this.ext = this.type        .getDefaultExtension();    if (this.ext == null && this.classname != null) this.ext = FormatterType        .getDefaultExtension(this.classname);    return this.ext != null ? this.ext : "";  }  /**   * Sets classname of formatter.   *    * @param classname   *          the classname to set.   * @since 1.1   */  public void setClassname(String classname)  {    this.classname = classname;  }  /**   * Sets type of formatter.   *    * @param type   *          the type to set   * @since 1.1   */  public void setType(String type)  {    Utility.assertNotEmpty(type, "type", "formatter");    this.type = new FormatterType(type);  }  /**   * Returns formatter instance to use.   *    * @param loader   *          ClassLoader to use for loading classes.   * @return class of formatter.   */  private UnitResultFormatter createFormatter(ClassLoader loader)  {    UnitResultFormatter res = null;    if (this.classname == null)    {      if (this.type == null) throw new BuildException(          "Either type or classname attribute of <formatter> must be set.");      this.classname = this.type.getFormatterClassName();    }    try    {      Class fClass = null;      if (loader != null) fClass = loader.loadClass(this.classname);      else fClass = Class.forName(this.classname);      res = (UnitResultFormatter) fClass.newInstance();    } catch (ClassNotFoundException e)    {      throw new BuildException("formatter class " + this.classname          + " not found.");    } catch (InstantiationException e)    {      throw new BuildException("Error instantiation formatter class "          + this.classname, e);    } catch (IllegalAccessException e)    {      throw new BuildException("Error accessing formatter class "          + this.classname, e);    } catch (ClassCastException e)    {      throw new BuildException("Class " + this.classname + " doesn't extend "          + UnitResultFormatter.class.getName());    }    return res;  }  /**   * Returns configured formatter.   *    * @param dir   *          directory where to create reports.   * @param fileName   *          name of report file.   * @param owner   *          task owning this tag.   * @param loader   *          ClassLoader intended to load formatter class.   * @return configured formatter.   * @since 1.1   */  public UnitResultFormatter createFormatter(File dir, String fileName,      Task owner, ClassLoader loader)  {    UnitResultFormatter res = this.createFormatter(loader);    res.setOutput((this.useFile) ? getFileOutput(dir, fileName) : System.out);    res.setFilterTrace(this.filterTrace);    if (this.encoding != null) res.setEncoding(this.encoding);    return res;  }  /**   * Returns {@link OutpuStream} of report file.   *    * @param testSuiteName   *          name of test suite the formatter wil be sued with.   * @param dir   *          directory where to create report file.   * @param fileName   *          name of report file.   * @return {@link OutpuStream} of report file.   */  private OutputStream getFileOutput(File dir, String fileName)  {    OutputStream out = null;    File file = new File(dir, fileName + this.getExtension());    try    {      out = new FileOutputStream(file, false);    } catch (FileNotFoundException e)    {      throw new BuildException("Error opening file " + file.getAbsolutePath());    }    return out;  }  /**   * Sets filtertrace flag.   *    * @param filterTrace   *          the filterTrace to set.   * @since 1.1   */  public void setFilterTrace(boolean filterTrace)  {    this.filterTrace = filterTrace;  }}

⌨️ 快捷键说明

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