Method: createResultFile(String)

1: package org.overture.codegen.tests.exec.util;
2:
3: import java.io.ByteArrayInputStream;
4: import java.io.ByteArrayOutputStream;
5: import java.io.File;
6: import java.io.IOException;
7: import java.io.ObjectInputStream;
8: import java.io.ObjectOutputStream;
9: import java.io.PrintWriter;
10: import java.io.Serializable;
11:
12: import org.junit.Assert;
13: import org.overture.ast.lex.LexLocation;
14: import org.overture.interpreter.values.Value;
15: import org.overture.test.framework.Properties;
16: import org.overture.test.framework.TestResourcesResultTestCase4;
17: import org.overture.util.Base64;
18: import org.w3c.dom.DOMException;
19: import org.w3c.dom.Document;
20: import org.w3c.dom.Element;
21: import org.w3c.dom.Node;
22:
23: public abstract class JavaCodeGenTestCase
24:                 extends TestResourcesResultTestCase4<Object>
25: {
26:         private static final String TESTS_PROPERTY_PREFIX = "tests.javagen.override.";
27:
28:         public JavaCodeGenTestCase(File file, String suiteName, File testSuiteRoot)
29:         {
30:                 super(file, suiteName, testSuiteRoot);
31:         }
32:
33:         /**
34:          * Read the object from Base64 string.
35:          *
36:          * @throws Exception
37:          */
38:         private static Object fromString(String s) throws Exception
39:         {
40:                 byte[] data = Base64.decode(s);
41:                 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
42:                 Object o = ois.readObject();
43:                 ois.close();
44:                 return o;
45:         }
46:
47:         /** Write the object to a Base64 string. */
48:         private static String toString(Serializable o) throws IOException
49:         {
50:                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
51:                 ObjectOutputStream oos = new ObjectOutputStream(baos);
52:                 oos.writeObject(o);
53:                 oos.close();
54:                 return new String(Base64.encode(baos.toByteArray()));
55:         }
56:
57:         public void encodeResult(Object result, Document doc, Element resultElement)
58:         {
59:                 Element message = doc.createElement("output");
60:
61:                 if (result instanceof Serializable)
62:                 {
63:                         try
64:                         {
65:                                 if (result instanceof Value)
66:                                 {
67:                                         result = ((Value) result).deref();
68:                                 }
69:
70:                                 message.setAttribute("object", toString((Serializable) result));
71:                         } catch (IOException | DOMException e)
72:                         {
73:                                 e.printStackTrace();
74:                                 Assert.fail("Could not encode result: " + e.getMessage());
75:                         }
76:
77:                         message.setAttribute("resource", file.getName());
78:                         message.setAttribute("value", result + "");
79:
80:                         resultElement.appendChild(message);
81:                 }
82:         }
83:
84:         public Object decodeResult(Node node)
85:         {
86:
87:                 String result = null;
88:                 for (int i = 0; i < node.getChildNodes().getLength(); i++)
89:                 {
90:                         Node cn = node.getChildNodes().item(i);
91:                         if (cn.getNodeType() == Node.ELEMENT_NODE
92:                                         && cn.getNodeName().equals("output"))
93:                         {
94:                                 try
95:                                 {
96:
97:                                         Node attribute = cn.getAttributes().getNamedItem("object");
98:
99:                                         if (attribute == null)
100:                                         {
101:                                                 return null;
102:                                         }
103:
104:                                         Object nodeType = fromString(attribute.getNodeValue());
105:                                         return nodeType;
106:                                 } catch (Exception e)
107:                                 {
108:                                         e.printStackTrace();
109:                                         Assert.fail("Not able to decode result");
110:                                 }
111:                         }
112:                 }
113:                 return result;
114:         }
115:
116:         @Override
117:         protected boolean assertEqualResults(Object expected, Object actual,
118:                         PrintWriter out)
119:         {
120:                 if (expected == null)
121:                 {
122:                         Assert.fail("No result file");
123:                 }
124:
125:                 if (!expected.equals(actual))
126:                 {
127:                         out.println("Expected result does not match actual:\n\tExpected:\n\t"
128:                                         + expected + "\n\tActual:\n\t" + actual);
129:                         return false;
130:                 }
131:                 return true;
132:         }
133:
134:         protected void configureResultGeneration()
135:         {
136:                 LexLocation.absoluteToStringLocation = false;
137:                 if (System.getProperty(getTestsPropertyPrefix() + "all") != null
138:                                 || getPropertyId() != null
139:                                                 && System.getProperty(getTestsPropertyPrefix()
140:                                                                 + getPropertyId()) != null)
141:                 {
142:                         Properties.recordTestResults = true;
143:                 }
144:         }
145:
146:         public String getTestsPropertyPrefix()
147:         {
148:                 return TESTS_PROPERTY_PREFIX;
149:         }
150:
151:         protected void unconfigureResultGeneration()
152:         {
153:                 Properties.recordTestResults = false;
154:         }
155:
156:         protected abstract String getPropertyId();
157:
158:         protected File getStorageLocation()
159:         {
160:                 return file.getParentFile();
161:         }
162:
163:         protected File getInputLocation()
164:         {
165:                 return file.getParentFile();
166:         }
167:
168:         @Override
169:         protected File createResultFile(String filename)
170:         {
171:                 getStorageLocation().mkdirs();
172:                 return getResultFile(filename);
173:         }
174:
175:         @Override
176:         protected File getResultFile(String filename)
177:         {
178:                 return new File(getStorageLocation(), filename);
179:         }
180: }