1   /*
2    * This file is part of AceLogger.
3    * 
4    * AceLogger is free software: you can redistribute it and/or modify it under the terms of the GNU
5    * Lesser General Public License as published by the Free Software Foundation, either version 3 of
6    * the License, or (at your option) any later version.
7    * 
8    * AceLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9    * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10   * Lesser General Public License for more details.
11   * 
12   * You should have received a copy of the GNU Lesser General Public License along with AceLogger.
13   * If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
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   * TODO: Create Doc.
36   * 
37   * @author Zardi (https://sourceforge.net/users/daniel_zardi)
38   * @version 1.0.0
39   * @since 1.0.0
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  										// TODO: handle exception
76  										e.printStackTrace();
77  									}
78  								}
79  							}
80  						} catch (ClassNotFoundException e1) {
81  							// TODO Auto-generated catch block
82  							e1.printStackTrace();
83  						}
84  					}
85  				} catch (NotFoundException e1) {
86  					// TODO Auto-generated catch block
87  					e1.printStackTrace();
88  				}
89  			}
90  		} catch (IOException e1) {
91  			// TODO Auto-generated catch block
92  			e1.printStackTrace();
93  		}
94  	}
95  
96  }