Package: SourceFile

SourceFile

nameinstructionbranchcomplexitylinemethod
SourceFile(File)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
SourceFile(File, String)
M: 39 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
detab(String, int)
M: 51 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
getCount()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getLine(int)
M: 18 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
htmlLine()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
htmlQuote(String)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
markupHTML(String, List)
M: 90 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
printCoverage(PrintWriter)
M: 149 C: 0
0%
M: 22 C: 0
0%
M: 12 C: 0
0%
M: 27 C: 0
0%
M: 1 C: 0
0%
printSource(PrintWriter)
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
printWordCoverage(PrintWriter)
M: 329 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 62 C: 0
0%
M: 1 C: 0
0%
rowHTML(boolean, String, String, String, String)
M: 169 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
writeCoverage(PrintWriter)
M: 45 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: *
3: *        Copyright (C) 2008 Fujitsu Services Ltd.
4: *
5: *        Author: Nick Battle
6: *
7: *        This file is part of VDMJ.
8: *
9: *        VDMJ is free software: you can redistribute it and/or modify
10: *        it under the terms of the GNU General Public License as published by
11: *        the Free Software Foundation, either version 3 of the License, or
12: *        (at your option) any later version.
13: *
14: *        VDMJ is distributed in the hope that it will be useful,
15: *        but WITHOUT ANY WARRANTY; without even the implied warranty of
16: *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: *        GNU General Public License for more details.
18: *
19: *        You should have received a copy of the GNU General Public License
20: *        along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21: *
22: ******************************************************************************/
23:
24: package org.overture.interpreter.runtime;
25:
26: import java.io.BufferedReader;
27: import java.io.File;
28: import java.io.IOException;
29: import java.io.PrintWriter;
30: import java.util.Collections;
31: import java.util.List;
32: import java.util.Map;
33: import java.util.Vector;
34:
35: import org.overture.ast.intf.lex.ILexLocation;
36: import org.overture.ast.intf.lex.ILexNameToken;
37: import org.overture.ast.lex.CoverageUtil;
38: import org.overture.ast.lex.LexLocation;
39: import org.overture.ast.lex.LexNameList;
40: import org.overture.interpreter.VDMJ;
41: import org.overture.parser.config.Properties;
42: import org.overture.parser.lex.BacktrackInputReader;
43:
44: /**
45: * A class to hold a source file for source debug output.
46: */
47:
48: public class SourceFile
49: {
50:         public final File filename;
51:         public final String charset;
52:         public List<String> lines = new Vector<String>();
53:
54:         private final static String HTMLSTART = "<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt; font-family:\"Courier New\"; color:black'>";
55:         private final static String HTMLEND = "</span></p>";
56:
57:         public SourceFile(File filename) throws IOException
58:         {
59:                 this(filename, VDMJ.filecharset);
60:         }
61:
62:         public SourceFile(File filename, String charset) throws IOException
63:         {
64:                 this.filename = filename;
65:                 this.charset = charset;
66:                 BufferedReader br = new BufferedReader(new BacktrackInputReader(filename, charset));
67:
68:                 String line = br.readLine();
69:
70:•                while (line != null)
71:                 {
72:                         lines.add(line);
73:                         line = br.readLine();
74:                 }
75:
76:                 br.close();
77:         }
78:
79:         public String getLine(int n)
80:         {
81:•                if (n < 1 || n > lines.size())
82:                 {
83:                         return "~";
84:                 }
85:
86:                 return lines.get(n - 1);
87:         }
88:
89:         public int getCount()
90:         {
91:                 return lines.size();
92:         }
93:
94:         public void printSource(PrintWriter out)
95:         {
96:•                for (String line : lines)
97:                 {
98:                         out.println(line);
99:                 }
100:         }
101:
102:         public void printCoverage(PrintWriter out)
103:         {
104:                 List<Integer> hitlist = new CoverageUtil(LexLocation.getAllLocations(),LexLocation.getNameSpans()).getHitList(filename);
105:                 List<Integer> srclist = LexLocation.getSourceList(filename);
106:
107:                 int hitcount = 0;
108:                 int srccount = 0;
109:                 boolean supress = false;
110:
111:                 out.println("Test coverage for " + filename + ":\n");
112:
113:•                for (int lnum = 1; lnum <= lines.size(); lnum++)
114:                 {
115:                         String line = lines.get(lnum - 1);
116:
117:                         // TODO remove this filtering it can never happen because of the new scanner
118:•                        if (line.startsWith("\\begin{vdm_al}"))
119:                         {
120:                                 supress = false;
121:                                 continue;
122:•                        } else if (line.startsWith("\\end{vdm_al}")
123:•                                        || line.startsWith("\\section")
124:•                                        || line.startsWith("\\subsection")
125:•                                        || line.startsWith("\\document") || line.startsWith("%"))
126:                         {
127:                                 supress = true;
128:                                 continue;
129:                         }
130:
131:•                        if (srclist.contains(lnum))
132:                         {
133:                                 srccount++;
134:
135:•                                if (hitlist.contains(lnum))
136:                                 {
137:                                         out.println("+ " + line);
138:                                         hitcount++;
139:                                 } else
140:                                 {
141:                                         out.println("- " + line);
142:                                 }
143:                         } else
144:                         {
145:•                                if (!supress)
146:                                 {
147:                                         out.println(" " + line);
148:                                 }
149:                         }
150:                 }
151:
152:•                out.println("\nCoverage = "
153:                                 + (srccount == 0 ? 0
154:                                                 : (float) (1000 * hitcount / srccount) / 10) + "%");
155:         }
156:
157:         public void printWordCoverage(PrintWriter out)
158:         {
159:                 CoverageUtil coverageUtil = new CoverageUtil(LexLocation.getAllLocations(),LexLocation.getNameSpans());
160:                 Map<Integer, List<ILexLocation>> hits = coverageUtil.getMissLocations(filename);
161:
162:                 out.println("<html>");
163:                 out.println("<head>");
164:                 out.println("<meta http-equiv=Content-Type content=\"text/html; charset="
165:                                 + VDMJ.filecharset + "\">");
166:                 out.println("<meta name=Generator content=\"Microsoft Word 11 (filtered)\">");
167:                 out.println("<title>" + filename.getName() + "</title>");
168:                 out.println("<style>");
169:                 out.println("<!--");
170:                 out.println("p.MsoNormal, li.MsoNormal, div.MsoNormal");
171:                 out.println("{margin:0in; margin-bottom:.0001pt; font-size:12.0pt; font-family:\"Times New Roman\";}");
172:                 out.println("h1");
173:                 out.println("{margin-top:12.0pt; margin-right:0in; margin-bottom:3.0pt; margin-left:0in; page-break-after:avoid; font-size:16.0pt; font-family:Arial;}");
174:                 out.println("@page Section1");
175:                 out.println("{size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in;}");
176:                 out.println("div.Section1");
177:                 out.println("{page:Section1;}");
178:                 out.println("-->");
179:                 out.println("</style>");
180:                 out.println("</head>");
181:                 out.println("<body lang=EN-GB>");
182:                 out.println("<div class=Section1>");
183:
184:                 out.println("<h1 align=center style='text-align:center'>"
185:                                 + filename.getName() + "</h1>");
186:                 out.println(htmlLine());
187:                 out.println(htmlLine());
188:
189:                 LexNameList spans = coverageUtil.getSpanNames(filename);
190:
191:•                for (int lnum = 1; lnum <= lines.size(); lnum++)
192:                 {
193:•                        for (ILexNameToken name : spans)
194:                         {
195:•                                if (name.getLocation().getStartLine() == lnum)
196:                                 {
197:                                         out.println("<a name=\"" + name.getName() + ":"
198:                                                         + name.getLocation().getStartLine() + "\" />");
199:
200:                                 }
201:                         }
202:
203:                         String line = lines.get(lnum - 1);
204:                         String spaced = detab(line, Properties.parser_tabstop);
205:                         List<ILexLocation> list = hits.get(lnum);
206:                         out.println(markupHTML(spaced, list));
207:                 }
208:
209:                 out.println(htmlLine());
210:                 out.println(htmlLine());
211:                 out.println(htmlLine());
212:
213:                 out.println("<div align=center>");
214:                 out.println("<table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width=\"60%\" style='width:60.0%;border-collapse:collapse'>");
215:                 out.println(rowHTML(true, "Function or Operation", "Line", "Coverage", "Calls"));
216:
217:                 long total = 0;
218:
219:                 Collections.sort(spans);
220:
221:•                for (ILexNameToken name : spans)
222:                 {
223:                         long calls = coverageUtil.getSpanCalls(name);
224:                         total += calls;
225:
226:                         out.println(rowHTML(false, "<a href=\"#" + name.getName() + ":"
227:                                         + name.getLocation().getStartLine() + "\">"
228:                                         + htmlQuote(name.toString()) + "</a>", name.getLocation().getStartLine()
229:                                         + "", Float.toString(coverageUtil.getSpanPercent(name))
230:                                         + "%", Long.toString(calls)));
231:                 }
232:
233:                 out.println(rowHTML(true, htmlQuote(filename.getName()), "", Float.toString(coverageUtil.getHitPercent(filename))
234:                                 + "%", Long.toString(total)));
235:
236:                 out.println("</table>");
237:                 out.println("</div>");
238:                 out.println("</div>");
239:                 out.println("</body>");
240:                 out.println("</html>");
241:         }
242:
243:         private String htmlLine()
244:         {
245:                 return "<p class=MsoNormal> </p>";
246:         }
247:
248:         private String rowHTML(boolean emph, String name, String lineNumber,
249:                         String coverage, String calls)
250:         {
251:                 StringBuilder sb = new StringBuilder();
252:•                String b1 = emph ? "<b>" : "";
253:•                String b2 = emph ? "</b>" : "";
254:•                String bg = emph ? "background:#D9D9D9;" : "";
255:
256:                 sb.append("<tr>\n");
257:
258:                 sb.append("<td width=\"50%\" valign=top style='width:50.0%;border:solid windowtext 1.0pt;"
259:                                 + bg + "padding:0in 0in 0in 0in'>\n");
260:                 sb.append("<p class=MsoNormal>" + b1 + name + b2 + "</p>\n");
261:                 sb.append("</td>\n");
262:
263:                 sb.append("<td width=\"10%\" valign=top style='width:25.0%;border:solid windowtext 1.0pt;"
264:                                 + bg + "padding:0in 0in 0in 0in'>\n");
265:                 sb.append("<p class=MsoNormal align=right style='text-align:right'>"
266:                                 + b1 + lineNumber + b2 + "</p>\n");
267:                 sb.append("</td>\n");
268:
269:                 sb.append("<td width=\"20%\" valign=top style='width:25.0%;border:solid windowtext 1.0pt;"
270:                                 + bg + "padding:0in 0in 0in 0in'>\n");
271:                 sb.append("<p class=MsoNormal align=right style='text-align:right'>"
272:                                 + b1 + coverage + b2 + "</p>\n");
273:                 sb.append("</td>\n");
274:
275:                 sb.append("<td width=\"20%\" valign=top style='width:25.0%;border:solid windowtext 1.0pt;"
276:                                 + bg + "padding:0in 0in 0in 0in'>\n");
277:                 sb.append("<p class=MsoNormal align=right style='text-align:right'>"
278:                                 + b1 + calls + b2 + "</p>\n");
279:                 sb.append("</td>\n");
280:
281:                 sb.append("</tr>\n");
282:
283:                 return sb.toString();
284:         }
285:
286:         private String markupHTML(String line, List<ILexLocation> list)
287:         {
288:•                if (line.isEmpty())
289:                 {
290:                         return htmlLine();
291:                 }
292:
293:                 StringBuilder sb = new StringBuilder(HTMLSTART);
294:                 int p = 0;
295:
296:•                if (list != null)
297:                 {
298:•                        for (ILexLocation m : list)
299:                         {
300:                                 int start = m.getStartPos() - 1;
301:•                                int end = m.getStartLine() == m.getEndLine() ? m.getEndPos() - 1
302:                                                 : line.length();
303:
304:•                                if (start >= p) // Backtracker produces duplicate tokens
305:                                 {
306:                                         sb.append(htmlQuote(line.substring(p, start)));
307:                                         sb.append("<span style='color:red'>");
308:                                         sb.append(htmlQuote(line.substring(start, end)));
309:                                         sb.append("</span>");
310:
311:                                         p = end;
312:                                 }
313:                         }
314:                 }
315:
316:                 sb.append(htmlQuote(line.substring(p)));
317:                 sb.append(HTMLEND);
318:                 return sb.toString();
319:         }
320:
321:         private String htmlQuote(String s)
322:         {
323:                 return s.replaceAll("&", "&").replaceAll(" ", " ").replaceAll("<", "<").replaceAll(">", ">");
324:         }
325:
326:         protected static String detab(String s, int tabstop)
327:         {
328:                 StringBuilder sb = new StringBuilder();
329:                 int p = 0;
330:
331:•                for (int i = 0; i < s.length(); i++)
332:                 {
333:                         char c = s.charAt(i);
334:
335:•                        if (c == '\t')
336:                         {
337:                                 int n = tabstop - p % tabstop;
338:
339:•                                for (int x = 0; x < n; x++)
340:                                 {
341:                                         sb.append(' ');
342:                                 }
343:
344:                                 p += n;
345:                         } else
346:                         {
347:                                 sb.append(c);
348:                                 p++;
349:                         }
350:                 }
351:
352:                 return sb.toString();
353:         }
354:
355:         public void writeCoverage(PrintWriter out)
356:         {
357:•                for (LexLocation l : LexLocation.getSourceLocations(filename))
358:                 {
359:•                        if (l.hits > 0)
360:                         {
361:                                 out.println("+" + l.startLine + " " + l.startPos + "-"
362:                                                 + l.endPos + "=" + l.hits);
363:                         }
364:                 }
365:         }
366: }