(→AHPSCRIPTS-40)
|
(→AHPSCRIPTS-66)
|
Line 899: | |||
} | } | ||
</pre> | </pre> | ||
+ | = Show Test Changes = | ||
+ | This is designed to be a "Evaluate a Script" step that creates a report that goes under the Build's report tab. | ||
+ | ==== AHPSCRIPTS-62 ==== | ||
+ | <pre>import com.urbancode.anthill3.domain.buildlife.BuildLife; | ||
+ | import com.urbancode.anthill3.domain.jobtrace.JobTrace; | ||
+ | import com.urbancode.anthill3.domain.test.TestCase; | ||
+ | import com.urbancode.anthill3.domain.test.TestReport; | ||
+ | import com.urbancode.anthill3.domain.test.TestReportFactory; | ||
+ | import com.urbancode.anthill3.domain.test.TestSuite; | ||
+ | import com.urbancode.anthill3.runtime.paths.PublishPathHelper; | ||
+ | import com.urbancode.anthill3.runtime.scripting.helpers.BuildLifeLookup; | ||
+ | import com.urbancode.anthill3.runtime.scripting.helpers.JobTraceLookup; | ||
+ | import com.urbancode.anthill3.services.template.TemplateHelper; | ||
+ | import com.urbancode.commons.fileutils.FileUtils; | ||
+ | import com.urbancode.devilfish.services.var.VarService; | ||
+ | import java.io.BufferedWriter; | ||
+ | import java.io.File; | ||
+ | import java.io.FileWriter; | ||
+ | import java.util.HashMap; | ||
+ | import java.util.Iterator; | ||
+ | import java.util.Map; | ||
+ | try { | ||
+ | // helper class for the tests | ||
+ | class TestInstance { | ||
+ | String key = null; | ||
+ | String suite = null; | ||
+ | String test = null; | ||
+ | boolean success = false; | ||
+ | int duration = 0; | ||
+ | } | ||
+ | // two maps to hold test information about this and previous build lives | ||
+ | Map oldTestMap = new HashMap(); | ||
+ | Map newTestMap = new HashMap(); | ||
+ | TemplateHelper templateHelper = TemplateHelper.getInstance(null); | ||
+ | // populate the sets | ||
+ | BuildLife currentBuildLife = BuildLifeLookup.getCurrent(); | ||
+ | TestReport[] testReportArray = TestReportFactory.getInstance() | ||
+ | .restoreAllForBuildLife(currentBuildLife); | ||
+ | for (int i = 0; i < testReportArray.length; i++) { | ||
+ | TestSuite[] testSuiteArray = testReportArray[i].getTestSuites(); | ||
+ | for (int j = 0; j < testSuiteArray.length; j++) { | ||
+ | TestCase[] testCaseArray = testSuiteArray[j].getTestCases(); | ||
+ | for (int k = 0; k < testCaseArray.length; k++) { | ||
+ | TestInstance testInstance = new TestInstance(); | ||
+ | testInstance.suite = testSuiteArray[j].getName(); | ||
+ | testInstance.test = testCaseArray[k].getName(); | ||
+ | testInstance.key = | ||
+ | testInstance.test + testInstance.suite; | ||
+ | testInstance.success = "success" | ||
+ | .equalsIgnoreCase(testCaseArray[k].getResult()); | ||
+ | testInstance.duration = testCaseArray[k].getTime(); | ||
+ | newTestMap.put(testInstance.key, testInstance); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | BuildLife lastSuccessBuildLife = BuildLifeLookup.mostRecentSuccess(); | ||
+ | testReportArray = TestReportFactory.getInstance() | ||
+ | .restoreAllForBuildLife(lastSuccessBuildLife); | ||
+ | for (int i = 0; i < testReportArray.length; i++) { | ||
+ | TestSuite[] testSuiteArray = testReportArray[i].getTestSuites(); | ||
+ | for (int j = 0; j < testSuiteArray.length; j++) { | ||
+ | TestCase[] testCaseArray = testSuiteArray[j].getTestCases(); | ||
+ | for (int k = 0; k < testCaseArray.length; k++) { | ||
+ | TestInstance testInstance = new TestInstance(); | ||
+ | testInstance.suite = testSuiteArray[j].getName(); | ||
+ | testInstance.test = testCaseArray[k].getName(); | ||
+ | testInstance.key = | ||
+ | testInstance.test + testInstance.suite; | ||
+ | testInstance.success = "success" | ||
+ | .equalsIgnoreCase(testCaseArray[k].getResult()); | ||
+ | testInstance.duration = testCaseArray[k].getTime(); | ||
+ | oldTestMap.put(testInstance.key, testInstance); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | // get the path to the reports directory | ||
+ | JobTrace jobTrace = JobTraceLookup.getCurrent(); | ||
+ | if (jobTrace != null) { | ||
+ | String publishPath = VarService.getInstance() | ||
+ | .resolve(PublishPathHelper.getInstance() | ||
+ | .getPublishPath(jobTrace, "Test Changes")); | ||
+ | FileUtils.assertDirectory(publishPath); | ||
+ | // create the report | ||
+ | File reportFile = | ||
+ | new File(publishPath, "Test Differences.html"); | ||
+ | BufferedWriter writer = | ||
+ | new BufferedWriter(new FileWriter(reportFile)); | ||
+ | writer.write("<HTML>\n" + "<HEAD>\n" + | ||
+ | "<TITLE>Anthill3 - Test Diff Report</TITLE>\n" + | ||
+ | "<link rel=\"StyleSheet\" href=\"/css/anthill3.css\" type=\"text/css\">" + | ||
+ | "</head>\n" + "<BODY>\n" + "<TABLE class=\"data-table\" WIDTH=\"100%\">\n" + | ||
+ | "\n" + "<TR><TD>\n" + "<H1>Test Diff Report</H1>\n" + | ||
+ | "</TD></TR>\n"); | ||
+ | // output all tests that are new | ||
+ | writer.write("<TR><TD>\n" + | ||
+ | "<H2>New Tests:</H2>\n" + | ||
+ | "<table cellpadding=\"4\" cellspacing=\"1\" width=\"100%\">\n" + | ||
+ | "<TH>Suite</TH>\n" + "<TH>Test</TH>\n"); | ||
+ | itr = newTestMap.keySet().iterator(); | ||
+ | while (itr.hasNext()) { | ||
+ | String key = (String) itr.next(); | ||
+ | TestInstance newTest = (TestInstance) newTestMap.get(key); | ||
+ | TestInstance oldTest = (TestInstance) oldTestMap.get(key); | ||
+ | if (newTest != null && oldTest == null) { | ||
+ | writer.write("<TR>"); | ||
+ | writer.write("<TD VALIGN=\"top\">" + newTest.suite + | ||
+ | "</TD>"); | ||
+ | writer.write( | ||
+ | "<TD VALIGN=\"top\">" + newTest.test + "</TD>"); | ||
+ | writer.write("</TR>"); | ||
+ | } | ||
+ | } | ||
+ | writer.write("</TABLE>\n" + "<BR/>\n" + "<BR/>\n" + "</TD></TR>"); | ||
+ | // output all tests failed this time but succeeded before | ||
+ | writer.write("<TR><TD>\n" + | ||
+ | "<H2>New Failing Tests:</H2>\n" + | ||
+ | "<table cellpadding=\"4\" cellspacing=\"1\" width=\"100%\">\n" + | ||
+ | "<TH>Suite</TH>\n" + "<TH>Test</TH>\n"); | ||
+ | itr = newTestMap.keySet().iterator(); | ||
+ | while (itr.hasNext()) { | ||
+ | String key = (String) itr.next(); | ||
+ | TestInstance newTest = (TestInstance) newTestMap.get(key); | ||
+ | TestInstance oldTest = (TestInstance) oldTestMap.get(key); | ||
+ | if (newTest != null && !newTest.success && (oldTest == null || oldTest.success)) { | ||
+ | writer.write("<TR>"); | ||
+ | writer.write("<TD VALIGN=\"top\">" + newTest.suite + | ||
+ | "</TD>"); | ||
+ | writer.write( | ||
+ | "<TD VALIGN=\"top\">" + newTest.test + "</TD>"); | ||
+ | writer.write("</TR>"); | ||
+ | } | ||
+ | } | ||
+ | writer.write("</TABLE>\n" + "<BR/>\n" + "<BR/>\n" + "</TD></TR>"); | ||
+ | // output all tests that took more time | ||
+ | writer.write("<TR><TD>\n" + | ||
+ | "<H2>Tests that ran slower:</H2>\n" + | ||
+ | "<table cellpadding=\"4\" cellspacing=\"1\" width=\"100%\">\n" + | ||
+ | "<TH>Suite</TH>\n" + "<TH>Test</TH>\n" + | ||
+ | "<TH>Last Run Duration</TH>\n" + | ||
+ | "<TH>Current Run Duration</TH>\n"); | ||
+ | Iterator itr = newTestMap.keySet().iterator(); | ||
+ | while (itr.hasNext()) { | ||
+ | String key = (String) itr.next(); | ||
+ | TestInstance newTest = (TestInstance) newTestMap.get(key); | ||
+ | TestInstance oldTest = (TestInstance) oldTestMap.get(key); | ||
+ | if (newTest != null && oldTest != null && | ||
+ | newTest.duration > oldTest.duration*1.1) { | ||
+ | writer.write("<TR>"); | ||
+ | writer.write("<TD VALIGN=\"top\">" + newTest.suite + | ||
+ | "</TD>"); | ||
+ | writer.write( | ||
+ | "<TD VALIGN=\"top\">" + newTest.test + "</TD>"); | ||
+ | writer.write("<TD VALIGN=\"top\" ALIGN=\"center\" >" + | ||
+ | templateHelper.formatNumber( | ||
+ | templateHelper.duration( | ||
+ | oldTest.duration), "##0.0000") + | ||
+ | " secs</TD>"); | ||
+ | writer.write("<TD VALIGN=\"top\" ALIGN=\"center\" >" + | ||
+ | templateHelper.formatNumber( | ||
+ | templateHelper.duration( | ||
+ | newTest.duration), "##0.0000") + | ||
+ | " secs</TD>"); | ||
+ | writer.write("</TR>"); | ||
+ | } | ||
+ | } | ||
+ | writer.write("</TABLE>\n" + "<BR/>\n" + "<BR/>\n" + "</TD></TR>"); | ||
+ | // output all tests that were fixed or are new and succeede | ||
+ | writer.write("<TR><TD>\n" + | ||
+ | "<H2>New or Newly Fixed Tests:</H2>\n" + | ||
+ | "<table cellpadding=\"4\" cellspacing=\"1\" width=\"100%\">\n" + | ||
+ | "<TH>Suite</TH>\n" + "<TH>Test</TH>\n"); | ||
+ | itr = newTestMap.keySet().iterator(); | ||
+ | while (itr.hasNext()) { | ||
+ | String key = (String) itr.next(); | ||
+ | TestInstance newTest = (TestInstance) newTestMap.get(key); | ||
+ | TestInstance oldTest = (TestInstance) oldTestMap.get(key); | ||
+ | if (newTest != null && newTest.success && (oldTest == null || !oldTest.success)) { | ||
+ | writer.write("<TR>"); | ||
+ | writer.write("<TD VALIGN=\"top\">" + newTest.suite + | ||
+ | "</TD>"); | ||
+ | writer.write( | ||
+ | "<TD VALIGN=\"top\">" + newTest.test + "</TD>"); | ||
+ | writer.write("</TR>"); | ||
+ | } | ||
+ | } | ||
+ | writer.write("</TABLE>\n" + "<BR/>\n" + "<BR/>\n" + "</TD></TR>"); | ||
+ | writer.write("</TABLE>\n" + "</BODY>\n" + "</HTML>"); | ||
+ | writer.flush(); | ||
+ | writer.close(); | ||
+ | } | ||
+ | } | ||
+ | catch (Exception e) { | ||
+ | e.printStackTrace(commandOutput); | ||
+ | } | ||
+ | </pre> |