Package: ParamStandardTest

ParamStandardTest

nameinstructionbranchcomplexitylinemethod
ParamStandardTest(String, String, String)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
checkAssumptions()
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
testCase()
M: 30 C: 28
48%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 5 C: 9
64%
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.FileNotFoundException;
25: import java.io.IOException;
26: import java.util.List;
27:
28: import org.junit.Assert;
29: import org.junit.Test;
30: import org.overture.ast.node.INode;
31: import org.overture.parser.lex.LexException;
32: import org.overture.parser.syntax.ParserException;
33:
34: /**
35: * Standard test in the new Overture test framework. This class runs testing on (correct) VDM sources and compares them
36: * with stored result files. It is meant to be subclassed as a way to quickly create your own test suites.<br>
37: * <br>
38: * A comparison method for results must be provided. JSON-based serialization of results is fully automated and
39: * deserialization is close to it.<br>
40: * <br>
41: * These testing are meant to be run as parameterized JUnit test and so any subclass must be annotated with
42: * <code>@RunWith(Parameterized.class)</code>. <br>
43: * <br>
44: * This class also has a type parameter <code>R</code> that represents the output of the functionality under test. You
45: * should create a specific <code>R</code> type for your functionality and write some kind of transformation between
46: * your native output type and <code>R</code>.
47: *
48: * @author ldc
49: * @param <R>
50: */
51: public abstract class ParamStandardTest<R> extends AbsResultTest<R>
52: {
53:
54:         /**
55:          * Constructor for the test. In order to use JUnit parameterized testing, the inputs for this class must be supplied
56:          * by a public static method. Subclasses must implement this method themselves and annotate with
57:          * <code>@Parameters(name = "{index} : {0}")</code>.<br>
58:          * <br>
59:          * The {@link PathsProvider#computePaths(String...)} method produces the correct input for this constructor and
60:          * should be called with the root folder of your testing inputs as its argument.
61:          *
62:          * @param nameParameter
63:          * the name of the test. Normally derived from the test input file
64:          * @param inputParameter
65:          * file path for the VDM source to test
66:          * @param resultParameter
67:          * test result file
68:          */
69:         public ParamStandardTest(String nameParameter, String inputParameter,
70:                         String resultParameter)
71:         {
72:                 this.testName = nameParameter;
73:                 this.modelPath = inputParameter;
74:                 this.resultPath = resultParameter;
75:                 updateResult = updateCheck();
76:         }
77:
78:         /**
79:          * Execute this test. Constructs the AST for the model and processes it via {@link #processModel(List)}. Then loads
80:          * a stored result via {@link #deSerializeResult(String)}. Finally, the two results are compared with
81:          * {@link #compareResults(Object, Object)}.<br>
82:          * <br>
83:          * If the test is run in update mode, then no comparison is made. Instead, the new result is saved. <br>
84:          * <br>
85:          * This test is not designed to run on VDM sources with syntax or type errors. The test will fail if the source
86:          * fails to parse or type check. While this behavior can be overridden, we suggest looking at
87:          * {@link ParamFineGrainTest} if you need to cope with these errors.
88:          *
89:          * @throws IOException
90:          * @throws LexException
91:          * @throws ParserException
92:          */
93:         @Test
94:         public void testCase() throws ParserException, LexException, IOException
95:         {
96:
97:                 checkAssumptions();
98:                 List<INode> ast = ParseTcFacade.typedAst(modelPath, testName);
99:                 R actual = processModel(ast);
100:•                if (updateResult)
101:                 {
102:                         this.testUpdate(actual);
103:                 } else
104:                 {
105:                         R expected = null;
106:                         try
107:                         {
108:                                 expected = deSerializeResult(resultPath);
109:                         } catch (FileNotFoundException e)
110:                         {
111:                                 Assert.fail("Test " + testName
112:                                                 + " failed. No result file found. Use \"-D"
113:                                                 + getUpdatePropertyString() + "." + testName
114:                                                 + "\" to create an initial one."
115:                                                 + "\n The test result was: " + actual.toString());
116:                         }
117:                         this.compareResults(actual, expected);
118:                 }
119:         }
120:
121:         /**
122:          * Analyse a model. This method is called during test execution to produce the actual result. It must, of course, be
123:          * overridden to perform whatever analysis the functionality under test performs.<br>
124:          * <br>
125:          * The output of this method must be of type <code>R</code>, the result type this test runs on. You will will likely
126:          * need to have a conversion method between the output of your analysis and <code>R</code>.
127:          *
128:          * @param ast
129:          * the model to process
130:          * @return the output of the analysis
131:          */
132:         public abstract R processModel(List<INode> ast);
133:
134:         protected void checkAssumptions() {}
135: }