changeset 564:c7b7bd6fffb3

Unit test integration for UnitTest++ with icedtea-web
author Adam Domurad <adomurad@redhat.com>
date Wed, 21 Nov 2012 13:09:54 -0500
parents dfc88a1018d9
children 4cd65d83965b
files ChangeLog Makefile.am plugin/icedteanp/IcedTeaPluginUtils.h plugin/icedteanp/IcedTeaRunnable.h tests/cpp-unit-tests/IcedTeaPluginUtilsTest.cc tests/cpp-unit-tests/main.cc
diffstat 6 files changed, 247 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Nov 21 12:37:38 2012 -0500
+++ b/ChangeLog	Wed Nov 21 13:09:54 2012 -0500
@@ -1,3 +1,16 @@
+2012-11-21  Adam Domurad  <adomurad@redhat.com>
+
+	Support for C++ unit testing with UnitTest++ for IcedTeaWeb.
+	* tests/cpp-unit-tests/IcedTeaPluginUtilsTest.cc: New, contains tests
+	for utility functions.
+	* tests/cpp-unit-tests/main.cc: New, contains unit test runner.
+	* plugin/icedteanp/IcedTeaPluginUtils.h: Remove incorrect circular 
+	include dependency
+	* plugin/icedteanp/IcedTeaRunnable.h: Add includes necessary for 
+	self-sustaining header.
+	* Makefile.am: Add targets for unit test compilation and running, 
+	eg 'make run-cpp-unit-tests'.
+
 2012-11-21  Adam Domurad  <adomurad@redhat.com>
 
 	Add the source code to UnitTest++ into the project.
--- a/Makefile.am	Wed Nov 21 12:37:38 2012 -0500
+++ b/Makefile.am	Wed Nov 21 13:09:54 2012 -0500
@@ -25,6 +25,9 @@
 export TEST_EXTENSIONS_TESTS_SRCDIR=$(TESTS_SRCDIR)/test-extensions-tests
 export REPRODUCERS_TESTS_SRCDIR=$(TESTS_SRCDIR)/reproducers
 export TEST_EXTENSIONS_DIR=$(TESTS_DIR)/test-extensions
+export CPP_UNITTEST_FRAMEWORK_SRCDIR=$(TESTS_SRCDIR)/UnitTest++
+export CPP_UNITTEST_SRCDIR=$(TESTS_SRCDIR)/cpp-unit-tests
+export CPP_UNITTEST_DIR=$(TESTS_DIR)/cpp-unit-tests
 export TEST_EXTENSIONS_COMPATIBILITY_SYMLINK=$(TESTS_DIR)/netx/jnlp_testsengine
 export TEST_EXTENSIONS_TESTS_DIR=$(TESTS_DIR)/test-extensions-tests
 export REPRODUCERS_TESTS_SERVER_DEPLOYDIR=$(TESTS_DIR)/reproducers_test_server_deploydir
@@ -61,6 +64,10 @@
 export OPERA_GLOBAL64_PLUGINDIR=/usr/lib64/opera/plugins
 export OPERA_GLOBAL32_PLUGINDIR=/usr/lib/opera/plugins
 export BUILT_PLUGIN_LIBRARY=IcedTeaPlugin.so
+export CPP_UNITTEST_FRAMEWORK_BUILDDIR=$(CPP_UNITTEST_DIR)/UnitTest++
+export CPP_UNITTEST_FRAMEWORK_LIB_NAME=libUnitTest++.a
+export CPP_UNITTEST_FRAMEWORK_LIB=$(CPP_UNITTEST_FRAMEWORK_BUILDDIR)/$(CPP_UNITTEST_FRAMEWORK_LIB_NAME)
+export CPP_UNITTEST_EXECUTABLE=$(CPP_UNITTEST_DIR)/IcedTeaPluginUnitTests
 export MOZILLA_LOCAL_BACKUP_FILE=${HOME}/$(PLUGIN_LINK_NAME).origU
 export MOZILLA_GLOBAL_BACKUP_FILE=${HOME}/$(PLUGIN_LINK_NAME).origMG
 export OPERA_GLOBAL_BACKUP_FILE=${HOME}/$(PLUGIN_LINK_NAME).origOG
@@ -288,6 +295,75 @@
 	  $(MOZILLA_LIBS) \
 	  -shared -o $@
 
