Better exception testing 87/35587/1
authorPiotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
Wed, 18 Feb 2015 15:16:43 +0000 (16:16 +0100)
committerPiotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
Wed, 18 Feb 2015 15:16:43 +0000 (16:16 +0100)
[Bug/Feature]   N/A
[Cause]         N/A
[Solution]      N/A
[Verification]  Run tests

Change-Id: I814deebc0f8e4fdecd5760eb2b1dce1fe6e7df15

20 files changed:
common/base-exception.hpp
common/utils/exception.hpp
common/utils/fs.cpp
common/utils/value-latch.hpp
server/exception.hpp
server/input-monitor.cpp
tests/unit_tests/dbus/ut-connection.cpp
tests/unit_tests/log/ut-logger.cpp
tests/unit_tests/lxc/ut-zone.cpp
tests/unit_tests/server/ut-input-monitor.cpp
tests/unit_tests/server/ut-server.cpp
tests/unit_tests/server/ut-zone-admin.cpp
tests/unit_tests/server/ut-zone-connection.cpp
tests/unit_tests/server/ut-zone-provision.cpp
tests/unit_tests/server/ut-zone.cpp
tests/unit_tests/server/ut-zones-manager.cpp
tests/unit_tests/ut.hpp
tests/unit_tests/utils/ut-fs.cpp
tests/unit_tests/utils/ut-value-latch.cpp
zone-daemon/exception.hpp

