Package: MessageReaderWriter

MessageReaderWriter

nameinstructionbranchcomplexitylinemethod
MessageReaderWriter(File)
M: 19 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
MessageReaderWriter(String)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
exists()
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%
getErrors()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getResult()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getWarnings()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
load()
M: 168 C: 0
0%
M: 16 C: 0
0%
M: 10 C: 0
0%
M: 44 C: 0
0%
M: 1 C: 0
0%
save()
M: 36 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
set(Result)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
setWarningsAndErrors(List, List)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
writeMessageSet(BufferedWriter, String, List)
M: 74 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
writeResult(BufferedWriter, String)
M: 58 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 15 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.BufferedReader;
25: import java.io.BufferedWriter;
26: import java.io.File;
27: import java.io.FileNotFoundException;
28: import java.io.FileReader;
29: import java.io.FileWriter;
30: import java.io.IOException;
31: import java.util.List;
32: import java.util.Vector;
33:
34: import org.overture.test.framework.results.IMessage;
35: import org.overture.test.framework.results.Message;
36: import org.overture.test.framework.results.Result;
37:
38: public class MessageReaderWriter
39: {
40:
41:         enum MsgType
42:         {
43:                 Warning, Error, Result
44:         };
45:
46:         public static String WARNING_LABEL = "WARNING";
47:         public static String ERROR_LABEL = "ERROR";
48:         public static String RESULT_LABEL = "RESULT";
49:
50:         final List<IMessage> errors = new Vector<IMessage>();
51:         final List<IMessage> warnings = new Vector<IMessage>();
52:         String result = "";
53:         final File file;
54:
55:         public MessageReaderWriter(File file)
56:         {
57:                 this.file = file;
58:         }
59:
60:         public MessageReaderWriter(String path)
61:         {
62:                 this(new File(path));
63:         }
64:
65:         public void setWarningsAndErrors(List<IMessage> errors,
66:                         List<IMessage> warnings)
67:         {
68:                 this.errors.clear();
69:                 this.warnings.clear();
70:                 this.errors.addAll(errors);
71:                 this.warnings.addAll(warnings);
72:         }
73:
74:         @SuppressWarnings({ "rawtypes", "unchecked" })
75:         public void set(Result result)
76:         {
77:                 setWarningsAndErrors(result.errors, result.warnings);
78:                 this.result = result.getStringResult();
79:         }
80:
81:         public List<IMessage> getErrors()
82:         {
83:                 return errors;
84:         }
85:
86:         public List<IMessage> getWarnings()
87:         {
88:                 return warnings;
89:         }
90:
91:         public String getResult()
92:         {
93:                 return result;
94:         }
95:
96:         public boolean exists()
97:         {
98:                 return file.exists();
99:         }
100:
101:         public boolean load()
102:         {
103:                 errors.clear();
104:                 warnings.clear();
105:                 result = "";
106:                 BufferedReader in = null;
107:                 try
108:                 {
109:                         in = new BufferedReader(new FileReader(file));
110:                         String line;
111:•                        while ((line = in.readLine()) != null)
112:                         {
113:                                 MsgType type = null;
114:•                                if (line.startsWith(ERROR_LABEL))
115:                                 {
116:                                         type = MsgType.Error;
117:                                         line = line.substring(ERROR_LABEL.length() + 1);
118:•                                } else if (line.startsWith(WARNING_LABEL))
119:                                 {
120:                                         type = MsgType.Warning;
121:                                         line = line.substring(WARNING_LABEL.length() + 1);
122:•                                } else if (line.startsWith(RESULT_LABEL))
123:                                 {
124:                                         type = MsgType.Result;
125:                                         line = line.substring(RESULT_LABEL.length() + 1);
126:                                 } else
127:                                 {
128:                                         return false;
129:                                 }
130:
131:                                 String[] splitLine = line.split(":");
132:
133:•                                if (splitLine.length != 4)
134:                                 {
135:                                         return false;
136:                                 }
137:
138:                                 int number = Integer.parseInt(splitLine[1]);
139:
140:                                 String[] position = splitLine[2].split(",");
141:
142:•                                if (position.length != 2)
143:                                 {
144:                                         return false;
145:                                 }
146:
147:                                 int startLine = Integer.parseInt(position[0]);
148:                                 int startCol = Integer.parseInt(position[1]);
149:
150:                                 String message = splitLine[3];
151:                                 String resource = splitLine[0];
152:
153:                                 IMessage msg = new Message(resource, number, startLine, startCol, message);
154:
155:•                                switch (type)
156:                                 {
157:                                         case Error:
158:                                                 errors.add(msg);
159:                                                 break;
160:                                         case Warning:
161:                                                 warnings.add(msg);
162:                                                 break;
163:                                         case Result:
164:                                                 result = message;
165:                                                 break;
166:                                 }
167:
168:                         }
169:
170:                 } catch (FileNotFoundException e)
171:                 {
172:                         return false;
173:                 } catch (IOException e)
174:                 {
175:                         return false;
176:                 } finally
177:                 {
178:                         try
179:                         {
180:                                 in.close();
181:                         } catch (IOException e)
182:                         {
183:                         }
184:                 }
185:
186:                 return true;
187:         }
188:
189:         public boolean save()
190:         {
191:                 try
192:                 {
193:                         BufferedWriter out = new BufferedWriter(new FileWriter(file));
194:
195:                         writeMessageSet(out, WARNING_LABEL, warnings);
196:                         writeMessageSet(out, ERROR_LABEL, errors);
197:                         writeResult(out, result);
198:
199:                         out.flush();
200:                         out.close();
201:                 } catch (IOException e)
202:                 {
203:                         return false;
204:                 }
205:
206:                 return true;
207:         }
208:
209:         private void writeResult(BufferedWriter out, String result2)
210:                         throws IOException
211:         {
212:                 StringBuffer sb = new StringBuffer();
213:                 sb.append(RESULT_LABEL);
214:                 sb.append(":");
215:                 sb.append("result");
216:                 sb.append(":");
217:                 sb.append(-1);
218:                 sb.append(":");
219:                 sb.append(-1);
220:                 sb.append(",");
221:                 sb.append(-1);
222:                 sb.append(":");
223:                 sb.append(result2.replace(':', '\''));
224:                 out.write(sb.toString());
225:                 out.newLine();
226:         }
227:
228:         public void writeMessageSet(BufferedWriter out, String label,
229:                         List<IMessage> list) throws IOException
230:         {
231:•                for (IMessage m : list)
232:                 {
233:                         StringBuffer sb = new StringBuffer();
234:                         sb.append(label);
235:                         sb.append(":");
236:                         sb.append(m.getResource());
237:                         sb.append(":");
238:                         sb.append(m.getNumber());
239:                         sb.append(":");
240:                         sb.append(m.getLine());
241:                         sb.append(",");
242:                         sb.append(m.getCol());
243:                         sb.append(":");
244:                         sb.append(m.getMessage().replace(':', '\''));
245:                         out.write(sb.toString());
246:                         out.newLine();
247:                 }
248:         }
249:
250: }