Package: XmlResultReaderWriter

XmlResultReaderWriter

nameinstructionbranchcomplexitylinemethod
XmlResultReaderWriter()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
XmlResultReaderWriter(File, XmlResultReaderWriter.IResultStore)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
convertNodeToMessage(List, Node)
M: 0 C: 37
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createWarningsAndErrors(Document, Element)
M: 149 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 28 C: 0
0%
M: 1 C: 0
0%
getErrors()
M: 2 C: 7
78%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
getResult()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getWarnings()
M: 2 C: 7
78%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
loadFromXml()
M: 3 C: 112
97%
M: 2 C: 10
83%
M: 2 C: 5
71%
M: 2 C: 25
93%
M: 0 C: 1
100%
saveInXml()
M: 68 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
setResult(String, Result)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
setResult(String, Result, XmlResultReaderWriter.IResultStore)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * Test Framework for Overture
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.test.util;
23:
24: import java.io.File;
25: import java.util.List;
26: import java.util.Vector;
27:
28: import javax.xml.parsers.DocumentBuilder;
29: import javax.xml.parsers.DocumentBuilderFactory;
30: import javax.xml.parsers.ParserConfigurationException;
31: import javax.xml.transform.OutputKeys;
32: import javax.xml.transform.Transformer;
33: import javax.xml.transform.TransformerException;
34: import javax.xml.transform.TransformerFactory;
35: import javax.xml.transform.dom.DOMSource;
36: import javax.xml.transform.stream.StreamResult;
37:
38: import org.overture.test.framework.Properties;
39: import org.overture.test.framework.results.IMessage;
40: import org.overture.test.framework.results.Message;
41: import org.overture.test.framework.results.Result;
42: import org.w3c.dom.Document;
43: import org.w3c.dom.Element;
44: import org.w3c.dom.NamedNodeMap;
45: import org.w3c.dom.Node;
46: import org.w3c.dom.NodeList;
47:
48: public class XmlResultReaderWriter<R>
49: {
50:         public interface IResultStore<T>
51:         {
52:                 void encodeResult(T result, Document doc, Element resultElement);
53:
54:                 T decodeResult(Node node);
55:         }
56:
57:         private File file;
58:         private Result<R> result = null;
59:         private String type;
60:         private IResultStore<R> resultStore;
61:
62:         public XmlResultReaderWriter()
63:         {
64:         }
65:
66:         public XmlResultReaderWriter(File file, IResultStore<R> store)
67:         {
68:                 this.file = file;
69:                 this.resultStore = store;
70:         }
71:
72:         // public XmlResultReaderWriter(String path)
73:         // {
74:         // this(new File(path));
75:         // }
76:
77:         public void setResult(String type, Result<R> result, IResultStore<R> store)
78:         {
79:                 this.type = type;
80:                 this.result = result;
81:                 this.resultStore = store;
82:         }
83:
84:         public void saveInXml() throws ParserConfigurationException,
85:                         TransformerException
86:         {
87:                 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
88:                 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
89:
90:                 // root elements
91:                 Document doc = docBuilder.newDocument();
92:                 Element rootElement = doc.createElement("testResult");
93:                 doc.appendChild(rootElement);
94:                 rootElement.setAttribute("type", type);
95:
96:                 createWarningsAndErrors(doc, rootElement);
97:
98:                 String oldLineSeparator = System.getProperty("line.separator");
99:•                if (Properties.forceUnixLineEndings)
100:                 {
101:                         // Output using LF line endings (UNIX-style),
102:                         // but only for this file.
103:                         oldLineSeparator = System.getProperty("line.separator");
104:                         System.setProperty("line.separator", "\n");
105:                 }
106:
107:                 // write the content into xml file
108:                 TransformerFactory transformerFactory = TransformerFactory.newInstance();
109:                 Transformer transformer = transformerFactory.newTransformer();
110:                 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
111:                 DOMSource source = new DOMSource(doc);
112:                 StreamResult result = new StreamResult(file);
113:                 // transformer.setOutputProperty(OutputKeys.INDENT, "yes");
114:                 // Output to console for testing
115:                 // StreamResult result = new StreamResult(System.out);
116:
117:                 transformer.transform(source, result);
118:
119:•                if (Properties.forceUnixLineEndings)
120:                 {
121:                         // Restore line endings
122:                         System.setProperty("line.separator", oldLineSeparator);
123:                 }
124:         }
125:
126:         private void createWarningsAndErrors(Document doc, Element rootElement)
127:         {
128:
129:                 List<IMessage> warnings = getResult().warnings;
130:                 List<IMessage> errors = getResult().errors;
131:
132:•                if (warnings != null)
133:                 {
134:•                        for (IMessage warning : warnings)
135:                         {
136:                                 Element message = doc.createElement("message");
137:                                 message.setAttribute("messageType", "warning");
138:                                 message.setAttribute("resource", warning.getResource());
139:                                 message.setAttribute("number", new Integer(warning.getNumber()).toString());
140:                                 message.setAttribute("message", warning.getMessage());
141:                                 message.setAttribute("column", new Integer(warning.getCol()).toString());
142:                                 message.setAttribute("line", new Integer(warning.getLine()).toString());
143:                                 rootElement.appendChild(message);
144:                         }
145:                 }
146:
147:•                if (errors != null)
148:                 {
149:•                        for (IMessage warning : errors)
150:                         {
151:                                 Element message = doc.createElement("message");
152:                                 message.setAttribute("messageType", "error");
153:                                 message.setAttribute("resource", warning.getResource());
154:                                 message.setAttribute("number", new Integer(warning.getNumber()).toString());
155:                                 message.setAttribute("message", warning.getMessage());
156:                                 message.setAttribute("column", new Integer(warning.getCol()).toString());
157:                                 message.setAttribute("line", new Integer(warning.getLine()).toString());
158:                                 rootElement.appendChild(message);
159:                         }
160:                 }
161:
162:                 Element resultElement = doc.createElement("result");
163:                 resultStore.encodeResult(result.result, doc, resultElement);
164:                 rootElement.appendChild(resultElement);
165:
166:         }
167:
168:         public boolean loadFromXml()
169:         {
170:                 // File resultFile = new File(file.getAbsoluteFile()+ ".result");
171:                 List<IMessage> warnings = new Vector<IMessage>();
172:                 List<IMessage> errors = new Vector<IMessage>();
173:                 R readResult = null;
174:
175:                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
176:                 DocumentBuilder db;
177:                 try
178:                 {
179:                         db = dbf.newDocumentBuilder();
180:
181:                         Document doc = db.parse(file);
182:                         doc.getDocumentElement().normalize();
183:                         NodeList nodeLst = doc.getElementsByTagName("message");
184:•                        for (int i = 0; i < nodeLst.getLength(); i++)
185:                         {
186:                                 Node node = nodeLst.item(i);
187:                                 String nodeType = node.getAttributes().getNamedItem("messageType").getNodeValue();
188:•                                if (nodeType.equals("error"))
189:                                 {
190:                                         convertNodeToMessage(errors, node);
191:•                                } else if (nodeType.equals("warning"))
192:                                 {
193:                                         convertNodeToMessage(warnings, node);
194:                                 }
195:                         }
196:
197:•                        for (int i = 0; i < doc.getDocumentElement().getChildNodes().getLength(); i++)
198:                         {
199:                                 Node node = doc.getDocumentElement().getChildNodes().item(i);
200:•                                if (node.getNodeName() != null
201:•                                                && node.getNodeName().equals("result"))
202:                                 {
203:                                         node.normalize();
204:                                         readResult = resultStore.decodeResult(node);
205:                                 }
206:                                 // System.out.println(node);
207:                         }
208:                         // doc.getDocumentElement().getChildNodes().getElementsByTagName("result")
209:
210:                         String type = doc.getDocumentElement().getAttributes().getNamedItem("type").getNodeValue();
211:                         setResult(type, new Result<R>(readResult, warnings, errors));
212:                 } catch (Exception e)
213:                 {
214:                         return false;
215:                 }
216:                 return true;
217:         }
218:
219:         private void convertNodeToMessage(List<IMessage> set, Node node)
220:         {
221:
222:                 NamedNodeMap nnm = node.getAttributes();
223:                 String resource = nnm.getNamedItem("resource").getNodeValue();
224:                 IMessage m = new Message(resource, Integer.parseInt(nnm.getNamedItem("number").getNodeValue()), Integer.parseInt(nnm.getNamedItem("line").getNodeValue()), Integer.parseInt(nnm.getNamedItem("column").getNodeValue()), nnm.getNamedItem("message").getNodeValue());
225:                 set.add(m);
226:         }
227:
228:         public Result<R> getResult()
229:         {
230:                 return result;
231:         }
232:
233:         public void setResult(String type, Result<R> result)
234:         {
235:                 this.result = result;
236:                 this.type = type;
237:         }
238:
239:         public List<IMessage> getWarnings()
240:         {
241:•                if (result != null)
242:                 {
243:                         return result.warnings;
244:                 }
245:
246:                 return null;
247:         }
248:
249:         public List<IMessage> getErrors()
250:         {
251:•                if (result != null)
252:                 {
253:                         return result.errors;
254:                 }
255:
256:                 return null;
257:         }
258:
259: }