From 7c98303882f7e57e39a2d3fbd9811bfa8c81cd89 Mon Sep 17 00:00:00 2001 From: Stephane Bailliez Date: Sun, 6 Jan 2002 18:03:19 +0000 Subject: [PATCH] Initial commit. Elements used to configure a Formatter. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270599 13f79535-47bb-0310-9956-ffa450edef68 --- .../optional/junit/FilterElement.java | 137 ++++++++++ .../optional/junit/FormatterElement.java | 258 ++++++++++++++++++ 2 files changed, 395 insertions(+) create mode 100644 proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/FilterElement.java create mode 100644 proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java diff --git a/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/FilterElement.java b/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/FilterElement.java new file mode 100644 index 000000000..51e79d156 --- /dev/null +++ b/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/FilterElement.java @@ -0,0 +1,137 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Ant", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.tools.ant.taskdefs.optional.junit; + +import java.lang.reflect.Constructor; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.taskdefs.optional.junit.formatter.FilterFormatter; +import org.apache.tools.ant.taskdefs.optional.junit.formatter.FilterStackFormatter; +import org.apache.tools.ant.taskdefs.optional.junit.formatter.Formatter; +import org.apache.tools.ant.types.EnumeratedAttribute; + +/** + * A filter element that can be use inside a FormatterElement to denote + * a filtering. Note that the filtering order correspond to the element + * order. The first element being the top filter, the last element + * being the bottom filter. + * + *
+ * 
+ * 
+ * 
+ * 
+ * + * @author Stephane Bailliez + */ +public class FilterElement { + + /** filter classname, is should inherit from FilterFormatter */ + private String classname; + + /** + * Called by introspection on type attribute. + * @see FilterAttribute + */ + public void setType(FilterAttribute fa) { + setClassName(fa.getClassName()); + } + + /** + * Called by introspection on classname attribute. + * It must inherit from FilterFormatter + * @see FilterFormatter + */ + public void setClassName(String name) { + classname = name; + } + + /** + * Wrap this filter around a given formatter. + * @throws BuildException if any error happens when creating this filter. + */ + public Formatter createFilterFormatter(Formatter f) throws BuildException { + try { + Class clazz = Class.forName(classname); + if (!FilterFormatter.class.isAssignableFrom(clazz)) { + throw new BuildException( clazz + " must be a FilterFormatter."); + } + Constructor ctor = clazz.getConstructor(new Class[]{Formatter.class}); + return (Formatter) ctor.newInstance(new Object[]{f}); + } catch (BuildException e) { + throw e; + } catch (Exception e) { + throw new BuildException(e); + } + } + + /** a predefined set of filters w/ their class mapping */ + public static class FilterAttribute extends EnumeratedAttribute { + /** the predefined alias for filters */ + private final static String[] VALUES = {"stack"}; + + /** the class corresponding to the alias (in the same order) */ + private final static String[] CLASSNAMES = {FilterStackFormatter.class.getName()}; + + public String[] getValues() { + return VALUES; + } + + /** get the classname matching the alias */ + public String getClassName() { + return CLASSNAMES[index]; + } + } + +} diff --git a/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java b/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java new file mode 100644 index 000000000..7a5c14fe5 --- /dev/null +++ b/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java @@ -0,0 +1,258 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Ant", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.tools.ant.taskdefs.optional.junit; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.util.StringTokenizer; +import java.util.Vector; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.taskdefs.optional.junit.formatter.BriefFormatter; +import org.apache.tools.ant.taskdefs.optional.junit.formatter.Formatter; +import org.apache.tools.ant.taskdefs.optional.junit.formatter.KeepAliveOutputStream; +import org.apache.tools.ant.taskdefs.optional.junit.formatter.XMLFormatter; +import org.apache.tools.ant.types.EnumeratedAttribute; + +/** + * An element representing a Formatter + * + *
+ * 
+ * 
+ * 
+ * 
+ * 
+ * 
+ * + * @author Stephane Bailliez + * + * @see JUnitTask, + * @see Formatter + */ +public class FormatterElement { + + private OutputStream out = new KeepAliveOutputStream(System.out); + private String classname; + + /** + * @fixme we can remove this and use a specific attribute + * to denote a filepathname and stdout as a reserved word. + */ + private String extension; + + /** are we using a file ? */ + private File outFile; + private boolean useFile = true; + + /** the filters to apply to this formatter */ + private Vector filters = new Vector(); + + /** + *

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. + */ + public void setType(TypeAttribute type) { + setClassname(type.getClassName()); + setExtension(type.getExtension()); + } + + /** + *

Set name of class to be used as the formatter. + * + *

This class must implement Formatter + */ + public void setClassname(String classname) { + this.classname = classname; + } + + /** + * Get name of class to be used as the formatter. + */ + public String getClassname() { + return classname; + } + + public void setExtension(String ext) { + this.extension = ext; + } + + public String getExtension() { + return extension; + } + + /** + * Setting a comma separated list of filters in the specified order. + * @see #addFilter(FilterAttribute) + * @see FilterAttribute + */ + public void setFilters(String filters) { + StringTokenizer st = new StringTokenizer(filters, ","); + while (st.hasMoreTokens()) { + FilterElement fe = new FilterElement(); + FilterElement.FilterAttribute fa = new FilterElement.FilterAttribute(); + fa.setValue(st.nextToken()); + fe.setType(fa); + addFilter(fe); + } + } + + /** + * Add a filter to this formatter. + */ + public void addFilter(FilterElement fe) { + filters.addElement(fe); + } + + /** + *

Set the file which the formatte should log to. + * + *

Note that logging to file must be enabled . + */ + void setOutfile(File out) { + this.outFile = out; + } + + /** + *

Set output stream for formatter to use. + * + *

Defaults to standard out. + */ + public void setOutput(OutputStream out) { + this.out = out; + } + + /** + * Set whether the formatter should log to file. + */ + public void setUseFile(boolean useFile) { + this.useFile = useFile; + } + + /** + * Get whether the formatter should log to file. + */ + boolean getUseFile() { + return useFile; + } + + /** + * create the Formatter corresponding to this element. + */ + protected Formatter createFormatter() throws BuildException { + if (classname == null) { + throw new BuildException("you must specify type or classname"); + } + Formatter f = null; + try { + Class clazz = Class.forName(classname); + if (!Formatter.class.isAssignableFrom(clazz)) { + throw new BuildException(clazz + " is not a JUnitResultFormatter"); + } + f = (Formatter) clazz.newInstance(); + + // create the stream if necessary + if (useFile && outFile != null) { + out = new FileOutputStream(outFile); + } + } catch (BuildException e) { + throw e; + } catch (Exception e) { + throw new BuildException(e); + } + + f.setOutput(out); + + // wrap filters in the reverse order: first = top, last = bottom. + for (int i = filters.size() - 1; i >= 0; i--) { + FilterElement fe = (FilterElement) filters.elementAt(i); + f = fe.createFilterFormatter(f); + } + + return f; + } + + /** + *

Enumerated attribute with the values "plain", "xml" and "brief". + *

Use to enumerate options for type attribute. + */ + public static class TypeAttribute extends EnumeratedAttribute { + private final static String[] VALUES = {"plain", "xml", "brief"}; + private final static String[] CLASSNAMES = {"xxx", XMLFormatter.class.getName(), BriefFormatter.class.getName()}; + private final static String[] EXTENSIONS = {".txt", ".xml", ".txt"}; + + public String[] getValues() { + return VALUES; + } + + public String getClassName() { + return CLASSNAMES[index]; + } + + public String getExtension() { + return EXTENSIONS[index]; + } + } + +} +