1 //%/////////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
13 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
14 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
16 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //==============================================================================
25 // Author: Mike Brasher (mbrasher@bmc.com)
27 // Modified By: Sushma Fernandes (sushma_fernandes@hp.com)
28 // Carol Ann Krug Graves, Hewlett-Packard Company
29 // (carolann_graves@hp.com)
32 //%/////////////////////////////////////////////////////////////////////////////
33 /* This program tests the generation and resolution of instances. It creates
34 a set of qualifiers and a class and then creates instances of the class
35 and confirms both the creation characteristics and the resolution
40 #include <Pegasus/Common/CIMInstance.h>
41 #include <Pegasus/Common/CIMClass.h>
42 #include <Pegasus/Common/CIMName.h>
43 #include <Pegasus/Common/DeclContext.h>
44 #include <Pegasus/Common/Resolver.h>
45 #include <Pegasus/Common/XmlWriter.h>
47 PEGASUS_USING_PEGASUS;
50 static char * verbose;
54 const CIMNamespaceName NAMESPACE = CIMNamespaceName ("/zzz");
56 // Create and populate a declaration context:
58 SimpleDeclContext* context = new SimpleDeclContext;
60 context->addQualifierDecl(
61 NAMESPACE, CIMQualifierDecl(CIMName ("counter"), false,
64 context->addQualifierDecl(
65 NAMESPACE, CIMQualifierDecl(CIMName ("classcounter"), false,
69 context->addQualifierDecl(
70 NAMESPACE, CIMQualifierDecl(CIMName ("min"), String(),
73 context->addQualifierDecl(
74 NAMESPACE, CIMQualifierDecl(CIMName ("max"), String(),
77 context->addQualifierDecl(NAMESPACE,
78 CIMQualifierDecl(CIMName ("Description"), String(),
81 CIMClass class1(CIMName ("MyClass"));
84 .addProperty(CIMProperty(CIMName ("count"), Uint32(55))
85 .addQualifier(CIMQualifier(CIMName ("counter"), true))
86 .addQualifier(CIMQualifier(CIMName ("min"), String("0")))
87 .addQualifier(CIMQualifier(CIMName ("max"), String("1"))))
88 .addProperty(CIMProperty(CIMName ("message"), String("Hello"))
89 .addQualifier(CIMQualifier(CIMName ("description"),
90 String("My Message"))))
91 .addProperty(CIMProperty(CIMName ("ratio"), Real32(1.5)));
95 assert(class1.findProperty(CIMName ("count")) != PEG_NOT_FOUND);
96 assert(class1.findProperty(CIMName ("message")) != PEG_NOT_FOUND);
97 assert(class1.findProperty(CIMName ("ratio")) != PEG_NOT_FOUND);
99 Resolver::resolveClass (class1, context, NAMESPACE);
100 context->addClass(NAMESPACE, class1);
103 XmlWriter::printClassElement(class1);
106 CIMInstance instance0(CIMName ("MyClass"));
107 assert(instance0.getClassName().equal(CIMName ("MyClass")));
108 instance0.setPath(CIMObjectPath("//localhost/root/cimv2:MyClass.Foo=1"));
109 assert(instance0.getPath() == CIMObjectPath("//localhost/root/cimv2:MyClass.Foo=1"));
111 assert(instance0.getPath() == CIMObjectPath("//localhost/root/cimv2:MyClass.Foo=1"));
113 CIMInstance instance1(CIMName ("MyClass"));
114 instance1.addQualifier(CIMQualifier(CIMName ("classcounter"), true));
116 instance1.addProperty(CIMProperty(CIMName ("message"), String("Goodbye")));
118 assert(instance1.findProperty(CIMName ("message")) != PEG_NOT_FOUND);
120 assert(instance1.findProperty(CIMName ("count")) == PEG_NOT_FOUND);
121 assert(instance1.findProperty(CIMName ("ratio")) == PEG_NOT_FOUND);
122 assert(instance1.findProperty(CIMName ("nuts")) == PEG_NOT_FOUND);
123 assert(instance1.getPropertyCount() == 1);
126 XmlWriter::printInstanceElement(instance1);
128 Resolver::resolveInstance (instance1, context, NAMESPACE, true);
131 XmlWriter::printInstanceElement(instance1);
133 // Now test for properties after resolution.
135 assert(instance1.findProperty(CIMName ("message")) != PEG_NOT_FOUND);
136 assert(instance1.findProperty(CIMName ("count")) != PEG_NOT_FOUND);
137 assert(instance1.findProperty(CIMName ("ratio")) != PEG_NOT_FOUND);
138 assert(instance1.findProperty(CIMName ("nuts")) == PEG_NOT_FOUND);
140 assert(instance1.getPropertyCount() == 3);
141 // Now remove a property
144 posProperty = instance1.findProperty(CIMName ("count"));
145 instance1.removeProperty(posProperty);
147 assert(instance1.findProperty(CIMName ("message")) != PEG_NOT_FOUND);
148 assert(instance1.findProperty(CIMName ("count")) == PEG_NOT_FOUND);
149 assert(instance1.findProperty(CIMName ("ratio")) != PEG_NOT_FOUND);
150 assert(instance1.findProperty(CIMName ("nuts")) == PEG_NOT_FOUND);
152 assert(instance1.getPropertyCount() == 2);
156 CIMQualifier cq=instance1.getQualifier(instance1.findQualifier
157 (CIMName ("classcounter")));
159 const CIMInstance instance2 = instance1.clone();
160 assert(instance2.identical(instance1));
161 assert(instance1.findQualifier(CIMName ("nuts")) == PEG_NOT_FOUND);
162 assert(instance2.findQualifier(CIMName ("nuts")) == PEG_NOT_FOUND);
163 assert(instance1.getQualifierCount() != 4);
165 // Confirm that the classcounter qualifier is in instance 2
166 // NOTE: This is dangerous coding and generates an exception
167 // out of bounds error if the qualifier does not exist.
168 CIMConstQualifier ccq=
169 instance2.getQualifier(instance2.findQualifier
170 (CIMName ("classcounter")));
173 XmlWriter::printInstanceElement(instance2);
175 // Tests for CIMConstInstance
176 CIMConstInstance cinstance1(CIMName ("MyClass")), cinstance3;
177 CIMConstInstance ccopy(cinstance1);
179 cinstance1 = instance1;
180 assert(cinstance1.identical(instance1));
182 ccq = cinstance1.getQualifier(cinstance1.findQualifier(CIMName ("classcounter")));
183 assert(cinstance1.findProperty(CIMName ("message")) != PEG_NOT_FOUND);
184 CIMConstProperty ccp =
185 cinstance1.getProperty(cinstance1.findProperty(CIMName ("message")));
187 cinstance3 = cinstance1;
188 assert(cinstance3.identical(cinstance1));
190 assert(cinstance1.getClassName() == CIMName ("MyClass"));
191 assert(cinstance1.getClassName().equal(CIMName ("MyClass")));
192 assert(cinstance1.getClassName().equal(CIMName ("MYCLASS")));
193 assert(cinstance1.getClassName().equal(CIMName ("myclass")));
194 assert(!cinstance1.getClassName().equal(CIMName ("blob")));
198 assert(cinstance1.getQualifierCount() != 4);
199 assert(cinstance1.getPropertyCount() == 2);
201 CIMConstInstance cinstance2 = cinstance1.clone();
202 assert(cinstance2.identical(cinstance1));
206 XmlWriter::printInstanceElement(cinstance1);
208 cinstance1.buildPath(class1);
210 assert( !cinstance1.isUninitialized() );
217 const CIMNamespaceName NAMESPACE = CIMNamespaceName ("/zzz");
219 CIMClass cimClass(CIMName ("MyClass"));
222 .addProperty(CIMProperty(CIMName ("Last"), String())
223 .addQualifier(CIMQualifier(CIMName ("key"), true)))
224 .addProperty(CIMProperty(CIMName ("First"), String())
225 .addQualifier(CIMQualifier(CIMName ("key"), true)))
226 .addProperty(CIMProperty(CIMName ("Age"), String())
227 .addQualifier(CIMQualifier(CIMName ("key"), true)));
229 CIMInstance cimInstance(CIMName ("MyClass"));
230 cimInstance.addProperty(CIMProperty(CIMName ("first"), String("John")));
231 cimInstance.addProperty(CIMProperty(CIMName ("last"), String("Smith")));
232 cimInstance.addProperty(CIMProperty(CIMName ("age"), Uint8(101)));
235 assert(cimInstance.findProperty(CIMName ("first")) != PEG_NOT_FOUND);
236 assert(cimInstance.findProperty(CIMName ("last")) != PEG_NOT_FOUND);
237 assert(cimInstance.findProperty(CIMName ("age")) != PEG_NOT_FOUND);
239 assert(cimInstance.getPropertyCount() == 3);
242 // ATTN: Should we be doing an instance qualifier add and test
245 CIMObjectPath instanceName
246 = cimInstance.buildPath(CIMConstClass(cimClass));
248 CIMObjectPath tmp("myclass.age=101,first=\"John\",last=\"Smith\"");
250 assert(tmp.makeHashCode() == instanceName.makeHashCode());
253 // Build the instance of an association class and test the build path functions.
256 const CIMNamespaceName NAMESPACE = CIMNamespaceName ("/zzz");
258 CIMClass myPersonClass(CIMName ("MY_PersonClass"));
261 .addProperty(CIMProperty(CIMName ("name"), String())
262 .addQualifier(CIMQualifier(CIMName ("Key"), true)));
264 CIMClass myAssocClass(CIMName ("MY_AssocClass"));
267 .addQualifier(CIMQualifier(CIMName ("association"), true))
268 .addProperty(CIMProperty(CIMName ("parent"), CIMObjectPath(),0, CIMName ("MY_PersonClass"))
269 .addQualifier(CIMQualifier(CIMName ("key"), true)))
270 .addProperty(CIMProperty(CIMName ("child"), CIMObjectPath(), 0, CIMName ("MY_PersonClass"))
271 .addQualifier(CIMQualifier(CIMName ("key"), true)))
272 .addProperty(CIMProperty(CIMName ("Age"), String()));
274 CIMInstance fatherInstance(CIMName ("MY_PersonClass"));
277 .addProperty(CIMProperty(CIMName ("name"), String("father")));
279 CIMInstance daughterInstance(CIMName ("My_PersonClass"));
281 .addProperty(CIMProperty(CIMName ("name"), String("daughter")));
283 CIMObjectPath fatherInstancePath = fatherInstance.buildPath(CIMConstClass(myPersonClass));
285 CIMObjectPath daughterInstancePath = daughterInstance.buildPath(CIMConstClass(myPersonClass));
287 CIMInstance assocInstance(CIMName ("MY_AssocClass"));
289 assocInstance.addProperty(CIMProperty(CIMName ("parent"),
290 CIMObjectPath(fatherInstancePath),0,CIMName("MY_PersonClass")));
292 assocInstance.addProperty(CIMProperty(CIMName ("child"),
293 CIMObjectPath(daughterInstancePath),0, CIMName("MY_PersonClass")));
295 CIMObjectPath assocInstancePath = assocInstance.buildPath(CIMConstClass(myAssocClass));
297 // Now do the asserts, etc. See if the pathing works on Associations and association instances
301 XmlWriter::printClassElement(myPersonClass);
302 XmlWriter::printClassElement(myAssocClass);
303 XmlWriter::printInstanceElement(fatherInstance);
304 XmlWriter::printInstanceElement(daughterInstance);
305 XmlWriter::printInstanceElement(assocInstance);
309 cout << "Paths " << endl;
310 cout << "FatherInstancePath = " << fatherInstancePath.toString()<< endl;
311 cout << "DaughterInstancePath = " << daughterInstancePath.toString()<< endl;
312 cout << "AssocInstancePath = " << assocInstancePath.toString()<< endl;
314 String x ="MY_AssocClass.child=\"My_PersonClass.name=\\\"daughter\\\"\",parent=\"MY_PersonClass.name=\\\"father\\\"\"";
315 assert(x == assocInstancePath.toString());
316 CIMObjectPath px = x;
317 assert(px.identical(assocInstancePath));
321 int main(int argc, char** argv)
323 verbose = getenv("PEGASUS_TEST_VERBOSE");
332 cout << "Exception: " << e.getMessage() << endl;
336 cout << argv[0] << " +++++ passed all tests" << endl;