+# Start of CPP Unit test targets
+
+# Note that UnitTest++ has its own makefile, however this is avoided because it creates an in-source build.
+$(CPP_UNITTEST_FRAMEWORK_LIB): $(CPP_UNITTEST_FRAMEWORK_SRCDIR)
+	mkdir -p $(CPP_UNITTEST_FRAMEWORK_BUILDDIR) && \
+	pushd $(CPP_UNITTEST_FRAMEWORK_SRCDIR) && \
+	for cppfile in $$(find $(CPP_UNITTEST_FRAMEWORK_SRCDIR) -name '*.cpp') ; \
+	do \
+	  objfile="$(CPP_UNITTEST_FRAMEWORK_BUILDDIR)/$$(basename $${cppfile%.cpp}).o" ; \
+	  $(CXX) $(CXXFLAGS) -c $$cppfile -o $$objfile || exit 1 ; \
+	done ; \
+	ar cr $(CPP_UNITTEST_FRAMEWORK_LIB) $(CPP_UNITTEST_FRAMEWORK_BUILDDIR)/*.o ; \
+	popd
+
+clean-unittest++:
+	rm $(CPP_UNITTEST_FRAMEWORK_BUILDDIR)/*.o
+	rm $(CPP_UNITTEST_FRAMEWORK_LIB)
+	rmdir $(CPP_UNITTEST_FRAMEWORK_BUILDDIR)
+	rmdir $(CPP_UNITTEST_DIR) &> /dev/null
+
+stamps/cpp-unit-tests-compile.stamp: $(CPP_UNITTEST_FRAMEWORK_LIB) $(CPP_UNITTEST_SRCDIR) $(addprefix $(PLUGIN_DIR)/,$(PLUGIN_OBJECTS))
+	mkdir -p $(CPP_UNITTEST_DIR) && \
+	pushd $(CPP_UNITTEST_SRCDIR) && \
+	for cppfile in $$(find $(CPP_UNITTEST_SRCDIR) -name '*.cc') ; \
+	do \
+	objfile="$(CPP_UNITTEST_DIR)/$$(basename $${cppfile%.cc}).o" ; \
+	echo "Compiling $$cppfile to $$objfile"; \
+	$(CXX) $(CXXFLAGS) \
+	   $(DEFS) $(VERSION_DEFS) \
+	  -DJDK_UPDATE_VERSION="\"$(JDK_UPDATE_VERSION)\"" \
+	  -DPLUGIN_NAME="\"IcedTea-Web Plugin\"" \
+	  -DPLUGIN_VERSION="\"$(PLUGIN_VERSION)\"" \
+	  -DPACKAGE_URL="\"$(PACKAGE_URL)\"" \
+	  -DMOZILLA_VERSION_COLLAPSED="$(MOZILLA_VERSION_COLLAPSED)" \
+	  -DICEDTEA_WEB_JRE="\"$(SYSTEM_JRE_DIR)\"" \
+	  -DPLUGIN_BOOTCLASSPATH=$(PLUGIN_BOOTCLASSPATH) \
+	  $(GLIB_CFLAGS) \
+	  $(GTK_CFLAGS) \
+	  $(MOZILLA_CFLAGS) \
+	  "-I$(CPP_UNITTEST_FRAMEWORK_SRCDIR)/src" \
+	  "-I$(PLUGIN_SRCDIR)" \
+	  -o $$objfile -c $$cppfile || exit 1 ; \
+	done ; \
+	popd ; \
+	mkdir -p stamps ; \
+	touch $@
+
+$(CPP_UNITTEST_EXECUTABLE): $(CPP_UNITTEST_FRAMEWORK_LIB) stamps/cpp-unit-tests-compile.stamp 
+	cd $(CPP_UNITTEST_DIR) && \
+	$(CXX) $(CXXFLAGS) \
+	  $(addprefix $(PLUGIN_DIR)/,$(PLUGIN_OBJECTS)) \
+	  $(CPP_UNITTEST_DIR)/*.o \
+	  $(GLIB_LIBS) \
+	  $(GTK_LIBS) \
+	  $(MOZILLA_LIBS) \
+	  $(CPP_UNITTEST_FRAMEWORK_LIB)\
+	  $(BUILT_CPP_UNIT_TEST_FRAMEWORK) -o $@
+
+clean-cpp-unit-tests: 
+	rm stamps/cpp-unit-tests-compile.stamp &> /dev/null 
+	rm $(CPP_UNITTEST_EXECUTABLE)
+	rm $(CPP_UNITTEST_DIR)/*.o
+	rmdir $(CPP_UNITTEST_DIR) &> /dev/null
+
+run-cpp-unit-tests: $(CPP_UNITTEST_EXECUTABLE)
+	$(CPP_UNITTEST_EXECUTABLE)
+
+# End of CPP Unit test targets
+
 clean-IcedTeaPlugin:
 	rm -f $(PLUGIN_DIR)/*.o
 	rm -f $(PLUGIN_DIR)/$(BUILT_PLUGIN_LIBRARY)
@@ -494,7 +570,7 @@
 # check
 # ==========================
 
-clean-tests: clean-netx-tests
+clean-tests: clean-netx-tests clean-cpp-unit-tests clean-unittest++
 	if [ -e $(TESTS_DIR) ]; then \
 		rmdir $(TESTS_DIR) ; \
 	fi
--- a/plugin/icedteanp/IcedTeaPluginUtils.h	Wed Nov 21 12:37:38 2012 -0500
+++ b/plugin/icedteanp/IcedTeaPluginUtils.h	Wed Nov 21 13:09:54 2012 -0500
@@ -56,6 +56,7 @@
 #include <vector>
 
 #include <npapi.h>
+#include <glib.h>
 
 #if MOZILLA_VERSION_COLLAPSED < 1090100
 #include <npupp.h>
@@ -64,8 +65,6 @@
 #include <npruntime.h>
 #endif
 
-#include "IcedTeaNPPlugin.h"
-
 #define PLUGIN_DEBUG(...) \
   do                                                          \
   {                                                           \
--- a/plugin/icedteanp/IcedTeaRunnable.h	Wed Nov 21 12:37:38 2012 -0500
+++ b/plugin/icedteanp/IcedTeaRunnable.h	Wed Nov 21 13:09:54 2012 -0500
@@ -42,6 +42,9 @@
 #define MOZILLA 1
 #if MOZILLA
 
+#include <string>
+#include <npapi.h>
+
 #if MOZILLA_VERSION_COLLAPSED < 1090100
 #include <nsIRunnable.h>
 #include <string>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cpp-unit-tests/IcedTeaPluginUtilsTest.cc	Wed Nov 21 13:09:54 2012 -0500
@@ -0,0 +1,48 @@
+/* Copyright (C) 2012 Red Hat
+
+ This file is part of IcedTea.
+
+ IcedTea is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ IcedTea is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with IcedTea; see the file COPYING.  If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301 USA.
+
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library.  Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library 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 this library.  If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so.  If you do not wish to do so, delete this
+ exception statement from your version. */
+
+#include <UnitTest++.h>
+
+#include <npapi.h>
+#include "IcedTeaPluginUtils.h"
+
+TEST(NPVariantAsString) {
+    NPVariant var;
+    STRINGZ_TO_NPVARIANT("test", var);
+
+    std::string cppstr = IcedTeaPluginUtilities::NPVariantAsString(var);
+    CHECK(cppstr == "test");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cpp-unit-tests/main.cc	Wed Nov 21 13:09:54 2012 -0500
@@ -0,0 +1,105 @@
+/* Copyright (C) 2012 Red Hat
+
+ This file is part of IcedTea.
+
+ IcedTea is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ IcedTea is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with IcedTea; see the file COPYING.  If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301 USA.
+
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library.  Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library 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 this library.  If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so.  If you do not wish to do so, delete this
+ exception statement from your version. */
+
+// The test runner.
+// Note that all modules compiled with the TEST macro will append tests to
+// a global test list, that is accessible via Test::GetTestList().
+#include <cstdio>
+
+#include <UnitTest++.h>
+#include <TestReporter.h>
+
+using namespace UnitTest;
+
+class IcedteaWebUnitTestReporter: public TestReporter {
+public:
+
+    IcedteaWebUnitTestReporter() {
+        // Unfortunately, there is no 'ReportSuccess'
+        // We use 'did_finish_correctly' to track successes
+        did_finish_correctly = false;
+    }
+
+    virtual void ReportTestStart(const TestDetails& test) {
+        did_finish_correctly = true;
+    }
+
+    virtual void ReportFailure(const TestDetails& details,
+            char const* failure) {
+
+        printf("FAILED: %s line %d (%s)\n", details.testName,
+                details.lineNumber, failure);
+
+        did_finish_correctly = false;
+    }
+
+    virtual void ReportTestFinish(const TestDetails& details,
+            float secondsElapsed) {
+
+        if (did_finish_correctly) {
+            printf("Passed: %s\n", details.testName);
+        }
+    }
+
+    virtual void ReportSummary(int totalTestCount, int failedTestCount,
+            int failureCount, float secondsElapsed) {
+
+        if (failedTestCount > 0) {
+            printf("TEST SUITE FAILURE: Not all tests have passed!\n");
+        }
+
+        printf("Total tests run: %d\n", totalTestCount);
+        printf("Test results: passed: %d; failed: %d\n",
+                totalTestCount - failedTestCount, failedTestCount);
+    }
+
+private:
+    bool did_finish_correctly;
+};
+
+static int run_icedtea_web_unit_tests() {
+    IcedteaWebUnitTestReporter reporter;
+    TestRunner runner(reporter);
+
+    return runner.RunTestsIf(Test::GetTestList(), NULL /*All suites*/,
+            True() /*All tests*/, 0 /*No time limit*/);
+}
+
+int main() {
+    return run_icedtea_web_unit_tests();
+}
+
+