configuration_test.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "configuration_test.h"
  19. #include "common/configuration.h"
  20. #include "common/configuration_loader.h"
  21. #include <gmock/gmock.h>
  22. #include <cstdio>
  23. #include <fstream>
  24. using ::testing::_;
  25. using namespace hdfs;
  26. namespace hdfs {
  27. TEST(ConfigurationTest, TestDegenerateInputs) {
  28. /* Completely empty stream */
  29. {
  30. std::stringstream stream;
  31. ConfigurationLoader config_loader;
  32. config_loader.ClearSearchPath();
  33. optional<Configuration> config = config_loader.Load<Configuration>("");
  34. EXPECT_FALSE(config && "Empty stream");
  35. }
  36. /* No values */
  37. {
  38. std::string data = "<configuration></configuration>";
  39. ConfigurationLoader config_loader;
  40. config_loader.ClearSearchPath();
  41. optional<Configuration> config = config_loader.Load<Configuration>(data);
  42. EXPECT_TRUE(config && "Blank config");
  43. }
  44. /* Extraneous values */
  45. {
  46. std::string data = "<configuration><spam></spam></configuration>";
  47. ConfigurationLoader config_loader;
  48. config_loader.ClearSearchPath();
  49. optional<Configuration> config = config_loader.Load<Configuration>(data);
  50. EXPECT_TRUE(config && "Extraneous values");
  51. }
  52. }
  53. TEST(ConfigurationTest, TestBasicOperations) {
  54. /* Single value */
  55. {
  56. std::stringstream stream;
  57. simpleConfigStream(stream, "key1", "value1");
  58. ConfigurationLoader config_loader;
  59. config_loader.ClearSearchPath();
  60. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  61. EXPECT_TRUE(config && "Parse single value");
  62. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  63. }
  64. /* Multiple values */
  65. {
  66. optional<Configuration> config =
  67. simpleConfig("key1", "value1", "key2", "value2");
  68. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  69. EXPECT_EQ("value2", config->GetWithDefault("key2", ""));
  70. }
  71. /* Case-insensitive */
  72. {
  73. std::stringstream stream;
  74. simpleConfigStream(stream, "key1", "value1");
  75. ConfigurationLoader config_loader;
  76. config_loader.ClearSearchPath();
  77. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  78. EXPECT_TRUE(config && "Parse single value");
  79. EXPECT_EQ("value1", config->GetWithDefault("KEY1", ""));
  80. }
  81. /* No defaults */
  82. {
  83. std::stringstream stream;
  84. simpleConfigStream(stream, "key1", "value1");
  85. ConfigurationLoader config_loader;
  86. config_loader.ClearSearchPath();
  87. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  88. EXPECT_TRUE(config && "Parse single value");
  89. optional<std::string> value = config->Get("key1");
  90. EXPECT_TRUE((bool)value);
  91. EXPECT_EQ("value1", *value);
  92. EXPECT_FALSE(config->Get("key2"));
  93. }
  94. }
  95. TEST(ConfigurationTest, TestCompactValues) {
  96. {
  97. std::stringstream stream;
  98. stream << "<configuration><property name=\"key1\" "
  99. "value=\"value1\"/></configuration>";
  100. ConfigurationLoader config_loader;
  101. config_loader.ClearSearchPath();
  102. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  103. EXPECT_TRUE(config && "Compact value parse");
  104. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  105. }
  106. }
  107. TEST(ConfigurationTest, TestMultipleResources) {
  108. /* Single value */
  109. {
  110. std::stringstream stream;
  111. simpleConfigStream(stream, "key1", "value1");
  112. ConfigurationLoader config_loader;
  113. config_loader.ClearSearchPath();
  114. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  115. EXPECT_TRUE(config && "Parse first stream");
  116. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  117. std::stringstream stream2;
  118. simpleConfigStream(stream2, "key2", "value2");
  119. optional<Configuration> config2 =
  120. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  121. EXPECT_TRUE(config2 && "Parse second stream");
  122. EXPECT_EQ("value1", config2->GetWithDefault("key1", ""));
  123. EXPECT_EQ("value2", config2->GetWithDefault("key2", ""));
  124. }
  125. }
  126. TEST(ConfigurationTest, TestStringResource) {
  127. /* Single value */
  128. {
  129. std::stringstream stream;
  130. simpleConfigStream(stream, "key1", "value1");
  131. std::string str = stream.str();
  132. ConfigurationLoader config_loader;
  133. config_loader.ClearSearchPath();
  134. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  135. EXPECT_TRUE(config && "Parse single value");
  136. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  137. }
  138. }
  139. TEST(ConfigurationTest, TestValueOverlay) {
  140. /* Incremental updates */
  141. {
  142. ConfigurationLoader loader;
  143. std::stringstream stream;
  144. stream << "<configuration>"
  145. "<property><name>key1</name><value>value1</value><final>false</final></property>"
  146. "<property><name>final2</name><value>value2</value><final>true</final></property>"
  147. "</configuration>";
  148. optional<Configuration> config = loader.Load<Configuration>(stream.str());
  149. EXPECT_TRUE(config && "Parse single value");
  150. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  151. EXPECT_EQ("value2", config->GetWithDefault("final2", ""));
  152. config = loader.OverlayValue(config.value(), "key3", "value3");
  153. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  154. EXPECT_EQ("value2", config->GetWithDefault("final2", ""));
  155. EXPECT_EQ("value3", config->GetWithDefault("key3", ""));
  156. config = loader.OverlayValue(config.value(), "final2", "value4");
  157. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  158. EXPECT_EQ("value2", config->GetWithDefault("final2", ""));
  159. EXPECT_EQ("value3", config->GetWithDefault("key3", ""));
  160. // Case insensitive overlay
  161. config = loader.OverlayValue(config.value(), "KEY3", "value3a");
  162. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  163. EXPECT_EQ("value2", config->GetWithDefault("final2", ""));
  164. EXPECT_EQ("value3a", config->GetWithDefault("key3", ""));
  165. }
  166. }
  167. TEST(ConfigurationTest, TestFinal) {
  168. {
  169. /* Explicitly non-final non-compact value */
  170. std::stringstream stream;
  171. stream << "<configuration><property><name>key1</name><value>value1</"
  172. "value><final>false</final></property></configuration>";
  173. ConfigurationLoader config_loader;
  174. config_loader.ClearSearchPath();
  175. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  176. EXPECT_TRUE(config && "Parse first stream");
  177. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  178. std::stringstream stream2;
  179. simpleConfigStream(stream2, "key1", "value2");
  180. optional<Configuration> config2 =
  181. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  182. EXPECT_TRUE(config2 && "Parse second stream");
  183. EXPECT_EQ("value2", config2->GetWithDefault("key1", ""));
  184. }
  185. {
  186. /* Explicitly final non-compact value */
  187. std::stringstream stream;
  188. stream << "<configuration><property><name>key1</name><value>value1</"
  189. "value><final>true</final></property></configuration>";
  190. ConfigurationLoader config_loader;
  191. config_loader.ClearSearchPath();
  192. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  193. EXPECT_TRUE(config && "Parse first stream");
  194. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  195. std::stringstream stream2;
  196. simpleConfigStream(stream2, "key1", "value2");
  197. optional<Configuration> config2 =
  198. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  199. EXPECT_TRUE(config2 && "Parse second stream");
  200. EXPECT_EQ("value1", config2->GetWithDefault("key1", ""));
  201. }
  202. {
  203. /* Explicitly non-final compact value */
  204. std::stringstream stream;
  205. stream << "<configuration><property name=\"key1\" value=\"value1\" "
  206. "final=\"false\"/></configuration>";
  207. ConfigurationLoader config_loader;
  208. config_loader.ClearSearchPath();
  209. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  210. EXPECT_TRUE(config && "Parse first stream");
  211. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  212. std::stringstream stream2;
  213. simpleConfigStream(stream2, "key1", "value2");
  214. optional<Configuration> config2 =
  215. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  216. EXPECT_TRUE(config2 && "Parse second stream");
  217. EXPECT_EQ("value2", config2->GetWithDefault("key1", ""));
  218. }
  219. {
  220. /* Explicitly final compact value */
  221. std::stringstream stream;
  222. stream << "<configuration><property name=\"key1\" value=\"value1\" "
  223. "final=\"true\"/></configuration>";
  224. ConfigurationLoader config_loader;
  225. config_loader.ClearSearchPath();
  226. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  227. EXPECT_TRUE(config && "Parse first stream");
  228. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  229. std::stringstream stream2;
  230. simpleConfigStream(stream2, "key1", "value2");
  231. optional<Configuration> config2 =
  232. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  233. EXPECT_TRUE(config2 && "Parse second stream");
  234. EXPECT_EQ("value1", config2->GetWithDefault("key1", ""));
  235. }
  236. {
  237. /* Bogus final value */
  238. std::stringstream stream;
  239. stream << "<configuration><property><name>key1</name><value>value1</"
  240. "value><final>spam</final></property></configuration>";
  241. ConfigurationLoader config_loader;
  242. config_loader.ClearSearchPath();
  243. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  244. EXPECT_TRUE(config && "Parse first stream");
  245. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  246. std::stringstream stream2;
  247. simpleConfigStream(stream2, "key1", "value2");
  248. optional<Configuration> config2 =
  249. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  250. EXPECT_TRUE(config2 && "Parse second stream");
  251. EXPECT_EQ("value2", config2->GetWithDefault("key1", ""));
  252. }
  253. {
  254. /* Blank final value */
  255. std::stringstream stream;
  256. stream << "<configuration><property><name>key1</name><value>value1</"
  257. "value><final></final></property></configuration>";
  258. ConfigurationLoader config_loader;
  259. config_loader.ClearSearchPath();
  260. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  261. EXPECT_TRUE(config && "Parse first stream");
  262. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  263. std::stringstream stream2;
  264. simpleConfigStream(stream2, "key1", "value2");
  265. optional<Configuration> config2 =
  266. ConfigurationLoader().OverlayResourceString(config.value(), stream2.str());
  267. EXPECT_TRUE(config2 && "Parse second stream");
  268. EXPECT_EQ("value2", config2->GetWithDefault("key1", ""));
  269. }
  270. }
  271. TEST(ConfigurationTest, TestFileReads)
  272. {
  273. // Single stream
  274. {
  275. TempFile tempFile;
  276. writeSimpleConfig(tempFile.filename, "key1", "value1");
  277. ConfigurationLoader config_loader;
  278. config_loader.ClearSearchPath();
  279. optional<Configuration> config = config_loader.LoadFromFile<Configuration>(tempFile.filename);
  280. EXPECT_TRUE(config && "Parse first stream");
  281. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  282. }
  283. // Multiple files
  284. {
  285. TempFile tempFile;
  286. writeSimpleConfig(tempFile.filename, "key1", "value1");
  287. ConfigurationLoader loader;
  288. optional<Configuration> config = loader.LoadFromFile<Configuration>(tempFile.filename);
  289. ASSERT_TRUE(config && "Parse first stream");
  290. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  291. TempFile tempFile2;
  292. writeSimpleConfig(tempFile2.filename, "key2", "value2");
  293. optional<Configuration> config2 = loader.OverlayResourceFile(*config, tempFile2.filename);
  294. ASSERT_TRUE(config2 && "Parse second stream");
  295. EXPECT_EQ("value1", config2->GetWithDefault("key1", ""));
  296. EXPECT_EQ("value2", config2->GetWithDefault("key2", ""));
  297. }
  298. // Try to add a directory
  299. {
  300. TempDir tempDir;
  301. ConfigurationLoader config_loader;
  302. config_loader.ClearSearchPath();
  303. optional<Configuration> config = config_loader.LoadFromFile<Configuration>(tempDir.path);
  304. EXPECT_FALSE(config && "Add directory as file resource");
  305. }
  306. // Search path splitting
  307. {
  308. ConfigurationLoader loader;
  309. loader.SetSearchPath("foo:/bar:baz/:/fioux/:/bar/bar/bong");
  310. // Paths will have / appended to them if not already present
  311. EXPECT_EQ("foo/:/bar/:baz/:/fioux/:/bar/bar/bong/", loader.GetSearchPath());
  312. }
  313. // Search path
  314. {
  315. TempDir tempDir1;
  316. TempFile tempFile1(tempDir1.path + "/file1.xml");
  317. writeSimpleConfig(tempFile1.filename, "key1", "value1");
  318. TempDir tempDir2;
  319. TempFile tempFile2(tempDir2.path + "/file2.xml");
  320. writeSimpleConfig(tempFile2.filename, "key2", "value2");
  321. TempDir tempDir3;
  322. TempFile tempFile3(tempDir3.path + "/file3.xml");
  323. writeSimpleConfig(tempFile3.filename, "key3", "value3");
  324. ConfigurationLoader loader;
  325. loader.SetSearchPath(tempDir1.path + ":" + tempDir2.path + ":" + tempDir3.path);
  326. optional<Configuration> config1 = loader.LoadFromFile<Configuration>("file1.xml");
  327. EXPECT_TRUE(config1 && "Parse first stream");
  328. optional<Configuration> config2 = loader.OverlayResourceFile(*config1, "file2.xml");
  329. EXPECT_TRUE(config2 && "Parse second stream");
  330. optional<Configuration> config3 = loader.OverlayResourceFile(*config2, "file3.xml");
  331. EXPECT_TRUE(config3 && "Parse third stream");
  332. EXPECT_EQ("value1", config3->GetWithDefault("key1", ""));
  333. EXPECT_EQ("value2", config3->GetWithDefault("key2", ""));
  334. EXPECT_EQ("value3", config3->GetWithDefault("key3", ""));
  335. }
  336. }
  337. TEST(ConfigurationTest, TestDefaultConfigs) {
  338. // Search path
  339. {
  340. TempDir tempDir;
  341. TempFile coreSite(tempDir.path + "/core-site.xml");
  342. writeSimpleConfig(coreSite.filename, "key1", "value1");
  343. ConfigurationLoader loader;
  344. loader.SetSearchPath(tempDir.path);
  345. optional<Configuration> config = loader.LoadDefaultResources<Configuration>();
  346. EXPECT_TRUE(config && "Parse streams");
  347. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  348. }
  349. }
  350. TEST(ConfigurationTest, TestIntConversions) {
  351. /* No defaults */
  352. {
  353. std::stringstream stream;
  354. simpleConfigStream(stream, "key1", "1");
  355. ConfigurationLoader config_loader;
  356. config_loader.ClearSearchPath();
  357. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  358. EXPECT_TRUE(config && "Parse single value");
  359. optional<int64_t> value = config->GetInt("key1");
  360. EXPECT_TRUE((bool)value);
  361. EXPECT_EQ(1, *value);
  362. EXPECT_FALSE(config->GetInt("key2"));
  363. }
  364. {
  365. optional<Configuration> config = simpleConfig("key1", "1");
  366. EXPECT_EQ(1, config->GetIntWithDefault("key1", -1));
  367. }
  368. {
  369. optional<Configuration> config = simpleConfig("key1", "-100");
  370. EXPECT_EQ(-100, config->GetIntWithDefault("key1", -1));
  371. }
  372. {
  373. optional<Configuration> config = simpleConfig("key1", " 1 ");
  374. EXPECT_EQ(1, config->GetIntWithDefault("key1", -1));
  375. }
  376. {
  377. optional<Configuration> config = simpleConfig("key1", "");
  378. EXPECT_EQ(-1, config->GetIntWithDefault("key1", -1));
  379. }
  380. {
  381. optional<Configuration> config = simpleConfig("key1", "spam");
  382. EXPECT_EQ(-1, config->GetIntWithDefault("key1", -1));
  383. }
  384. {
  385. optional<Configuration> config = simpleConfig("key2", "");
  386. EXPECT_EQ(-1, config->GetIntWithDefault("key1", -1));
  387. }
  388. }
  389. TEST(ConfigurationTest, TestDoubleConversions) {
  390. /* No defaults */
  391. {
  392. std::stringstream stream;
  393. simpleConfigStream(stream, "key1", "1");
  394. ConfigurationLoader config_loader;
  395. config_loader.ClearSearchPath();
  396. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  397. EXPECT_TRUE(config && "Parse single value");
  398. optional<double> value = config->GetDouble("key1");
  399. EXPECT_TRUE((bool)value);
  400. EXPECT_EQ(1, *value);
  401. EXPECT_FALSE(config->GetDouble("key2"));
  402. }
  403. {
  404. optional<Configuration> config = simpleConfig("key1", "1");
  405. EXPECT_EQ(1, config->GetDoubleWithDefault("key1", -1));
  406. }
  407. {
  408. optional<Configuration> config = simpleConfig("key1", "-100");
  409. EXPECT_EQ(-100, config->GetDoubleWithDefault("key1", -1));
  410. }
  411. {
  412. optional<Configuration> config = simpleConfig("key1", " 1 ");
  413. EXPECT_EQ(1, config->GetDoubleWithDefault("key1", -1));
  414. }
  415. {
  416. optional<Configuration> config = simpleConfig("key1", "");
  417. EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1));
  418. }
  419. {
  420. optional<Configuration> config = simpleConfig("key1", "spam");
  421. EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1));
  422. }
  423. {
  424. optional<Configuration> config = simpleConfig("key2", "");
  425. EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1));
  426. }
  427. { /* Out of range */
  428. optional<Configuration> config = simpleConfig("key2", "1e9999");
  429. EXPECT_EQ(-1, config->GetDoubleWithDefault("key1", -1));
  430. }
  431. }
  432. TEST(ConfigurationTest, TestBoolConversions) {
  433. /* No defaults */
  434. {
  435. std::stringstream stream;
  436. simpleConfigStream(stream, "key1", "true");
  437. ConfigurationLoader config_loader;
  438. config_loader.ClearSearchPath();
  439. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  440. EXPECT_TRUE(config && "Parse single value");
  441. optional<bool> value = config->GetBool("key1");
  442. EXPECT_TRUE((bool)value);
  443. EXPECT_EQ(true, *value);
  444. EXPECT_FALSE(config->GetBool("key2"));
  445. }
  446. {
  447. optional<Configuration> config = simpleConfig("key1", "true");
  448. EXPECT_EQ(true, config->GetBoolWithDefault("key1", false));
  449. }
  450. {
  451. optional<Configuration> config = simpleConfig("key1", "tRuE");
  452. EXPECT_EQ(true, config->GetBoolWithDefault("key1", false));
  453. }
  454. {
  455. optional<Configuration> config = simpleConfig("key1", "false");
  456. EXPECT_FALSE(config->GetBoolWithDefault("key1", true));
  457. }
  458. {
  459. optional<Configuration> config = simpleConfig("key1", "FaLsE");
  460. EXPECT_FALSE(config->GetBoolWithDefault("key1", true));
  461. }
  462. {
  463. optional<Configuration> config = simpleConfig("key1", " FaLsE ");
  464. EXPECT_FALSE(config->GetBoolWithDefault("key1", true));
  465. }
  466. {
  467. optional<Configuration> config = simpleConfig("key1", "");
  468. EXPECT_EQ(true, config->GetBoolWithDefault("key1", true));
  469. }
  470. {
  471. optional<Configuration> config = simpleConfig("key1", "spam");
  472. EXPECT_EQ(true, config->GetBoolWithDefault("key1", true));
  473. }
  474. {
  475. optional<Configuration> config = simpleConfig("key1", "");
  476. EXPECT_EQ(true, config->GetBoolWithDefault("key2", true));
  477. }
  478. }
  479. TEST(ConfigurationTest, TestUriConversions) {
  480. /* No defaults */
  481. {
  482. std::stringstream stream;
  483. simpleConfigStream(stream, "key1", "hdfs:///");
  484. ConfigurationLoader config_loader;
  485. config_loader.ClearSearchPath();
  486. optional<Configuration> config = config_loader.Load<Configuration>(stream.str());
  487. EXPECT_TRUE(config && "Parse single value");
  488. optional<URI> value = config->GetUri("key1");
  489. EXPECT_TRUE((bool)value);
  490. EXPECT_EQ("hdfs:///", value->str());
  491. EXPECT_FALSE(config->GetUri("key2"));
  492. }
  493. {
  494. optional<Configuration> config = simpleConfig("key1", "hdfs:///");
  495. EXPECT_EQ("hdfs:///", config->GetUriWithDefault("key1", "http:///").str());
  496. }
  497. {
  498. optional<Configuration> config = simpleConfig("key1", " hdfs:/// ");
  499. EXPECT_EQ("hdfs:///", config->GetUriWithDefault("key1", "http:///").str());
  500. }
  501. {
  502. optional<Configuration> config = simpleConfig("key1", "");
  503. EXPECT_EQ("", config->GetUriWithDefault("key1", "http:///").str());
  504. }
  505. {
  506. optional<Configuration> config = simpleConfig("key1", "%%"); // invalid URI
  507. EXPECT_EQ("http:///", config->GetUriWithDefault("key1", "http:///").str());
  508. }
  509. {
  510. optional<Configuration> config = simpleConfig("key2", "hdfs:///");
  511. EXPECT_EQ("http:///", config->GetUriWithDefault("key1", "http:///").str());
  512. }
  513. }
  514. int main(int argc, char *argv[]) {
  515. /*
  516. * The following line must be executed to initialize Google Mock
  517. * (and Google Test) before running the tests.
  518. */
  519. ::testing::InitGoogleMock(&argc, argv);
  520. return RUN_ALL_TESTS();
  521. }
  522. }