Package: ResultTestCase

ResultTestCase

nameinstructionbranchcomplexitylinemethod
ResultTestCase()
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%
ResultTestCase(File)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
ResultTestCase(File, String, String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
checkMessages(String, List, List)
M: 28 C: 53
65%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 4 C: 12
75%
M: 0 C: 1
100%
compareResults(Result, String)
M: 35 C: 100
74%
M: 6 C: 6
50%
M: 6 C: 1
14%
M: 11 C: 13
54%
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%

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.File;
25: import java.util.List;
26: import java.util.Set;
27: import java.util.Vector;
28:
29: import javax.xml.parsers.ParserConfigurationException;
30: import javax.xml.transform.TransformerException;
31:
32: import org.overture.test.framework.results.IMessage;
33: import org.overture.test.framework.results.IResultCombiner;
34: import org.overture.test.framework.results.Result;
35: import org.overture.test.util.XmlResultReaderWriter;
36: import org.overture.test.util.XmlResultReaderWriter.IResultStore;
37:
38: public abstract class ResultTestCase<R> extends BaseTestCase implements
39:                 IResultStore<R>
40: {
41:         public ResultTestCase()
42:         {
43:                 super();
44:         }
45:
46:         public ResultTestCase(File file)
47:         {
48:                 super(file);
49:         }
50:
51:         public ResultTestCase(File rootSource, String name, String content)
52:         {
53:                 super(rootSource, name, content);
54:         }
55:
56:         protected void compareResults(Result<R> result, String filename)
57:         {
58:•                if (Properties.recordTestResults)
59:                 {
60:                         // MessageReaderWriter mrw = new MessageReaderWriter(createResultFile(filename));
61:                         // mrw.set(result);
62:                         // mrw.save();
63:                         File resultFile = createResultFile(filename);
64:                         resultFile.getParentFile().mkdirs();
65:                         XmlResultReaderWriter<R> xmlResult = new XmlResultReaderWriter<R>(resultFile, this);
66:                         xmlResult.setResult(this.getClass().getName(), result);
67:                         try
68:                         {
69:                                 xmlResult.saveInXml();
70:                         } catch (ParserConfigurationException e)
71:                         {
72:                                 // TODO Auto-generated catch block
73:                                 e.printStackTrace();
74:                         } catch (TransformerException e)
75:                         {
76:                                 // TODO Auto-generated catch block
77:                                 e.printStackTrace();
78:                         }
79:
80:                         return;
81:                 }
82:
83:                 File file = getResultFile(filename);
84:
85:                 assertNotNull("Result file " + file.getName() + " was not found", file);
86:                 assertTrue("Result file " + file.getAbsolutePath() + " does not exist", file.exists());
87:
88:                 // MessageReaderWriter mrw = new MessageReaderWriter(file);
89:                 XmlResultReaderWriter<R> xmlResult = new XmlResultReaderWriter<R>(file, this);
90:                 boolean parsed = xmlResult.loadFromXml();
91:
92:                 assertTrue("Could not read result file: " + file.getName(), parsed);
93:
94:•                if (parsed)
95:                 {
96:                         boolean errorsFound = checkMessages("warning", xmlResult.getWarnings(), result.warnings);
97:•                        errorsFound = checkMessages("error", xmlResult.getErrors(), result.errors)
98:                                         || errorsFound;
99:•                        errorsFound = !assertEqualResults(xmlResult.getResult().result, result.result)
100:                                         || errorsFound;
101:                         assertFalse("Errors found in file \"" + filename + "\"", errorsFound);
102:                 }
103:         }
104:
105:         /**
106:          * Checks if the results are equal.
107:          *
108:          * @param expected
109:          * The expected result
110:          * @param actual
111:          * The actual result
112:          * @return If equal true or check has to be ignored true is returned else false
113:          */
114:         protected abstract boolean assertEqualResults(R expected, R actual);
115:
116:         protected abstract File createResultFile(String filename);
117:
118:         protected abstract File getResultFile(String filename);
119:
120:         public boolean checkMessages(String typeName, List<IMessage> expectedList,
121:                         List<IMessage> list)
122:         {
123:                 String TypeName = typeName.toUpperCase().toCharArray()[0]
124:                                 + typeName.substring(1);
125:                 boolean errorFound = false;
126:•                for (IMessage w : list)
127:                 {
128:                         boolean isContainedIn = containedIn(expectedList, w);
129:•                        if (!isContainedIn)
130:                         {
131:                                 System.out.println(TypeName + " not expected: " + w);
132:                                 errorFound = true;
133:                         }
134:
135:                         // assertTrue(TypeName + " not expected: " + w, isContainedIn);
136:                 }
137:•                for (IMessage w : expectedList)
138:                 {
139:                         boolean isContainedIn = containedIn(list, w);
140:•                        if (!isContainedIn)
141:                         {
142:                                 System.out.println(TypeName + " expected but not found: " + w);
143:                                 errorFound = true;
144:                         }
145:                         // assertTrue(TypeName + " expected but not found: " + w, isContainedIn);
146:                 }
147:                 return errorFound;
148:         }
149:
150:         private static boolean containedIn(List<IMessage> list, IMessage m)
151:         {
152:•                for (IMessage m1 : list)
153:                 {
154:•                        if (m1.equals(m))
155:                         {
156:                                 return true;
157:                         }
158:                 }
159:                 return false;
160:         }
161:
162:         protected <T> Result<T> mergeResults(Set<? extends Result<T>> parse,
163:                         IResultCombiner<T> c)
164:         {
165:                 List<IMessage> warnings = new Vector<IMessage>();
166:                 List<IMessage> errors = new Vector<IMessage>();
167:                 T result = null;
168:
169:•                for (Result<T> r : parse)
170:                 {
171:                         warnings.addAll(r.warnings);
172:                         errors.addAll(r.errors);
173:                         result = c.combine(result, r.result);
174:                 }
175:                 return new Result<T>(result, warnings, errors);
176:         }
177:
178: }