index 5637e76..89b79bc 100644 (file)
@@ -38,7 +38,7 @@ namespace vasum {
  */
 struct VasumException: public std::runtime_error {
 
-    VasumException(const std::string& error = "") : std::runtime_error(error) {}
+    VasumException(const std::string& error) : std::runtime_error(error) {}
 };
 
 /**
index 56fd0bd..32f416c 100644 (file)
@@ -37,7 +37,7 @@ namespace vasum {
  */
 struct UtilsException: public VasumException {
 
-    UtilsException(const std::string& error = "") : VasumException(error) {}
+    UtilsException(const std::string& error) : VasumException(error) {}
 };
 
 
index b2260cc..d7ae2dd 100644 (file)
@@ -53,7 +53,7 @@ std::string readFileContent(const std::string& path)
 {
     std::string result;
     if (!readFileContent(path, result)) {
-        throw UtilsException();
+        throw UtilsException("Read failed");
     }
     return result;
 }
index 72c7873..0e06f6f 100644 (file)
@@ -78,7 +78,7 @@ void ValueLatch<T>::set(const T& value)
 {
     std::unique_lock<std::mutex> lock(mMutex);
     if (mValue) {
-        throw UtilsException("Cannot set ValueLatch multiple times!");
+        throw UtilsException("Cannot set value multiple times");
     }
     mValue.reset(new T(value));
     mCondition.notify_one();
@@ -89,7 +89,7 @@ void ValueLatch<T>::set(T&& value)
 {
     std::unique_lock<std::mutex> lock(mMutex);
     if (mValue) {
-        throw UtilsException("Cannot set ValueLatch multiple times!");
+        throw UtilsException("Cannot set value multiple times");
     }
     mValue.reset(new T(std::move(value)));
     mCondition.notify_one();
@@ -116,7 +116,7 @@ T ValueLatch<T>::get(const unsigned int timeoutMs)
         std::unique_ptr<T> retValue(std::move(mValue));
         return T(std::move(*retValue));
     } else {
-        throw UtilsException("Timeout occured.");
+        throw UtilsException("Timeout occured");
     }
 }
 
index cb358b1..bf5a106 100644 (file)
@@ -37,7 +37,7 @@ namespace vasum {
  */
 struct ServerException: public VasumException {
 
-    ServerException(const std::string& error = "") : VasumException(error) {}
+    ServerException(const std::string& error) : VasumException(error) {}
 };
 
 /**
@@ -46,7 +46,7 @@ struct ServerException: public VasumException {
  */
 struct ZoneOperationException: public ServerException {
 
-    ZoneOperationException(const std::string& error = "") : ServerException(error) {}
+    ZoneOperationException(const std::string& error) : ServerException(error) {}
 };
 
 /**
@@ -54,7 +54,7 @@ struct ZoneOperationException: public ServerException {
  */
 struct InvalidZoneIdException : public ServerException {
 
-    InvalidZoneIdException(const std::string& error = "") : ServerException(error) {}
+    InvalidZoneIdException(const std::string& error) : ServerException(error) {}
 };
 
 /**
@@ -62,7 +62,7 @@ struct InvalidZoneIdException : public ServerException {
  */
 struct ZoneConnectionException: public ServerException {
 
-    ZoneConnectionException(const std::string& error = "") : ServerException(error) {}
+    ZoneConnectionException(const std::string& error) : ServerException(error) {}
 };
 
 /**
@@ -70,7 +70,7 @@ struct ZoneConnectionException: public ServerException {
  */
 struct HostConnectionException: public ServerException {
 
-    HostConnectionException(const std::string& error = "") : ServerException(error) {}
+    HostConnectionException(const std::string& error) : ServerException(error) {}
 };
 
 /**
@@ -79,7 +79,7 @@ struct HostConnectionException: public ServerException {
 */
 struct InputMonitorException: public ServerException {
 
-    InputMonitorException(const std::string& error = "") : ServerException(error) {}
+    InputMonitorException(const std::string& error) : ServerException(error) {}
 };
 
 
index 7937840..68e63c0 100644 (file)
@@ -71,12 +71,12 @@ InputMonitor::InputMonitor(const InputConfig& inputConfig,
 {
     if (mConfig.timeWindowMs > MAX_TIME_WINDOW_SEC * 1000L) {
         LOGE("Time window exceeds maximum: " << MAX_TIME_WINDOW_SEC);
-        throw InputMonitorException();
+        throw InputMonitorException("Time window exceeds maximum");
     }
 
     if (mConfig.numberOfEvents > MAX_NUMBER_OF_EVENTS) {
         LOGE("Number of events exceeds maximum: " << MAX_NUMBER_OF_EVENTS);
-        throw InputMonitorException();
+        throw InputMonitorException("Number of events exceeds maximum");
     }
 
     std::string devicePath = getDevicePath();
@@ -168,7 +168,7 @@ std::string InputMonitor::getDevicePath() const
     if (it == end) {
         LOGE("None of the files under '" << DEVICE_DIR <<
              "' represents device named: " << device);
-        throw InputMonitorException();
+        throw InputMonitorException("Cannot find a device");
     }
 
     return it->path().string();
@@ -183,7 +183,7 @@ void InputMonitor::createGIOChannel(const std::string& devicePath)
     if (fd < 0) {
         LOGE("Cannot create input monitor channel. Device file: " <<
              devicePath << " doesn't exist");
-        throw InputMonitorException();
+        throw InputMonitorException("Device does not exist");
     }
 
     mChannelPtr = g_io_channel_unix_new(fd);
@@ -193,7 +193,7 @@ void InputMonitor::createGIOChannel(const std::string& devicePath)
                                   NULL,
                                   NULL) != G_IO_STATUS_NORMAL) {
         LOGE("Cannot set encoding for input monitor channel ");
-        throw InputMonitorException();
+        throw InputMonitorException("Cannot set encoding");
     }
 
     using namespace std::placeholders;
@@ -207,7 +207,7 @@ void InputMonitor::createGIOChannel(const std::string& devicePath)
                                     &utils::deleteCallbackWrapper<ReadDeviceCallback>);
     if (!mSourceId) {
         LOGE("Cannot add watch on device input file");
-        throw InputMonitorException();
+        throw InputMonitorException("Cannot add watch");
     }
 }
 
