changeset 108:4b7bbc21aea4

Added support for selecting valid and non-valid tests according to the annotations which might be added to each test.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Thu, 03 Oct 2013 11:16:31 +0200
parents 793668818e6c
children 5daac2ad6ffe
files ChangeLog src/org/thermostat/qa/common/Configuration.java src/org/thermostat/qa/framework/ThermostatTest.java test.properties
diffstat 4 files changed, 154 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Oct 02 19:05:39 2013 +0200
+++ b/ChangeLog	Thu Oct 03 11:16:31 2013 +0200
@@ -1,3 +1,13 @@
+2013-10-03  Pavel Tisnovsky  <ptisnovs@redhat.com>
+
+	* src/org/thermostat/qa/common/Configuration.java:
+	Added missing setter.
+	* src/org/thermostat/qa/framework/ThermostatTest.java:
+	Added support for selecting valid and non-valid tests
+	according to the annotations which might be added to each test.
+	* test.properties:
+	Changed default version number.
+
 2013-10-02  Jana Fabrikova  <jfabriko@redhat.com>
 
 	* src/org/thermostat/qa/framework/ThermostatGuiTest.java:
--- a/src/org/thermostat/qa/common/Configuration.java	Wed Oct 02 19:05:39 2013 +0200
+++ b/src/org/thermostat/qa/common/Configuration.java	Thu Oct 03 11:16:31 2013 +0200
@@ -114,7 +114,12 @@
     {
         return this.thermostatVersion;
     }
-    
+
+    public void setThermostatVersion(String thermostatVersion)
+    {
+        this.thermostatVersion = thermostatVersion;
+    }
+
     public String getThermostatExecutable()
     {
         return this.thermostatExecutablePath + this.thermostatExecutableName;
--- a/src/org/thermostat/qa/framework/ThermostatTest.java	Wed Oct 02 19:05:39 2013 +0200
+++ b/src/org/thermostat/qa/framework/ThermostatTest.java	Thu Oct 03 11:16:31 2013 +0200
@@ -38,6 +38,9 @@
 import java.lang.reflect.Method;
 import java.util.List;
 
+import org.thermostat.qa.annotations.SinceVersion;
+import org.thermostat.qa.annotations.TillVersion;
+import org.thermostat.qa.annotations.ValidVersions;
 import org.thermostat.qa.common.Configuration;
 
 
@@ -51,6 +54,12 @@
  */
 public abstract class ThermostatTest extends ThermostatUtilities
 {
+    private static final int LT = -1;
+
+    private static final int GT = 1;
+
+    private static final int EQ = 0;
+
     /**
      * Number of tests that passed.
      */
@@ -67,6 +76,11 @@
     private int error = 0;
 
     /**
+     * Number of tests that have been ignored.
+     */
+    private int ignored = 0;
+
+    /**
      * Set up the test suite. To be implemented by specific test cases.
      */
     protected abstract void setUp();
@@ -103,7 +117,7 @@
             // check test duration
             long t2 = System.currentTimeMillis();
             // print all results
-            printResults(t1, t2, this.passed, this.failed, this.error);
+            printResults(t1, t2, this.passed, this.failed, this.error, this.ignored);
         }
     }
 
@@ -124,6 +138,99 @@
         }
     }
 
