Method: processTraceResult(ExecutionResult)

1: package org.overture.codegen.tests.exec.util.testhandlers;
2:
3: import java.io.File;
4: import java.io.IOException;
5: import java.net.ServerSocket;
6: import java.util.LinkedList;
7: import java.util.List;
8:
9: import javax.xml.parsers.ParserConfigurationException;
10: import javax.xml.xpath.XPathExpressionException;
11:
12: import org.apache.commons.io.FileUtils;
13: import org.apache.log4j.Logger;
14: import org.junit.Assert;
15: import org.overture.ast.lex.Dialect;
16: import org.overture.codegen.runtime.traces.InMemoryTestAccumulator;
17: import org.overture.codegen.tests.exec.util.ExecutionResult;
18: import org.overture.config.Release;
19: import org.overture.config.Settings;
20: import org.overture.ct.ctruntime.TraceRunnerMain;
21: import org.overture.ct.ctruntime.utils.CtHelper;
22: import org.overture.ct.ctruntime.utils.Data;
23: import org.overture.ct.ctruntime.utils.TraceReductionInfo;
24: import org.overture.ct.ctruntime.utils.TraceResult;
25: import org.overture.ct.ctruntime.utils.TraceResultReader;
26: import org.overture.ct.ctruntime.utils.TraceTest;
27: import org.xml.sax.SAXException;
28:
29: public class TraceHandler extends ExecutableSpecTestHandler
30: {
31:         // The socket is used to communicate with the trace interpreter
32:         protected ServerSocket socket;
33:         protected static final int SOCKET_TIMEOUT = 0;
34:
35:         private Logger log = Logger.getLogger(this.getClass().getName());
36:
37:         public TraceHandler(Release release, Dialect dialect)
38:         {
39:                 super(release, dialect);
40:         }
41:
42:         public String getTraceName()
43:         {
44:                 return "T1";
45:         }
46:
47:         @Override
48:         public String getVdmEntry()
49:         {
50:                 return getTraceName();
51:         }
52:
53:         @Override
54:         public String getJavaEntry()
55:         {
56:                 return "computeTests()";
57:         }
58:
59:         @Override
60:         public String getFullyQualifiedEntry(String rootPackage)
61:         {
62:                 // The entry point is local so we can ignore the package and the enclosing class
63:                 return getJavaEntry();
64:         }
65:
66:         public int getFreePort()
67:         {
68:                 ServerSocket s = null;
69:                 try
70:                 {
71:                         s = new ServerSocket(0);
72:
73:                         // returns the port the system selected
74:                         return s.getLocalPort();
75:                 } catch (IOException e)
76:                 {
77:                         e.printStackTrace();
78:                 } finally
79:                 {
80:                         if (s != null)
81:                         {
82:                                 try
83:                                 {
84:                                         s.close();
85:                                 } catch (IOException e)
86:                                 {
87:                                         e.printStackTrace();
88:                                 }
89:                         }
90:                 }
91:
92:                 return 8999;
93:         }
94:
95:         @Override
96:         public List<String> getMainClassMethods(String rootPackage)
97:         {
98:                 String runTraceCall;
99:
100:                 if (Settings.dialect == Dialect.VDM_SL)
101:                 {
102:                         runTraceCall = toFullName(rootPackage, "Entry.Entry_T1_Run(acc);");
103:                 } else
104:                 {
105:                         runTraceCall = " new " + toFullName(rootPackage, "Entry")
106:                                         + "().Entry_T1_Run(acc);";
107:                 }
108:
109:                 String computeTestsMethod = " public static TestAccumulator computeTests()\n"
110:                                 + " {\n"
111:                                 + " InMemoryTestAccumulator acc = new InMemoryTestAccumulator();\n"
112:                                 + runTraceCall + "\n" + " return acc;\n" + " }\n";
113:
114:                 List<String> methods = new LinkedList<String>();
115:                 methods.add(computeTestsMethod);
116:
117:                 return methods;
118:         }
119:
120:         @Override
121:         public ExecutionResult interpretVdm(File intputFile) throws Exception
122:         {
123:                 File vdmTraceResultFile = computeVdmTraceResult(intputFile);
124:
125:                 List<TraceResult> testResult = new TraceResultReader().read(vdmTraceResultFile);
126:
127:                 if (testResult.size() != 1
128:                                 || !testResult.get(0).traceName.equals(getTraceName()))
129:                 {
130:                         log.error("Expected a single trace named: " + getTraceName()
131:                                         + ". Got: " + testResult);
132:                         return null;
133:                 }
134:
135:                 TraceResult t1 = testResult.get(0);
136:
137:                 StringBuilder sb = new StringBuilder();
138:
139:                 for (TraceTest t : t1.tests)
140:                 {
141:                         sb.append(t).append('\n');
142:                 }
143:
144:                 return new ExecutionResult(sb.toString(), t1.tests);
145:         }
146:
147:         @Override
148:         public ExecutionResult runJava(File folder)
149:         {
150:                 ExecutionResult javaResult = super.runJava(folder);
151:
152:                 return processTraceResult(javaResult);
153:         }
154:
155:         public ExecutionResult processTraceResult(ExecutionResult javaResult)
156:         {
157:                 Object executionResult = javaResult.getExecutionResult();
158:
159:•                if (executionResult instanceof InMemoryTestAccumulator)
160:                 {
161:                         InMemoryTestAccumulator acc = (InMemoryTestAccumulator) executionResult;
162:
163:                         return new ExecutionResult(javaResult.getStrRepresentation(), acc.getAllTests());
164:                 } else
165:                 {
166:                         return new ExecutionResult(javaResult.getStrRepresentation(), javaResult.getExecutionResult().toString());
167:                 }
168:         }
169:
170:         public File computeVdmTraceResult(File specFile) throws IOException,
171:                         XPathExpressionException, SAXException, ParserConfigurationException
172:         {
173:                 // The client thread will close the socket
174:                 int port = getFreePort();
175:                 socket = new ServerSocket(port);
176:                 socket.setSoTimeout(SOCKET_TIMEOUT);
177:                 final Data data = new Data();
178:
179:                 File traceFolder = new File("target" + File.separatorChar + "cgtest"
180:                                 + File.separatorChar + "traceoutput");
181:
182:                 traceFolder.mkdirs();
183:
184:                 final File actualOutputFile = new File(traceFolder, "Entry-"
185:                                 + getVdmEntry() + ".xml");
186:
187:                 CtHelper testHelper = new CtHelper();
188:                 Thread t = testHelper.consCtClientThread(socket, data);
189:                 t.setDaemon(false);
190:                 t.start();
191:
192:                 File destFile = new File(traceFolder, specFile.getName() + ".vdmpp");
193:                 FileUtils.copyFile(specFile, destFile);
194:
195:                 TraceRunnerMain.USE_SYSTEM_EXIT = false;
196:                 TraceReductionInfo info = new TraceReductionInfo();
197:
198:                 String dialect = Settings.dialect == Dialect.VDM_SL ? "-vdmsl"
199:                                 : "-vdmpp";
200:
201:                 String[] buildArgs = new String[] { "-h", "localhost", "-p", port + "",
202:                                 "-k", "whatever", "-e", "Entry", dialect, "-r", "vdm10", "-t",
203:                                 getVdmEntry(), "-tracefolder",
204:                                 traceFolder.toURI().toASCIIString(),
205:                                 destFile.toURI().toASCIIString(), "-traceReduction",
206:                                 "{" + info.getSubset() + ","
207:                                                 + info.getReductionType().toString() + ","
208:                                                 + info.getSeed() + "}" };
209:
210:                 TraceRunnerMain.main(buildArgs);
211:
212:                 final String message = data.getMessage();
213:
214:                 Assert.assertTrue("Test did not succeed", message.contains("status=\"completed\" progress=\"100\""));
215:
216:                 return actualOutputFile;
217:         }
218: }