diff --git a/src/main/java/com/gyrobian/assessment/ExecutionLogComparisonAssessment.java b/src/main/java/com/gyrobian/assessment/ExecutionLogComparisonAssessment.java new file mode 100644 index 0000000..24818c1 --- /dev/null +++ b/src/main/java/com/gyrobian/assessment/ExecutionLogComparisonAssessment.java @@ -0,0 +1,78 @@ +package com.gyrobian.assessment; + +import com.gyrobian.database.CachedResultSet; +import com.gyrobian.database.ExecutionLog; +import com.gyrobian.database.QueryAction; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class ExecutionLogComparisonAssessment { + + private boolean sameNumberOfActions; + + private boolean sameNumberOfSelectQueries; + + private List templateExceptions; + + private List testingExceptions; + + private List queryDiscrepancies; + + private ExecutionLogComparisonAssessment() { + this.queryDiscrepancies = new ArrayList<>(); + } + + /** + * Builds an assessment from two execution logs. + * @param template The template log to check the testing log against. + * @param testing The testing log, to check for errors in. + * @return A new assessment object containing details about how these two execution logs differ. + */ + public static ExecutionLogComparisonAssessment fromExecutionLogs(ExecutionLog template, ExecutionLog testing) { + ExecutionLogComparisonAssessment assessment = new ExecutionLogComparisonAssessment(); + assessment.sameNumberOfActions = template.getActions().size() == testing.getActions().size(); + assessment.templateExceptions = template.getExceptions(); + assessment.testingExceptions = testing.getExceptions(); + + assessment.sameNumberOfSelectQueries = template.getQueryActions().size() == testing.getQueryActions().size(); + // If both scripts have the same number of selects, check if they're similar. + if (assessment.sameNumberOfSelectQueries) { + List templateQueries = template.getQueryActions(); + List testingQueries = testing.getQueryActions(); + for (int i = 0; i < templateQueries.size(); i++) { + CachedResultSet templateResult = templateQueries.get(i).getResultSet(); + CachedResultSet testingResult = testingQueries.get(i).getResultSet(); + if (templateResult.getRowCount() != testingResult.getRowCount()) { + assessment.queryDiscrepancies.add("Template row count doesn't match testing row count for query " + i + "."); + } + if (templateResult.getColumnNames().length != testingResult.getColumnNames().length) { + assessment.queryDiscrepancies.add("Template query has different number of columns, compared to testing query, for query " + i + "."); + } + } + } + + return assessment; + } + + public boolean hasSameNumberOfActions() { + return sameNumberOfActions; + } + + public boolean hasSameNumberOfSelectQueries() { + return sameNumberOfSelectQueries; + } + + public List getTemplateExceptions() { + return templateExceptions; + } + + public List getTestingExceptions() { + return testingExceptions; + } + + public List getQueryDiscrepancies() { + return queryDiscrepancies; + } +} diff --git a/src/main/java/com/gyrobian/database/ExecutionLog.java b/src/main/java/com/gyrobian/database/ExecutionLog.java index 104dab8..23bc685 100644 --- a/src/main/java/com/gyrobian/database/ExecutionLog.java +++ b/src/main/java/com/gyrobian/database/ExecutionLog.java @@ -1,5 +1,6 @@ package com.gyrobian.database; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -27,14 +28,41 @@ public class ExecutionLog { } /** - * @return True if there's an exception somewhere in this execution log, or false otherwise. + * @return The list of all exceptions encountered. */ - public boolean containsExceptions() { - for (ExecutionAction action : this.getActions()) { + public List getExceptions() { + List exceptions = new ArrayList<>(); + this.actions.forEach(action -> { if (action.getException() != null) { - return true; + exceptions.add(action.getException()); } - } - return false; + }); + return exceptions; + } + + /** + * @return The list of all schema update actions encountered. + */ + public List getSchemaUpdateActions() { + List updateActions = new ArrayList<>(); + this.actions.forEach(action -> { + if (action instanceof UpdateAction) { + updateActions.add((UpdateAction) action); + } + }); + return updateActions; + } + + /** + * @return The list of all query actions encountered. + */ + public List getQueryActions() { + List queryActions = new ArrayList<>(); + this.actions.forEach(action -> { + if (action instanceof QueryAction) { + queryActions.add((QueryAction) action); + } + }); + return queryActions; } } diff --git a/src/main/java/com/gyrobian/listener/AssessExecutionsListener.java b/src/main/java/com/gyrobian/listener/AssessExecutionsListener.java index 2c1d3f4..5760c53 100644 --- a/src/main/java/com/gyrobian/listener/AssessExecutionsListener.java +++ b/src/main/java/com/gyrobian/listener/AssessExecutionsListener.java @@ -1,5 +1,6 @@ package com.gyrobian.listener; +import com.gyrobian.assessment.ExecutionLogComparisonAssessment; import com.gyrobian.database.ExecutionLog; import com.gyrobian.view.AssessmentDisplay; import com.gyrobian.view.ExecutionLogDisplay; @@ -40,22 +41,20 @@ public class AssessExecutionsListener implements ActionListener { */ @Override public void actionPerformed(ActionEvent e) { + triggerButton(this.executeTemplateButton, this); + triggerButton(this.executeTestingButton, this); ExecutionLog lastTemplateExecution = this.templateExecutionLogDisplay.getLastExecutionLogDisplayed(); ExecutionLog lastTestingExecution = this.testingExecutionLogDisplay.getLastExecutionLogDisplayed(); - if (lastTemplateExecution == null || lastTestingExecution == null) { - this.assessmentDisplay.appendToDocument("Could not find a recent execution for one of the scripts, re-running...\n", null); - triggerButton(this.executeTemplateButton, this); - triggerButton(this.executeTestingButton, this); - lastTemplateExecution = this.templateExecutionLogDisplay.getLastExecutionLogDisplayed(); - lastTestingExecution = this.testingExecutionLogDisplay.getLastExecutionLogDisplayed(); - if (lastTemplateExecution == null || lastTestingExecution == null) { - JOptionPane.showMessageDialog(this.assessmentDisplay, "Could not execute both scripts.", "Assessment Error", JOptionPane.WARNING_MESSAGE); - return; - } + if (lastTemplateExecution == null || lastTestingExecution == null) { + JOptionPane.showMessageDialog(this.assessmentDisplay, "Could not execute both scripts.", "Assessment Error", JOptionPane.WARNING_MESSAGE); + return; } + this.assessmentDisplay.setText(null); + ExecutionLogComparisonAssessment assessment = ExecutionLogComparisonAssessment.fromExecutionLogs(lastTemplateExecution, lastTestingExecution); + this.assessmentDisplay.displayAssessment(assessment); } /** diff --git a/src/main/java/com/gyrobian/view/AssessmentDisplay.java b/src/main/java/com/gyrobian/view/AssessmentDisplay.java index 91a1e8b..9e99e89 100644 --- a/src/main/java/com/gyrobian/view/AssessmentDisplay.java +++ b/src/main/java/com/gyrobian/view/AssessmentDisplay.java @@ -1,7 +1,37 @@ package com.gyrobian.view; +import com.gyrobian.assessment.ExecutionLogComparisonAssessment; + /** * A text pane that's specially suited for displaying the results of assessing two execution logs. */ public class AssessmentDisplay extends AppendableJTextPane { + + public void displayAssessment(ExecutionLogComparisonAssessment assessment) { + if (assessment.hasSameNumberOfActions()) { + this.appendToDocument("Scripts perform the same number of actions.\n", null); + } else { + this.appendToDocument("Warning! Scripts do not perform the same number of actions. This does not explicitly mean the testing script is wrong, but may indicate so.\n", null); + } + + if (assessment.hasSameNumberOfSelectQueries()) { + this.appendToDocument("Scripts perform the same number of SELECT queries.\n", null); + } else { + this.appendToDocument("Warning! Scripts do not perform the same number of SELECT queries. While not a 100% indicator, it is very likely that the testing script is incorrect.\n", null); + } + + if (assessment.getTemplateExceptions().size() > 0) { + this.appendToDocument("Warning! Template script throws exceptions! Check execution output.\n", null); + } + if (assessment.getTestingExceptions().size() > 0) { + this.appendToDocument("Warning! Testing script throws exceptions! Check execution output. This is indicative of an incorrect script.\n", null); + } + + if (assessment.getQueryDiscrepancies().size() > 0) { + this.appendToDocument("Warning! There are discrepancies in the results of SELECT queries done by the template and testing scripts.\n", null); + for (String message : assessment.getQueryDiscrepancies()) { + this.appendToDocument("\t" + message + "\n", null); + } + } + } }