Package: AbsResultTest

AbsResultTest

nameinstructionbranchcomplexitylinemethod
AbsResultTest()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
checkAssumptions()
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
compareResults(Object, Object)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
deSerializeResult(String)
M: 9 C: 37
80%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 8
80%
M: 0 C: 1
100%
getTestResultUpdateMessage()
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
testInfo()
M: 0 C: 47
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
testUpdate(Object)
M: 30 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
updateCheck()
M: 4 C: 21
84%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 4
67%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * Overture Testing Framework
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.core.testing;
23:
24: import java.io.File;
25: import java.io.FileInputStream;
26: import java.io.FileNotFoundException;
27: import java.io.FileOutputStream;
28: import java.io.IOException;
29: import java.io.InputStreamReader;
30: import java.lang.reflect.Type;
31:
32: import org.apache.commons.io.IOUtils;
33: import org.junit.Assert;
34: import org.overture.parser.lex.LexException;
35: import org.overture.parser.syntax.ParserException;
36:
37: import com.google.gson.Gson;
38: import com.google.gson.reflect.TypeToken;
39:
40: /**
41: * Top level class for new testing framework. Provides common result handling code to all other test classes. This class
42: * should <b>not</b> be subclassed directly. Use one of its existing subclasses instead. Test results are always stored
43: * with UTF-8 encoding.
44: *
45: * @param <R> the (user-provided) type of results this test operates on
46: * @author ldc
47: * @see ParamExternalsTest
48: * @see ParamFineGrainTest
49: * @see ParamStandardTest
50: */
51: public abstract class AbsResultTest<R> {
52:
53: protected boolean updateResult;
54: protected String resultPath;
55: protected String testName;
56: protected String modelPath;
57:
58: /**
59: * Deserialize test results. This method is capable of deserializing most results, provided the correct type
60: * information is provided via getResultType(). If your results are too complex for this method or if you are not
61: * using JSON to store then, them you must override the entire method.
62: *
63: * @param resultPath the file path to the stored result file
64: * @return the deserialized stored result
65: * @throws IOException
66: * @throws FileNotFoundException
67: */
68: public R deSerializeResult(String resultPath) throws FileNotFoundException,
69: IOException {
70: Gson gson = new Gson();
71: Type resultType = getResultType();
72:
73: // check if exists
74: File f = new File(resultPath);
75:• if (!f.exists()) {
76: f.getParentFile().mkdirs();
77: throw new FileNotFoundException(resultPath);
78: }
79:
80: InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(resultPath)), ParseTcFacade.UTF8);
81: String json = IOUtils.toString(reader);
82: R result = gson.fromJson(json, resultType);
83: return result;
84: }
85:
86: /**
87: * Calculate the type of the test result. This method must be overridden to provide the specific result type for
88: * each test. When doing so, you can use the snippet below (replacing <code>R</code> with the actual type of your
89: * result). Keep in mind this does not work for wildcards or type parameters. You <b>must</b> declare the actual
90: * type. <pre>{@code
91: * Type resultType = new TypeToken< R >() {}.getType(); <br>
92: * return resultType;
93: * }</pre>
94: *
95: * @return the {@link Type} of the result file
96: * @see TypeToken
97: */
98: abstract public Type getResultType();
99:
100: /**
101: * Return the Java System property to update this set of testing. Should have the following naming scheme:
102: * <code>testing.update.[maven-module].[test-class].[testcase/input-file]>. <br>
103: * <br>
104: * The test ID <b>must</b> be unique to each test.
105: * the module is enough.
106: *
107: * @return the test Update Property, as a {@link String}
108: */
109: protected abstract String getUpdatePropertyString();
110:
111: /**
112: * Returns a message on how to update test results. Should be used in {@link #compareResults(Object, Object)}. * @return
113: * the result update message.
114: * <p/>>
115: * This method is deprecated. Use testInfo instead.
116: */
117: @Deprecated
118: protected String getTestResultUpdateMessage() {
119: StringBuilder sb = new StringBuilder();
120: sb.append("Use -D\"");
121: sb.append(getUpdatePropertyString());
122: sb.append("\" to update the result file");
123:
124: return sb.toString();
125: }
126:
127: /**
128: * Update the result file for this test. Result serialization is done with JSON and this should adequate for most
129: * users. If you need an alternative format, you may override this method.
130: *
131: * @param actual the new result to be saved
132: * @throws ParserException
133: * @throws LexException
134: * @throws IOException
135: */
136: protected void testUpdate(R actual) throws ParserException, LexException,
137: IOException {
138: Gson gson = new Gson();
139: String json = gson.toJson(actual);
140:
141: // Make sure file can be created
142: File f = new File(resultPath);
143:• if (!f.exists()) {
144: f.getParentFile().mkdirs();
145: }
146:
147: IOUtils.write(json, new FileOutputStream(resultPath), ParseTcFacade.UTF8);
148: }
149:
150: /**
151: * Computes an information message to be used with test failures. This message reports
152: * on:
153: * <li>Test Input Source path</li>
154: * <li>Result File path</li>
155: * <li>Test Override property</li>
156: *
157: * @return the information message, as a {@link String}
158: */
159: protected String testInfo() {
160: StringBuilder sb = new StringBuilder();
161: sb.append("[Input: ");
162: sb.append(modelPath);
163: sb.append(", Result:");
164: sb.append(resultPath);
165: sb.append(", Update Property:");
166: sb.append(getUpdatePropertyString());
167: sb.append(".");
168: sb.append(testName);
169: sb.append("]");
170: return sb.toString();
171: }
172:
173: /**
174: * Check if test running in result update mode. This is done by consulting the update property as returned by {
175: * {@link #getUpdatePropertyString()}.
176: *
177: * @return True if test is running in update mode. False otherwise
178: */
179: protected boolean updateCheck() {
180: String update_results_property = getUpdatePropertyString();
181:
182: // check update this test
183:• if (System.getProperty(update_results_property + "." + testName) != null) {
184: return true;
185: }
186:
187: // check update all
188:• if (System.getProperty(update_results_property) != null) {
189: return true;
190: }
191: return false;
192: }
193:
194:
195: /**
196: * This is the first method to be invoked in the test case. It provides a convenient place to place
197: * {@link org.junit.Assume} assumptions.
198: */
199: protected void checkAssumptions() {
200: }
201:
202: /**
203: * Compare output of the processed model with previously stored result. This method may be overridden to implement
204: * more sophisticated result comparison behavior. By default, it applies assertEquals. If you override
205: * this method, please call {@link #testInfo()} in the failure message.
206: *
207: * @param actual the result processed model
208: * @param expected the stored result
209: */
210: public void compareResults(R actual, R expected){
211: Assert.assertEquals(this.testInfo(),expected,actual);
212: }
213:
214: }