1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
#include <cppunit/extensions/HelperMacros.h>
#include <typeinfo>
#include "cppUnitHelper.hxx"
#include "DummyProducts.hxx"
#include "Factory.hxx" // CLAM
namespace CLAMTest
{
class FactoryRegistryTest;
CPPUNIT_TEST_SUITE_REGISTRATION( FactoryRegistryTest );
class FactoryRegistryTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( FactoryRegistryTest );
CPPUNIT_TEST( testGetCreator_WhenIsEmpty );
CPPUNIT_TEST( testGetCreatorSafe_WhenIsEmpty );
CPPUNIT_TEST( testGetCreator_WrongKeyWithASingleCreator );
CPPUNIT_TEST( testGetCreatorSafe_WrongKeyWithASingleCreator );
CPPUNIT_TEST( testGetCreator_CorrectKeyWithASingleCreator );
CPPUNIT_TEST( testGetCreatorSafe_CorrectKeyWithASingleCreator );
CPPUNIT_TEST( testGetCreator_CorrectKeyWithTwoCreators );
CPPUNIT_TEST( testGetCreatorSafe_CorrectKeyWithTwoCreators );
CPPUNIT_TEST( testAddCreator_RepeatedKey );
CPPUNIT_TEST( testAddCreatorSafe_RepeatedKey );
CPPUNIT_TEST( testRemoveCreators_WhenIsEmpty );
CPPUNIT_TEST( testRemoveCreators_WhenNotEmtpy );
CPPUNIT_TEST( testCount_WhenEmpty );
CPPUNIT_TEST( testCount_WithTwoCreators );
CPPUNIT_TEST( testGetRegisteredNames_WhenEmpty );
CPPUNIT_TEST( testGetRegisteredNames_WhenNotEmpty );
CPPUNIT_TEST_SUITE_END();
protected:
typedef CLAM::Factory<DummyProduct> MyFactoryType;
private:
// helper methods:
class FooCreator : public MyFactoryType::Creator
{
public:
DummyProduct *Create()
{
return new DummyProductFoo();
}
};
class BarCreator : public MyFactoryType::Creator
{
public:
DummyProduct *Create()
{
return new DummyProductBar();
}
};
// tests definition
void testGetCreator_WhenIsEmpty()
{
MyFactoryType::Registry reg; // an empty factory register
try {
reg.GetCreator("bla");
CPPUNIT_FAIL( "Assert was expected to happen");
} catch( CLAM::ErrAssertionFailed& expected ) {
CPPUNIT_ASSERT_EQUAL(
std::string("the Factory Registry shouldn't be empty"),
std::string( expected.what() ) );
}
}
void testGetCreatorSafe_WhenIsEmpty()
{
MyFactoryType::Registry reg; // an empty factor y register
try {
reg.GetCreatorSafe("foo");
CPPUNIT_FAIL( "it was expected to catch a CLAM::ErrFactory" );
} catch (CLAM::ErrFactory& expected) {
CPPUNIT_ASSERT_EQUAL_MESSAGE("In ErrFactory message:",
std::string("GetCreatorSafe invoked on an empty registry"),
std::string( expected.what() ) );
}
}
void testGetCreator_WrongKeyWithASingleCreator()
{
MyFactoryType::Registry reg;
// set up:
reg.AddCreator( "DummyProductFoo", new FooCreator() );
try{
reg.GetCreator("non existent key");
CPPUNIT_FAIL( "Assertion should happen" );
} catch (CLAM::ErrAssertionFailed& ) {}
}
void testGetCreatorSafe_WrongKeyWithASingleCreator()
{
MyFactoryType::Registry reg;
// set up:
reg.AddCreator( "DummyProductFoo", new FooCreator() );
try{
reg.GetCreatorSafe("non existent key");
CPPUNIT_FAIL( "ErrFactory expected" );
} catch (CLAM::ErrFactory& ) {}
}
void testGetCreator_CorrectKeyWithASingleCreator()
{
MyFactoryType::Registry reg;
// set up:
MyFactoryType::Creator* inserted;
inserted = new FooCreator();
reg.AddCreator( "DummyProductFoo", inserted);
CPPUNIT_ASSERT( inserted == ®.GetCreator("DummyProductFoo") );
}
void testGetCreatorSafe_CorrectKeyWithASingleCreator()
{
MyFactoryType::Registry reg;
// set up:
MyFactoryType::Creator* inserted;
inserted = new FooCreator();
reg.AddCreator( "DummyProductFoo", inserted);
CPPUNIT_ASSERT( inserted == ®.GetCreatorSafe("DummyProductFoo") );
}
void testGetCreator_CorrectKeyWithTwoCreators()
{
MyFactoryType::Registry reg;
// set up
MyFactoryType::Creator * fooCreator = new FooCreator();
reg.AddCreator( "DummyProductFoo", fooCreator );
reg.AddCreator( "DummyProductBar", new BarCreator() );
CPPUNIT_ASSERT( fooCreator == ®.GetCreator("DummyProductFoo") );
}
void testGetCreatorSafe_CorrectKeyWithTwoCreators()
{
MyFactoryType::Registry reg;
// set up
MyFactoryType::Creator* fooCreator = new FooCreator();
reg.AddCreator( "DummyProductFoo", fooCreator );
reg.AddCreator( "DummyProductBar", new BarCreator() );
CPPUNIT_ASSERT( fooCreator == ®.GetCreatorSafe("DummyProductFoo") );
}
void testGetCreator_WrongKeyWithTwoCreators()
{
MyFactoryType::Registry reg;
// set up
reg.AddCreator( "DummyProductFoo", new FooCreator() );
reg.AddCreator( "DummyProductBar", new BarCreator() );
try{
reg.GetCreator("wrong name");
CPPUNIT_FAIL( "Assert expected to happen" );
} catch (CLAM::ErrAssertionFailed& ) {}
}
void testGetCreatorSafe_WrongKeyWithTwoCreators()
{
MyFactoryType::Registry reg;
// set up
reg.AddCreator( "DummyProductFoo", new FooCreator() );
reg.AddCreator( "DummyProductBar", new BarCreator() );
try{
reg.GetCreatorSafe("incorrect as well");
CPPUNIT_FAIL( "CLAM::ErrFactory exptected" );
} catch (CLAM::ErrFactory&) {}
}
void testAddCreator_RepeatedKey()
{
MyFactoryType::Registry reg;
reg.AddCreator( "DummyProductFoo", new FooCreator() );
try {
reg.AddCreator( "DummyProductFoo", new BarCreator() );
CPPUNIT_FAIL( "Assert expected to happen" );
} catch (CLAM::ErrAssertionFailed& ) {}
}
void testAddCreatorSafe_RepeatedKey()
{
MyFactoryType::Registry reg;
reg.AddCreator( "DummyProductFoo", new FooCreator() );
try {
reg.AddCreatorSafe( "DummyProductFoo", new BarCreator() );
CPPUNIT_FAIL( "CLAM::ErrFactory expected" );
} catch (CLAM::ErrFactory& expected) {
CPPUNIT_ASSERT_EQUAL_MESSAGE("In ErrFactory message:",
std::string("A repeated key was passed"),
std::string( expected.what() ) );
}
}
void testRemoveCreators_WhenIsEmpty()
{
MyFactoryType::Registry reg;
reg.RemoveAllCreators();
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Count() should be 0 after RemoveAllCreators()",
std::size_t(0), reg.Count() );
}
void testRemoveCreators_WhenNotEmtpy()
{
MyFactoryType::Registry reg;
reg.AddCreator("foo", new FooCreator() );
reg.AddCreator("bar", new BarCreator() );
reg.RemoveAllCreators();
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Count() should be 0 after RemoveAllCreators()",
std::size_t(0), reg.Count() );
}
void testCount_WhenEmpty()
{
MyFactoryType::Registry reg;
CPPUNIT_ASSERT_EQUAL( std::size_t(0), reg.Count() );
}
void testCount_WithTwoCreators()
{
MyFactoryType::Registry reg;
reg.AddCreator("foo", new FooCreator() );
reg.AddCreator("bar", new BarCreator() );
CPPUNIT_ASSERT_EQUAL( std::size_t(2), reg.Count() );
}
void testGetRegisteredNames_WhenEmpty()
{
MyFactoryType::Registry reg;
std::list<std::string> namesList;
reg.GetRegisteredNames( namesList );
CPPUNIT_ASSERT_EQUAL( std::size_t(0), namesList.size() );
}
void testGetRegisteredNames_WhenNotEmpty()
{
MyFactoryType::Registry reg;
std::list<MyFactoryType::RegistryKey> namesList;
reg.AddCreator( "foo", new FooCreator() );
reg.AddCreator( "bar", new BarCreator() );
reg.GetRegisteredNames( namesList );
CPPUNIT_ASSERT_EQUAL( reg.Count(), namesList.size() );
}
};
} // namespace CLAMTest
|