PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/pegasus/src/Pegasus/Common/tests/ClassDecl/ClassDecl.cpp

#
C++ | 406 lines | 263 code | 80 blank | 63 comment | 62 complexity | 3d4f9cbe0ddf565b670a82825b1923b3 MD5 | raw file
  1. //%LICENSE////////////////////////////////////////////////////////////////
  2. //
  3. // Licensed to The Open Group (TOG) under one or more contributor license
  4. // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // Each contributor licenses this file to you under the OpenPegasus Open
  7. // Source License; you may not use this file except in compliance with the
  8. // License.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a
  11. // copy of this software and associated documentation files (the "Software"),
  12. // to deal in the Software without restriction, including without limitation
  13. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  14. // and/or sell copies of the Software, and to permit persons to whom the
  15. // Software is furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included
  18. // in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  21. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  24. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. //////////////////////////////////////////////////////////////////////////
  29. //
  30. //
  31. //%/////////////////////////////////////////////////////////////////////////////
  32. #include <Pegasus/Common/PegasusAssert.h>
  33. #include <iostream>
  34. #include <Pegasus/Common/CIMClass.h>
  35. #include <Pegasus/Common/CIMName.h>
  36. #include <Pegasus/Common/XmlWriter.h>
  37. #include <Pegasus/General/MofWriter.h>
  38. // ATTN-P3-KS - 20 March 2002 - Extend exception tests.
  39. PEGASUS_USING_PEGASUS;
  40. PEGASUS_USING_STD;
  41. static Boolean verbose;
  42. void test01()
  43. {
  44. // class MyClass : YourClass
  45. // {
  46. // string message = "Hello";
  47. // }
  48. try
  49. {
  50. CIMName a = "A_class1";
  51. CIMName b = "A_class2";
  52. CIMClass c0(a, b);
  53. CIMClass c1(a, CIMName("A_class2"));
  54. CIMClass c2(CIMName("A_class1"), b);
  55. CIMClass c3(b, a);
  56. }
  57. catch (InvalidNameException & ine)
  58. {
  59. if (verbose)
  60. {
  61. cout << "Caught unexpected exception: " << ine.getMessage() << endl;
  62. }
  63. }
  64. try
  65. {
  66. //
  67. // Invalid class name
  68. //
  69. CIMClass class0(CIMName ("//localhost/root/cimv2:MyClass"),
  70. CIMName ("YourClass"));
  71. PEGASUS_TEST_ASSERT(class0.getPath() ==
  72. CIMObjectPath("//localhost/root/cimv2:MyClass"));
  73. }
  74. catch (InvalidNameException & ine)
  75. {
  76. if (verbose)
  77. {
  78. cout << "Caught expected exception: " << ine.getMessage() << endl;
  79. }
  80. }
  81. CIMClass class1(CIMName ("MyClass"), CIMName ("YourClass"));
  82. class1
  83. .addQualifier(CIMQualifier(CIMName ("association"), true))
  84. .addQualifier(CIMQualifier(CIMName ("q1"), Uint32(55)))
  85. .addQualifier(CIMQualifier(CIMName ("q2"), String("Hello")))
  86. .addProperty(CIMProperty(CIMName ("message"), String("Hello")))
  87. .addProperty(CIMProperty(CIMName ("count"), Uint32(77), 0, CIMName(),
  88. CIMName("YourClass"), true))
  89. .addMethod(CIMMethod(CIMName ("isActive"), CIMTYPE_BOOLEAN)
  90. .addParameter(CIMParameter(CIMName ("hostname"), CIMTYPE_STRING))
  91. .addParameter(CIMParameter(CIMName ("port"), CIMTYPE_UINT32)));
  92. // Test the method count function
  93. PEGASUS_TEST_ASSERT(class1.getClassName().equal(CIMName ("myclass")));
  94. PEGASUS_TEST_ASSERT(class1.getSuperClassName() == CIMName ("YourClass"));
  95. PEGASUS_TEST_ASSERT(class1.getMethodCount() ==1);
  96. // Test the findMethod and isMethod functions
  97. PEGASUS_TEST_ASSERT(class1.findMethod(
  98. CIMName ("isActive")) != PEG_NOT_FOUND);
  99. PEGASUS_TEST_ASSERT(class1.findMethod(
  100. CIMName ("DoesNotExist")) == PEG_NOT_FOUND);
  101. PEGASUS_TEST_ASSERT(class1.findMethod(
  102. CIMName ("isActive")) != PEG_NOT_FOUND);
  103. PEGASUS_TEST_ASSERT(class1.findMethod(
  104. CIMName ("DoesNotExist")) == PEG_NOT_FOUND);
  105. // Now add another method and reconfirm.
  106. class1.addMethod(CIMMethod(CIMName ("makeActive"), CIMTYPE_BOOLEAN)
  107. .addParameter(CIMParameter(CIMName ("hostname"), CIMTYPE_STRING))
  108. .addParameter(CIMParameter(CIMName ("port"), CIMTYPE_UINT32)));
  109. PEGASUS_TEST_ASSERT(class1.getMethodCount() == 2);
  110. // Test the findMethod and isMethod functions
  111. // with two methods defined
  112. PEGASUS_TEST_ASSERT(class1.findMethod(
  113. CIMName ("isActive")) != PEG_NOT_FOUND);
  114. PEGASUS_TEST_ASSERT(class1.findMethod(
  115. CIMName ("makeActive")) != PEG_NOT_FOUND);
  116. PEGASUS_TEST_ASSERT(class1.findMethod(
  117. CIMName ("DoesNotExist")) == PEG_NOT_FOUND);
  118. PEGASUS_TEST_ASSERT(class1.findMethod(
  119. CIMName ("isActive")) != PEG_NOT_FOUND);
  120. PEGASUS_TEST_ASSERT(class1.findMethod(
  121. CIMName ("makeActive")) != PEG_NOT_FOUND);
  122. PEGASUS_TEST_ASSERT(class1.findMethod(
  123. CIMName ("DoesNotExist")) == PEG_NOT_FOUND);
  124. // Test RemoveMethod function
  125. Uint32 posMethod;
  126. posMethod = class1.findMethod(CIMName ("isActive"));
  127. PEGASUS_TEST_ASSERT(posMethod != PEG_NOT_FOUND);
  128. class1.removeMethod(posMethod);
  129. PEGASUS_TEST_ASSERT(class1.findMethod(
  130. CIMName ("isActive")) == PEG_NOT_FOUND);
  131. PEGASUS_TEST_ASSERT(class1.getMethodCount() == 1);
  132. //ATTN: P3 TODO add tests for different case names
  133. //Qualifier manipulation tests (find, remove)
  134. PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q1")) != PEG_NOT_FOUND);
  135. PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q2")) != PEG_NOT_FOUND);
  136. PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("qx")) == PEG_NOT_FOUND);
  137. PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q1")) != PEG_NOT_FOUND);
  138. PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q2")) != PEG_NOT_FOUND);
  139. PEGASUS_TEST_ASSERT(class1.findQualifier(
  140. CIMName ("association")) != PEG_NOT_FOUND);
  141. PEGASUS_TEST_ASSERT(class1.isAssociation());
  142. // Remove middle Qualifier "q2"
  143. Uint32 posQualifier;
  144. posQualifier = class1.findQualifier(CIMName ("q2"));
  145. CIMConstQualifier qconst = class1.getQualifier(posQualifier);
  146. PEGASUS_TEST_ASSERT(class1.getQualifierCount() == 3);
  147. PEGASUS_TEST_ASSERT(posQualifier <= class1.getQualifierCount());
  148. class1.removeQualifier(posQualifier);
  149. PEGASUS_TEST_ASSERT(class1.getQualifierCount() == 2);
  150. PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q2")) == PEG_NOT_FOUND);
  151. PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q1")) != PEG_NOT_FOUND);
  152. PEGASUS_TEST_ASSERT(class1.isAssociation());
  153. // Remove the first parameter "q1"
  154. posQualifier = class1.findQualifier(CIMName ("q1"));
  155. PEGASUS_TEST_ASSERT(class1.getQualifierCount() == 2);
  156. CIMQualifier cq = class1.getQualifier( class1.findQualifier(
  157. CIMName ("q1")));
  158. PEGASUS_TEST_ASSERT(posQualifier <= class1.getQualifierCount());
  159. class1.removeQualifier(posQualifier);
  160. PEGASUS_TEST_ASSERT(class1.getQualifierCount() == 1);
  161. PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q1")) == PEG_NOT_FOUND);
  162. PEGASUS_TEST_ASSERT(class1.findQualifier(CIMName ("q2")) == PEG_NOT_FOUND);
  163. PEGASUS_TEST_ASSERT(class1.isAssociation());
  164. // ATTH: P3 Add tests for try block for outofbounds
  165. //The property manipulation tests.
  166. PEGASUS_TEST_ASSERT(class1.findProperty(
  167. CIMName ("count")) != PEG_NOT_FOUND);
  168. PEGASUS_TEST_ASSERT(class1.findProperty(
  169. CIMName ("message")) != PEG_NOT_FOUND);
  170. PEGASUS_TEST_ASSERT(class1.findProperty(
  171. CIMName ("isActive")) == PEG_NOT_FOUND);
  172. PEGASUS_TEST_ASSERT(class1.getPropertyCount() == 2);
  173. Uint32 posProperty;
  174. posProperty = class1.findProperty(CIMName ("count"));
  175. CIMConstProperty constprop = class1.getProperty(posProperty);
  176. PEGASUS_TEST_ASSERT(constprop.getClassOrigin() == CIMName("YourClass"));
  177. PEGASUS_TEST_ASSERT(constprop.getPropagated());
  178. class1.removeProperty(posProperty);
  179. PEGASUS_TEST_ASSERT(class1.findProperty(
  180. CIMName ("message")) != PEG_NOT_FOUND);
  181. PEGASUS_TEST_ASSERT(class1.findProperty(
  182. CIMName ("count")) == PEG_NOT_FOUND);
  183. PEGASUS_TEST_ASSERT(class1.getPropertyCount() == 1);
  184. CIMProperty cp = class1.getProperty( class1.findProperty
  185. (CIMName ("message")));
  186. PEGASUS_TEST_ASSERT(cp.getClassOrigin().isNull());
  187. PEGASUS_TEST_ASSERT(!cp.getPropagated());
  188. if(verbose)
  189. {
  190. XmlWriter::printClassElement(class1);
  191. MofWriter::printClassElement(class1);
  192. }
  193. Buffer out;
  194. MofWriter::appendClassElement(out, class1);
  195. out.clear();
  196. XmlWriter::appendClassElement(out, class1);
  197. PEGASUS_TEST_ASSERT(!class1.isAbstract());
  198. CIMName squal("q1");
  199. PEGASUS_TEST_ASSERT(class1.findQualifier(squal) == PEG_NOT_FOUND);
  200. PEGASUS_TEST_ASSERT(!class1.hasKeys());
  201. Array<CIMName> keyNames;
  202. class1.getKeyNames(keyNames);
  203. CIMClass c2(CIMName ("MyClass"));
  204. PEGASUS_TEST_ASSERT(c2.getClassName().equal(CIMName ("myclass")));
  205. // Error uninitialized handle
  206. c2.setSuperClassName(CIMName ("CIM_Element"));
  207. PEGASUS_TEST_ASSERT(c2.getSuperClassName() == CIMName ("CIM_Element"));
  208. CIMClass c3 = c2.clone();
  209. c3 = c2;
  210. try
  211. {
  212. CIMMethod cm = c2.getMethod(0);
  213. }
  214. catch(IndexOutOfBoundsException& e)
  215. {
  216. if(verbose)
  217. cout << "Exception: " << e.getMessage() << endl;
  218. }
  219. const CIMClass c4(CIMName ("MyClass"), CIMName ("YourClass"));
  220. CIMConstClass c5(CIMName ("MyClass"), CIMName ("YourClass"));
  221. CIMConstClass c6(CIMName ("MyClass"));
  222. CIMConstClass cc7(c6);
  223. CIMClass c7 = c5.clone();
  224. const CIMClass c8(class1);
  225. // Test the findMethod and isMethod functions
  226. PEGASUS_TEST_ASSERT(c7.findMethod(
  227. CIMName ("DoesNotExist")) == PEG_NOT_FOUND);
  228. PEGASUS_TEST_ASSERT(c7.findQualifier(CIMName ("dummy")) == PEG_NOT_FOUND);
  229. try
  230. {
  231. CIMConstMethod cm = c8.getMethod(0);
  232. }
  233. catch(IndexOutOfBoundsException& e)
  234. {
  235. if(verbose)
  236. cout << "Exception: " << e.getMessage() << endl;
  237. }
  238. try
  239. {
  240. CIMConstProperty ccp = c8.getProperty(c8.findProperty
  241. (CIMName ("count")));
  242. }
  243. catch(IndexOutOfBoundsException& e)
  244. {
  245. if(verbose)
  246. cout << "Exception: " << e.getMessage() << endl;
  247. }
  248. if(verbose)
  249. {
  250. XmlWriter::printClassElement(c5);
  251. }
  252. try
  253. {
  254. CIMConstMethod cm = cc7.getMethod(0);
  255. }
  256. catch(IndexOutOfBoundsException& e)
  257. {
  258. if(verbose)
  259. cout << "Exception: " << e.getMessage() << endl;
  260. }
  261. // Test the findMethod and isMethod functions
  262. PEGASUS_TEST_ASSERT(c4.findMethod(
  263. CIMName ("DoesNotExist")) == PEG_NOT_FOUND);
  264. //Qualifier manipulation tests (find, remove)
  265. PEGASUS_TEST_ASSERT(c4.findQualifier(CIMName ("qx")) == PEG_NOT_FOUND);
  266. PEGASUS_TEST_ASSERT(c4.findQualifier(CIMName ("q1")) == PEG_NOT_FOUND);
  267. PEGASUS_TEST_ASSERT(c4.findQualifier(CIMName ("q2")) == PEG_NOT_FOUND);
  268. PEGASUS_TEST_ASSERT(c4.findQualifier(
  269. CIMName ("association")) == PEG_NOT_FOUND);
  270. posProperty = c4.findProperty(CIMName ("count"));
  271. try
  272. {
  273. CIMConstQualifier ccq = c4.getQualifier(c4.findQualifier
  274. (CIMName ("q1")));
  275. }
  276. catch (IndexOutOfBoundsException& e)
  277. {
  278. if(verbose)
  279. cout << "Exception: " << e.getMessage() << endl;
  280. }
  281. PEGASUS_TEST_ASSERT(c4.findProperty(CIMName ("count")) == PEG_NOT_FOUND);
  282. PEGASUS_TEST_ASSERT(c4.getClassName() == CIMName ("MyClass"));
  283. PEGASUS_TEST_ASSERT(c4.getClassName().equal(CIMName ("MyClass")));
  284. PEGASUS_TEST_ASSERT(c4.getClassName().equal(CIMName ("MYCLASS")));
  285. PEGASUS_TEST_ASSERT(c4.getClassName().equal(CIMName ("myclass")));
  286. PEGASUS_TEST_ASSERT(!c4.getClassName().equal(CIMName ("blob")));
  287. PEGASUS_TEST_ASSERT(c4.getSuperClassName() == CIMName ("YourClass"));
  288. // test the setSuperClassName function
  289. /* ATTN KS 29 April. This test has problems. Relook later.
  290. Think test, not code.
  291. c4.setSuperClassName(CIMName ("JunkClass"));
  292. PEGASUS_TEST_ASSERT(c4.getSuperClassName() == CIMName ("JunkClass"));
  293. c4.setSuperClassName(CIMName ("YourClass"));
  294. */
  295. PEGASUS_TEST_ASSERT(c5.getSuperClassName() == CIMName ("YourClass"));
  296. PEGASUS_TEST_ASSERT(c5.getQualifierCount() == 0);
  297. posQualifier = c5.findQualifier(CIMName ("q2"));
  298. // throws out of bounds
  299. try
  300. {
  301. CIMConstQualifier qconst1 = c5.getQualifier(posQualifier);
  302. }
  303. catch(IndexOutOfBoundsException& e)
  304. {
  305. if(verbose)
  306. cout << "Exception: " << e.getMessage() << endl;
  307. }
  308. if(verbose)
  309. {
  310. cout << "All tests" << endl;
  311. }
  312. }
  313. int main(int argc, char** argv)
  314. {
  315. verbose = getenv("PEGASUS_TEST_VERBOSE") ? true : false;
  316. try
  317. {
  318. test01();
  319. }
  320. catch (Exception& e)
  321. {
  322. cout << "Exception: " << e.getMessage() << endl;
  323. }
  324. cout << argv[0] << " +++++ passed all tests" << endl;
  325. return 0;
  326. }