Package: ExecutableTestHandler

ExecutableTestHandler

nameinstructionbranchcomplexitylinemethod
ExecutableTestHandler(Release, Dialect)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getMainClass(String)
M: 41 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getMainClassAnnotation()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMainClassFile(File)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMainClassMethods(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
injectArgIntoMainClassFile(File, String, String)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
produceExecResult(File, String[], File[])
M: 102 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 27 C: 0
0%
M: 1 C: 0
0%
runJava(File)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * VDM Code Generator
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.codegen.tests.exec.util.testhandlers;
23:
24: import java.io.File;
25: import java.io.FileInputStream;
26: import java.io.IOException;
27: import java.io.ObjectInputStream;
28: import java.net.URL;
29: import java.net.URLClassLoader;
30: import java.util.LinkedList;
31: import java.util.List;
32: import java.util.Random;
33:
34: import org.apache.commons.io.input.ClassLoaderObjectInputStream;
35: import org.overture.ast.lex.Dialect;
36: import org.overture.codegen.tests.exec.util.ExecutionResult;
37: import org.overture.codegen.tests.exec.util.JavaExecution;
38: import org.overture.codegen.utils.GeneralUtils;
39: import org.overture.config.Release;
40:
41: public abstract class ExecutableTestHandler extends TestHandler
42: {
43:         private final static Random rand = new Random(100);
44:         private static final String MAIN_CLASS = "Exp";
45:
46:         public static final String SERIALIZE_METHOD = " public static void serialize(File file){\n"
47:                         + " try{\n" + " //File file = new File(\"myData.bin\");\n"
48:                         + "         FileOutputStream fout = new FileOutputStream( file );\n"
49:                         + "         ObjectOutputStream oos = new ObjectOutputStream(fout);\n"
50:                         + " Object exp = null;\n" + " try{\n"
51:                         + " exp = exp();\n" + " } catch(Exception e){\n"
52:                         + " exp = e.getMessage();\n" + " }\n"
53:                         + "                 java.lang.System.out.println(exp);\n"
54:                         + "         oos.writeObject( exp );\n" + "         oos.close();\n"
55:                         + " }catch(Exception ex){\n"
56:                         + "         ex.printStackTrace();\n " + " }\n" + " }\n";
57:
58:         public String getMainClass(String rootPackage)
59:         {
60:                 StringBuilder methodsMerged = new StringBuilder();
61:
62:•                for (String method : getMainClassMethods(rootPackage))
63:                 {
64:                         methodsMerged.append(method).append("\n\n");
65:                 }
66:
67:                 return " import java.io.File;\n" + "import java.io.FileOutputStream;\n"
68:                                 + "import java.io.ObjectOutputStream;\n"
69:                                 + "import org.overture.codegen.runtime.*;\n"
70:                                 + "import org.overture.codegen.runtime.traces.*;\n"
71:                                 + "import java.util.*;\n\n" + getMainClassAnnotation() + "\n"
72:                                 + "public class Exp {\n" + " public static Object exp()\n"
73:                                 + " {\n" + " return %s;\n" + " }\n\n"
74:                                 + " public static void main(String[] args)" + " {\n"
75:                                 + " if(args.length < 1)\n" + " {\n"
76:                                 + " \t java.lang.System.err.println(\"Error: Missing serilization file path\"); java.lang.System.exit( 1);"
77:                                 + " }\n" + " serialize(new File(args[0]));\n" + " }\n\n"
78:                                 + SERIALIZE_METHOD + methodsMerged + "}\n";
79:         }
80:
81:         public String getMainClassAnnotation()
82:         {
83:                 return "";
84:         }
85:
86:         public ExecutableTestHandler(Release release, Dialect dialect)
87:         {
88:                 super(release, dialect);
89:         }
90:
91:         public abstract ExecutionResult interpretVdm(File intputFile)
92:                         throws Exception;
93:
94:         public List<String> getMainClassMethods(String rootPackage)
95:         {
96:                 return new LinkedList<String>();
97:         }
98:
99:         public void injectArgIntoMainClassFile(File parent, String body,
100:                         String rootPackage) throws IOException
101:         {
102:                 File mainClassFile = getMainClassFile(parent);
103:                 writeToFile(String.format(getMainClass(rootPackage), body), mainClassFile);
104:         }
105:
106:         private File getMainClassFile(File parent) throws IOException
107:         {
108:                 return getFile(parent, MAIN_CLASS);
109:         }
110:
111:         public ExecutionResult runJava(File folder)
112:         {
113:                 File cgRuntime = new File(org.overture.codegen.runtime.EvaluatePP.class.getProtectionDomain().getCodeSource().getLocation().getFile());
114:                 return produceExecResult(folder, new String[] { /* no args */ }, cgRuntime);
115:         }
116:
117:         public static ExecutionResult produceExecResult(File folder,
118:                         String[] preArgs, File... cpJars)
119:         {
120:                 FileInputStream fin = null;
121:                 ObjectInputStream ois = null;
122:
123:                 try
124:                 {
125:                         String resultFilename = String.format("serilizedExecutionResult-%d.bin", rand.nextLong());
126:
127:                         String processOutput = JavaExecution.run(ExecutableTestHandler.MAIN_CLASS, preArgs, new String[] {
128:                                         resultFilename }, folder, GeneralUtils.concat(new File[] {
129:                                                         folder }, cpJars));
130:
131:                         File dataFile = new File(folder, resultFilename);
132:                         dataFile.deleteOnExit();
133:                         fin = new FileInputStream(dataFile);
134:
135:                         // Create a new class loader to load classes specific to the run folder
136:                         URL[] urls = null;
137:                         urls = new URL[] { folder.toURI().toURL() };
138:                         ClassLoader cl = new URLClassLoader(urls);
139:
140:                         // Use a ObjectInputStream that loads from a custom class loader
141:                         ois = new ClassLoaderObjectInputStream(cl, fin);
142:                         Object cgValue = (Object) ois.readObject();
143:
144:                         return new ExecutionResult(processOutput, cgValue);
145:
146:                 } catch (Exception e)
147:                 {
148:                         e.printStackTrace();
149:                 } finally
150:                 {
151:•                        if (fin != null)
152:                         {
153:                                 try
154:                                 {
155:                                         fin.close();
156:                                 } catch (IOException e)
157:                                 {
158:                                         e.printStackTrace();
159:                                 }
160:                         }
161:
162:•                        if (ois != null)
163:                         {
164:                                 try
165:                                 {
166:                                         ois.close();
167:                                 } catch (IOException e)
168:                                 {
169:                                         e.printStackTrace();
170:                                 }
171:                         }
172:                 }
173:
174:                 return null;
175:         }
176: }