Parcourir la source

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


git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@921 e5f2f494-b856-4b98-b285-d166d9295462

Jelte Jansen il y a 15 ans
Parent
commit
6af2264545

+ 9 - 18
src/lib/config/cpp/config_data.cc

@@ -53,8 +53,7 @@ find_spec_part(ElementPtr spec, const std::string& identifier)
                 }
             }
             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 @@ find_spec_part(ElementPtr spec, const std::string& identifier)
                     }
                 }
                 if (!found) {
-                    // raise exception?
-                    return ElementPtr();
+                    dns_throw(DataNotFoundError, identifier);
                 }
             }
         }
@@ -92,8 +90,7 @@ find_spec_part(ElementPtr spec, const std::string& identifier)
                 }
             }
             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 @@ find_spec_part(ElementPtr spec, const std::string& identifier)
                     }
                 }
                 if (!found) {
-                    // raise exception?
-                    return ElementPtr();
+                    dns_throw(DataNotFoundError, identifier);
                 }
             }
         }
@@ -153,17 +149,12 @@ ElementPtr
 ConfigData::getValue(bool& is_default, const std::string& identifier)
 {
     ElementPtr value = _config->find(identifier);
-    if (!value) {
-        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 {
+    if (value) {
         is_default = false;
+    } else {
+        ElementPtr spec_part = find_spec_part(_module_spec.getConfigSpec(), identifier);
+        value = spec_part->get("item_default");
+        is_default = true;
     }
     return value;
 }

+ 1 - 1
src/lib/config/cpp/config_data.h

@@ -28,7 +28,7 @@ namespace config {
 
 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) {}
 };
     

+ 11 - 0
src/lib/config/cpp/config_data_unittests.cc

@@ -43,20 +43,31 @@ TEST(ConfigData, getValue) {
     //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) {

+ 10 - 0
src/lib/exceptions/cpp/exceptions.h

@@ -43,6 +43,16 @@ public:
     /// @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() {}
     //@}