From e929b5f4c6d9351b29150e6c1843f06806ee7b00 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Mon, 1 Dec 2014 12:23:47 +0100 Subject: added status servlet to pdf-as-web --- .../at/gv/egiz/status/impl/BaseTestResult.java | 91 +++++++++++++ .../java/at/gv/egiz/status/impl/TestManager.java | 149 +++++++++++++++++++++ .../java/at/gv/egiz/status/impl/TestRunResult.java | 88 ++++++++++++ .../at/gv/egiz/status/impl/TestStatusString.java | 29 ++++ 4 files changed, 357 insertions(+) create mode 100644 pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/BaseTestResult.java create mode 100644 pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestManager.java create mode 100644 pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestRunResult.java create mode 100644 pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestStatusString.java (limited to 'pdf-as-web-status/src/main/java/at/gv/egiz/status/impl') diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/BaseTestResult.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/BaseTestResult.java new file mode 100644 index 00000000..da77f424 --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/BaseTestResult.java @@ -0,0 +1,91 @@ +package at.gv.egiz.status.impl; + +import java.util.ArrayList; +import java.util.List; + +import at.gv.egiz.status.TestResult; +import at.gv.egiz.status.TestStatus; + +/** + * The Class BaseTestResult. + */ +public class BaseTestResult implements TestResult { + + /** The status. */ + private TestStatus status; + + /** The details. */ + private List details; + + /** + * Instantiates a new base test result. + */ + public BaseTestResult() { + this.init(TestStatus.INDETERMINATE, new ArrayList()); + } + + /** + * Instantiates a new base test result. + * + * @param status the status + */ + public BaseTestResult(TestStatus status) { + this.init(status, new ArrayList()); + } + + /** + * Instantiates a new base test result. + * + * @param status the status + * @param details the details + */ + public BaseTestResult(TestStatus status, List details) { + this.init(status, details); + } + + /** + * Inits the. + * + * @param status the status + * @param details the details + */ + private void init(TestStatus status, List details) { + this.status = status; + this.details = details; + } + + /** + * Sets the status. + * + * @param status the new status + */ + public void setStatus(TestStatus status) { + this.status = status; + } + + /** + * Sets the details. + * + * @param details the new details + */ + public void setDetails(List details) { + this.details = details; + } + + /* (non-Javadoc) + * @see at.gv.egiz.status.TestResult#getStatus() + */ + @Override + public TestStatus getStatus() { + return status; + } + + /* (non-Javadoc) + * @see at.gv.egiz.status.TestResult#getDetails() + */ + @Override + public List getDetails() { + return details; + } + +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestManager.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestManager.java new file mode 100644 index 00000000..db40f96c --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestManager.java @@ -0,0 +1,149 @@ +package at.gv.egiz.status.impl; + +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.ServiceLoader; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.status.Test; +import at.gv.egiz.status.TestFactory; +import at.gv.egiz.status.TestResult; + +/** + * The Class TestManager. + */ +public class TestManager { + + /** The log. */ + private final Logger log = LoggerFactory.getLogger(TestManager.class); + + /** The tests. */ + private Map tests = new HashMap(); + + /** The test result cache. */ + private Map testResultCache = new HashMap(); + + /** The loader. */ + private static ServiceLoader loader = ServiceLoader + .load(TestFactory.class); + + /** + * Instantiates a new test manager. + */ + public TestManager() { + Iterator factoryIterator = loader.iterator(); + + while (factoryIterator.hasNext()) { + TestFactory factory = factoryIterator.next(); + + log.debug("Running Factory: " + factory.getClass().getName()); + + List testList = factory.createTests(); + if (testList != null && !testList.isEmpty()) { + log.debug("Factory: " + factory.getClass().getName() + + " produced " + testList.size() + " tests!"); + Iterator testIterator = testList.iterator(); + while (testIterator.hasNext()) { + Test test = testIterator.next(); + log.debug("adding Test: " + test.getName() + " [" + + test.getClass().getName() + "]"); + tests.put(test.getName(), test); + } + } else { + log.debug("Factory: " + factory.getClass().getName() + + " produced no tests!"); + } + } + } + + /** + * Store test result. + * + * @param testName the test name + * @param testResult the test result + * @return the test result + */ + private TestResult storeTestResult(String testName, TestResult testResult) { + if(testResult != null) { + TestRunResult runResult = new TestRunResult(); + runResult.setExecutionTimestamp(new Date().getTime()); + runResult.setTestResult(testResult); + testResultCache.put(testName, runResult); + } + return testResult; + } + + /** + * Run test. + * + * @param testName the test name + * @return the test result + */ + public TestResult runTest(String testName) { + if(tests.containsKey(testName)) { + return storeTestResult(testName, tests.get(testName).runTest()); + } + log.debug("Not test \"" + testName + "\" available"); + return null; + } + + /** + * Run test. + * + * @param testName the test name + * @param forceExecution the force execution + * @return the test result + */ + public TestResult runTest(String testName, boolean forceExecution) { + if(!tests.containsKey(testName)) { + log.debug("Not test \"" + testName + "\" available"); + return null; + } + + if (forceExecution) { + return runTest(testName); + } + + Test test = tests.get(testName); + + if (testResultCache.containsKey(testName)) { + // + Date now = new Date(); + + TestRunResult result = testResultCache.get(testName); + long lastTest = result.getExecutionTimestamp(); + long delay = test.getCacheDelay(); + + if (lastTest < now.getTime() - delay) { + // Too old + return runTest(testName); + } else { + // Cache is fine! + return result.getTestResult(); + } + } + return runTest(testName); + } + + /** + * Run all tests. + * + * @param forceExecution the force execution + * @return the map + */ + public Map runAllTests(boolean forceExecution) { + Map results = new HashMap(); + Iterator testIterator = tests.values().iterator(); + while(testIterator.hasNext()) { + Test test = testIterator.next(); + String testName = test.getName(); + results.put(testName, runTest(testName, forceExecution)); + } + return results; + } +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestRunResult.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestRunResult.java new file mode 100644 index 00000000..9385392c --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestRunResult.java @@ -0,0 +1,88 @@ +package at.gv.egiz.status.impl; + +import at.gv.egiz.status.TestResult; + +/** + * The Class TestRunResult. + */ +public class TestRunResult { + + /** The test result. */ + private TestResult testResult; + + /** The execution timestamp. */ + private long executionTimestamp; + + /** + * Instantiates a new test run result. + */ + public TestRunResult() { + this.init(null, 0); + } + + /** + * Instantiates a new test run result. + * + * @param testResult the test result + */ + public TestRunResult(TestResult testResult) { + this.init(testResult, 0); + } + + /** + * Instantiates a new test run result. + * + * @param testResult the test result + * @param executionTimestamp the execution timestamp + */ + public TestRunResult(TestResult testResult, long executionTimestamp) { + this.init(testResult, executionTimestamp); + } + + /** + * Inits the. + * + * @param testResult the test result + * @param executionTimestamp the execution timestamp + */ + private void init(TestResult testResult, long executionTimestamp) { + this.testResult = testResult; + this.executionTimestamp = executionTimestamp; + } + + /** + * Gets the test result. + * + * @return the test result + */ + public TestResult getTestResult() { + return testResult; + } + + /** + * Sets the test result. + * + * @param testResult the new test result + */ + public void setTestResult(TestResult testResult) { + this.testResult = testResult; + } + + /** + * Gets the execution timestamp. + * + * @return the execution timestamp + */ + public long getExecutionTimestamp() { + return executionTimestamp; + } + + /** + * Sets the execution timestamp. + * + * @param executionTimestamp the new execution timestamp + */ + public void setExecutionTimestamp(long executionTimestamp) { + this.executionTimestamp = executionTimestamp; + } +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestStatusString.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestStatusString.java new file mode 100644 index 00000000..50605cf5 --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestStatusString.java @@ -0,0 +1,29 @@ +package at.gv.egiz.status.impl; + +import at.gv.egiz.status.TestStatus; + +// TODO: Auto-generated Javadoc +/** + * The Class TestStatusString. + */ +public class TestStatusString { + + /** + * Gets the string. + * + * @param status the status + * @return the string + */ + public static String getString(TestStatus status) { + switch (status) { + case OK: + return "OK"; + case FAILED: + return "FAILED"; + case INDETERMINATE: + return "INDETERMINATE"; + default: + return "UNKNOWN"; + } + } +} -- cgit v1.2.3