3 #include <gtest/gtest.h>
14 const size_t pos_slash = str.find(
'/');
15 const size_t pos_backslash = str.find(
'\\');
17 if (pos_slash != std::string::npos && pos_backslash != std::string::npos)
18 return std::min(pos_slash, pos_backslash);
20 if (pos_slash != std::string::npos)
23 if (pos_backslash != std::string::npos)
26 return std::string::npos;
33 using Ptr = std::shared_ptr<TestResourceLocator>;
34 using ConstPtr = std::shared_ptr<const TestResourceLocator>;
40 std::string mod_url = url;
41 if (url.find(
"package://tesseract_common") == 0)
43 mod_url.erase(0, strlen(
"package://tesseract_common"));
45 if (pos == std::string::npos)
48 mod_url.erase(0, pos);
50 std::filesystem::path file_path(__FILE__);
51 std::string package_path = file_path.parent_path().parent_path().string();
53 if (package_path.empty())
56 mod_url = package_path + mod_url;
59 if (!std::filesystem::path(mod_url).is_absolute())
62 return std::make_shared<tesseract_common::SimpleLocatedResource>(
63 url, mod_url, std::make_shared<TestResourceLocator>(*
this));
67 TEST(ResourceLocatorUnit, SimpleResourceLocatorUnit)
70 std::filesystem::path file_path(__FILE__);
71 std::filesystem::path package_path = file_path.parent_path().parent_path();
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);
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);
93 EXPECT_TRUE(sub_resource_empty ==
nullptr);
96 EXPECT_TRUE(resource_empty ==
nullptr);
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);
105 TEST(ResourceLocatorUnit, GeneralResourceLocatorUnit1)
108 std::filesystem::path file_path(__FILE__);
109 std::filesystem::path package_path = file_path.parent_path().parent_path();
112 std::string env_var =
"TESSERACT_RESOURCE_PATH=" + package_path.string();
114 std::string env_var =
"TESSERACT_RESOURCE_PATH=" + package_path.string();
116 putenv(env_var.data());
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);
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);
138 EXPECT_TRUE(sub_resource_empty ==
nullptr);
141 EXPECT_TRUE(resource_empty ==
nullptr);
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);
150 TEST(ResourceLocatorUnit, GeneralResourceLocatorUnit2)
153 std::filesystem::path file_path(__FILE__);
154 std::filesystem::path package_path = file_path.parent_path().parent_path();
157 std::string env_var =
"ROS_PACKAGE_PATH=" + package_path.string();
159 std::string env_var =
"ROS_PACKAGE_PATH=" + package_path.string();
161 putenv(env_var.data());
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);
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);
183 EXPECT_TRUE(sub_resource_empty ==
nullptr);
186 EXPECT_TRUE(resource_empty ==
nullptr);
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);
195 TEST(ResourceLocatorUnit, ByteResourceUnit)
198 std::filesystem::path file_path(__FILE__);
199 std::filesystem::path package_path = file_path.parent_path().parent_path();
202 Resource::Ptr resource = locator->locateResource(
"package://tesseract_common/package.xml");
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);
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);
223 EXPECT_TRUE(resource_empty ==
nullptr);
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);
232 TEST(ResourceLocatorUnit, SimpleLocatedResourceSerializUnit)
236 tesseract_common::testSerialization<SimpleLocatedResource>(resource,
"SimpleLocatedResource");
239 TEST(ResourceLocatorUnit, BytesResourceSerializUnit)
243 tesseract_common::testSerialization<BytesResource>(resource,
"BytesResource");
246 int main(
int argc,
char** argv)
248 testing::InitGoogleTest(&argc, argv);
250 return RUN_ALL_TESTS();