[resource-manipulation] Add Test Helper class
authorcoderhyme <jhyo.kim@samsung.com>
Tue, 21 Jul 2015 08:06:10 +0000 (17:06 +0900)
committerUze Choi <uzchoi@samsung.com>
Wed, 22 Jul 2015 06:49:09 +0000 (06:49 +0000)
The class is to avoid aborting test execution, when expectation is not fulfilled on GCC 4.8.
The issue is caused by a difference of destructor's noexcept specifier between MockRepository of HippoMock and Test of gtest.
This class allows test fixtures have same usage regardless of GCC version.

Change-Id: I247a63009ca2625b27a29eb5320a1a6e3ebbd71b
Signed-off-by: coderhyme <jhyo.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1779
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/resource-manipulation/src/common/primitiveResource/SConscript
service/resource-manipulation/src/common/primitiveResource/unittests/PresenceSubscriberTest.cpp
service/resource-manipulation/src/common/primitiveResource/unittests/PrimitiveResourceTest.cpp
service/resource-manipulation/src/common/utils/include/UnitTestHelper.h [new file with mode: 0644]
service/resource-manipulation/src/serverBuilder/SConscript
service/resource-manipulation/src/serverBuilder/unittests/RCSResponseTest.cpp
service/resource-manipulation/src/serverBuilder/unittests/RequestHandlerTest.cpp
service/resource-manipulation/src/serverBuilder/unittests/ResourceObjectTest.cpp

index 3c0b9cf..cc3f398 100644 (file)
@@ -77,7 +77,8 @@ service_common_test_env = service_common_env.Clone();
 service_common_test_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
 service_common_test_env.PrependUnique(CPPPATH = [
     env.get('SRC_DIR')+'/extlibs/hippomocks-master',
-    gtest_dir + '/include'
+    gtest_dir + '/include',
+    '../utils/include'
     ])
 
 gtest = File(gtest_dir + '/lib/.libs/libgtest.a')
@@ -90,13 +91,13 @@ service_common_test_env.PrependUnique(LIBS = [
     'connectivity_abstraction',
     'coap',
     'service_common',
-    'pthread',
     gtest,
-    gtest_main
+    gtest_main,
+    'pthread'
     ])
 
-#service_common_test_src = env.Glob('unittests/*.cpp')
+service_common_test_src = env.Glob('unittests/*.cpp')
 
-#service_common_test = service_common_test_env.Program('service_common_test', service_common_test_src)
-#Alias("service_common_test", service_common_test)
-#env.AppendTarget('service_common_test')
+service_common_test = service_common_test_env.Program('service_common_test', service_common_test_src)
+Alias("service_common_test", service_common_test)
+env.AppendTarget('service_common_test')
index 6a4b7ff..845737a 100644 (file)
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-#include <gtest/gtest.h>
-#include <HippoMocks/hippomocks.h>
+#include <UnitTestHelper.h>
 
 #include <PresenceSubscriber.h>
 #include <RCSException.h>
 
 #include <OCPlatform.h>
 
-using namespace testing;
 using namespace OIC::Service;
 
