Add Cynara::BucketRecordCorruptedException
authorAleksander Zdyb <a.zdyb@partner.samsung.com>
Thu, 26 Jun 2014 10:32:54 +0000 (12:32 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Thu, 3 Jul 2014 12:19:10 +0000 (14:19 +0200)
Change-Id: Id4b28a9c3dfbf5e4bae0c750dc8d173db44eea6e

src/common/exceptions/BucketRecordCorruptedException.h [new file with mode: 0644]
test/CMakeLists.txt
test/common/exceptions/bucketrecordcorrupted.cpp [new file with mode: 0644]

diff --git a/src/common/exceptions/BucketRecordCorruptedException.h b/src/common/exceptions/BucketRecordCorruptedException.h
new file mode 100644 (file)
index 0000000..d0a5473
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd 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.
+ */
+/*
+ * @file        BucketRecordCorruptedException.h
+ * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
+ * @version     1.0
+ * @brief       Implementation of BucketRecordCorruptedException
+ */
+#ifndef SRC_COMMON_EXCEPTIONS_BUCKETRECORDCORRUPTEDEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_BUCKETRECORDCORRUPTEDEXCEPTION_H_
+
+#include "Exception.h"
+
+#include <string>
+
+namespace Cynara {
+
+class BucketRecordCorruptedException : public Exception {
+public:
+    BucketRecordCorruptedException(void) = delete;
+    virtual ~BucketRecordCorruptedException(void) = default;
+
+    BucketRecordCorruptedException(const std::string &line)
+        : m_line(line), m_lineNumber(0) {}
+
+    BucketRecordCorruptedException withLineNumber(const size_t &lineNumber) const {
+        BucketRecordCorruptedException copy(*this);
+        copy.m_lineNumber = lineNumber;
+        copy.m_whatMsg.clear();
+        return copy;
+    }
+
+    BucketRecordCorruptedException withFilename(const std::string &filename) const {
+        BucketRecordCorruptedException copy(*this);
+        copy.m_filename = filename;
+        copy.m_whatMsg.clear();
+        return copy;
+    }
+
+    virtual const char* what(void) const noexcept {
+        if (m_whatMsg.empty()) {
+            m_whatMsg = "Bucket record corrupted at"
+                + formatedFilename()
+                + formatedLineNumber()
+                + ": <" + slicedLine() + ">";
+        }
+        return m_whatMsg.c_str();
+    }
+
+protected:
+    inline std::string slicedLine(void) const {
+        return m_line.substr(0, 50) + (m_line.size() > 50 ? "..." : "");
+    }
+
+    inline std::string formatedFilename(void) const {
+        return m_filename.empty() ? " line" : " " + m_filename;
+    }
+
+    inline std::string formatedLineNumber(void) const {
+        return m_lineNumber <= 0 ? ""
+                : (m_filename.empty() ? " " : ":")
+                  + std::to_string(static_cast<long int>(m_lineNumber));
+    }
+
+private:
+    size_t m_lineNumber;
+    std::string m_line;
+    std::string m_filename;
+    mutable std::string m_whatMsg;
+
+public:
+    const std::string &filename(void) const {
+        return m_filename;
+    }
+
+    const std::string &line(void) const {
+        return m_line;
+    }
+
+    size_t lineNumber(void) const {
+        return m_lineNumber;
+    }
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_EXCEPTIONS_BUCKETRECORDCORRUPTEDEXCEPTION_H_ */
index fddc664..7068a51 100644 (file)
@@ -32,6 +32,7 @@ SET(CYNARA_SOURCES_FOR_TESTS
 )
 
 SET(CYNARA_TESTS_SOURCES
+    common/exceptions/bucketrecordcorrupted.cpp
     types/policykey.cpp
     storage/storage/policies.cpp
     storage/storage/check.cpp
diff --git a/test/common/exceptions/bucketrecordcorrupted.cpp b/test/common/exceptions/bucketrecordcorrupted.cpp
new file mode 100644 (file)
index 0000000..80aaadb
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd 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.
+ */
+/*
+ * @file        bucketrecordcorrupted.cpp
+ * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
+ * @version     1.0
+ * @brief       Tests for Cynara::BucketRecordCorruptedException
+ */
+
+
+#include <gtest/gtest.h>
+#include <gmock/gmock.h>
+
+#include "common/exceptions/BucketRecordCorruptedException.h"
+
+using namespace Cynara;
+
+TEST(BucketRecordCorruptedException, line) {
+    BucketRecordCorruptedException ex("line");
+    auto expected = "Bucket record corrupted at line: <line>";
+    ASSERT_STREQ(expected, ex.what());
+    ASSERT_EQ("line", ex.line());
+    ASSERT_EQ("", ex.filename());
+    ASSERT_EQ(0, ex.lineNumber());
+}
+
+TEST(BucketRecordCorruptedException, line_lineno) {
+    auto ex = BucketRecordCorruptedException("line").withLineNumber(10);
+    auto expected = "Bucket record corrupted at line 10: <line>";
+    ASSERT_STREQ(expected, ex.what());
+    ASSERT_EQ("line", ex.line());
+    ASSERT_EQ("", ex.filename());
+    ASSERT_EQ(10, ex.lineNumber());
+}
+
+TEST(BucketRecordCorruptedException, line_lineno_filename) {
+    auto ex = BucketRecordCorruptedException("line").withLineNumber(10).withFilename("bucket.bkt");
+    auto expected = "Bucket record corrupted at bucket.bkt:10: <line>";
+    ASSERT_STREQ(expected, ex.what());
+    ASSERT_EQ("line", ex.line());
+    ASSERT_EQ("bucket.bkt", ex.filename());
+    ASSERT_EQ(10, ex.lineNumber());
+}
+
+TEST(BucketRecordCorruptedException, line_filename) {
+    auto ex = BucketRecordCorruptedException("line").withFilename("bucket.bkt");
+    auto expected = "Bucket record corrupted at bucket.bkt: <line>";
+    ASSERT_STREQ(expected, ex.what());
+    ASSERT_EQ("line", ex.line());
+    ASSERT_EQ("bucket.bkt", ex.filename());
+    ASSERT_EQ(0, ex.lineNumber());
+}
+
+TEST(BucketRecordCorruptedException, line_sliced) {
+    std::string line = "A very long line placed here just to check,"
+                       " if slicing works as expected (83 chars)";
+    auto ex = BucketRecordCorruptedException(line);
+    auto expected = "Bucket record corrupted at line:"
+            " <A very long line placed here just to check, if sli...>";
+    ASSERT_STREQ(expected, ex.what());
+    ASSERT_EQ(line, ex.line());
+    ASSERT_EQ("", ex.filename());
+    ASSERT_EQ(0, ex.lineNumber());
+}