1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package net.sourceforge.acelogger.test;
16
17 import java.io.IOException;
18 import java.lang.reflect.Method;
19 import java.net.URL;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.scannotation.AnnotationDB;
24 import org.scannotation.ClasspathUrlFinder;
25 import org.testng.annotations.DataProvider;
26 import org.testng.annotations.Test;
27
28 import javassist.NotFoundException;
29
30 import javassist.ClassPool;
31 import javassist.CtClass;
32 import javassist.CtMethod;
33
34
35
36
37
38
39
40
41 public final class DataProviderExporter {
42
43 private DataProviderExporter() {
44 }
45
46 public static void main(String[] args) {
47 ClassPool pool = ClassPool.getDefault();
48 URL[] urls = ClasspathUrlFinder.findClassPaths();
49 AnnotationDB db = new AnnotationDB();
50 try {
51 db.scanArchives(urls);
52 Map<String, Set<String>> annotatedClasses = db.getAnnotationIndex();
53 Set<String> testClasses = annotatedClasses.get(Test.class.getName());
54 for (String testClass : testClasses) {
55 CtClass currentClass;
56 try {
57 currentClass = pool.get(testClass);
58 for (CtMethod currentMethod : currentClass.getDeclaredMethods()) {
59 Object[] annotations;
60 try {
61 annotations = currentMethod.getAnnotations();
62 for (Object currentAnnotation : annotations) {
63 if (currentAnnotation instanceof DataProvider) {
64 String identifier = currentClass.getName() + "#"
65 + currentMethod.getName();
66 try {
67 Class<?> desiredClass = Class.forName(currentClass
68 .getName());
69 Method desiredMethod = desiredClass.getMethod(currentMethod
70 .getName());
71 Object[][] data = (Object[][]) desiredMethod
72 .invoke(desiredClass.newInstance());
73 DataProviderHelper.saveDataFile(identifier, data);
74 } catch (Exception e) {
75
76 e.printStackTrace();
77 }
78 }
79 }
80 } catch (ClassNotFoundException e1) {
81
82 e1.printStackTrace();
83 }
84 }
85 } catch (NotFoundException e1) {
86
87 e1.printStackTrace();
88 }
89 }
90 } catch (IOException e1) {
91
92 e1.printStackTrace();
93 }
94 }
95
96 }