Method: setUp()

1: package org.overture.codegen.tests.exec.util;
2:
3: import java.io.File;
4: import java.io.IOException;
5: import java.io.PrintWriter;
6: import java.util.Collection;
7: import java.util.List;
8: import java.util.Vector;
9:
10: import org.junit.Assert;
11: import org.junit.Assume;
12: import org.junit.Before;
13: import org.junit.Rule;
14: import org.junit.Test;
15: import org.overture.codegen.ir.IRSettings;
16: import org.overture.codegen.tests.exec.util.testhandlers.ExecutableTestHandler;
17: import org.overture.codegen.tests.exec.util.testhandlers.TestHandler;
18: import org.overture.codegen.tests.util.TestUtils;
19: import org.overture.codegen.utils.GeneralCodeGenUtils;
20: import org.overture.interpreter.runtime.ContextException;
21: import org.overture.test.framework.ConditionalIgnoreMethodRule;
22: import org.overture.test.framework.Properties;
23: import org.overture.test.framework.results.IMessage;
24: import org.overture.test.framework.results.Result;
25: import org.overture.typechecker.util.TypeCheckerUtil.TypeCheckResult;
26:
27: public abstract class CheckerTestBase extends JavaCodeGenTestCase
28: {
29:         public static final String EXEC_TEST_PROPERTY = "tests.javagen.javac";
30:         protected TestHandler testHandler;
31:         protected File outputDir;
32:
33:         public CheckerTestBase(File vdmSpec, TestHandler testHandler)
34:         {
35:                 super(vdmSpec, null, null);
36:                 this.testHandler = testHandler;
37:         }
38:
39:         protected static Collection<Object[]> collectTests(File root,
40:                         TestHandler handler)
41:         {
42:                 Collection<Object[]> tests = new Vector<Object[]>();
43:
44:                 List<File> vdmSources = TestUtils.getTestInputFiles(root);
45:
46:                 final int testCount = vdmSources.size();
47:
48:                 for (int i = 0; i < testCount; i++)
49:                 {
50:                         File vdmSource = vdmSources.get(i);
51:                         String name = vdmSource.getAbsolutePath().substring(root.getAbsolutePath().length()
52:                                         + 1);
53:
54:                         tests.add(new Object[] { name, vdmSource, handler });
55:                 }
56:
57:                 return tests;
58:         }
59:
60:         @Before
61:         public void setUp() throws Exception
62:         {
63:                 outputDir = new File(new File(new File("target"), getClass().getSimpleName()), file.getName());
64:                 outputDir.mkdirs();
65:         }
66:
67:         @Rule
68:         public ConditionalIgnoreMethodRule rule = new ConditionalIgnoreMethodRule();
69:
70:         public boolean runTest()
71:         {
72:                 return System.getProperty(getExecProperty()) != null;
73:         }
74:
75:         public String getExecProperty()
76:         {
77:                 return EXEC_TEST_PROPERTY;
78:         }
79:
80:         abstract public void genSourcesAndCompile();
81:
82:         @Test
83:         public void test() throws Exception
84:         {
85:                 Assume.assumeTrue("Pass property -D" + getExecProperty()
86:                                 + " to run test", runTest());
87:
88:                 assumeTest();
89:
90:                 configureResultGeneration();
91:                 try
92:                 {
93:                         genSourcesAndCompile();
94:
95:                         if (testHandler instanceof ExecutableTestHandler)
96:                         {
97:                                 Result<Object> result = produceResult();
98:                                 compareResults(result, file.getName() + ".eval.result");
99:                         }
100:                 } finally
101:                 {
102:                         unconfigureResultGeneration();
103:                 }
104:         }
105:
106:         public void assumeTest()
107:         {
108:                 /* Allow all tests to run by default */
109:         }
110:
111:         public static <T extends TypeCheckResult<?>> T checkTcResult(T tcResult)
112:         {
113:                 if (GeneralCodeGenUtils.hasErrors(tcResult))
114:                 {
115:                         Assert.fail("Problems parsing/type checking VDM model:\n"
116:                                         + GeneralCodeGenUtils.errorStr(tcResult));
117:                 }
118:
119:                 return tcResult;
120:         }
121:
122:         public IRSettings getIrSettings()
123:         {
124:                 IRSettings irSettings = new IRSettings();
125:                 irSettings.setCharSeqAsString(false);
126:
127:                 return irSettings;
128:         }
129:
130:         private Result<Object> produceResult() throws IOException
131:         {
132:                 if (testHandler instanceof ExecutableTestHandler)
133:                 {
134:                         ExecutableTestHandler executableTestHandler = (ExecutableTestHandler) testHandler;
135:                         if (Properties.recordTestResults)
136:                         {
137:                                 Object vdmResult = evalVdm(file, executableTestHandler);
138:                                 return new Result<Object>(vdmResult, new Vector<IMessage>(), new Vector<IMessage>());
139:                         }
140:
141:                         // Note that the classes returned in javaResult may be loaded by another class loader. This is the case for
142:                         // classes representing VDM classes, Quotes etc. that's not part of the cg-runtime
143:
144:                         ExecutionResult javaResult = executableTestHandler.runJava(outputDir);
145:
146:                         if (javaResult == null)
147:                         {
148:                                 Assert.fail("No Java result could be produced");
149:                         }
150:
151:                         return new Result<Object>(javaResult, new Vector<IMessage>(), new Vector<IMessage>());
152:
153:                 }
154:
155:                 Assert.fail("Trying to produce result using an unsupported test handler: "
156:                                 + testHandler);
157:
158:                 return new Result<Object>(null, new Vector<IMessage>(), new Vector<IMessage>());
159:         }
160:
161:         public File[] consCpFiles()
162:         {
163:                 File cgRuntime = new File(org.overture.codegen.runtime.EvaluatePP.class.getProtectionDomain().getCodeSource().getLocation().getFile());
164:                 return new File[] { cgRuntime };
165:         }
166:
167:         public void compile(File[] cpJars)
168:         {
169:                 ProcessResult result = JavaCommandLineCompiler.compile(outputDir, cpJars);
170:                 Assert.assertTrue("Generated Java code did not compile: "
171:                                 + result.getOutput().toString(), result.getExitCode() == 0);
172:         }
173:
174:         /**
175:          * Evaluate the VDM specification, exceptions are returned as a String otherwise Value
176:          *
177:          * @param currentInputFile
178:          * @param executableTestHandler
179:          * @return the result of the VDM execution
180:          */
181:         private Object evalVdm(File currentInputFile,
182:                         ExecutableTestHandler executableTestHandler)
183:         {
184:                 // Calculating the VDM Result:
185:                 Object vdmResult = null;
186:
187:                 try
188:                 {
189:                         ExecutionResult res = executableTestHandler.interpretVdm(currentInputFile);
190:
191:                         if (res == null)
192:                         {
193:                                 Assert.fail("no vdm result");
194:                         }
195:
196:                         vdmResult = res.getExecutionResult();
197:                 } catch (ContextException ce1)
198:                 {
199:                         // Context exceptions are used to report the result of erroneous VDM executions
200:                         vdmResult = ce1.getMessage();
201:                 } catch (Exception e1)
202:                 {
203:                         e1.printStackTrace();
204:                         Assert.fail("Got unexpected exception when computing the VDM value");
205:                 }
206:
207:                 return vdmResult;
208:         }
209:
210:         @Override
211:         protected boolean assertEqualResults(Object expected, Object actual,
212:                         PrintWriter out)
213:         {
214:                 ExecutionResult javaResult = (ExecutionResult) actual;
215:
216:                 // Comparison of VDM and Java results
217:                 ComparisonIR comp = new ComparisonIR(file);
218:                 boolean equal = comp.compare(javaResult.getExecutionResult(), expected);
219:
220:                 if (!equal)
221:                 {
222:                         out.println(String.format("Actual result: %s does not match Expected: %s", ""
223:                                         + actual, "" + expected));
224:                 }
225:
226:                 return equal;
227:         }
228: }