1 18 19 package org.apache.tools.ant.taskdefs.optional.junit; 20 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.OutputStream ; 24 import java.io.BufferedOutputStream ; 25 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.Task; 28 import org.apache.tools.ant.types.EnumeratedAttribute; 29 30 52 public class FormatterElement { 53 54 private String classname; 55 private String extension; 56 private OutputStream out = System.out; 57 private File outFile; 58 private boolean useFile = true; 59 private String ifProperty; 60 private String unlessProperty; 61 62 63 public static final String XML_FORMATTER_CLASS_NAME = 64 "org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter"; 65 66 public static final String BRIEF_FORMATTER_CLASS_NAME = 67 "org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter"; 68 69 public static final String PLAIN_FORMATTER_CLASS_NAME = 70 "org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter"; 71 72 86 public void setType(TypeAttribute type) { 87 if ("xml".equals(type.getValue())) { 88 setClassname(XML_FORMATTER_CLASS_NAME); 89 } else { 90 if ("brief".equals(type.getValue())) { 91 setClassname(BRIEF_FORMATTER_CLASS_NAME); 92 } else { setClassname(PLAIN_FORMATTER_CLASS_NAME); 94 } 95 } 96 } 97 98 104 public void setClassname(String classname) { 105 this.classname = classname; 106 if (XML_FORMATTER_CLASS_NAME.equals(classname)) { 107 setExtension(".xml"); 108 } else if (PLAIN_FORMATTER_CLASS_NAME.equals(classname)) { 109 setExtension(".txt"); 110 } else if (BRIEF_FORMATTER_CLASS_NAME.equals(classname)) { 111 setExtension(".txt"); 112 } 113 } 114 115 119 public String getClassname() { 120 return classname; 121 } 122 123 127 public void setExtension(String ext) { 128 this.extension = ext; 129 } 130 131 135 public String getExtension() { 136 return extension; 137 } 138 139 144 void setOutfile(File out) { 145 this.outFile = out; 146 } 147 148 154 public void setOutput(OutputStream out) { 155 this.out = out; 156 } 157 158 163 public void setUseFile(boolean useFile) { 164 this.useFile = useFile; 165 } 166 167 170 boolean getUseFile() { 171 return useFile; 172 } 173 174 179 public void setIf(String ifProperty) { 180 this.ifProperty = ifProperty; 181 } 182 183 189 public void setUnless(String unlessProperty) { 190 this.unlessProperty = unlessProperty; 191 } 192 193 199 public boolean shouldUse(Task t) { 200 if (ifProperty != null && t.getProject().getProperty(ifProperty) == null) { 201 return false; 202 } else if (unlessProperty != null 203 && t.getProject().getProperty(unlessProperty) != null) { 204 return false; 205 } 206 207 return true; 208 } 209 210 213 JUnitTaskMirror.JUnitResultFormatterMirror createFormatter() throws BuildException { 214 return createFormatter(null); 215 } 216 217 220 JUnitTaskMirror.JUnitResultFormatterMirror createFormatter(ClassLoader loader) 221 throws BuildException { 222 223 if (classname == null) { 224 throw new BuildException("you must specify type or classname"); 225 } 226 Class f = null; 230 try { 231 if (loader == null) { 232 f = Class.forName(classname); 233 } else { 234 f = Class.forName(classname, true, loader); 235 } 236 } catch (ClassNotFoundException e) { 237 throw new BuildException( 238 "Using loader " + loader + " on class " + classname 239 + ": " + e, e); 240 } catch (NoClassDefFoundError e) { 241 throw new BuildException( 242 "Using loader " + loader + " on class " + classname 243 + ": " + e, e); 244 } 245 246 Object o = null; 247 try { 248 o = f.newInstance(); 249 } catch (InstantiationException e) { 250 throw new BuildException(e); 251 } catch (IllegalAccessException e) { 252 throw new BuildException(e); 253 } 254 255 if (!(o instanceof JUnitTaskMirror.JUnitResultFormatterMirror)) { 256 throw new BuildException(classname 257 + " is not a JUnitResultFormatter"); 258 } 259 JUnitTaskMirror.JUnitResultFormatterMirror r = 260 (JUnitTaskMirror.JUnitResultFormatterMirror) o; 261 if (useFile && outFile != null) { 262 try { 263 out = new BufferedOutputStream (new FileOutputStream (outFile)); 264 } catch (java.io.IOException e) { 265 throw new BuildException("Unable to open file " + outFile, e); 266 } 267 } 268 r.setOutput(out); 269 return r; 270 } 271 272 277 public static class TypeAttribute extends EnumeratedAttribute { 278 279 public String [] getValues() { 280 return new String [] {"plain", "xml", "brief"}; 281 } 282 } 283 } 284 | Popular Tags |