587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.helium.ant.coverage.listener;
|
|
18 |
|
|
19 |
import java.io.File;
|
|
20 |
import java.io.FileOutputStream;
|
|
21 |
import java.io.IOException;
|
|
22 |
import java.io.OutputStream;
|
|
23 |
import java.io.OutputStreamWriter;
|
|
24 |
import java.io.PrintWriter;
|
|
25 |
import java.io.StringWriter;
|
|
26 |
import java.util.Enumeration;
|
|
27 |
import java.util.Hashtable;
|
|
28 |
import java.util.Map;
|
|
29 |
|
|
30 |
import org.apache.ant.antunit.listener.BaseAntUnitListener;
|
|
31 |
import org.apache.tools.ant.BuildEvent;
|
|
32 |
import org.apache.tools.ant.BuildException;
|
|
33 |
import org.apache.tools.ant.BuildListener;
|
|
34 |
import org.apache.tools.ant.Project;
|
|
35 |
import org.apache.tools.ant.ProjectHelper;
|
|
36 |
import org.apache.tools.ant.Target;
|
|
37 |
|
|
38 |
import com.nokia.helium.ant.coverage.AntMacros;
|
|
39 |
import com.nokia.helium.ant.coverage.AntScriptDefs;
|
|
40 |
import com.nokia.helium.ant.coverage.AntTargets;
|
|
41 |
|
|
42 |
import freemarker.cache.ClassTemplateLoader;
|
|
43 |
import freemarker.template.Configuration;
|
|
44 |
import freemarker.template.Template;
|
|
45 |
import freemarker.template.TemplateException;
|
|
46 |
|
|
47 |
/**
|
|
48 |
* Helium antunit listener. This listener will collect the information of test
|
|
49 |
* targets run and feeds the information into ParseTestFiles to get the tested
|
|
50 |
* targets, macros and scriptdefs information.
|
|
51 |
*/
|
|
52 |
|
|
53 |
public class HlmAntUnitListener extends BaseAntUnitListener {
|
|
54 |
private static final String NEW_LINE = System.getProperty("line.separator");
|
|
55 |
private OutputStream out;
|
|
56 |
private AntTargets antTargets;
|
|
57 |
private AntMacros antMacro;
|
|
58 |
private AntScriptDefs antScriptDef;
|
|
59 |
private PrintWriter wri;
|
|
60 |
private StringWriter inner;
|
|
61 |
private File outputFile;
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Default constructor.
|
|
65 |
*/
|
|
66 |
public HlmAntUnitListener() {
|
|
67 |
super(new BaseAntUnitListener.SendLogTo(SendLogTo.ANT_LOG), "txt");
|
|
68 |
antTargets = new AntTargets();
|
|
69 |
antMacro = new AntMacros();
|
|
70 |
antScriptDef = new AntScriptDefs();
|
|
71 |
}
|
|
72 |
|
|
73 |
/*
|
|
74 |
* (non-Javadoc)
|
|
75 |
*
|
|
76 |
* @see org.apache.ant.antunit.AntUnitListener#endTest(java.lang.String)
|
|
77 |
*/
|
|
78 |
@Override
|
|
79 |
public void endTest(String target) {
|
|
80 |
}
|
|
81 |
|
|
82 |
/*
|
|
83 |
* (non-Javadoc)
|
|
84 |
*
|
|
85 |
* @see
|
|
86 |
* org.apache.ant.antunit.AntUnitListener#endTestSuite(org.apache.tools.
|
|
87 |
* ant.Project, java.lang.String)
|
|
88 |
*/
|
|
89 |
@Override
|
|
90 |
public void endTestSuite(Project testProject, String buildFile) {
|
|
91 |
try {
|
|
92 |
StringWriter ftlWriter = new StringWriter();
|
|
93 |
Configuration cfg = new Configuration();
|
|
94 |
Template template = null;
|
|
95 |
cfg.setTemplateLoader(new ClassTemplateLoader(this.getClass(), ""));
|
|
96 |
template = cfg.getTemplate("ant_coverage_report.txt.ftl");
|
|
97 |
Map<String, Object> data = new Hashtable<String, Object>();
|
|
98 |
data.put("target_percentage", (int)this.getTargetCoverage());
|
|
99 |
data.put("target_testcases", antTargets.getExecutedCount());
|
|
100 |
data.put("total_targets", antTargets.getCount());
|
|
101 |
|
|
102 |
data.put("macros_percentage", (int)this.getMacroCoverage());
|
|
103 |
data.put("macros_testcases", antMacro.getExecutedCount());
|
|
104 |
data.put("total_macros", antMacro.getCount());
|
|
105 |
|
|
106 |
data.put("scriptdefs_percentage", (int)this.getScriptDefCoverage());
|
|
107 |
data.put("scriptdefs_testcases", antScriptDef.getExecutedCount());
|
|
108 |
data.put("total_scriptdefs", antScriptDef.getCount());
|
|
109 |
template.process(data, ftlWriter);
|
|
110 |
|
|
111 |
if (getOutputFile() != null) {
|
|
112 |
File outputFile = getOutputFile();
|
|
113 |
if (!outputFile.getParentFile().exists()) {
|
|
114 |
outputFile.getParentFile().mkdirs();
|
|
115 |
}
|
|
116 |
OutputStreamWriter output = new OutputStreamWriter(
|
|
117 |
new FileOutputStream(getOutputFile()));
|
|
118 |
output.append(ftlWriter.getBuffer().toString());
|
|
119 |
output.close();
|
|
120 |
output = new OutputStreamWriter(new FileOutputStream(outputFile
|
|
121 |
.getParentFile().toString()
|
|
122 |
+ File.separator + "target.plot.property"));
|
|
123 |
output.append("YVALUE=" + (int)this.getTargetCoverage());
|
|
124 |
output.close();
|
|
125 |
output = new OutputStreamWriter(new FileOutputStream(outputFile
|
|
126 |
.getParentFile().toString()
|
|
127 |
+ File.separator + "macro.plot.property"));
|
|
128 |
output.append("YVALUE=" + (int)this.getMacroCoverage());
|
|
129 |
output.close();
|
|
130 |
output = new OutputStreamWriter(new FileOutputStream(outputFile
|
|
131 |
.getParentFile().toString()
|
|
132 |
+ File.separator + "scriptdef.plot.property"));
|
|
133 |
output.append("YVALUE="
|
|
134 |
+ (int)this.getScriptDefCoverage());
|
|
135 |
output.close();
|
|
136 |
} else {
|
|
137 |
StringBuffer sb = new StringBuffer(NEW_LINE);
|
|
138 |
sb.append(ftlWriter.getBuffer().toString());
|
|
139 |
if (out != null) {
|
|
140 |
out.write(sb.toString().getBytes());
|
|
141 |
wri.close();
|
|
142 |
out.write(inner.toString().getBytes());
|
|
143 |
out.flush();
|
|
144 |
}
|
|
145 |
}
|
|
146 |
} catch (IOException ioe) {
|
|
147 |
throw new BuildException(ioe.getMessage(), ioe);
|
|
148 |
} catch (TemplateException ftle) {
|
|
149 |
throw new BuildException(ftle.getMessage(), ftle);
|
|
150 |
} finally {
|
|
151 |
close(out);
|
|
152 |
}
|
|
153 |
}
|
|
154 |
|
|
155 |
/*
|
|
156 |
* (non-Javadoc)
|
|
157 |
*
|
|
158 |
* @see
|
|
159 |
* org.apache.ant.antunit.listener.BaseAntUnitListener#startTestSuite(org
|
|
160 |
* .apache.tools.ant.Project, java.lang.String)
|
|
161 |
*/
|
|
162 |
public void startTestSuite(Project testProject, String buildFile) {
|
|
163 |
inner = new StringWriter();
|
|
164 |
wri = new PrintWriter(inner);
|
|
165 |
out = getOut(buildFile);
|
|
166 |
addTargets(testProject);
|
|
167 |
addMacros(testProject);
|
|
168 |
addScriptDefs(testProject);
|
|
169 |
}
|
|
170 |
|
|
171 |
/*
|
|
172 |
* (non-Javadoc)
|
|
173 |
*
|
|
174 |
* @see
|
|
175 |
* org.apache.ant.antunit.listener.BaseAntUnitListener#startTest(java.lang
|
|
176 |
* .String)
|
|
177 |
*/
|
|
178 |
public void startTest(String target) {
|
|
179 |
|
|
180 |
}
|
|
181 |
|
|
182 |
/**
|
|
183 |
* @param outputFile
|
|
184 |
* the outputFile to set
|
|
185 |
*/
|
|
186 |
public void setOutputFile(File outputFile) {
|
|
187 |
this.outputFile = outputFile;
|
|
188 |
}
|
|
189 |
|
|
190 |
/**
|
|
191 |
* Returns the output file path.
|
|
192 |
*
|
|
193 |
* @return
|
|
194 |
*/
|
|
195 |
public File getOutputFile() {
|
|
196 |
return outputFile;
|
|
197 |
}
|
|
198 |
|
|
199 |
/**
|
|
200 |
* To add Ant targets.
|
|
201 |
*
|
|
202 |
* @param project
|
|
203 |
*/
|
|
204 |
@SuppressWarnings("unchecked")
|
|
205 |
private void addTargets(Project project) {
|
|
206 |
Hashtable<String, Target> projectTargets = project.getTargets();
|
588
|
207 |
Enumeration<String> targetEnum = projectTargets.keys();
|
|
208 |
while (targetEnum.hasMoreElements()) {
|
628
|
209 |
String key = targetEnum.nextElement();
|
587
|
210 |
if (!isAntUnitTestTarget(key) && key.length() > 0) {
|
|
211 |
antTargets.add((Target) projectTargets.get(key));
|
|
212 |
}
|
|
213 |
}
|
|
214 |
}
|
|
215 |
|
|
216 |
/**
|
|
217 |
* To add Ant macros.
|
|
218 |
*
|
|
219 |
* @param project
|
|
220 |
*/
|
|
221 |
@SuppressWarnings("unchecked")
|
|
222 |
private void addMacros(Project project) {
|
|
223 |
Hashtable<String, Class> projectMacros = project.getTaskDefinitions();
|
588
|
224 |
Enumeration<String> macrosEnum = projectMacros.keys();
|
|
225 |
while (macrosEnum.hasMoreElements()) {
|
|
226 |
String key = macrosEnum.nextElement();
|
587
|
227 |
Class newTask = projectMacros.get(key);
|
|
228 |
if (newTask.getName().equals(
|
|
229 |
"org.apache.tools.ant.taskdefs.MacroInstance")) {
|
|
230 |
String macroName = ProjectHelper
|
|
231 |
.extractNameFromComponentName(key);
|
|
232 |
if (macroName != null && !macroName.contains("assert")) {
|
|
233 |
antMacro.add(macroName);
|
|
234 |
}
|
|
235 |
}
|
|
236 |
}
|
|
237 |
}
|
|
238 |
|
|
239 |
/**
|
|
240 |
* To add Ant scriptdefs.
|
|
241 |
*
|
|
242 |
* @param project
|
|
243 |
*/
|
|
244 |
@SuppressWarnings("unchecked")
|
|
245 |
private void addScriptDefs(Project project) {
|
|
246 |
Hashtable<String, Class> projectMacros = project.getTaskDefinitions();
|
588
|
247 |
Enumeration<String> scriptDefsEnum = projectMacros.keys();
|
|
248 |
while (scriptDefsEnum.hasMoreElements()) {
|
|
249 |
String key = scriptDefsEnum.nextElement();
|
587
|
250 |
Class newTask = projectMacros.get(key);
|
|
251 |
if (newTask
|
|
252 |
.getName()
|
|
253 |
.equals(
|
|
254 |
"org.apache.tools.ant.taskdefs.optional.script.ScriptDefBase")) {
|
|
255 |
String scriptdefName = ProjectHelper
|
|
256 |
.extractNameFromComponentName(key);
|
|
257 |
if (scriptdefName != null) {
|
|
258 |
antScriptDef.add(scriptdefName);
|
|
259 |
}
|
|
260 |
}
|
|
261 |
}
|
|
262 |
}
|
|
263 |
|
|
264 |
/**
|
|
265 |
* To get the Target covered percentage.
|
|
266 |
*
|
|
267 |
* @param measure
|
|
268 |
* @return
|
|
269 |
*/
|
|
270 |
private float getTargetCoverage() {
|
|
271 |
if (antTargets.getCount() == 0) {
|
|
272 |
return 100;
|
|
273 |
} else {
|
|
274 |
return ((float)antTargets.getExecutedCount() / (float)antTargets
|
|
275 |
.getCount()) * 100;
|
|
276 |
}
|
|
277 |
}
|
|
278 |
|
|
279 |
/**
|
|
280 |
* To get Macros covered percentage.
|
|
281 |
*
|
|
282 |
* @param measure
|
|
283 |
* @return
|
|
284 |
*/
|
|
285 |
private float getMacroCoverage() {
|
|
286 |
if (antMacro.getCount() == 0) {
|
|
287 |
return 100;
|
|
288 |
} else {
|
|
289 |
return ((float)antMacro.getExecutedCount() / (float)antMacro.getCount()) * 100;
|
|
290 |
}
|
|
291 |
}
|
|
292 |
|
|
293 |
/**
|
|
294 |
* To get ScriptDefs covered percentage.
|
|
295 |
*
|
|
296 |
* @param measure
|
|
297 |
* @return
|
|
298 |
*/
|
|
299 |
private float getScriptDefCoverage() {
|
|
300 |
if (antScriptDef.getCount() == 0) {
|
|
301 |
return 100;
|
|
302 |
} else {
|
|
303 |
return ((float)antScriptDef.getExecutedCount() / (float)antScriptDef
|
|
304 |
.getCount()) * 100;
|
|
305 |
}
|
|
306 |
}
|
|
307 |
|
|
308 |
/**
|
|
309 |
* Listener for collecting the targets/tasks information. This listener will
|
|
310 |
* collect from the targets which actually run by test projects.
|
|
311 |
*
|
|
312 |
*/
|
|
313 |
private class AntListener implements BuildListener {
|
|
314 |
|
|
315 |
@Override
|
|
316 |
public void buildFinished(BuildEvent event) {
|
|
317 |
}
|
|
318 |
|
|
319 |
@Override
|
|
320 |
public void buildStarted(BuildEvent event) {
|
|
321 |
}
|
|
322 |
|
|
323 |
@Override
|
|
324 |
public void messageLogged(BuildEvent event) {
|
|
325 |
}
|
|
326 |
|
|
327 |
@Override
|
|
328 |
public void targetFinished(BuildEvent event) {
|
|
329 |
}
|
|
330 |
|
|
331 |
@Override
|
|
332 |
public void targetStarted(BuildEvent event) {
|
|
333 |
String targetName = event.getTarget().getName();
|
|
334 |
if (!isAntUnitTestTarget(targetName) && targetName.length() > 0) {
|
|
335 |
antTargets.markAsExecuted(targetName);
|
|
336 |
}
|
|
337 |
|
|
338 |
}
|
|
339 |
|
|
340 |
@Override
|
|
341 |
public void taskFinished(BuildEvent event) {
|
|
342 |
|
|
343 |
}
|
|
344 |
|
|
345 |
@Override
|
|
346 |
public void taskStarted(BuildEvent event) {
|
|
347 |
// macro and scriptdef are creating UnknownElement type of tasks
|
|
348 |
if (event.getTask().getTaskType() != null) {
|
|
349 |
String taskName = ProjectHelper
|
|
350 |
.extractNameFromComponentName(event.getTask()
|
|
351 |
.getTaskType());
|
|
352 |
if (taskName != null) {
|
|
353 |
antMacro.markAsExecuted(taskName);
|
|
354 |
antScriptDef.markAsExecuted(taskName);
|
|
355 |
}
|
|
356 |
}
|
|
357 |
}
|
|
358 |
}
|
|
359 |
|
|
360 |
/*
|
|
361 |
* (non-Javadoc)
|
|
362 |
*
|
|
363 |
* @see
|
|
364 |
* org.apache.ant.antunit.listener.BaseAntUnitListener#setCurrentTestProject
|
|
365 |
* (org.apache.tools.ant.Project)
|
|
366 |
*/
|
|
367 |
@Override
|
|
368 |
public void setCurrentTestProject(Project p) {
|
|
369 |
p.addBuildListener(new AntListener());
|
|
370 |
}
|
|
371 |
|
|
372 |
/**
|
|
373 |
* Check is the target is a special name for AntUnit.
|
|
374 |
* @param targetName
|
|
375 |
* @return
|
|
376 |
*/
|
|
377 |
public static boolean isAntUnitTestTarget(String targetName) {
|
|
378 |
if (targetName == null) {
|
|
379 |
return false;
|
|
380 |
}
|
|
381 |
if ((targetName.startsWith("test") && !targetName.equals("test"))
|
|
382 |
|| targetName.equals("setUp") || targetName.equals("tearDown")) {
|
|
383 |
return true;
|
|
384 |
}
|
|
385 |
return false;
|
|
386 |
}
|
|
387 |
}
|