Mercurial > hg > ThermostatQA
view src/org/thermostat/qa2/framework/TestRunner.java @ 202:b82b8bc167ed
Fixed thermostat setup
author | Zdenek Zambersky <zzambers@redhat.com> |
---|---|
date | Tue, 22 Sep 2015 18:43:48 +0200 |
parents | 877b403e8344 |
children | a74aa91ab44c |
line wrap: on
line source
/* ThermostatQA - test framework for Thermostat Monitoring Tool Copyright 2015 Red Hat, Inc. This file is part of ThermostatQA ThermostatQA is distributed under the GNU General Public License, version 2 or any later version (with a special exception described below, commonly known as the "Classpath Exception"). A copy of GNU General Public License (GPL) is included in this distribution, in the file COPYING. Linking ThermostatQA code with other modules is making a combined work based on ThermostatQA. Thus, the terms and conditions of the GPL cover the whole combination. As a special exception, the copyright holders of ThermostatQA give you permission to link this code with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on ThermostatQA code. If you modify ThermostatQA, you may extend this exception to your version of the software, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package org.thermostat.qa2.framework; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.thermostat.qa2.framework.annotations.*; import org.thermostat.qa2.framework.services.*; import org.thermostat.qa2.framework.utils.AnnotationUtilities; import org.thermostat.qa2.framework.utils.CommonUtilities; import org.thermostat.qa2.framework.utils.ThermostatUtilities; /** * * @author Zdeněk Žamberský */ public class TestRunner { static ArrayList<Service> services = new ArrayList(); boolean interrupted = false; String testClass; public void setTestClass(String name) { testClass = name; } public synchronized void interrupt() { interrupted = true; } public synchronized boolean isInterrupted() { return interrupted; } public void run() { try { Class c = Class.forName(testClass); runTestClass(c); } catch (Exception ex) { } } public void runTestClass(Class c) throws Exception { List<Method> methods = Arrays.asList(c.getDeclaredMethods()); List<Method> testMethods = AnnotationUtilities.getAnnotatedObjects(methods, Test.class); int testCount = testMethods.size(); if (testCount > 0) { long startTime = System.currentTimeMillis(); int passedCount = 0; int failedCount = 0; int errorCount = 0; int ignoredCount = 0; Object o = c.newInstance(); String className = c.getCanonicalName(); List<Method> beforeMethods = AnnotationUtilities.getAnnotatedObjects(methods, Before.class); boolean beforeMethodFailed = false; for (Method m : beforeMethods) { Throwable t = null; try { runThermostatSetup(m); CommonUtilities.printHeading("Running \"before\" method: " + m.getName() + " ..."); m.invoke(o); } catch (Throwable e) { t = e; beforeMethodFailed = true; } finally { runServiceCleanups(); } String logNameString = "@Before: " + className + "." + m.getName(); if (t != null) { System.out.println("ERROR: " + logNameString); t.printStackTrace(System.out); break; } else { System.out.println("PASSED: " + logNameString); } } for (Method m : testMethods) { TestResult result; boolean afterSetup = false; Throwable t = null; testrun: if (beforeMethodFailed || isInterrupted()) { result = TestResult.ERROR; } else { try { runThermostatSetup(m); afterSetup = true; if (isInterrupted()) { result = TestResult.ERROR; break testrun; } CommonUtilities.printHeading("Running test method: " + m.getName() + " ..."); m.invoke(o); result = TestResult.PASSED; } catch (Throwable e) { t = e; //e.printStackTrace(); System.out.println("INFO: thrown: " + e); result = afterSetup ? TestResult.FAILED : TestResult.ERROR; } finally { CommonUtilities.printHeading("Running automatic cleanup: ..."); runServiceCleanups(); } } String testName = className + "." + m.getName(); switch (result) { case PASSED: System.out.println("PASSED: " + testName); ++passedCount; break; case FAILED: System.out.println("FAILED: " + testName); if (t != null) { t.printStackTrace(System.out); } ++failedCount; break; case ERROR: System.out.println("ERROR: " + testName); if (t != null) { t.printStackTrace(System.out); } ++errorCount; break; case IGNORED: System.out.println("IGNORED: " + testName); ++ignoredCount; break; } } long duration = System.currentTimeMillis() - startTime; System.out.println("SUMMARY: " + className + " total: " + testCount + " passed: " + passedCount + " failed: " + failedCount + " error: " + errorCount + " ignored: " + ignoredCount + " duration: " + duration); } } public static void runThermostatSetup(Method testMethod) throws Exception { ThermostatSetupTarget setupTargetA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, ThermostatSetupTarget.class); if (setupTargetA == null) { return; } SetupThermostat setupThermostatA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, SetupThermostat.class); SetupStorage setupStorageA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, SetupStorage.class); RunStorage startStorageA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, RunStorage.class); RunAgent startAgentA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, RunAgent.class); RunGnomeKeyring startGnomeKeyringA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, RunGnomeKeyring.class); RunGui startGuiA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, RunGui.class); boolean setupThermostat = setupThermostatA != null; boolean setupStorage = setupStorageA != null; boolean startStorage = startStorageA != null; boolean startAgent = startAgentA != null; boolean startGnomeKeyring = startGnomeKeyringA != null; boolean startGui = startGuiA != null; String setupTarget = setupTargetA.value(); // setup storage if (setupStorage) { String storageType = setupStorageA.type(); boolean web = ThermostatQAConfig.getStorageTypeAsBoolean(storageType); boolean badAgentLogin = setupStorageA.badAgentLogin(); boolean badClientLogin = setupStorageA.badClientLogin(); boolean agentSaveOnExit = setupStorageA.agentSaveOnExit(); ThermostatUtilities.setupThermostat(setupTarget, web, badAgentLogin, badClientLogin, agentSaveOnExit, true, false); } // start storage if (startStorage) { String storageType = setupStorageA.type(); boolean web = ThermostatQAConfig.getStorageTypeAsBoolean(storageType); ThermostatStorage storage = new ThermostatStorage(setupTarget); storage.start(); if (web) { Tomcat tomcat = new Tomcat(); tomcat.start(); } } // start agent if (startAgent) { ThermostatAgent agent = new ThermostatAgent(setupTarget); agent.start(); } // start gnome keyring if (startGnomeKeyring) { GnomeKeyring keyring = new GnomeKeyring(); keyring.start(); } // start gui if (startGui) { ThermostatGui gui = new ThermostatGui(setupTarget); gui.start(); } } public static void registerServiceCleanup(Service service) { services.add(service); } public static void unregisterServiceCleanup(Service service) { services.remove(service); } public static void runServiceCleanups() { for (int i = services.size() - 1; i >= 0; --i) { try { services.get(i).stop(); services.remove(i); } catch (Exception ex) { } } } public static void runTests(final TestRunner r) { final Thread testingThread = new Thread() { @Override public void run() { r.run(); } }; final Thread hookThread = new Thread() { @Override public void run() { try { r.interrupt(); if (testingThread.isAlive()) { testingThread.join(); } } catch (InterruptedException ex) { } } }; testingThread.setDaemon(true); Runtime runtime = Runtime.getRuntime(); runtime.addShutdownHook(hookThread); testingThread.start(); try { testingThread.join(); } catch (InterruptedException ex) { } runtime.removeShutdownHook(hookThread); System.exit(0); } public static void main(String[] args) { if (args.length > 0) { String className = args[0]; try { Class c = Class.forName(className); try { TestRunner runner = new TestRunner(); runner.testClass = className; runTests(runner); } catch (Exception ex) { Logger.getLogger(TestRunner.class.getName()).log(Level.SEVERE, null, ex); } } catch (ClassNotFoundException ex) { Logger.getLogger(TestRunner.class.getName()).log(Level.SEVERE, null, ex); } } } }