Repository: hbase
Updated Branches:
  refs/heads/HBASE-14850 1193812d7 -> 6c442d54c


HBASE-18407 [C++] make Configuration::Set/GetBool work for both true/false and 
1/0 (Xiaobing Zhou)


Project: http://git-wip-us.apache.org/repos/asf/hbase/repo
Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/6c442d54
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/6c442d54
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/6c442d54

Branch: refs/heads/HBASE-14850
Commit: 6c442d54ca118d2af763336ec1ea79f2c8470d95
Parents: 1193812
Author: Enis Soztutar <e...@apache.org>
Authored: Fri Jul 21 16:46:54 2017 -0700
Committer: Enis Soztutar <e...@apache.org>
Committed: Fri Jul 21 16:46:54 2017 -0700

----------------------------------------------------------------------
 hbase-native-client/core/configuration-test.cc | 58 ++++++++++++++++++++-
 hbase-native-client/core/configuration.cc      | 10 ++--
 2 files changed, 64 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/6c442d54/hbase-native-client/core/configuration-test.cc
----------------------------------------------------------------------
diff --git a/hbase-native-client/core/configuration-test.cc 
b/hbase-native-client/core/configuration-test.cc
index 192ed46..abdf0c7 100644
--- a/hbase-native-client/core/configuration-test.cc
+++ b/hbase-native-client/core/configuration-test.cc
@@ -22,6 +22,62 @@
 
 using hbase::Configuration;
 
+TEST(Configuration, SetGetBool) {
+  Configuration conf;
+
+  /* test true/false */
+  conf.SetBool("bool_key1", true);
+  EXPECT_EQ(true, conf.GetBool("bool_key1", false));
+  conf.SetBool("bool_key2", false);
+  EXPECT_EQ(false, conf.GetBool("bool_key2", true));
+
+  /* test 1/0 */
+  conf.SetBool("bool_key3", 1);
+  EXPECT_EQ(true, conf.GetBool("bool_key3", false));
+  conf.SetBool("bool_key4", 0);
+  EXPECT_EQ(false, conf.GetBool("bool_key4", true));
+
+  /* test non zero integer */
+  conf.SetBool("bool_key5", 5);
+  EXPECT_EQ(true, conf.GetBool("bool_key5", false));
+  conf.SetBool("bool_key6", -1);
+  EXPECT_EQ(true, conf.GetBool("bool_key5", false));
+
+  /* test non zero float */
+  conf.SetBool("bool_key7", 5.1);
+  EXPECT_EQ(true, conf.GetBool("bool_key7", false));
+  conf.SetBool("bool_key8", -1.2);
+  EXPECT_EQ(true, conf.GetBool("bool_key8", false));
+}
+
+TEST(Configuration, SetGetForBool) {
+  Configuration conf;
+
+  /* test true/false */
+  conf.Set("bool_key1", "true");
+  EXPECT_EQ(true, conf.GetBool("bool_key1", false));
+  conf.Set("bool_key2", "false");
+  EXPECT_EQ(false, conf.GetBool("bool_key2", true));
+
+  /* test 1/0 */
+  conf.Set("bool_key3", "1");
+  EXPECT_EQ(true, conf.GetBool("bool_key3", false));
+  conf.Set("bool_key4", "0");
+  EXPECT_EQ(false, conf.GetBool("bool_key4", true));
+
+  /* test non zero integer */
+  conf.Set("bool_key5", "5");
+  EXPECT_THROW(conf.GetBool("bool_key5", false), std::runtime_error);
+  conf.Set("bool_key6", "-1");
+  EXPECT_THROW(conf.GetBool("bool_key6", false), std::runtime_error);
+
+  /* test non zero float */
+  conf.Set("bool_key7", "5.1");
+  EXPECT_THROW(conf.GetBool("bool_key7", false), std::runtime_error);
+  conf.Set("bool_key8", "-1.2");
+  EXPECT_THROW(conf.GetBool("bool_key8", false), std::runtime_error);
+}
+
 TEST(Configuration, SetGet) {
   Configuration conf;
 
@@ -54,7 +110,7 @@ TEST(Configuration, SetGetDouble) {
   EXPECT_EQ(conf.GetDouble("foo", 0), 42.0);
 }
 
-TEST(Configuration, SetGetBool) {
+TEST(Configuration, SetGetBoolBasic) {
   Configuration conf;
 
   EXPECT_EQ(conf.GetBool("foo", false), false);

http://git-wip-us.apache.org/repos/asf/hbase/blob/6c442d54/hbase-native-client/core/configuration.cc
----------------------------------------------------------------------
diff --git a/hbase-native-client/core/configuration.cc 
b/hbase-native-client/core/configuration.cc
index afca122..f4fc46d 100644
--- a/hbase-native-client/core/configuration.cc
+++ b/hbase-native-client/core/configuration.cc
@@ -25,6 +25,7 @@
 
 #include <glog/logging.h>
 #include <boost/lexical_cast.hpp>
+#include <boost/format.hpp>
 
 namespace hbase {
 
@@ -202,12 +203,15 @@ double Configuration::GetDouble(const std::string &key, 
double default_value) co
 optional<bool> Configuration::GetBool(const std::string &key) const {
   optional<std::string> raw = Get(key);
   if (raw) {
-    if (!strcasecmp((*raw).c_str(), "true")) {
+    if (!strcasecmp((*raw).c_str(), "true") || !strcasecmp((*raw).c_str(), 
"1")) {
       return std::experimental::make_optional(true);
-    } else if (!strcasecmp((*raw).c_str(), "false")) {
+    } else if (!strcasecmp((*raw).c_str(), "false") || 
!strcasecmp((*raw).c_str(), "0")) {
       return std::experimental::make_optional(false);
     } else {
-      throw std::runtime_error("Unexpected value found while conversion to 
bool.");
+      boost::format what("Unexpected value \"%s\" found being converted to 
bool for key \"%s\"");
+      what % (*raw);
+      what % key;
+      throw std::runtime_error(what.str());
     }
   }
   return optional<bool>();

Reply via email to