utest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <gtest/gtest.h>
31 
33 
34 #include "./test_base.h"
35 
36 TEST(PluginlibTest, unknownPlugin) {
37  pluginlib::ClassLoader<test_base::Fubar> test_loader("pluginlib", "test_base::Fubar");
38  ASSERT_THROW(test_loader.createInstance("pluginlib/foobar"), pluginlib::LibraryLoadException);
39 }
40 
41 TEST(PluginlibTest, misspelledPlugin) {
42  pluginlib::ClassLoader<test_base::Fubar> bad_test_loader("pluginlib", "test_base::Fuba");
43  ASSERT_THROW(bad_test_loader.createInstance("pluginlib/foo"), pluginlib::LibraryLoadException);
44 }
45 
46 TEST(PluginlibTest, invalidPackage) {
47  ASSERT_THROW(pluginlib::ClassLoader<test_base::Fubar>("pluginlib_bad",
48  "test_base::Fubar"),
50 }
51 
52 TEST(PluginlibTest, brokenPlugin) {
53  pluginlib::ClassLoader<test_base::Fubar> test_loader("pluginlib", "test_base::Fubar");
54  ASSERT_THROW(test_loader.createInstance("pluginlib/none"), pluginlib::PluginlibException);
55 }
56 
57 TEST(PluginlibTest, workingPlugin) {
58  pluginlib::ClassLoader<test_base::Fubar> test_loader("pluginlib", "test_base::Fubar");
59 
60  try {
61  boost::shared_ptr<test_base::Fubar> foo = test_loader.createInstance("pluginlib/foo");
62  foo->initialize(10.0);
63  EXPECT_EQ(100.0, foo->result());
64  } catch (pluginlib::PluginlibException & ex) {
65  FAIL() << "Throwing exception: " << ex.what();
66  return;
67  } catch (...) {
68  FAIL() << "Uncaught exception";
69  }
70 }
71 
72 TEST(PluginlibTest, createUnmanagedInstanceAndUnloadLibrary) {
73  ROS_INFO("Making the ClassLoader...");
74  pluginlib::ClassLoader<test_base::Fubar> pl("pluginlib", "test_base::Fubar");
75 
76  ROS_INFO("Instantiating plugin...");
77  test_base::Fubar * inst = pl.createUnmanagedInstance("pluginlib/foo");
78 
79  ROS_INFO("Deleting plugin...");
80  delete inst;
81 
82  ROS_INFO("Checking if plugin is loaded with isClassLoaded...");
83  if (pl.isClassLoaded("pluginlib/foo")) {
84  ROS_INFO("Class is loaded");
85  } else {
86  FAIL() << "Library containing class should be loaded but isn't.";
87  }
88  ROS_INFO("Trying to unload class with unloadLibraryForClass...");
89  try {
90  pl.unloadLibraryForClass("pluginlib/foo");
91  } catch (pluginlib::PluginlibException & e) {
92  FAIL() << "Could not unload library when I should be able to.";
93  }
94  ROS_INFO("Done.");
95 }
96 
97 TEST(PluginlibTest, createManagedInstanceAndUnloadLibrary) {
98  ROS_INFO("Making the ClassLoader...");
99  pluginlib::ClassLoader<test_base::Fubar> pl("pluginlib", "test_base::Fubar");
100 
101  ROS_INFO("Instantiating plugin...");
102  {
103  boost::shared_ptr<test_base::Fubar> inst = pl.createInstance("pluginlib/foo");
104  }
105 
106  ROS_INFO("Checking if plugin is loaded with isClassLoaded...");
107  if (pl.isClassLoaded("pluginlib/foo")) {
108  ROS_INFO("Class is loaded");
109  } else {
110  FAIL() << "Library containing class should be loaded but isn't.";
111  }
112 
113  ROS_INFO("Trying to unload class with unloadLibraryForClass...");
114  try {
115  pl.unloadLibraryForClass("pluginlib/foo");
116  } catch (pluginlib::PluginlibException & e) {
117  FAIL() << "Could not unload library when I should be able to.";
118  }
119  ROS_INFO("Done.");
120 }
121 
122 TEST(PluginlibTest, brokenXML) {
123  try {
124  pluginlib::ClassLoader<test_base::Fubar> test_loader("pluginlib", "test_base::Fubar",
125  "plugin_test");
126  test_loader.createInstance("pluginlib/foo");
127  } catch (pluginlib::PluginlibException & ex) {
128  SUCCEED();
129  return;
130  }
131 
132  ADD_FAILURE() << "Didn't throw exception as expected";
133 }
134 
135 // Run all the tests that were declared with TEST()
136 int main(int argc, char ** argv)
137 {
138  testing::InitGoogleTest(&argc, argv);
139  return RUN_ALL_TESTS();
140 }
Thrown when pluginlib is unable to instantiate a class loader.
Definition: exceptions.hpp:78
virtual int unloadLibraryForClass(const std::string &lookup_name)
Decrement the counter for the library containing a class with a given name.
TEST(PluginlibTest, unknownPlugin)
Definition: utest.cpp:36
T * createUnmanagedInstance(const std::string &lookup_name)
Create an instance of a desired class.
A base class for all pluginlib exceptions that inherits from std::runtime_exception.
Definition: exceptions.hpp:45
bool isClassLoaded(const std::string &lookup_name)
Check if the library for a given class is currently loaded.
#define ROS_INFO(...)
int main(int argc, char **argv)
Definition: utest.cpp:136
A class to help manage and load classes.
Thrown when pluginlib is unable to load the library associated with a given plugin.
Definition: exceptions.hpp:67


pluginlib
Author(s): Eitan Marder-Eppstein, Tully Foote, Dirk Thomas, Mirza Shah
autogenerated on Mon Jun 10 2019 14:15:48