CorbaNamingTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00011 /*
00012  * $Log: CorbaNamingTests.cpp,v $
00013  * Revision 1.1  2008/04/29 09:54:28  arafune
00014  * The first commitment.
00015  *
00016  *
00017  */
00018 
00019 #ifndef CorbaNaming_cpp
00020 #define CorbaNaming_cpp
00021 
00022 #include <cppunit/ui/text/TestRunner.h>
00023 #include <cppunit/TextOutputter.h>
00024 #include <cppunit/extensions/TestFactoryRegistry.h>
00025 #include <cppunit/extensions/HelperMacros.h>
00026 #include <cppunit/TestAssert.h>
00027 
00028 #include <rtm/RTC.h>
00029 #include <rtm/RTObject.h>
00030 #include <rtm/CorbaNaming.h>
00031 
00032 namespace RTC
00033 {
00034   class CorbaObjectManager
00035   {
00036   public:
00037     CorbaObjectManager(::CORBA::ORB_ptr orb, ::PortableServer::POA_ptr poa)
00038       : m_orb(orb), m_poa(poa)
00039     {
00040     }
00041 
00042     void activate(::PortableServer::ServantBase* servant)
00043     {
00044       try
00045         {
00046           m_poa->activate_object(servant);
00047         }
00048       catch(const ::PortableServer::POA::ServantAlreadyActive &)
00049         {
00050         }
00051     }
00052   protected:
00053     ::CORBA::ORB_ptr m_orb;
00054     ::PortableServer::POA_ptr m_poa;
00055   };
00056 };
00057 
00062 namespace CorbaNaming
00063 {
00064   class CorbaNamingTests
00065     : public CppUnit::TestFixture
00066   {
00067     CPPUNIT_TEST_SUITE(CorbaNamingTests);
00068     CPPUNIT_TEST(test_resolveStr);
00069     CPPUNIT_TEST(test_bindOrResolve);
00070     CPPUNIT_TEST(test_bindOrResolveContext);
00071     CPPUNIT_TEST(test_bind);
00072     CPPUNIT_TEST(test_bindByString);
00073     CPPUNIT_TEST(test_bindRecursive);
00074     CPPUNIT_TEST(test_bind_already_bound);
00075     CPPUNIT_TEST(test_rebind);
00076     CPPUNIT_TEST(test_rebindByString);
00077     CPPUNIT_TEST(tset_rebindRecursive);
00078     CPPUNIT_TEST(test_bindContext);
00079     CPPUNIT_TEST(test_bindContextRecursive);
00080     CPPUNIT_TEST(test_rebindContext);
00081     CPPUNIT_TEST(test_rebindContextRecursive);
00082     CPPUNIT_TEST(test_unbind);
00083     CPPUNIT_TEST(test_bindNewContext);
00084     CPPUNIT_TEST(test_destroyRecursive);
00085     CPPUNIT_TEST_SUITE_END();
00086         
00087   private:
00088     // helper
00089     CORBA::Object_ptr resolveRecursive(const char* fullName)
00090     {
00091       CORBA::Object_ptr context
00092         = CosNaming::NamingContext::_duplicate(m_pNaming->getRootContext());
00093                         
00094       CosNaming::Name name = m_pNaming->toName(fullName);
00095                         
00096       CORBA::ULong len(name.length());
00097       for (CORBA::ULong i = 0; i < len; ++i)
00098         {
00099           if (m_pNaming->isNamingContext(context))
00100             {
00101               CosNaming::NamingContext_var nc
00102                 = CosNaming::NamingContext::_narrow(context);
00103               CPPUNIT_ASSERT(! CORBA::is_nil(nc));
00104 
00105               CosNaming::Name subName = m_pNaming->subName(name, i, i);
00106                                 
00107               CORBA::Object_ptr resolvedObj = nc->resolve(subName);
00108                                 
00109               context = resolvedObj;
00110                                         
00111               if (i == len - 1)
00112                 {
00113                   return context;
00114                 }
00115             }
00116           else
00117             {
00118               CosNaming::NamingContext_var nc
00119                 = CosNaming::NamingContext::_narrow(context);
00120 
00121               throw CosNaming::NamingContext::CannotProceed(
00122                                                             nc, m_pNaming->subName(name, i));
00123             }
00124         }
00125     }
00126 
00127     void printName(CosNaming::Name name)
00128     {
00129       CORBA::ULong len(name.length());
00130       for (CORBA::ULong i = 0; i < len; i++)
00131         {
00132           std::cout << name[i].id << "." << name[i].kind;
00133           if (i < len - 1)
00134             {
00135               std::cout << "/";
00136             }
00137         }
00138       std::cout << std::endl;
00139     }
00140         
00141   private:
00142     CORBA::ORB_ptr m_pORB;
00143     PortableServer::POA_ptr m_pPOA;
00144     RTC::CorbaNaming* m_pNaming;
00145         
00146   public:
00150     CorbaNamingTests()
00151     {
00152     }
00153                     
00157     virtual ~CorbaNamingTests()
00158     {
00159     }
00160                   
00164     virtual void setUp()
00165     {
00166       int argc(0);
00167       char** argv(NULL);
00168                         
00169       m_pORB = CORBA::ORB_init(argc, argv);
00170       m_pPOA = PortableServer::POA::_narrow(
00171                                             m_pORB->resolve_initial_references("RootPOA"));
00172       m_pPOA->the_POAManager()->activate();
00173       m_pNaming = new RTC::CorbaNaming(m_pORB);
00174 
00175       m_pNaming->init("localhost:2809");
00176       m_pNaming->clearAll();
00177     }
00178     
00182     virtual void tearDown()
00183     {
00184       if (m_pNaming != NULL) delete m_pNaming;
00185     }
00186                 
00192     void test_resolveStr()
00193     {
00194       std::string set_name("corbaloc::localhost:2809/NameService");
00195       m_pNaming->init("localhost:2809");
00196 
00197       // ネームサーバの名前を正しく取得できるか
00198       std::string get_name(m_pNaming->getNameServer());
00199       CPPUNIT_ASSERT_EQUAL(set_name, get_name);
00200 
00201       CosNaming::Name name;
00202       name.length(1);
00203       name[0].id = "id";
00204       name[0].kind = "kind";
00205       // 与えられた NameComponent の文字列表現を正しく返すか
00206       std::string str(m_pNaming->toString(name));
00207       CPPUNIT_ASSERT("id.kind" == str);
00208 
00209 //      std::string url(m_pNaming->toUrl("", "" ));
00210 
00211       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00212       RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00213       objMgr.activate(rto);
00214       CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00215       m_pNaming->bindByString("id.kind", rto->getObjRef());
00216 
00217       // 与えられた文字列表現を resolve しオブジェクトを返すか
00218       CORBA::Object_ptr obj = m_pNaming->resolveStr("id.kind");
00219       CPPUNIT_ASSERT(obj->_is_equivalent(rto->_this()));
00220 
00221       rto->_remove_ref();
00222     }
00223                 
00229     void test_bindOrResolve()
00230     {
00231       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00232                         
00233       // バインド先となるコンテキストを登録しておく
00234       RTC::RTObject_impl* rto0 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00235       objMgr.activate(rto0);
00236       CPPUNIT_ASSERT(! CORBA::is_nil(rto0->getObjRef()));
00237 
00238       const char* fullName0 = "id0-lv0.kind0-lv0/id0-lv1.kind0-lv1";
00239       m_pNaming->bind(m_pNaming->toName(fullName0), rto0->getObjRef());
00240       CORBA::Object_ptr obj0 = resolveRecursive("id0-lv0.kind0-lv0");
00241       CosNaming::NamingContext_var nc0 = CosNaming::NamingContextExt::_narrow(obj0);
00242       CPPUNIT_ASSERT(! CORBA::is_nil(nc0));
00243                         
00244       // アクティブ化する
00245       RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00246       objMgr.activate(rto);
00247       CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00248 
00249       // オブジェクトをバインドし、それが正しく設定されるか?
00250       const char* fullName = "lv1-id.lv1-kind";
00251       CORBA::Object_ptr obj1 = m_pNaming->bindOrResolve(nc0._retn(), 
00252                                           m_pNaming->toName(fullName), rto->getObjRef());
00253       CORBA::Object_ptr obj = resolveRecursive("id0-lv0.kind0-lv0/lv1-id.lv1-kind");
00254       CPPUNIT_ASSERT(obj->_is_equivalent(rto->_this()));
00255 
00256       rto->_remove_ref();
00257       rto0->_remove_ref();
00258     }
00259                 
00265     void test_bindOrResolveContext()
00266     {
00267       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00268                         
00269       // バインド先となるコンテキストを登録しておく
00270       RTC::RTObject_impl* rto0 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00271       objMgr.activate(rto0);
00272       CPPUNIT_ASSERT(! CORBA::is_nil(rto0->getObjRef()));
00273 
00274       const char* fullName0 = "id0-lv0.kind0-lv0/id0-lv1.kind0-lv1";
00275       m_pNaming->bind(m_pNaming->toName(fullName0), rto0->getObjRef());
00276       CORBA::Object_ptr obj0 = resolveRecursive("id0-lv0.kind0-lv0");
00277       CosNaming::NamingContext_var nc0 = CosNaming::NamingContextExt::_narrow(obj0);
00278       CPPUNIT_ASSERT(! CORBA::is_nil(nc0));
00279                         
00280       // アクティブ化する
00281       RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00282       objMgr.activate(rto);
00283       CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00284 
00285       // オブジェクトをバインドし、それが正しく設定されるか?
00286       const char* fullName = "lv1-id.lv1-kind";
00287       CosNaming::NamingContext_var nc1 = m_pNaming->bindOrResolveContext(nc0._retn(), 
00288                                           m_pNaming->toName(fullName) );
00289       CPPUNIT_ASSERT(! CORBA::is_nil(nc1));
00290 
00291       CosNaming::NamingContext_var nc2 = m_pNaming->bindOrResolveContext(nc1._retn(), 
00292                                           m_pNaming->toName(fullName), m_pNaming->newContext() );
00293       CPPUNIT_ASSERT(! CORBA::is_nil(nc2));
00294 
00295       rto->_remove_ref();
00296       rto0->_remove_ref();
00297     }
00298                 
00304     void test_bind()
00305     {
00306       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00307                         
00308       // アクティブ化する
00309       RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00310       objMgr.activate(rto);
00311       CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00312 
00313       // オブジェクトをバインドし、それが正しく設定されるか?
00314       const char* fullName = "lv1-id.lv1-kind/lv2-id.lv2-kind/lv3-id.lv3-kind";
00315       m_pNaming->bind(m_pNaming->toName(fullName), rto->getObjRef());
00316       CORBA::Object_ptr obj = resolveRecursive(fullName);
00317       CPPUNIT_ASSERT(obj->_is_equivalent(rto->_this()));
00318       rto->_remove_ref();
00319     }
00320                 
00326     void test_bindByString()
00327     {
00328 
00329       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00330                         
00331       // アクティブ化する
00332       RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00333       objMgr.activate(rto);
00334       CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00335 
00336       // オブジェクトをバインドし、それが正しく設定されるか?
00337       const char* fullName = "lv1-id.lv1-kind/lv2-id.lv2-kind/lv3-id.lv3-kind";
00338       m_pNaming->bindByString(fullName, rto->getObjRef());
00339       CORBA::Object_ptr obj = resolveRecursive(fullName);
00340       CPPUNIT_ASSERT(obj->_is_equivalent(rto->_this()));
00341       rto->_remove_ref();
00342     }
00343                 
00349     void test_bindRecursive()
00350     {
00351       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00352                         
00353       // バインド先となるコンテキストを登録しておく
00354       RTC::RTObject_impl* rto0 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00355       objMgr.activate(rto0);
00356       CPPUNIT_ASSERT(! CORBA::is_nil(rto0->getObjRef()));
00357 
00358       const char* fullName0 = "id0-lv0.kind0-lv0/id0-lv1.kind0-lv1";
00359       m_pNaming->bind(m_pNaming->toName(fullName0), rto0->getObjRef());
00360 
00361       CORBA::Object_ptr obj0 = resolveRecursive("id0-lv0.kind0-lv0");
00362       CosNaming::NamingContext_var nc0 = CosNaming::NamingContextExt::_narrow(obj0);
00363       CPPUNIT_ASSERT(! CORBA::is_nil(nc0));
00364                         
00365       // アクティブ化する
00366       RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00367       objMgr.activate(rto);
00368       CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00369 
00370       // オブジェクトをバインドし、それが正しく設定されるか?
00371       const char* fullName = "lv1-id.lv1-kind/lv2-id.lv2-kind/lv3-id.lv3-kind";
00372       m_pNaming->bindRecursive(nc0._retn(), m_pNaming->toName(fullName), rto->getObjRef());
00373       CORBA::Object_ptr obj = resolveRecursive("id0-lv0.kind0-lv0/lv1-id.lv1-kind/lv2-id.lv2-kind/lv3-id.lv3-kind");
00374       CPPUNIT_ASSERT(obj->_is_equivalent(rto->_this()));
00375       rto->_remove_ref();
00376       rto0->_remove_ref();
00377     }
00378                 
00384     void test_bind_already_bound()
00385     {
00386       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00387                         
00388       // アクティブ化する
00389       RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00390       objMgr.activate(rto);
00391       CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00392 
00393       // オブジェクトをバインドし、それが正しく設定されるか?
00394       const char* fullName = "lv1-id.lv1-kind/lv2-id.lv2-kind/lv3-id.lv3-kind";
00395       m_pNaming->bind(m_pNaming->toName(fullName), rto->getObjRef());
00396       CORBA::Object_ptr obj = resolveRecursive(fullName);
00397       CPPUNIT_ASSERT(obj->_is_equivalent(rto->_this()));
00398                         
00399       // すでにバインド済みの名称を指定してバインドを試みた場合、意図どおりの例外がスローされるか?
00400       try
00401         {
00402           m_pNaming->bind(m_pNaming->toName(fullName), rto->getObjRef());
00403           CPPUNIT_FAIL("Expected exception not thrown.");
00404         }
00405       catch (CosNaming::NamingContext::AlreadyBound expected) {}
00406       rto->_remove_ref();
00407     }
00408                 
00414     void test_rebind()
00415     {
00416       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00417                         
00418       // アクティブ化する
00419       RTC::RTObject_impl* rto1 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00420       objMgr.activate(rto1);
00421       CPPUNIT_ASSERT(! CORBA::is_nil(rto1->getObjRef()));
00422       RTC::RTObject_impl* rto2 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00423       objMgr.activate(rto2);
00424       CPPUNIT_ASSERT(! CORBA::is_nil(rto2->getObjRef()));
00425 
00426       // オブジェクトをバインドし、それが正しく設定されるか?
00427       const char* fullName = "lv1-id.lv1-kind/lv2-id.lv2-kind/lv3-id.lv3-kind";
00428       m_pNaming->bind(m_pNaming->toName(fullName), rto1->getObjRef());
00429       CORBA::Object_ptr obj1 = resolveRecursive(fullName);
00430       CPPUNIT_ASSERT(obj1->_is_equivalent(rto1->_this()));
00431                         
00432       // すでにバインド済みの名称を指定してリバインドを試みた場合、新しいバインディングに置き換わるか?
00433       m_pNaming->rebind(m_pNaming->toName(fullName), rto2->getObjRef());
00434       CORBA::Object_ptr obj2 = resolveRecursive(fullName);
00435       CPPUNIT_ASSERT(obj2->_is_equivalent(rto2->_this()));
00436       rto2->_remove_ref();
00437       rto1->_remove_ref();
00438     }
00439                 
00445     void test_rebindByString()
00446     {
00447       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00448                         
00449       // アクティブ化する
00450       RTC::RTObject_impl* rto1 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00451       objMgr.activate(rto1);
00452       CPPUNIT_ASSERT(! CORBA::is_nil(rto1->getObjRef()));
00453       RTC::RTObject_impl* rto2 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00454       objMgr.activate(rto2);
00455       CPPUNIT_ASSERT(! CORBA::is_nil(rto2->getObjRef()));
00456 
00457       // オブジェクトをバインドし、それが正しく設定されるか?
00458       const char* fullName = "lv1-id.lv1-kind/lv2-id.lv2-kind/lv3-id.lv3-kind";
00459       m_pNaming->bind(m_pNaming->toName(fullName), rto1->getObjRef());
00460       CORBA::Object_ptr obj1 = resolveRecursive(fullName);
00461       CPPUNIT_ASSERT(obj1->_is_equivalent(rto1->_this()));
00462                         
00463       // すでにバインド済みの名称を指定してリバインドを試みた場合、新しいバインディングに置き換わるか?
00464       m_pNaming->rebindByString(fullName, rto2->getObjRef());
00465       CORBA::Object_ptr obj2 = resolveRecursive(fullName);
00466       CPPUNIT_ASSERT(obj2->_is_equivalent(rto2->_this()));
00467       rto2->_remove_ref();
00468       rto1->_remove_ref();
00469     }
00470                 
00476     void tset_rebindRecursive()
00477     {
00478       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00479                         
00480       // バインド先となるコンテキストを作成し、バインドしておく
00481       CosNaming::NamingContext_ptr ncParent = m_pNaming->newContext();
00482       CPPUNIT_ASSERT(! CORBA::is_nil(ncParent));
00483                         
00484       m_pNaming->bindContext("id-parent.kind-parent", ncParent);
00485       CORBA::Object_ptr objParent = resolveRecursive("id-parent.kind-parent");
00486       CPPUNIT_ASSERT(objParent->_is_equivalent(ncParent));
00487                         
00488       // テスト用にバインドするオブジェクトを作成しておく
00489       RTC::RTObject_impl* rto1 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00490       objMgr.activate(rto1);
00491       CPPUNIT_ASSERT(! CORBA::is_nil(rto1->getObjRef()));
00492                         
00493       RTC::RTObject_impl* rto2 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00494       objMgr.activate(rto2);
00495       CPPUNIT_ASSERT(! CORBA::is_nil(rto2->getObjRef()));
00496                         
00497       // 1つ目のオブジェクトをバインドする
00498       m_pNaming->bindRecursive(
00499                                ncParent, m_pNaming->toName("id-rto.kind-rto"), rto1->getObjRef());
00500       CORBA::Object_ptr objRto1 = resolveRecursive(
00501                                                    "id-parent.kind-parent/id-rto.kind-rto");
00502       CPPUNIT_ASSERT(objRto1->_is_equivalent(rto1->getObjRef()));
00503                         
00504       // 2つ目のオブジェクトを、1つ目と同じ位置に正しくリバインドできるか?
00505       m_pNaming->rebindRecursive(
00506                                  ncParent, m_pNaming->toName("id-rto.kind-rto"), rto2->getObjRef());
00507       CORBA::Object_ptr objRto2 = resolveRecursive(
00508                                                    "id-parent.kind-parent/id-rto.kind-rto");
00509       CPPUNIT_ASSERT(objRto2->_is_equivalent(rto2->getObjRef()));
00510       rto2->_remove_ref();
00511       rto1->_remove_ref();
00512     }
00513                 
00519     void test_bindContext()
00520     {
00521       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00522 
00523       // バインド先となるコンテキストを登録しておく
00524       RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00525       objMgr.activate(rto);
00526       CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00527 
00528       // テストに用いるコンテキストを準備しておく
00529       CosNaming::NamingContext_ptr nc = m_pNaming->newContext();
00530       CPPUNIT_ASSERT(! CORBA::is_nil(nc));
00531                         
00532       nc->bind(m_pNaming->toName("id.kind"), rto->getObjRef());
00533                         
00534       // コンテキストをバインドし、正しくバインドできたことを確認する
00535       m_pNaming->bindContext("id-lv0.kind-lv0/id-lv1.kind-lv1", nc);
00536                         
00537       CORBA::Object_ptr objNc = resolveRecursive("id-lv0.kind-lv0/id-lv1.kind-lv1");
00538       CPPUNIT_ASSERT(objNc->_is_equivalent(nc));
00539                         
00540       CORBA::Object_ptr objRto = resolveRecursive("id-lv0.kind-lv0/id-lv1.kind-lv1/id.kind");
00541       CPPUNIT_ASSERT(objRto->_is_equivalent(rto->getObjRef()));
00542       rto->_remove_ref();
00543     }
00544 
00550     void test_bindContextRecursive()
00551     {
00552       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00553 
00554       // バインド先となるコンテキストを作成し、あらかじめバインドしておく
00555       CosNaming::NamingContext_ptr ncParent = m_pNaming->newContext();
00556       CPPUNIT_ASSERT(! CORBA::is_nil(ncParent));
00557                         
00558       m_pNaming->bindContext("id-parent.kind-parent", ncParent);
00559       CORBA::Object_ptr objParent = resolveRecursive("id-parent.kind-parent");
00560       CPPUNIT_ASSERT(objParent->_is_equivalent(ncParent));
00561                         
00562       // バインドするコンテキストを作成しておく
00563       RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00564       objMgr.activate(rto);
00565       CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00566 
00567       CosNaming::NamingContext_ptr nc = m_pNaming->newContext();
00568       CPPUNIT_ASSERT(! CORBA::is_nil(nc));
00569       nc->bind(m_pNaming->toName("id.kind"), rto->getObjRef());
00570                         
00571       // バインド先となるコンテキストを指定して、バインド対象となるコンテキストをバインドできるか?
00572       m_pNaming->bindContextRecursive(
00573                                       ncParent, m_pNaming->toName("id-lv0.kind-lv0/id-lv1.kind-lv1"), nc);
00574                         
00575       CORBA::Object_ptr objNc = resolveRecursive(
00576                                                  "id-parent.kind-parent/id-lv0.kind-lv0/id-lv1.kind-lv1");
00577       CPPUNIT_ASSERT(objNc->_is_equivalent(nc));
00578                         
00579       CORBA::Object_ptr objRto = resolveRecursive(
00580                                                   "id-parent.kind-parent/id-lv0.kind-lv0/id-lv1.kind-lv1/id.kind");
00581       CPPUNIT_ASSERT(objRto->_is_equivalent(rto->getObjRef()));
00582       rto->_remove_ref();
00583     }
00584                 
00590     void test_rebindContext()
00591     {
00592 
00593       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00594 
00595       // テストに用いるコンテキストを準備しておく
00596       RTC::RTObject_impl* rto1 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00597       objMgr.activate(rto1);
00598       CPPUNIT_ASSERT(! CORBA::is_nil(rto1->getObjRef()));
00599                         
00600       RTC::RTObject_impl* rto2 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00601       objMgr.activate(rto2);
00602       CPPUNIT_ASSERT(! CORBA::is_nil(rto2->getObjRef()));
00603                         
00604       CosNaming::NamingContext_ptr nc1 = m_pNaming->newContext();
00605       CPPUNIT_ASSERT(! CORBA::is_nil(nc1));
00606       nc1->bind(m_pNaming->toName("id-rto.kind-rto"), rto1->getObjRef());
00607                         
00608       CosNaming::NamingContext_ptr nc2 = m_pNaming->newContext();
00609       CPPUNIT_ASSERT(! CORBA::is_nil(nc2));
00610       nc2->bind(m_pNaming->toName("id-rto.kind-rto"), rto2->getObjRef());
00611                         
00612       // 1つ目のコンテキストをバインドする
00613       m_pNaming->bindContext(m_pNaming->toName("id-nc.kind-nc"), nc1);
00614       CORBA::Object_ptr objRto1 = resolveRecursive("id-nc.kind-nc/id-rto.kind-rto");
00615       CPPUNIT_ASSERT(objRto1->_is_equivalent(rto1->getObjRef()));
00616                         
00617       // 2つ目のコンテキストを同じ位置にバインドし、正しくリバインドできることを確認する
00618       m_pNaming->rebindContext(m_pNaming->toName("id-nc.kind-nc"), nc2);
00619       CORBA::Object_ptr objRto2 = resolveRecursive("id-nc.kind-nc/id-rto.kind-rto");
00620       CPPUNIT_ASSERT(objRto2->_is_equivalent(rto2->getObjRef()));
00621       rto2->_remove_ref();
00622       rto1->_remove_ref();
00623     }
00624 
00630     void test_rebindContextRecursive()
00631     {
00632       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00633                         
00634       // バインド先となるコンテキストを作成し、バインドしておく
00635       CosNaming::NamingContext_ptr ncParent = m_pNaming->newContext();
00636       CPPUNIT_ASSERT(! CORBA::is_nil(ncParent));
00637                         
00638       m_pNaming->bindContext("id-parent.kind-parent", ncParent);
00639       CORBA::Object_ptr objParent = resolveRecursive("id-parent.kind-parent");
00640       CPPUNIT_ASSERT(objParent->_is_equivalent(ncParent));
00641                         
00642       // テストに用いるコンテキストを準備しておく
00643       RTC::RTObject_impl* rto1 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00644       objMgr.activate(rto1);
00645       CPPUNIT_ASSERT(! CORBA::is_nil(rto1->getObjRef()));
00646                         
00647       RTC::RTObject_impl* rto2 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00648       objMgr.activate(rto2);
00649       CPPUNIT_ASSERT(! CORBA::is_nil(rto2->getObjRef()));
00650                         
00651       CosNaming::NamingContext_ptr nc1 = m_pNaming->newContext();
00652       CPPUNIT_ASSERT(! CORBA::is_nil(nc1));
00653       nc1->bind(m_pNaming->toName("id-rto.kind-rto"), rto1->getObjRef());
00654                         
00655       CosNaming::NamingContext_ptr nc2 = m_pNaming->newContext();
00656       CPPUNIT_ASSERT(! CORBA::is_nil(nc2));
00657       nc2->bind(m_pNaming->toName("id-rto.kind-rto"), rto2->getObjRef());
00658                         
00659       // 1つ目のコンテキストをバインドする
00660       m_pNaming->bindContextRecursive(
00661                                       ncParent, m_pNaming->toName("id-nc.kind-nc"), nc1);
00662       CORBA::Object_ptr objRto1 = resolveRecursive(
00663                                                    "id-parent.kind-parent/id-nc.kind-nc/id-rto.kind-rto");
00664       CPPUNIT_ASSERT(objRto1->_is_equivalent(rto1->getObjRef()));
00665                         
00666       // 2つ目のコンテキストを同じ位置にバインドし、正しくリバインドできることを確認する
00667       m_pNaming->rebindContextRecursive(
00668                                         ncParent, m_pNaming->toName("id-nc.kind-nc"), nc2);
00669       CORBA::Object_ptr objRto2 = resolveRecursive(
00670                                                    "id-parent.kind-parent/id-nc.kind-nc/id-rto.kind-rto");
00671       CPPUNIT_ASSERT(objRto2->_is_equivalent(rto2->getObjRef()));
00672       rto2->_remove_ref();
00673       rto1->_remove_ref();
00674     }
00675                 
00676     void test_resolve()
00677     {
00678       // 他テスト群にて兼ねる
00679     }
00680                 
00687     void test_unbind()
00688     {
00689       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00690                         
00691       // アクティブ化する
00692       RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00693       objMgr.activate(rto);
00694       CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00695 
00696       // オブジェクトをバインドする
00697       m_pNaming->bindByString("id.kind", rto->getObjRef());
00698       CORBA::Object_ptr obj = m_pNaming->resolve("id.kind");
00699       CPPUNIT_ASSERT(obj->_is_equivalent(rto->_this()));
00700                         
00701       // オブジェクトをアンバインドする
00702       m_pNaming->unbind("id.kind");
00703                         
00704       // アンバインドしたオブジェクトの解決を試みて、意図どおりの例外がスローされるか?
00705       try
00706         {
00707           m_pNaming->resolve("id.kind");
00708           CPPUNIT_FAIL("Expected exception not thrown.");
00709         }
00710       catch (CosNaming::NamingContext::NotFound expected)
00711         {
00712           // do nothing
00713         }
00714       catch (...)
00715         {
00716           CPPUNIT_FAIL("Unexpected exception catched.");
00717         }
00718       rto->_remove_ref();
00719     }
00720                 
00727     void test_bindNewContext()
00728     {
00729       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00730                         
00731       // アクティブ化する
00732       RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00733       objMgr.activate(rto);
00734       CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00735 
00736       // 新しいコンテキストをバインドできるか?
00737       CosNaming::NamingContext_ptr nc
00738         = m_pNaming->bindNewContext(m_pNaming->toName("id-lv0.kind-lv0"));
00739       CPPUNIT_ASSERT(! CORBA::is_nil(nc));
00740                         
00741       // バインドしたコンテキストを利用してオブジェクト参照をバインドできるか?
00742       nc->bind(m_pNaming->toName("id.kind"), rto->getObjRef());
00743       CORBA::Object_ptr obj = resolveRecursive("id-lv0.kind-lv0/id.kind");
00744       CPPUNIT_ASSERT(! CORBA::is_nil(obj));
00745 
00746       rto->_remove_ref();
00747     }
00748                 
00749     void test_destroy()
00750     {
00751       // CosNaming::NamingContext::destroy()への単純な委譲なので、テスト省略
00752     }
00753                 
00754     void test_destroyRecursive()
00755     {
00756       RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00757                         
00758       // destroy対象となるコンテキストのツリーを構築する(親1:子2)
00759       // (親)
00760       CosNaming::NamingContext_ptr ncParent = m_pNaming->newContext();
00761       CPPUNIT_ASSERT(! CORBA::is_nil(ncParent));
00762                         
00763       m_pNaming->bindContext("id-parent.kind-parent", ncParent);
00764       CORBA::Object_ptr objNcParent = resolveRecursive("id-parent.kind-parent");
00765       CPPUNIT_ASSERT(objNcParent->_is_equivalent(ncParent));
00766                         
00767       // (子1)
00768       CosNaming::NamingContext_ptr nc1 = m_pNaming->newContext();
00769       CPPUNIT_ASSERT(! CORBA::is_nil(nc1));
00770                         
00771       m_pNaming->bindContextRecursive(ncParent, m_pNaming->toName("id1.kind1"), nc1);
00772       CORBA::Object_ptr objNc1 = resolveRecursive("id-parent.kind-parent/id1.kind1");
00773       CPPUNIT_ASSERT(objNc1->_is_equivalent(nc1));
00774 
00775       // (子2)
00776       CosNaming::NamingContext_ptr nc2 = m_pNaming->newContext();
00777       CPPUNIT_ASSERT(! CORBA::is_nil(nc2));
00778                         
00779       m_pNaming->bindContextRecursive(ncParent, m_pNaming->toName("id2.kind2"), nc2);
00780       CORBA::Object_ptr objNc2 = resolveRecursive("id-parent.kind-parent/id2.kind2");
00781       CPPUNIT_ASSERT(objNc2->_is_equivalent(nc2));
00782                         
00783       // destroyRecursive()を呼び出す
00784       m_pNaming->destroyRecursive(m_pNaming->getRootContext());
00785                         
00786       // 各コンテキストがdestroyされているか?
00787       // (各コンテキストへのメソッド呼出しを行い、意図どおり例外がスローされるか?)
00788       // ※CORBA実装によってはCORBA::OBJECT_NOT_EXIST例外ではないかも知れない?
00789       //   そこで、CORBA::OBJECT_NOT_EXISTでない場合も考慮してチェックしている。
00790 
00791       //new_context doesn't throw out the exception. 
00792       //Therefore, the following tests are omitted.  
00793 /*
00794       {
00795 
00796         bool thrown = false;
00797         try
00798           {
00799             ncParent->new_context();
00800           }
00801         catch (CORBA::OBJECT_NOT_EXIST expected)
00802           {
00803             // expected
00804             thrown = true;
00805           }
00806         catch (...)
00807           {
00808             // expected
00809             thrown = true;
00810           }
00811         if (! thrown) CPPUNIT_FAIL("Exception not thrown.");
00812 
00813       }
00814                         
00815       {
00816         bool thrown = false;
00817         try
00818           {
00819             nc1->new_context();
00820           }
00821         catch (CORBA::OBJECT_NOT_EXIST expected)
00822           {
00823             // expected
00824             thrown = true;
00825           }
00826         catch (...)
00827           {
00828             // expected
00829             thrown = true;
00830           }
00831         if (! thrown) CPPUNIT_FAIL("Exception not thrown.");
00832       }
00833                         
00834       {
00835         bool thrown = false;
00836         try
00837           {
00838             nc2->new_context();
00839           }
00840         catch (CORBA::OBJECT_NOT_EXIST expected)
00841           {
00842             // expected
00843             thrown = true;
00844           }
00845         catch (...)
00846           {
00847             // expected
00848             thrown = true;
00849           }
00850         if (! thrown) CPPUNIT_FAIL("Exception not thrown.");
00851       }
00852 */
00853     }
00854                 
00855   };
00856 }; // namespace CorbaNaming
00857 
00858 /*
00859  * Register test suite
00860  */
00861 CPPUNIT_TEST_SUITE_REGISTRATION(CorbaNaming::CorbaNamingTests);
00862 
00863 #ifdef LOCAL_MAIN
00864 int main(int argc, char* argv[])
00865 {
00866 
00867   FORMAT format = TEXT_OUT;
00868   int target = 0;
00869   std::string xsl;
00870   std::string ns;
00871   std::string fname;
00872   std::ofstream ofs;
00873 
00874   int i(1);
00875   while (i < argc)
00876     {
00877       std::string arg(argv[i]);
00878       std::string next_arg;
00879       if (i + 1 < argc) next_arg = argv[i + 1];
00880       else              next_arg = "";
00881 
00882       if (arg == "--text") { format = TEXT_OUT; break; }
00883       if (arg == "--xml")
00884         {
00885           if (next_arg == "")
00886             {
00887               fname = argv[0];
00888               fname += ".xml";
00889             }
00890           else
00891             {
00892               fname = next_arg;
00893             }
00894           format = XML_OUT;
00895           ofs.open(fname.c_str());
00896         }
00897       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00898       if ( arg == "--cerr"      ) { target = 1; break; }
00899       if ( arg == "--xsl"       )
00900         {
00901           if (next_arg == "") xsl = "default.xsl"; 
00902           else                xsl = next_arg;
00903         }
00904       if ( arg == "--namespace" )
00905         {
00906           if (next_arg == "")
00907             {
00908               std::cerr << "no namespace specified" << std::endl;
00909               exit(1); 
00910             }
00911           else
00912             {
00913               xsl = next_arg;
00914             }
00915         }
00916       ++i;
00917     }
00918   CppUnit::TextUi::TestRunner runner;
00919   if ( ns.empty() )
00920     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00921   else
00922     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00923   CppUnit::Outputter* outputter = 0;
00924   std::ostream* stream = target ? &std::cerr : &std::cout;
00925   switch ( format )
00926     {
00927     case TEXT_OUT :
00928       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00929       break;
00930     case XML_OUT :
00931       std::cout << "XML_OUT" << std::endl;
00932       outputter = new CppUnit::XmlOutputter(&runner.result(),
00933                                             ofs, "shift_jis");
00934       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00935       break;
00936     case COMPILER_OUT :
00937       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00938       break;
00939     }
00940   runner.setOutputter(outputter);
00941   runner.run();
00942   return 0; // runner.run() ? 0 : 1;
00943 }
00944 #endif // MAIN
00945 #endif // CorbaNaming_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:03