Index: TestHarness4LAMS2/src/org/lamsfoundation/testharness/TestReporter.java =================================================================== diff -u -r63f3f29ba085acacf9281740bf3396e3eea197c5 -rc2bb66114371b24fc999d99ef26e2e81ecce274b --- TestHarness4LAMS2/src/org/lamsfoundation/testharness/TestReporter.java (.../TestReporter.java) (revision 63f3f29ba085acacf9281740bf3396e3eea197c5) +++ TestHarness4LAMS2/src/org/lamsfoundation/testharness/TestReporter.java (.../TestReporter.java) (revision c2bb66114371b24fc999d99ef26e2e81ecce274b) @@ -22,19 +22,33 @@ */ package org.lamsfoundation.testharness; +import java.io.BufferedWriter; 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.util.ArrayList; +import java.io.BufferedReader; +import java.lang.reflect.Array; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.net.MalformedURLException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import org.apache.log4j.Logger; import org.lamsfoundation.testharness.Call.CallRecord; import org.lamsfoundation.testharness.learner.LearnerTest; +import edu.stanford.ejalbert.BrowserLauncher; +import edu.stanford.ejalbert.exception.BrowserLaunchingExecutionException; +import edu.stanford.ejalbert.exception.BrowserLaunchingInitializingException; +import edu.stanford.ejalbert.exception.UnsupportedOperatingSystemException; + /** * @version * @@ -138,46 +152,245 @@ public static void generateReport(AbstractTestManager manager) { report(manager); log.info("Generating the formal test report document..."); - log.info("Sorry, this feature is not ready yet. It should come soon."); + TemplateCompiler.init(manager.testSuites, callRecords); + String filename = generateFileName(); + try { + String report = TemplateCompiler.compile(TemplateCompiler.load()); + BufferedWriter out = new BufferedWriter(new FileWriter(filename)); + out.write(report); + out.close(); + BrowserLauncher launcher = new BrowserLauncher(null); + launcher.openURLinBrowser(new File(filename).toURL().toString()); + } catch (MalformedURLException e) { + log.debug(e.getMessage(),e); + } catch (IOException e) { + log.debug(e.getMessage(), e); + }catch (BrowserLaunchingInitializingException e) { + log.debug(e.getMessage(),e); + } catch (UnsupportedOperatingSystemException e) { + log.debug(e.getMessage(),e); + } catch (BrowserLaunchingExecutionException e) { + log.debug(e.getMessage(),e); + } } + + private static String generateFileName(){ + return fileName + "_" + new SimpleDateFormat("MMM-d-HH'h-'mm'm'").format(new Date())+".html"; + } + public static void main(String[] args){ + String[] results = "testSuite.suiteIndex".split("\\."); + System.out.println(results.length); + } + private static class TemplateCompiler{ - static String loadAndCompile() throws IOException{ - StringBuilder result = new StringBuilder(); - LineNumberReader lnReader = new LineNumberReader(new FileReader(fileTemplate)); + + static Map context; + static final int LIST_FIRST = 0; + static final int IF_FIRST = 1; + static final int EL_FIRST = 2; + static final int TEXT_FIRST = 3; + + static final String EL_START = "${"; + static final char EL_END = '}'; + static final String LIST_START = "<#list"; + static final String LIST_AS = "as"; + //static final String LIST_GROUP_BY = "group by"; + static final String LIST_END = ""; + static final String IF_START = "<#if"; + static final String IF_ELSE = "<#else>"; + static final String IF_END = ""; + static final char TAG_END = '>'; + //static final String AVG = "avg"; + //static final String SUM = "sum"; + //static final String COUNT = "count"; + //static final char PARAM_START = '('; + //static final char PARAM_END = ')'; + + static void init(List testSuites, List callRecords){ + context = new HashMap(); + context.put("callRecords", callRecords); + context.put("testSuites", testSuites); + context.put("time", new SimpleDateFormat("HH:mm:ss dd MMM yyyy").format(new Date())); + } + + static String load() throws IOException{ + StringBuilder source = new StringBuilder(); + BufferedReader lnReader = new BufferedReader(new FileReader(fileTemplate)); String line = lnReader.readLine(); while (line != null){ - List block = new ArrayList(); - int statementIdx = line.indexOf("<#"); - int elIdx = line.indexOf("${"); - if(statementIdx!=-1){ - block.add(line); - String keyword = line.substring(statementIdx+2,line.indexOf(' ',statementIdx)); - String endTag = "'; - while((line!=null)&&(line.indexOf(endTag) == -1)){ - block.add(line = lnReader.readLine()); - } - if(line == null){ - throw new TestHarnessException("Cannot find end tag "+endTag); - } - result.append(compileBlock((String[])block.toArray(),keyword, endTag)); - }else if(elIdx!=-1){ - String el = line.substring(elIdx,line.indexOf('}',elIdx)+1); - result.append(line.replace(el, compileEl(el))); + source.append(line).append('\n'); + line = lnReader.readLine(); + } + return source.toString(); + } + + private static String compile(String source) { + int listStartIndex = source.indexOf(LIST_START); + int ifStartIndex = source.indexOf(IF_START); + int elStartIndex = source.indexOf(EL_START); + switch(whichFirst(convert(listStartIndex,source), convert(ifStartIndex, source), convert(elStartIndex, source), source.length())){ + case LIST_FIRST: + int listEndIndex = source.indexOf(LIST_END, listStartIndex); + int listStartTagIndex = source.indexOf(TAG_END, listStartIndex); + int listAsIndex = source.indexOf(LIST_AS, listStartIndex); + if(listStartTagIndex == -1) throw new TestHarnessException("'list' tag missing '>'"); + if(listAsIndex == -1) throw new TestHarnessException("'as' is required for 'list'"); + if(listEndIndex == -1) throw new TestHarnessException("'list' tag unclosed"); + StringBuilder middle = new StringBuilder(); + String middlePart = source.substring(listStartTagIndex+1,listEndIndex); + String collection = extract(source.substring(listStartIndex + LIST_START.length(), listAsIndex)); + String element = extract(source.substring(listAsIndex + LIST_AS.length(), listStartTagIndex)).trim(); + for(Object o : (List)compileEL(collection)){ + //log.debug("Put " + o + " into context as "+element); + context.put(element, o); + middle.append(compile(middlePart)); + } + String frontEnd = source.substring(0, listStartIndex); + String backEnd = source.substring(listEndIndex + LIST_END.length()); + return compile(frontEnd) + middle.toString() + compile(backEnd); + case IF_FIRST: + int ifEndIndex = source.indexOf(IF_END, ifStartIndex); + int ifStartTagIndex = source.indexOf(TAG_END, ifStartIndex); + int elseIndex = source.indexOf(IF_ELSE, ifStartIndex); + if(ifStartTagIndex == -1) throw new TestHarnessException("'if' tag missing '>'"); + if(ifEndIndex == -1) throw new TestHarnessException("'if' tag unclosed"); + String condition = extract(source.substring(ifStartIndex + IF_START.length(), ifStartTagIndex)); + Boolean b = (Boolean)compileEL(condition); + String ifBlock; + if(elseIndex != -1){ + String middlePart1 = source.substring(ifStartTagIndex+1, elseIndex); + String middlePart2 = source.substring(elseIndex + IF_ELSE.length(), ifEndIndex); + ifBlock = b? middlePart1 : middlePart2; }else{ - result.append(line).append('\n'); + String middlePart1 = source.substring(ifStartTagIndex+1, ifEndIndex); + ifBlock = b? middlePart1:""; } - line = lnReader.readLine(); + String ifFrontEnd = source.substring(0, ifStartIndex); + String ifBackEnd = source.substring(ifEndIndex + IF_END.length()); + return compile(ifFrontEnd) + compile(ifBlock) + compile(ifBackEnd); + case EL_FIRST: + int elEndIndex = source.indexOf(EL_END, elStartIndex); + if(elEndIndex == -1) throw new TestHarnessException("'}' expected"); + String elBlock = compileEL(source.substring(elStartIndex + EL_START.length(), elEndIndex)).toString(); + String elFrontEnd = source.substring(0, elStartIndex); + String elBackEnd = source.substring(elEndIndex+1); + return compile(elFrontEnd) + elBlock + compile(elBackEnd); + case TEXT_FIRST: + return source; + default: + throw new TestHarnessException("unexpected error happened!"); } - return null; } - private static Object compileBlock(String[] strings, String keyword, String endTag) { - return null; + private static int whichFirst(int listStartIndex, int ifStartIndex, int elStartIndex, int textIndex){ + int[] indexes = {listStartIndex, ifStartIndex, elStartIndex, textIndex}; + return whichMinimum(indexes); } + + + private static int whichMinimum(int[] nums) { + if(nums.length == 0){ + return -1; + } + int min = nums[0]; + int minIndex = 0; + for(int i=1; i