Package: ResultTestCase4

ResultTestCase4

nameinstructionbranchcomplexitylinemethod
ResultTestCase4()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
ResultTestCase4(File)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
checkMessages(String, List, List, PrintWriter)
M: 48 C: 53
52%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 4 C: 12
75%
M: 0 C: 1
100%
compareResults(Result, String)
M: 43 C: 137
76%
M: 7 C: 7
50%
M: 7 C: 1
13%
M: 12 C: 22
65%
M: 0 C: 1
100%
containedIn(List, IMessage)
M: 2 C: 17
89%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 4
80%
M: 0 C: 1
100%
mergeResults(Set, IResultCombiner)
M: 44 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
padRight(String, int)
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * Test Framework for Overture
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.test.framework;
23:
24: import java.io.ByteArrayOutputStream;
25: import java.io.File;
26: import java.io.PrintWriter;
27: import java.util.List;
28: import java.util.Set;
29: import java.util.Vector;
30:
31: import javax.xml.parsers.ParserConfigurationException;
32: import javax.xml.transform.TransformerException;
33:
34: import org.junit.Assert;
35: import org.overture.test.framework.results.IMessage;
36: import org.overture.test.framework.results.IResultCombiner;
37: import org.overture.test.framework.results.Result;
38: import org.overture.test.util.XmlResultReaderWriter;
39: import org.overture.test.util.XmlResultReaderWriter.IResultStore;
40:
41: public abstract class ResultTestCase4<R> implements IResultStore<R>
42: {
43:         protected File file;
44:
45:         public ResultTestCase4()
46:         {
47:                 super();
48:         }
49:
50:         public ResultTestCase4(File file)
51:         {
52:                 this.file = file;
53:         }
54:
55:         protected void compareResults(Result<R> result, String filename)
56:         {
57:•                if (Properties.recordTestResults)
58:                 {
59:                         File resultFile = createResultFile(filename);
60:                         resultFile.getParentFile().mkdirs();
61:                         XmlResultReaderWriter<R> xmlResult = new XmlResultReaderWriter<R>(resultFile, this);
62:                         xmlResult.setResult(this.getClass().getName(), result);
63:                         try
64:                         {
65:                                 xmlResult.saveInXml();
66:                         } catch (ParserConfigurationException e)
67:                         {
68:                                 throw new RuntimeException("Failed to encode recorded test result xml", e);
69:                         } catch (TransformerException e)
70:                         {
71:                                 throw new RuntimeException("Failed to transform recorded test result xml", e);
72:                         }
73:
74:                         return;
75:                 }
76:
77:                 File file = getResultFile(filename);
78:
79:                 Assert.assertNotNull("Result file " + filename + " was not found", file);
80:                 Assert.assertTrue("The result files does not exist: " + file.getPath()
81:                                 + "\n\n Cannot compare result:\n " + result, file.exists());
82:•                if (!file.exists())
83:                 {
84:                         // Assume doesn't always work.
85:                         return;
86:                 }
87:                 Assert.assertTrue("Result file " + file.getAbsolutePath()
88:                                 + " does not exist", file.exists());
89:
90:                 // MessageReaderWriter mrw = new MessageReaderWriter(file);
91:                 XmlResultReaderWriter<R> xmlResult = new XmlResultReaderWriter<R>(file, this);
92:                 boolean parsed = xmlResult.loadFromXml();
93:
94:                 Assert.assertTrue("Could not read result file: " + file.getName(), parsed);
95:
96:•                if (parsed)
97:                 {
98:                         ByteArrayOutputStream os = new ByteArrayOutputStream();
99:                         PrintWriter pw = new PrintWriter(os);
100:                         boolean errorsFound = checkMessages("warning", xmlResult.getWarnings(), result.warnings, pw);
101:•                        errorsFound = checkMessages("error", xmlResult.getErrors(), result.errors, pw)
102:                                         || errorsFound;
103:•                        errorsFound = !assertEqualResults(xmlResult.getResult().result, result.result, pw)
104:                                         || errorsFound;
105:                         pw.flush();
106:                         pw.close();
107:                         Assert.assertFalse("Errors found in file \"" + filename + "\"\n\n"
108:                                         + os.toString(), errorsFound);
109:                 }
110:         }
111:
112:         /**
113:          * Checks if the results are equal.
114:          *
115:          * @param expected
116:          * The expected result
117:          * @param actual
118:          * The actual result
119:          * @return If equal true or check has to be ignored true is returned else false
120:          */
121:         protected abstract boolean assertEqualResults(R expected, R actual,
122:                         PrintWriter out);
123:
124:         protected abstract File createResultFile(String filename);
125:
126:         protected abstract File getResultFile(String filename);
127:
128:         public boolean checkMessages(String typeName, List<IMessage> expectedList,
129:                         List<IMessage> list, PrintWriter out)
130:         {
131:                 String TypeName = typeName.toUpperCase().toCharArray()[0]
132:                                 + typeName.substring(1);
133:                 boolean errorFound = false;
134:•                for (IMessage w : list)
135:                 {
136:                         boolean isContainedIn = containedIn(expectedList, w);
137:•                        if (!isContainedIn)
138:                         {
139:                                 out.println(padRight("Unexpected " + TypeName + ": ", 20) + w);
140:                                 errorFound = true;
141:                         }
142:
143:                         // assertTrue(TypeName + " not expected: " + w, isContainedIn);
144:                 }
145:•                for (IMessage w : expectedList)
146:                 {
147:                         boolean isContainedIn = containedIn(list, w);
148:•                        if (!isContainedIn)
149:                         {
150:                                 out.println(padRight("Missing " + TypeName + ": ", 20) + w);
151:                                 errorFound = true;
152:                         }
153:                         // assertTrue(TypeName + " expected but not found: " + w, isContainedIn);
154:                 }
155:                 return errorFound;
156:         }
157:
158:         private static boolean containedIn(List<IMessage> list, IMessage m)
159:         {
160:•                for (IMessage m1 : list)
161:                 {
162:•                        if (m1.equals(m))
163:                         {
164:                                 return true;
165:                         }
166:                 }
167:                 return false;
168:         }
169:
170:         private String padRight(String text, int length)
171:         {
172:•                while (text.length() < length)
173:                 {
174:                         text += " ";
175:                 }
176:                 return text;
177:         }
178:
179:         protected <T> Result<T> mergeResults(Set<? extends Result<T>> parse,
180:                         IResultCombiner<T> c)
181:         {
182:                 List<IMessage> warnings = new Vector<IMessage>();
183:                 List<IMessage> errors = new Vector<IMessage>();
184:                 T result = null;
185:
186:•                for (Result<T> r : parse)
187:                 {
188:                         warnings.addAll(r.warnings);
189:                         errors.addAll(r.errors);
190:                         result = c.combine(result, r.result);
191:                 }
192:                 return new Result<T>(result, warnings, errors);
193:         }
194:
195: }