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;
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.core.LoggerContext;
26 import org.junit.runner.JUnitCore;
27 import org.junit.runner.Result;
28 import org.junit.runner.notification.Failure;
29 import org.openecomp.sdc.ci.tests.api.AttSdcTest;
30 import org.openecomp.sdc.ci.tests.config.Config;
31 import org.openecomp.sdc.ci.tests.utils.Utils;
32 import org.openecomp.sdc.ci.tests.utils.general.FileUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39 import java.lang.annotation.Annotation;
40 import java.lang.reflect.Method;
41 import java.net.URISyntaxException;
43 import java.text.SimpleDateFormat;
44 import java.util.ArrayList;
45 import java.util.Calendar;
46 import java.util.Enumeration;
47 import java.util.List;
48 import java.util.concurrent.atomic.AtomicBoolean;
49 import java.util.jar.JarEntry;
50 import java.util.jar.JarFile;
52 public class StartTest2backup {
54 private List<Class<? extends AttSdcTest>> testClasses = new ArrayList<Class<? extends AttSdcTest>>();
55 public static long timeOfTest = 0;
57 public static boolean debug = false;
59 public static AtomicBoolean loggerInitialized = new AtomicBoolean(false);
61 protected static Logger logger = null;
63 public static void main(String[] args) {
65 String debugEnabled = System.getProperty("debug");
66 if (debugEnabled != null && debugEnabled.equalsIgnoreCase("true")) {
69 System.out.println("Debug mode is " + (debug ? "enabled" : "disabled"));
75 config = Utils.getConfig();
76 } catch (FileNotFoundException e) {
81 logger.error("Failed to configuration file of ci tests.");
85 List<String> packagesToRun = config.getPackages();
86 if (packagesToRun == null || true == packagesToRun.isEmpty()) {
87 logger.error("No package was configured to be executed.");
90 StartTest2backup tests = new StartTest2backup();
92 boolean stopOnClassFailure = false;
93 String stopOnClassFailureStr = System.getProperty("stopOnClassFailure");
94 if (stopOnClassFailureStr != null && stopOnClassFailureStr.equalsIgnoreCase("true")) {
95 stopOnClassFailure = true;
97 Boolean stopOnClassFailureObj = config.isStopOnClassFailure();
98 if (stopOnClassFailureObj != null) {
99 stopOnClassFailure = stopOnClassFailureObj.booleanValue();
103 tests.start(packagesToRun, stopOnClassFailure);
106 public StartTest2backup() {
107 logger = LoggerFactory.getLogger(StartTest2backup.class.getName());
110 public static void enableLogger() {
112 if (false == loggerInitialized.get()) {
114 loggerInitialized.set(true);
116 String log4jPropsFile = System.getProperty("log4j.configuration");
117 if (System.getProperty("os.name").contains("Windows")) {
118 String logProps = "src/main/resources/ci/conf/log4j2.properties";
119 if (log4jPropsFile == null) {
120 System.setProperty("targetlog", "target/");
121 log4jPropsFile = logProps;
126 LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
127 File file = new File(log4jPropsFile);
128 context.setConfigLocation(file.toURI());
132 public void start(List<String> packages, boolean exitOnFailure) {
134 boolean success = true;
135 StringBuilder results = new StringBuilder();
138 if (packages == null) {
142 for (String packageName : packages) {
143 // List<Class> classesForPackage =
144 // getClassesForPackage("org.openecomp.sdc.ci.tests.execute");
145 List<Class> classesForPackage = getClassesForPackage(packageName);
146 if (classesForPackage != null && false == classesForPackage.isEmpty()) {
147 for (Class testUnit : classesForPackage) {
148 testClasses.add(testUnit);
153 System.out.println(testClasses);
155 // tsetClasses.add(LogValidatorTest.class);
156 // tsetClasses.add(AttNorthboundTest.class);
159 "<Html><head><style>th{background-color: gray;color: white;height: 30px;}td {color: black;height: 30px;}.fail {background-color: #FF5555;width: 100px;text-align: center;}.success {background-color: #00FF00;width: 100px;text-align: center;}.name {width: 200px;background-color: #F0F0F0;}.message {width: 300px;background-color: #F0F0F0;}</style></head><body>");
161 Calendar calendar = Calendar.getInstance();
162 timeOfTest = calendar.getTimeInMillis();
163 SimpleDateFormat date_format = new SimpleDateFormat("MMM dd yyyy HH:mm:ss");
164 results.append("<br/><h2> This report generated on " + date_format.format(calendar.getTime()) + "</h2><br/>");
166 results.append("<table>");
167 addTableHead(results);
169 int size = testClasses.size();
172 int totalRunTests = 0;
173 int totalFailureTests = 0;
174 int totalIgnoreTests = 0;
175 int numOfFailureClasses = 0;
176 for (Class<? extends AttSdcTest> testClass : testClasses) {
180 StringBuilder builder = new StringBuilder();
181 String str = "***************************************************************************";
182 builder.append(str + "\n");
183 String current = "class " + index + "/" + size + " failure(" + numOfFailureClasses + ") + RUNS("
184 + totalRunTests + ")" + " FAILURES(" + totalFailureTests + ") IGNORED(" + totalIgnoreTests + ")";
185 int interval = ((str.length() - current.length() - 2) / 2);
186 String substring = str.substring(0, interval);
187 builder.append(substring + " " + current + " " + substring + "\n");
188 builder.append(str + "\n");
190 System.out.println(builder.toString());
192 logger.debug(builder.toString());
193 logger.debug("Going to run test class {}", testClass.getName());
195 result = JUnitCore.runClasses(testClass);
196 if (result.wasSuccessful() == false) {
197 numOfFailureClasses++;
199 logger.debug("Test class {} finished {}",testClass.getName(),(result.wasSuccessful() ? "OK." : " WITH ERROR."));
200 List<Failure> failures = result.getFailures();
201 if (failures != null) {
202 for (Failure failure : failures) {
203 logger.error("Test class {} failure test {}-{}",testClass.getName(),failure.getTestHeader(),failure.getTrace());
206 int runsPerClass = result.getRunCount();
207 int failuresPerClass = result.getFailureCount();
208 int ignoredPerClass = result.getIgnoreCount();
210 totalRunTests += runsPerClass;
211 totalFailureTests += failuresPerClass;
212 totalIgnoreTests += ignoredPerClass;
214 logger.debug("class {} Failed tests {} %",testClass.getName(),(failuresPerClass * 1.0) / runsPerClass * 100);
215 logger.debug("class {} Ignored tests {} %",testClass.getName(),(ignoredPerClass * 1.0) / runsPerClass * 100);
217 // List<Failure> failures = result.getFailures();
218 // if (failures != null) {
219 // for (Failure failure : failures) {
220 // System.err.println("9999999999" + failure.getTestHeader());
224 addUnitTestResult(results, testClass, result);
225 success &= result.wasSuccessful();
227 if (numOfFailureClasses > 0) {
228 // if (exitOnFailure) {
235 results.append("</table>");
236 results.append("<br/><h2> Tests Summary: </h2><br/>");
237 results.append("Total Runs : " + totalRunTests + "<br/>");
238 results.append("Total Failure : " + totalFailureTests + "<br/>");
239 results.append("Total: " + totalFailureTests + "/" + totalRunTests + "<br/>");
240 results.append("</html>");
242 FileUtils.writeToFile(Config.instance().getOutputFolder() + File.separator + Config.instance().getReportName(),
246 System.out.println("FAILURE");
247 logger.error("Failure tests : {} %",((totalFailureTests + totalIgnoreTests) * 1.0) / (totalRunTests + totalIgnoreTests));
248 logger.error("Ignored tests : {} %",(totalIgnoreTests * 1.0) / (totalRunTests + totalIgnoreTests));
252 System.out.println("SUCCESS");
255 private List<Class> getClassesForPackage(String pkgname) {
257 List<Class> classes = new ArrayList<Class>();
259 // Get a File object for the package
260 File directory = null;
262 String relPath = pkgname.replace('.', '/');
264 // System.out.println("ClassDiscovery: Package: " + pkgname +
265 // " becomes Path:" + relPath);
267 URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
269 // System.out.println("ClassDiscovery: Resource = " + resource);
270 if (resource == null) {
271 throw new RuntimeException("No resource for " + relPath);
273 fullPath = resource.getFile();
274 // System.out.println("ClassDiscovery: FullPath = " + resource);
277 System.out.println("fullPath is " + fullPath);
281 directory = new File(resource.toURI());
282 } catch (URISyntaxException e) {
283 throw new RuntimeException(
284 pkgname + " (" + resource
285 + ") does not appear to be a valid URL / URI. Strange, since we got it from the system...",
287 } catch (IllegalArgumentException e) {
290 // System.out.println("ClassDiscovery: Directory = " + directory);
292 if (directory != null && directory.exists()) {
294 // Get the list of the files contained in the package
295 String[] files = directory.list();
296 for (int i = 0; i < files.length; i++) {
298 // we are only interested in .class files
299 if (files[i].endsWith(".class") && false == files[i].contains("$")) {
301 // removes the .class extension
302 String className = pkgname + '.' + files[i].substring(0, files[i].length() - 6);
304 // System.out.println("ClassDiscovery: className = " +
308 System.out.println("ClassDiscovery: className = " + className);
312 Class clas = Class.forName(className);
313 boolean isAddToRun = false;
314 Method[] methods = clas.getMethods();
315 for (Method method : methods) {
316 Annotation[] anns = method.getAnnotations();
317 for (Annotation an : anns) {
318 if (an.annotationType().getSimpleName().equalsIgnoreCase("Test")) {
326 } catch (ClassNotFoundException e) {
327 throw new RuntimeException("ClassNotFoundException loading " + className);
333 String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
336 System.out.println("jarPath is " + jarPath);
339 JarFile jarFile = new JarFile(jarPath);
340 Enumeration<JarEntry> entries = jarFile.entries();
341 while (entries.hasMoreElements()) {
342 JarEntry entry = entries.nextElement();
343 String entryName = entry.getName();
344 if (entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) {
346 // System.out.println("ClassDiscovery: JarEntry: " +
348 String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
350 // System.out.println("ClassDiscovery: className = " +
353 if (false == className.contains("$")) {
356 System.out.println("ClassDiscovery: className = " + className);
360 Class clas = Class.forName(className);
361 boolean isAddToRun = false;
362 Method[] methods = clas.getMethods();
363 for (Method method : methods) {
364 Annotation[] anns = method.getAnnotations();
365 for (Annotation an : anns) {
366 if (an.annotationType().getSimpleName().equalsIgnoreCase("Test")) {
374 } catch (ClassNotFoundException e) {
375 throw new RuntimeException("ClassNotFoundException loading " + className);
382 } catch (IOException e) {
383 throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
389 private void addTableHead(StringBuilder results) {
390 results.append("<tr>");
391 results.append("<th>").append("Unit Test").append("</th>");
392 results.append("<th>").append("Result").append("</th>");
393 results.append("</tr>");
396 private void addUnitTestResult(StringBuilder results, Class<? extends AttSdcTest> testClass,
397 Result unitTestResult) {
399 boolean isSuccess = unitTestResult.wasSuccessful();
401 String result = (isSuccess) ? "success" : "fail";
402 String fileName = FileUtils.getFileName(testClass.getName());
403 results.append("<tr>");
404 // results.append("<td>").append(FileUtils.getFileName(testClass.getName())).append("</td>");
405 results.append("<td class=\"name\">")
406 .append("<a href=\"" + fileName + timeOfTest + ".html\">" + fileName + "</a>").append("</td>");
407 results.append("<td class=\"" + result + "\">").append(result).append("</td>");
408 results.append("</tr>");