From 0496ccb45def3f8908b238a47bd0ad56ac4fba3e Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Wed, 23 Nov 2011 22:24:21 -0800
Subject: [PATCH] Added support for JUnit in CMake.

A new function add_junit_test() has been added to execute JUnit
tests in CMake. The function is used to execute the unit tests in
the common package.

Ticket #36
---
 pki/CMakeLists.txt                  |    3 +
 pki/base/common/CMakeLists.txt      |    1 +
 pki/base/common/test/CMakeLists.txt |   93 +++++++++++++++++++++++++++++++++++
 pki/cmake/Modules/JUnit.cmake       |   53 ++++++++++++++++++++
 pki/specs/pki-core.spec             |    5 ++-
 5 files changed, 154 insertions(+), 1 deletions(-)
 create mode 100644 pki/base/common/test/CMakeLists.txt
 create mode 100644 pki/cmake/Modules/JUnit.cmake

diff --git a/pki/CMakeLists.txt b/pki/CMakeLists.txt
index 88c0a0e693440d226ff30b94bb8ce8e7ff98ec01..b17efe53b15b14889c52bd49ccfbbbeb90350ace 100644
--- a/pki/CMakeLists.txt
+++ b/pki/CMakeLists.txt
@@ -72,6 +72,9 @@ macro_ensure_out_of_source_build("${PROJECT_NAME} requires an out of source buil
 # add macros
 include(MacroCopyFile)
 include(UseJava)
+include(JUnit)
+
+add_custom_target(test)
 
 # search for libraries
 
diff --git a/pki/base/common/CMakeLists.txt b/pki/base/common/CMakeLists.txt
index 5a35f6fbc76580e101618c59d50b4ca693f01516..10a7cc0bb7e2051e75774354aa12cdc2ce13c8c5 100644
--- a/pki/base/common/CMakeLists.txt
+++ b/pki/base/common/CMakeLists.txt
@@ -13,3 +13,4 @@ install(
 )
 
 add_subdirectory(src)
+add_subdirectory(test)
diff --git a/pki/base/common/test/CMakeLists.txt b/pki/base/common/test/CMakeLists.txt
new file mode 100644
index 0000000000000000000000000000000000000000..4f3ebf7bf40013fc88a6324d912a9dfe69233da2
--- /dev/null
+++ b/pki/base/common/test/CMakeLists.txt
@@ -0,0 +1,93 @@
+project(pki-common-test Java)
+
+find_file(PKI_CERTSRV_JAR
+    NAMES
+		pki-certsrv.jar
+    PATHS
+        ${JAVA_JAR_INSTALL_DIR}/pki
+)
+
+find_file(PKI_CMS_JAR
+    NAMES
+		pki-cms
+    PATHS
+        ${JAVA_JAR_INSTALL_DIR}/pki
+)
+
+find_file(PKI_CMSCORE_JAR
+    NAMES
+		pki-cmscore
+    PATHS
+        ${JAVA_JAR_INSTALL_DIR}/pki
+)
+
+find_file(PKI_CMSBUNDLE_JAR
+    NAMES
+		pki-cmsbundle
+    PATHS
+        ${JAVA_JAR_INSTALL_DIR}/pki
+)
+
+# TODO: create CMake function to find all Java files
+set(pki-common-test_SRCS
+    com/netscape/certsrv/app/CMSEngineDefaultStub.java
+    com/netscape/certsrv/authentication/AuthTokenTest.java
+    com/netscape/certsrv/logging/LoggerDefaultStub.java
+    com/netscape/certsrv/request/AgentApprovalsTest.java
+    com/netscape/cmscore/dbs/CertRecordListTest.java
+    com/netscape/cmscore/dbs/DBRegistryDefaultStub.java
+    com/netscape/cmscore/dbs/DBRegistryTest.java
+    com/netscape/cmscore/dbs/DBSSessionDefaultStub.java
+    com/netscape/cmscore/dbs/DBSubsystemDefaultStub.java
+    com/netscape/cmscore/dbs/DBVirtualListDefaultStub.java
+    com/netscape/cmscore/dbs/RequestRecordDefaultStub.java
+    com/netscape/cmscore/request/DBDynAttrMapperDefaultStub.java
+    com/netscape/cmscore/request/ExtAttrDynMapperTest.java
+    com/netscape/cmscore/request/ExtDataHashtableTest.java
+    com/netscape/cmscore/request/RequestDefaultStub.java
+    com/netscape/cmscore/request/RequestModDefaultStub.java
+    com/netscape/cmscore/request/RequestQueueTest.java
+    com/netscape/cmscore/request/RequestRecordTest.java
+    com/netscape/cmscore/request/RequestTest.java
+    com/netscape/cmscore/test/CMSBaseTestCase.java
+    com/netscape/cmscore/test/TestHelper.java
+)
+
+set(CMAKE_JAVA_INCLUDE_PATH
+    ${PKI_NSUTIL_JAR} ${PKI_CMSUTIL_JAR}
+    ${PKI_CERTSRV_JAR} ${PKI_CMS_JAR} ${PKI_CMSCORE_JAR} ${PKI_CMSBUNDLE_JAR}
+    ${LDAPJDK_JAR} ${SERVLET_JAR} ${VELOCITY_JAR} ${XALAN_JAR} ${XERCES_JAR}
+    ${JSS_JAR} ${OSUTIL_JAR} ${SYMKEY_JAR} ${JUNIT_JAR}
+)
+
+set(CMAKE_JAVA_TARGET_VERSION ${APPLICATION_VERSION})
+
+# build test jar file
+# TODO: create CMake function to compile without building jar file
+set(CMAKE_JAR_CLASSES_PREFIX com/netscape)
+add_jar(pki-common-test ${pki-common-test_SRCS})
+add_dependencies(pki-common-test
+    osutil pki-nsutil pki-cmsutil
+    pki-certsrv pki-cms pki-cmscore pki-cmsbundle
+)
+
+# create test target
+# TODO: create CMake function to find all JUnit test classes
+add_junit_test(test-pki-common
+    CLASSPATH
+        ${pki-common-test_JAR_FILE}
+        ${CMAKE_JAVA_INCLUDE_PATH}
+    TESTS
+        com.netscape.certsrv.authentication.AuthTokenTest
+        com.netscape.certsrv.request.AgentApprovalsTest
+        com.netscape.cmscore.dbs.CertRecordListTest
+        com.netscape.cmscore.dbs.DBRegistryTest
+        com.netscape.cmscore.request.ExtAttrDynMapperTest
+        com.netscape.cmscore.request.ExtDataHashtableTest
+        com.netscape.cmscore.request.RequestQueueTest
+        com.netscape.cmscore.request.RequestRecordTest
+        com.netscape.cmscore.request.RequestTest
+)
+
+# include test into the main test
+add_dependencies(test test-pki-common)
diff --git a/pki/cmake/Modules/JUnit.cmake b/pki/cmake/Modules/JUnit.cmake
new file mode 100644
index 0000000000000000000000000000000000000000..936dd000eb2e4ef2166cf5a8a6f8ab4f0632c726
--- /dev/null
+++ b/pki/cmake/Modules/JUnit.cmake
@@ -0,0 +1,53 @@
+#
+# This file provides functions for JUnit support.
+#
+# Available Functions:
+#
+#   add_junit_test(<target name> 
+#       CLASSPATH [path1 ...]
+#       TESTS [class1 ...]
+#   )
+#
+#   This command creates a target for executing JUnit test classes
+#   using the specified class path.
+#
+
+find_file(JUNIT_JAR
+    NAMES
+        junit4.jar
+    PATHS
+        ${JAVA_LIB_INSTALL_DIR}
+        /usr/share/java
+)
+
+function(add_junit_test TARGET_NAME)
+
+    if (WIN32 AND NOT CYGWIN)
+        set(SEPARATOR ";")
+    else (WIN32 AND NOT CYGWIN)
+        set(SEPARATOR ":")
+    endif(WIN32 AND NOT CYGWIN)
+
+    foreach (ARG ${ARGN})
+        if (ARG MATCHES "CLASSPATH" OR ARG MATCHES "TESTS")
+            set(TYPE ${ARG})
+        
+        else (ARG MATCHES "TESTS")
+
+            if (TYPE MATCHES "CLASSPATH")
+                set(CLASSPATH "${CLASSPATH}${SEPARATOR}${ARG}")
+
+            elseif (TYPE MATCHES "TESTS")
+                set(TESTS ${TESTS} ${ARG})
+
+            endif(TYPE MATCHES "TESTS")
+
+        endif(ARG MATCHES "CLASSPATH" OR ARG MATCHES "TESTS")
+
+    endforeach(ARG)
+
+    add_custom_target(${TARGET_NAME}
+        COMMAND ${CMAKE_Java_RUNTIME} -classpath ${CLASSPATH} org.junit.runner.JUnitCore ${TESTS}
+    )
+
+endfunction(add_junit_test)
diff --git a/pki/specs/pki-core.spec b/pki/specs/pki-core.spec
index 020b31a14ca9d09e1d8ed2caf56138b20aa10e5d..3408581450b9d65c9e3778967884ce427fffbaf7 100644
--- a/pki/specs/pki-core.spec
+++ b/pki/specs/pki-core.spec
@@ -447,7 +447,7 @@ This package is a part of the PKI Core used by the Certificate System.
 %{__mkdir_p} build
 cd build
 %cmake -DVAR_INSTALL_DIR:PATH=/var -DBUILD_PKI_CORE:BOOL=ON -DJAVA_LIB_INSTALL_DIR=%{_jnidir} ..
-%{__make} VERBOSE=1 %{?_smp_mflags}
+%{__make} VERBOSE=1 %{?_smp_mflags} all test
 
 
 %install
@@ -721,6 +721,9 @@ fi
 
 
 %changelog
+* Mon Nov 28 2011 Endi S. Dewata <edewata@redhat.com> 9.0.16-2
+- Added JUnit tests
+ 
 * Fri Oct 28 2011 Matthew Harmsen <mharmsen@redhat.com> 9.0.16-1
 - 'pki-setup'
 - 'pki-symkey'
-- 
1.7.6.4

