resource_locator_unit.cpp
Go to the documentation of this file.
3 #include <gtest/gtest.h>
4 #include <iostream>
5 #include <fstream>
7 
11 
12 std::size_t findSeparator(const std::string& str)
13 {
14  const size_t pos_slash = str.find('/');
15  const size_t pos_backslash = str.find('\\');
16 
17  if (pos_slash != std::string::npos && pos_backslash != std::string::npos)
18  return std::min(pos_slash, pos_backslash);
19 
20  if (pos_slash != std::string::npos)
21  return pos_slash;
22 
23  if (pos_backslash != std::string::npos)
24  return pos_backslash;
25 
26  return std::string::npos;
27 }
28 
31 {
32 public:
33  using Ptr = std::shared_ptr<TestResourceLocator>;
34  using ConstPtr = std::shared_ptr<const TestResourceLocator>;
35 
36  ~TestResourceLocator() override = default;
37 
38  tesseract_common::Resource::Ptr locateResource(const std::string& url) const override final
39  {
40  std::string mod_url = url;
41  if (url.find("package://tesseract_common") == 0)
42  {
43  mod_url.erase(0, strlen("package://tesseract_common"));
44  const size_t pos = findSeparator(mod_url);
45  if (pos == std::string::npos)
46  return nullptr;
47 
48  mod_url.erase(0, pos);
49 
50  std::filesystem::path file_path(__FILE__);
51  std::string package_path = file_path.parent_path().parent_path().string();
52 
53  if (package_path.empty())
54  return nullptr;
55 
56  mod_url = package_path + mod_url;
57  }
58 
59  if (!std::filesystem::path(mod_url).is_absolute())
60  return nullptr;
61 
62  return std::make_shared<tesseract_common::SimpleLocatedResource>(
63  url, mod_url, std::make_shared<TestResourceLocator>(*this));
64  }
65 };
66 
67 TEST(ResourceLocatorUnit, SimpleResourceLocatorUnit) // NOLINT
68 {
69  using namespace tesseract_common;
70  std::filesystem::path file_path(__FILE__);
71  std::filesystem::path package_path = file_path.parent_path().parent_path();
72 
73  ResourceLocator::Ptr locator = std::make_shared<TestResourceLocator>();
74 
75  Resource::Ptr resource = locator->locateResource("package://tesseract_common/package.xml");
76  EXPECT_TRUE(resource != nullptr);
77  EXPECT_TRUE(resource->isFile());
78  EXPECT_EQ(resource->getUrl(), "package://tesseract_common/package.xml");
79  EXPECT_EQ(std::filesystem::path(resource->getFilePath()), (package_path / "package.xml"));
80  EXPECT_FALSE(resource->getResourceContents().empty());
81  EXPECT_TRUE(resource->getResourceContentStream() != nullptr);
82 
83  const std::string separator(1, std::filesystem::path::preferred_separator);
84  Resource::Ptr sub_resource = resource->locateResource("colcon.pkg");
85  EXPECT_TRUE(sub_resource != nullptr);
86  EXPECT_TRUE(sub_resource->isFile());
87  EXPECT_EQ(sub_resource->getUrl(), "package://tesseract_common" + separator + "colcon.pkg");
88  EXPECT_EQ(std::filesystem::path(sub_resource->getFilePath()), (package_path / "colcon.pkg"));
89  EXPECT_FALSE(sub_resource->getResourceContents().empty());
90  EXPECT_TRUE(sub_resource->getResourceContentStream() != nullptr);
91 
92  tesseract_common::Resource::Ptr sub_resource_empty = sub_resource->locateResource("");
93  EXPECT_TRUE(sub_resource_empty == nullptr);
94 
95  tesseract_common::Resource::Ptr resource_empty = locator->locateResource("");
96  EXPECT_TRUE(resource_empty == nullptr);
97 
98  tesseract_common::Resource::Ptr resource_does_not_exist = locator->locateResource("package://tesseract_common/"
99  "does_not_exist.txt");
100  EXPECT_TRUE(resource_does_not_exist != nullptr);
101  EXPECT_TRUE(resource_does_not_exist->getResourceContents().empty());
102  EXPECT_TRUE(resource_does_not_exist->getResourceContentStream() == nullptr);
103 }
104 
105 TEST(ResourceLocatorUnit, GeneralResourceLocatorUnit1) // NOLINT
106 {
107  using namespace tesseract_common;
108  std::filesystem::path file_path(__FILE__);
109  std::filesystem::path package_path = file_path.parent_path().parent_path();
110 
111 #ifndef _WIN32
112  std::string env_var = "TESSERACT_RESOURCE_PATH=" + package_path.string();
113 #else
114  std::string env_var = "TESSERACT_RESOURCE_PATH=" + package_path.string();
115 #endif
116  putenv(env_var.data());
117 
118  ResourceLocator::Ptr locator = std::make_shared<GeneralResourceLocator>();
119 
120  Resource::Ptr resource = locator->locateResource("package://tesseract_common/package.xml");
121  EXPECT_TRUE(resource != nullptr);
122  EXPECT_TRUE(resource->isFile());
123  EXPECT_EQ(resource->getUrl(), "package://tesseract_common/package.xml");
124  EXPECT_EQ(std::filesystem::path(resource->getFilePath()), (package_path / "package.xml"));
125  EXPECT_FALSE(resource->getResourceContents().empty());
126  EXPECT_TRUE(resource->getResourceContentStream() != nullptr);
127 
128  const std::string separator(1, std::filesystem::path::preferred_separator);
129  Resource::Ptr sub_resource = resource->locateResource("colcon.pkg");
130  EXPECT_TRUE(sub_resource != nullptr);
131  EXPECT_TRUE(sub_resource->isFile());
132  EXPECT_EQ(sub_resource->getUrl(), "package://tesseract_common" + separator + "colcon.pkg");
133  EXPECT_EQ(std::filesystem::path(sub_resource->getFilePath()), (package_path / "colcon.pkg"));
134  EXPECT_FALSE(sub_resource->getResourceContents().empty());
135  EXPECT_TRUE(sub_resource->getResourceContentStream() != nullptr);
136 
137  tesseract_common::Resource::Ptr sub_resource_empty = sub_resource->locateResource("");
138  EXPECT_TRUE(sub_resource_empty == nullptr);
139 
140  tesseract_common::Resource::Ptr resource_empty = locator->locateResource("");
141  EXPECT_TRUE(resource_empty == nullptr);
142 
143  tesseract_common::Resource::Ptr resource_does_not_exist = locator->locateResource("package://tesseract_common/"
144  "does_not_exist.txt");
145  EXPECT_TRUE(resource_does_not_exist != nullptr);
146  EXPECT_TRUE(resource_does_not_exist->getResourceContents().empty());
147  EXPECT_TRUE(resource_does_not_exist->getResourceContentStream() == nullptr);
148 }
149 
150 TEST(ResourceLocatorUnit, GeneralResourceLocatorUnit2) // NOLINT
151 {
152  using namespace tesseract_common;
153  std::filesystem::path file_path(__FILE__);
154  std::filesystem::path package_path = file_path.parent_path().parent_path();
155 
156 #ifndef _WIN32
157  std::string env_var = "ROS_PACKAGE_PATH=" + package_path.string();
158 #else
159  std::string env_var = "ROS_PACKAGE_PATH=" + package_path.string();
160 #endif
161  putenv(env_var.data());
162 
163  ResourceLocator::Ptr locator = std::make_shared<GeneralResourceLocator>();
164 
165  Resource::Ptr resource = locator->locateResource("package://tesseract_common/package.xml");
166  EXPECT_TRUE(resource != nullptr);
167  EXPECT_TRUE(resource->isFile());
168  EXPECT_EQ(resource->getUrl(), "package://tesseract_common/package.xml");
169  EXPECT_EQ(std::filesystem::path(resource->getFilePath()), (package_path / "package.xml"));
170  EXPECT_FALSE(resource->getResourceContents().empty());
171  EXPECT_TRUE(resource->getResourceContentStream() != nullptr);
172 
173  const std::string separator(1, std::filesystem::path::preferred_separator);
174  Resource::Ptr sub_resource = resource->locateResource("colcon.pkg");
175  EXPECT_TRUE(sub_resource != nullptr);
176  EXPECT_TRUE(sub_resource->isFile());
177  EXPECT_EQ(sub_resource->getUrl(), "package://tesseract_common" + separator + "colcon.pkg");
178  EXPECT_EQ(std::filesystem::path(sub_resource->getFilePath()), (package_path / "colcon.pkg"));
179  EXPECT_FALSE(sub_resource->getResourceContents().empty());
180  EXPECT_TRUE(sub_resource->getResourceContentStream() != nullptr);
181 
182  tesseract_common::Resource::Ptr sub_resource_empty = sub_resource->locateResource("");
183  EXPECT_TRUE(sub_resource_empty == nullptr);
184 
185  tesseract_common::Resource::Ptr resource_empty = locator->locateResource("");
186  EXPECT_TRUE(resource_empty == nullptr);
187 
188  tesseract_common::Resource::Ptr resource_does_not_exist = locator->locateResource("package://tesseract_common/"
189  "does_not_exist.txt");
190  EXPECT_TRUE(resource_does_not_exist != nullptr);
191  EXPECT_TRUE(resource_does_not_exist->getResourceContents().empty());
192  EXPECT_TRUE(resource_does_not_exist->getResourceContentStream() == nullptr);
193 }
194 
195 TEST(ResourceLocatorUnit, ByteResourceUnit) // NOLINT
196 {
197  using namespace tesseract_common;
198  std::filesystem::path file_path(__FILE__);
199  std::filesystem::path package_path = file_path.parent_path().parent_path();
200 
201  ResourceLocator::Ptr locator = std::make_shared<TestResourceLocator>();
202  Resource::Ptr resource = locator->locateResource("package://tesseract_common/package.xml");
203 
204  auto byte_resource = std::make_shared<BytesResource>(
205  "package://tesseract_common/package.xml", std::vector<uint8_t>({ 1, 2, 3, 4 }), resource);
206  EXPECT_TRUE(byte_resource != nullptr);
207  EXPECT_FALSE(byte_resource->isFile());
208  EXPECT_EQ(byte_resource->getUrl(), "package://tesseract_common/package.xml");
209  EXPECT_TRUE(byte_resource->getFilePath().empty());
210  EXPECT_FALSE(byte_resource->getResourceContents().empty());
211  EXPECT_TRUE(byte_resource->getResourceContentStream() != nullptr);
212 
213  const std::string separator(1, std::filesystem::path::preferred_separator);
214  Resource::Ptr sub_resource = byte_resource->locateResource("colcon.pkg");
215  EXPECT_TRUE(sub_resource != nullptr);
216  EXPECT_TRUE(sub_resource->isFile());
217  EXPECT_EQ(sub_resource->getUrl(), "package://tesseract_common" + separator + "colcon.pkg");
218  EXPECT_EQ(std::filesystem::path(sub_resource->getFilePath()), (package_path / "colcon.pkg"));
219  EXPECT_FALSE(sub_resource->getResourceContents().empty());
220  EXPECT_TRUE(sub_resource->getResourceContentStream() != nullptr);
221 
222  tesseract_common::Resource::Ptr resource_empty = byte_resource->locateResource("");
223  EXPECT_TRUE(resource_empty == nullptr);
224 
225  tesseract_common::Resource::Ptr resource_does_not_exist = byte_resource->locateResource("package://tesseract_common/"
226  "does_not_exist.txt");
227  EXPECT_TRUE(resource_does_not_exist != nullptr);
228  EXPECT_TRUE(resource_does_not_exist->getResourceContents().empty());
229  EXPECT_TRUE(resource_does_not_exist->getResourceContentStream() == nullptr);
230 }
231 
232 TEST(ResourceLocatorUnit, SimpleLocatedResourceSerializUnit) // NOLINT
233 {
234  using namespace tesseract_common;
235  SimpleLocatedResource resource("url", "file_path");
236  tesseract_common::testSerialization<SimpleLocatedResource>(resource, "SimpleLocatedResource");
237 }
238 
239 TEST(ResourceLocatorUnit, BytesResourceSerializUnit) // NOLINT
240 {
241  using namespace tesseract_common;
242  BytesResource resource("url", { 1, 2, 3, 4, 5 });
243  tesseract_common::testSerialization<BytesResource>(resource, "BytesResource");
244 }
245 
246 int main(int argc, char** argv)
247 {
248  testing::InitGoogleTest(&argc, argv);
249 
250  return RUN_ALL_TESTS();
251 }
TestResourceLocator::~TestResourceLocator
~TestResourceLocator() override=default
tesseract_common
Definition: allowed_collision_matrix.h:19
types.h
Common Tesseract Types.
TestResourceLocator
Resource locator implementation using a provided function to locate file resources.
Definition: resource_locator_unit.cpp:30
unit_test_utils.h
Common Tesseract unit test utilities.
macros.h
Common Tesseract Macros.
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
Definition: macros.h:71
findSeparator
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH TESSERACT_COMMON_IGNORE_WARNINGS_POP std::size_t findSeparator(const std::string &str)
Definition: resource_locator_unit.cpp:12
TestResourceLocator::locateResource
tesseract_common::Resource::Ptr locateResource(const std::string &url) const override final
Locate a resource based on a URL.
Definition: resource_locator_unit.cpp:38
tesseract_common::SimpleLocatedResource
Resource implementation for a local file.
Definition: resource_locator.h:201
TEST
TEST(ResourceLocatorUnit, SimpleResourceLocatorUnit)
Definition: resource_locator_unit.cpp:67
main
int main(int argc, char **argv)
Definition: resource_locator_unit.cpp:246
tesseract_common::ResourceLocator::ConstPtr
std::shared_ptr< const ResourceLocator > ConstPtr
Definition: resource_locator.h:57
tesseract_common::ResourceLocator
Abstract class for resource loaders.
Definition: resource_locator.h:53
TESSERACT_COMMON_IGNORE_WARNINGS_POP
#define TESSERACT_COMMON_IGNORE_WARNINGS_POP
Definition: macros.h:72
tesseract_common::BytesResource
Definition: resource_locator.h:249
resource_locator.h
Locate and retrieve resource data.
tesseract_common::Resource::Ptr
std::shared_ptr< Resource > Ptr
Definition: resource_locator.h:150
tesseract_common::ResourceLocator::Ptr
std::shared_ptr< ResourceLocator > Ptr
Definition: resource_locator.h:56


tesseract_common
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:01:40