HP-RK API Review: Add a new constructor and a new method to the CIMKeyBinding class...
authorkumpf <kumpf>
Fri, 20 Sep 2002 16:48:11 +0000 (16:48 +0000)
committerkumpf <kumpf>
Fri, 20 Sep 2002 16:48:11 +0000 (16:48 +0000)
src/Pegasus/Common/CIMInstanceRep.cpp
src/Pegasus/Common/CIMObjectPath.cpp
src/Pegasus/Common/CIMObjectPath.h
src/Pegasus/Common/tests/Reference/Reference.cpp

index 66819eabbc13c9e5fcf755cee519691d555c84ba..042847324343ad839db1b9cd533f05f8dd450222 100644 (file)
@@ -250,6 +250,7 @@ void CIMInstanceRep::toMof(Array<Sint8>& out) const
     out << "\n};\n";
 
 }
+
 CIMObjectPath CIMInstanceRep::buildPath(
     const CIMConstClass& cimClass) const
 {
@@ -267,7 +268,7 @@ CIMObjectPath CIMInstanceRep::buildPath(
     cimClass.getKeyNames(keyNames);
 
     if (keyNames.size() == 0)
-       return CIMObjectPath();
+       return CIMObjectPath("", CIMNamespaceName(), className);
 
     //--------------------------------------------------------------------------
     // Get type and value for each key (building up key bindings):
@@ -286,54 +287,7 @@ CIMObjectPath CIMInstanceRep::buildPath(
 
        if (keyName.equal(tmp.getName()))
        {
-           const CIMValue& value = tmp.getValue();
-
-           // ATTN-A: for now just assert:
-           if (value.isArray())
-               PEGASUS_ASSERT(false);
-
-           CIMType type = value.getType();
-           String valueStr;
-
-           CIMKeyBinding::Type kbType = CIMKeyBinding::STRING;
-
-           switch (type)
-           {
-               case CIMTYPE_BOOLEAN:
-                   kbType = CIMKeyBinding::BOOLEAN;
-                   valueStr = value.toString();
-                   break;
-
-               case CIMTYPE_UINT8:
-               case CIMTYPE_SINT8:
-               case CIMTYPE_UINT16:
-               case CIMTYPE_SINT16:
-               case CIMTYPE_UINT32:
-               case CIMTYPE_SINT32:
-               case CIMTYPE_UINT64:
-               case CIMTYPE_SINT64:
-               case CIMTYPE_CHAR16:
-                   kbType = CIMKeyBinding::NUMERIC;
-                   valueStr = value.toString();
-                   break;
-
-               case CIMTYPE_STRING:
-               case CIMTYPE_DATETIME:
-                   kbType = CIMKeyBinding::STRING;
-                   valueStr = value.toString();
-                   break;
-
-               case CIMTYPE_REFERENCE:
-                   kbType = CIMKeyBinding::REFERENCE;
-                   valueStr = value.toString();
-                   break;
-
-               case CIMTYPE_REAL32:
-               case CIMTYPE_REAL64:
-                   PEGASUS_ASSERT(false);
-           }
-
-           keyBindings.append(CIMKeyBinding(keyName, valueStr, kbType));
+           keyBindings.append(CIMKeyBinding(keyName, tmp.getValue()));
        }
     }
 
index 70e64d4f9e2065e4bc99d64ae1177544b437675a..437fff7699058704519c62abc33daa2f134b3f7e 100644 (file)
@@ -184,9 +184,16 @@ CIMKeyBinding::CIMKeyBinding(const CIMName& name, const String& value, Type type
     _rep = new CIMKeyBindingRep(name, value, type);
 }
 
-#ifdef PEGASUS_FUTURE
 CIMKeyBinding::CIMKeyBinding(const CIMName& name, const CIMValue& value)
 {
+    // ATTN-RK-20020920: Verify that real numbers cannot be keys
+    if (value.isArray() ||
+        (value.getType() == CIMTYPE_REAL32) ||
+        (value.getType() == CIMTYPE_REAL64))
+    {
+        throw TypeMismatchException();
+    }
+
     String kbValue = value.toString();
     Type kbType;
 
@@ -210,7 +217,6 @@ CIMKeyBinding::CIMKeyBinding(const CIMName& name, const CIMValue& value)
 
     _rep = new CIMKeyBindingRep(name, kbValue, kbType);
 }
-#endif
 
 CIMKeyBinding::~CIMKeyBinding()
 {
@@ -253,9 +259,16 @@ void CIMKeyBinding::setType(CIMKeyBinding::Type type)
     _rep->_type = type;
 }
 
-#ifdef PEGASUS_FUTURE
 Boolean CIMKeyBinding::equal(CIMValue value)
 {
+    // ATTN-RK-20020920: Verify that real numbers cannot be keys
+    if (value.isArray() ||
+        (value.getType() == CIMTYPE_REAL32) ||
+        (value.getType() == CIMTYPE_REAL64))
+    {
+        return false;
+    }
+
     CIMValue kbValue;
 
     try
@@ -263,18 +276,28 @@ Boolean CIMKeyBinding::equal(CIMValue value)
         switch (value.getType())
         {
         case CIMTYPE_CHAR16:
+            if (getType() != STRING) return false;
             kbValue.set(getValue()[0]);
             break;
         case CIMTYPE_DATETIME:
+            if (getType() != STRING) return false;
             kbValue.set(CIMDateTime(getValue()));
             break;
         case CIMTYPE_STRING:
+            if (getType() != STRING) return false;
             kbValue.set(getValue());
             break;
         case CIMTYPE_REFERENCE:
+            if (getType() != REFERENCE) return false;
             kbValue.set(CIMObjectPath(getValue()));
             break;
-        default:  // Boolean and numerics
+        case CIMTYPE_BOOLEAN:
+            if (getType() != BOOLEAN) return false;
+            kbValue = XmlReader::stringToValue(0, getValue().getCString(),
+                                               value.getType());
+            break;
+        default:  // Numerics
+            if (getType() != NUMERIC) return false;
             kbValue = XmlReader::stringToValue(0, getValue().getCString(),
                                                value.getType());
             break;
@@ -287,7 +310,6 @@ Boolean CIMKeyBinding::equal(CIMValue value)
 
     return value.equal(kbValue);
 }
-#endif
 
 Boolean operator==(const CIMKeyBinding& x, const CIMKeyBinding& y)
 {
index 17382f64d5f0b3c4cc38806d63ff8b2eb9bb47f9..4c01cfd286fb9f40eaf29fc99d73d77bfc316aad 100644 (file)
@@ -71,14 +71,14 @@ public:
     */
     CIMKeyBinding(const CIMName& name, const String& value, Type type);
 
-#ifdef PEGASUS_FUTURE
     /** Construct a CIMKeyBinding with a name and CIMValue, mapping from
         CIMValue types to CIMKeyBinding types.
         @param name CIMName for the key for this binding object.
         @param value CIMValue from which to extract the value for this key.
+        @exception TypeMismatchException if the type of the value is not valid
+        for a key property.
     */
     CIMKeyBinding(const CIMName& name, const CIMValue& value);
-#endif
 
     /** Destructor */
     ~CIMKeyBinding();
@@ -105,9 +105,7 @@ public:
     /** Modifier */
     void setType(Type type);
 
-#ifdef PEGASUS_FUTURE
     Boolean equal(CIMValue value);
-#endif
 
 private:
 
index a806d65cba1e029a89d442ac2696250498d23eec..e3dc1e6dd33a4f085e147cf74fd9d75597789137 100644 (file)
@@ -221,6 +221,89 @@ void test02()
     assert(errorDetected);
 }
 
+// Test CIMKeyBinding constructor (CIMValue variety) and equal(CIMValue) method
+void test03()
+{
+    Boolean exceptionFlag = false;
+    try
+    {
+        CIMKeyBinding kb0("test0", Real32(3.14159));
+    }
+    catch (TypeMismatchException&)
+    {
+        exceptionFlag = true;
+    }
+    assert(exceptionFlag);
+
+    CIMKeyBinding kb1("test1", String("3.14159"), CIMKeyBinding::NUMERIC);
+    assert(!kb1.equal(Real32(3.14159)));
+
+    CIMKeyBinding kb2("test2", Uint32(1000));
+    assert(kb2.equal(Uint32(1000)));
+    assert(!kb2.equal(Uint32(1001)));
+    assert(kb2.getValue() == "1000");
+
+    CIMKeyBinding kb3("test3", Char16('X'));
+    assert(kb3.equal(Char16('X')));
+    assert(!kb3.equal(Char16('Y')));
+    assert(kb3.getValue() == "X");
+
+    CIMKeyBinding kb4("test4", CIMDateTime("19991224120000.000000+360"));
+    assert(kb4.equal(CIMDateTime("19991224120000.000000+360")));
+    assert(!kb4.equal(CIMDateTime("19991225120000.000000+360")));
+    assert(kb4.getValue() == "19991224120000.000000+360");
+    kb4.setValue("0");
+    assert(!kb4.equal(CIMDateTime("19991224120000.000000+360")));
+
+    CIMKeyBinding kb5("test5", String("StringTest"));
+    assert(kb5.equal(String("StringTest")));
+    assert(!kb5.equal(String("StringTest1")));
+    assert(kb5.getValue() == "StringTest");
+
+    CIMKeyBinding kb6("test6", Boolean(true));
+    assert(kb6.equal(Boolean(true)));
+    assert(!kb6.equal(Boolean(false)));
+    assert(kb6.getValue() == "TRUE");
+    kb6.setValue("true1");
+    assert(!kb6.equal(Boolean(true)));
+
+    CIMKeyBinding kb7("test7", CIMObjectPath("//atp:77/root/cimv25:TennisPlayer.last=\"Rafter\",first=\"Patrick\""));
+    assert(kb7.equal(CIMObjectPath("//atp:77/root/cimv25:TennisPlayer.last=\"Rafter\",first=\"Patrick\"")));
+    assert(kb7.equal(CIMObjectPath("//atp:77/root/cimv25:TennisPlayer.FIRST=\"Patrick\",LAST=\"Rafter\"")));
+    assert(!kb7.equal(CIMObjectPath("//atp:77/root/cimv25:TennisPlayer.last=\"Rafter\"")));
+
+    exceptionFlag = false;
+    try
+    {
+        CIMKeyBinding kb8("test8", Array<Uint32>());
+    }
+    catch (TypeMismatchException&)
+    {
+        exceptionFlag = true;
+    }
+    assert(exceptionFlag);
+
+    CIMKeyBinding kb9("test9", String("1000"), CIMKeyBinding::STRING);
+    assert(!kb9.equal(Uint32(1000)));
+
+    CIMKeyBinding kb10("test10", String("100"), CIMKeyBinding::NUMERIC);
+    assert(kb10.equal(Uint64(100)));
+    assert(kb10.equal(Uint32(100)));
+    assert(kb10.equal(Uint16(100)));
+    assert(kb10.equal(Uint8(100)));
+    assert(kb10.equal(Sint64(100)));
+    assert(kb10.equal(Sint32(100)));
+    assert(kb10.equal(Sint16(100)));
+    assert(kb10.equal(Sint8(100)));
+    assert(!kb10.equal(String("100")));
+
+    CIMKeyBinding kb11("test11", String("+100"), CIMKeyBinding::NUMERIC);
+    assert(kb11.equal(Sint64(100)));
+    assert(kb11.equal(Sint32(100)));
+    assert(kb11.equal(Sint16(100)));
+    assert(kb11.equal(Sint8(100)));
+    assert(!kb11.equal(String("100")));
+}
 
 int main(int argc, char** argv)
 {
@@ -230,6 +313,7 @@ int main(int argc, char** argv)
     {
        test01();
        test02();
+       test03();
 
         cout << argv[0] << " +++++ passed all tests" << endl;
     }