
org.intermine.task.AcceptanceTestTask Maven / Gradle / Ivy
package org.intermine.task;
/*
* Copyright (C) 2002-2022 FlyMine
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. See the LICENSE file for more
* information or http://www.gnu.org/copyleft/lesser.html.
*
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.intermine.sql.Database;
import org.intermine.sql.DatabaseFactory;
/**
* A Task to run acceptance tests, configurable from a file.
*
* @author Kim Rutherford
*/
public class AcceptanceTestTask extends Task
{
private String database;
private File outputFile;
private File configFile;
/**
* Prefix of the ticket pages in Trac.
*/
public static final String TRAC_TICKET_URL_PREFIX = "http://intrac.flymine.org/ticket/";
/**
* Set the File to read configuration from
* @param configFile the config File
*/
public void setConfigFile(File configFile) {
this.configFile = configFile;
}
/**
* Set the File to write output to
* @param outputFile the output file
*/
public void setOutputFile(File outputFile) {
this.outputFile = outputFile;
}
/**
* Set the database alias
* @param database the database alias
*/
public void setDatabase(String database) {
this.database = database;
}
/**
* @throws BuildException if a problem occurs
* @see Task#execute
*/
@Override
public void execute() {
if (database == null) {
throw new BuildException("database attribute is not set");
}
if (configFile == null) {
throw new BuildException("configFile attribute is not set");
}
if (outputFile == null) {
throw new BuildException("outputFile attribute is not set");
}
try {
Database db = DatabaseFactory.getDatabase(database);
System.err .println("Processing configuration file: " + configFile.getCanonicalPath());
LineNumberReader reader = new LineNumberReader(new FileReader(configFile));
List testResults = runAllTests(db, reader);
FileWriter fw;
try {
fw = new FileWriter(outputFile);
} catch (IOException e) {
throw new BuildException("failed to open output file: " + outputFile, e);
}
PrintWriter pw = new PrintWriter(fw);
processResults(testResults, pw);
System.err .println("Printing results file: " + outputFile.getCanonicalPath());
try {
fw.close();
} catch (IOException e) {
throw new BuildException("couldn't close " + outputFile, e);
}
} catch (Exception e) {
throw new BuildException(e);
}
}
/**
* Run all the tests and return a List of AcceptanceTestResult objects.
* @param db the Database to run the queries against
* @param configReader the reader to get configuration information from.
* @return a List of AcceptanceTestResult objects
* @throws SQLException if there is a problem running the SQL query
* @throws IOException if there is a problem reading or parsing the config file
*/
protected List runAllTests(Database db, LineNumberReader configReader)
throws IOException, SQLException {
Connection con = db.getConnection();
List testResults = new ArrayList();
try {
AcceptanceTest test;
while ((test = readOneTestConfig(configReader)) != null) {
AcceptanceTestResult testResult = runTest(con, test);
testResults.add(testResult);
}
} catch (FileNotFoundException e) {
throw new BuildException("problem reading file - file not found: "
+ configFile, e);
}
return testResults;
}
/**
* Write a formatted HTML summary of the given AcceptanceTestResult objects to the PrintWriter.
* @param testResults a List of AcceptanceTestResult objects
* @param pw the PrintWriter
*/
protected void processResults(List testResults, PrintWriter pw) {
pw.println("");
pw.println("Acceptance Test Results ");
pw.println("");
pw.println("Acceptance Test Results
");
int testCount = 0;
int failingTestsCount = 0;
for (AcceptanceTestResult atr : testResults) {
if (!atr.isSuccessful()) {
failingTestsCount++;
}
testCount++;
}
pw.println("Total tests: " + testCount + "
");
if (testCount == 0) {
pw.println("");
return;
}
pw.println("Failing tests: " + failingTestsCount + "
");
pw.println("Percentage passed: "
+ 100 * (testCount - failingTestsCount) / testCount + "%
");
int count = 0;
if (failingTestsCount > 0) {
pw.println("
Failing tests:
");
pw.println("");
pw.println("
");
for (AcceptanceTestResult atr : testResults) {
if (!atr.isSuccessful()) {
pw.println("- ");
pw.println(atr.getTest().getSql());
pw.println("
(" + atr.getTest().getNote()
+ ")
");
}
count++;
}
pw.println("
");
}
pw.println("
");
count = 0;
for (AcceptanceTestResult atr : testResults) {
pw.println("Testing: "
+ atr.getTest().getSql() + "
");
pw.println("test type: " + atr.getTest().getType() + "
");
pw.println("(completed in " + atr.getTime() / 1000.0 + " seconds)
");
if (atr.getTest().getNote() != null) {
String hyperlinkedDescription = hyperLinkNote(atr.getTest().getNote());
pw.println("Description: " + hyperlinkedDescription + "
");
}
if (atr.isSuccessful()) {
pw.println("Result: successful
");
} else {
pw.println("Result: FAILED
");
}
if (atr.getException() == null) {
if ((atr.getTest().getType().equals(AcceptanceTest.NO_RESULTS_TEST)
|| atr.getTest().getType().equals(AcceptanceTest.RESULTS_REPORT))
&& atr.getResults().size() > 0) {
outputTable(pw, atr, atr.getColumnLabels(), atr.getResults());
pw.println("total rows: " + atr.getResultsCount() + "
");
}
} else {
pw.println("SQLException while executing SQL:
");
pw.println("");
atr.getException().printStackTrace(pw);
pw.println("
");
}
pw.println("
");
count++;
}
List allTrackerIds = new ArrayList();
for (AcceptanceTestResult atr : testResults) {
for (Integer id : atr.getTrackerMap().keySet()) {
// to avoid repeating trackers entries
if (allTrackerIds.contains(id)) {
continue;
} else {
allTrackerIds.add(id);
}
List> trackerRows = atr.getTrackerMap().get(id);
pw.println("Tracker entries for "
+ id + "
");
outputTable(pw, atr, null, trackerRows);
pw.println("
");
}
}
pw.println("");
pw.println("