index 94b02b7..5182225 100644 (file)
@@ -574,10 +574,9 @@ BOOST_AUTO_TEST_CASE(DbusApiTest)
     BOOST_CHECK_EQUAL("Processed: arg", client.process("arg"));
     BOOST_CHECK_NO_THROW(client.throwException(0));
 
-    auto checkException = [](const DbusCustomException& e) {
-        return e.what() == std::string("Argument: 666");
-    };
-    BOOST_CHECK_EXCEPTION(client.throwException(666), DbusCustomException, checkException);
+    BOOST_CHECK_EXCEPTION(client.throwException(666),
+                          DbusCustomException,
+                          WhatEquals("Argument: 666"));
 }
 
 BOOST_AUTO_TEST_CASE(DbusApiNotifyTest)
index 74028db..ef3634e 100644 (file)
@@ -33,7 +33,7 @@
 
 #include <stdexcept>
 
-BOOST_AUTO_TEST_SUITE(LogSuite)
+BOOST_AUTO_TEST_SUITE(LoggerSuite)
 
 using namespace logger;
 
@@ -135,7 +135,9 @@ BOOST_AUTO_TEST_CASE(StringLogLevelSetandGet)
     Logger::setLogLevel("ERROR");
     BOOST_CHECK(LogLevel::ERROR == Logger::getLogLevel());
 
-    BOOST_REQUIRE_THROW(Logger::setLogLevel("UNKNOWN"), std::runtime_error);
+    BOOST_REQUIRE_EXCEPTION(Logger::setLogLevel("UNKNOWN"),
+                            std::runtime_error,
+                            WhatEquals("Invalid LogLevel to parse")); //TODO change message
 }
 
 BOOST_AUTO_TEST_CASE(TestLogsError)
index 6840e31..3b375bd 100644 (file)
@@ -94,7 +94,7 @@ BOOST_AUTO_TEST_CASE(CreateDestroyTest)
 
     BOOST_CHECK(lxc.isDefined());
     BOOST_CHECK_EQUAL(lxc.getConfigItem("lxc.rootfs"), LXC_PATH + ZONE_NAME + "/rootfs");
-    BOOST_CHECK_THROW(lxc.getConfigItem("xxx"), LxcException);
+    BOOST_CHECK_EXCEPTION(lxc.getConfigItem("xxx"), LxcException, WhatEquals("Key not found"));
 
     BOOST_CHECK(lxc.destroy());
 
