unique_ptr_test.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(PluginlibUniquePtrTest, unknownPlugin) {
37  pluginlib::ClassLoader<test_base::Fubar> test_loader("pluginlib", "test_base::Fubar");
38  ASSERT_THROW(test_loader.createUniqueInstance("pluginlib/foobar"),
40 }
41 
42 
43 TEST(PluginlibUniquePtrTest, misspelledPlugin) {
44  pluginlib::ClassLoader<test_base::Fubar> bad_test_loader("pluginlib", "test_base::Fuba");
45  ASSERT_THROW(bad_test_loader.createUniqueInstance(
46  "pluginlib/foo"), pluginlib::LibraryLoadException);
47 }
48 
49 TEST(PluginlibTest, brokenPlugin) {
50  pluginlib::ClassLoader<test_base::Fubar> test_loader("pluginlib", "test_base::Fubar");
51  ASSERT_THROW(test_loader.createUniqueInstance("pluginlib/none"), pluginlib::PluginlibException);
52 }
53 
54 TEST(PluginlibUniquePtrTest, workingPlugin) {
55  pluginlib::ClassLoader<test_base::Fubar> test_loader("pluginlib", "test_base::Fubar");
56 
57  try {
58  pluginlib::UniquePtr<test_base::Fubar> foo = test_loader.createUniqueInstance("pluginlib/foo");
59  foo->initialize(10.0);
60  EXPECT_EQ(100.0, foo->result());
61  } catch (pluginlib::PluginlibException & ex) {
62  FAIL() << "Throwing exception: " << ex.what();
63  return;
64  } catch (...) {
65  FAIL() << "Uncaught exception";
66  }
67 }
68 
69 TEST(PluginlibUniquePtrTest, createUniqueInstanceAndUnloadLibrary) {
70  ROS_INFO("Making the ClassLoader...");
71  pluginlib::ClassLoader<test_base::Fubar> pl("pluginlib", "test_base::Fubar");
72 
73  ROS_INFO("Instantiating plugin...");
74  {
75  pluginlib::UniquePtr<test_base::Fubar> inst = pl.createUniqueInstance("pluginlib/foo");
76  }
77 
78  ROS_INFO("Checking if plugin is loaded with isClassLoaded...");
79  if (pl.isClassLoaded("pluginlib/foo")) {
80  ROS_INFO("Class is loaded");
81  } else {
82  FAIL() << "Library containing class should be loaded but isn't.";
83  }
84 
85  ROS_INFO("Trying to unload class with unloadLibraryForClass...");
86  try {
87  pl.unloadLibraryForClass("pluginlib/foo");
88  } catch (pluginlib::PluginlibException & e) {
89  FAIL() << "Could not unload library when I should be able to.";
90  }
91  ROS_INFO("Done.");
92 }
93 
94 // Run all the tests that were declared with TEST()
95 int main(int argc, char ** argv)
96 {
97  testing::InitGoogleTest(&argc, argv);
98  return RUN_ALL_TESTS();
99 }
TEST(PluginlibUniquePtrTest, unknownPlugin)
virtual int unloadLibraryForClass(const std::string &lookup_name)
Decrement the counter for the library containing a class with a given name.
A base class for all pluginlib exceptions that inherits from std::runtime_exception.
Definition: exceptions.hpp:43
bool isClassLoaded(const std::string &lookup_name)
Check if the library for a given class is currently loaded.
int main(int argc, char **argv)
#define ROS_INFO(...)
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:65


pluginlib
Author(s): Eitan Marder-Eppstein, Tully Foote, Dirk Thomas, Mirza Shah
autogenerated on Thu Jun 6 2019 22:03:47