private void writeJavaClass() { try { File sourceFile = new File(getLocationName() + ".java"); verbose("Write collector class to '" + sourceFile.getAbsolutePath() + "'"); if (sourceFile.exists() && !sourceFile.delete()) { throw new IOException("could not delete " + sourceFile); } writer = new BufferedWriter(new FileWriter(sourceFile)); createClassHeader(); createSuiteMethod(); createClassFooter(); } catch (IOException e) { log(StringUtils.getStackTrace(e)); } finally { FileUtils.close(writer); } }
/** * Logging facade in INFO-mode. * @param message Log-message */ @Override public void log(String message) { getProject().log(LOG_PREFIX + " " + message, Project.MSG_INFO); }
/** * The task outside of this JUnitResultFormatter is the <junit> task. So all tests passed * and we could create the new java class. * @param event not used * @see org.apache.tools.ant.BuildListener#taskFinished(org.apache.tools.ant.BuildEvent) */ @Override public void taskFinished(BuildEvent event) { if (!failedTests.isEmpty()) { writeJavaClass(); } }
if (locationName == null) { String syspropValue = System.getProperty(MAGIC_PROPERTY_CLASS_LOCATION); String antpropValue = getProject().getProperty(MAGIC_PROPERTY_CLASS_LOCATION); verbose("System property '" + MAGIC_PROPERTY_CLASS_LOCATION + "' set, so use " + "its value '" + syspropValue + "' as location for collector class."); } else if (antpropValue != null) { locationName = antpropValue; verbose("Ant property '" + MAGIC_PROPERTY_CLASS_LOCATION + "' set, so use " + "its value '" + antpropValue + "' as location for collector class."); } else { locationName = DEFAULT_CLASS_LOCATION; verbose("System property '" + MAGIC_PROPERTY_CLASS_LOCATION + "' not set, so use " + "value as location for collector class: '" + DEFAULT_CLASS_LOCATION + "'"); File f = new File(getProject().getBaseDir(), locationName); locationName = f.getAbsolutePath(); verbose("Location file is relative (" + locationFile + ")" + " use absolute path instead (" + locationName + ")");
private void createClassHeader() throws IOException { String className = getLocationName().replace('\\', '/'); if (className.contains("/")) { className = className.substring(className.lastIndexOf('/') + 1); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss,SSS"); writer.write("// generated on: "); writer.write(sdf.format(new Date())); writer.newLine(); writer.write("import junit.framework.*;"); writer.newLine(); writer.write("public class "); writer.write(className); // If this class does not extend TC, Ant doesn't run these writer.write(" extends TestCase {"); writer.newLine(); // standard String-constructor writer.write(" public "); writer.write(className); writer.write("(String testname) {"); writer.newLine(); writer.write(" super(testname);"); writer.newLine(); writer.write(" }"); writer.newLine(); }
/** * This method is called by the Ant runtime by reflection. We use the project reference for * registration of this class as BuildListener. * * @param project * project reference */ @Override public void setProject(Project project) { // store project reference for logging super.setProject(project); // check if already registered // register if needed if (project.getBuildListeners().stream().noneMatch(FailureRecorder.class::isInstance)) { verbose("Register FailureRecorder (@" + this.hashCode() + ") as BuildListener"); project.addBuildListener(this); } }
/** * Logging facade in VERBOSE-mode. * @param message Log-message */ public void verbose(String message) { getProject().log(LOG_PREFIX + " " + message, Project.MSG_VERBOSE); }