index 241da6c..9b4292f 100644 (file)
@@ -94,23 +94,25 @@ BOOST_FIXTURE_TEST_SUITE(InputMonitorSuite, Fixture)
 
 BOOST_AUTO_TEST_CASE(Config_OK)
 {
-    BOOST_REQUIRE_NO_THROW(InputMonitor inputMonitor(inputConfig, InputMonitor::NotifyCallback()));
+    InputMonitor inputMonitor(inputConfig, InputMonitor::NotifyCallback());
 }
 
 BOOST_AUTO_TEST_CASE(Config_timeWindowMsTooHigh)
 {
     inputConfig.timeWindowMs = 50000;
 
-    BOOST_REQUIRE_THROW(InputMonitor inputMonitor(inputConfig, InputMonitor::NotifyCallback()),
-                        InputMonitorException);
+    BOOST_REQUIRE_EXCEPTION(InputMonitor inputMonitor(inputConfig, InputMonitor::NotifyCallback()),
+                            InputMonitorException,
+                            WhatEquals("Time window exceeds maximum"));
 }
 
 BOOST_AUTO_TEST_CASE(Config_deviceFilePathNotExisting)
 {
     inputConfig.device = TEST_INPUT_DEVICE + "notExisting";
 
-    BOOST_REQUIRE_THROW(InputMonitor inputMonitor(inputConfig, InputMonitor::NotifyCallback()),
-                        InputMonitorException);
+    BOOST_REQUIRE_EXCEPTION(InputMonitor inputMonitor(inputConfig, InputMonitor::NotifyCallback()),
+                            InputMonitorException,
+                            WhatEquals("Cannot find a device"));
 }
 
 namespace {
@@ -119,8 +121,7 @@ void sendNEvents(Fixture& f, unsigned int noOfEventsToSend)
 {
     Latch eventLatch;
 
-    std::unique_ptr<InputMonitor> inputMonitor;
-    BOOST_REQUIRE_NO_THROW(inputMonitor.reset(new InputMonitor(f.inputConfig, [&] {eventLatch.set();})));
+    InputMonitor inputMonitor(f.inputConfig, [&] {eventLatch.set();});
 
     int fd = ::open(TEST_INPUT_DEVICE.c_str(), O_WRONLY);
     BOOST_REQUIRE(fd >= 0);
@@ -166,8 +167,7 @@ void sendNEventsWithPauses(Fixture& f, unsigned int noOfEventsToSend)
 {
     Latch eventLatch;
 
-    std::unique_ptr<InputMonitor> inputMonitor;
-    BOOST_REQUIRE_NO_THROW(inputMonitor.reset(new InputMonitor(f.inputConfig, [&] {eventLatch.set();})));
+    InputMonitor inputMonitor(f.inputConfig, [&] {eventLatch.set();});
 
     int fd = ::open(TEST_INPUT_DEVICE.c_str(), O_WRONLY);
     BOOST_REQUIRE(fd >= 0);
index c65e652..4df5d89 100644 (file)
@@ -82,7 +82,9 @@ BOOST_AUTO_TEST_CASE(ConstructorDestructorTest)
 
 BOOST_AUTO_TEST_CASE(MissingConfigTest)
 {
-    BOOST_REQUIRE_THROW(Server(MISSING_CONFIG_PATH).run(AS_ROOT), ConfigException);//TODO check message
+    BOOST_REQUIRE_EXCEPTION(Server(MISSING_CONFIG_PATH).run(AS_ROOT),
+                            ConfigException,
+                            WhatEquals("Could not load " + MISSING_CONFIG_PATH));
 }
 
 BOOST_AUTO_TEST_CASE(TerminateTest)
index 21cc850..255e75e 100644 (file)
@@ -87,7 +87,9 @@ BOOST_AUTO_TEST_CASE(ConstructorDestructorTest)
 
 BOOST_AUTO_TEST_CASE(MissingConfigTest)
 {
-    BOOST_REQUIRE_THROW(create(MISSING_CONFIG_PATH), ZoneOperationException);//TODO check message
+    BOOST_REQUIRE_EXCEPTION(create(MISSING_CONFIG_PATH),
+                            ZoneOperationException,
+                            WhatEquals("Could not create zone"));
 }
 
 BOOST_AUTO_TEST_CASE(StartTest)
@@ -103,7 +105,9 @@ BOOST_AUTO_TEST_CASE(StartTest)
 BOOST_AUTO_TEST_CASE(StartBuggyTest)
 {
     auto admin = create(BUGGY_CONFIG_PATH);
-    BOOST_REQUIRE_THROW(admin->start(), ZoneOperationException);//TODO check message
+    BOOST_REQUIRE_EXCEPTION(admin->start(),
+                            ZoneOperationException,
+                            WhatEquals("Could not start zone"));
 }
 
 BOOST_AUTO_TEST_CASE(StopShutdownTest)
@@ -158,9 +162,9 @@ BOOST_AUTO_TEST_CASE(SuspendResumeTest)
 //
 //    admin->start();
 //    ensureStarted();
-//    BOOST_REQUIRE_NO_THROW(admin->setSchedulerLevel(SchedulerLevel::FOREGROUND));
+//    admin->setSchedulerLevel(SchedulerLevel::FOREGROUND);
 //    BOOST_REQUIRE(admin->getSchedulerQuota() == config.cpuQuotaForeground);
-//    BOOST_REQUIRE_NO_THROW(admin->setSchedulerLevel(SchedulerLevel::BACKGROUND));
+//    admin->setSchedulerLevel(SchedulerLevel::BACKGROUND);
 //    BOOST_REQUIRE(admin->getSchedulerQuota() == config.cpuQuotaBackground);
 //}
 
index a93b469..6d3e8e4 100644 (file)
@@ -132,7 +132,7 @@ BOOST_AUTO_TEST_CASE(ConstructorDestructorConnectTest)
     ScopedGlibLoop loop;
     ScopedDbusDaemon dbus;
 
-    BOOST_REQUIRE_NO_THROW(ZoneConnection(dbus.acquireAddress(), nullptr));
+    ZoneConnection(dbus.acquireAddress(), nullptr);
 }
 
 BOOST_AUTO_TEST_CASE(NotifyActiveZoneApiTest)
@@ -141,16 +141,14 @@ BOOST_AUTO_TEST_CASE(NotifyActiveZoneApiTest)
     ScopedDbusDaemon dbus;
 
     Latch notifyCalled;
-    std::unique_ptr<ZoneConnection> connection;
-
-    BOOST_REQUIRE_NO_THROW(connection.reset(new ZoneConnection(dbus.acquireAddress(), nullptr)));
+    ZoneConnection connection(dbus.acquireAddress(), nullptr);
 
     auto callback = [&](const std::string& application, const std::string& message) {
         if (application == "testapp" && message == "testmessage") {
             notifyCalled.set();
         }
     };
-    connection->setNotifyActiveZoneCallback(callback);
+    connection.setNotifyActiveZoneCallback(callback);
 
     DbusConnection::Pointer client = DbusConnection::create(dbus.acquireAddress());
     client->callMethod(api::zone::BUS_NAME,
@@ -168,9 +166,7 @@ BOOST_AUTO_TEST_CASE(SignalNotificationApiTest)
     ScopedDbusDaemon dbus;
 
     Latch signalEmitted;
-    std::unique_ptr<ZoneConnection> connection;
-
-    BOOST_REQUIRE_NO_THROW(connection.reset(new ZoneConnection(dbus.acquireAddress(), nullptr)));
+    ZoneConnection connection(dbus.acquireAddress(), nullptr);
 
     DbusConnection::Pointer client = DbusConnection::create(dbus.acquireAddress());
 
@@ -197,7 +193,7 @@ BOOST_AUTO_TEST_CASE(SignalNotificationApiTest)
     };
     client->signalSubscribe(handler, api::zone::BUS_NAME);
 
-    connection->sendNotification("testzone", "testapp", "testmessage");
+    connection.sendNotification("testzone", "testapp", "testmessage");
 
     BOOST_CHECK(signalEmitted.wait(EVENT_TIMEOUT));
 }
@@ -208,10 +204,7 @@ BOOST_AUTO_TEST_CASE(SignalDisplayOffApiTest)
     ScopedDbusDaemon dbus;
 
     Latch displayOffCalled;
-    std::unique_ptr<ZoneConnection> connection;
-
-    BOOST_REQUIRE_NO_THROW(connection.reset(new ZoneConnection(dbus.acquireAddress(),
-                                            nullptr)));
+    ZoneConnection connection(dbus.acquireAddress(), nullptr);
 
     DbusConnection::Pointer client = DbusConnection::create(dbus.acquireAddress());
 
@@ -219,7 +212,7 @@ BOOST_AUTO_TEST_CASE(SignalDisplayOffApiTest)
         displayOffCalled.set();
     };
 
