1 package net.sf.jameleon.reporting; 2 3 import net.sf.jameleon.TestCaseTag; 4 import net.sf.jameleon.util.JameleonUtility; 5 import net.sf.jameleon.bean.TestCase; 6 import net.sf.jameleon.result.*; 7 8 import java.io.StringWriter; 9 import java.io.File; 10 import java.io.IOException; 11 import java.util.HashMap; 12 import java.util.Iterator; 13 import java.util.Map; 14 15 /*** 16 * Generates the HTML results for a given test case 17 */ 18 public class HtmlTestCaseResultGenerator { 19 20 private TestCaseTag tct; 21 private final static String RES_DIR = ".."+File.separator+".."; 22 23 public HtmlTestCaseResultGenerator(TestCaseTag tct){ 24 this.tct = tct; 25 } 26 27 /*** 28 * Generates the main page along with all other iframes and content. 29 * @return The main file that was written to. 30 */ 31 public File generateTestCaseResultPage(){ 32 File resultsDir = tct.getTimestampedResultsDir(); 33 File mainFile = new File(resultsDir, "index.html"); 34 File tcSummaryFile = new File(resultsDir, "summary.html"); 35 File tcResultSummaryFile = new File(resultsDir, "resultsSummary.html"); 36 try { 37 JameleonUtility.recordResultsToFile(mainFile, generateTestCasePage()); 38 JameleonUtility.recordResultsToFile(tcSummaryFile, generateTestCaseSummary()); 39 JameleonUtility.recordResultsToFile(tcResultSummaryFile, generateTestCaseResultSummary()); 40 } catch (IOException e) { 41 System.err.println("Error writing results to file: "); 42 e.printStackTrace(); 43 } 44 return mainFile; 45 } 46 47 public String generateTestCaseResult(){ 48 StringWriter writer = new StringWriter(); 49 String childrenResults = generateAppropriateResult(tct.getResults()); 50 Map params = new HashMap(); 51 params.put("results_res_dir", RES_DIR); 52 params.put("childrenResults", childrenResults); 53 ReporterUtils.outputToTemplate(writer, tct.getTestCaseResultTemplate(), params); 54 return writer.toString(); 55 } 56 57 public String generateTestCasePage() { 58 final TestCase testCase = tct.getTestCase(); 59 StringWriter writer = new StringWriter(); 60 String results = generateTestCaseResult(); 61 Map params = new HashMap(); 62 params.put("testCase", testCase); 63 params.put("results_res_dir", RES_DIR); 64 params.put("encoding", tct.getGenTestCaseDocsEncoding()); 65 params.put("results", results); 66 ReporterUtils.outputToTemplate(writer, tct.getTestCaseMainPageTemplate(), params); 67 return writer.toString(); 68 } 69 70 public String generateTestCaseSummary() { 71 TestCase testCase = tct.getTestCase(); 72 TestCaseResult result = tct.getResults(); 73 StringWriter writer = new StringWriter(); 74 Map params = new HashMap(); 75 params.put("result", result); 76 params.put("tc", testCase); 77 params.put("encoding", tct.getGenTestCaseDocsEncoding()); 78 params.put("results_res_dir", RES_DIR); 79 if (tct.getBugTrackerUrl() != null) { 80 params.put("bugTrackerUrl", tct.getBugTrackerUrl()); 81 } 82 ReporterUtils.outputToTemplate(writer, tct.getTestCaseSummaryTemplate(), params); 83 return writer.toString(); 84 } 85 86 public String generateTestCaseResultSummary() { 87 TestCaseResult result = tct.getResults(); 88 StringWriter writer = new StringWriter(); 89 Map params = new HashMap(); 90 params.put("result", result); 91 params.put("encoding", tct.getGenTestCaseDocsEncoding()); 92 params.put("results_res_dir", RES_DIR); 93 ReporterUtils.outputToTemplate(writer, tct.getTestCaseResultSummaryTemplate(), params); 94 95 return writer.toString(); 96 } 97 98 protected String generateSessionResult(SessionResult result) { 99 StringWriter writer = new StringWriter(); 100 String childrenResults = generateResultsForChildren(result); 101 102 Map params = new HashMap(); 103 params.put("result", result); 104 params.put("childrenResults", childrenResults); 105 params.put("results_res_dir", RES_DIR); 106 ReporterUtils.outputToTemplate(writer, tct.getTestCaseResultSessionTemplate(), params); 107 return writer.toString(); 108 } 109 110 protected String generateFunctionResult(FunctionResult result) { 111 StringWriter writer = new StringWriter(); 112 Map params = new HashMap(); 113 params.put("result", result); 114 params.put("results_res_dir", RES_DIR); 115 ReporterUtils.outputToTemplate(writer, tct.getTestCaseResultFunctionTemplate(), params); 116 return writer.toString(); 117 } 118 119 protected String generateDDRowResult(DataDrivableRowResult result){ 120 StringWriter writer = new StringWriter(); 121 String childrenResults = generateResultsForChildren(result); 122 123 Map params = new HashMap(); 124 params.put("result", result); 125 params.put("childrenResults", childrenResults); 126 params.put("results_res_dir", RES_DIR); 127 ReporterUtils.outputToTemplate(writer, tct.getTestCaseResultDataRowTemplate(), params); 128 return writer.toString(); 129 } 130 131 protected String generateResultsForChildren(TestResultWithChildren result) { 132 StringBuffer childrenResults = new StringBuffer(); 133 for(Iterator i = result.getChildrenResults().iterator(); i.hasNext();){ 134 childrenResults.append(generateAppropriateResult((JameleonTestResult)i.next())); 135 } 136 return childrenResults.toString(); 137 } 138 139 protected String generateAppropriateResult(JameleonTestResult result) { 140 String resString = null; 141 if (result instanceof SessionResult){ 142 resString = generateSessionResult((SessionResult)result); 143 }else if (result instanceof FunctionResult){ 144 resString = generateFunctionResult((FunctionResult)result); 145 }else if (result instanceof DataDrivableResultContainer){ 146 resString = generateResultsForChildren((DataDrivableResultContainer)result); 147 }else if (result instanceof DataDrivableRowResult){ 148 resString = generateDDRowResult((DataDrivableRowResult)result); 149 }else if (result instanceof TestCaseResult){ 150 TestCaseResult tcr = (TestCaseResult)result; 151 if (tcr.getChildrenResults() != null && tcr.getChildrenResults().size() > 0 && 152 tcr.getChildrenResults().get(0) instanceof DataDrivableResultContainer ){ 153 resString = generateResultsForChildren((DataDrivableResultContainer)tcr.getChildrenResults().get(0)); 154 }else if (tcr.getChildrenResults() != null && tcr.getChildrenResults().size() > 0 && 155 !(tcr.getChildrenResults().get(0) instanceof DataDrivableResultContainer) ){ 156 resString = generateResultsForChildren((TestResultWithChildren) result); 157 } 158 } 159 return resString; 160 } 161 162 }