-typedef OCStackResult (*subscribePresenceSig1)(OC::OCPlatform::OCPresenceHandle&,
+typedef OCStackResult (*SubscribePresence1)(OC::OCPlatform::OCPresenceHandle&,
         const std::string&, OCConnectivityType, SubscribeCallback);
-typedef OCStackResult (*subscribePresenceSig2)(OC::OCPlatform::OCPresenceHandle&,
+typedef OCStackResult (*SubscribePresence2)(OC::OCPlatform::OCPresenceHandle&,
         const std::string&, const std::string&, OCConnectivityType, SubscribeCallback);
 
 const std::string HOST{ "host" };
 const OCConnectivityType CONTYPE{ };
 
-class PresenceSubscriberNonMemberTest: public Test
+
+class PresenceSubscriberNonMemberTest: public TestWithMock
 {
 public:
     OCDoHandle handle;
-    MockRepository mocks;
-
 };
 
 TEST_F(PresenceSubscriberNonMemberTest, OCPlatformSubscribePresenceWillBeCalled)
 {
     mocks.ExpectCallFuncOverload(
-            static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence))
+            static_cast< SubscribePresence1 >(OC::OCPlatform::subscribePresence))
                         .With(_, HOST,CONTYPE, _).Return(OC_STACK_OK);
 
     subscribePresence(handle, HOST, CONTYPE, SubscribeCallback());
@@ -57,7 +54,7 @@ TEST_F(PresenceSubscriberNonMemberTest, OCPlatformSubscribePresenceWillBeCalled)
 TEST_F(PresenceSubscriberNonMemberTest, SubscribePresenceThrowsIfResultIsNotOK)
 {
     mocks.ExpectCallFuncOverload(
-            static_cast< subscribePresenceSig1>(OC::OCPlatform::subscribePresence))
+            static_cast< SubscribePresence1>(OC::OCPlatform::subscribePresence))
                     .Return(OC_STACK_ERROR);
 
     ASSERT_THROW(subscribePresence(handle, "", CONTYPE, SubscribeCallback()), PlatformException);
@@ -77,17 +74,12 @@ TEST_F(PresenceSubscriberNonMemberTest, UnsubscribePresenceThrowIfResultIsNotOK)
     ASSERT_THROW(unsubscribePresence(handle), PlatformException);
 }
 
-
-
-class PresenceSubscriberTest: public Test
+class PresenceSubscriberTest: public TestWithMock
 {
-public:
-    MockRepository mocks;
-
 protected:
     void SetUp() {
         mocks.OnCallFuncOverload(
-                static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Do(
+                static_cast< SubscribePresence1 >(OC::OCPlatform::subscribePresence)).Do(
 
             [](OC::OCPlatform::OCPresenceHandle& handle, const std::string&,
                     OCConnectivityType, SubscribeCallback) -> OCStackResult
@@ -99,6 +91,7 @@ protected:
 
         mocks.OnCallFunc(OC::OCPlatform::unsubscribePresence).Return(OC_STACK_OK);
     }
+
 };
 
 TEST_F(PresenceSubscriberTest, IsNotSubscribingWhenCreatedWithDefaultConstructor)
@@ -110,7 +103,7 @@ TEST_F(PresenceSubscriberTest, IsNotSubscribingWhenCreatedWithDefaultConstructor
 TEST_F(PresenceSubscriberTest, ConstructorCallOCPlatformSubscribe)
 {
     mocks.ExpectCallFuncOverload(
-            static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence))
+            static_cast< SubscribePresence1 >(OC::OCPlatform::subscribePresence))
                      .With(_, HOST, CONTYPE, _).Return(OC_STACK_OK);
 
     PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };
@@ -121,7 +114,7 @@ TEST_F(PresenceSubscriberTest, ConstructorWithResourceTypeCallOCPlatformSubscrib
     const std::string resType { "resType" };
 
     mocks.ExpectCallFuncOverload(
-            static_cast< subscribePresenceSig2 >(OC::OCPlatform::subscribePresence))
+            static_cast< SubscribePresence2 >(OC::OCPlatform::subscribePresence))
                      .With(_, HOST, resType, CONTYPE, _).Return(OC_STACK_OK);
 
     PresenceSubscriber subscriber{ HOST, resType, CONTYPE, SubscribeCallback() };
@@ -130,7 +123,7 @@ TEST_F(PresenceSubscriberTest, ConstructorWithResourceTypeCallOCPlatformSubscrib
 TEST_F(PresenceSubscriberTest, ConstructorThrowsIfResultIsNotOK)
 {
     mocks.ExpectCallFuncOverload(
-            static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence))
+            static_cast< SubscribePresence1 >(OC::OCPlatform::subscribePresence))
                     .Return(OC_STACK_ERROR);
 
     ASSERT_THROW(PresenceSubscriber(HOST, CONTYPE, SubscribeCallback()), PlatformException);
@@ -166,7 +159,6 @@ TEST_F(PresenceSubscriberTest, IsSubscribingOfMovedSubscriberWithAssignmentRetur
 TEST_F(PresenceSubscriberTest, UnsubscribeWillBeCalledWhenSubscriberIsDestoryed)
 {
     mocks.ExpectCallFunc(OC::OCPlatform::unsubscribePresence).Return(OC_STACK_OK);
-    {
-        PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };
-    }
+
+    PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };
 }
