[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci-dev / src / main / java / org / openecomp / sdc / uici / tests / run / StartTest.java
1 package org.openecomp.sdc.uici.tests.run;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.lang.annotation.Annotation;
7 import java.lang.reflect.Method;
8 import java.net.URISyntaxException;
9 import java.net.URL;
10 import java.util.ArrayList;
11 import java.util.Enumeration;
12 import java.util.List;
13 import java.util.concurrent.atomic.AtomicBoolean;
14 import java.util.jar.JarEntry;
15 import java.util.jar.JarFile;
16
17 import org.apache.log4j.Logger;
18 import org.apache.log4j.PropertyConfigurator;
19 import org.testng.TestNG;
20
21 import org.openecomp.sdc.ci.tests.config.Config;
22 import org.openecomp.sdc.ci.tests.utils.Utils;
23
24 public class StartTest {
25
26         public static long timeOfTest = 0;
27
28         public static boolean debug = false;
29
30         public static AtomicBoolean loggerInitialized = new AtomicBoolean(false);
31
32         protected static Logger logger = null;
33
34         public static void main(String[] args) {
35                 // TODO ui-ci add jar building
36                 String debugEnabled = System.getProperty("debug");
37                 if (debugEnabled != null && debugEnabled.equalsIgnoreCase("true")) {
38                         debug = true;
39                 }
40                 System.out.println("Debug mode is " + (debug ? "enabled" : "disabled"));
41
42                 enableLogger();
43
44                 Config config = null;
45                 try {
46                         config = Utils.getConfig();
47                 } catch (FileNotFoundException e) {
48                         // TODO Auto-generated catch block
49                         e.printStackTrace();
50                 }
51
52                 if (config == null) {
53                         logger.error("Failed to configuration file of ci tests.");
54                         System.exit(1);
55                 }
56
57                 TestNG testng = new TestNG();
58
59                 List<String> suites = new ArrayList<String>();
60                 suites.add("testSuites/" + args[0]);
61                 testng.setTestSuites(suites);
62                 // testng.setUseDefaultListeners(true);
63                 testng.setOutputDirectory("target/");
64
65                 testng.run();
66
67         }
68
69         public StartTest() {
70                 logger = Logger.getLogger(StartTest.class.getName());
71         }
72
73         public static void enableLogger() {
74
75                 if (false == loggerInitialized.get()) {
76
77                         loggerInitialized.set(true);
78
79                         String log4jPropsFile = System.getProperty("log4j.configuration");
80                         if (System.getProperty("os.name").contains("Windows")) {
81                                 String logProps = "src/main/resources/ci/conf/log4j.properties";
82                                 if (log4jPropsFile == null) {
83                                         System.setProperty("targetlog", "target/");
84                                         log4jPropsFile = logProps;
85                                 }
86
87                         }
88                         PropertyConfigurator.configureAndWatch(log4jPropsFile);
89
90                 }
91         }
92
93         private List<Class> getClassesForPackage(String pkgname) {
94
95                 List<Class> classes = new ArrayList<Class>();
96
97                 // Get a File object for the package
98                 File directory = null;
99                 String fullPath;
100                 String relPath = pkgname.replace('.', '/');
101
102                 // System.out.println("ClassDiscovery: Package: " + pkgname +
103                 // " becomes Path:" + relPath);
104
105                 URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
106
107                 // System.out.println("ClassDiscovery: Resource = " + resource);
108                 if (resource == null) {
109                         throw new RuntimeException("No resource for " + relPath);
110                 }
111                 fullPath = resource.getFile();
112                 // System.out.println("ClassDiscovery: FullPath = " + resource);
113
114                 if (debug) {
115                         System.out.println("fullPath is " + fullPath);
116                 }
117
118                 try {
119                         directory = new File(resource.toURI());
120                 } catch (URISyntaxException e) {
121                         throw new RuntimeException(
122                                         pkgname + " (" + resource
123                                                         + ") does not appear to be a valid URL / URI.  Strange, since we got it from the system...",
124                                         e);
125                 } catch (IllegalArgumentException e) {
126                         directory = null;
127                 }
128                 // System.out.println("ClassDiscovery: Directory = " + directory);
129
130                 if (directory != null && directory.exists()) {
131
132                         // Get the list of the files contained in the package
133                         String[] files = directory.list();
134                         for (int i = 0; i < files.length; i++) {
135
136                                 // we are only interested in .class files
137                                 if (files[i].endsWith(".class") && false == files[i].contains("$")) {
138
139                                         // removes the .class extension
140                                         String className = pkgname + '.' + files[i].substring(0, files[i].length() - 6);
141
142                                         // System.out.println("ClassDiscovery: className = " +
143                                         // className);
144
145                                         if (debug) {
146                                                 System.out.println("ClassDiscovery: className = " + className);
147                                         }
148
149                                         try {
150                                                 Class clas = Class.forName(className);
151                                                 boolean isAddToRun = false;
152                                                 Method[] methods = clas.getMethods();
153                                                 for (Method method : methods) {
154                                                         Annotation[] anns = method.getAnnotations();
155                                                         for (Annotation an : anns) {
156                                                                 if (an.annotationType().getSimpleName().equalsIgnoreCase("Test")) {
157                                                                         isAddToRun = true;
158                                                                         break;
159                                                                 }
160                                                         }
161                                                 }
162                                                 if (isAddToRun)
163                                                         classes.add(clas);
164                                         } catch (ClassNotFoundException e) {
165                                                 throw new RuntimeException("ClassNotFoundException loading " + className);
166                                         }
167                                 }
168                         }
169                 } else {
170                         try {
171                                 String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
172
173                                 if (debug) {
174                                         System.out.println("jarPath is " + jarPath);
175                                 }
176
177                                 JarFile jarFile = new JarFile(jarPath);
178                                 Enumeration<JarEntry> entries = jarFile.entries();
179                                 while (entries.hasMoreElements()) {
180                                         JarEntry entry = entries.nextElement();
181                                         String entryName = entry.getName();
182                                         if (entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) {
183
184                                                 // System.out.println("ClassDiscovery: JarEntry: " +
185                                                 // entryName);
186                                                 String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
187
188                                                 // System.out.println("ClassDiscovery: className = " +
189                                                 // className);
190
191                                                 if (false == className.contains("$")) {
192
193                                                         if (debug) {
194                                                                 System.out.println("ClassDiscovery: className = " + className);
195                                                         }
196
197                                                         try {
198                                                                 Class clas = Class.forName(className);
199                                                                 boolean isAddToRun = false;
200                                                                 Method[] methods = clas.getMethods();
201                                                                 for (Method method : methods) {
202                                                                         Annotation[] anns = method.getAnnotations();
203                                                                         for (Annotation an : anns) {
204                                                                                 if (an.annotationType().getSimpleName().equalsIgnoreCase("Test")) {
205                                                                                         isAddToRun = true;
206                                                                                         break;
207                                                                                 }
208                                                                         }
209                                                                 }
210                                                                 if (isAddToRun)
211                                                                         classes.add(clas);
212                                                         } catch (ClassNotFoundException e) {
213                                                                 throw new RuntimeException("ClassNotFoundException loading " + className);
214                                                         }
215                                                 }
216                                         }
217                                 }
218                                 jarFile.close();
219
220                         } catch (IOException e) {
221                                 throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
222                         }
223                 }
224                 return classes;
225         }
226
227         private void addTableHead(StringBuilder results) {
228                 results.append("<tr>");
229                 results.append("<th>").append("Unit Test").append("</th>");
230                 results.append("<th>").append("Result").append("</th>");
231                 results.append("</tr>");
232         }
233
234         // private void addUnitTestResult(StringBuilder results,
235         // Class<? extends AttSdcTest> testClass, Result unitTestResult) {
236         //
237         // boolean isSuccess = unitTestResult.wasSuccessful();
238         //
239         // String result = (isSuccess) ? "success" : "fail";
240         // String fileName = FileUtils.getFileName(testClass.getName());
241         // results.append("<tr>");
242         // //
243         // results.append("<td>").append(FileUtils.getFileName(testClass.getName())).append("</td>");
244         // results.append("<td class=\"name\">")
245         // .append("<a href=\"" + fileName + timeOfTest + ".html\">"
246         // + fileName + "</a>").append("</td>");
247         // results.append("<td class=\"" + result + "\">").append(result)
248         // .append("</td>");
249         // results.append("</tr>");
250         // }
251
252 }