Esempio n. 1
0
void test01()
{
    // class MyClass : YourClass
    // {
    //     string message = "Hello";
    // }
    try
    {
        CIMName a = "A_class1";
        CIMName b = "A_class2";
        CIMClass c0(a, b);
        CIMClass c1(a, CIMName("A_class2"));
        CIMClass c2(CIMName("A_class1"), b);
        CIMClass c3(b, a);
    }
    catch (InvalidNameException & ine)
    {
        if (verbose)
        {
        cout << "Caught unexpected exception: " << ine.getMessage() << endl;
        }
    }
    try
    {
        //
        //  Invalid class name
        //
        CIMClass class0(CIMName ("//localhost/root/cimv2:MyClass"),
            CIMName ("YourClass"));

        PEGASUS_TEST_ASSERT(class0.getPath() ==
            CIMObjectPath("//localhost/root/cimv2:MyClass"));
    }
    catch (InvalidNameException & ine)
    {
        if (verbose)
        {
        cout << "Caught expected exception: " << ine.getMessage() << endl;
        }
    }

    CIMClass class1(CIMName ("MyClass"), CIMName ("YourClass"));

    class1
    .addQualifier(CIMQualifier(CIMName ("association"), true))
    .addQualifier(CIMQualifier(CIMName ("q1"), Uint32(55)))
    .addQualifier(CIMQualifier(CIMName ("q2"), String("Hello")))
    .addProperty(CIMProperty(CIMName ("message"), String("Hello")))
    .addProperty(CIMProperty(CIMName ("count"), Uint32(77), 0, CIMName(),
            CIMName("YourClass"), true))
    .addMethod(CIMMethod(CIMName ("isActive"), CIMTYPE_BOOLEAN)
        .addParameter(CIMParameter(CIMName ("hostname"), CIMTYPE_STRING))
        .addParameter(CIMParameter(CIMName ("port"), CIMTYPE_UINT32)));

    // Test the method count function
    PEGASUS_TEST_ASSERT(class1.getClassName().equal(CIMName ("myclass")));
    PEGASUS_TEST_ASSERT(class1.getSuperClassName() == CIMName ("YourClass"));

    PEGASUS_TEST_ASSERT(class1.getMethodCount() ==1);


    // Test the findMethod and isMethod functions
    PEGASUS_TEST_ASSERT(class1.findMethod(
                CIMName ("isActive")) != PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.findMethod(
                CIMName ("DoesNotExist")) == PEG_NOT_FOUND);

    PEGASUS_TEST_ASSERT(class1.findMethod(
                CIMName ("isActive")) != PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.findMethod(
                CIMName ("DoesNotExist")) == PEG_NOT_FOUND);

    // Test the manipulation of an embeddedObjectProperty
    CIMClass embedClass(CIMName ("embedObj"), CIMName ());
    class1.addProperty(CIMProperty(CIMName ("embedObj"),
        CIMObject(embedClass), 0, CIMName(),
            CIMName(), false));

    PEGASUS_TEST_ASSERT(class1.findProperty(
                CIMName ("embedObj")) != PEG_NOT_FOUND);
    Uint32  posProp = class1.findProperty(CIMName ("embedObj"));
    CIMConstProperty constprop1 = class1.getProperty(posProp);
    PEGASUS_TEST_ASSERT(constprop1.getClassOrigin() == CIMName());
    PEGASUS_TEST_ASSERT(constprop1.getType() == CIMTYPE_OBJECT);
    class1.removeProperty(posProp);

    // Now add another method and reconfirm.

    class1.addMethod(CIMMethod(CIMName ("makeActive"), CIMTYPE_BOOLEAN)
    .addParameter(CIMParameter(CIMName ("hostname"), CIMTYPE_STRING))
    .addParameter(CIMParameter(CIMName ("port"), CIMTYPE_UINT32)));

    PEGASUS_TEST_ASSERT(class1.getMethodCount() == 2);

    // Test the findMethod and isMethod functions
    // with two methods defined
    PEGASUS_TEST_ASSERT(class1.findMethod(
                CIMName ("isActive")) != PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.findMethod(
                CIMName ("makeActive")) != PEG_NOT_FOUND);

    PEGASUS_TEST_ASSERT(class1.findMethod(
                CIMName ("DoesNotExist")) == PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.findMethod(
                CIMName ("isActive")) != PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.findMethod(
                CIMName ("makeActive")) != PEG_NOT_FOUND);

    PEGASUS_TEST_ASSERT(class1.findMethod(
                CIMName ("DoesNotExist")) == PEG_NOT_FOUND);


    // Test RemoveMethod function
    Uint32 posMethod;
    posMethod = class1.findMethod(CIMName ("isActive"));
    PEGASUS_TEST_ASSERT(posMethod != PEG_NOT_FOUND);

    class1.removeMethod(posMethod);

    PEGASUS_TEST_ASSERT(class1.findMethod(
                CIMName ("isActive")) == PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.getMethodCount() == 1);

    //ATTN: P3 TODO add tests for different case names

    //Qualifier manipulation tests  (find, remove)

    PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q1")) != PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q2")) != PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("qx")) == PEG_NOT_FOUND);

    PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q1")) != PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q2")) != PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.findQualifier(
                CIMName ("association")) != PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.isAssociation());

    // Remove middle Qualifier "q2"
    Uint32 posQualifier;
    posQualifier = class1.findQualifier(CIMName ("q2"));
    CIMConstQualifier qconst = class1.getQualifier(posQualifier);

    PEGASUS_TEST_ASSERT(class1.getQualifierCount() == 3);
    PEGASUS_TEST_ASSERT(posQualifier <= class1.getQualifierCount());
    class1.removeQualifier(posQualifier);
    PEGASUS_TEST_ASSERT(class1.getQualifierCount() == 2);

    PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q2")) == PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q1")) != PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.isAssociation());


    // Remove the first parameter "q1"
    posQualifier = class1.findQualifier(CIMName ("q1"));

    PEGASUS_TEST_ASSERT(class1.getQualifierCount() == 2);
    CIMQualifier cq = class1.getQualifier( class1.findQualifier(
                CIMName ("q1")));
    PEGASUS_TEST_ASSERT(posQualifier <= class1.getQualifierCount());
    class1.removeQualifier(posQualifier);
    PEGASUS_TEST_ASSERT(class1.getQualifierCount() == 1);

    PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q1")) == PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q2")) == PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.isAssociation());


    // ATTH: P3 Add tests for try block for outofbounds



    //The property manipulation tests.

    PEGASUS_TEST_ASSERT(class1.findProperty(
                CIMName ("count")) != PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.findProperty(
                CIMName ("message")) != PEG_NOT_FOUND);

    PEGASUS_TEST_ASSERT(class1.findProperty(
                CIMName ("isActive")) == PEG_NOT_FOUND);

    PEGASUS_TEST_ASSERT(class1.getPropertyCount() == 2);


    Uint32  posProperty;
    posProperty = class1.findProperty(CIMName ("count"));
    CIMConstProperty constprop = class1.getProperty(posProperty);
    PEGASUS_TEST_ASSERT(constprop.getClassOrigin() == CIMName("YourClass"));
    PEGASUS_TEST_ASSERT(constprop.getPropagated());
    class1.removeProperty(posProperty);
    PEGASUS_TEST_ASSERT(class1.findProperty(
                CIMName ("message")) != PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(class1.findProperty(
                CIMName ("count")) == PEG_NOT_FOUND);

    PEGASUS_TEST_ASSERT(class1.getPropertyCount() == 1);
    CIMProperty cp = class1.getProperty( class1.findProperty
        (CIMName ("message")));
    PEGASUS_TEST_ASSERT(cp.getClassOrigin().isNull());
    PEGASUS_TEST_ASSERT(!cp.getPropagated());

    if(verbose)
    {
        XmlWriter::printClassElement(class1);
        MofWriter::printClassElement(class1);
    }

    Buffer out;
    MofWriter::appendClassElement(out, class1);
    out.clear();
    XmlWriter::appendClassElement(out, class1);

    PEGASUS_TEST_ASSERT(!class1.isAbstract());

    CIMName squal("q1");
    PEGASUS_TEST_ASSERT(class1.findQualifier(squal) == PEG_NOT_FOUND);

    PEGASUS_TEST_ASSERT(!class1.hasKeys());

    Array<CIMName> keyNames;
    class1.getKeyNames(keyNames);

    CIMClass c2(CIMName ("MyClass"));

    PEGASUS_TEST_ASSERT(c2.getClassName().equal(CIMName ("myclass")));


    // Error uninitialized handle
    c2.setSuperClassName(CIMName ("CIM_Element"));
    PEGASUS_TEST_ASSERT(c2.getSuperClassName() == CIMName ("CIM_Element"));

    CIMClass c3 = c2.clone();
    c3 = c2;


    try
    {
        CIMMethod cm = c2.getMethod(0);
    }
    catch(IndexOutOfBoundsException& e)
    {
        if(verbose)
            cout << "Exception: " << e.getMessage() << endl;
    }

    const CIMClass c4(CIMName ("MyClass"), CIMName ("YourClass"));

    CIMConstClass c5(CIMName ("MyClass"), CIMName ("YourClass"));
    CIMConstClass c6(CIMName ("MyClass"));
    CIMConstClass cc7(c6);
    CIMClass c7 = c5.clone();
    const CIMClass c8(class1);

    // Test the findMethod and isMethod functions
    PEGASUS_TEST_ASSERT(c7.findMethod(
                CIMName ("DoesNotExist")) == PEG_NOT_FOUND);

    PEGASUS_TEST_ASSERT(c7.findQualifier(CIMName ("dummy")) == PEG_NOT_FOUND);

    try
    {
        CIMConstMethod cm = c8.getMethod(0);
    }
    catch(IndexOutOfBoundsException& e)
    {
        if(verbose)
            cout << "Exception: " << e.getMessage() << endl;
    }

    try
    {
        CIMConstProperty ccp = c8.getProperty(c8.findProperty
            (CIMName ("count")));
    }
    catch(IndexOutOfBoundsException& e)
    {
        if(verbose)
            cout << "Exception: " << e.getMessage() << endl;
    }

    if(verbose)
    {
    XmlWriter::printClassElement(c5);
    }

    try
    {
        CIMConstMethod cm = cc7.getMethod(0);
    }
    catch(IndexOutOfBoundsException& e)
    {
    if(verbose)
        cout << "Exception: " << e.getMessage() << endl;
    }
    // Test the findMethod and isMethod functions
    PEGASUS_TEST_ASSERT(c4.findMethod(
                CIMName ("DoesNotExist")) == PEG_NOT_FOUND);

    //Qualifier manipulation tests  (find, remove)

    PEGASUS_TEST_ASSERT(c4.findQualifier(CIMName ("qx")) == PEG_NOT_FOUND);

    PEGASUS_TEST_ASSERT(c4.findQualifier(CIMName ("q1")) == PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(c4.findQualifier(CIMName ("q2")) == PEG_NOT_FOUND);
    PEGASUS_TEST_ASSERT(c4.findQualifier(
                CIMName ("association")) == PEG_NOT_FOUND);

    posProperty = c4.findProperty(CIMName ("count"));

    try
    {
        CIMConstQualifier ccq = c4.getQualifier(c4.findQualifier
            (CIMName ("q1")));
    }
    catch (IndexOutOfBoundsException& e)
    {
        if(verbose)
            cout << "Exception: " << e.getMessage() << endl;
    }

    PEGASUS_TEST_ASSERT(c4.findProperty(CIMName ("count")) == PEG_NOT_FOUND);

    PEGASUS_TEST_ASSERT(c4.getClassName() == CIMName ("MyClass"));
    PEGASUS_TEST_ASSERT(c4.getClassName().equal(CIMName ("MyClass")));
    PEGASUS_TEST_ASSERT(c4.getClassName().equal(CIMName ("MYCLASS")));
    PEGASUS_TEST_ASSERT(c4.getClassName().equal(CIMName ("myclass")));
    PEGASUS_TEST_ASSERT(!c4.getClassName().equal(CIMName ("blob")));


    PEGASUS_TEST_ASSERT(c4.getSuperClassName() == CIMName ("YourClass"));

    // test the setSuperClassName function
    /* ATTN KS 29 April.  This test has problems.  Relook later.
      Think test, not code.
    c4.setSuperClassName(CIMName ("JunkClass"));
    PEGASUS_TEST_ASSERT(c4.getSuperClassName() == CIMName ("JunkClass"));
    c4.setSuperClassName(CIMName ("YourClass"));
    */
    PEGASUS_TEST_ASSERT(c5.getSuperClassName() == CIMName ("YourClass"));

    PEGASUS_TEST_ASSERT(c5.getQualifierCount() == 0);
    posQualifier = c5.findQualifier(CIMName ("q2"));

    // throws out of bounds
    try
    {
        CIMConstQualifier qconst1 = c5.getQualifier(posQualifier);
    }
    catch(IndexOutOfBoundsException& e)
    {
        if(verbose)
            cout << "Exception: " << e.getMessage() << endl;
    }
    if(verbose)
    {
        cout << "All tests" << endl;
    }
}
void CIMInstanceRep::resolve(
    DeclContext* context,
    const CIMNamespaceName& nameSpace,
    CIMConstClass& cimClassOut,
    Boolean propagateQualifiers)
{
    // ATTN: Verify that references are initialized.

    if (!context)
        throw NullPointer();

    //----------------------------------------------------------------------
    // First obtain the class:
    //----------------------------------------------------------------------

    CIMConstClass cimClass =
        context->lookupClass(nameSpace, _reference.getClassName());

    if (cimClass.isUninitialized())
        throw PEGASUS_CIM_EXCEPTION(CIM_ERR_INVALID_CLASS,
            _reference.getClassName().getString ());

    cimClassOut = cimClass;

    //----------------------------------------------------------------------
    // Disallow instantiation of abstract classes.
    //----------------------------------------------------------------------

    if (cimClass.isAbstract())
        throw InstantiatedAbstractClass(_reference.getClassName().getString ());

    //----------------------------------------------------------------------
    // Validate and propagate qualifiers.
    //----------------------------------------------------------------------
    _qualifiers.resolve(
        context,
        nameSpace,
        (cimClass.isAssociation()) ? CIMScope::ASSOCIATION : CIMScope::CLASS,
        false,
        cimClass._rep->_qualifiers,
        propagateQualifiers);

    //----------------------------------------------------------------------
    // First iterate the properties of this instance and verify that
    // each one is defined in the class and then resolve each one.
    //----------------------------------------------------------------------

    CIMName className = cimClass.getClassName();

    for (Uint32 i = 0, n = _properties.size(); i < n; i++)
    {
        CIMProperty& property = _properties[i];

        Uint32 index = cimClass.findProperty(property.getName());

        if (index == PEG_NOT_FOUND)
        {
            //
            //  Allow addition of Creator property to Indication Subscription,
            //  Filter and Handler instances
            //
// l10n add language property support
            if (!(((className.equal
                    (CIMName (PEGASUS_CLASSNAME_INDSUBSCRIPTION))) ||
                (className.equal
                    (CIMName (PEGASUS_CLASSNAME_FORMATTEDINDSUBSCRIPTION))) ||
                (className.equal
                    (CIMName (PEGASUS_CLASSNAME_INDHANDLER_CIMXML))) ||
                (className.equal
                    (CIMName (PEGASUS_CLASSNAME_LSTNRDST_CIMXML))) ||
                (className.equal
                    (CIMName (PEGASUS_CLASSNAME_INDHANDLER_SNMP))) ||
#ifdef  PEGASUS_ENABLE_SYSTEM_LOG_HANDLER
                (className.equal
                    (CIMName (PEGASUS_CLASSNAME_LSTNRDST_SYSTEM_LOG))) ||
#endif
#ifdef  PEGASUS_ENABLE_EMAIL_HANDLER
                (className.equal
                    (CIMName (PEGASUS_CLASSNAME_LSTNRDST_EMAIL))) ||
#endif
                (className.equal (CIMName (PEGASUS_CLASSNAME_INDFILTER)))) &&
                ((property.getName ().equal
                    (CIMName (PEGASUS_PROPERTYNAME_INDSUB_CREATOR))) ||
                (property.getName ().equal
                    (CIMName (PEGASUS_PROPERTYNAME_INDSUB_ACCEPTLANGS))) ||
                (property.getName ().equal
                    (CIMName (PEGASUS_PROPERTYNAME_INDSUB_CONTENTLANGS))))))
            {
                throw NoSuchProperty(property.getName().getString ());
            }
        }
        else
        {
            // resolve the property
            Resolver::resolveProperty (property, context, nameSpace, true,
                cimClass.getProperty (index), propagateQualifiers);
        }
    }

    //----------------------------------------------------------------------
    // Inject all properties from the class that are not included in the
    // instance. Copy over the class-origin and set the propagated flag
    // to true. NOTE: The propagated flag indicates that the property
    // was not part of the property set input with the create and
    // was inherited from the default in the class (see cimxml spec sect 3.1.5)
    //----------------------------------------------------------------------

    for (Uint32 i = 0, m = 0, n = cimClass.getPropertyCount(); i < n; i++)
    {
        CIMConstProperty property = cimClass.getProperty(i);
        const CIMName& name = property.getName();

        // See if this instance already contains a property with this name:

        Boolean found = false;

        for (Uint32 j = m, s = _properties.size(); j < s; j++)
        {
            if (name.equal(_properties[j].getName()))
            {
                found = true;
                break;
            }
        }

        if (!found)
        {
            CIMProperty p;
            if (propagateQualifiers)
            {
                p = property.clone();
            }
            else
            {
                p = CIMProperty(
                    property.getName(),
                    property.getValue(),
                    property.getArraySize(),
                    property.getReferenceClassName(),
                    property.getClassOrigin(),
                    property.getPropagated());
            }
            p.setPropagated(true);
            _properties.insert(m++, p);
        }
    }
}