Package: BaseTestSuite

BaseTestSuite

nameinstructionbranchcomplexitylinemethod
BaseTestSuite()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
BaseTestSuite(String)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
createCompleteFile(TestSuite, File, Constructor, Constructor, File, String[])
M: 18 C: 58
76%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 2 C: 11
85%
M: 0 C: 1
100%
createDirectory(TestSuite, File, Constructor, String[])
M: 28 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
createTestCompleteDirectory(String, String, Class, String[])
M: 53 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
createTestCompleteFile(String, String, Class, String[])
M: 18 C: 51
74%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 2 C: 9
82%
M: 0 C: 1
100%
createTestSingleLineFile(String, String, Class, String[])
M: 53 C: 60
53%
M: 7 C: 7
50%
M: 5 C: 3
38%
M: 7 C: 9
56%
M: 0 C: 1
100%
getFile(String)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isNotAcceptedFile(File, List)
M: 2 C: 45
96%
M: 4 C: 10
71%
M: 4 C: 4
50%
M: 1 C: 8
89%
M: 0 C: 1
100%
readFile(File)
M: 45 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
test()
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 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.framework;
23:
24: import java.io.BufferedReader;
25: import java.io.File;
26: import java.io.FileReader;
27: import java.io.IOException;
28: import java.lang.reflect.Constructor;
29: import java.lang.reflect.InvocationTargetException;
30: import java.util.Arrays;
31: import java.util.List;
32: import java.util.Vector;
33:
34: import junit.framework.Test;
35: import junit.framework.TestSuite;
36:
37: public class BaseTestSuite extends TestSuite
38: {
39:
40:         public BaseTestSuite(String name)
41:         {
42:                 super(name);
43:         }
44:
45:         public BaseTestSuite()
46:         {
47:
48:         }
49:
50:         public void test()
51:         {
52:         }
53:
54:         /**
55:          * Utility method to create a test suite from files
56:          *
57:          * @param name
58:          * the suite name
59:          * @param testRootPath
60:          * the root folder to start the file search
61:          * @param testCase
62:          * The test case class instantiated. It must have a constructor taking a {@link File}
63:          * @param extensions
64:          * an array of accepted extensions. If none are given all files are accepted. Empty sequence for the
65:          * empty extension.
66:          * @return a new test suite
67:          * @throws IllegalArgumentException
68:          * @throws InstantiationException
69:          * @throws IllegalAccessException
70:          * @throws InvocationTargetException
71:          * @throws SecurityException
72:          * @throws NoSuchMethodException
73:          */
74:         @SuppressWarnings({ "unchecked", "rawtypes" })
75:         protected static TestSuite createTestCompleteDirectory(String name,
76:                         String testRootPath, Class testCase, String... extensions)
77:                         throws IllegalArgumentException, InstantiationException,
78:                         IllegalAccessException, InvocationTargetException,
79:                         SecurityException, NoSuchMethodException
80:         {
81:                 File testRoot = getFile(testRootPath.replace('/', File.separatorChar));
82:                 Constructor ctor = testCase.getConstructor(new Class[] { File.class });
83:                 TestSuite suite = new BaseTestSuite(name);
84:
85:•                if (testRoot != null && testRoot.exists())
86:                 {
87:
88:•                        for (File file : testRoot.listFiles())
89:                         {
90:•                                if (file.isDirectory())
91:                                 {
92:                                         createDirectory(suite, file, ctor);
93:                                 }
94:                         }
95:                 }
96:                 return suite;
97:
98:         }
99:
100:         /**
101:          * Utility method to create a test suite from files
102:          *
103:          * @param name
104:          * the suite name
105:          * @param testRootPath
106:          * the root folder to start the file search
107:          * @param testCase
108:          * The test case class instantiated. It must have a constructor taking a {@link File} or a {@link File},
109:          * {@link String} and {@link File}
110:          * @param extensions
111:          * an array of accepted extensions. If none are given all files are accepted. Empty sequence for the
112:          * empty extension.
113:          * @return a new test suite
114:          * @throws IllegalArgumentException
115:          * @throws InstantiationException
116:          * @throws IllegalAccessException
117:          * @throws InvocationTargetException
118:          * @throws SecurityException
119:          * @throws NoSuchMethodException
120:          */
121:         @SuppressWarnings({ "unchecked", "rawtypes" })
122:         protected static TestSuite createTestCompleteFile(String name,
123:                         String testRootPath, Class testCase, String... extensions)
124:                         throws IllegalArgumentException, InstantiationException,
125:                         IllegalAccessException, InvocationTargetException,
126:                         SecurityException, NoSuchMethodException
127:         {
128:                 File testRoot = getFile(testRootPath);
129:                 Constructor ctor = testCase.getConstructor(new Class[] { File.class });
130:                 Constructor ctorCustom = null;
131:                 try
132:                 {
133:                         ctorCustom = testCase.getConstructor(new Class[] { File.class,
134:                                         String.class, File.class });
135:                 } catch (Exception e)
136:                 {
137:                 }
138:                 TestSuite suite = new BaseTestSuite(name);
139:
140:•                if (testRoot != null && testRoot.exists())
141:                 {
142:
143:•                        for (File file : testRoot.listFiles())
144:                         {
145:                                 createCompleteFile(suite, file, ctor, ctorCustom, testRoot, extensions);
146:                         }
147:                 }
148:                 return suite;
149:
150:         }
151:
152:         private static void createCompleteFile(TestSuite suite, File file,
153:                         @SuppressWarnings("rawtypes") Constructor ctor,
154:                         @SuppressWarnings("rawtypes") Constructor ctorCustom,
155:                         File testRoot, String... extensions)
156:                         throws IllegalArgumentException, InstantiationException,
157:                         IllegalAccessException, InvocationTargetException
158:         {
159:•                if (file.getName().startsWith(".")
160:•                                || !isNotAcceptedFile(file, Arrays.asList(extensions)))// file.getName().endsWith(".assert")||file.getName().endsWith(".vdmj")
161:                                                                                                                                                 // || file.getName().endsWith(".result")||
162:                                                                                                                                                 // file.getName().endsWith(".entry"))
163:                 {
164:                         return;
165:                 }
166:•                if (file.isDirectory())
167:                 {
168:•                        for (File f : file.listFiles())
169:                         {
170:                                 createCompleteFile(suite, f, ctor, ctorCustom, testRoot, extensions);
171:                         }
172:                 } else
173:                 {
174:                         // System.out.println("Creating test for:" + file);
175:                         Object instance = null;
176:•                        if (ctorCustom == null)
177:                         {
178:                                 instance = ctor.newInstance(new Object[] { file });
179:                         } else
180:                         {
181:                                 instance = ctorCustom.newInstance(new Object[] { file,
182:                                                 suite.getName(), testRoot });
183:                         }
184:                         suite.addTest((Test) instance);
185:                 }
186:
187:         }
188:
189:         private static boolean isNotAcceptedFile(File file, List<String> extensions)
190:         {
191:•                if (extensions == null || extensions.isEmpty() || file.isDirectory())
192:                 {
193:                         return true;
194:                 }
195:•                for (String ext : extensions)
196:                 {
197:•                        if (ext.isEmpty() && !file.getName().contains("."))
198:                         {
199:                                 return true;
200:                         }
201:•                        if (file.getName().endsWith("." + ext))
202:                         {
203:                                 return true;
204:                         }
205:                         // System.out.println("Skipping: "+file.getName());
206:                 }
207:                 return false;
208:         }
209:
210:         private static void createDirectory(TestSuite suite, File file,
211:                         @SuppressWarnings("rawtypes") Constructor ctor,
212:                         String... extensions) throws IllegalArgumentException,
213:                         InstantiationException, IllegalAccessException,
214:                         InvocationTargetException
215:         {
216:•                if (file.getName().startsWith(".")
217:•                                || !isNotAcceptedFile(file, Arrays.asList(extensions)))
218:                 {
219:                         return;
220:                 }
221:•                if (file.isDirectory())
222:                 {
223:                         // System.out.println("Creating test for:" + file);
224:                         Object instance = ctor.newInstance(new Object[] { file });
225:                         suite.addTest((Test) instance);
226:                 }
227:
228:         }
229:
230:         /**
231:          * Utility method to create a test suite from files
232:          *
233:          * @param name
234:          * the suite name
235:          * @param testRootPath
236:          * the root folder to start the file search
237:          * @param testCase
238:          * The test case class instantiated. It must have a constructor taking a {@link File}, {@link String} and
239:          * {@link String}
240:          * @param extensions
241:          * an array of accepted extensions. If none are given all files are accepted. Empty sequence for the
242:          * empty extension.
243:          * @return a new test suite
244:          * @throws IllegalArgumentException
245:          * @throws InstantiationException
246:          * @throws IllegalAccessException
247:          * @throws InvocationTargetException
248:          * @throws SecurityException
249:          * @throws NoSuchMethodException
250:          * @throws IOException
251:          */
252:         @SuppressWarnings({ "unchecked", "rawtypes" })
253:         protected static TestSuite createTestSingleLineFile(String name,
254:                         String testRootPath, Class testCase, String... extensions)
255:                         throws IllegalArgumentException, InstantiationException,
256:                         IllegalAccessException, InvocationTargetException,
257:                         SecurityException, NoSuchMethodException, IOException
258:         {
259:                 File testRoot = getFile(testRootPath);
260:                 Constructor ctor = testCase.getConstructor(new Class[] { File.class,
261:                                 String.class, String.class });
262:                 TestSuite suite = new BaseTestSuite(name);
263:
264:•                if (testRoot != null && testRoot.exists())
265:                 {
266:•                        for (File file : testRoot.listFiles())
267:                         {
268:•                                if (file.getName().startsWith(".")
269:•                                                || isNotAcceptedFile(file, Arrays.asList(extensions)))// ||file.getName().endsWith("_results"))
270:                                 {
271:                                         continue;
272:                                 }
273:                                 List<String> lines = readFile(file);
274:•                                if (lines != null)
275:                                 {
276:•                                        for (int i = 0; i < lines.size(); i++)
277:                                         {
278:                                                 Object instance = ctor.newInstance(new Object[] { file,
279:                                                                 file.getName() + "_L" + i + "_" + lines.get(i),
280:                                                                 lines.get(i) });
281:                                                 suite.addTest((Test) instance);
282:                                         }
283:                                 }
284:
285:                         }
286:                 }
287:                 return suite;
288:
289:         }
290:
291:         protected static List<String> readFile(File file) throws IOException
292:         {
293:                 List<String> lines = new Vector<String>();
294:                 BufferedReader reader = null;
295:
296:                 try
297:                 {
298:                         reader = new BufferedReader(new FileReader(file));
299:                         String text = null;
300:
301:                         // repeat until all lines is read
302:•                        while ((text = reader.readLine()) != null)
303:                         {
304:•                                if (text.trim().length() > 0 && !text.trim().startsWith("//"))
305:                                 {
306:                                         lines.add(text);
307:                                 }
308:                         }
309:                         return lines;
310:                 } finally
311:                 {
312:                         try
313:                         {
314:•                                if (reader != null)
315:                                 {
316:                                         reader.close();
317:                                 }
318:                         } catch (IOException e)
319:                         {
320:                         }
321:                 }
322:         }
323:
324:         protected static File getFile(String pathname)
325:         {
326:                 return new File(pathname.replace('\\', File.separatorChar).replace('/', File.separatorChar));
327:         }
328: }