/codebase/src-cpp/test/ng6/ddm/RegisterObjectDDMTest.cpp

https://github.com/graeme-muller/portico · C++ · 389 lines · 275 code · 34 blank · 80 comment · 0 complexity · 9105ce49b3e12e5318594c79f112490c MD5 · raw file

  1. /*
  2. * Copyright 2008 The Portico Project
  3. *
  4. * This file is part of portico.
  5. *
  6. * portico is free software; you can redistribute it and/or modify
  7. * it under the terms of the Common Developer and Distribution License (CDDL)
  8. * as published by Sun Microsystems. For more information see the LICENSE file.
  9. *
  10. * Use of this software is strictly AT YOUR OWN RISK!!!
  11. * If something bad happens you do not have permission to come crying to me.
  12. * (that goes for your lawyer as well)
  13. *
  14. */
  15. #include "RegisterObjectDDMTest.h"
  16. // Register test suite with the global repository
  17. CPPUNIT_TEST_SUITE_REGISTRATION( RegisterObjectDDMTest );
  18. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RegisterObjectDDMTest, "RegisterObjectDDMTest" );
  19. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RegisterObjectDDMTest, "registerObjectWithRegion" );
  20. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RegisterObjectDDMTest, "ddm" );
  21. /////////////////////////////////////////////////////////////////////////////////////////////
  22. ////////////////////////////////// Constructors/Destructors /////////////////////////////////
  23. /////////////////////////////////////////////////////////////////////////////////////////////
  24. RegisterObjectDDMTest::RegisterObjectDDMTest()
  25. {
  26. this->defaultFederate = new TestNG6Federate( "defaultFederate" );
  27. this->federateA = new TestNG6Federate( "federateA" );
  28. this->federateB = new TestNG6Federate( "federateB" );
  29. this->federateC = new TestNG6Federate( "federateC" );
  30. }
  31. RegisterObjectDDMTest::~RegisterObjectDDMTest()
  32. {
  33. delete this->defaultFederate;
  34. delete this->federateA;
  35. delete this->federateB;
  36. delete this->federateC;
  37. }
  38. /////////////////////////////////////////////////////////////////////////////////////////////
  39. /////////////////////////////// Test Setup and Helper Methods ///////////////////////////////
  40. /////////////////////////////////////////////////////////////////////////////////////////////
  41. void RegisterObjectDDMTest::setUp()
  42. {
  43. this->defaultFederate->quickCreate();
  44. this->defaultFederate->quickJoin();
  45. this->federateA->quickJoin();
  46. this->federateB->quickJoin();
  47. this->federateC->quickJoin();
  48. // get the handle information
  49. this->aHandle = defaultFederate->quickOCHandle( "ObjectRoot.A" );
  50. this->aaHandle = defaultFederate->quickACHandle( "ObjectRoot.A", "aa" );
  51. this->abHandle = defaultFederate->quickACHandle( "ObjectRoot.A", "ab" );
  52. this->acHandle = defaultFederate->quickACHandle( "ObjectRoot.A", "ac" );
  53. this->bHandle = defaultFederate->quickOCHandle( "ObjectRoot.A.B" );
  54. this->baHandle = defaultFederate->quickACHandle( "ObjectRoot.A.B", "ba" );
  55. this->bbHandle = defaultFederate->quickACHandle( "ObjectRoot.A.B", "bb" );
  56. this->bcHandle = defaultFederate->quickACHandle( "ObjectRoot.A.B", "bc" );
  57. // create the regions
  58. this->regionOne = defaultFederate->quickCreateTestRegion( 100, 200 );
  59. this->regionTwo = defaultFederate->quickCreateTestRegion( 400, 500 );
  60. this->federateARegion = federateA->quickCreateTestRegion( 150, 250 );
  61. this->federateBRegion = federateB->quickCreateTestRegion( 450, 550 );
  62. // publish and subscribe
  63. this->defaultFederate->quickPublish( "ObjectRoot.A.B", 6, "aa", "ab", "ac", "ba", "bb", "bc" );
  64. this->federateA->quickSubscribeWithRegion( "ObjectRoot.A.B", federateARegion, 6,
  65. "aa", "ab", "ac", "ba", "bb", "bc" );
  66. this->federateB->quickSubscribeWithRegion( "ObjectRoot.A.B", federateBRegion, 6,
  67. "aa", "ab", "ac", "ba", "bb", "bc" );
  68. this->federateC->quickSubscribe( "ObjectRoot.A.B", 6, "aa", "ab", "ac", "ba", "bb", "bc" );
  69. // create the helper arrays
  70. //this->allHandles = RTI::AttributeHandle[6];
  71. this->allHandles[0] = aaHandle;
  72. this->allHandles[1] = abHandle;
  73. this->allHandles[2] = acHandle;
  74. this->allHandles[3] = baHandle;
  75. this->allHandles[4] = bbHandle;
  76. this->allHandles[5] = bcHandle;
  77. //this->regions = new RTI::Region*[6];
  78. this->regions[0] = regionOne;
  79. this->regions[1] = regionOne;
  80. this->regions[2] = regionOne;
  81. this->regions[3] = regionOne;
  82. this->regions[4] = regionOne;
  83. this->regions[5] = regionOne;
  84. }
  85. void RegisterObjectDDMTest::tearDown()
  86. {
  87. //delete [] this->allHandles(); -- auto deleted
  88. //delete [] this->regions(); -- auto deleted
  89. delete this->regionOne;
  90. delete this->regionTwo;
  91. delete this->federateARegion;
  92. delete this->federateBRegion;
  93. this->federateC->quickResign();
  94. this->federateB->quickResign();
  95. this->federateA->quickResign();
  96. this->defaultFederate->quickResign();
  97. this->defaultFederate->quickDestroy();
  98. }
  99. ////////////////////////////////////////////////////////////////////////////////////////////
  100. /////////////////////////////////////// Test Methods ///////////////////////////////////////
  101. ////////////////////////////////////////////////////////////////////////////////////////////
  102. /*
  103. * Update the object in region one and make sure only the attributes associated with that region
  104. * are updated.
  105. */
  106. void RegisterObjectDDMTest::validateRegistration( RTI::ObjectHandle theObject )
  107. {
  108. // make sure the other federates discover the object
  109. federateA->fedamb->waitForDiscoveryAs( theObject, bHandle );
  110. federateB->fedamb->waitForDiscoveryAs( theObject, bHandle );
  111. federateC->fedamb->waitForDiscoveryAs( theObject, bHandle );
  112. // update all the attributes
  113. defaultFederate->quickReflect( theObject, 6, "aa", "ab", "ac", "ba", "bb", "bc" );
  114. // make sure reflections sent to federateA
  115. TestNG6Object *ng6Object = federateA->fedamb->waitForROUpdate( theObject );
  116. int result = strcmp( "aa", ng6Object->getAttribute(aaHandle) );
  117. CPPUNIT_ASSERT_EQUAL( 0, result );
  118. result = strcmp( "ab", ng6Object->getAttribute(abHandle) );
  119. CPPUNIT_ASSERT_EQUAL( 0, result );
  120. result = strcmp( "ac", ng6Object->getAttribute(acHandle) );
  121. CPPUNIT_ASSERT_EQUAL( 0, result );
  122. result = strcmp( "ba", ng6Object->getAttribute(baHandle) );
  123. CPPUNIT_ASSERT_EQUAL( 0, result );
  124. result = strcmp( "bb", ng6Object->getAttribute(bbHandle) );
  125. CPPUNIT_ASSERT_EQUAL( 0, result );
  126. result = strcmp( "bc", ng6Object->getAttribute(bcHandle) );
  127. CPPUNIT_ASSERT_EQUAL( 0, result );
  128. // make sure reflections NOT sent to federateB
  129. federateB->fedamb->waitForROUpdateTimeout( theObject );
  130. // make sure reflections sent to federateA
  131. ng6Object = federateC->fedamb->waitForROUpdate( theObject );
  132. result = strcmp( "aa", ng6Object->getAttribute(aaHandle) );
  133. CPPUNIT_ASSERT_EQUAL( 0, result );
  134. result = strcmp( "ab", ng6Object->getAttribute(abHandle) );
  135. CPPUNIT_ASSERT_EQUAL( 0, result );
  136. result = strcmp( "ac", ng6Object->getAttribute(acHandle) );
  137. CPPUNIT_ASSERT_EQUAL( 0, result );
  138. result = strcmp( "ba", ng6Object->getAttribute(baHandle) );
  139. CPPUNIT_ASSERT_EQUAL( 0, result );
  140. result = strcmp( "bb", ng6Object->getAttribute(bbHandle) );
  141. CPPUNIT_ASSERT_EQUAL( 0, result );
  142. result = strcmp( "bc", ng6Object->getAttribute(bcHandle) );
  143. CPPUNIT_ASSERT_EQUAL( 0, result );
  144. }
  145. ////////////////////////////////////////
  146. // TEST: (valid) testRegisterObject() //
  147. ////////////////////////////////////////
  148. void RegisterObjectDDMTest::testRegisterObjectDDM()
  149. {
  150. RTI::ObjectHandle theObject = 0;
  151. try
  152. {
  153. theObject = defaultFederate->rtiamb->registerObjectInstanceWithRegion( bHandle,
  154. allHandles,
  155. regions,
  156. 6 );
  157. }
  158. catch( RTI::Exception &e )
  159. {
  160. failTest( "Unexpected exception while registering object instance with region: %s",
  161. e._reason );
  162. }
  163. // make sure it is discovered by the other federates
  164. validateRegistration( theObject );
  165. }
  166. //////////////////////////////////////////////////////
  167. // TEST: testRegisterObjectWithInvalidClassHandle() //
  168. //////////////////////////////////////////////////////
  169. void RegisterObjectDDMTest::testRegisterObjectDDMWithInvalidClassHandle()
  170. {
  171. try
  172. {
  173. defaultFederate->rtiamb->registerObjectInstanceWithRegion( (RTI::ObjectClassHandle)100000,
  174. allHandles,
  175. regions,
  176. 6 );
  177. failTestMissingException( "ObjectClassNotDefined",
  178. "registering instance with invalid class handle" );
  179. }
  180. catch( RTI::ObjectClassNotDefined& ocnd )
  181. {
  182. // success!
  183. }
  184. catch( RTI::Exception &e )
  185. {
  186. failTestWrongException( "ObjectClassNotDefined", e,
  187. "registering instance with invalid class handle" );
  188. }
  189. }
  190. ///////////////////////////////////////////////////////////
  191. // TEST: testRegisterObjectWithNonPublishedClassHandle() //
  192. ///////////////////////////////////////////////////////////
  193. void RegisterObjectDDMTest::testRegisterObjectDDMWithNonPublishedClassHandle()
  194. {
  195. try
  196. {
  197. defaultFederate->rtiamb->registerObjectInstanceWithRegion( aHandle, allHandles, regions, 6 );
  198. failTestMissingException( "ObjectClassNotPublished",
  199. "registering instance with unpublished class handle" );
  200. }
  201. catch( RTI::ObjectClassNotPublished& ocnp )
  202. {
  203. // success!
  204. }
  205. catch( RTI::Exception &e )
  206. {
  207. failTestWrongException( "ObjectClassNotPublished", e,
  208. "registering instance with unpublished class handle" );
  209. }
  210. }
  211. ////////////////////////////////////////////////////////////////////////////////////////////
  212. /////////////////////////////// Register Named Object Tests ////////////////////////////////
  213. ////////////////////////////////////////////////////////////////////////////////////////////
  214. /////////////////////////////////////////////
  215. // TEST: (valid) testRegisterNamedObject() //
  216. /////////////////////////////////////////////
  217. void RegisterObjectDDMTest::testRegisterNamedObjectDDM()
  218. {
  219. RTI::ObjectHandle theObject = 0;
  220. try
  221. {
  222. theObject = defaultFederate->rtiamb->registerObjectInstanceWithRegion( bHandle,
  223. "MyObject",
  224. allHandles,
  225. regions,
  226. 6 );
  227. }
  228. catch( RTI::Exception &e )
  229. {
  230. failTest( "Unexpected exception while registering object instance: %s", e._reason );
  231. }
  232. // make sure it is discovered by the other federates
  233. validateRegistration( theObject );
  234. }
  235. /////////////////////////////////////////////////////////
  236. // TEST: (valid) testRegisterNamedObjectWithNullName() //
  237. /////////////////////////////////////////////////////////
  238. void RegisterObjectDDMTest::testRegisterNamedObjectDDMWithNullName()
  239. {
  240. RTI::ObjectHandle theObject = 0;
  241. try
  242. {
  243. theObject = defaultFederate->rtiamb->registerObjectInstance( bHandle, NULL );
  244. }
  245. catch( RTI::Exception &e )
  246. {
  247. failTest( "Unexpected exception while registering object instance: %s", e._reason );
  248. }
  249. // make sure it is discovered by the other federates
  250. char *expectedName = new char[8];
  251. sprintf( expectedName, "HLA%lo", theObject );
  252. federateA->fedamb->waitForDiscoveryAsWithName( theObject, bHandle, expectedName );
  253. federateB->fedamb->waitForDiscoveryAsWithName( theObject, bHandle, expectedName );
  254. delete expectedName; // the waitForDiscoveryAsWithName() will delete if the test fails
  255. }
  256. ///////////////////////////////////////////////////////////////
  257. // TEST: (valid) testRegisterNamedObjectWithWhitespaceName() //
  258. ///////////////////////////////////////////////////////////////
  259. void RegisterObjectDDMTest::testRegisterNamedObjectDDMWithWhitespaceName()
  260. {
  261. RTI::ObjectHandle theObject = 0;
  262. try
  263. {
  264. theObject = defaultFederate->rtiamb->registerObjectInstance( bHandle, " " );
  265. }
  266. catch( RTI::Exception &e )
  267. {
  268. failTest( "Unexpected exception while registering object instance: %s", e._reason );
  269. }
  270. // make sure it is discovered by the other federates
  271. char *expectedName = new char[4];
  272. sprintf( expectedName, " " );
  273. federateA->fedamb->waitForDiscoveryAsWithName( theObject, bHandle, expectedName );
  274. federateB->fedamb->waitForDiscoveryAsWithName( theObject, bHandle, expectedName );
  275. delete expectedName;
  276. }
  277. ///////////////////////////////////////////////////////////
  278. // TEST: testRegisterNamedObjectWithInvalidClassHandle() //
  279. ///////////////////////////////////////////////////////////
  280. void RegisterObjectDDMTest::testRegisterNamedObjectDDMWithInvalidClassHandle()
  281. {
  282. try
  283. {
  284. defaultFederate->rtiamb->registerObjectInstanceWithRegion( (RTI::ObjectClassHandle)100000,
  285. "MyName",
  286. allHandles,
  287. regions,
  288. 6 );
  289. failTestMissingException( "ObjectClassNotDefined",
  290. "registering instance with invalid class handle" );
  291. }
  292. catch( RTI::ObjectClassNotDefined& ocnd )
  293. {
  294. // success!
  295. }
  296. catch( RTI::Exception &e )
  297. {
  298. failTestWrongException( "ObjectClassNotDefined", e,
  299. "registering instance with invalid class handle" );
  300. }
  301. }
  302. /////////////////////////////////////////////////////
  303. // TEST: testRegisterNamedObjectWithExistingName() //
  304. /////////////////////////////////////////////////////
  305. void RegisterObjectDDMTest::testRegisterNamedObjectDDMWithExistingName()
  306. {
  307. RTI::ObjectClassHandle firstObject = defaultFederate->quickRegister( bHandle, "MyObject" );
  308. federateA->fedamb->waitForDiscoveryAsWithName( firstObject, bHandle, "MyObject" );
  309. federateB->fedamb->waitForDiscoveryAsWithName( firstObject, bHandle, "MyObject" );
  310. federateC->fedamb->waitForDiscoveryAsWithName( firstObject, bHandle, "MyObject" );
  311. try
  312. {
  313. defaultFederate->rtiamb->registerObjectInstanceWithRegion( bHandle,
  314. "MyObject",
  315. allHandles,
  316. regions,
  317. 6 );
  318. failTestMissingException( "ObjectAlreadyRegistered",
  319. "registering instance with name that has already been taken" );
  320. }
  321. catch( RTI::ObjectAlreadyRegistered& ocnd )
  322. {
  323. // success!
  324. }
  325. catch( RTI::Exception &e )
  326. {
  327. failTestWrongException( "ObjectAlreadyRegistered", e,
  328. "registering instance with name that has already been taken" );
  329. }
  330. }
  331. ////////////////////////////////////////////////////////////////
  332. // TEST: testRegisterNamedObjectWithNonPublishedClassHandle() //
  333. ////////////////////////////////////////////////////////////////
  334. void RegisterObjectDDMTest::testRegisterNamedObjectDDMWithNonPublishedClassHandle()
  335. {
  336. try
  337. {
  338. defaultFederate->rtiamb->registerObjectInstanceWithRegion( aHandle,
  339. "MyObject",
  340. allHandles,
  341. regions,
  342. 6 );
  343. failTestMissingException( "ObjectClassNotPublished",
  344. "registering instance with unpublished class handle" );
  345. }
  346. catch( RTI::ObjectClassNotPublished& ocnp )
  347. {
  348. // success!
  349. }
  350. catch( RTI::Exception &e )
  351. {
  352. failTestWrongException( "ObjectClassNotPublished", e,
  353. "registering instance with unpublished class handle" );
  354. }
  355. }