[svn] commit: r921 - in /trunk/src/lib: config/cpp/config_data.cc config/cpp/config_data.h config/cpp/config_data_unittests.cc exceptions/cpp/exceptions.h

BIND 10 source code commits bind10-changes at lists.isc.org
Tue Feb 23 13:03:09 UTC 2010


Author: jelte
Date: Tue Feb 23 13:03:09 2010
New Revision: 921

Log:
allow for std:string exception constructors
getValue now raises DataNotFoundError if a bad identifier is given
some more tests

Modified:
    trunk/src/lib/config/cpp/config_data.cc
    trunk/src/lib/config/cpp/config_data.h
    trunk/src/lib/config/cpp/config_data_unittests.cc
    trunk/src/lib/exceptions/cpp/exceptions.h

Modified: trunk/src/lib/config/cpp/config_data.cc
==============================================================================
--- trunk/src/lib/config/cpp/config_data.cc (original)
+++ trunk/src/lib/config/cpp/config_data.cc Tue Feb 23 13:03:09 2010
@@ -53,8 +53,7 @@
                 }
             }
             if (!found) {
-                // raise exception?
-                return ElementPtr();
+                dns_throw(DataNotFoundError, identifier);
             }
         } else if (spec_part->getType() == Element::map) {
             if (spec_part->contains("map_item_spec")) {
@@ -68,8 +67,7 @@
                     }
                 }
                 if (!found) {
-                    // raise exception?
-                    return ElementPtr();
+                    dns_throw(DataNotFoundError, identifier);
                 }
             }
         }
@@ -92,8 +90,7 @@
                 }
             }
             if (!found) {
-                // raise exception?
-                return ElementPtr();
+                dns_throw(DataNotFoundError, identifier);
             }
         } else if (spec_part->getType() == Element::map) {
             if (spec_part->contains("map_item_spec")) {
@@ -107,8 +104,7 @@
                     }
                 }
                 if (!found) {
-                    // raise exception?
-                    return ElementPtr();
+                    dns_throw(DataNotFoundError, identifier);
                 }
             }
         }
@@ -153,17 +149,12 @@
 ConfigData::getValue(bool& is_default, const std::string& identifier)
 {
     ElementPtr value = _config->find(identifier);
-    if (!value) {
+    if (value) {
+        is_default = false;
+    } else {
         ElementPtr spec_part = find_spec_part(_module_spec.getConfigSpec(), identifier);
-        if (spec_part) {
-            value = spec_part->get("item_default");
-            is_default = true;
-        } else {
-            // we should raise an error here
-            dns_throw(DataNotFoundError, "identifier not found");
-        }
-    } else {
-        is_default = false;
+        value = spec_part->get("item_default");
+        is_default = true;
     }
     return value;
 }

Modified: trunk/src/lib/config/cpp/config_data.h
==============================================================================
--- trunk/src/lib/config/cpp/config_data.h (original)
+++ trunk/src/lib/config/cpp/config_data.h Tue Feb 23 13:03:09 2010
@@ -28,7 +28,7 @@
 
 class DataNotFoundError : public isc::Exception {
 public:
-    DataNotFoundError(const char* file, size_t line, const char* what) :
+    DataNotFoundError(const char* file, size_t line, const std::string& what) :
         isc::Exception(file, line, what) {}
 };
     

Modified: trunk/src/lib/config/cpp/config_data_unittests.cc
==============================================================================
--- trunk/src/lib/config/cpp/config_data_unittests.cc (original)
+++ trunk/src/lib/config/cpp/config_data_unittests.cc Tue Feb 23 13:03:09 2010
@@ -43,20 +43,31 @@
     //std::cout << "[XX] SPEC2: " << cd.getModuleSpec().getFullSpec() << std::endl;
     bool is_default;
     //ElementPtr value = cd.getValue(is_default, "item1");
+    EXPECT_EQ(1, cd.getValue("item1")->intValue());
     EXPECT_EQ(1, cd.getValue(is_default, "item1")->intValue());
     EXPECT_TRUE(is_default);
+    EXPECT_EQ(1.1, cd.getValue("item2")->doubleValue());
     EXPECT_EQ(1.1, cd.getValue(is_default, "item2")->doubleValue());
     EXPECT_TRUE(is_default);
+    EXPECT_TRUE(cd.getValue("item3")->boolValue());
     EXPECT_TRUE(cd.getValue(is_default, "item3")->boolValue());
     EXPECT_TRUE(is_default);
+    EXPECT_EQ("test", cd.getValue("item4")->stringValue());
     EXPECT_EQ("test", cd.getValue(is_default, "item4")->stringValue());
     EXPECT_TRUE(is_default);
+    EXPECT_EQ("a", cd.getValue("item5")->get(0)->stringValue());
     EXPECT_EQ("a", cd.getValue(is_default, "item5")->get(0)->stringValue());
     EXPECT_TRUE(is_default);
+    EXPECT_EQ("b", cd.getValue("item5")->get(1)->stringValue());
     EXPECT_EQ("b", cd.getValue(is_default, "item5")->get(1)->stringValue());
     EXPECT_TRUE(is_default);
+    EXPECT_EQ("{}", cd.getValue("item6")->str());
     EXPECT_EQ("{}", cd.getValue(is_default, "item6")->str());
     EXPECT_TRUE(is_default);
+
+    EXPECT_THROW(cd.getValue("no_such_item")->str(), DataNotFoundError);
+    EXPECT_THROW(cd.getValue("item6/no_such_item")->str(), DataNotFoundError);
+
 }
 
 TEST(ConfigData, setLocalConfig) {

Modified: trunk/src/lib/exceptions/cpp/exceptions.h
==============================================================================
--- trunk/src/lib/exceptions/cpp/exceptions.h (original)
+++ trunk/src/lib/exceptions/cpp/exceptions.h Tue Feb 23 13:03:09 2010
@@ -43,6 +43,16 @@
     /// @param what a description (type) of the exception.
     Exception(const char* file, size_t line, const char* what) :
         file_(file), line_(line), what_(what) {}
+
+    /// \brief Constructor for a given type for exceptions with file name and
+    /// file line number.
+    ///
+    /// @param file the file name where the exception was thrown.
+    /// @param line the line in @ref file where the exception was thrown.
+    /// @param what a description (type) of the exception.
+    Exception(const char* file, size_t line, const std::string& what) :
+        file_(file), line_(line), what_(what) {}
+
     /// The destructor
     virtual ~Exception() throw() {}
     //@}




More information about the bind10-changes mailing list