summaryrefslogtreecommitdiff
path: root/src/cppunit/TestFactoryRegistry.cpp
diff options
context:
space:
mode:
authorBaptiste Lepilleur <gaiacrtn@free.fr>2002-04-17 21:27:28 +0000
committerBaptiste Lepilleur <gaiacrtn@free.fr>2002-04-17 21:27:28 +0000
commit87e5cf8f526380c40f63208c6c9e785f73d327b7 (patch)
treeba1871a917dfae7667b31587ced4072d5d18657d /src/cppunit/TestFactoryRegistry.cpp
parent6c1e9ed0f8fd7339084186df71b4dfc4c98a524e (diff)
downloadcppunit-87e5cf8f526380c40f63208c6c9e785f73d327b7.tar.gz
Bumped version to 1.
bumped version to 1.9.3 * FAQ: added question about 4786 warning on VC++. * NEWS: updated. * contrib/msvc/readme.txt: moved to contrib/readme.txt. * contrib/xml-xsl/report.xsl: added XML style sheet contributed by 'cuppa' project team (http://sourceforge.jp/projects/cuppa/) * examples/cppunittest/TestResultTest.h: * examples/cppunittest/TestResultTest.cpp: added tests for startTestRun()/endTestRun(). * examples/simple/*: added. A simple example. * include/cppunit/BriefTestProgressListener.h: * src/cppunit/BriefTestProgressListener.cpp: added. Verbose progess listener that print the test name before running the test. * include/cppunit/TestListener.h: added startTestRun()/endTestRun(). * include/cppunit/TestResult.h: * src/cppunit/TestResult.cpp: added runTest(), to be called to run a test by test runner. * src/cppunit/TextTestRunner.cpp: * src/cppunit/TestRunner.cpp: updated to use TestResult::runTest(). * include/cppunit/plugin/PlugInManager.h: * src/cppunit/PlugInManager.cpp: added. Managers for all loaded plug-ins. * include/cppunit/plugin/TestPlugInDefaultImpl.h: * src/cppunit/TestPlugInDefaultImpl.cpp: renamed TestPlugInAdapter. All implementations are empty. * include/cppunit/plugin/TestPlugInSuite.h: removed. * src/cppunit/TestPlugInSuite.cpp: removed. Replaced by PlugInManager. * include/cppunit/plugin/TestPlugIn.h: rewrote the plug-in interface to provide more versatility. updated macros to match new interface. * include/cppunit/extensions/TestFactoryRegistry.h: * include/cppunit/extensions/TestFactoryRegistry.cpp: Added unregisterFactory(). Added convenience method addRegistry(). Rewrote registry life cycle management. AutoRegisterSuite can now detect that the registry has been destroy and not call to it to unregister its test factory. * include/cppunit/extensions/AutoRegisterTest.h: on destruction, the registered factory is unregistered from the registry. * include/cppunit/extensions/HelperMacros.h: added macros CPPUNIT_REGISTRY_ADD_TO_DEFAULT and CPPUNIT_REGISTRY_ADD to help build test suite hierarchy. * src/cppunit/msvc/DllPlugInTester/*: moved to src/cppunit/DllPlugInTester/. * src/cppunit/DllPlugInTester/DllPlugInTester.cpp: removed UNICODE stuffs. Use the PlugInManager instead of PlugInTestSuite. Simplified: only one test on command line, but many DLL can be specified. Added configurations to link against cppunit dll, those are now the default configuration (static linking don't make much sense for plug-in).
Diffstat (limited to 'src/cppunit/TestFactoryRegistry.cpp')
-rw-r--r--src/cppunit/TestFactoryRegistry.cpp171
1 files changed, 79 insertions, 92 deletions
diff --git a/src/cppunit/TestFactoryRegistry.cpp b/src/cppunit/TestFactoryRegistry.cpp
index d46ff29..ec10c5f 100644
--- a/src/cppunit/TestFactoryRegistry.cpp
+++ b/src/cppunit/TestFactoryRegistry.cpp
@@ -1,7 +1,7 @@
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestSuite.h>
-#include <set>
-
+#include <map>
+#include <assert.h>
#if CPPUNIT_USE_TYPEINFO_NAME
# include "cppunit/extensions/TypeInfoHelper.h"
@@ -10,86 +10,77 @@
namespace CppUnit {
-/** (Implementation) This class manages all the TestFactoryRegistry.
- *
- * Responsible for the life-cycle of the TestFactoryRegistry.
- *
- * TestFactory registry must call wasDestroyed() to indicate that
- * a given TestRegistry was destroyed, and needDestroy() to
- * know if a given TestFactory need to be destroyed (was not already
- * destroyed by another TestFactoryRegistry).
- */
-class NamedRegistries
-{
-public:
- ~NamedRegistries();
-
- static NamedRegistries &getInstance();
-
- TestFactoryRegistry &getRegistry( std::string name );
-
- void wasDestroyed( TestFactory *factory );
-
- bool needDestroy( TestFactory *factory );
+class TestFactoryRegistryList
+{
private:
typedef std::map<std::string, TestFactoryRegistry *> Registries;
Registries m_registries;
- typedef std::set<TestFactory *> Factories;
- Factories m_factoriesToDestroy;
- Factories m_destroyedFactories;
-};
-
+ enum State {
+ doNotChange =0,
+ notCreated,
+ exist,
+ destroyed
+ };
-NamedRegistries::~NamedRegistries()
-{
- Registries::iterator it = m_registries.begin();
- while ( it != m_registries.end() )
+ static State stateFlag( State newState = doNotChange )
{
- TestFactoryRegistry *registry = (it++)->second;
- if ( needDestroy( registry ) )
- delete registry;
+ static State state = notCreated;
+ if ( newState != doNotChange )
+ state = newState;
+ return state;
}
-}
-
-NamedRegistries &
-NamedRegistries::getInstance()
-{
- static NamedRegistries namedRegistries;
- return namedRegistries;
-}
+ static TestFactoryRegistryList *getInstance()
+ {
+ static TestFactoryRegistryList list;
+ return &list;
+ }
+ TestFactoryRegistry *getInternalRegistry( const std::string &name )
+ {
+ Registries::const_iterator foundIt = m_registries.find( name );
+ if ( foundIt == m_registries.end() )
+ {
+ TestFactoryRegistry *factory = new TestFactoryRegistry( name );
+ m_registries.insert( std::make_pair( name, factory ) );
+ return factory;
+ }
+ return foundIt->second;
+ }
-TestFactoryRegistry &
-NamedRegistries::getRegistry( std::string name )
-{
- Registries::const_iterator foundIt = m_registries.find( name );
- if ( foundIt == m_registries.end() )
+public:
+ TestFactoryRegistryList()
{
- TestFactoryRegistry *factory = new TestFactoryRegistry( name );
- m_registries.insert( std::make_pair( name, factory ) );
- m_factoriesToDestroy.insert( factory );
- return *factory;
+ stateFlag( exist );
}
- return *foundIt->second;
-}
+ ~TestFactoryRegistryList()
+ {
+ for ( Registries::iterator it = m_registries.begin(); it != m_registries.end(); ++it )
+ delete it->second;
-void
-NamedRegistries::wasDestroyed( TestFactory *factory )
-{
- m_factoriesToDestroy.erase( factory );
- m_destroyedFactories.insert( factory );
-}
+ stateFlag( destroyed );
+ }
+ static TestFactoryRegistry *getRegistry( const std::string &name )
+ {
+ // If the following assertion failed, then TestFactoryRegistry::getRegistry()
+ // was called during static variable destruction without checking the registry
+ // validity beforehand using TestFactoryRegistry::isValid() beforehand.
+ assert( isValid() );
+ if ( !isValid() ) // release mode
+ return NULL; // => force CRASH
+
+ return getInstance()->getInternalRegistry( name );
+ }
-bool
-NamedRegistries::needDestroy( TestFactory *factory )
-{
- return m_destroyedFactories.count( factory ) == 0;
-}
+ static bool isValid()
+ {
+ return stateFlag() != destroyed;
+ }
+};
@@ -101,33 +92,13 @@ TestFactoryRegistry::TestFactoryRegistry( std::string name ) :
TestFactoryRegistry::~TestFactoryRegistry()
{
- // The wasDestroyed() and needDestroy() is used to prevent
- // a double destruction of a factory registry.
- // registerFactory( "All Tests", getRegistry( "Unit Tests" ) );
- // => the TestFactoryRegistry "Unit Tests" is owned by both
- // the "All Tests" registry and the NamedRegistries...
- NamedRegistries::getInstance().wasDestroyed( this );
-
- for ( Factories::iterator it = m_factories.begin(); it != m_factories.end(); ++it )
- {
- TestFactory *factory = it->second;
- if ( NamedRegistries::getInstance().needDestroy( factory ) )
- delete factory;
- }
-}
-
-
-TestFactoryRegistry &
-TestFactoryRegistry::getRegistry()
-{
- return getRegistry( "All Tests" );
}
TestFactoryRegistry &
TestFactoryRegistry::getRegistry( const std::string &name )
{
- return NamedRegistries::getInstance().getRegistry( name );
+ return *TestFactoryRegistryList::getRegistry( name );
}
@@ -135,19 +106,28 @@ void
TestFactoryRegistry::registerFactory( const std::string &name,
TestFactory *factory )
{
- m_factories[name] = factory;
+ registerFactory( factory );
}
void
TestFactoryRegistry::registerFactory( TestFactory *factory )
{
- static int serialNumber = 1;
+ m_factories.insert( factory );
+}
+
+
+void
+TestFactoryRegistry::unregisterFactory( TestFactory *factory )
+{
+ m_factories.erase( factory );
+}
- OStringStream ost;
- ost << "@Dummy@" << serialNumber++;
- registerFactory( ost.str(), factory );
+void
+TestFactoryRegistry::addRegistry( const std::string &name )
+{
+ registerFactory( &getRegistry( name ) );
}
@@ -167,10 +147,17 @@ TestFactoryRegistry::addTestToSuite( TestSuite *suite )
it != m_factories.end();
++it )
{
- TestFactory *factory = (*it).second;
+ TestFactory *factory = *it;
suite->addTest( factory->makeTest() );
}
}
+bool
+TestFactoryRegistry::isValid()
+{
+ return TestFactoryRegistryList::isValid();
+}
+
+
} // namespace CppUnit