2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.ci.tests.run;
23 import org.apache.log4j.PropertyConfigurator;
24 import org.openecomp.sdc.ci.tests.config.Config;
25 import org.openecomp.sdc.ci.tests.utils.Utils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.testng.TestNG;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.lang.annotation.Annotation;
34 import java.lang.reflect.Method;
35 import java.net.URISyntaxException;
37 import java.util.ArrayList;
38 import java.util.Enumeration;
39 import java.util.List;
40 import java.util.concurrent.atomic.AtomicBoolean;
41 import java.util.jar.JarEntry;
42 import java.util.jar.JarFile;
44 public class StartTest {
46 // private List<Class<? extends AttSdcTest>> testClasses = new
47 // ArrayList<Class<? extends AttSdcTest>>();
48 public static long timeOfTest = 0;
50 public static boolean debug = false;
52 public static AtomicBoolean loggerInitialized = new AtomicBoolean(false);
54 protected static Logger logger = null;
56 public static void main(String[] args) {
58 String debugEnabled = System.getProperty("debug");
59 if (debugEnabled != null && debugEnabled.equalsIgnoreCase("true")) {
62 System.out.println("Debug mode is " + (debug ? "enabled" : "disabled"));
68 config = Utils.getConfig();
69 } catch (FileNotFoundException e) {
74 logger.error("Failed to configuration file of ci tests.");
78 TestNG testng = new TestNG();
80 List<String> suites = new ArrayList<String>();
81 suites.add("testSuites/" + args[0]);
82 testng.setTestSuites(suites);
83 testng.setUseDefaultListeners(true);
84 testng.setOutputDirectory("target/");
91 logger = LoggerFactory.getLogger(StartTest.class.getName());
94 public static void enableLogger() {
96 if (false == loggerInitialized.get()) {
98 loggerInitialized.set(true);
100 String log4jPropsFile = System.getProperty("log4j.configuration");
101 if (System.getProperty("os.name").contains("Windows")) {
102 String logProps = "src/main/resources/ci/conf/log4j.properties";
103 if (log4jPropsFile == null) {
104 System.setProperty("targetlog", "target/");
105 log4jPropsFile = logProps;
109 PropertyConfigurator.configureAndWatch(log4jPropsFile);
114 private List<Class> getClassesForPackage(String pkgname) {
116 List<Class> classes = new ArrayList<Class>();
118 // Get a File object for the package
119 File directory = null;
121 String relPath = pkgname.replace('.', '/');
123 // System.out.println("ClassDiscovery: Package: " + pkgname +
124 // " becomes Path:" + relPath);
126 URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
128 // System.out.println("ClassDiscovery: Resource = " + resource);
129 if (resource == null) {
130 throw new RuntimeException("No resource for " + relPath);
132 fullPath = resource.getFile();
133 // System.out.println("ClassDiscovery: FullPath = " + resource);
136 System.out.println("fullPath is " + fullPath);
140 directory = new File(resource.toURI());
141 } catch (URISyntaxException e) {
142 throw new RuntimeException(
143 pkgname + " (" + resource
144 + ") does not appear to be a valid URL / URI. Strange, since we got it from the system...",
146 } catch (IllegalArgumentException e) {
149 // System.out.println("ClassDiscovery: Directory = " + directory);
151 if (directory != null && directory.exists()) {
153 // Get the list of the files contained in the package
154 String[] files = directory.list();
155 for (int i = 0; i < files.length; i++) {
157 // we are only interested in .class files
158 if (files[i].endsWith(".class") && false == files[i].contains("$")) {
160 // removes the .class extension
161 String className = pkgname + '.' + files[i].substring(0, files[i].length() - 6);
163 // System.out.println("ClassDiscovery: className = " +
167 System.out.println("ClassDiscovery: className = " + className);
171 Class clas = Class.forName(className);
172 boolean isAddToRun = false;
173 Method[] methods = clas.getMethods();
174 for (Method method : methods) {
175 Annotation[] anns = method.getAnnotations();
176 for (Annotation an : anns) {
177 if (an.annotationType().getSimpleName().equalsIgnoreCase("Test")) {
185 } catch (ClassNotFoundException e) {
186 throw new RuntimeException("ClassNotFoundException loading " + className);
192 String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
195 System.out.println("jarPath is " + jarPath);
198 JarFile jarFile = new JarFile(jarPath);
199 Enumeration<JarEntry> entries = jarFile.entries();
200 while (entries.hasMoreElements()) {
201 JarEntry entry = entries.nextElement();
202 String entryName = entry.getName();
203 if (entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) {
205 // System.out.println("ClassDiscovery: JarEntry: " +
207 String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
209 // System.out.println("ClassDiscovery: className = " +
212 if (false == className.contains("$")) {
215 System.out.println("ClassDiscovery: className = " + className);
219 Class clas = Class.forName(className);
220 boolean isAddToRun = false;
221 Method[] methods = clas.getMethods();
222 for (Method method : methods) {
223 Annotation[] anns = method.getAnnotations();
224 for (Annotation an : anns) {
225 if (an.annotationType().getSimpleName().equalsIgnoreCase("Test")) {
233 } catch (ClassNotFoundException e) {
234 throw new RuntimeException("ClassNotFoundException loading " + className);
241 } catch (IOException e) {
242 throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
248 private void addTableHead(StringBuilder results) {
249 results.append("<tr>");
250 results.append("<th>").append("Unit Test").append("</th>");
251 results.append("<th>").append("Result").append("</th>");
252 results.append("</tr>");
255 // private void addUnitTestResult(StringBuilder results,
256 // Class<? extends AttSdcTest> testClass, Result unitTestResult) {
258 // boolean isSuccess = unitTestResult.wasSuccessful();
260 // String result = (isSuccess) ? "success" : "fail";
261 // String fileName = FileUtils.getFileName(testClass.getName());
262 // results.append("<tr>");
264 // results.append("<td>").append(FileUtils.getFileName(testClass.getName())).append("</td>");
265 // results.append("<td class=\"name\">")
266 // .append("<a href=\"" + fileName + timeOfTest + ".html\">"
267 // + fileName + "</a>").append("</td>");
268 // results.append("<td class=\"" + result + "\">").append(result)
270 // results.append("</tr>");