diff options
author | Endi Sukma Dewata <edewata@redhat.com> | 2012-03-24 02:27:47 -0500 |
---|---|---|
committer | Endi Sukma Dewata <edewata@redhat.com> | 2012-03-26 11:43:54 -0500 |
commit | 621d9e5c413e561293d7484b93882d985b3fe15f (patch) | |
tree | 638f3d75761c121d9a8fb50b52a12a6686c5ac5c /base/test | |
parent | 40d3643b8d91886bf210aa27f711731c81a11e49 (diff) | |
download | pki-621d9e5c413e561293d7484b93882d985b3fe15f.tar.gz pki-621d9e5c413e561293d7484b93882d985b3fe15f.tar.xz pki-621d9e5c413e561293d7484b93882d985b3fe15f.zip |
Removed unnecessary pki folder.
Previously the source code was located inside a pki folder.
This folder was created during svn migration and is no longer
needed. This folder has now been removed and the contents have
been moved up one level.
Ticket #131
Diffstat (limited to 'base/test')
-rw-r--r-- | base/test/CMakeLists.txt | 3 | ||||
-rw-r--r-- | base/test/src/CMakeLists.txt | 20 | ||||
-rw-r--r-- | base/test/src/com/netscape/test/TestListener.java | 249 | ||||
-rw-r--r-- | base/test/src/com/netscape/test/TestRunner.java | 23 |
4 files changed, 295 insertions, 0 deletions
diff --git a/base/test/CMakeLists.txt b/base/test/CMakeLists.txt new file mode 100644 index 000000000..9a4acceef --- /dev/null +++ b/base/test/CMakeLists.txt @@ -0,0 +1,3 @@ +project(test Java) + +add_subdirectory(src) diff --git a/base/test/src/CMakeLists.txt b/base/test/src/CMakeLists.txt new file mode 100644 index 000000000..3631baa73 --- /dev/null +++ b/base/test/src/CMakeLists.txt @@ -0,0 +1,20 @@ +project(pki-test_java Java) + +# TODO: create CMake function to find all Java files +set(pki-test_java_SRCS + com/netscape/test/TestListener.java + com/netscape/test/TestRunner.java +) + +set(CMAKE_JAVA_INCLUDE_PATH + ${XALAN_JAR} ${XERCES_JAR} ${JUNIT_JAR} +) + +set(CMAKE_JAVA_TARGET_VERSION ${APPLICATION_VERSION}) + +# build test jar file +# TODO: create CMake function to compile without building jar file +# TODO: build test only when the test is invoked +set(CMAKE_JAR_CLASSES_PREFIX com/netscape) +add_jar(pki-test ${pki-test_java_SRCS}) +set(PKI_TEST_JAR ${pki-test_JAR_FILE} CACHE INTERNAL "pki-test jar file")
\ No newline at end of file diff --git a/base/test/src/com/netscape/test/TestListener.java b/base/test/src/com/netscape/test/TestListener.java new file mode 100644 index 000000000..96c4c9068 --- /dev/null +++ b/base/test/src/com/netscape/test/TestListener.java @@ -0,0 +1,249 @@ +package com.netscape.test; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileWriter; +import java.io.PrintStream; +import java.net.InetAddress; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.junit.runner.Description; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; +import org.junit.runner.notification.RunListener; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Text; + +public class TestListener extends RunListener { + + DateFormat dateFormat; + + DocumentBuilderFactory docBuilderFactory; + DocumentBuilder docBuilder; + Document document; + + TransformerFactory transFactory; + Transformer trans; + + String reportsDir; + + Element testSuiteElement; + long testSuiteStartTime; + + Element testCaseElement; + long testCaseStartTime; + + String currentTestSuiteName; + + long testCount; + long successCount; + long failureCount; + + PrintStream stdOut; + PrintStream stdErr; + + ByteArrayOutputStream out; + ByteArrayOutputStream err; + + public TestListener() throws Exception { + + dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + + docBuilderFactory = DocumentBuilderFactory.newInstance(); + docBuilder = docBuilderFactory.newDocumentBuilder(); + + transFactory = TransformerFactory.newInstance(); + trans = transFactory.newTransformer(); + trans.setOutputProperty(OutputKeys.INDENT, "yes"); + + reportsDir = System.getProperty("junit.reports.dir"); + } + + public void testRunFinished(Result result) throws Exception { + if (currentTestSuiteName != null) { + finishTestSuite(); // finish last suite + } + } + + public void testStarted(Description description) throws Exception { + + String testSuiteName = description.getClassName(); + + if (currentTestSuiteName == null) { + startTestSuite(testSuiteName); // start first suite + + } else if (!currentTestSuiteName.equals(testSuiteName)) { + finishTestSuite(); // finish old suite + startTestSuite(testSuiteName); // start new suite + } + + currentTestSuiteName = testSuiteName; + + startTestCase(description); + } + + public void testFinished(Description description) throws Exception { + finishTestCase(); + recordTestCaseSuccess(); + } + + public void testFailure(Failure failure) throws Exception { + finishTestCase(); + recordTestCaseFailure(failure); + } + + public void startTestSuite(String testSuiteName) throws Exception { + + testSuiteStartTime = System.currentTimeMillis(); + + document = docBuilder.newDocument(); + + // test suite + testSuiteElement = document.createElement("testsuite"); + document.appendChild(testSuiteElement); + + testSuiteElement.setAttribute("name", testSuiteName); + testSuiteElement.setAttribute("timestamp", + dateFormat.format(new Date(testSuiteStartTime))); + testSuiteElement.setAttribute("hostname", + InetAddress.getLocalHost().getHostName()); + + // system properties + Element propertiesElement = document.createElement("properties"); + testSuiteElement.appendChild(propertiesElement); + + for (String name : System.getProperties().stringPropertyNames()) { + Element propertyElement = document.createElement("property"); + propertyElement.setAttribute("name", name); + propertyElement.setAttribute("value", System.getProperty(name)); + propertiesElement.appendChild(propertyElement); + } + + // reset counters + testCount = 0; + successCount = 0; + failureCount = 0; + + // redirect outputs + stdOut = System.out; + out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out, true)); + + stdErr = System.err; + err = new ByteArrayOutputStream(); + System.setErr(new PrintStream(err, true)); + } + + public void finishTestSuite() throws Exception { + + double time = (System.currentTimeMillis() - testSuiteStartTime) / 1000.0; + testSuiteElement.setAttribute("time", "" + time); + + // save counters + long errorCount = testCount - successCount - failureCount; + + testSuiteElement.setAttribute("tests", "" + testCount); + testSuiteElement.setAttribute("failures", "" + failureCount); + testSuiteElement.setAttribute("errors", "" + errorCount); + + // save outputs + System.setOut(stdOut); + System.setErr(stdErr); + + Element systemOutElement = document.createElement("system-out"); + testSuiteElement.appendChild(systemOutElement); + + systemOutElement.appendChild( + document.createCDATASection(out.toString()) + ); + + Element systemErrElement = document.createElement("system-err"); + testSuiteElement.appendChild(systemErrElement); + + systemErrElement.appendChild( + document.createCDATASection(err.toString()) + ); + + // write to file + FileWriter fw = new FileWriter( + reportsDir + File.separator + "TEST-" + currentTestSuiteName + ".xml" + ); + StreamResult sr = new StreamResult(fw); + DOMSource source = new DOMSource(document); + trans.transform(source, sr); + fw.close(); + } + + public void startTestCase(Description description) throws Exception { + + testCaseStartTime = System.currentTimeMillis(); + + testCaseElement = document.createElement("testcase"); + testSuiteElement.appendChild(testCaseElement); + + testCaseElement.setAttribute("classname", description.getClassName()); + testCaseElement.setAttribute("name", description.getMethodName()); + + testCount++; + } + + public void finishTestCase() throws Exception { + double time = (System.currentTimeMillis() - testCaseStartTime) / 1000.0; + testCaseElement.setAttribute("time", "" + time); + } + + public void recordTestCaseSuccess() throws Exception { + successCount++; + } + + public void recordTestCaseFailure(Failure failure) throws Exception { + + Element failureElement = document.createElement("failure"); + testCaseElement.appendChild(failureElement); + + Description description = failure.getDescription(); + Throwable exception = failure.getException(); + String exceptionName = exception.getClass().getName(); + + failureElement.setAttribute("message", failure.getMessage()); + failureElement.setAttribute("type", exceptionName); + + Text messageElement = document.createTextNode( + exceptionName + ": " + failure.getMessage() + "\n" + ); + + // print stack trace + for (StackTraceElement element : exception.getStackTrace()) { + if (!element.getClassName().equals(description.getClassName())) + continue; + + String source = "Unknown Source"; + if (element.getFileName() != null && element.getLineNumber() >= 0) { + source = element.getFileName() + ":" + element.getLineNumber(); + } + + messageElement.appendData("\tat " + + element.getClassName() + "." + element.getMethodName() + + "(" + source + ")\n" + ); + } + + failureElement.appendChild(messageElement); + + failureCount++; + } +} diff --git a/base/test/src/com/netscape/test/TestRunner.java b/base/test/src/com/netscape/test/TestRunner.java new file mode 100644 index 000000000..7eb4bfd3e --- /dev/null +++ b/base/test/src/com/netscape/test/TestRunner.java @@ -0,0 +1,23 @@ +package com.netscape.test; + +import org.junit.internal.RealSystem; +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; + +public class TestRunner { + + public Result run(String... args) throws Exception { + + JUnitCore core = new JUnitCore(); + core.addListener(new TestListener()); + + return core.runMain(new RealSystem(), args); + } + + public static void main(String... args) throws Exception { + + TestRunner runner = new TestRunner(); + Result result = runner.run(args); + System.exit(result.wasSuccessful() ? 0 : 1); + } +} |