-    connection->setDisplayOffCallback(callback);
+    connection.setDisplayOffCallback(callback);
 
     client->emitSignal(fake_power_manager_api::OBJECT_PATH,
                        fake_power_manager_api::INTERFACE,
index 9038fd4..4109cb2 100644 (file)
@@ -89,12 +89,6 @@ struct Fixture {
     }
 };
 
-std::function<bool(const std::exception&)> expectedMessage(const std::string& message) {
-    return [=](const std::exception& e) {
-        return e.what() == message;
-    };
-}
-
 } // namespace
 
 
@@ -265,7 +259,7 @@ BOOST_AUTO_TEST_CASE(DeclareMountTest)
     zoneProvision.declareMount("/fake/path2", "/fake/path2", "tmpfs", 077, "fake");
     BOOST_CHECK_EXCEPTION(zoneProvision.declareMount("/fake/path2", "/fake/path2", "tmpfs", 077, "fake"),
                           UtilsException,
-                          expectedMessage("Provision already exists"));
+                          WhatEquals("Provision already exists"));
 
     ZoneProvisioningConfig config;
     load(config);
@@ -397,7 +391,7 @@ BOOST_AUTO_TEST_CASE(RemoveTest)
     zoneProvision.remove("link /fake/path2 /fake/path4");
     BOOST_CHECK_EXCEPTION(zoneProvision.remove("link /fake/path_fake /fake/path2"),
                           UtilsException,
-                          expectedMessage("Can't find provision"));
+                          WhatEquals("Can't find provision"));
 
     const std::vector<std::string> provisions = zoneProvision.list();
     BOOST_REQUIRE_EQUAL(provisions.size(), expected.size());
