View Javadoc

1   package com.github.jlgrock.javascriptframework.closuretestingreport;
2   
3   import java.util.Set;
4   
5   import org.apache.log4j.Logger;
6   import org.apache.maven.doxia.sink.Sink;
7   import org.apache.maven.reporting.AbstractMavenReportRenderer;
8   
9   import com.github.jlgrock.javascriptframework.closuretesting.resultparsing.testingcomponents.TestCase;
10  
11  /**
12   * Will write the output for the unit testing.
13   * 
14   */
15  public class ClosureTestingReportGenerator extends AbstractMavenReportRenderer {
16  	/**
17  	 * Denoting the first character of the upper case ASCII letters.
18  	 */
19  	static final int UPPERCASE_ASCII_LOWER_BOUND = 65;
20  	/**
21  	 * Denoting the last character of the upper case ASCII letters.
22  	 */
23  	static final int UPPERCASE_ASCII_UPPER_BOUND = 90;
24  	/**
25  	 * Denoting the first character of the lower case ASCII letters.
26  	 */
27  	static final int LOWERCASE_ASCII_LOWER_BOUND = 97;
28  	/**
29  	 * Denoting the last character of the lower case ASCII letters.
30  	 */
31  	static final int LOWERCASE_ASCII_UPPER_BOUND = 122;
32  
33  	/**
34  	 * The Logger.
35  	 */
36  	private static final Logger LOGGER = Logger
37  			.getLogger(ClosureTestingReportGenerator.class);
38  
39  	/**
40  	 * the parsed test cases, used for writing statistical output.
41  	 */
42  	private final Set<TestCase> testCases;
43  
44  	/**
45  	 * Constructor.
46  	 * 
47  	 * @param sink
48  	 *            the sink to use for writing
49  	 * @param testCasesIn
50  	 *            the parsed test cases, used for writing statistical output
51  	 */
52  	public ClosureTestingReportGenerator(final Sink sink,
53  			final Set<TestCase> testCasesIn) {
54  		super(sink);
55  		this.testCases = testCasesIn;
56  	}
57  
58  	@Override
59  	public final String getTitle() {
60  		return "Google Closure Unit Testing";
61  	}
62  
63  	@Override
64  	protected final void renderBody() {
65  
66  		sink.body();
67  
68  		// Summary section
69  		summarySection(sink);
70  
71  		// File Link section
72  		linkSection(sink);
73  
74  		// Raw Dump section
75  		rawDumpSection(sink);
76  
77  		sink.body_();
78  		sink.flush();
79  		sink.close();
80  	}
81  
82  	/**
83  	 * Writes the entire raw dump section.
84  	 * 
85  	 * @param sink
86  	 *            the sink used for writing
87  	 */
88  	private void rawDumpSection(final Sink sink) {
89  		sink.horizontalRule();
90  		sink.lineBreak();
91  		sink.section2();
92  		sink.sectionTitle2();
93  		sink.text("Closure Unit Testing Raw Dump");
94  		sink.sectionTitle2_();
95  		makeRawDump(sink);
96  		sink.section2_();
97  	}
98  
99  	/**
100 	 * Writes the entire link section.
101 	 * 
102 	 * @param sink
103 	 *            the sink used for writing
104 	 */
105 	private void linkSection(final Sink sink) {
106 		sink.section2();
107 		sink.lineBreak();
108 		sink.sectionTitle2();
109 		sink.text("Files");
110 		sink.sectionTitle2_();
111 		makeLinks(sink);
112 		sink.section2_();
113 	}
114 
115 	/**
116 	 * Writes the entire summary section.
117 	 * 
118 	 * @param sink
119 	 *            the sink used for writing
120 	 */
121 	private void summarySection(final Sink sink) {
122 		sink.section1();
123 		sink.sectionTitle1();
124 		sink.text("Google Closure Unit Testing");
125 		sink.sectionTitle1_();
126 
127 		sink.text("List of executed Google Closure Library Unit Test results.");
128 		sink.section1_();
129 	}
130 
131 	/**
132 	 * Iterates over all the raw input and writes them to the sink.
133 	 * 
134 	 * @param sink
135 	 *            the sink used for writing
136 	 */
137 	private void makeRawDump(final Sink sink) {
138 		for (TestCase testCase : testCases) {
139 			LOGGER.debug("doing raw dump for test case: "
140 					+ testCase.getSummary().getRelativeLocation());
141 			sink.paragraph();
142 			sink.bold();
143 			String pathAnchor = encodeAnchor(testCase.getSummary()
144 					.getRelativeLocation());
145 			sink.anchor(pathAnchor);
146 			sink.text(testCase.getSummary().getRelativeLocation());
147 			sink.anchor_();
148 			sink.bold_();
149 			sink.lineBreak();
150 			sink.lineBreak();
151 			for (String div : testCase.getRawDivs()) {
152 				sink.text(div);
153 			}
154 			sink.paragraph_();
155 			sink.horizontalRule();
156 		}
157 	}
158 
159 	/**
160 	 * Replace all characters that are unsafe in a url string (past the anchor
161 	 * mark "#").
162 	 * 
163 	 * @param relativeLocation
164 	 *            the anchor location
165 	 * @return return the url safe string
166 	 */
167 	private String encodeAnchor(final String relativeLocation) {
168 		StringBuffer calcString = new StringBuffer();
169 		calcString.append("fileref");
170 		for (int i = 0; i < relativeLocation.length(); i++) {
171 			int asciiVal = (int) relativeLocation.charAt(i);
172 			if ((asciiVal >= UPPERCASE_ASCII_LOWER_BOUND && asciiVal <= UPPERCASE_ASCII_UPPER_BOUND)
173 					|| (asciiVal >= LOWERCASE_ASCII_LOWER_BOUND && asciiVal <= LOWERCASE_ASCII_UPPER_BOUND)) {
174 				calcString.append((char) asciiVal);
175 			} else {
176 				calcString.append(Integer.toHexString(asciiVal));
177 			}
178 		}
179 
180 		return calcString.toString();
181 	}
182 
183 	/**
184 	 * Iterates over all of the information to make the files table.
185 	 * 
186 	 * @param sink
187 	 *            the sink used for writing
188 	 */
189 	private void makeLinks(final Sink sink) {
190 		sink.table();
191 
192 		sink.tableRow();
193 		sink.tableHeaderCell();
194 		sink.text("Relative Filename");
195 		sink.tableHeaderCell_();
196 
197 		sink.tableHeaderCell();
198 		sink.text("Number Passed");
199 		sink.tableHeaderCell_();
200 
201 		sink.tableHeaderCell();
202 		sink.text("Number Failed");
203 		sink.tableHeaderCell_();
204 
205 		sink.tableRow_();
206 		for (TestCase testCase : testCases) {
207 			// TODO create the output dump for later review of metrics and such.
208 			createRow(testCase, sink);
209 		}
210 		sink.table_();
211 	}
212 
213 	/**
214 	 * Create a single row in the files table.
215 	 * 
216 	 * @param testCase
217 	 *            the test case to output to the table
218 	 * @param sink
219 	 *            the sink used for writing
220 	 */
221 	private void createRow(final TestCase testCase, final Sink sink) {
222 		// Output
223 		sink.tableRow();
224 		// Relative Filename
225 		sink.tableCell();
226 
227 		// link to anchored section below
228 		sink.link("#"
229 				+ encodeAnchor(testCase.getSummary().getRelativeLocation()));
230 		sink.text(testCase.getSummary().getRelativeLocation());
231 		sink.link_();
232 		sink.tableCell_();
233 
234 		// Number Passed
235 		sink.tableCell();
236 		sink.text(String.valueOf(testCase.getFailureStatistic().getNumPassed()));
237 		sink.link_();
238 		sink.tableCell_();
239 
240 		// Number Failed
241 		sink.tableCell();
242 		sink.text(String.valueOf(testCase.getFailureStatistic().getNumFailed()));
243 		sink.link_();
244 		sink.tableCell_();
245 
246 		sink.tableRow_();
247 	}
248 }