1 | // |
---|---|
2 | // DynamicFactoryTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "DynamicFactoryTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/DynamicFactory.h" |
15 | #include "Poco/RefCountedObject.h" |
16 | #include "Poco/Exception.h" |
17 | #include <memory> |
18 | |
19 | |
20 | using Poco::DynamicFactory; |
21 | using Poco::RefCountedObject; |
22 | using Poco::AutoPtr; |
23 | using Poco::Instantiator; |
24 | |
25 | |
26 | namespace |
27 | { |
28 | class Base : public RefCountedObject |
29 | { |
30 | public: |
31 | Base() |
32 | { |
33 | } |
34 | |
35 | virtual ~Base() |
36 | { |
37 | } |
38 | }; |
39 | |
40 | class A: public Base |
41 | { |
42 | }; |
43 | |
44 | class B: public Base |
45 | { |
46 | }; |
47 | } |
48 | |
49 | |
50 | DynamicFactoryTest::DynamicFactoryTest(const std::string& rName): CppUnit::TestCase(rName) |
51 | { |
52 | } |
53 | |
54 | |
55 | DynamicFactoryTest::~DynamicFactoryTest() |
56 | { |
57 | } |
58 | |
59 | |
60 | void DynamicFactoryTest::testDynamicFactoryAutoPtr() |
61 | { |
62 | DynamicFactory<Base> dynFactory; |
63 | |
64 | dynFactory.registerClass<A>("A"); |
65 | dynFactory.registerClass<B>("B"); |
66 | |
67 | assertTrue (dynFactory.isClass("A")); |
68 | assertTrue (dynFactory.isClass("B")); |
69 | |
70 | assertTrue (!dynFactory.isClass("C")); |
71 | |
72 | AutoPtr<A> a(dynFactory.createInstance("A").cast<A>()); |
73 | AutoPtr<B> b(dynFactory.createInstance("B").cast<B>()); |
74 | |
75 | assertNotNull(a.get()); |
76 | assertNotNull(b.get()); |
77 | |
78 | try |
79 | { |
80 | dynFactory.registerClass<A>("A"); |
81 | fail("already registered - must throw"); |
82 | } |
83 | catch (Poco::ExistsException&) |
84 | { |
85 | } |
86 | |
87 | dynFactory.unregisterClass("B"); |
88 | assert (dynFactory.isClass("A")); |
89 | assert (!dynFactory.isClass("B")); |
90 | |
91 | try |
92 | { |
93 | AutoPtr<B> b(dynFactory.createInstance("B").cast<B>()); |
94 | fail("unregistered - must throw"); |
95 | } |
96 | catch (Poco::NotFoundException&) |
97 | { |
98 | } |
99 | } |
100 | |
101 | |
102 | void DynamicFactoryTest::testDynamicFactoryPtr() |
103 | { |
104 | DynamicFactory<Base, Base*> dynFactory; |
105 | |
106 | dynFactory.registerClass<A>("A"); |
107 | dynFactory.registerClass<B>("B"); |
108 | |
109 | assert (dynFactory.isClass("A")); |
110 | assert (dynFactory.isClass("B")); |
111 | |
112 | assert (!dynFactory.isClass("C")); |
113 | |
114 | std::unique_ptr<A> a(dynamic_cast<A*>(dynFactory.createInstance("A"))); |
115 | std::unique_ptr<B> b(dynamic_cast<B*>(dynFactory.createInstance("B"))); |
116 | |
117 | assertNotNull(a.get()); |
118 | assertNotNull(b.get()); |
119 | |
120 | try |
121 | { |
122 | dynFactory.registerClass<A>("A"); |
123 | fail("already registered - must throw"); |
124 | } |
125 | catch (Poco::ExistsException&) |
126 | { |
127 | } |
128 | |
129 | dynFactory.unregisterClass("B"); |
130 | assertTrue (dynFactory.isClass("A")); |
131 | assertTrue (!dynFactory.isClass("B")); |
132 | |
133 | try |
134 | { |
135 | std::unique_ptr<B> b(dynamic_cast<B*>(dynFactory.createInstance("B"))); |
136 | fail("unregistered - must throw"); |
137 | } |
138 | catch (Poco::NotFoundException&) |
139 | { |
140 | } |
141 | } |
142 | |
143 | |
144 | void DynamicFactoryTest::setUp() |
145 | { |
146 | } |
147 | |
148 | |
149 | void DynamicFactoryTest::tearDown() |
150 | { |
151 | } |
152 | |
153 | |
154 | CppUnit::Test* DynamicFactoryTest::suite() |
155 | { |
156 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("DynamicFactoryTest"); |
157 | |
158 | CppUnit_addTest(pSuite, DynamicFactoryTest, testDynamicFactoryPtr); |
159 | CppUnit_addTest(pSuite, DynamicFactoryTest, testDynamicFactoryAutoPtr); |
160 | |
161 | return pSuite; |
162 | } |
163 |