index dbdf65a..3cd6031 100644 (file)
@@ -94,12 +94,16 @@ BOOST_AUTO_TEST_CASE(ConstructorDestructorTest)
 
 BOOST_AUTO_TEST_CASE(BuggyConfigTest)
 {
-    BOOST_REQUIRE_THROW(create(BUGGY_CONFIG_PATH), ZoneOperationException);//TODO check message
+    BOOST_REQUIRE_EXCEPTION(create(BUGGY_CONFIG_PATH),
+                            ZoneOperationException,
+                            WhatEquals("Could not create zone"));
 }
 
 BOOST_AUTO_TEST_CASE(MissingConfigTest)
 {
-    BOOST_REQUIRE_THROW(create(MISSING_CONFIG_PATH), ConfigException);//TODO check message
+    BOOST_REQUIRE_EXCEPTION(create(MISSING_CONFIG_PATH),
+                            ConfigException,
+                            WhatEquals("Could not load " + MISSING_CONFIG_PATH));
 }
 
 BOOST_AUTO_TEST_CASE(StartStopTest)
index 58ac060..e93ab84 100644 (file)
@@ -451,13 +451,6 @@ private:
     }
 };
 
-std::function<bool(const std::exception&)> expectedMessage(const std::string& message)
-{
-    return [=](const std::exception& e) {
-        return e.what() == message;
-    };
-}
-
 template<class Predicate>
 bool spinWaitFor(int timeoutMs, Predicate pred)
 {
@@ -497,7 +490,9 @@ BOOST_AUTO_TEST_CASE(ConstructorDestructorTest)
 
 BOOST_AUTO_TEST_CASE(MissingConfigTest)
 {
-    BOOST_REQUIRE_THROW(ZonesManager cm(MISSING_CONFIG_PATH), ConfigException);
+    BOOST_REQUIRE_EXCEPTION(ZonesManager{MISSING_CONFIG_PATH},
+                            ConfigException,
+                            WhatEquals("Could not load " + MISSING_CONFIG_PATH));
 }
 
 BOOST_AUTO_TEST_CASE(CreateTest)
@@ -867,12 +862,12 @@ BOOST_AUTO_TEST_CASE(ProxyCallTest)
     // host -> unknown
     BOOST_CHECK_EXCEPTION(dbuses.at(0)->testApiProxyCall("unknown", "param"),
                           DbusCustomException,
-                          expectedMessage("Unknown proxy call target"));
+                          WhatEquals("Unknown proxy call target"));
 
     // forwarding error
     BOOST_CHECK_EXCEPTION(dbuses.at(0)->testApiProxyCall("host", ""),
                           DbusCustomException,
-                          expectedMessage("Test error"));
+                          WhatEquals("Test error"));
 
     // forbidden call
     BOOST_CHECK_EXCEPTION(dbuses.at(0)->proxyCall("host",
@@ -882,7 +877,7 @@ BOOST_AUTO_TEST_CASE(ProxyCallTest)
                                               "foo",
                                               g_variant_new("(s)", "arg")),
                           DbusCustomException,
-                          expectedMessage("Proxy call forbidden"));
+                          WhatEquals("Proxy call forbidden"));
 }
 
 namespace {
@@ -1039,12 +1034,14 @@ BOOST_AUTO_TEST_CASE(SetActiveZoneTest)
         BOOST_CHECK(dbus.callMethodGetActiveZoneId() == zoneId);
     }
 
