From b04cf27e254105c0df5a5d4ce6ffaadda2d7fdb4 Mon Sep 17 00:00:00 2001 From: Tim Jacomb <21194782+timja@users.noreply.github.com> Date: Thu, 5 Aug 2021 08:02:03 +0100 Subject: [PATCH] Extract anonymous class to inner (#110) --- .../database/DatabaseTestResultStorage.java | 947 +++++++++--------- 1 file changed, 480 insertions(+), 467 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/junit/storage/database/DatabaseTestResultStorage.java b/src/main/java/io/jenkins/plugins/junit/storage/database/DatabaseTestResultStorage.java index b8d50f3..321c1e0 100644 --- a/src/main/java/io/jenkins/plugins/junit/storage/database/DatabaseTestResultStorage.java +++ b/src/main/java/io/jenkins/plugins/junit/storage/database/DatabaseTestResultStorage.java @@ -79,473 +79,7 @@ private interface Querier { T run(Connection connection) throws SQLException; } @Override public TestResultImpl load(String job, int build) { - return new TestResultImpl() { - private T query(Querier querier) { - try { - Connection connection = getConnectionSupplier().connection(); - return querier.run(connection); - } catch (SQLException x) { - throw new RuntimeException(x); - } - } - private int getCaseCount(String and) { - return query(connection -> { - try (PreparedStatement statement = connection.prepareStatement("SELECT COUNT(*) FROM caseResults WHERE job = ? AND build = ?" + and)) { - statement.setString(1, job); - statement.setInt(2, build); - try (ResultSet result = statement.executeQuery()) { - result.next(); - int anInt = result.getInt(1); - return anInt; - } - } - }); - } - - private List retrieveCaseResult(String whereCondition) { - return query(connection -> { - try (PreparedStatement statement = connection.prepareStatement("SELECT suite, package, testname, classname, errordetails, skipped, duration, stdout, stderr, stacktrace FROM caseResults WHERE job = ? AND build = ? AND " + whereCondition)) { - statement.setString(1, job); - statement.setInt(2, build); - try (ResultSet result = statement.executeQuery()) { - - List results = new ArrayList<>(); - Map classResults = new HashMap<>(); - TestResult parent = new TestResult(this); - while (result.next()) { - String testName = result.getString("testname"); - String packageName = result.getString("package"); - String errorDetails = result.getString("errordetails"); - String suite = result.getString("suite"); - String className = result.getString("classname"); - String skipped = result.getString("skipped"); - String stdout = result.getString("stdout"); - String stderr = result.getString("stderr"); - String stacktrace = result.getString("stacktrace"); - float duration = result.getFloat("duration"); - - SuiteResult suiteResult = new SuiteResult(suite, null, null, null); - suiteResult.setParent(parent); - CaseResult caseResult = new CaseResult(suiteResult, className, testName, errorDetails, skipped, duration, stdout, stderr, stacktrace); - ClassResult classResult = classResults.get(className); - if (classResult == null) { - classResult = new ClassResult(new PackageResult(new TestResult(this), packageName), className); - } - classResult.add(caseResult); - caseResult.setClass(classResult); - classResults.put(className, classResult); - results.add(caseResult); - } - classResults.values().forEach(ClassResult::tally); - return results; - } - } - }); - } - - @Override - public List getAllPackageResults() { - return query(connection -> { - try (PreparedStatement statement = connection.prepareStatement("SELECT suite, testname, package, classname, errordetails, skipped, duration, stdout, stderr, stacktrace FROM caseResults WHERE job = ? AND build = ?")) { - statement.setString(1, job); - statement.setInt(2, build); - try (ResultSet result = statement.executeQuery()) { - - Map results = new HashMap<>(); - Map classResults = new HashMap<>(); - TestResult parent = new TestResult(this); - while (result.next()) { - String testName = result.getString("testname"); - String packageName = result.getString("package"); - String errorDetails = result.getString("errordetails"); - String suite = result.getString("suite"); - String className = result.getString("classname"); - String skipped = result.getString("skipped"); - String stdout = result.getString("stdout"); - String stderr = result.getString("stderr"); - String stacktrace = result.getString("stacktrace"); - float duration = result.getFloat("duration"); - - SuiteResult suiteResult = new SuiteResult(suite, null, null, null); - suiteResult.setParent(parent); - CaseResult caseResult = new CaseResult(suiteResult, className, testName, errorDetails, skipped, duration, stdout, stderr, stacktrace); - PackageResult packageResult = results.get(packageName); - if (packageResult == null) { - packageResult = new PackageResult(parent, packageName); - } - ClassResult classResult = classResults.get(className); - if (classResult == null) { - classResult = new ClassResult(new PackageResult(parent, packageName), className); - } - caseResult.setClass(classResult); - classResult.add(caseResult); - - classResults.put(className, classResult); - packageResult.add(caseResult); - - results.put(packageName, packageResult); - } - classResults.values().forEach(ClassResult::tally); - final List resultList = new ArrayList<>(results.values()); - resultList.forEach((PackageResult::tally)); - resultList.sort(Comparator.comparing(PackageResult::getName, String::compareTo)); - return resultList; - } - } - }); - } - - @Override - public List getTrendTestResultSummary() { - return query(connection -> { - try (PreparedStatement statement = connection.prepareStatement("SELECT build, sum(case when errorDetails is not null then 1 else 0 end) as failCount, sum(case when skipped is not null then 1 else 0 end) as skipCount, sum(case when errorDetails is null and skipped is null then 1 else 0 end) as passCount FROM caseResults" + " WHERE job = ? group by build order by build;")) { - statement.setString(1, job); - try (ResultSet result = statement.executeQuery()) { - - List trendTestResultSummaries = new ArrayList<>(); - while (result.next()) { - int buildNumber = result.getInt("build"); - int passed = result.getInt("passCount"); - int failed = result.getInt("failCount"); - int skipped = result.getInt("skipCount"); - int total = passed + failed + skipped; - - trendTestResultSummaries.add(new TrendTestResultSummary(buildNumber, new TestResultSummary(failed, skipped, passed, total))); - } - return trendTestResultSummaries; - } - } - }); - } - - @Override - public List getTestDurationResultSummary() { - return query(connection -> { - try (PreparedStatement statement = connection.prepareStatement("SELECT build, sum(duration) as duration FROM caseResults" + " WHERE job = ? group by build order by build;")) { - statement.setString(1, job); - try (ResultSet result = statement.executeQuery()) { - - List testDurationResultSummaries = new ArrayList<>(); - while (result.next()) { - int buildNumber = result.getInt("build"); - int duration = result.getInt("duration"); - - testDurationResultSummaries.add(new TestDurationResultSummary(buildNumber, duration)); - } - return testDurationResultSummaries; - } - } - }); - } - - @Override - public int getCountOfBuildsWithTestResults() { - return query(connection -> { - try (PreparedStatement statement = connection.prepareStatement("SELECT COUNT(DISTINCT build) as count FROM caseResults WHERE job = ?;")) { - statement.setString(1, job); - try (ResultSet result = statement.executeQuery()) { - result.next(); - int count = result.getInt("count"); - return count; - } - } - }); - } - - @Override - public PackageResult getPackageResult(String packageName) { - return query(connection -> { - try (PreparedStatement statement = connection.prepareStatement("SELECT suite, testname, classname, errordetails, skipped, duration, stdout, stderr, stacktrace FROM caseResults WHERE job = ? AND build = ? AND package = ?")) { - statement.setString(1, job); - statement.setInt(2, build); - statement.setString(3, packageName); - try (ResultSet result = statement.executeQuery()) { - - PackageResult packageResult = new PackageResult(new TestResult(this), packageName); - Map classResults = new HashMap<>(); - while (result.next()) { - String testName = result.getString("testname"); - String errorDetails = result.getString("errordetails"); - String suite = result.getString("suite"); - String className = result.getString("classname"); - String skipped = result.getString("skipped"); - String stdout = result.getString("stdout"); - String stderr = result.getString("stderr"); - String stacktrace = result.getString("stacktrace"); - float duration = result.getFloat("duration"); - - SuiteResult suiteResult = new SuiteResult(suite, null, null, null); - suiteResult.setParent(new TestResult(this)); - CaseResult caseResult = new CaseResult(suiteResult, className, testName, errorDetails, skipped, duration, stdout, stderr, stacktrace); - - ClassResult classResult = classResults.get(className); - if (classResult == null) { - classResult = new ClassResult(packageResult, className); - } - classResult.add(caseResult); - classResults.put(className, classResult); - caseResult.setClass(classResult); - - packageResult.add(caseResult); - } - classResults.values().forEach(ClassResult::tally); - packageResult.tally(); - return packageResult; - } - } - }); - - } - - @Override - public Run getFailedSinceRun(CaseResult caseResult) { - return query(connection -> { - int lastPassingBuildNumber; - Job theJob = Objects.requireNonNull(Jenkins.get().getItemByFullName(job, Job.class)); - try (PreparedStatement statement = connection.prepareStatement( - "SELECT build " + - "FROM caseResults " + - "WHERE job = ? " + - "AND build < ? " + - "AND suite = ? " + - "AND package = ? " + - "AND classname = ? " + - "AND testname = ? " + - "AND errordetails IS NULL " + - "ORDER BY BUILD DESC " + - "LIMIT 1" - )) { - statement.setString(1, job); - statement.setInt(2, build); - statement.setString(3, caseResult.getSuiteResult().getName()); - statement.setString(4, caseResult.getPackageName()); - statement.setString(5, caseResult.getClassName()); - statement.setString(6, caseResult.getName()); - try (ResultSet result = statement.executeQuery()) { - boolean hasPassed = result.next(); - if (!hasPassed) { - return theJob.getBuildByNumber(1); - } - - lastPassingBuildNumber = result.getInt("build"); - } - } - try (PreparedStatement statement = connection.prepareStatement( - "SELECT build " + - "FROM caseResults " + - "WHERE job = ? " + - "AND build > ? " + - "AND suite = ? " + - "AND package = ? " + - "AND classname = ? " + - "AND testname = ? " + - "AND errordetails is NOT NULL " + - "ORDER BY BUILD ASC " + - "LIMIT 1" - ) - ) { - statement.setString(1, job); - statement.setInt(2, lastPassingBuildNumber); - statement.setString(3, caseResult.getSuiteResult().getName()); - statement.setString(4, caseResult.getPackageName()); - statement.setString(5, caseResult.getClassName()); - statement.setString(6, caseResult.getName()); - - try (ResultSet result = statement.executeQuery()) { - result.next(); - - int firstFailingBuildAfterPassing = result.getInt("build"); - return theJob.getBuildByNumber(firstFailingBuildAfterPassing); - } - } - }); - - } - - @Override - public String getJobName() { - return job; - } - - @Override - public int getBuild() { - return build; - } - - @Override - public List getFailedTestsByPackage(String packageName) { - return getByPackage(packageName, "AND errorDetails IS NOT NULL"); - } - - private List getByPackage(String packageName, String filter) { - return query(connection -> { - try (PreparedStatement statement = connection.prepareStatement("SELECT suite, testname, classname, errordetails, duration, skipped, stdout, stderr, stacktrace FROM caseResults WHERE job = ? AND build = ? AND package = ? " + filter)) { - statement.setString(1, job); - statement.setInt(2, build); - statement.setString(3, packageName); - try (ResultSet result = statement.executeQuery()) { - - List results = new ArrayList<>(); - while (result.next()) { - String testName = result.getString("testname"); - String errorDetails = result.getString("errordetails"); - String suite = result.getString("suite"); - String className = result.getString("classname"); - String skipped = result.getString("skipped"); - String stdout = result.getString("stdout"); - String stderr = result.getString("stderr"); - String stacktrace = result.getString("stacktrace"); - float duration = result.getFloat("duration"); - - SuiteResult suiteResult = new SuiteResult(suite, null, null, null); - suiteResult.setParent(new TestResult(this)); - CaseResult caseResult = new CaseResult(suiteResult, className, testName, errorDetails, skipped, duration, stdout, stderr, stacktrace); - - PackageResult packageResult = new PackageResult(new TestResult(this), packageName); - ClassResult classResult = new ClassResult(packageResult, className); - classResult.add(caseResult); - packageResult.add(caseResult); - packageResult.tally(); - caseResult.tally(); - caseResult.setClass(classResult); - - results.add(caseResult); - } - return results; - } - } - }); - } - - - private List getCaseResults(String column) { - return retrieveCaseResult(column + " IS NOT NULL"); - } - - @Override - public SuiteResult getSuite(String name) { - return query(connection -> { - try (PreparedStatement statement = connection.prepareStatement("SELECT testname, package, classname, errordetails, skipped, duration, stdout, stderr, stacktrace FROM caseResults WHERE job = ? AND build = ? AND suite = ?")) { - statement.setString(1, job); - statement.setInt(2, build); - statement.setString(3, name); - try (ResultSet result = statement.executeQuery()) { - SuiteResult suiteResult = new SuiteResult(name, null, null, null); - TestResult parent = new TestResult(this); - while (result.next()) { - String resultTestName = result.getString("testname"); - String errorDetails = result.getString("errordetails"); - String packageName = result.getString("package"); - String className = result.getString("classname"); - String skipped = result.getString("skipped"); - String stdout = result.getString("stdout"); - String stderr = result.getString("stderr"); - String stacktrace = result.getString("stacktrace"); - float duration = result.getFloat("duration"); - - suiteResult.setParent(parent); - CaseResult caseResult = new CaseResult(suiteResult, className, resultTestName, errorDetails, skipped, duration, stdout, stderr, stacktrace); - final PackageResult packageResult = new PackageResult(parent, packageName); - packageResult.add(caseResult); - ClassResult classResult = new ClassResult(packageResult, className); - classResult.add(caseResult); - caseResult.setClass(classResult); - suiteResult.addCase(caseResult); - } - return suiteResult; - } - } - }); - - } - - @Override - public float getTotalTestDuration() { - return query(connection -> { - try (PreparedStatement statement = connection.prepareStatement("SELECT sum(duration) as duration FROM caseResults" + " WHERE job = ? and build = ?;")) { - statement.setString(1, job); - statement.setInt(2, build); - try (ResultSet result = statement.executeQuery()) { - if (result.next()) { - return result.getFloat("duration"); - } - return 0f; - } - } - }); - } - - @Override public int getFailCount() { - int caseCount = getCaseCount(" AND errorDetails IS NOT NULL"); - return caseCount; - } - @Override public int getSkipCount() { - int caseCount = getCaseCount(" AND skipped IS NOT NULL"); - return caseCount; - } - @Override public int getPassCount() { - int caseCount = getCaseCount(" AND errorDetails IS NULL AND skipped IS NULL"); - return caseCount; - } - @Override public int getTotalCount() { - int caseCount = getCaseCount(""); - return caseCount; - } - - @Override - public List getFailedTests() { - List errordetails = getCaseResults("errordetails"); - return errordetails; - } - - @Override - public List getSkippedTests() { - List errordetails = getCaseResults("skipped"); - return errordetails; - } - - @Override - public List getSkippedTestsByPackage(String packageName) { - return getByPackage(packageName, "AND skipped IS NOT NULL"); - } - - @Override - public List getPassedTests() { - List errordetails = retrieveCaseResult("errordetails IS NULL AND skipped IS NULL"); - return errordetails; - } - - @Override - public List getPassedTestsByPackage(String packageName) { - return getByPackage(packageName, "AND errordetails IS NULL AND skipped IS NULL"); - } - - @Override - @CheckForNull - public TestResult getPreviousResult() { - return query(connection -> { - try (PreparedStatement statement = connection.prepareStatement("SELECT build FROM caseResults WHERE job = ? AND build < ? ORDER BY build DESC LIMIT 1")) { - statement.setString(1, job); - statement.setInt(2, build); - try (ResultSet result = statement.executeQuery()) { - - if (result.next()) { - int previousBuild = result.getInt("build"); - - return new TestResult(load(job, previousBuild)); - } - return null; - } - } - }); - } - - @NonNull - @Override - public TestResult getResultByNodes(@NonNull List nodeIds) { - return new TestResult(this); // TODO - } - }; + return new TestResultStorage(job, build); } private static class RemotePublisherImpl implements RemotePublisher { @@ -662,4 +196,483 @@ static class RemoteConnectionSupplier extends ConnectionSupplier implements Seri } } + public final class TestResultStorage implements TestResultImpl { + private final String job; + private final int build; + + public TestResultStorage(String job, int build) { + this.job = job; + this.build = build; + } + + private T query(Querier querier) { + try { + Connection connection = getConnectionSupplier().connection(); + return querier.run(connection); + } catch (SQLException x) { + throw new RuntimeException(x); + } + } + + private int getCaseCount(String and) { + return query(connection -> { + try (PreparedStatement statement = connection.prepareStatement("SELECT COUNT(*) FROM caseResults WHERE job = ? AND build = ?" + and)) { + statement.setString(1, job); + statement.setInt(2, build); + try (ResultSet result = statement.executeQuery()) { + result.next(); + int anInt = result.getInt(1); + return anInt; + } + } + }); + } + + private List retrieveCaseResult(String whereCondition) { + return query(connection -> { + try (PreparedStatement statement = connection.prepareStatement("SELECT suite, package, testname, classname, errordetails, skipped, duration, stdout, stderr, stacktrace FROM caseResults WHERE job = ? AND build = ? AND " + whereCondition)) { + statement.setString(1, job); + statement.setInt(2, build); + try (ResultSet result = statement.executeQuery()) { + + List results = new ArrayList<>(); + Map classResults = new HashMap<>(); + TestResult parent = new TestResult(this); + while (result.next()) { + String testName = result.getString("testname"); + String packageName = result.getString("package"); + String errorDetails = result.getString("errordetails"); + String suite = result.getString("suite"); + String className = result.getString("classname"); + String skipped = result.getString("skipped"); + String stdout = result.getString("stdout"); + String stderr = result.getString("stderr"); + String stacktrace = result.getString("stacktrace"); + float duration = result.getFloat("duration"); + + SuiteResult suiteResult = new SuiteResult(suite, null, null, null); + suiteResult.setParent(parent); + CaseResult caseResult = new CaseResult(suiteResult, className, testName, errorDetails, skipped, duration, stdout, stderr, stacktrace); + ClassResult classResult = classResults.get(className); + if (classResult == null) { + classResult = new ClassResult(new PackageResult(new TestResult(this), packageName), className); + } + classResult.add(caseResult); + caseResult.setClass(classResult); + classResults.put(className, classResult); + results.add(caseResult); + } + classResults.values().forEach(ClassResult::tally); + return results; + } + } + }); + } + + @Override + public List getAllPackageResults() { + return query(connection -> { + try (PreparedStatement statement = connection.prepareStatement("SELECT suite, testname, package, classname, errordetails, skipped, duration, stdout, stderr, stacktrace FROM caseResults WHERE job = ? AND build = ?")) { + statement.setString(1, job); + statement.setInt(2, build); + try (ResultSet result = statement.executeQuery()) { + + Map results = new HashMap<>(); + Map classResults = new HashMap<>(); + TestResult parent = new TestResult(this); + while (result.next()) { + String testName = result.getString("testname"); + String packageName = result.getString("package"); + String errorDetails = result.getString("errordetails"); + String suite = result.getString("suite"); + String className = result.getString("classname"); + String skipped = result.getString("skipped"); + String stdout = result.getString("stdout"); + String stderr = result.getString("stderr"); + String stacktrace = result.getString("stacktrace"); + float duration = result.getFloat("duration"); + + SuiteResult suiteResult = new SuiteResult(suite, null, null, null); + suiteResult.setParent(parent); + CaseResult caseResult = new CaseResult(suiteResult, className, testName, errorDetails, skipped, duration, stdout, stderr, stacktrace); + PackageResult packageResult = results.get(packageName); + if (packageResult == null) { + packageResult = new PackageResult(parent, packageName); + } + ClassResult classResult = classResults.get(className); + if (classResult == null) { + classResult = new ClassResult(new PackageResult(parent, packageName), className); + } + caseResult.setClass(classResult); + classResult.add(caseResult); + + classResults.put(className, classResult); + packageResult.add(caseResult); + + results.put(packageName, packageResult); + } + classResults.values().forEach(ClassResult::tally); + final List resultList = new ArrayList<>(results.values()); + resultList.forEach((PackageResult::tally)); + resultList.sort(Comparator.comparing(PackageResult::getName, String::compareTo)); + return resultList; + } + } + }); + } + + @Override + public List getTrendTestResultSummary() { + return query(connection -> { + try (PreparedStatement statement = connection.prepareStatement("SELECT build, sum(case when errorDetails is not null then 1 else 0 end) as failCount, sum(case when skipped is not null then 1 else 0 end) as skipCount, sum(case when errorDetails is null and skipped is null then 1 else 0 end) as passCount FROM caseResults" + " WHERE job = ? group by build order by build;")) { + statement.setString(1, job); + try (ResultSet result = statement.executeQuery()) { + + List trendTestResultSummaries = new ArrayList<>(); + while (result.next()) { + int buildNumber = result.getInt("build"); + int passed = result.getInt("passCount"); + int failed = result.getInt("failCount"); + int skipped = result.getInt("skipCount"); + int total = passed + failed + skipped; + + trendTestResultSummaries.add(new TrendTestResultSummary(buildNumber, new TestResultSummary(failed, skipped, passed, total))); + } + return trendTestResultSummaries; + } + } + }); + } + + @Override + public List getTestDurationResultSummary() { + return query(connection -> { + try (PreparedStatement statement = connection.prepareStatement("SELECT build, sum(duration) as duration FROM caseResults" + " WHERE job = ? group by build order by build;")) { + statement.setString(1, job); + try (ResultSet result = statement.executeQuery()) { + + List testDurationResultSummaries = new ArrayList<>(); + while (result.next()) { + int buildNumber = result.getInt("build"); + int duration = result.getInt("duration"); + + testDurationResultSummaries.add(new TestDurationResultSummary(buildNumber, duration)); + } + return testDurationResultSummaries; + } + } + }); + } + + @Override + public int getCountOfBuildsWithTestResults() { + return query(connection -> { + try (PreparedStatement statement = connection.prepareStatement("SELECT COUNT(DISTINCT build) as count FROM caseResults WHERE job = ?;")) { + statement.setString(1, job); + try (ResultSet result = statement.executeQuery()) { + result.next(); + int count = result.getInt("count"); + return count; + } + } + }); + } + + @Override + public PackageResult getPackageResult(String packageName) { + return query(connection -> { + try (PreparedStatement statement = connection.prepareStatement("SELECT suite, testname, classname, errordetails, skipped, duration, stdout, stderr, stacktrace FROM caseResults WHERE job = ? AND build = ? AND package = ?")) { + statement.setString(1, job); + statement.setInt(2, build); + statement.setString(3, packageName); + try (ResultSet result = statement.executeQuery()) { + + PackageResult packageResult = new PackageResult(new TestResult(this), packageName); + Map classResults = new HashMap<>(); + while (result.next()) { + String testName = result.getString("testname"); + String errorDetails = result.getString("errordetails"); + String suite = result.getString("suite"); + String className = result.getString("classname"); + String skipped = result.getString("skipped"); + String stdout = result.getString("stdout"); + String stderr = result.getString("stderr"); + String stacktrace = result.getString("stacktrace"); + float duration = result.getFloat("duration"); + + SuiteResult suiteResult = new SuiteResult(suite, null, null, null); + suiteResult.setParent(new TestResult(this)); + CaseResult caseResult = new CaseResult(suiteResult, className, testName, errorDetails, skipped, duration, stdout, stderr, stacktrace); + + ClassResult classResult = classResults.get(className); + if (classResult == null) { + classResult = new ClassResult(packageResult, className); + } + classResult.add(caseResult); + classResults.put(className, classResult); + caseResult.setClass(classResult); + + packageResult.add(caseResult); + } + classResults.values().forEach(ClassResult::tally); + packageResult.tally(); + return packageResult; + } + } + }); + + } + + @Override + public Run getFailedSinceRun(CaseResult caseResult) { + return query(connection -> { + int lastPassingBuildNumber; + Job theJob = Objects.requireNonNull(Jenkins.get().getItemByFullName(job, Job.class)); + try (PreparedStatement statement = connection.prepareStatement( + "SELECT build " + + "FROM caseResults " + + "WHERE job = ? " + + "AND build < ? " + + "AND suite = ? " + + "AND package = ? " + + "AND classname = ? " + + "AND testname = ? " + + "AND errordetails IS NULL " + + "ORDER BY BUILD DESC " + + "LIMIT 1" + )) { + statement.setString(1, job); + statement.setInt(2, build); + statement.setString(3, caseResult.getSuiteResult().getName()); + statement.setString(4, caseResult.getPackageName()); + statement.setString(5, caseResult.getClassName()); + statement.setString(6, caseResult.getName()); + try (ResultSet result = statement.executeQuery()) { + boolean hasPassed = result.next(); + if (!hasPassed) { + return theJob.getBuildByNumber(1); + } + + lastPassingBuildNumber = result.getInt("build"); + } + } + try (PreparedStatement statement = connection.prepareStatement( + "SELECT build " + + "FROM caseResults " + + "WHERE job = ? " + + "AND build > ? " + + "AND suite = ? " + + "AND package = ? " + + "AND classname = ? " + + "AND testname = ? " + + "AND errordetails is NOT NULL " + + "ORDER BY BUILD ASC " + + "LIMIT 1" + ) + ) { + statement.setString(1, job); + statement.setInt(2, lastPassingBuildNumber); + statement.setString(3, caseResult.getSuiteResult().getName()); + statement.setString(4, caseResult.getPackageName()); + statement.setString(5, caseResult.getClassName()); + statement.setString(6, caseResult.getName()); + + try (ResultSet result = statement.executeQuery()) { + result.next(); + + int firstFailingBuildAfterPassing = result.getInt("build"); + return theJob.getBuildByNumber(firstFailingBuildAfterPassing); + } + } + }); + + } + + @Override + public String getJobName() { + return job; + } + + @Override + public int getBuild() { + return build; + } + + @Override + public List getFailedTestsByPackage(String packageName) { + return getByPackage(packageName, "AND errorDetails IS NOT NULL"); + } + + private List getByPackage(String packageName, String filter) { + return query(connection -> { + try (PreparedStatement statement = connection.prepareStatement("SELECT suite, testname, classname, errordetails, duration, skipped, stdout, stderr, stacktrace FROM caseResults WHERE job = ? AND build = ? AND package = ? " + filter)) { + statement.setString(1, job); + statement.setInt(2, build); + statement.setString(3, packageName); + try (ResultSet result = statement.executeQuery()) { + + List results = new ArrayList<>(); + while (result.next()) { + String testName = result.getString("testname"); + String errorDetails = result.getString("errordetails"); + String suite = result.getString("suite"); + String className = result.getString("classname"); + String skipped = result.getString("skipped"); + String stdout = result.getString("stdout"); + String stderr = result.getString("stderr"); + String stacktrace = result.getString("stacktrace"); + float duration = result.getFloat("duration"); + + SuiteResult suiteResult = new SuiteResult(suite, null, null, null); + suiteResult.setParent(new TestResult(this)); + CaseResult caseResult = new CaseResult(suiteResult, className, testName, errorDetails, skipped, duration, stdout, stderr, stacktrace); + + PackageResult packageResult = new PackageResult(new TestResult(this), packageName); + ClassResult classResult = new ClassResult(packageResult, className); + classResult.add(caseResult); + packageResult.add(caseResult); + packageResult.tally(); + caseResult.tally(); + caseResult.setClass(classResult); + + results.add(caseResult); + } + return results; + } + } + }); + } + + + private List getCaseResults(String column) { + return retrieveCaseResult(column + " IS NOT NULL"); + } + + @Override + public SuiteResult getSuite(String name) { + return query(connection -> { + try (PreparedStatement statement = connection.prepareStatement("SELECT testname, package, classname, errordetails, skipped, duration, stdout, stderr, stacktrace FROM caseResults WHERE job = ? AND build = ? AND suite = ?")) { + statement.setString(1, job); + statement.setInt(2, build); + statement.setString(3, name); + try (ResultSet result = statement.executeQuery()) { + SuiteResult suiteResult = new SuiteResult(name, null, null, null); + TestResult parent = new TestResult(this); + while (result.next()) { + String resultTestName = result.getString("testname"); + String errorDetails = result.getString("errordetails"); + String packageName = result.getString("package"); + String className = result.getString("classname"); + String skipped = result.getString("skipped"); + String stdout = result.getString("stdout"); + String stderr = result.getString("stderr"); + String stacktrace = result.getString("stacktrace"); + float duration = result.getFloat("duration"); + + suiteResult.setParent(parent); + CaseResult caseResult = new CaseResult(suiteResult, className, resultTestName, errorDetails, skipped, duration, stdout, stderr, stacktrace); + final PackageResult packageResult = new PackageResult(parent, packageName); + packageResult.add(caseResult); + ClassResult classResult = new ClassResult(packageResult, className); + classResult.add(caseResult); + caseResult.setClass(classResult); + suiteResult.addCase(caseResult); + } + return suiteResult; + } + } + }); + + } + + @Override + public float getTotalTestDuration() { + return query(connection -> { + try (PreparedStatement statement = connection.prepareStatement("SELECT sum(duration) as duration FROM caseResults" + " WHERE job = ? and build = ?;")) { + statement.setString(1, job); + statement.setInt(2, build); + try (ResultSet result = statement.executeQuery()) { + if (result.next()) { + return result.getFloat("duration"); + } + return 0f; + } + } + }); + } + + @Override public int getFailCount() { + int caseCount = getCaseCount(" AND errorDetails IS NOT NULL"); + return caseCount; + } + + @Override public int getSkipCount() { + int caseCount = getCaseCount(" AND skipped IS NOT NULL"); + return caseCount; + } + + @Override public int getPassCount() { + int caseCount = getCaseCount(" AND errorDetails IS NULL AND skipped IS NULL"); + return caseCount; + } + + @Override public int getTotalCount() { + int caseCount = getCaseCount(""); + return caseCount; + } + + @Override + public List getFailedTests() { + List errordetails = getCaseResults("errordetails"); + return errordetails; + } + + @Override + public List getSkippedTests() { + List errordetails = getCaseResults("skipped"); + return errordetails; + } + + @Override + public List getSkippedTestsByPackage(String packageName) { + return getByPackage(packageName, "AND skipped IS NOT NULL"); + } + + @Override + public List getPassedTests() { + List errordetails = retrieveCaseResult("errordetails IS NULL AND skipped IS NULL"); + return errordetails; + } + + @Override + public List getPassedTestsByPackage(String packageName) { + return getByPackage(packageName, "AND errordetails IS NULL AND skipped IS NULL"); + } + + @Override + @CheckForNull + public TestResult getPreviousResult() { + return query(connection -> { + try (PreparedStatement statement = connection.prepareStatement("SELECT build FROM caseResults WHERE job = ? AND build < ? ORDER BY build DESC LIMIT 1")) { + statement.setString(1, job); + statement.setInt(2, build); + try (ResultSet result = statement.executeQuery()) { + + if (result.next()) { + int previousBuild = result.getInt("build"); + + return new TestResult(load(job, previousBuild)); + } + return null; + } + } + }); + } + + @NonNull + @Override + public TestResult getResultByNodes(@NonNull List nodeIds) { + return new TestResult(this); // TODO + } + } }