index 04684e9..825585f 100644 (file)
@@ -18,8 +18,7 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-#include <gtest/gtest.h>
-#include <HippoMocks/hippomocks.h>
+#include <UnitTestHelper.h>
 
 #include <PrimitiveResourceImpl.h>
 #include <AssertUtils.h>
@@ -27,7 +26,6 @@
 #include <OCResource.h>
 #include <OCPlatform.h>
 
-using namespace testing;
 using namespace OIC::Service;
 
 const std::string KEY{ "key" };
@@ -56,15 +54,16 @@ public:
     virtual bool isObservable() const = 0;
 };
 
-class PrimitiveResourceTest: public Test
+class PrimitiveResourceTest: public TestWithMock
 {
 public:
-    MockRepository mocks;
     PrimitiveResource::Ptr resource;
     FakeOCResource* fakeResource;
 
 protected:
     void SetUp() {
+        TestWithMock::SetUp();
+
         fakeResource = mocks.Mock< FakeOCResource >();
 
         resource.reset(new PrimitiveResourceImpl< FakeOCResource >{
@@ -174,18 +173,14 @@ TEST_F(PrimitiveResourceTest, ResponseStatementHasSameValuesWithOCRepresentation
 }
 
 
-class DiscoverResourceTest: public Test
+class DiscoverResourceTest: public TestWithMock
 {
 public:
-    MockRepository mocks;
-
     typedef OCStackResult (*FindResource)(const std::string&, const std::string&,
             OCConnectivityType, OC::FindCallback);
 
-    static void discovered(std::shared_ptr< PrimitiveResource >)
-    {
-    }
-
+public:
+    static void discovered(std::shared_ptr< PrimitiveResource >) {}
 };
 
 TEST_F(DiscoverResourceTest, CallbackIsInvokedWhenResourceIsDiscovered)
diff --git a/service/resource-manipulation/src/common/utils/include/UnitTestHelper.h b/service/resource-manipulation/src/common/utils/include/UnitTestHelper.h
new file mode 100644 (file)
index 0000000..3a9e490
--- /dev/null
@@ -0,0 +1,48 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef COMMON_UTILS_UNITTESTHELPER_H
+#define COMMON_UTILS_UNITTESTHELPER_H
+
+#include <gtest/gtest.h>
+#include <HippoMocks/hippomocks.h>
+
+class TestWithMock: public testing::Test
+{
+public:
+    MockRepository mocks;
+
+protected:
+    virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
+
+    virtual void TearDown() {
+        try
+        {
+            mocks.VerifyAll();
+        }
+        catch (...)
+        {
+            mocks.reset();
+            throw;
+        }
+    }
+};
+
+#endif // COMMON_UTILS_UNITTESTHELPER_H
index 85f1377..2973af9 100644 (file)
@@ -80,7 +80,8 @@ server_builder_test_env = server_builder_env.Clone();
 server_builder_test_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
 server_builder_test_env.AppendUnique(CPPPATH = [
     env.get('SRC_DIR')+'/extlibs/hippomocks-master',
-    gtest_dir + '/include'
+    gtest_dir + '/include',
+    '../common/utils/include'
     ])
 
 gtest = File(gtest_dir + '/lib/.libs/libgtest.a')
@@ -89,7 +90,6 @@ gtest_main = File(gtest_dir + '/lib/.libs/libgtest_main.a')
 server_builder_test_env.PrependUnique(LIBS = [
     'server_builder',
     'service_common',
-    'pthread',
     'oc',
     'octbstack',
     'oc_logger',
@@ -97,10 +97,11 @@ server_builder_test_env.PrependUnique(LIBS = [
     'coap',
     gtest,
     gtest_main,
+    'pthread',
     ])
 
-#server_builder_test_src = env.Glob('unittests/*.cpp')
+server_builder_test_src = env.Glob('unittests/*.cpp')
 
-#server_builder_test = server_builder_test_env.Program('server_builder_test', server_builder_test_src)
-#Alias("server_builder_test", server_builder_test)
-#env.AppendTarget('server_builder_test')
+server_builder_test = server_builder_test_env.Program('server_builder_test', server_builder_test_src)
+Alias("server_builder_test", server_builder_test)
+env.AppendTarget('server_builder_test')
index 666c9ae..801c5d4 100644 (file)
@@ -18,8 +18,7 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-#include <gtest/gtest.h>
-#include <HippoMocks/hippomocks.h>
+#include <UnitTestHelper.h>
 
 #include <RCSResponse.h>
 #include <ResourceObject.h>
@@ -31,8 +30,6 @@
 
 using namespace std;
 
-using namespace testing;
-
 using namespace OIC::Service;
 using namespace OC;
 
@@ -56,12 +53,9 @@ void EXPECT_RESPONSE(shared_ptr< OCResourceResponse > ocResponse,
 }
 
 
-class RCSResponseTest: public Test
+class RCSResponseTest: public TestWithMock
 {
 public:
-    MockRepository mocks;
-
-public:
     template< typename T >
     shared_ptr< OCResourceResponse > buildResponse(const T& response)
     {
@@ -74,6 +68,8 @@ public:
 protected:
     void SetUp()
     {
+        TestWithMock::SetUp();
+
         mocks.OnCallFuncOverload(static_cast< registerResourceSig >(OCPlatform::registerResource))
                 .Return(OC_STACK_OK);
 
index f83a4e5..b863589 100644 (file)
@@ -18,8 +18,7 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-#include <gtest/gtest.h>
-#include <HippoMocks/hippomocks.h>
+#include <UnitTestHelper.h>
 
 #include <RequestHandler.h>
 
@@ -27,7 +26,6 @@
 
 using namespace std;
 
-using namespace testing;
 using namespace OIC::Service;
 
 constexpr char EXISTING[]{ "ext" };
@@ -38,16 +36,16 @@ constexpr int NEW_VALUE{ 1 };
 typedef OCStackResult (*RegisterResource)(OCResourceHandle&, std::string&,
         const std::string&, const std::string&, OC::EntityHandler, uint8_t);
 
-class RequestHandlerTest: public Test
+class RequestHandlerTest: public TestWithMock
 {
 public:
     ResourceObject::Ptr server;
 
-    MockRepository mocks;
-
 protected:
     void SetUp()
     {
+        TestWithMock::SetUp();
+
         mocks.OnCallFuncOverload(static_cast<RegisterResource>(OC::OCPlatform::registerResource))
                 .Return(OC_STACK_OK);
 
index efacf15..eaa327a 100755 (executable)
@@ -18,8 +18,7 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-#include <gtest/gtest.h>
-#include <HippoMocks/hippomocks.h>
+#include <UnitTestHelper.h>
 
 #include <ResourceObject.h>
 
@@ -28,8 +27,6 @@
 using namespace std;
 using namespace std::placeholders;
 
-using namespace testing;
-
 using namespace OIC::Service;
 using namespace OC;
 
@@ -48,14 +45,13 @@ TEST(ResourceObjectBuilderCreateTest, ThrowIfUriIsInvalid)
     ASSERT_THROW(ResourceObject::Builder("", "", "").build(), PlatformException);
 }
 
-class ResourceObjectBuilderTest: public Test
+class ResourceObjectBuilderTest: public TestWithMock
 {
-public:
-    MockRepository mocks;
-
 protected:
     void SetUp()
     {
+        TestWithMock::SetUp();
+
         mocks.OnCallFuncOverload(static_cast< registerResource >(OCPlatform::registerResource))
                 .Return(OC_STACK_OK);
     }
@@ -91,15 +87,16 @@ TEST_F(ResourceObjectBuilderTest, ResourceServerHasAttrsSetByBuilder)
 }
 
 
-class ResourceObjectTest: public Test
+class ResourceObjectTest: public TestWithMock
 {
 public:
-    MockRepository mocks;
     ResourceObject::Ptr server;
 
 protected:
     void SetUp()
     {
+        TestWithMock::SetUp();
+
         initMocks();
 
         server = ResourceObject::Builder(RESOURCE_URI, RESOURCE_TYPE, "").build();