-    BOOST_REQUIRE_THROW(dbus.callMethodSetActiveZone(NON_EXISTANT_ZONE_ID),
-                        DbusException);
+    BOOST_REQUIRE_EXCEPTION(dbus.callMethodSetActiveZone(NON_EXISTANT_ZONE_ID),
+                            DbusException,
+                            WhatEquals("No such zone id"));
 
     cm.stopAll();
-    BOOST_REQUIRE_THROW(dbus.callMethodSetActiveZone("zone1"),
-                        DbusException);
+    BOOST_REQUIRE_EXCEPTION(dbus.callMethodSetActiveZone("zone1"),
+                            DbusException,
+                            WhatEquals("Could not activate stopped or paused zone"));
 }
 
 BOOST_AUTO_TEST_CASE(CreateDestroyZoneTest)
@@ -1204,16 +1201,20 @@ BOOST_AUTO_TEST_CASE(LockUnlockZoneTest)
         BOOST_CHECK(cm.isRunning(zoneId));
     }
 
-    BOOST_REQUIRE_THROW(dbus.callMethodLockZone(NON_EXISTANT_ZONE_ID),
-                        DbusException);
-    BOOST_REQUIRE_THROW(dbus.callMethodUnlockZone(NON_EXISTANT_ZONE_ID),
-                        DbusException);
+    BOOST_REQUIRE_EXCEPTION(dbus.callMethodLockZone(NON_EXISTANT_ZONE_ID),
+                            DbusException,
+                            WhatEquals("No such zone id"));
+    BOOST_REQUIRE_EXCEPTION(dbus.callMethodUnlockZone(NON_EXISTANT_ZONE_ID),
+                            DbusException,
+                            WhatEquals("No such zone id"));
 
     cm.stopAll();
-    BOOST_REQUIRE_THROW(dbus.callMethodLockZone("zone1"),
-                        DbusException);
-    BOOST_REQUIRE_THROW(dbus.callMethodUnlockZone("zone1"),
-                        DbusException);
+    BOOST_REQUIRE_EXCEPTION(dbus.callMethodLockZone("zone1"),
+                            DbusException,
+                            WhatEquals("Zone is not running"));
+    BOOST_REQUIRE_EXCEPTION(dbus.callMethodUnlockZone("zone1"),
+                            DbusException,
+                            WhatEquals("Zone is not paused"));
 }
 
 BOOST_AUTO_TEST_SUITE_END()
index cbc322a..d8b4b94 100644 (file)
 #define BOOST_TEST_DYN_LINK
 #include <boost/test/unit_test.hpp>
 
