Go to the documentation of this file.00001 #include <pluginlib/class_loader.h>
00002 #include "test_base.h"
00003 #include <gtest/gtest.h>
00004
00005 TEST(PluginlibTest, unknownPlugin)
00006 {
00007 pluginlib::ClassLoader<test_base::Fubar> test_loader("pluginlib", "test_base::Fubar");
00008 test_base::Fubar* foo = NULL;
00009
00010 try
00011 {
00012 foo = test_loader.createClassInstance("pluginlib/foobar");
00013 foo->initialize(10.0);
00014 }
00015 catch(pluginlib::LibraryLoadException& ex)
00016 {
00017 SUCCEED();
00018 return;
00019 }
00020 catch(...)
00021 {
00022 FAIL() << "Uncaught exception";
00023 }
00024 ADD_FAILURE() << "Didn't throw exception as expected";
00025
00026 }
00027
00028
00029 TEST(PluginlibTest, misspelledPlugin)
00030 {
00031 pluginlib::ClassLoader<test_base::Fubar> bad_test_loader("pluginlib", "test_base::Fuba");
00032 test_base::Fubar* foo = NULL;
00033
00034 try
00035 {
00036 foo = bad_test_loader.createClassInstance("pluginlib/foo");
00037 foo->initialize(10.0);
00038 }
00039 catch(pluginlib::LibraryLoadException& ex)
00040 {
00041 SUCCEED();
00042 return;
00043 }
00044 catch(...)
00045 {
00046 FAIL() << "Uncaught exception";
00047 }
00048 ADD_FAILURE() << "Didn't throw exception as expected";
00049
00050 }
00051
00052 TEST(PluginlibTest, invalidPackage)
00053 {
00054 try
00055 {
00056 pluginlib::ClassLoader<test_base::Fubar> bad_test_loader("pluginlib_bad", "test_base::Fubar");
00057 }
00058 catch(pluginlib::LibraryLoadException& ex)
00059 {
00060 SUCCEED();
00061 return;
00062 }
00063 catch(...)
00064 {
00065 FAIL() << "Uncaught exception";
00066 }
00067 ADD_FAILURE() << "Didn't throw exception as expected";
00068
00069 }
00070
00071 TEST(PluginlibTest, brokenPlugin)
00072 {
00073 pluginlib::ClassLoader<test_base::Fubar> test_loader("pluginlib", "test_base::Fubar");
00074 test_base::Fubar* none = NULL;
00075
00076 try
00077 {
00078 none = test_loader.createClassInstance("pluginlib/none");
00079 none->initialize(10.0);
00080 }
00081 catch(pluginlib::PluginlibException& ex)
00082 {
00083 SUCCEED();
00084 return;
00085 }
00086 catch(class_loader::ClassLoaderException& ex)
00087 {
00088 FAIL() << "class_loader exception instead of pluginlib, argh. " << ex.what() << "\n";
00089 }
00090 catch(...)
00091 {
00092 FAIL() << "Uncaught exception";
00093 }
00094 ADD_FAILURE() << "Didn't throw exception as expected";
00095
00096 }
00097
00098 TEST(PluginlibTest, workingPlugin)
00099 {
00100 pluginlib::ClassLoader<test_base::Fubar> test_loader("pluginlib", "test_base::Fubar");
00101 test_base::Fubar* foo = NULL;
00102
00103 try
00104 {
00105 foo = test_loader.createClassInstance("pluginlib/foo");
00106 foo->initialize(10.0);
00107 EXPECT_EQ(foo->result(),100.0);
00108
00109 }
00110 catch(pluginlib::PluginlibException& ex)
00111 {
00112 FAIL() << "Throwing exception: " << ex.what();
00113 return;
00114 }
00115 catch(...)
00116 {
00117 FAIL() << "Uncaught exception";
00118 }
00119 }
00120
00121 TEST(PluginlibTest, createUnmanagedInstanceAndUnloadLibrary)
00122 {
00123 ROS_INFO( "Making the ClassLoader..." );
00124 pluginlib::ClassLoader<test_base::Fubar> pl("pluginlib", "test_base::Fubar");
00125
00126 ROS_INFO( "Instantiating plugin..." );
00127 test_base::Fubar *inst = pl.createUnmanagedInstance("pluginlib/foo");
00128
00129 ROS_INFO( "Deleting plugin..." );
00130 delete inst;
00131
00132 ROS_INFO( "Checking if plugin is loaded with isClassLoaded..." );
00133 if( pl.isClassLoaded( "pluginlib/foo" ) )
00134 ROS_INFO( "Class is loaded" );
00135 else
00136 {
00137 FAIL() << "Library containing class should be loaded but isn't.";
00138 }
00139 ROS_INFO( "Trying to unload class with unloadLibraryForClass..." );
00140 try
00141 {
00142 pl.unloadLibraryForClass("pluginlib/foo");
00143 }
00144 catch(pluginlib::PluginlibException& e)
00145 {
00146 FAIL() << "Could not unload library when I should be able to.";
00147 }
00148 ROS_INFO( "Done." );
00149 }
00150
00151 TEST(PluginlibTest, createManagedInstanceAndUnloadLibrary)
00152 {
00153 ROS_INFO( "Making the ClassLoader..." );
00154 pluginlib::ClassLoader<test_base::Fubar> pl("pluginlib", "test_base::Fubar");
00155
00156 ROS_INFO( "Instantiating plugin..." );
00157 {
00158 boost::shared_ptr<test_base::Fubar> inst = pl.createInstance("pluginlib/foo");
00159 }
00160
00161 ROS_INFO( "Checking if plugin is loaded with isClassLoaded..." );
00162 if( pl.isClassLoaded( "pluginlib/foo" ) )
00163 ROS_INFO( "Class is loaded" );
00164 else
00165 {
00166 FAIL() << "Library containing class should be loaded but isn't.";
00167 }
00168
00169 ROS_INFO( "Trying to unload class with unloadLibraryForClass..." );
00170 try
00171 {
00172 pl.unloadLibraryForClass("pluginlib/foo");
00173 }
00174 catch(pluginlib::PluginlibException& e)
00175 {
00176 FAIL() << "Could not unload library when I should be able to.";
00177 }
00178 ROS_INFO( "Done." );
00179 }
00180
00181
00182 int main(int argc, char **argv){
00183 testing::InitGoogleTest(&argc, argv);
00184 return RUN_ALL_TESTS();
00185 }
00186
00187