1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #include <Ice/Ice.h>
6 #include <TestHelper.h>
7 
8 using namespace std;
9 
10 namespace
11 {
12 
13 class Plugin : public Ice::Plugin
14 {
15 
16 public:
17 
Plugin(const Ice::CommunicatorPtr & communicator)18     Plugin(const Ice::CommunicatorPtr& communicator) :
19          _communicator(communicator),
20          _initialized(false),
21          _destroyed(false)
22     {
23     }
24 
25     void
initialize()26     initialize()
27     {
28         _initialized = true;
29     }
30 
31     void
destroy()32     destroy()
33     {
34         _destroyed = true;
35     }
36 
~Plugin()37     ~Plugin()
38     {
39         test(_initialized);
40         test(_destroyed);
41     }
42 
43 private:
44 
45     const Ice::CommunicatorPtr _communicator;
46     Ice::StringSeq _args;
47     bool _initialized;
48     bool _destroyed;
49 };
50 
51 class PluginInitializeFailExeption : public std::exception
52 {
53 
54 public:
55 
PluginInitializeFailExeption()56     PluginInitializeFailExeption() ICE_NOEXCEPT {}
57 #ifndef ICE_CPP11_COMPILER
~PluginInitializeFailExeption()58     virtual ~PluginInitializeFailExeption() throw() {}
59 #endif
what() const60     virtual const char* what() const ICE_NOEXCEPT { return "PluginInitializeFailExeption"; }
61 };
62 
63 class PluginInitializeFail : public Ice::Plugin
64 {
65 
66 public:
67 
PluginInitializeFail(const Ice::CommunicatorPtr & communicator)68     PluginInitializeFail(const Ice::CommunicatorPtr& communicator) :
69          _communicator(communicator)
70     {
71     }
72 
73     void
initialize()74     initialize()
75     {
76         throw PluginInitializeFailExeption();
77     }
78 
79     void
destroy()80     destroy()
81     {
82         test(false);
83     }
84 
85 private:
86 
87     const Ice::CommunicatorPtr _communicator;
88 };
89 
90 class BasePlugin;
91 ICE_DEFINE_PTR(BasePluginPtr, BasePlugin);
92 
93 class BasePlugin : public Ice::Plugin
94 {
95 
96 public:
97 
BasePlugin(const Ice::CommunicatorPtr & communicator)98     BasePlugin(const Ice::CommunicatorPtr& communicator) :
99          _communicator(communicator),
100          _initialized(false),
101          _destroyed(false)
102     {
103     }
104 
105     bool
isInitialized() const106     isInitialized() const
107     {
108         return _initialized;
109     }
110 
111     bool
isDestroyed() const112     isDestroyed() const
113     {
114         return _destroyed;
115     }
116 
117 protected:
118 
119     const Ice::CommunicatorPtr _communicator;
120     bool _initialized;
121     bool _destroyed;
122     BasePluginPtr _other;
123 };
124 
125 class PluginOne : public BasePlugin
126 {
127 
128 public:
129 
PluginOne(const Ice::CommunicatorPtr & communicator)130     PluginOne(const Ice::CommunicatorPtr& communicator) :
131         BasePlugin(communicator)
132     {
133     }
134 
135     void
initialize()136     initialize()
137     {
138         _other = ICE_DYNAMIC_CAST(BasePlugin, _communicator->getPluginManager()->getPlugin("PluginTwo"));
139         test(!_other->isInitialized());
140         _initialized = true;
141     }
142 
143     void
destroy()144     destroy()
145     {
146         _destroyed = true;
147         test(_other->isDestroyed());
148         _other = 0;
149     }
150 };
151 
152 class PluginTwo : public BasePlugin
153 {
154 
155 public:
156 
PluginTwo(const Ice::CommunicatorPtr & communicator)157     PluginTwo(const Ice::CommunicatorPtr& communicator) :
158          BasePlugin(communicator)
159     {
160     }
161 
162     void
initialize()163     initialize()
164     {
165         _initialized = true;
166         _other = ICE_DYNAMIC_CAST(BasePlugin, _communicator->getPluginManager()->getPlugin("PluginOne"));
167         test(_other->isInitialized());
168     }
169 
170     void
destroy()171     destroy()
172     {
173         _destroyed = true;
174         test(!_other->isDestroyed());
175         _other = 0;
176     }
177 };
178 
179 class PluginThree : public BasePlugin
180 {
181 
182 public:
183 
PluginThree(const Ice::CommunicatorPtr & communicator)184     PluginThree(const Ice::CommunicatorPtr& communicator) :
185          BasePlugin(communicator)
186     {
187     }
188 
189     void
initialize()190     initialize()
191     {
192         _initialized = true;
193         _other = ICE_DYNAMIC_CAST(BasePlugin, _communicator->getPluginManager()->getPlugin("PluginTwo"));
194         test(_other->isInitialized());
195     }
196 
197     void
destroy()198     destroy()
199     {
200         _destroyed = true;
201         test(!_other->isDestroyed());
202         _other = 0;
203     }
204 };
205 
206 class BasePluginFail;
207 ICE_DEFINE_PTR(BasePluginFailPtr, BasePluginFail);
208 
209 class BasePluginFail : public Ice::Plugin
210 {
211 
212 public:
213 
BasePluginFail(const Ice::CommunicatorPtr & communicator)214     BasePluginFail(const Ice::CommunicatorPtr& communicator) :
215          _communicator(communicator),
216          _initialized(false),
217          _destroyed(false)
218     {
219     }
220 
221     bool
isInitialized() const222     isInitialized() const
223     {
224         return _initialized;
225     }
226 
227     bool
isDestroyed() const228     isDestroyed() const
229     {
230         return _destroyed;
231     }
232 
233 protected:
234 
235     const Ice::CommunicatorPtr _communicator;
236     bool _initialized;
237     bool _destroyed;
238     BasePluginFailPtr _one;
239     BasePluginFailPtr _two;
240     BasePluginFailPtr _three;
241 };
242 
243 class PluginOneFail : public BasePluginFail
244 {
245 
246 public:
247 
PluginOneFail(const Ice::CommunicatorPtr & communicator)248     PluginOneFail(const Ice::CommunicatorPtr& communicator) :
249         BasePluginFail(communicator)
250     {
251     }
252 
253     void
initialize()254     initialize()
255     {
256         _two = ICE_DYNAMIC_CAST(BasePluginFail, _communicator->getPluginManager()->getPlugin("PluginTwoFail"));
257         test(!_two->isInitialized());
258         _three = ICE_DYNAMIC_CAST(BasePluginFail, _communicator->getPluginManager()->getPlugin("PluginThreeFail"));
259         test(!_three->isInitialized());
260         _initialized = true;
261     }
262 
263     void
destroy()264     destroy()
265     {
266         test(_two->isDestroyed());
267         //
268         // Not destroyed because initialize fails.
269         //
270         test(!_three->isDestroyed());
271         _destroyed = true;
272         _two = 0;
273         _three = 0;
274     }
275 
~PluginOneFail()276     ~PluginOneFail()
277     {
278         test(_initialized);
279         test(_destroyed);
280     }
281 };
282 
283 class PluginTwoFail : public BasePluginFail
284 {
285 
286 public:
287 
PluginTwoFail(const Ice::CommunicatorPtr & communicator)288     PluginTwoFail(const Ice::CommunicatorPtr& communicator) :
289         BasePluginFail(communicator)
290     {
291     }
292 
293     void
initialize()294     initialize()
295     {
296         _initialized = true;
297         _one = ICE_DYNAMIC_CAST(BasePluginFail, _communicator->getPluginManager()->getPlugin("PluginOneFail"));
298         test(_one->isInitialized());
299         _three = ICE_DYNAMIC_CAST(BasePluginFail, _communicator->getPluginManager()->getPlugin("PluginThreeFail"));
300         test(!_three->isInitialized());
301     }
302 
303     void
destroy()304     destroy()
305     {
306         _destroyed = true;
307         test(!_one->isDestroyed());
308         _one = 0;
309         _three = 0;
310     }
311 
~PluginTwoFail()312     ~PluginTwoFail()
313     {
314         test(_initialized);
315         test(_destroyed);
316     }
317 };
318 
319 class PluginThreeFail : public BasePluginFail
320 {
321 
322 public:
323 
PluginThreeFail(const Ice::CommunicatorPtr & communicator)324     PluginThreeFail(const Ice::CommunicatorPtr& communicator) :
325          BasePluginFail(communicator)
326     {
327     }
328 
329     void
initialize()330     initialize()
331     {
332         throw PluginInitializeFailExeption();
333     }
334 
335     void
destroy()336     destroy()
337     {
338         test(false);
339     }
340 
~PluginThreeFail()341     ~PluginThreeFail()
342     {
343         test(!_initialized);
344         test(!_destroyed);
345     }
346 };
347 
348 }
349 
350 extern "C"
351 {
352 
353 ICE_DECLSPEC_EXPORT ::Ice::Plugin*
createPlugin(const Ice::CommunicatorPtr & communicator,const string &,const Ice::StringSeq &)354 createPlugin(const Ice::CommunicatorPtr& communicator, const string&, const Ice::StringSeq&)
355 {
356     return new Plugin(communicator);
357 }
358 
359 ICE_DECLSPEC_EXPORT ::Ice::Plugin*
createPluginWithArgs(const Ice::CommunicatorPtr & communicator,const string &,const Ice::StringSeq & args)360 createPluginWithArgs(const Ice::CommunicatorPtr& communicator, const string&, const Ice::StringSeq& args)
361 {
362     test(args.size() == 3);
363     test(args[0] == "C:\\Program Files\\");
364     test(args[1] == "--DatabasePath");
365     test(args[2] == "C:\\Program Files\\Application\\db");
366     return new Plugin(communicator);
367 }
368 
369 ICE_DECLSPEC_EXPORT ::Ice::Plugin*
createPluginInitializeFail(const Ice::CommunicatorPtr & communicator,const string &,const Ice::StringSeq &)370 createPluginInitializeFail(const Ice::CommunicatorPtr& communicator, const string&, const Ice::StringSeq&)
371 {
372     return new PluginInitializeFail(communicator);
373 }
374 
375 ICE_DECLSPEC_EXPORT ::Ice::Plugin*
createPluginOne(const Ice::CommunicatorPtr & communicator,const string &,const Ice::StringSeq &)376 createPluginOne(const Ice::CommunicatorPtr& communicator, const string&, const Ice::StringSeq&)
377 {
378     return new PluginOne(communicator);
379 }
380 
381 ICE_DECLSPEC_EXPORT ::Ice::Plugin*
createPluginTwo(const Ice::CommunicatorPtr & communicator,const string &,const Ice::StringSeq &)382 createPluginTwo(const Ice::CommunicatorPtr& communicator, const string&, const Ice::StringSeq&)
383 {
384     return new PluginTwo(communicator);
385 }
386 
387 ICE_DECLSPEC_EXPORT ::Ice::Plugin*
createPluginThree(const Ice::CommunicatorPtr & communicator,const string &,const Ice::StringSeq &)388 createPluginThree(const Ice::CommunicatorPtr& communicator, const string&, const Ice::StringSeq&)
389 {
390     return new PluginThree(communicator);
391 }
392 
393 ICE_DECLSPEC_EXPORT ::Ice::Plugin*
createPluginOneFail(const Ice::CommunicatorPtr & communicator,const string &,const Ice::StringSeq &)394 createPluginOneFail(const Ice::CommunicatorPtr& communicator, const string&, const Ice::StringSeq&)
395 {
396     return new PluginOneFail(communicator);
397 }
398 
399 ICE_DECLSPEC_EXPORT ::Ice::Plugin*
createPluginTwoFail(const Ice::CommunicatorPtr & communicator,const string &,const Ice::StringSeq &)400 createPluginTwoFail(const Ice::CommunicatorPtr& communicator, const string&, const Ice::StringSeq&)
401 {
402     return new PluginTwoFail(communicator);
403 }
404 
405 ICE_DECLSPEC_EXPORT ::Ice::Plugin*
createPluginThreeFail(const Ice::CommunicatorPtr & communicator,const string &,const Ice::StringSeq &)406 createPluginThreeFail(const Ice::CommunicatorPtr& communicator, const string&, const Ice::StringSeq&)
407 {
408     return new PluginThreeFail(communicator);
409 }
410 
411 }
412