+#include <string>
+
+/**
+ * An exception message checker
+ *
+ * Usage example:
+ * BOOST_CHECK_EXCEPTION(foo(), SomeException, WhatEquals("oops"))
+ */
+class WhatEquals {
+public:
+    explicit WhatEquals(const std::string& message)
+        : mMessage(message) {}
+
+    template <typename T>
+    bool operator()(const T& e)
+    {
+        BOOST_WARN_EQUAL(e.what(), mMessage); // additional failure info
+        return e.what() == mMessage;
+    }
+private:
+    std::string mMessage;
+};
+
 #endif // UNIT_TESTS_UT_HPP
index 63b4f63..365c1e1 100644 (file)
@@ -76,7 +76,9 @@ const std::string FILE_NAME_RANDOM_2 =
 BOOST_AUTO_TEST_CASE(ReadFileContentTest)
 {
     BOOST_CHECK_EQUAL(FILE_CONTENT, readFileContent(FILE_PATH));
-    BOOST_CHECK_THROW(readFileContent(BUGGY_FILE_PATH), UtilsException);
+    BOOST_CHECK_EXCEPTION(readFileContent(BUGGY_FILE_PATH),
+                          UtilsException,
+                          WhatEquals("Read failed"));
 }
 
 BOOST_AUTO_TEST_CASE(SaveFileContentTest)
@@ -217,8 +219,7 @@ BOOST_AUTO_TEST_CASE(CopyDirContentsTest)
     BOOST_CHECK_EQUAL(readFileContent(dst_inner2 + "/" + FILE_NAME_RANDOM_1), FILE_CONTENT_3);
     BOOST_CHECK_EQUAL(readFileContent(dst_inner2 + "/" + FILE_NAME_RANDOM_2), FILE_CONTENT_2);
 
-    fs::file_status st;
-    BOOST_REQUIRE_NO_THROW(st = fs::status(fs::path(dst_inner2)));
+    fs::file_status st = fs::status(fs::path(dst_inner2));
     BOOST_CHECK(fs::owner_read == st.permissions());
 }
 
index a16128e..711c8e6 100644 (file)
@@ -105,7 +105,9 @@ BOOST_AUTO_TEST_CASE(TimeoutTest)
 {
     ValueLatch<int> testLatch;
 
-    BOOST_REQUIRE_THROW(testLatch.get(EXPECTED_TIMEOUT), vasum::UtilsException);
+    BOOST_REQUIRE_EXCEPTION(testLatch.get(EXPECTED_TIMEOUT),
+                            vasum::UtilsException,
+                            WhatEquals("Timeout occured"));
 }
 
 BOOST_AUTO_TEST_CASE(MultipleSetTest)
@@ -113,7 +115,9 @@ BOOST_AUTO_TEST_CASE(MultipleSetTest)
     ValueLatch<int> testLatch;
 
     testLatch.set(3);
-    BOOST_REQUIRE_THROW(testLatch.set(2), vasum::UtilsException);
+    BOOST_REQUIRE_EXCEPTION(testLatch.set(2),
+                            vasum::UtilsException,
+                            WhatEquals("Cannot set value multiple times"));
 }
 
 BOOST_AUTO_TEST_CASE(MultipleGetTest)
@@ -122,7 +126,9 @@ BOOST_AUTO_TEST_CASE(MultipleGetTest)
 
     testLatch.set(3);
     testLatch.get(TIMEOUT);
-    BOOST_REQUIRE_THROW(testLatch.get(EXPECTED_TIMEOUT), vasum::UtilsException);
+    BOOST_REQUIRE_EXCEPTION(testLatch.get(EXPECTED_TIMEOUT),
+                            vasum::UtilsException,
+                            WhatEquals("Timeout occured"));
 }
 
 BOOST_AUTO_TEST_SUITE_END()
index a281910..5fcf54d 100644 (file)
@@ -37,7 +37,7 @@ namespace zone_daemon {
  */
 struct ZoneDaemonException: public VasumException {
 
-    ZoneDaemonException(const std::string& error = "") : VasumException(error) {}
+    ZoneDaemonException(const std::string& error) : VasumException(error) {}
 };