From: prascle Date: Mon, 7 Nov 2005 18:28:08 +0000 (+0000) Subject: PR: some unit tests on Naming service X-Git-Tag: BR_UT_V310a2_20051115~8 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=e41ac40ea879bbd5d4c5f78f2eadfa61f533577d;p=modules%2Fkernel.git PR: some unit tests on Naming service --- diff --git a/idl/nstest.idl b/idl/nstest.idl new file mode 100644 index 000000000..9d3692355 --- /dev/null +++ b/idl/nstest.idl @@ -0,0 +1,18 @@ + +#ifndef NSTEST_IDL +#define NSTEST_IDL + +module NSTEST +{ + interface echo + { + long getId(); + }; + + interface aFactory + { + echo createInstance(); + }; +}; + +#endif \ No newline at end of file diff --git a/src/LifeCycleCORBA/Test/Makefile.in b/src/LifeCycleCORBA/Test/Makefile.in index 50253ddf5..cf8d73628 100644 --- a/src/LifeCycleCORBA/Test/Makefile.in +++ b/src/LifeCycleCORBA/Test/Makefile.in @@ -44,7 +44,7 @@ EXPORT_PYSCRIPTS = TestLifeCycleCORBA.py LIB = libLifeCycleCORBATest.la LIB_SRC = LifeCycleCORBATest.cxx LIB_CLIENT_IDL = SALOME_Component.idl SALOME_TestComponent.idl \ - SALOME_Exception.idl + SALOME_Exception.idl nstest.idl # Executables targets diff --git a/src/NamingService/Test/Makefile.in b/src/NamingService/Test/Makefile.in index e95d9f56b..36d8bcd17 100644 --- a/src/NamingService/Test/Makefile.in +++ b/src/NamingService/Test/Makefile.in @@ -43,6 +43,9 @@ EXPORT_PYSCRIPTS = TestNamingService.py LIB = libNamingServiceTest.la LIB_SRC = NamingServiceTest.cxx + +LIB_SERVER_IDL = nstest.idl + LIB_CLIENT_IDL = SALOME_Component.idl SALOME_TestComponent.idl \ SALOME_Exception.idl diff --git a/src/NamingService/Test/NamingServiceTest.cxx b/src/NamingService/Test/NamingServiceTest.cxx index fcac909c1..dd5dbdf76 100644 --- a/src/NamingService/Test/NamingServiceTest.cxx +++ b/src/NamingService/Test/NamingServiceTest.cxx @@ -7,6 +7,7 @@ #include #include #include +#include using namespace std; @@ -24,6 +25,48 @@ using namespace std; #define TRACEFILE "/tmp/traceUnitTest.log" +// ============================================================================ +/*! + * + */ +// ============================================================================ + +NSTEST_echo_i::NSTEST_echo_i() +{ + _num=-1; +} + +NSTEST_echo_i::NSTEST_echo_i(CORBA::Long num) +{ + _num=num; +} + +NSTEST_echo_i::~NSTEST_echo_i() +{ +} + +CORBA::Long NSTEST_echo_i::getId() +{ + return _num; +} + +NSTEST_aFactory_i::NSTEST_aFactory_i() +{ + _num=0; +} + +NSTEST_aFactory_i::~NSTEST_aFactory_i() +{ +} + +NSTEST::echo_ptr NSTEST_aFactory_i::createInstance() +{ + NSTEST_echo_i * anEcho = new NSTEST_echo_i(_num); + _num++; + NSTEST::echo_var anEchoRef = anEcho->_this(); + return anEchoRef._retn(); +} + // ============================================================================ /*! * Set Trace mecanism @@ -70,6 +113,18 @@ NamingServiceTest::setUp() // --- Create a SALOME_NamingService instance _NS.init_orb(_orb) ; + + // --- Create an NSTEST::factory + + CORBA::Object_var obj = _orb->resolve_initial_references("RootPOA"); + ASSERT(!CORBA::is_nil(obj)); + _root_poa = PortableServer::POA::_narrow(obj); + _pman = _root_poa->the_POAManager(); + _myFactory = new NSTEST_aFactory_i(); + _myFactoryId = _root_poa->activate_object(_myFactory); + _factoryRef = _myFactory->_this(); + _pman->activate(); + } // ============================================================================ @@ -89,7 +144,7 @@ NamingServiceTest::tearDown() // ============================================================================ /*! - * + * Test default constructor: must be followed by a call to init_orb(ORB) */ // ============================================================================ @@ -109,7 +164,7 @@ NamingServiceTest::testConstructorDefault() // ============================================================================ /*! - * + * Test constructor with ORB parameter */ // ============================================================================ @@ -125,5 +180,106 @@ NamingServiceTest::testConstructorOrb() CPPUNIT_ASSERT(!CORBA::is_nil(rootContext)); } +// ============================================================================ +/*! + * Test Register and resolve of a single CORBA object with absolute pathname, + * without subdirectories + */ +// ============================================================================ +void +NamingServiceTest::testRegisterResolveAbsNoPath() +{ + _NS.Register(_factoryRef,"/nstest_factory"); + CORBA::Object_var obj = _NS.Resolve("/nstest_factory"); + CPPUNIT_ASSERT(!CORBA::is_nil(obj)); + NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj); + CPPUNIT_ASSERT(!CORBA::is_nil(myFactory)); +} + +// ============================================================================ +/*! + * Test Register and resolve of a single CORBA object with absolute pathname, + * in a subdirectory + */ +// ============================================================================ + +void +NamingServiceTest::testRegisterResolveAbsWithPath() +{ + CORBA::Object_var obj = _NS.Resolve("/nstest_factory"); + CPPUNIT_ASSERT(!CORBA::is_nil(obj)); + NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj); + CPPUNIT_ASSERT(!CORBA::is_nil(myFactory)); + NSTEST::echo_var anEchoRef = myFactory->createInstance(); + _NS.Register(anEchoRef,"/nstest/echo_0"); + obj = _NS.Resolve("/nstest/echo_0"); + CPPUNIT_ASSERT(!CORBA::is_nil(obj)); + NSTEST::echo_var anEchoRef2 = NSTEST::echo::_narrow(obj); + CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRef2)); +} + +// ============================================================================ +/*! + * Test resolve with a name not known + */ +// ============================================================================ +void +NamingServiceTest::testResolveBadName() +{ + CORBA::Object_var obj = _NS.Resolve("/notRegisteredName"); + CPPUNIT_ASSERT(CORBA::is_nil(obj)); + obj = _NS.Resolve("/nstest/notRegisteredName"); + CPPUNIT_ASSERT(CORBA::is_nil(obj)); + obj = _NS.Resolve("/unknownPath/notRegisteredName"); + CPPUNIT_ASSERT(CORBA::is_nil(obj)); + obj = _NS.Resolve("/anUnknown/ComplicatedPath/notRegisteredName"); + CPPUNIT_ASSERT(CORBA::is_nil(obj)); +} + +// ============================================================================ +/*! + * Test register and resolve multiple objects, test resolveFirst + */ +// ============================================================================ + +#define NB_OBJS 10 + +void +NamingServiceTest::testResolveFirst() +{ + CORBA::Object_var obj = _NS.Resolve("/nstest_factory"); + CPPUNIT_ASSERT(!CORBA::is_nil(obj)); + NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj); + CPPUNIT_ASSERT(!CORBA::is_nil(myFactory)); + int ref[NB_OBJS]; + for (int i=0; icreateInstance(); + ref[i] = anEchoRef->getId(); + string name = "/nstestfirst/echo_"; + char anum[10]; + sprintf(anum,"%d",ref[i]); + name += anum; + _NS.Register(anEchoRef,name.c_str()); + } + for (int i=0; igetId() == ref[i]); + } + string name = "/nstestfirst/echo"; + obj = _NS.ResolveFirst(name.c_str()); + CPPUNIT_ASSERT(!CORBA::is_nil(obj)); + NSTEST::echo_var anEchoRef = NSTEST::echo::_narrow(obj); + CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRef)); + CPPUNIT_ASSERT(anEchoRef->getId() == ref[0]); +} diff --git a/src/NamingService/Test/NamingServiceTest.hxx b/src/NamingService/Test/NamingServiceTest.hxx index d82af640a..e44ed8ea5 100644 --- a/src/NamingService/Test/NamingServiceTest.hxx +++ b/src/NamingService/Test/NamingServiceTest.hxx @@ -5,12 +5,41 @@ #include #include "SALOME_NamingService.hxx" +#include +#include CORBA_SERVER_HEADER(nstest) + +class NSTEST_echo_i : public virtual POA_NSTEST::echo, + public virtual PortableServer::RefCountServantBase +{ +public: + NSTEST_echo_i(); + NSTEST_echo_i(CORBA::Long num); + ~NSTEST_echo_i(); + CORBA::Long getId(); +private: + int _num; +}; + +class NSTEST_aFactory_i : public virtual POA_NSTEST::aFactory, + public virtual PortableServer::RefCountServantBase +{ +public: + NSTEST_aFactory_i(); + ~NSTEST_aFactory_i(); + NSTEST::echo_ptr createInstance(); +private: + int _num; +}; + class NamingServiceTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE( NamingServiceTest ); CPPUNIT_TEST( testConstructorDefault ); CPPUNIT_TEST( testConstructorOrb ); -// CPPUNIT_TEST( ); + CPPUNIT_TEST( testRegisterResolveAbsNoPath ); + CPPUNIT_TEST( testRegisterResolveAbsWithPath ); + CPPUNIT_TEST( testResolveBadName ); + CPPUNIT_TEST( testResolveFirst ); CPPUNIT_TEST_SUITE_END(); public: @@ -20,11 +49,20 @@ public: void testConstructorDefault(); void testConstructorOrb(); -// void testFindOrLoad_Component_(); + void testRegisterResolveAbsNoPath(); + void testRegisterResolveAbsWithPath(); + void testResolveBadName(); + void testResolveFirst(); protected: CORBA::ORB_var _orb; SALOME_NamingService _NS; + + PortableServer::POA_var _root_poa; + PortableServer::POAManager_var _pman; + PortableServer::ObjectId_var _myFactoryId; + NSTEST_aFactory_i * _myFactory; + CORBA::Object_var _factoryRef; }; #endif diff --git a/src/UnitTests/Makefile.in b/src/UnitTests/Makefile.in index bdc46c93e..f7651111f 100644 --- a/src/UnitTests/Makefile.in +++ b/src/UnitTests/Makefile.in @@ -48,7 +48,7 @@ LIB_SRC = BIN = UnitTests BIN_SRC = BIN_CLIENT_IDL = SALOME_Component.idl SALOME_TestComponent.idl \ - SALOME_Exception.idl + SALOME_Exception.idl nstest.idl CXXFLAGS += @CPPUNIT_INCLUDES@ CPPFLAGS += @CPPUNIT_INCLUDES@