1 package org.openecomp.sdc.uici.tests.run;
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;
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;
17 import org.apache.log4j.Logger;
18 import org.apache.log4j.PropertyConfigurator;
19 import org.testng.TestNG;
21 import org.openecomp.sdc.ci.tests.config.Config;
22 import org.openecomp.sdc.ci.tests.utils.Utils;
24 public class StartTest {
26 public static long timeOfTest = 0;
28 public static boolean debug = false;
30 public static AtomicBoolean loggerInitialized = new AtomicBoolean(false);
32 protected static Logger logger = null;
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")) {
40 System.out.println("Debug mode is " + (debug ? "enabled" : "disabled"));
46 config = Utils.getConfig();
47 } catch (FileNotFoundException e) {
48 // TODO Auto-generated catch block
53 logger.error("Failed to configuration file of ci tests.");
57 TestNG testng = new TestNG();
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/");
70 logger = Logger.getLogger(StartTest.class.getName());
73 public static void enableLogger() {
75 if (false == loggerInitialized.get()) {
77 loggerInitialized.set(true);
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;
88 PropertyConfigurator.configureAndWatch(log4jPropsFile);
93 private List<Class> getClassesForPackage(String pkgname) {
95 List<Class> classes = new ArrayList<Class>();
97 // Get a File object for the package
98 File directory = null;
100 String relPath = pkgname.replace('.', '/');
102 // System.out.println("ClassDiscovery: Package: " + pkgname +
103 // " becomes Path:" + relPath);
105 URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
107 // System.out.println("ClassDiscovery: Resource = " + resource);
108 if (resource == null) {
109 throw new RuntimeException("No resource for " + relPath);
111 fullPath = resource.getFile();
112 // System.out.println("ClassDiscovery: FullPath = " + resource);
115 System.out.println("fullPath is " + fullPath);
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...",
125 } catch (IllegalArgumentException e) {
128 // System.out.println("ClassDiscovery: Directory = " + directory);
130 if (directory != null && directory.exists()) {
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++) {
136 // we are only interested in .class files
137 if (files[i].endsWith(".class") && false == files[i].contains("$")) {
139 // removes the .class extension
140 String className = pkgname + '.' + files[i].substring(0, files[i].length() - 6);
142 // System.out.println("ClassDiscovery: className = " +
146 System.out.println("ClassDiscovery: className = " + className);
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")) {
164 } catch (ClassNotFoundException e) {
165 throw new RuntimeException("ClassNotFoundException loading " + className);
171 String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
174 System.out.println("jarPath is " + jarPath);
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())) {
184 // System.out.println("ClassDiscovery: JarEntry: " +
186 String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
188 // System.out.println("ClassDiscovery: className = " +
191 if (false == className.contains("$")) {
194 System.out.println("ClassDiscovery: className = " + className);
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")) {
212 } catch (ClassNotFoundException e) {
213 throw new RuntimeException("ClassNotFoundException loading " + className);
220 } catch (IOException e) {
221 throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
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>");
234 // private void addUnitTestResult(StringBuilder results,
235 // Class<? extends AttSdcTest> testClass, Result unitTestResult) {
237 // boolean isSuccess = unitTestResult.wasSuccessful();
239 // String result = (isSuccess) ? "success" : "fail";
240 // String fileName = FileUtils.getFileName(testClass.getName());
241 // results.append("<tr>");
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)
249 // results.append("</tr>");