+    private boolean validTest(Method method) {
+        SinceVersion sinceVersion = method.getAnnotation(SinceVersion.class);
+        TillVersion tillVersion = method.getAnnotation(TillVersion.class);
+        ValidVersions validVersions = method.getAnnotation(ValidVersions.class);
+
+        // check if test has any annotation before other checks
+        if (tillVersion == null && sinceVersion == null && validVersions == null) {
+            return true;
+        }
+
+        String actualVersion = this.configuration.getThermostatVersion();
+
+        boolean sinceVersionOk = processSinceVersion(sinceVersion, actualVersion);
+        boolean tillVersionOk = processTillVersion(tillVersion, actualVersion);
+        boolean validVersionsOk = processValidVersions(validVersions, actualVersion);
+
+        return sinceVersionOk && tillVersionOk && validVersionsOk;
+    }
+
+    private boolean processSinceVersion(SinceVersion sinceVersion, String actualVersion)
+    {
+        if (sinceVersion != null)
+        {
+            String since = sinceVersion.value();
+            int comparison = compareVersions(since, actualVersion);
+            return comparison == LT || comparison == EQ;
+        }
+        // null
+        return true;
+    }
+
+    private boolean processTillVersion(TillVersion tillVersion, String actualVersion)
+    {
+        if (tillVersion != null)
+        {
+            String till = tillVersion.value();
+            int comparison = compareVersions(till, actualVersion);
+            return comparison == GT || comparison == EQ;
+        }
+        // null
+        return true;
+    }
+
+    private boolean processValidVersions(ValidVersions validVersions, String actualVersion) {
+        if (validVersions != null)
+        {
+            String[] versions = validVersions.value();
+            for (String version : versions)
+            {
+                if (compareVersions(version, actualVersion) == EQ)
+                {
+                    return true;
+                }
+            }
+            return false;
+        }
+        // null
+        return true;
+    }
+
+    private int compareVersions(String testVersion, String actualVersion) {
+        int[] testMajorMinorRelease = parseMajorMinorRelease(testVersion);
+        int[] actualMajorMinorRelease = parseMajorMinorRelease(actualVersion);
+        return performComparison(testMajorMinorRelease, actualMajorMinorRelease);
+    }
+
+    /**
+     * @param testMajorMinorRelease
+     * @param actualMajorMinorRelease
+     * @return
+     */
+    private int performComparison(int[] testMajorMinorRelease, int[] actualMajorMinorRelease) {
+        for (int i = 0; i < 3; i++)
+        {
+            int test = testMajorMinorRelease[i];
+            int actual = actualMajorMinorRelease[i];
+            if (test < actual) return LT;
+            if (test > actual) return GT;
+        }
+        return EQ;
+    }
+
+    /**
+     * Parse a string in a format "1.2.3" into an int array containing values [1,2,3].
+     * 
+     * @param versionString
+     * @return
+     */
+    private int[] parseMajorMinorRelease(String versionString) {
+        String[] str = versionString.split("\\.");
+        return new int[] {Integer.parseInt(str[0]), Integer.parseInt(str[1]), Integer.parseInt(str[2])};
+    }
+
     /**
      * Call a method if its name begins with "test" prefix. Such methods should
      * have public or protected access declaration.
@@ -139,6 +246,10 @@
         // all test methods should have a prefix "test"
         if (methodName.startsWith("test"))
         {
+            if (!validTest(method)) {
+                testIgnored(methodName);
+                return;
+            }
             try
             {
                 method.invoke(this);
@@ -209,6 +320,16 @@
     }
 
     /**
+     * @param methodName
+     * @param e
+     */
+    private void testIgnored(String methodName)
+    {
+        printTestIgnored(methodName);
+        this.ignored++;
+    }
+
+    /**
      * Print information about test which passed.
      * 
      * @param methodName
@@ -229,6 +350,16 @@
     }
 
     /**
+     * Print information about test which have been ignored.
+     * 
+     * @param methodName
+     *            name of method implementing the test.
+     */
+    private void printTestIgnored(String methodName) {
+        log("IGNORED", '.', methodName);
+    }
+
+    /**
      * Print information about test which failed due to other error.
      * 
      * @param methodName
@@ -249,14 +380,17 @@
      *            number of passed tests
      * @param failedTests
      *            number of failed tests
+     * @param ignoredTests
+     *            number of tests which have been ignored
      */
     private void printResults(long t1, long t2, int passedTests,
-            int failedTests, int errorTests)
+            int failedTests, int errorTests, int ignoredTests)
     {
-        log("SUMMARY", ' ', "   total: " + (passedTests + failedTests) +
+        log("SUMMARY", ' ', "   total: " + (passedTests + failedTests + errorTests + ignoredTests) +
             "    passed: " + passedTests +
             "    failed: " + failedTests +
             "    error: " + errorTests +
+            "    ignored: " + ignoredTests +
             "    duration: " + (t2-t1) + " ms");
     }
 
--- a/test.properties	Wed Oct 02 19:05:39 2013 +0200
+++ b/test.properties	Thu Oct 03 11:16:31 2013 +0200
@@ -1,6 +1,6 @@
 thermostat_executable_path=/home/jfabriko/thermostaty/testing_thermostat/thermostat/distribution/target/image/bin/
 thermostat_executable_name=thermostat
-thermostat_version=head
 thermostat_home=/home/jfabriko/thermostaty/testing_thermostat/thermostat/distribution/target/image/
 thermostat_user_home=/home/jfabriko/.thermostat/
 apache_tomcat_home=/home/jfabriko/thermostaty/apache-tomcat-7.0.42/
+thermostat_version=0.3.0